00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef AKONADI_CHANGERECORDER_P_H
00021 #define AKONADI_CHANGERECORDER_P_H
00022
00023 #include "akonadiprivate_export.h"
00024 #include "monitor_p.h"
00025
00026 class AKONADI_TESTS_EXPORT Akonadi::ChangeRecorderPrivate : public Akonadi::MonitorPrivate
00027 {
00028 public:
00029 ChangeRecorderPrivate( ChangeRecorder* parent ) :
00030 MonitorPrivate( parent ),
00031 settings( 0 ),
00032 enableChangeRecording( true )
00033 {
00034 }
00035
00036 Q_DECLARE_PUBLIC( ChangeRecorder )
00037 QSettings *settings;
00038 bool enableChangeRecording;
00039
00040 virtual int pipelineSize() const
00041 {
00042 if ( enableChangeRecording )
00043 return 0;
00044 return MonitorPrivate::pipelineSize();
00045 }
00046
00047 virtual void slotNotify( const NotificationMessage::List &msgs )
00048 {
00049 Q_Q( ChangeRecorder );
00050 const int oldChanges = pendingNotifications.size();
00051 MonitorPrivate::slotNotify( msgs );
00052 if ( enableChangeRecording && pendingNotifications.size() != oldChanges ) {
00053 saveNotifications();
00054 emit q->changesAdded();
00055 }
00056 }
00057
00058 void loadNotifications()
00059 {
00060 pendingNotifications.clear();
00061
00062 const QString changesFileName = settings->fileName() + QLatin1String( "_changes.dat" );
00063
00070 if ( !QFile::exists( changesFileName ) ) {
00071 QStringList list;
00072 settings->beginGroup( QLatin1String( "ChangeRecorder" ) );
00073 const int size = settings->beginReadArray( QLatin1String( "change" ) );
00074
00075 for ( int i = 0; i < size; ++i ) {
00076 settings->setArrayIndex( i );
00077 NotificationMessage msg;
00078 msg.setSessionId( settings->value( QLatin1String( "sessionId" ) ).toByteArray() );
00079 msg.setType( (NotificationMessage::Type)settings->value( QLatin1String( "type" ) ).toInt() );
00080 msg.setOperation( (NotificationMessage::Operation)settings->value( QLatin1String( "op" ) ).toInt() );
00081 msg.setUid( settings->value( QLatin1String( "uid" ) ).toLongLong() );
00082 msg.setRemoteId( settings->value( QLatin1String( "rid" ) ).toString() );
00083 msg.setResource( settings->value( QLatin1String( "resource" ) ).toByteArray() );
00084 msg.setParentCollection( settings->value( QLatin1String( "parentCol" ) ).toLongLong() );
00085 msg.setParentDestCollection( settings->value( QLatin1String( "parentDestCol" ) ).toLongLong() );
00086 msg.setMimeType( settings->value( QLatin1String( "mimeType" ) ).toString() );
00087 list = settings->value( QLatin1String( "itemParts" ) ).toStringList();
00088 QSet<QByteArray> itemParts;
00089 Q_FOREACH( const QString &entry, list )
00090 itemParts.insert( entry.toLatin1() );
00091 msg.setItemParts( itemParts );
00092 pendingNotifications << msg;
00093 }
00094
00095 settings->endArray();
00096
00097
00098 saveNotifications();
00099
00100
00101 settings->remove( QString() );
00102 settings->endGroup();
00103
00104
00105 }
00106
00107 QFile file( changesFileName );
00108 if ( !file.open( QIODevice::ReadOnly ) )
00109 return;
00110
00111 QDataStream stream( &file );
00112 stream.setVersion( QDataStream::Qt_4_6 );
00113
00114 qulonglong size;
00115 QByteArray sessionId, resource;
00116 int type, operation;
00117 qlonglong uid, parentCollection, parentDestCollection;
00118 QString remoteId, mimeType;
00119 QSet<QByteArray> itemParts;
00120
00121 stream >> size;
00122 for ( qulonglong i = 0; i < size; ++i ) {
00123 NotificationMessage msg;
00124
00125 stream >> sessionId;
00126 stream >> type;
00127 stream >> operation;
00128 stream >> uid;
00129 stream >> remoteId;
00130 stream >> resource;
00131 stream >> parentCollection;
00132 stream >> parentDestCollection;
00133 stream >> mimeType;
00134 stream >> itemParts;
00135
00136 msg.setSessionId( sessionId );
00137 msg.setType( static_cast<NotificationMessage::Type>( type ) );
00138 msg.setOperation( static_cast<NotificationMessage::Operation>( operation ) );
00139 msg.setUid( uid );
00140 msg.setRemoteId( remoteId );
00141 msg.setResource( resource );
00142 msg.setParentCollection( parentCollection );
00143 msg.setParentDestCollection( parentDestCollection );
00144 msg.setMimeType( mimeType );
00145 msg.setItemParts( itemParts );
00146 pendingNotifications << msg;
00147 }
00148 }
00149
00150 void saveNotifications()
00151 {
00152 if ( !settings )
00153 return;
00154
00155 QFile file( settings->fileName() + QLatin1String( "_changes.dat" ) );
00156 QFileInfo info( file );
00157 if ( !QFile::exists( info.absolutePath() ) ) {
00158 QDir dir;
00159 dir.mkpath( info.absolutePath() );
00160 }
00161 if ( !file.open( QIODevice::WriteOnly ) ) {
00162 qWarning() << "could not save notifications to file " << file.fileName();
00163 return;
00164 }
00165
00166 QDataStream stream( &file );
00167 stream.setVersion( QDataStream::Qt_4_6 );
00168
00169 stream << (qulonglong)pendingNotifications.count();
00170
00171 for ( int i = 0; i < pendingNotifications.count(); ++i ) {
00172 const NotificationMessage msg = pendingNotifications.at( i );
00173
00174 stream << msg.sessionId();
00175 stream << msg.type();
00176 stream << msg.operation();
00177 stream << msg.uid();
00178 stream << msg.remoteId();
00179 stream << msg.resource();
00180 stream << msg.parentCollection();
00181 stream << msg.parentDestCollection();
00182 stream << msg.mimeType();
00183 stream << msg.itemParts();
00184 }
00185
00186 file.close();
00187 }
00188 };
00189
00190 #endif