summaryrefslogtreecommitdiffstats
path: root/kmail/kmpopfiltercnfrmdlg.cpp
blob: 3139aa32f9d7ff208ca322c831ad06ace165a729 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/***************************************************************************
                          kmpopheadersdlg.cpp
                             -------------------
    begin                : Mon Oct 22 2001
    copyright            : (C) 2001 by Heiko Hund
                                       Thorsten Zachmann
    email                : heiko@ist.eigentlich.net
                           T.Zachmann@zagge.de
 ***************************************************************************/

#include <config.h>
#include "kmpopfiltercnfrmdlg.h"
#include "kmheaders.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqheader.h>
#include <tqcheckbox.h>
#include <tqvgroupbox.h>
#include <tqtimer.h>

#include <klocale.h>
#include <kio/global.h>

#include <assert.h>

////////////////////////////////////////
///  view
KMPopHeadersView::KMPopHeadersView(TQWidget *aParent, KMPopFilterCnfrmDlg *aDialog)
      : KListView(aParent)
{
  mDialog=aDialog;
  int mDownIndex=addColumn(TQIconSet(TQPixmap(mDown)), TQString::null, 24);
  assert( mDownIndex == Down ); //This code relies on the fact that radiobuttons are the first three columns for easier Column-Action mapping
			        //it does not necessarily be true - you could redefine mapToColumn and mapToAction to eg. shift those numbers by 1
  addColumn(TQIconSet(TQPixmap(mLater)), TQString::null, 24);
  addColumn(TQIconSet(TQPixmap(mDel)), TQString::null, 24);

  /*int subjCol =*/ addColumn(i18n("Subject"), 180);
  /*int sendCol =*/ addColumn(i18n("Sender"), 150);
  /*int recvCol =*/ addColumn(i18n("Receiver"), 150);
  int dateCol = addColumn(i18n("Date"), 120);
  int sizeCol = addColumn(i18n("Size"), 80);

  setAllColumnsShowFocus(true);

  setColumnAlignment(Down, Qt::AlignHCenter);
  setColumnAlignment(Later, Qt::AlignHCenter);
  setColumnAlignment(Delete, Qt::AlignHCenter);
  setColumnAlignment(sizeCol, Qt::AlignRight);

  setSorting(dateCol, false);
  setShowSortIndicator(true);
  header()->setResizeEnabled(false, Down);
  header()->setResizeEnabled(false, Later);
  header()->setResizeEnabled(false, Delete);
  header()->setClickEnabled(false, Down);
  header()->setClickEnabled(false, Later);
  header()->setClickEnabled(false, Delete);

  //we rely on fixed column order, so we forbid this
  header()->setMovingEnabled(false);

  connect(this, TQT_SIGNAL(pressed(TQListViewItem*, const TQPoint&, int)),
        TQT_SLOT(slotPressed(TQListViewItem*, const TQPoint&, int)));
}

KMPopHeadersView::~KMPopHeadersView()
{
}

//Handle keystrokes - Left and Right key select previous/next action correspondingly
void KMPopHeadersView::keyPressEvent( TQKeyEvent *e )
{
    if (e->key() == Key_Left) {
	    KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>( selectedItem() );
	    if (item&&mDialog) {
		    if (item->action()) { //here we rely on the fact that the leftmost action is equal to 0!
			    item->setAction((KMPopFilterAction)((int)item->action()-1));
			    mDialog->setAction( selectedItem(), item->action());
		    }
	    }
    } else if (e->key() == Key_Right) {
	    KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>( selectedItem() );
	    if (item&&mDialog) {
		    if (item->action()<NoAction-1) { //here we rely on the fact that right most action is one less than NoAction!
			    item->setAction((KMPopFilterAction)((int)item->action()+1));
			    mDialog->setAction( selectedItem(), item->action());
		    }
	    }
    } else {
	    TQListView::keyPressEvent( e );
    }
}

void KMPopHeadersView::slotPressed(TQListViewItem* aItem, const TQPoint&, int aColumn) {
  if ( !( aItem && aColumn>=0 && aColumn<NoAction ) ) return;
  KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>(aItem);
  assert( item );
  item->setAction(mapToAction(aColumn));
}

const char *KMPopHeadersView::mUnchecked[26] = {
"19 16 9 1",
"  c None",
"# c #000000",
". c #ffffff",
"a c #4a4c4a",
"b c #524c52",
"c c #efefef",
"e c #fff2ff",
"f c #f6f2f6",
"g c #fff6ff",
"                   ",
"                   ",
"         aaaa      ",
"       ba####aa    ",
"      a##....aac   ",
"      a#......ec   ",
"     a#........fc  ",
"     a#........gc  ",
"     a#........fc  ",
"     b#........gc  ",
"      a#......gc   ",
"      age....gec   ",
"       ccfgfgcc    ",
"         cccc      ",
"                   ",
"                   ",};

const char *KMPopHeadersView::mChecked[26] = {
"19 16 9 1",
"  c None",
"# c #000000",
". c #ffffff",
"a c #4a4c4a",
"b c #524c52",
"c c #efefef",
"e c #fff2ff",
"f c #f6f2f6",
"g c #fff6ff",
"                   ",
"                   ",
"         aaaa      ",
"       ba####aa    ",
"      a##....aac   ",
"      a#......ec   ",
"     a#...##...fc  ",
"     a#..####..gc  ",
"     a#..####..fc  ",
"     b#...##...gc  ",
"      a#......gc   ",
"      age....gec   ",
"       ccfgfgcc    ",
"         cccc      ",
"                   ",
"                   ",};

const char *KMPopHeadersView::mLater[25] = {
"16 16 8 1",
". c None",
"g c #303030",
"c c #585858",
"f c #a0a0a0",
"b c #c0c000",
"e c #dcdcdc",
"a c #ffff00",
"d c #ffffff",
"................",
"...........eaa..",
"..........eaaa..",
".........ebaab..",
".........eaaa...",
"........eaaab...",
"........eaaa....",
".......eaaab....",
"eaae..ebaccccc..",
"eaaae.eaacdedc..",
"ebaaabaaabcdc...",
".ebaaaaaa.fgf...",
"..ebaaaa..cec...",
"...ebaab.cdedc..",
"........eccccc..",
"................"};

const char *KMPopHeadersView::mDown[20] = {
"16 16 3 1",
". c None",
"a c #008000",
"b c #00c000",
"................",
"............aa..",
"...........aaa..",
"..........baab..",
"..........aaa...",
".........baab...",
".........aaa....",
"........aaab....",
".aa....baaa.....",
".aaa...aaa......",
".baaabaaab......",
"..baaaaaa.......",
"...baaaa........",
"....baab........",
"................",
"................"};

const char *KMPopHeadersView::mDel[19] = {
"16 16 2 1",
". c None",
"# c #c00000",
"................",
"................",
"..##.......##...",
"..###.....###...",
"...###...###....",
"....###.###.....",
".....#####......",
"......###.......",
".....#####......",
"....###.###.....",
"...###...###....",
"..###.....###...",
"..##.......##...",
"................",
"................",
"................"};


/////////////////////////////////////////
/////////////////////////////////////////
///  viewitem
/////////////////////////////////////////
/////////////////////////////////////////
KMPopHeadersViewItem::KMPopHeadersViewItem(KMPopHeadersView *aParent, KMPopFilterAction aAction)
      : KListViewItem(aParent)
{
  mParent = aParent;
  mAction = NoAction;

  setPixmap(mParent->mapToColumn(Delete), TQPixmap(KMPopHeadersView::mUnchecked));
  setPixmap(mParent->mapToColumn(Down), TQPixmap(KMPopHeadersView::mUnchecked));
  setPixmap(mParent->mapToColumn(Later), TQPixmap(KMPopHeadersView::mUnchecked));

  setAction( aAction );
}

KMPopHeadersViewItem::~KMPopHeadersViewItem()
{
}

void KMPopHeadersViewItem::setAction(KMPopFilterAction aAction)
{
  if(aAction != NoAction && aAction!=mAction)
  {
    if ( mAction!=NoAction ) setPixmap(mParent->mapToColumn(mAction), TQPixmap(KMPopHeadersView::mUnchecked));
    setPixmap(mParent->mapToColumn(aAction), TQPixmap(KMPopHeadersView::mChecked));
    mAction=aAction;
  }
}

void KMPopHeadersViewItem::paintFocus(TQPainter *, const TQColorGroup &, const TQRect &)
{
}

TQString KMPopHeadersViewItem::key(int col, bool) const
{
  if (col == 3) return KMMsgBase::skipKeyword(text(col).lower());
  if (col == 6) return text(8);
  if (col == 7)
    return text(col).rightJustify( 10, '0', false);
  return text(col);
}

/////////////////////////////////////////
/////////////////////////////////////////
///  dlg
/////////////////////////////////////////
/////////////////////////////////////////
KMPopFilterCnfrmDlg::KMPopFilterCnfrmDlg(TQPtrList<KMPopHeaders> *aHeaders, const TQString &aAccount, bool aShowLaterMsgs, TQWidget *aParent, const char *aName)
      : KDialogBase(aParent, aName, true, i18n("POP Filter"), Ok | Help, Ok, false)
{
  unsigned int rulesetCount = 0;
  //mHeaders = aHeaders;
  mShowLaterMsgs = aShowLaterMsgs;
  mLowerBoxVisible = false;

  TQWidget *w = new TQWidget(this);
  setMainWidget(w);

  TQVBoxLayout *vbl = new TQVBoxLayout(w, 0, spacingHint());

  TQLabel *l = new TQLabel(i18n("Messages to filter found on POP Account: <b>%1</b><p>"
      "The messages shown exceed the maximum size limit you defined for this account.<br>You can select "
      "what you want to do with them by checking the appropriate button.").arg(aAccount), w);
  vbl->addWidget(l);

  TQVGroupBox *upperBox = new TQVGroupBox(i18n("Messages Exceeding Size"), w);
  upperBox->hide();
  KMPopHeadersView *lv = new KMPopHeadersView(upperBox, this);
  vbl->addWidget(upperBox);

  TQVGroupBox *lowerBox = new TQVGroupBox(i18n("Ruleset Filtered Messages: none"), w);
  TQString checkBoxText((aShowLaterMsgs)?
      i18n("Show messages matched by a ruleset and tagged 'Download' or 'Delete'"):
      i18n("Show messages matched by a filter ruleset"));
  TQCheckBox* cb = new TQCheckBox(checkBoxText, lowerBox);
  cb->setEnabled(false);
  mFilteredHeaders = new KMPopHeadersView(lowerBox, this);
  mFilteredHeaders->hide();
  vbl->addWidget(lowerBox);

  mFilteredHeaders->header()->setResizeEnabled(false, 8);
  mFilteredHeaders->setColumnWidth(8, 0);

  // fill the listviews with data from the headers
  KMPopHeaders *headers;
  for(headers = aHeaders->first(); headers; headers = aHeaders->next())
  {
    KMPopHeadersViewItem *lvi = 0;

    if(headers->ruleMatched())
    {
      if(aShowLaterMsgs && headers->action() == Later)
      {
        // insert messages tagged 'later' only
        lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
        mFilteredHeaders->show();
        mLowerBoxVisible = true;
      }
      else if(aShowLaterMsgs)
      {
        // enable checkbox to show 'delete' and 'download' msgs
        // but don't insert them into the listview yet
        mDDLList.append(headers);
        cb->setEnabled(true);
      }
      else if(!aShowLaterMsgs)
      {
        // insert all messaged tagged by a ruleset, enable
        // the checkbox, but don't show the listview yet
        lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
        cb->setEnabled(true);
      }
      rulesetCount++;
    }
    else
    {
      // insert all messages not tagged by a ruleset
      // into the upper listview
      lvi = new KMPopHeadersViewItem(lv, headers->action());
      upperBox->show();
    }

    if(lvi)
    {
      mItemMap[lvi] = headers;
      setupLVI(lvi,headers->header());
    }
  }

  if(rulesetCount)
      lowerBox->setTitle(i18n("Ruleset Filtered Messages: %1").arg(rulesetCount));

  // connect signals and slots
  connect(lv, TQT_SIGNAL(pressed(TQListViewItem*, const TQPoint&, int)),
      this, TQT_SLOT(slotPressed(TQListViewItem*, const TQPoint&, int)));
  connect(mFilteredHeaders, TQT_SIGNAL(pressed(TQListViewItem*, const TQPoint&, int)),
      this, TQT_SLOT(slotPressed(TQListViewItem*, const TQPoint&, int)));
  connect(cb, TQT_SIGNAL(toggled(bool)),
      this, TQT_SLOT(slotToggled(bool)));

  adjustSize();
  TQTimer::singleShot(0, this, TQT_SLOT(slotUpdateMinimumSize()));
}

KMPopFilterCnfrmDlg::~KMPopFilterCnfrmDlg()
{
}

void KMPopFilterCnfrmDlg::setupLVI(KMPopHeadersViewItem *lvi, KMMessage *msg)
{
      // set the subject
      TQString tmp = msg->subject();
      if(tmp.isEmpty())
        tmp = i18n("no subject");
      lvi->setText(3, tmp);

      // set the sender
      tmp = msg->fromStrip();
      if(tmp.isEmpty())
        tmp = i18n("unknown");
      lvi->setText(4, tmp);

      // set the receiver
      tmp = msg->toStrip();
      if(tmp.isEmpty())
        tmp = i18n("unknown");
      lvi->setText(5, tmp);

      // set the date
      lvi->setText(6, KMime::DateFormatter::formatDate( KMime::DateFormatter::Fancy, msg->date() ) );
      // set the size
      lvi->setText(7, KIO::convertSize(msg->msgLength()));
      // Date for sorting
      lvi->setText(8, msg->dateIsoStr());
}

void KMPopFilterCnfrmDlg::setAction(TQListViewItem *aItem, KMPopFilterAction aAction)
{
    mItemMap[aItem]->setAction(aAction);
}
/**
  This Slot is called whenever a ListView item was pressed.
  It checks for the column the button was pressed in and changes the action if the
  click happened over a radio button column.
  Of course the radio button state is changed as well if the above is true.
*/
void KMPopFilterCnfrmDlg::slotPressed(TQListViewItem *aItem, const TQPoint &, int aColumn)
{
  if ( aColumn>=0 && aColumn<NoAction ) setAction(aItem,KMPopHeadersView::mapToAction(aColumn));
}

void KMPopFilterCnfrmDlg::slotToggled(bool aOn)
{
  if(aOn)
  {
    if(mShowLaterMsgs)
    {
      // show download and delete msgs in the list view too
      for(KMPopHeaders* headers = mDDLList.first(); headers; headers = mDDLList.next())
      {
        KMPopHeadersViewItem *lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
        mItemMap[lvi] = headers;
        mDelList.append(lvi);
        setupLVI(lvi,headers->header());
      }
    }

    if(!mLowerBoxVisible)
    {
      mFilteredHeaders->show();
    }
  }
  else
  {
    if(mShowLaterMsgs)
    {
      // delete download and delete msgs from the lower listview
      for(KMPopHeadersViewItem* item = mDelList.first(); item; item = mDelList.next())
      {
        mFilteredHeaders->takeItem(item);
      }
      mDelList.clear();
    }

    if(!mLowerBoxVisible)
    {
      mFilteredHeaders->hide();
    }
  }
  TQTimer::singleShot(0, this, TQT_SLOT(slotUpdateMinimumSize()));
}

void KMPopFilterCnfrmDlg::slotUpdateMinimumSize()
{
  mainWidget()->setMinimumSize(mainWidget()->tqsizeHint());
}

#include "kmpopfiltercnfrmdlg.moc"