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)
00038 {
00039 if( logDeletedMails )
00040 addEntry( FActDelete, dateTime, sender, account, subject, "" );
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 );
00047 }
00048
00049 void FilterLog::addEntry(FilterAction_Type action, const KDateTime & dateTime, const QString & sender, const QString & account, const QString & subject, const QString & mailbox)
00050 {
00051
00052 FilterLogEntry entry = FilterLogEntry( action, dateTime, sender, account, subject, mailbox );
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 minTime = KDateTime::currentLocalDateTime();
00098 minTime = minTime.addDays( daysStoreDeletedMails * -1 );
00099
00100
00101 QDomDocument doc( LOG_DOCTYPE );
00102
00103
00104 QDomElement rootElem = doc.createElement( LOG_ROOT_ELEMENT );
00105 doc.appendChild( rootElem );
00106
00107
00108
00109 if( deletedMailsStorageMode != exit )
00110 {
00111 LogEntryList::iterator it;
00112 for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
00113 {
00114 if( (*it).getDate() >= minTime )
00115 (*it).save( doc, rootElem );
00116 }
00117 }
00118
00119
00120
00121 QString filename = KStandardDirs::locateLocal( "appdata", LOG_FILE );
00122
00123
00124 QFile file( filename );
00125
00126 if ( file.open( QFile::WriteOnly ) )
00127 {
00128 QTextStream stream( &file );
00129 doc.save( stream, 2 );
00130 file.close();
00131 }
00132 else
00133 {
00134 KMessageBox::error( NULL, i18n( "Could not save the filter log." ) );
00135 }
00136 }
00137
00138 void FilterLog::load()
00139 {
00140
00141 KDateTime minTime = KDateTime::currentLocalDateTime();
00142 minTime = minTime.addDays( daysStoreDeletedMails * -1 );
00143
00144
00145 QDomDocument doc( LOG_DOCTYPE );
00146
00147
00148 QString filename = KStandardDirs::locateLocal( "appdata", LOG_FILE );
00149
00150
00151 QFile file( filename );
00152
00153 if ( !file.open( QFile::ReadOnly ) )
00154 return;
00155
00156 if ( !doc.setContent( &file ) ) {
00157 file.close();
00158 return;
00159 }
00160
00161
00162 file.close();
00163
00164
00165 QDomElement docElem = doc.documentElement();
00166
00167
00168 if( docElem.tagName() != LOG_ROOT_ELEMENT ) return;
00169
00170 QDomNode n = docElem.firstChild();
00171 while( !n.isNull() )
00172 {
00173 QDomElement e = n.toElement();
00174 if( !e.isNull() )
00175 {
00176 if( e.tagName() == LOG_ENTRY_ELEMENT )
00177 {
00178
00179 KDateTime mailTime = KDateTime::fromString( e.attribute( LOG_ENTRY_ATTRIBUTE_DATETIME ), KDateTime::ISODate );
00180 if( mailTime >= minTime )
00181 addDeletedMail( mailTime,
00182 e.attribute( LOG_ENTRY_ATTRIBUTE_SENDER ),
00183 e.attribute( LOG_ENTRY_ATTRIBUTE_ACCOUNT ),
00184 e.attribute( LOG_ENTRY_ATTRIBUTE_SUBJECT ) );
00185 }
00186 }
00187 n = n.nextSibling();
00188 }
00189 }
00190
00191 LogEntryList FilterLog::getDeletedMails( )
00192 {
00193 return listDeletedMails;
00194 }
00195
00196 LogEntryList FilterLog::getMovedMails( )
00197 {
00198 return listMovedMails;
00199 }
00200
00201 void FilterLog::loadSetup( )
00202 {
00203 KConfigGroup* configLog = new KConfigGroup( config, CONFIG_GROUP_LOG );
00204
00205 logDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_LOG_DELETED_MAILS, DEFAULT_LOG_LOG_DELETED_MAILS );
00206 logMovedMails = configLog->readEntry( CONFIG_ENTRY_LOG_LOG_MOVED_MAILS, DEFAULT_LOG_LOG_MOVED_MAILS );
00207
00208 if( logDeletedMails )
00209 {
00210 QString storageMode = configLog->readEntry(CONFIG_ENTRY_LOG_REMOVE_DELETED_MAILS, DEFAULT_LOG_REMOVE_DELETED_MAILS );
00211 if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AT_EXIT )
00212 deletedMailsStorageMode = exit;
00213 else if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AFTER_DAYS )
00214 deletedMailsStorageMode = days;
00215 else
00216 deletedMailsStorageMode = days;
00217
00218 if( deletedMailsStorageMode == days )
00219 daysStoreDeletedMails = configLog->readEntry( CONFIG_ENTRY_LOG_HOLDDAYS_DELETED_MAILS, DEFAULT_LOG_HOLDDAYS_DELETED_MAILS );
00220 else
00221 daysStoreDeletedMails = 7;
00222 }
00223 else
00224 {
00225 deletedMailsStorageMode = days;
00226 daysStoreDeletedMails = 7;
00227 }
00228 }
00229
00230 int FilterLog::numberDeletedMails( )
00231 {
00232 return listDeletedMails.count();
00233 }
00234
00235 int FilterLog::numberMovedMails( )
00236 {
00237 return listMovedMails.count();
00238 }
00239
00240
00241