21 #include "kmsystemtray.h" 23 #include "kmfoldertree.h" 24 #include "kmfoldermgr.h" 25 #include "kmfolderimap.h" 26 #include "kmmainwidget.h" 27 #include "accountmanager.h" 29 #include "globalsettings.h" 31 #include <tdeapplication.h> 32 #include <tdemainwindow.h> 33 #include <tdeglobalsettings.h> 34 #include <kiconloader.h> 35 #include <kiconeffect.h> 38 #include <tdepopupmenu.h> 40 #include <tqpainter.h> 42 #include <tqtooltip.h> 43 #include <tqwidgetlist.h> 44 #include <tqobjectlist.h> 61 : KSystemTray( parent, name ),
62 mParentVisible( true ),
63 mPosOfMainWin( 0, 0 ),
64 mDesktopOfMainWin( 0 ),
65 mMode( GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ),
67 mNewMessagePopupId(-1),
70 setAlignment( AlignCenter );
71 kdDebug(5006) <<
"Initting systray" << endl;
73 mLastUpdate = time( 0 );
74 mUpdateTimer =
new TQTimer(
this,
"systraytimer" );
75 connect( mUpdateTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( updateNewMessages() ) );
77 mDefaultIcon = loadIcon(
"kmail" );
78 mLightIconImage = loadIcon(
"kmaillight" ).convertToImage();
80 setPixmap(mDefaultIcon);
82 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
84 TQWidget * mainWin = mainWidget->topLevelWidget();
86 mDesktopOfMainWin = KWin::windowInfo( mainWin->winId(),
87 NET::WMDesktop ).desktop();
88 mPosOfMainWin = mainWin->pos();
93 kmkernel->registerSystemTrayApplet(
this );
98 connect( kmkernel->folderMgr(), TQT_SIGNAL(changed()), TQT_SLOT(
foldersChanged()));
99 connect( kmkernel->imapFolderMgr(), TQT_SIGNAL(changed()), TQT_SLOT(
foldersChanged()));
100 connect( kmkernel->dimapFolderMgr(), TQT_SIGNAL(changed()), TQT_SLOT(
foldersChanged()));
101 connect( kmkernel->searchFolderMgr(), TQT_SIGNAL(changed()), TQT_SLOT(
foldersChanged()));
103 connect( kmkernel->acctMgr(), TQT_SIGNAL( checkedMail(
bool,
bool,
const TQMap<TQString, int> & ) ),
104 TQT_SLOT( updateNewMessages() ) );
106 connect(
this, TQT_SIGNAL( quitSelected() ), TQT_SLOT( tray_quit() ) );
109 void KMSystemTray::buildPopupMenu()
114 mPopupMenu =
new TDEPopupMenu();
115 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
119 mPopupMenu->insertTitle(*(this->pixmap()),
"KMail");
121 if ( ( action = mainWidget->action(
"check_mail") ) )
122 action->plug( mPopupMenu );
123 if ( ( action = mainWidget->action(
"check_mail_in") ) )
124 action->plug( mPopupMenu );
125 if ( ( action = mainWidget->action(
"send_queued") ) )
126 action->plug( mPopupMenu );
127 if ( ( action = mainWidget->action(
"send_queued_via") ) )
128 action->plug( mPopupMenu );
129 mPopupMenu->insertSeparator();
130 if ( ( action = mainWidget->action(
"new_message") ) )
131 action->plug( mPopupMenu );
132 if ( ( action = mainWidget->action(
"kmail_configure_kmail") ) )
133 action->plug( mPopupMenu );
134 mPopupMenu->insertSeparator();
136 mPopupMenu->insertItem( SmallIcon(
"system-log-out"), i18n(
"&Quit"),
this, TQT_SLOT(maybeQuit()) );
139 void KMSystemTray::tray_quit()
148 kmkernel->unregisterSystemTrayApplet(
this );
154 void KMSystemTray::setMode(
int newMode)
156 if(newMode == mMode)
return;
158 kdDebug(5006) <<
"Setting systray mMode to " << newMode << endl;
162 case GlobalSettings::EnumSystemTrayPolicy::ShowAlways:
166 case GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread:
167 if ( mCount == 0 && !isHidden() )
169 else if ( mCount > 0 && isHidden() )
173 kdDebug(5006) << k_funcinfo <<
" Unknown systray mode " << mMode << endl;
177 int KMSystemTray::mode()
const 182 void KMSystemTray::resizeEvent(TQResizeEvent *)
196 int oldPixmapWidth = pixmap()->size().width();
197 int oldPixmapHeight = pixmap()->size().height();
199 TQString countString = TQString::number( mCount );
200 TQFont countFont = TDEGlobalSettings::generalFont();
201 countFont.setBold(
true);
206 int realIconHeight = height();
207 if (realIconHeight < 22) {
208 countFont.setPointSizeFloat( countFont.pointSizeFloat() * 2.0 );
213 float countFontSize = countFont.pointSizeFloat();
214 TQFontMetrics qfm( countFont );
215 int width = qfm.width( countString );
216 if( width > oldPixmapWidth )
218 countFontSize *= float( oldPixmapWidth ) / float( width );
219 countFont.setPointSizeFloat( countFontSize );
239 TQPixmap numberPixmap( oldPixmapWidth, oldPixmapHeight );
240 numberPixmap.fill( TQt::white );
241 TQPainter p( &numberPixmap );
242 p.setFont( countFont );
243 p.setPen( TQt::blue );
244 p.drawText( numberPixmap.rect(), TQt::AlignCenter, countString );
245 numberPixmap.setMask( numberPixmap.createHeuristicMask() );
246 TQImage numberImage = numberPixmap.convertToImage();
249 TQImage iconWithNumberImage = mLightIconImage.copy();
250 TDEIconEffect::overlay( iconWithNumberImage, numberImage );
252 TQPixmap iconWithNumber;
253 iconWithNumber.convertFromImage( iconWithNumberImage );
254 setPixmap( iconWithNumber );
257 setPixmap( mDefaultIcon );
271 mFoldersWithUnread.clear();
274 if ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ) {
279 disconnect(
this, TQT_SLOT(updateNewMessageNotification(
KMFolder *)));
281 TQStringList folderNames;
282 TQValueList<TQGuardedPtr<KMFolder> > folderList;
283 kmkernel->folderMgr()->createFolderList(&folderNames, &folderList);
284 kmkernel->imapFolderMgr()->createFolderList(&folderNames, &folderList);
285 kmkernel->dimapFolderMgr()->createFolderList(&folderNames, &folderList);
286 kmkernel->searchFolderMgr()->createFolderList(&folderNames, &folderList);
288 TQStringList::iterator strIt = folderNames.begin();
290 for(TQValueList<TQGuardedPtr<KMFolder> >::iterator it = folderList.begin();
291 it != folderList.end() && strIt != folderNames.end(); ++it, ++strIt)
294 TQString currentName = *strIt;
296 if ( ((!currentFolder->
isSystemFolder() || (currentFolder->name().lower() ==
"inbox")) ||
297 (currentFolder->
folderType() == KMFolderTypeImap)) &&
301 connect(currentFolder, TQT_SIGNAL(numUnreadMsgsChanged(
KMFolder *)),
302 this, TQT_SLOT(updateNewMessageNotification(
KMFolder *)));
305 updateNewMessageNotification(currentFolder);
308 disconnect(currentFolder, TQT_SIGNAL(numUnreadMsgsChanged(
KMFolder *)),
this, TQT_SLOT(updateNewMessageNotification(
KMFolder *)) );
320 if( e->button() == Qt::LeftButton )
322 if( mParentVisible && mainWindowIsOnCurrentDesktop() )
329 if( e->button() == Qt::RightButton )
331 mPopupFolders.clear();
332 mPopupFolders.reserve( mFoldersWithUnread.count() );
338 if(mNewMessagePopupId != -1)
340 mPopupMenu->removeItem(mNewMessagePopupId);
343 if(mFoldersWithUnread.count() > 0)
345 TDEPopupMenu *newMessagesPopup =
new TDEPopupMenu();
347 TQMap<TQGuardedPtr<KMFolder>,
int>::Iterator it = mFoldersWithUnread.begin();
348 for(uint i=0; it != mFoldersWithUnread.end(); ++i)
350 kdDebug(5006) <<
"Adding folder" << endl;
351 mPopupFolders.append( it.key() );
352 TQString item =
prettyName(it.key()) +
" (" + TQString::number(it.data()) +
")";
353 newMessagesPopup->insertItem(item,
this, TQT_SLOT(selectedAccount(
int)), 0, i);
357 mNewMessagePopupId = mPopupMenu->insertItem(i18n(
"New Messages In"),
358 newMessagesPopup, mNewMessagePopupId, 3);
360 kdDebug(5006) <<
"Folders added" << endl;
363 mPopupMenu->popup(e->globalPos());
374 TQString rvalue = fldr->
label();
377 KMFolderImap * imap =
dynamic_cast<KMFolderImap*
> (fldr->storage());
380 if((imap->account() != 0) &&
381 (imap->account()->name() != 0) )
383 kdDebug(5006) <<
"IMAP folder, prepend label with type" << endl;
384 rvalue = imap->account()->name() +
"->" + rvalue;
388 kdDebug(5006) <<
"Got label " << rvalue << endl;
394 bool KMSystemTray::mainWindowIsOnCurrentDesktop()
396 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
400 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
404 return KWin::windowInfo( mainWin->winId(),
405 NET::WMDesktop ).isOnCurrentDesktop();
414 if (!kmkernel->getKMMainWidget())
416 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
420 KWin::WindowInfo cur = KWin::windowInfo( mainWin->winId(), NET::WMDesktop );
421 if ( cur.valid() ) mDesktopOfMainWin = cur.desktop();
423 if ( mDesktopOfMainWin != NET::OnAllDesktops )
424 KWin::setCurrentDesktop( mDesktopOfMainWin );
425 if ( !mParentVisible ) {
426 if ( mDesktopOfMainWin == NET::OnAllDesktops )
427 KWin::setOnAllDesktops( mainWin->winId(), true );
428 mainWin->move( mPosOfMainWin );
431 KWin::activateWindow( mainWin->winId() );
432 mParentVisible =
true;
440 void KMSystemTray::hideKMail()
442 if (!kmkernel->getKMMainWidget())
444 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
448 mDesktopOfMainWin = KWin::windowInfo( mainWin->winId(),
449 NET::WMDesktop ).desktop();
450 mPosOfMainWin = mainWin->pos();
452 KWin::iconifyWindow( mainWin->winId() );
454 mParentVisible =
false;
464 void KMSystemTray::updateNewMessageNotification(
KMFolder * fldr)
475 mPendingUpdates[ fldr ] =
true;
476 if ( time( 0 ) - mLastUpdate > 2 ) {
477 mUpdateTimer->stop();
481 mUpdateTimer->start(150,
true);
485 void KMSystemTray::updateNewMessages()
487 for ( TQMap<TQGuardedPtr<KMFolder>,
bool>::Iterator it = mPendingUpdates.begin();
488 it != mPendingUpdates.end(); ++it)
497 TQMap<TQGuardedPtr<KMFolder>,
int>::Iterator unread_it =
498 mFoldersWithUnread.find(fldr);
499 bool unmapped = (unread_it == mFoldersWithUnread.end());
503 if(unmapped) mCount += unread;
508 int diff = unread - unread_it.data();
515 mFoldersWithUnread.insert(fldr, unread);
527 if(unread == 0)
continue;
530 if ( ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread )
540 kdDebug(5006) <<
"Removing folder from internal store " << fldr->name() << endl;
543 mFoldersWithUnread.remove(fldr);
546 if(mFoldersWithUnread.count() == 0)
548 mPopupFolders.clear();
549 disconnect(
this, TQT_SLOT(selectedAccount(
int)));
553 if ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ) {
561 mPendingUpdates.clear();
565 TQToolTip::remove(
this);
566 TQToolTip::add(
this, mCount == 0 ?
567 i18n(
"There are no unread messages")
568 : i18n(
"There is 1 unread message.",
569 "There are %n unread messages.",
572 mLastUpdate = time( 0 );
580 void KMSystemTray::selectedAccount(
int id)
584 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
587 kmkernel->openReader();
588 mainWidget = kmkernel->getKMMainWidget();
594 KMFolder * fldr = mPopupFolders.at(
id);
596 KMFolderTree * ft = mainWidget->folderTree();
598 TQListViewItem * fldrIdx = ft->indexOfFolder(fldr);
601 ft->setCurrentItem(fldrIdx);
602 ft->selectCurrentFolder();
605 bool KMSystemTray::hasUnreadMail()
const 607 return ( mCount != 0 );
610 #include "kmsystemtray.moc" void showKMail()
Shows and raises the first KMMainWidget and switches to the appropriate virtual desktop.
KMSystemTray(TQWidget *parent=0, const char *name=0)
construtor
int countUnread()
Number of new or unread messages in this folder.
void foldersChanged()
Refreshes the list of folders we are monitoring.
void mousePressEvent(TQMouseEvent *)
On left mouse click, switch focus to the first KMMainWidget.
virtual TQString label() const
Returns the label of the folder for visualization.
bool isSystemFolder() const
Returns true if the folder is a kmail system folder.
void updateCount()
Update the count of unread messages.
TQString prettyName(KMFolder *)
Return the name of the folder in which the mail is deposited, prepended with the account name if the ...
bool ignoreNewMail() const
Returns true if the user doesn't want to get notified about new mail in this folder.
The account manager is responsible for creating accounts of various types via the factory method crea...
KMFolderType folderType() const
Returns the type of this folder.
~KMSystemTray()
destructor