00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "favoritefolderview.h"
00020
00021 #include "kmfolder.h"
00022 #include "kmfoldermgr.h"
00023 #include "kmfolderseldlg.h"
00024 #include "kmmainwidget.h"
00025 #include "kmailicalifaceimpl.h"
00026 #include "folderstorage.h"
00027 #include "kmfolderimap.h"
00028 #include "kmfoldercachedimap.h"
00029 #include "kmacctcachedimap.h"
00030 #include "folderviewtooltip.h"
00031 #include "korghelper.h"
00032
00033 #include <libkdepim/maillistdrag.h>
00034 #include <libkdepim/kaddrbook.h>
00035
00036 #include <dcopclient.h>
00037 #include <kdebug.h>
00038 #include <kglobalsettings.h>
00039 #include <kiconloader.h>
00040 #include <kinputdialog.h>
00041 #include <klocale.h>
00042 #include <kpopupmenu.h>
00043 #include <kio/global.h>
00044
00045 #include <qheader.h>
00046 #include <qtimer.h>
00047
00048 #include <cassert>
00049
00050 using namespace KMail;
00051
00052 FavoriteFolderViewItem::FavoriteFolderViewItem(FavoriteFolderView * parent, const QString & name, KMFolder * folder)
00053 : KMFolderTreeItem( parent, name, folder ),
00054 mOldName( folder->label() )
00055 {
00056
00057 init();
00058 connect( folder, SIGNAL(nameChanged()), SLOT(nameChanged()) );
00059 connect( folder, SIGNAL(iconsChanged()), SLOT(slotIconsChanged()) );
00060
00061 connect( folder, SIGNAL(msgAdded(KMFolder*,Q_UINT32)), SLOT(updateCount()) );
00062 connect( folder, SIGNAL(numUnreadMsgsChanged(KMFolder*)), SLOT(updateCount()) );
00063 connect( folder, SIGNAL(msgRemoved(KMFolder*)), SLOT(updateCount()) );
00064 connect( folder, SIGNAL(folderSizeChanged( KMFolder* )), SLOT(updateCount()) );
00065
00066 QTimer::singleShot( 0, this, SLOT(updateCount()) );
00067
00068 if ( unreadCount() > 0 )
00069 setPixmap( 0, unreadIcon( iconSize() ) );
00070 else
00071 setPixmap( 0, normalIcon( iconSize() ) );
00072 }
00073
00074 void FavoriteFolderViewItem::nameChanged()
00075 {
00076 QString txt = text( 0 );
00077 txt.replace( mOldName, folder()->label() );
00078 setText( 0, txt );
00079 mOldName = folder()->label();
00080 }
00081
00082 QValueList<FavoriteFolderView*> FavoriteFolderView::mInstances;
00083
00084 FavoriteFolderView::FavoriteFolderView( KMMainWidget *mainWidget, QWidget * parent) :
00085 FolderTreeBase( mainWidget, parent ),
00086 mContextMenuItem( 0 ),
00087 mReadingConfig( false )
00088 {
00089 assert( mainWidget );
00090 addColumn( i18n("Favorite Folders") );
00091 setResizeMode( LastColumn );
00092 header()->setClickEnabled( false );
00093 setDragEnabled( true );
00094 setAcceptDrops( true );
00095 setRootIsDecorated( false );
00096 setSelectionModeExt( KListView::Single );
00097 setSorting( -1 );
00098 setShowSortIndicator( false );
00099
00100 connect( this, SIGNAL(selectionChanged()), SLOT(selectionChanged()) );
00101 connect( this, SIGNAL(clicked(QListViewItem*)), SLOT(itemClicked(QListViewItem*)) );
00102 connect( this, SIGNAL(dropped(QDropEvent*,QListViewItem*)), SLOT(dropped(QDropEvent*,QListViewItem*)) );
00103 connect( this, SIGNAL(contextMenuRequested(QListViewItem*, const QPoint &, int)),
00104 SLOT(contextMenu(QListViewItem*,const QPoint&)) );
00105 connect( this, SIGNAL(moved()), SLOT(notifyInstancesOnChange()) );
00106 connect( this, SIGNAL(triggerRefresh()), SLOT(refresh()) );
00107
00108 connect( kmkernel->folderMgr(), SIGNAL(changed()), SLOT(initializeFavorites()) );
00109 connect( kmkernel->dimapFolderMgr(), SIGNAL(changed()), SLOT(initializeFavorites()) );
00110 connect( kmkernel->imapFolderMgr(), SIGNAL(changed()), SLOT(initializeFavorites()) );
00111 connect( kmkernel->searchFolderMgr(), SIGNAL(changed()), SLOT(initializeFavorites()) );
00112
00113 connect( kmkernel->folderMgr(), SIGNAL(folderRemoved(KMFolder*)), SLOT(folderRemoved(KMFolder*)) );
00114 connect( kmkernel->dimapFolderMgr(), SIGNAL(folderRemoved(KMFolder*)), SLOT(folderRemoved(KMFolder*)) );
00115 connect( kmkernel->imapFolderMgr(), SIGNAL(folderRemoved(KMFolder*)), SLOT(folderRemoved(KMFolder*)) );
00116 connect( kmkernel->searchFolderMgr(), SIGNAL(folderRemoved(KMFolder*)), SLOT(folderRemoved(KMFolder*)) );
00117
00118 QFont f = font();
00119 f.setItalic( true );
00120 setFont( f );
00121
00122 new FolderViewToolTip( this );
00123
00124 mInstances.append( this );
00125 }
00126
00127 FavoriteFolderView::~FavoriteFolderView()
00128 {
00129 mInstances.remove( this );
00130 }
00131
00132 void FavoriteFolderView::readConfig()
00133 {
00134 mReadingConfig = true;
00135 clear();
00136 QValueList<int> folderIds = GlobalSettings::self()->favoriteFolderIds();
00137 QStringList folderNames = GlobalSettings::self()->favoriteFolderNames();
00138 QListViewItem *afterItem = 0;
00139 for ( uint i = 0; i < folderIds.count(); ++i ) {
00140 KMFolder *folder = kmkernel->folderMgr()->findById( folderIds[i] );
00141 if ( !folder )
00142 folder = kmkernel->imapFolderMgr()->findById( folderIds[i] );
00143 if ( !folder )
00144 folder = kmkernel->dimapFolderMgr()->findById( folderIds[i] );
00145 if ( !folder )
00146 folder = kmkernel->searchFolderMgr()->findById( folderIds[i] );
00147 QString name;
00148 if ( folderNames.count() > i )
00149 name = folderNames[i];
00150 afterItem = addFolder( folder, name, afterItem );
00151 }
00152 if ( firstChild() )
00153 ensureItemVisible( firstChild() );
00154
00155
00156 QTimer::singleShot( 0, this, SLOT(initializeFavorites()) );
00157
00158 readColorConfig();
00159 mReadingConfig = false;
00160 }
00161
00162 void FavoriteFolderView::writeConfig()
00163 {
00164 QValueList<int> folderIds;
00165 QStringList folderNames;
00166 for ( QListViewItemIterator it( this ); it.current(); ++it ) {
00167 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00168 folderIds << fti->folder()->id();
00169 folderNames << fti->text( 0 );
00170 }
00171 GlobalSettings::self()->setFavoriteFolderIds( folderIds );
00172 GlobalSettings::self()->setFavoriteFolderNames( folderNames );
00173 }
00174
00175 bool FavoriteFolderView::acceptDrag(QDropEvent * e) const
00176 {
00177 KMFolderTree *ft = mainWidget()->folderTree();
00178 assert( ft );
00179 if ( e->provides( "application/x-qlistviewitem" ) &&
00180 (e->source() == ft->viewport() || e->source() == viewport() ) )
00181 return true;
00182 return FolderTreeBase::acceptDrag( e );
00183 }
00184
00185 KMFolderTreeItem* FavoriteFolderView::addFolder(KMFolder * folder, const QString &name, QListViewItem *after)
00186 {
00187 if ( !folder )
00188 return 0;
00189 KMFolderTreeItem *item = new FavoriteFolderViewItem( this, name.isEmpty() ? folder->label() : name, folder );
00190 if ( after )
00191 item->moveItem( after );
00192 else
00193 item->moveItem( lastItem() );
00194 ensureItemVisible( item );
00195 insertIntoFolderToItemMap( folder, item );
00196 notifyInstancesOnChange();
00197 return item;
00198 }
00199
00200 void FavoriteFolderView::selectionChanged()
00201 {
00202 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( selectedItem() );
00203 if ( !fti )
00204 return;
00205 KMFolderTree *ft = mainWidget()->folderTree();
00206 assert( ft );
00207 assert( fti );
00208 ft->showFolder( fti->folder() );
00209 handleGroupwareFolder( fti );
00210 }
00211
00212 void FavoriteFolderView::handleGroupwareFolder( KMFolderTreeItem *fti )
00213 {
00214 if ( !fti || !fti->folder() || !fti->folder()->storage() )
00215 return;
00216 switch ( fti->folder()->storage()->contentsType() ) {
00217 case KMail::ContentsTypeContact:
00218 KAddrBookExternal::openAddressBook( this );
00219 break;
00220 case KMail::ContentsTypeNote:
00221 {
00222 QByteArray arg;
00223 QDataStream s( arg, IO_WriteOnly );
00224 s << QString( "kontact_knotesplugin" );
00225 kapp->dcopClient()->send( "kontact", "KontactIface", "selectPlugin(QString)", arg );
00226 break;
00227 }
00228 case KMail::ContentsTypeCalendar:
00229 case KMail::ContentsTypeTask:
00230 case KMail::ContentsTypeJournal:
00231 {
00232 KMail::KorgHelper::ensureRunning();
00233 QByteArray arg;
00234 QDataStream s( arg, IO_WriteOnly );
00235 switch ( fti->folder()->storage()->contentsType() ) {
00236 case KMail::ContentsTypeCalendar:
00237 s << QString( "kontact_korganizerplugin" ); break;
00238 case KMail::ContentsTypeTask:
00239 s << QString( "kontact_todoplugin" ); break;
00240 case KMail::ContentsTypeJournal:
00241 s << QString( "kontact_journalplugin" ); break;
00242 default: assert( false );
00243 }
00244 kapp->dcopClient()->send( "kontact", "KontactIface", "selectPlugin(QString)", arg );
00245 break;
00246 }
00247 default: break;
00248 }
00249 }
00250
00251 void FavoriteFolderView::itemClicked(QListViewItem * item)
00252 {
00253 if ( !item ) return;
00254 if ( !item->isSelected() )
00255 item->setSelected( true );
00256 item->repaint();
00257 handleGroupwareFolder( static_cast<KMFolderTreeItem*>( item ) );
00258 }
00259
00260 void FavoriteFolderView::folderTreeSelectionChanged(KMFolder * folder)
00261 {
00262 blockSignals( true );
00263 bool found = false;
00264 for ( QListViewItemIterator it( this ); it.current(); ++it ) {
00265 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00266 if ( fti->folder() == folder && !fti->isSelected() ) {
00267 fti->setSelected( true );
00268 setCurrentItem( fti );
00269 ensureItemVisible( fti );
00270 fti->repaint();
00271 found = true;
00272 } else if ( fti->folder() != folder && fti->isSelected() ) {
00273 fti->setSelected( false );
00274 fti->repaint();
00275 }
00276 }
00277 blockSignals( false );
00278 if ( !found ) {
00279 clearSelection();
00280 setSelectionModeExt( KListView::NoSelection );
00281 setSelectionModeExt( KListView::Single );
00282 }
00283 }
00284
00285 void FavoriteFolderView::folderRemoved(KMFolder * folder)
00286 {
00287 QValueList<KMFolderTreeItem*> delItems;
00288 for ( QListViewItemIterator it( this ); it.current(); ++it ) {
00289 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00290 if ( fti->folder() == folder )
00291 delItems << fti;
00292 if ( fti == mContextMenuItem )
00293 mContextMenuItem = 0;
00294 }
00295 for ( uint i = 0; i < delItems.count(); ++i )
00296 delete delItems[i];
00297 removeFromFolderToItemMap(folder);
00298 }
00299
00300 void FavoriteFolderView::dropped(QDropEvent * e, QListViewItem * after)
00301 {
00302 QListViewItem* afterItem = after;
00303 KMFolderTree *ft = mainWidget()->folderTree();
00304 assert( ft );
00305 if ( e->source() == ft->viewport() && e->provides( "application/x-qlistviewitem" ) ) {
00306 for ( QListViewItemIterator it( ft ); it.current(); ++it ) {
00307 if ( !it.current()->isSelected() )
00308 continue;
00309 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00310 if ( !fti->folder() )
00311 continue;
00312 afterItem = addFolder( fti->folder(), prettyName( fti ), afterItem );
00313 }
00314 e->accept();
00315 }
00316 }
00317
00318 void FavoriteFolderView::contextMenu(QListViewItem * item, const QPoint & point)
00319 {
00320 KMFolderTree *ft = mainWidget()->folderTree();
00321 assert( ft );
00322 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( item );
00323 mContextMenuItem = fti;
00324 KPopupMenu contextMenu;
00325 if ( fti && fti->folder() ) {
00326 contextMenu.insertItem( SmallIconSet("editdelete"), i18n("Remove From Favorites"),
00327 this, SLOT(removeFolder()) );
00328 contextMenu.insertItem( SmallIconSet("edit"), i18n("Rename Favorite"), this, SLOT(renameFolder()) );
00329 contextMenu.insertSeparator();
00330
00331 mainWidget()->action("mark_all_as_read")->plug( &contextMenu );
00332 if ( fti->folder()->folderType() == KMFolderTypeImap || fti->folder()->folderType() == KMFolderTypeCachedImap )
00333 mainWidget()->action("refresh_folder")->plug( &contextMenu );
00334 if ( fti->folder()->isMailingListEnabled() )
00335 mainWidget()->action("post_message")->plug( &contextMenu );
00336
00337 contextMenu.insertItem( SmallIconSet("configure_shortcuts"), i18n("&Assign Shortcut..."), fti, SLOT(assignShortcut()) );
00338 contextMenu.insertItem( i18n("Expire..."), fti, SLOT(slotShowExpiryProperties()) );
00339 mainWidget()->action("modify")->plug( &contextMenu );
00340 } else {
00341 contextMenu.insertItem( SmallIconSet("bookmark_add"), i18n("Add Favorite Folder..."),
00342 this, SLOT(addFolder()) );
00343 }
00344 contextMenu.exec( point, 0 );
00345 }
00346
00347 void FavoriteFolderView::removeFolder()
00348 {
00349 delete mContextMenuItem;
00350 mContextMenuItem = 0;
00351 notifyInstancesOnChange();
00352 }
00353
00354 void FavoriteFolderView::initializeFavorites()
00355 {
00356 QValueList<int> seenInboxes = GlobalSettings::self()->favoriteFolderViewSeenInboxes();
00357 KMFolderTree *ft = mainWidget()->folderTree();
00358 assert( ft );
00359 for ( QListViewItemIterator it( ft ); it.current(); ++it ) {
00360 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00361 if ( fti->type() == KFolderTreeItem::Inbox && fti->folder() && !seenInboxes.contains( fti->folder()->id() ) ) {
00362 seenInboxes.append( fti->folder()->id() );
00363 if ( fti->folder() == kmkernel->inboxFolder() && hideLocalInbox() )
00364 continue;
00365 if ( kmkernel->iCalIface().hideResourceFolder( fti->folder() ) )
00366 continue;
00367 addFolder( fti->folder(), prettyName( fti ) );
00368 }
00369 }
00370 GlobalSettings::self()->setFavoriteFolderViewSeenInboxes( seenInboxes );
00371 }
00372
00373 void FavoriteFolderView::renameFolder()
00374 {
00375 if ( !mContextMenuItem )
00376 return;
00377 bool ok;
00378 QString name = KInputDialog::getText( i18n("Rename Favorite"), i18n("Name"), mContextMenuItem->text( 0 ), &ok, this );
00379 if ( !ok )
00380 return;
00381 mContextMenuItem->setText( 0, name );
00382 notifyInstancesOnChange();
00383 }
00384
00385 QString FavoriteFolderView::prettyName(KMFolderTreeItem * fti)
00386 {
00387 assert( fti );
00388 assert( fti->folder() );
00389 QString name = fti->folder()->label();
00390 QListViewItem *accountFti = fti;
00391 while ( accountFti->parent() )
00392 accountFti = accountFti->parent();
00393 if ( fti->type() == KFolderTreeItem::Inbox ) {
00394 if ( fti->protocol() == KFolderTreeItem::Local || fti->protocol() == KFolderTreeItem::NONE ) {
00395 name = i18n( "Local Inbox" );
00396 } else {
00397 name = i18n( "Inbox of %1" ).arg( accountFti->text( 0 ) );
00398 }
00399 } else {
00400 if ( fti->protocol() != KFolderTreeItem::Local && fti->protocol() != KFolderTreeItem::NONE ) {
00401 name = i18n( "%1 on %2" ).arg( fti->text( 0 ) ).arg( accountFti->text( 0 ) );
00402 } else {
00403 name = i18n( "%1 (local)" ).arg( fti->text( 0 ) );
00404 }
00405 }
00406 return name;
00407 }
00408
00409 void FavoriteFolderView::contentsDragEnterEvent(QDragEnterEvent * e)
00410 {
00411 if ( e->provides( "application/x-qlistviewitem" ) ) {
00412 setDropVisualizer( true );
00413 setDropHighlighter( false );
00414 } else if ( e->provides( KPIM::MailListDrag::format() ) ) {
00415 setDropVisualizer( false );
00416 setDropHighlighter( true );
00417 } else {
00418 setDropVisualizer( false );
00419 setDropHighlighter( false );
00420 }
00421 FolderTreeBase::contentsDragEnterEvent( e );
00422 }
00423
00424 void FavoriteFolderView::readColorConfig()
00425 {
00426 FolderTreeBase::readColorConfig();
00427 KConfig* conf = KMKernel::config();
00428
00429 KConfigGroupSaver saver(conf, "Reader");
00430 QColor c = KGlobalSettings::alternateBackgroundColor();
00431 if ( !conf->readBoolEntry("defaultColors", true) )
00432 mPaintInfo.colBack = conf->readColorEntry( "AltBackgroundColor",&c );
00433 else
00434 mPaintInfo.colBack = c;
00435
00436 QPalette newPal = palette();
00437 newPal.setColor( QColorGroup::Base, mPaintInfo.colBack );
00438 setPalette( newPal );
00439 }
00440
00441 void FavoriteFolderView::addFolder()
00442 {
00443 KMFolderSelDlg dlg( mainWidget(), i18n("Add Favorite Folder"), false );
00444 if ( dlg.exec() != QDialog::Accepted )
00445 return;
00446 KMFolder *folder = dlg.folder();
00447 if ( !folder )
00448 return;
00449 KMFolderTreeItem *fti = findFolderTreeItem( folder );
00450 addFolder( folder, fti ? prettyName( fti ) : folder->label() );
00451 }
00452
00453 void KMail::FavoriteFolderView::addFolder(KMFolderTreeItem * fti)
00454 {
00455 if ( !fti || !fti->folder() )
00456 return;
00457 addFolder( fti->folder(), prettyName( fti ) );
00458 }
00459
00460 KMFolderTreeItem * FavoriteFolderView::findFolderTreeItem(KMFolder * folder) const
00461 {
00462 assert( folder );
00463 KMFolderTree *ft = mainWidget()->folderTree();
00464 assert( ft );
00465 for ( QListViewItemIterator it( ft ); it.current(); ++it ) {
00466 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00467 if ( fti->folder() == folder )
00468 return fti;
00469 }
00470 return 0;
00471 }
00472
00473 void FavoriteFolderView::checkMail()
00474 {
00475 bool found = false;
00476 for ( QListViewItemIterator it( this ); it.current(); ++it ) {
00477 KMFolderTreeItem *fti = static_cast<KMFolderTreeItem*>( it.current() );
00478 if ( fti->folder()->folderType() == KMFolderTypeImap || fti->folder()->folderType() == KMFolderTypeCachedImap ) {
00479 if ( !found )
00480 if ( !kmkernel->askToGoOnline() )
00481 break;
00482 found = true;
00483 if ( fti->folder()->folderType() == KMFolderTypeImap ) {
00484 KMFolderImap *imap = static_cast<KMFolderImap*>( fti->folder()->storage() );
00485 imap->getAndCheckFolder();
00486 } else if ( fti->folder()->folderType() == KMFolderTypeCachedImap ) {
00487 KMFolderCachedImap* f = static_cast<KMFolderCachedImap*>( fti->folder()->storage() );
00488 f->account()->processNewMailSingleFolder( fti->folder() );
00489 }
00490 }
00491 }
00492 }
00493
00494 void FavoriteFolderView::notifyInstancesOnChange()
00495 {
00496 if ( mReadingConfig )
00497 return;
00498 writeConfig();
00499 for ( QValueList<FavoriteFolderView*>::ConstIterator it = mInstances.begin(); it != mInstances.end(); ++it ) {
00500 if ( (*it) == this || (*it)->mReadingConfig )
00501 continue;
00502 (*it)->readConfig();
00503 }
00504 }
00505
00506 void FavoriteFolderView::refresh()
00507 {
00508 for ( QListViewItemIterator it( this ) ; it.current() ; ++it ) {
00509 KMFolderTreeItem* fti = static_cast<KMFolderTreeItem*>(it.current());
00510 if (!fti || !fti->folder())
00511 continue;
00512 fti->repaint();
00513 }
00514 update();
00515 }
00516
00517 #include "favoritefolderview.moc"