summaryrefslogtreecommitdiffstats
path: root/kshowmail/filterlog.cpp
blob: 38484cbe2cb5aba833677ada49e4287474178117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// C++ Implementation: filterlog
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "filterlog.h"

FilterLog::FilterLog()
{
  //get the application config object
  config = TDEApplication::kApplication()->config();

  //load the setup
  loadSetup();

  //load stored entries
  load();
}


FilterLog::~FilterLog()
{
}

void FilterLog::addDeletedMail(const TQDateTime & dateTime, const TQString & sender, const TQString & account, const TQString & subject)
{
  if( logDeletedMails )
    addEntry( FActDelete, dateTime, sender, account, subject, "" );
}

void FilterLog::addMovedMail(const TQDateTime & dateTime, const TQString & sender, const TQString & account, const TQString & subject, const TQString & mailbox)
{
  if( logMovedMails )
    addEntry( FActMove, dateTime, sender, account, subject, mailbox );
}

void FilterLog::addEntry(FilterAction_Type action, const TQDateTime & dateTime, const TQString & sender, const TQString & account, const TQString & subject, const TQString & mailbox)
{
  //create entry
  FilterLogEntry entry = FilterLogEntry( action, dateTime, sender, account, subject, mailbox );

  //add entry to the appropriate list
  switch( action )
  {
    case FActDelete   : listDeletedMails.append( entry ); break;
    case FActMove     : listMovedMails.append( entry ); break;
    default           : kdError( "FilterLog::addEntry: Could not relate the following mail:" );
                        entry.print();
                        break;
  }
}

void FilterLog::print()
{
  kdDebug() << "Log state:" << endl;
  kdDebug() << "----------" << endl;

  //print all entries about deleted mails
  kdDebug() << "Deleted mails:" << endl;
  LogEntryList::iterator it;
  for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
    (*it).print();

  kdDebug() << endl;

  //print all entries about moved mails
  kdDebug() << "Moved mails:" << endl;
  for ( it = listMovedMails.begin(); it != listMovedMails.end(); ++it )
    (*it).print();
}

void FilterLog::clearDeletedMailsLog()
{
  listDeletedMails.clear();
}

void FilterLog::clearMovedMailsLog()
{
  listMovedMails.clear();
}

void FilterLog::save()
{
  //maybe we have to remove old entries, calculate minimum date
  TQDateTime minTime = TQDateTime::currentDateTime();
  minTime = minTime.addDays( daysStoreDeletedMails * -1 );

  //we need a XML document
  TQDomDocument doc( LOG_DOCTYPE );

  //and a root element
  TQDomElement rootElem = doc.createElement( LOG_ROOT_ELEMENT );
  doc.appendChild( rootElem );

  //store the entries of the deleted mails list into the document
  //if the user want it
  if( deletedMailsStorageMode != exit )
  {
    LogEntryList::iterator it;
    for ( it = listDeletedMails.begin(); it != listDeletedMails.end(); ++it )
    {
      if( (*it).getDate() >= minTime )
        (*it).save( doc, rootElem );
    }
  }


  //get the name of the file to save
  TQString filename = locateLocal( "appdata", LOG_FILE );

  //and save
  TQFile file( filename );

  if ( file.open( IO_WriteOnly ) ) //open file
  {
    TQTextStream stream( &file );
    doc.save( stream, 2 );
    file.close();
  }
  else
  {
    KMessageBox::error( NULL, i18n( "Could not save the filter log." ) );
  }
}

void FilterLog::load()
{
  //maybe we have to remove old entries, calculate minimum date
  TQDateTime minTime = TQDateTime::currentDateTime();
  minTime = minTime.addDays( daysStoreDeletedMails * -1 );

  //we need a XML document
  TQDomDocument doc( LOG_DOCTYPE );

  //get the name of the file
  TQString filename = locateLocal( "appdata", LOG_FILE );

  //load the log from file into the DOM document
  TQFile file( filename );

  if ( !file.open( IO_ReadOnly ) )
    return;         //return, if the file can't opened

  if ( !doc.setContent( &file ) ) {
    file.close();   //return, if the content of the file is invalid
    return;
  }

  //the content was loaded, close the file
  file.close();

  //iterate over all DOM elements and generate the log entries
  TQDomElement docElem = doc.documentElement();  //get root element

  //return, if the root element is not LOG_ROOT_ELEMENT
  if( docElem.tagName() != LOG_ROOT_ELEMENT ) return;

  TQDomNode n = docElem.firstChild();  //get first child (this is the first log entry)
  while( !n.isNull() )
  {
    TQDomElement e = n.toElement();   //try to convert the node to an element.
    if( !e.isNull() )
    {
      if( e.tagName() == LOG_ENTRY_ELEMENT )
      {
        //add the read entry to the list of deleted mails
        TQDateTime mailTime = TQDateTime::fromString( e.attribute( LOG_ENTRY_ATTRIBUTE_DATETIME ), TQt::ISODate );
        if( mailTime >= minTime )
          addDeletedMail( mailTime,
                          e.attribute( LOG_ENTRY_ATTRIBUTE_SENDER ),
                          e.attribute( LOG_ENTRY_ATTRIBUTE_ACCOUNT ),
                          e.attribute( LOG_ENTRY_ATTRIBUTE_SUBJECT ) );
      }
    }
    n = n.nextSibling();            //get next child
  }
}

LogEntryList FilterLog::getDeletedMails( )
{
  return listDeletedMails;
}

LogEntryList FilterLog::getMovedMails( )
{
  return listMovedMails;
}

void FilterLog::loadSetup( )
{
  config->setGroup( CONFIG_GROUP_LOG );

  logDeletedMails = config->readBoolEntry( CONFIG_ENTRY_LOG_LOG_DELETED_MAILS, DEFAULT_LOG_LOG_DELETED_MAILS );
  logMovedMails = config->readBoolEntry( CONFIG_ENTRY_LOG_LOG_MOVED_MAILS, DEFAULT_LOG_LOG_MOVED_MAILS );

  if( logDeletedMails )
  {
    TQString storageMode = config->readEntry(CONFIG_ENTRY_LOG_REMOVE_DELETED_MAILS, DEFAULT_LOG_REMOVE_DELETED_MAILS );
    if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AT_EXIT )
      deletedMailsStorageMode = exit;
    else if( storageMode == CONFIG_VALUE_LOG_REMOVE_MAILS_AFTER_DAYS )
      deletedMailsStorageMode = days;
    else
      deletedMailsStorageMode = days;

    if( deletedMailsStorageMode == days )
      daysStoreDeletedMails = config->readNumEntry( CONFIG_ENTRY_LOG_HOLDDAYS_DELETED_MAILS, DEFAULT_LOG_HOLDDAYS_DELETED_MAILS );
    else
      daysStoreDeletedMails = 7;
  }
  else
  {
    deletedMailsStorageMode = days;
    daysStoreDeletedMails = 7;
  }
}

int FilterLog::numberDeletedMails( )
{
  return listDeletedMails.count();
}

int FilterLog::numberMovedMails( )
{
  return listMovedMails.count();
}