kmail

foldertreebase.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "foldertreebase.h"
00020 
00021 #include "globalsettings.h"
00022 #include "kmfolder.h"
00023 #include "kmfolderdir.h"
00024 #include "kmfoldertree.h"
00025 #include "kmheaders.h"
00026 #include "kmmainwidget.h"
00027 #include "messagecopyhelper.h"
00028 #include "folderstorage.h"
00029 
00030 #include <libkdepim/maillistdrag.h>
00031 using KPIM::MailList;
00032 using KPIM::MailListDrag;
00033 
00034 #include <kconfig.h>
00035 #include <kiconloader.h>
00036 #include <kpopupmenu.h>
00037 
00038 #include <qcursor.h>
00039 
00040 using namespace KMail;
00041 
00042 FolderTreeBase::FolderTreeBase(KMMainWidget *mainWidget, QWidget * parent, const char * name) :
00043     KFolderTree( parent, name ),
00044     mMainWidget( mainWidget )
00045 {
00046   addAcceptableDropMimetype(MailListDrag::format(), false);
00047 }
00048 
00049 void FolderTreeBase::contentsDropEvent(QDropEvent * e)
00050 {
00051   QListViewItem *item = itemAt( contentsToViewport(e->pos()) );
00052   KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>(item);
00053   if ( fti && fti->folder() && e->provides( MailListDrag::format() ) ) {
00054     if ( e->source() == mMainWidget->headers()->viewport() ) {
00055       int action;
00056       if ( mMainWidget->headers()->folder() && mMainWidget->headers()->folder()->isReadOnly() )
00057         action = DRAG_COPY;
00058       else
00059         action = dndMode();
00060       // KMHeaders does copy/move itself
00061       if ( action == DRAG_MOVE && fti->folder() )
00062         emit folderDrop( fti->folder() );
00063       else if ( action == DRAG_COPY && fti->folder() )
00064         emit folderDropCopy( fti->folder() );
00065     } else {
00066       handleMailListDrop( e, fti->folder() );
00067     }
00068     e->accept( true );
00069   } else {
00070     KFolderTree::contentsDropEvent( e );
00071   }
00072 }
00073 
00074 int FolderTreeBase::dndMode(bool alwaysAsk)
00075 {
00076   int action = -1;
00077   int keybstate = kapp->keyboardModifiers();
00078   if ( keybstate & KApplication::ControlModifier ) {
00079     action = DRAG_COPY;
00080   } else if ( keybstate & KApplication::ShiftModifier ) {
00081     action = DRAG_MOVE;
00082   } else {
00083     if ( GlobalSettings::self()->showPopupAfterDnD() || alwaysAsk ) {
00084       KPopupMenu *menu = new KPopupMenu( this );
00085       menu->insertItem( i18n("&Move Here"), DRAG_MOVE, 0 );
00086       menu->insertItem( SmallIcon("editcopy"), i18n("&Copy Here"), DRAG_COPY, 1 );
00087       menu->insertSeparator();
00088       menu->insertItem( SmallIcon("cancel"), i18n("C&ancel"), DRAG_CANCEL, 3 );
00089       action = menu->exec( QCursor::pos(), 0 );
00090     }
00091     else
00092       action = DRAG_MOVE;
00093   }
00094   return action;
00095 }
00096 
00097 bool FolderTreeBase::event(QEvent * e)
00098 {
00099   if (e->type() == QEvent::ApplicationPaletteChange) {
00100      readColorConfig();
00101      return true;
00102   }
00103   return KFolderTree::event(e);
00104 }
00105 
00106 void FolderTreeBase::readColorConfig()
00107 {
00108   KConfig* conf = KMKernel::config();
00109   // Custom/System color support
00110   KConfigGroupSaver saver(conf, "Reader");
00111   QColor c1=QColor(kapp->palette().active().text());
00112   QColor c2=QColor("blue");
00113   QColor c4=QColor(kapp->palette().active().base());
00114   QColor c5=QColor("red");
00115 
00116   if (!conf->readBoolEntry("defaultColors",true)) {
00117     mPaintInfo.colFore = conf->readColorEntry("ForegroundColor",&c1);
00118     mPaintInfo.colUnread = conf->readColorEntry("UnreadMessage",&c2);
00119     mPaintInfo.colBack = conf->readColorEntry("BackgroundColor",&c4);
00120     mPaintInfo.colCloseToQuota = conf->readColorEntry("CloseToQuotaColor",&c5);
00121   }
00122   else {
00123     mPaintInfo.colFore = c1;
00124     mPaintInfo.colUnread = c2;
00125     mPaintInfo.colBack = c4;
00126     mPaintInfo.colCloseToQuota = c5;
00127   }
00128   QPalette newPal = kapp->palette();
00129   newPal.setColor( QColorGroup::Base, mPaintInfo.colBack );
00130   newPal.setColor( QColorGroup::Text, mPaintInfo.colFore );
00131   setPalette( newPal );
00132 }
00133 
00134 bool FolderTreeBase::hideLocalInbox() const
00135 {
00136   if ( !GlobalSettings::self()->hideLocalInbox() )
00137     return false;
00138   KMFolder *localInbox = kmkernel->inboxFolder();
00139   assert( localInbox );
00140   // check if it is empty
00141   localInbox->open( "foldertreebase" );
00142   if ( localInbox->count() > 0 ) {
00143     localInbox->close( "foldertreebase" );
00144     return false;
00145   }
00146   localInbox->close( "foldertreebase" );
00147   // check if it has subfolders
00148   if ( localInbox->child() && !localInbox->child()->isEmpty() )
00149     return false;
00150   // check if it is an account target
00151   if ( localInbox->hasAccounts() )
00152     return false;
00153   return true;
00154 }
00155 
00156 
00157 void FolderTreeBase::slotUpdateCounts(KMFolder * folder, bool force /* = false*/)
00158 {
00159  // kdDebug(5006) << "KMFolderTree::slotUpdateCounts()" << endl;
00160   QListViewItem * current;
00161   if (folder)
00162     current = indexOfFolder(folder);
00163   else
00164     current = currentItem();
00165 
00166   KMFolderTreeItem* fti = static_cast<KMFolderTreeItem*>(current);
00167 
00168   // sanity check
00169   if (!fti) return;
00170   if (!fti->folder()) fti->setTotalCount(-1);
00171 
00172   // get the unread count
00173   int count = 0;
00174   if (folder && folder->noContent()) // always empty
00175     count = -1;
00176   else {
00177     if ( fti->folder() )
00178       count = fti->folder()->countUnread();
00179   }
00180 
00181   // set it
00182   bool repaint = false;
00183   if (fti->unreadCount() != count) {
00184      fti->adjustUnreadCount( count );
00185      repaint = true;
00186   }
00187   if (isTotalActive() || force)
00188   {
00189     // get the total-count
00190     if (fti->folder()->noContent())
00191       count = -1;
00192     else {
00193       // get the cached count if the folder is not open
00194       count = fti->folder()->count( !fti->folder()->isOpened() );
00195     }
00196     // set it
00197     if ( count != fti->totalCount() ) {
00198       fti->setTotalCount(count);
00199       repaint = true;
00200     }
00201   }
00202   if ( isSizeActive() || force ) {
00203     if ( !fti->folder()->noContent()) {
00204       Q_INT64 size = folder->storage()->folderSize();
00205       if ( size != fti->folderSize() ) {
00206         fti->setFolderSize( size );
00207         repaint = true;
00208       }
00209     }
00210   }
00211   if ( fti->folderIsCloseToQuota() != folder->storage()->isCloseToQuota() ) {
00212       fti->setFolderIsCloseToQuota( folder->storage()->isCloseToQuota() );
00213   }
00214 
00215   if (fti->parent() && !fti->parent()->isOpen())
00216     repaint = false; // we're not visible
00217   if (repaint) {
00218     fti->setNeedsRepaint( true );
00219     emit triggerRefresh();
00220   }
00221   // tell the kernel that one of the counts has changed
00222   kmkernel->messageCountChanged();
00223 }
00224 
00225 void FolderTreeBase::handleMailListDrop(QDropEvent * event, KMFolder *destination )
00226 {
00227   MailList list;
00228   if ( !MailListDrag::decode( event, list ) ) {
00229     kdWarning() << k_funcinfo << "Could not decode drag data!" << endl;
00230   } else {
00231     QValueList<Q_UINT32> serNums = MessageCopyHelper::serNumListFromMailList( list );
00232     int action;
00233     if ( MessageCopyHelper::inReadOnlyFolder( serNums ) )
00234       action = DRAG_COPY;
00235     else
00236       action = dndMode();
00237     if ( action == DRAG_COPY || action == DRAG_MOVE ) {
00238       new MessageCopyHelper( serNums, destination, action == DRAG_MOVE, this );
00239     }
00240   }
00241 }
00242 
00243 #include "foldertreebase.moc"