00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "filterlog.h"
00019
00020 FilterLog::FilterLog()
00021 {
00022
00023 config = KGlobal::config();
00024
00025
00026 loadSetup();
00027
00028
00029 load();
00030 }
00031
00032
00033 FilterLog::~FilterLog()
00034 {
00035 }
00036
00037 void FilterLog::addDeletedMail(const KDateTime & dateTime, const QString & sender, const QString & account, const QString & subject, KindOfMailDeleting kindDelete, const QString& filter )
00038 {
00039 if( logDeletedMails )
00040 addEntry( FActDelete, dateTime, sender, account, subject, "", kindDelete, filter );
00041 }
00042
00043 void FilterLog::addMovedMail(const KDateTime & dateTime, const QString & sender, const QString & account, const QString & subject, const QString & mailbox)
00044 {
00045 if( logMovedMails )
00046 addEntry( FActMove, dateTime, sender, account, subject, mailbox, DelFilter );
00047 }
00048
00049 void FilterLog::addEntry(FilterAction_Type action, const KDateTime & dateTime, const QString & sender, const QString & account, const QString & subject, const QString & mailbox, KindOfMailDeleting kindDelete, const QString& filter )
00050 {
00051
00052 FilterLogEntry entry = FilterLogEntry( action, dateTime, sender, account, subject, mailbox, kindDelete, filter );
00053
00054
00055 switch( action )
00056 {
00057 case FActDelete : listDeletedMails.append( entry ); break;
00058 case FActMove : listMovedMails.append( entry ); break;
00059 default : kdError() << "FilterLog::addEntry: Could not relate the following mail:" << endl;
00060 entry.print();
00061 break;
00062 }
00063 }
00064
00065 void FilterLog::print()
00066 {
00067 kdDebug() << "Log state:" << endl;
00068 kdDebug() << "----------" << endl;
00069
00070
00071 kdDebug() << "Deleted mails:" << endl;
00072 LogEntryList::iterator it;
00073 for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
00074 (*it).print();
00075
00076 kdDebug() << endl;
00077
00078
00079 kdDebug() << "Moved mails:" << endl;
00080 for ( it = listMovedMails.begin(); it != listMovedMails.end(); ++it )
00081 (*it).print();
00082 }
00083
00084 void FilterLog::clearDeletedMailsLog()
00085 {
00086 listDeletedMails.clear();
00087 }
00088
00089 void FilterLog::clearMovedMailsLog()
00090 {
00091 listMovedMails.clear();
00092 }
00093
00094 void FilterLog::save()
00095 {
00096
00097 KDateTime minTimeDelete = KDateTime::currentLocalDateTime();
00098 minTimeDelete = minTimeDelete.addDays( daysStoreDeletedMails * -1 );
00099 KDateTime minTimeManualDelete = KDateTime::currentLocalDateTime();
00100 minTimeManualDelete = minTimeManualDelete.addDays( daysStoreManualDeletedMails * -1 );
00101
00102
00103 QDomDocument doc( LOG_DOCTYPE );
00104
00105
00106 QDomElement rootElem = doc.createElement( LOG_ROOT_ELEMENT );
00107 doc.appendChild( rootElem );
00108
00109
00110
00111 if( deletedMailsStorageMode != exit )
00112 {
00113 LogEntryList::iterator it;
00114 for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
00115 {
00116 if( (*it).getKindOfDeleting() == DelFilter ) {
00117
00118 if( (*it).getDate() >= minTimeDelete )
00119 (*it).save( doc, rootElem );
00120
00121 }
00122 }
00123 }
00124
00125
00126
00127 if( manualDeletedMailsStorageMode != exit )
00128 {
00129 LogEntryList::iterator it;
00130 for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
00131 {
00132 if( (*it).getKindOfDeleting() == DelManual ) {
00133 if( (*it).getDate() >= minTimeManualDelete )
00134 (*it).save( doc, rootElem );
00135
00136 }
00137 }
00138 }
00139
00140
00141
00142 QString filename = KStandardDirs::locateLocal( "appdata", LOG_FILE );
00143
00144
00145 QFile file( filename );
00146
00147 if ( file.open( QFile::WriteOnly ) )
00148 {
00149 QTextStream stream( &file );
00150 doc.save( stream, 2 );
00151 file.close();
00152 }
00153 else
00154 {
00155 KMessageBox::error( NULL, i18n( "Could not save the filter log." ) );
00156 }
00157 }
00158
00159 void FilterLog::load()
00160 {
00161
00162 KDateTime minTimeDeleted = KDateTime::currentLocalDateTime();
00163 minTimeDeleted = minTimeDeleted.addDays( daysStoreDeletedMails * -1 );
00164 KDateTime minTimeManualDeleted = KDateTime::currentLocalDateTime();
00165 minTimeManualDeleted = minTimeManualDeleted.addDays( daysStoreManualDeletedMails * -1 );
00166
00167
00168 QDomDocument doc( LOG_DOCTYPE );
00169
00170
00171 QString filename = KStandardDirs::locateLocal( "appdata", LOG_FILE );
00172
00173
00174 QFile file( filename );
00175
00176 if ( !file.open( QFile::ReadOnly ) )
00177 return;
00178
00179 if ( !doc.setContent( &file ) ) {
00180 file.close();
00181 return;
00182 }
00183
00184
00185 file.close();
00186
00187
00188 QDomElement docElem = doc.documentElement();
00189
00190
00191 if( docElem.tagName() != LOG_ROOT_ELEMENT ) return;
00192
00193 QDomNode n = docElem.firstChild();
00194 while( !n.isNull() )
00195 {
00196 QDomElement e = n.toElement();
00197 if( !e.isNull() )
00198 {
00199 if( e.tagName() == LOG_ENTRY_ELEMENT )
00200 {
00201
00202 KindOfMailDeleting kindDel;
00203 if( e.attribute( LOG_ENTRY_ATTRIBUTE_KIND_DELETE ) == LOG_ENTRY_VALUE_KIND_DELETE_MANUAL ) {
00204 kindDel = DelManual;
00205 } else {
00206 kindDel = DelFilter;
00207 }
00208
00209
00210
00211 KDateTime mailTime = KDateTime::fromString( e.attribute( LOG_ENTRY_ATTRIBUTE_DATETIME ), KDateTime::ISODate );
00212 if( kindDel == DelManual ) {
00213
00214 if( mailTime >= minTimeManualDeleted ) {
00215
00216
00217 addDeletedMail( mailTime,
00218 e.attribute( LOG_ENTRY_ATTRIBUTE_SENDER ),
00219 e.attribute( LOG_ENTRY_ATTRIBUTE_ACCOUNT ),
00220 e.attribute( LOG_ENTRY_ATTRIBUTE_SUBJECT ),
00221 kindDel
00222 );
00223 }
00224
00225 } else {
00226
00227 if( mailTime >= minTimeDeleted ) {
00228
00229
00230 addDeletedMail( mailTime,
00231 e.attribute( LOG_ENTRY_ATTRIBUTE_SENDER ),
00232 e.attribute( LOG_ENTRY_ATTRIBUTE_ACCOUNT ),
00233 e.attribute( LOG_ENTRY_ATTRIBUTE_SUBJECT ),
00234 kindDel,
00235 e.attribute( LOG_ENTRY_ATTRIBUTE_FILTER )
00236 );
00237 }
00238 }
00239
00240 }
00241 }
00242 n = n.nextSibling();
00243 }
00244 }
00245
00246 LogEntryList FilterLog::getDeletedMails( )
00247 {
00248 return listDeletedMails;
00249 }
00250
00251 LogEntryList FilterLog::getMovedMails( )
00252 {
00253 return listMovedMails;
00254 }
00255
00256 void FilterLog::loadSetup( )
00257 {
00258 KConfigGroup* configLog = new KConfigGroup( config, CONFIG_GROUP_LOG );
00259
00260 logDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_LOG_DELETED_MAILS, DEFAULT_LOG_LOG_DELETED_MAILS );
00261 logMovedMails = configLog->readEntry( CONFIG_ENTRY_LOG_LOG_MOVED_MAILS, DEFAULT_LOG_LOG_MOVED_MAILS );
00262 logManualDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_LOG_MANUAL_DELETED_MAILS, DEFAULT_LOG_LOG_MANUAL_DELETED_MAILS );
00263
00264 if( logDeletedMails )
00265 {
00266 QString storageMode = configLog->readEntry(CONFIG_ENTRY_LOG_REMOVE_DELETED_MAILS, DEFAULT_LOG_REMOVE_DELETED_MAILS );
00267 if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AT_EXIT )
00268 deletedMailsStorageMode = exit;
00269 else if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AFTER_DAYS )
00270 deletedMailsStorageMode = days;
00271 else
00272 deletedMailsStorageMode = days;
00273
00274 if( deletedMailsStorageMode == days )
00275 daysStoreDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_HOLDDAYS_DELETED_MAILS, DEFAULT_LOG_HOLDDAYS_DELETED_MAILS );
00276 else
00277 daysStoreDeletedMails = 7;
00278 }
00279 else
00280 {
00281 deletedMailsStorageMode = days;
00282 daysStoreDeletedMails = 7;
00283 }
00284
00285
00286 if( logManualDeletedMails )
00287 {
00288 QString storageMode = configLog->readEntry(CONFIG_ENTRY_LOG_REMOVE_MANUAL_DELETED_MAILS, DEFAULT_LOG_REMOVE_MANUAL_DELETED_MAILS );
00289 if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AT_EXIT )
00290 manualDeletedMailsStorageMode = exit;
00291 else if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AFTER_DAYS )
00292 manualDeletedMailsStorageMode = days;
00293 else
00294 manualDeletedMailsStorageMode = days;
00295
00296 if( manualDeletedMailsStorageMode == days )
00297 daysStoreManualDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_HOLDDAYS_MANUAL_DELETED_MAILS, DEFAULT_LOG_HOLDDAYS_MANUAL_DELETED_MAILS );
00298 else
00299 daysStoreManualDeletedMails = 7;
00300 }
00301 else
00302 {
00303 manualDeletedMailsStorageMode = days;
00304 daysStoreManualDeletedMails = 7;
00305 }
00306
00307 }
00308
00309 int FilterLog::numberDeletedMails( )
00310 {
00311 return listDeletedMails.count();
00312 }
00313
00314 int FilterLog::numberMovedMails( )
00315 {
00316 return listMovedMails.count();
00317 }
00318
00319 int FilterLog::numberFilterDeletedMails()
00320 {
00321 int ctr = 0;
00322
00323 QListIterator<FilterLogEntry> it( listDeletedMails );
00324
00325
00326 while( it.hasNext() )
00327 {
00328 FilterLogEntry entry = it.next();
00329
00330 if( entry.getKindOfDeleting() == DelFilter ) {
00331 ctr++;
00332 }
00333 }
00334
00335
00336 return ctr;
00337
00338 }
00339
00340