kmail

kmpopfiltercnfrmdlg.cpp

00001 /***************************************************************************
00002                           kmpopheadersdlg.cpp
00003                              -------------------
00004     begin                : Mon Oct 22 2001
00005     copyright            : (C) 2001 by Heiko Hund
00006                                        Thorsten Zachmann
00007     email                : heiko@ist.eigentlich.net
00008                            T.Zachmann@zagge.de
00009  ***************************************************************************/
00010 
00011 #include <config.h>
00012 #include "kmpopfiltercnfrmdlg.h"
00013 #include "kmheaders.h"
00014 
00015 #include <qlayout.h>
00016 #include <qlabel.h>
00017 #include <qheader.h>
00018 #include <qcheckbox.h>
00019 #include <qvgroupbox.h>
00020 #include <qtimer.h>
00021 
00022 #include <klocale.h>
00023 #include <kio/global.h>
00024 
00025 #include <assert.h>
00026 
00029 KMPopHeadersView::KMPopHeadersView(QWidget *aParent, KMPopFilterCnfrmDlg *aDialog)
00030       : KListView(aParent)
00031 {
00032   mDialog=aDialog;
00033   int mDownIndex=addColumn(QIconSet(QPixmap(mDown)), QString::null, 24);
00034   assert( mDownIndex == Down ); //This code relies on the fact that radiobuttons are the first three columns for easier Column-Action mapping
00035                     //it does not necessarily be true - you could redefine mapToColumn and mapToAction to eg. shift those numbers by 1
00036   addColumn(QIconSet(QPixmap(mLater)), QString::null, 24);
00037   addColumn(QIconSet(QPixmap(mDel)), QString::null, 24);
00038 
00039   /*int subjCol =*/ addColumn(i18n("Subject"), 180);
00040   /*int sendCol =*/ addColumn(i18n("Sender"), 150);
00041   /*int recvCol =*/ addColumn(i18n("Receiver"), 150);
00042   int dateCol = addColumn(i18n("Date"), 120);
00043   int sizeCol = addColumn(i18n("Size"), 80);
00044 
00045   setAllColumnsShowFocus(true);
00046 
00047   setColumnAlignment(Down, Qt::AlignHCenter);
00048   setColumnAlignment(Later, Qt::AlignHCenter);
00049   setColumnAlignment(Delete, Qt::AlignHCenter);
00050   setColumnAlignment(sizeCol, Qt::AlignRight);
00051 
00052   setSorting(dateCol, false);
00053   setShowSortIndicator(true);
00054   header()->setResizeEnabled(false, Down);
00055   header()->setResizeEnabled(false, Later);
00056   header()->setResizeEnabled(false, Delete);
00057   header()->setClickEnabled(false, Down);
00058   header()->setClickEnabled(false, Later);
00059   header()->setClickEnabled(false, Delete);
00060 
00061   //we rely on fixed column order, so we forbid this
00062   header()->setMovingEnabled(false);
00063 
00064   connect(this, SIGNAL(pressed(QListViewItem*, const QPoint&, int)),
00065         SLOT(slotPressed(QListViewItem*, const QPoint&, int)));
00066 }
00067 
00068 KMPopHeadersView::~KMPopHeadersView()
00069 {
00070 }
00071 
00072 //Handle keystrokes - Left and Right key select previous/next action correspondingly
00073 void KMPopHeadersView::keyPressEvent( QKeyEvent *e )
00074 {
00075     if (e->key() == Key_Left) {
00076         KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>( selectedItem() );
00077         if (item&&mDialog) {
00078             if (item->action()) { //here we rely on the fact that the leftmost action is equal to 0!
00079                 item->setAction((KMPopFilterAction)((int)item->action()-1));
00080                 mDialog->setAction( selectedItem(), item->action());
00081             }
00082         }
00083     } else if (e->key() == Key_Right) {
00084         KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>( selectedItem() );
00085         if (item&&mDialog) {
00086             if (item->action()<NoAction-1) { //here we rely on the fact that right most action is one less than NoAction!
00087                 item->setAction((KMPopFilterAction)((int)item->action()+1));
00088                 mDialog->setAction( selectedItem(), item->action());
00089             }
00090         }
00091     } else {
00092         QListView::keyPressEvent( e );
00093     }
00094 }
00095 
00096 void KMPopHeadersView::slotPressed(QListViewItem* aItem, const QPoint&, int aColumn) {
00097   if ( !( aItem && aColumn>=0 && aColumn<NoAction ) ) return;
00098   KMPopHeadersViewItem *item = dynamic_cast<KMPopHeadersViewItem*>(aItem);
00099   assert( item );
00100   item->setAction(mapToAction(aColumn));
00101 }
00102 
00103 const char *KMPopHeadersView::mUnchecked[26] = {
00104 "19 16 9 1",
00105 "  c None",
00106 "# c #000000",
00107 ". c #ffffff",
00108 "a c #4a4c4a",
00109 "b c #524c52",
00110 "c c #efefef",
00111 "e c #fff2ff",
00112 "f c #f6f2f6",
00113 "g c #fff6ff",
00114 "                   ",
00115 "                   ",
00116 "         aaaa      ",
00117 "       ba####aa    ",
00118 "      a##....aac   ",
00119 "      a#......ec   ",
00120 "     a#........fc  ",
00121 "     a#........gc  ",
00122 "     a#........fc  ",
00123 "     b#........gc  ",
00124 "      a#......gc   ",
00125 "      age....gec   ",
00126 "       ccfgfgcc    ",
00127 "         cccc      ",
00128 "                   ",
00129 "                   ",};
00130 
00131 const char *KMPopHeadersView::mChecked[26] = {
00132 "19 16 9 1",
00133 "  c None",
00134 "# c #000000",
00135 ". c #ffffff",
00136 "a c #4a4c4a",
00137 "b c #524c52",
00138 "c c #efefef",
00139 "e c #fff2ff",
00140 "f c #f6f2f6",
00141 "g c #fff6ff",
00142 "                   ",
00143 "                   ",
00144 "         aaaa      ",
00145 "       ba####aa    ",
00146 "      a##....aac   ",
00147 "      a#......ec   ",
00148 "     a#...##...fc  ",
00149 "     a#..####..gc  ",
00150 "     a#..####..fc  ",
00151 "     b#...##...gc  ",
00152 "      a#......gc   ",
00153 "      age....gec   ",
00154 "       ccfgfgcc    ",
00155 "         cccc      ",
00156 "                   ",
00157 "                   ",};
00158 
00159 const char *KMPopHeadersView::mLater[25] = {
00160 "16 16 8 1",
00161 ". c None",
00162 "g c #303030",
00163 "c c #585858",
00164 "f c #a0a0a0",
00165 "b c #c0c000",
00166 "e c #dcdcdc",
00167 "a c #ffff00",
00168 "d c #ffffff",
00169 "................",
00170 "...........eaa..",
00171 "..........eaaa..",
00172 ".........ebaab..",
00173 ".........eaaa...",
00174 "........eaaab...",
00175 "........eaaa....",
00176 ".......eaaab....",
00177 "eaae..ebaccccc..",
00178 "eaaae.eaacdedc..",
00179 "ebaaabaaabcdc...",
00180 ".ebaaaaaa.fgf...",
00181 "..ebaaaa..cec...",
00182 "...ebaab.cdedc..",
00183 "........eccccc..",
00184 "................"};
00185 
00186 const char *KMPopHeadersView::mDown[20] = {
00187 "16 16 3 1",
00188 ". c None",
00189 "a c #008000",
00190 "b c #00c000",
00191 "................",
00192 "............aa..",
00193 "...........aaa..",
00194 "..........baab..",
00195 "..........aaa...",
00196 ".........baab...",
00197 ".........aaa....",
00198 "........aaab....",
00199 ".aa....baaa.....",
00200 ".aaa...aaa......",
00201 ".baaabaaab......",
00202 "..baaaaaa.......",
00203 "...baaaa........",
00204 "....baab........",
00205 "................",
00206 "................"};
00207 
00208 const char *KMPopHeadersView::mDel[19] = {
00209 "16 16 2 1",
00210 ". c None",
00211 "# c #c00000",
00212 "................",
00213 "................",
00214 "..##.......##...",
00215 "..###.....###...",
00216 "...###...###....",
00217 "....###.###.....",
00218 ".....#####......",
00219 "......###.......",
00220 ".....#####......",
00221 "....###.###.....",
00222 "...###...###....",
00223 "..###.....###...",
00224 "..##.......##...",
00225 "................",
00226 "................",
00227 "................"};
00228 
00229 
00235 KMPopHeadersViewItem::KMPopHeadersViewItem(KMPopHeadersView *aParent, KMPopFilterAction aAction)
00236       : KListViewItem(aParent)
00237 {
00238   mParent = aParent;
00239   mAction = NoAction;
00240 
00241   setPixmap(mParent->mapToColumn(Delete), QPixmap(KMPopHeadersView::mUnchecked));
00242   setPixmap(mParent->mapToColumn(Down), QPixmap(KMPopHeadersView::mUnchecked));
00243   setPixmap(mParent->mapToColumn(Later), QPixmap(KMPopHeadersView::mUnchecked));
00244 
00245   setAction( aAction );
00246 }
00247 
00248 KMPopHeadersViewItem::~KMPopHeadersViewItem()
00249 {
00250 }
00251 
00252 void KMPopHeadersViewItem::setAction(KMPopFilterAction aAction)
00253 {
00254   if(aAction != NoAction && aAction!=mAction)
00255   {
00256     if ( mAction!=NoAction ) setPixmap(mParent->mapToColumn(mAction), QPixmap(KMPopHeadersView::mUnchecked));
00257     setPixmap(mParent->mapToColumn(aAction), QPixmap(KMPopHeadersView::mChecked));
00258     mAction=aAction;
00259   }
00260 }
00261 
00262 void KMPopHeadersViewItem::paintFocus(QPainter *, const QColorGroup &, const QRect &)
00263 {
00264 }
00265 
00266 QString KMPopHeadersViewItem::key(int col, bool) const
00267 {
00268   if (col == 3) return KMMsgBase::skipKeyword(text(col).lower());
00269   if (col == 5) return text(7);
00270   if (col == 6)
00271   {
00272     QString st = text(col);
00273     while (st.length() < 10) st = "0" + st;
00274     return st;
00275   }
00276   return text(col);
00277 }
00278 
00284 KMPopFilterCnfrmDlg::KMPopFilterCnfrmDlg(QPtrList<KMPopHeaders> *aHeaders, const QString &aAccount, bool aShowLaterMsgs, QWidget *aParent, const char *aName)
00285       : KDialogBase(aParent, aName, TRUE, i18n("POP Filter"), Ok | Help, Ok, FALSE)
00286 {
00287   unsigned int rulesetCount = 0;
00288   //mHeaders = aHeaders;
00289   mShowLaterMsgs = aShowLaterMsgs;
00290   mLowerBoxVisible = false;
00291 
00292   QWidget *w = new QWidget(this);
00293   setMainWidget(w);
00294 
00295   QVBoxLayout *vbl = new QVBoxLayout(w, 0, spacingHint());
00296 
00297   QLabel *l = new QLabel(i18n("Messages to filter found on POP Account: <b>%1</b><p>"
00298       "The messages shown exceed the maximum size limit you defined for this account.<br>You can select "
00299       "what you want to do with them by checking the appropriate button.").arg(aAccount), w);
00300   vbl->addWidget(l);
00301 
00302   QVGroupBox *upperBox = new QVGroupBox(i18n("Messages Exceeding Size"), w);
00303   upperBox->hide();
00304   KMPopHeadersView *lv = new KMPopHeadersView(upperBox, this);
00305   vbl->addWidget(upperBox);
00306 
00307   QVGroupBox *lowerBox = new QVGroupBox(i18n("Ruleset Filtered Messages: none"), w);
00308   QString checkBoxText((aShowLaterMsgs)?
00309       i18n("Show messages matched by a ruleset and tagged 'Download' or 'Delete'"):
00310       i18n("Show messages matched by a filter ruleset"));
00311   QCheckBox* cb = new QCheckBox(checkBoxText, lowerBox);
00312   cb->setEnabled(false);
00313   mFilteredHeaders = new KMPopHeadersView(lowerBox, this);
00314   mFilteredHeaders->hide();
00315   vbl->addWidget(lowerBox);
00316 
00317   mFilteredHeaders->header()->setResizeEnabled(false, 8);
00318   mFilteredHeaders->setColumnWidth(8, 0);
00319 
00320   // fill the listviews with data from the headers
00321   KMPopHeaders *headers;
00322   for(headers = aHeaders->first(); headers; headers = aHeaders->next())
00323   {
00324     KMPopHeadersViewItem *lvi = 0;
00325 
00326     if(headers->ruleMatched())
00327     {
00328       if(aShowLaterMsgs && headers->action() == Later)
00329       {
00330         // insert messages tagged 'later' only
00331         lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
00332         mFilteredHeaders->show();
00333         mLowerBoxVisible = true;
00334       }
00335       else if(aShowLaterMsgs)
00336       {
00337         // enable checkbox to show 'delete' and 'download' msgs
00338         // but don't insert them into the listview yet
00339         mDDLList.append(headers);
00340         cb->setEnabled(true);
00341       }
00342       else if(!aShowLaterMsgs)
00343       {
00344         // insert all messaged tagged by a ruleset, enable
00345         // the checkbox, but don't show the listview yet
00346         lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
00347         cb->setEnabled(true);
00348       }
00349       rulesetCount++;
00350     }
00351     else
00352     {
00353       // insert all messages not tagged by a ruleset
00354       // into the upper listview
00355       lvi = new KMPopHeadersViewItem(lv, headers->action());
00356       upperBox->show();
00357     }
00358 
00359     if(lvi)
00360     {
00361       mItemMap[lvi] = headers;
00362       setupLVI(lvi,headers->header());
00363     }
00364   }
00365 
00366   if(rulesetCount)
00367       lowerBox->setTitle(i18n("Ruleset Filtered Messages: %1").arg(rulesetCount));
00368 
00369   // connect signals and slots
00370   connect(lv, SIGNAL(pressed(QListViewItem*, const QPoint&, int)),
00371       this, SLOT(slotPressed(QListViewItem*, const QPoint&, int)));
00372   connect(mFilteredHeaders, SIGNAL(pressed(QListViewItem*, const QPoint&, int)),
00373       this, SLOT(slotPressed(QListViewItem*, const QPoint&, int)));
00374   connect(cb, SIGNAL(toggled(bool)),
00375       this, SLOT(slotToggled(bool)));
00376 
00377   adjustSize();
00378   QTimer::singleShot(0, this, SLOT(slotUpdateMinimumSize()));
00379 }
00380 
00381 KMPopFilterCnfrmDlg::~KMPopFilterCnfrmDlg()
00382 {
00383 }
00384 
00385 void KMPopFilterCnfrmDlg::setupLVI(KMPopHeadersViewItem *lvi, KMMessage *msg)
00386 {
00387       // set the subject
00388       QString tmp = msg->subject();
00389       if(tmp.isEmpty())
00390         tmp = i18n("no subject");
00391       lvi->setText(3, tmp);
00392 
00393       // set the sender
00394       tmp = msg->fromStrip();
00395       if(tmp.isEmpty())
00396         tmp = i18n("unknown");
00397       lvi->setText(4, tmp);
00398 
00399       // set the receiver
00400       tmp = msg->toStrip();
00401       if(tmp.isEmpty())
00402         tmp = i18n("unknown");
00403       lvi->setText(5, tmp);
00404 
00405       // set the date
00406       lvi->setText(6, KMime::DateFormatter::formatDate( KMime::DateFormatter::Fancy, msg->date() ) );
00407       // set the size
00408       lvi->setText(7, KIO::convertSize(msg->msgLength()));
00409       // Date for sorting
00410       lvi->setText(8, msg->dateIsoStr());
00411 }
00412 
00413 void KMPopFilterCnfrmDlg::setAction(QListViewItem *aItem, KMPopFilterAction aAction)
00414 {
00415     mItemMap[aItem]->setAction(aAction);
00416 }
00423 void KMPopFilterCnfrmDlg::slotPressed(QListViewItem *aItem, const QPoint &, int aColumn)
00424 {
00425   if ( aColumn>=0 && aColumn<NoAction ) setAction(aItem,KMPopHeadersView::mapToAction(aColumn));
00426 }
00427 
00428 void KMPopFilterCnfrmDlg::slotToggled(bool aOn)
00429 {
00430   if(aOn)
00431   {
00432     if(mShowLaterMsgs)
00433     {
00434       // show download and delete msgs in the list view too
00435       for(KMPopHeaders* headers = mDDLList.first(); headers; headers = mDDLList.next())
00436       {
00437         KMPopHeadersViewItem *lvi = new KMPopHeadersViewItem(mFilteredHeaders, headers->action());
00438         mItemMap[lvi] = headers;
00439         mDelList.append(lvi);
00440         setupLVI(lvi,headers->header());
00441       }
00442     }
00443 
00444     if(!mLowerBoxVisible)
00445     {
00446       mFilteredHeaders->show();
00447     }
00448   }
00449   else
00450   {
00451     if(mShowLaterMsgs)
00452     {
00453       // delete download and delete msgs from the lower listview
00454       for(KMPopHeadersViewItem* item = mDelList.first(); item; item = mDelList.next())
00455       {
00456         mFilteredHeaders->takeItem(item);
00457       }
00458       mDelList.clear();
00459     }
00460 
00461     if(!mLowerBoxVisible)
00462     {
00463       mFilteredHeaders->hide();
00464     }
00465   }
00466   QTimer::singleShot(0, this, SLOT(slotUpdateMinimumSize()));
00467 }
00468 
00469 void KMPopFilterCnfrmDlg::slotUpdateMinimumSize()
00470 {
00471   mainWidget()->setMinimumSize(mainWidget()->sizeHint());
00472 }
00473 
00474 #include "kmpopfiltercnfrmdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys