20 #include "kmsystemtray.h"
22 #include "kmfoldertree.h"
23 #include "kmfoldermgr.h"
24 #include "kmfolderimap.h"
25 #include "kmmainwidget.h"
26 #include "accountmanager.h"
28 #include "globalsettings.h"
30 #include <tdeapplication.h>
31 #include <tdemainwindow.h>
32 #include <tdeglobalsettings.h>
33 #include <kiconloader.h>
34 #include <kiconeffect.h>
37 #include <tdepopupmenu.h>
39 #include <tqpainter.h>
41 #include <tqtooltip.h>
42 #include <tqwidgetlist.h>
43 #include <tqobjectlist.h>
60 : KSystemTray( parent, name ),
61 mParentVisible( true ),
62 mPosOfMainWin( 0, 0 ),
63 mDesktopOfMainWin( 0 ),
64 mMode( GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ),
66 mNewMessagePopupId(-1),
69 setAlignment( AlignCenter );
70 kdDebug(5006) <<
"Initting systray" << endl;
72 mLastUpdate = time( 0 );
73 mUpdateTimer =
new TQTimer(
this,
"systraytimer" );
74 connect( mUpdateTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( updateNewMessages() ) );
76 mDefaultIcon = loadIcon(
"kmail" );
77 mLightIconImage = loadIcon(
"kmaillight" ).convertToImage();
79 setPixmap(mDefaultIcon);
81 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
83 TQWidget * mainWin = mainWidget->topLevelWidget();
85 mDesktopOfMainWin = KWin::windowInfo( mainWin->winId(),
86 NET::WMDesktop ).desktop();
87 mPosOfMainWin = mainWin->pos();
92 kmkernel->registerSystemTrayApplet(
this );
97 connect( kmkernel->folderMgr(), TQ_SIGNAL(changed()), TQ_SLOT(
foldersChanged()));
98 connect( kmkernel->imapFolderMgr(), TQ_SIGNAL(changed()), TQ_SLOT(
foldersChanged()));
99 connect( kmkernel->dimapFolderMgr(), TQ_SIGNAL(changed()), TQ_SLOT(
foldersChanged()));
100 connect( kmkernel->searchFolderMgr(), TQ_SIGNAL(changed()), TQ_SLOT(
foldersChanged()));
102 connect( kmkernel->acctMgr(), TQ_SIGNAL( checkedMail(
bool,
bool,
const TQMap<TQString, int> & ) ),
103 TQ_SLOT( updateNewMessages() ) );
105 connect(
this, TQ_SIGNAL( quitSelected() ), TQ_SLOT( tray_quit() ) );
108 void KMSystemTray::buildPopupMenu()
113 mPopupMenu =
new TDEPopupMenu();
114 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
118 mPopupMenu->insertTitle(*(this->pixmap()),
"KMail");
120 if ( ( action = mainWidget->action(
"check_mail") ) )
121 action->plug( mPopupMenu );
122 if ( ( action = mainWidget->action(
"check_mail_in") ) )
123 action->plug( mPopupMenu );
124 if ( ( action = mainWidget->action(
"send_queued") ) )
125 action->plug( mPopupMenu );
126 if ( ( action = mainWidget->action(
"send_queued_via") ) )
127 action->plug( mPopupMenu );
128 mPopupMenu->insertSeparator();
129 if ( ( action = mainWidget->action(
"new_message") ) )
130 action->plug( mPopupMenu );
131 if ( ( action = mainWidget->action(
"kmail_configure_kmail") ) )
132 action->plug( mPopupMenu );
133 mPopupMenu->insertSeparator();
135 mPopupMenu->insertItem( SmallIcon(
"system-log-out"), i18n(
"&Quit"),
this, TQ_SLOT(maybeQuit()) );
138 void KMSystemTray::tray_quit()
147 kmkernel->unregisterSystemTrayApplet(
this );
153 void KMSystemTray::setMode(
int newMode)
155 if(newMode == mMode)
return;
157 kdDebug(5006) <<
"Setting systray mMode to " << newMode << endl;
161 case GlobalSettings::EnumSystemTrayPolicy::ShowAlways:
165 case GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread:
166 if ( mCount == 0 && !isHidden() )
168 else if ( mCount > 0 && isHidden() )
172 kdDebug(5006) << k_funcinfo <<
" Unknown systray mode " << mMode << endl;
176 int KMSystemTray::mode()
const
181 void KMSystemTray::resizeEvent(TQResizeEvent *)
195 int oldPixmapWidth = pixmap()->size().width();
196 int oldPixmapHeight = pixmap()->size().height();
198 TQString countString = TQString::number( mCount );
199 TQFont countFont = TDEGlobalSettings::generalFont();
200 countFont.setBold(
true);
205 int realIconHeight = height();
206 if (realIconHeight < 22) {
207 countFont.setPointSizeFloat( countFont.pointSizeFloat() * 2.0 );
212 float countFontSize = countFont.pointSizeFloat();
213 TQFontMetrics qfm( countFont );
214 int width = qfm.width( countString );
215 if( width > oldPixmapWidth )
217 countFontSize *= float( oldPixmapWidth ) / float( width );
218 countFont.setPointSizeFloat( countFontSize );
238 TQPixmap numberPixmap( oldPixmapWidth, oldPixmapHeight );
239 numberPixmap.fill( TQt::white );
240 TQPainter p( &numberPixmap );
241 p.setFont( countFont );
242 p.setPen( TQt::blue );
243 p.drawText( numberPixmap.rect(), TQt::AlignCenter, countString );
244 numberPixmap.setMask( numberPixmap.createHeuristicMask() );
245 TQImage numberImage = numberPixmap.convertToImage();
248 TQImage iconWithNumberImage = mLightIconImage.copy();
249 TDEIconEffect::overlay( iconWithNumberImage, numberImage );
251 TQPixmap iconWithNumber;
252 iconWithNumber.convertFromImage( iconWithNumberImage );
253 setPixmap( iconWithNumber );
256 setPixmap( mDefaultIcon );
270 mFoldersWithUnread.clear();
273 if ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ) {
278 disconnect(
this, TQ_SLOT(updateNewMessageNotification(
KMFolder *)));
280 TQStringList folderNames;
281 TQValueList<TQGuardedPtr<KMFolder> > folderList;
282 kmkernel->folderMgr()->createFolderList(&folderNames, &folderList);
283 kmkernel->imapFolderMgr()->createFolderList(&folderNames, &folderList);
284 kmkernel->dimapFolderMgr()->createFolderList(&folderNames, &folderList);
285 kmkernel->searchFolderMgr()->createFolderList(&folderNames, &folderList);
287 TQStringList::iterator strIt = folderNames.begin();
289 for(TQValueList<TQGuardedPtr<KMFolder> >::iterator it = folderList.begin();
290 it != folderList.end() && strIt != folderNames.end(); ++it, ++strIt)
293 TQString currentName = *strIt;
295 if ( ((!currentFolder->
isSystemFolder() || (currentFolder->name().lower() ==
"inbox")) ||
296 (currentFolder->
folderType() == KMFolderTypeImap)) &&
300 connect(currentFolder, TQ_SIGNAL(numUnreadMsgsChanged(
KMFolder *)),
301 this, TQ_SLOT(updateNewMessageNotification(
KMFolder *)));
304 updateNewMessageNotification(currentFolder);
307 disconnect(currentFolder, TQ_SIGNAL(numUnreadMsgsChanged(
KMFolder *)),
this, TQ_SLOT(updateNewMessageNotification(
KMFolder *)) );
319 if( e->button() == TQt::LeftButton )
321 if( mParentVisible && mainWindowIsOnCurrentDesktop() )
328 if( e->button() == TQt::RightButton )
330 mPopupFolders.clear();
331 mPopupFolders.reserve( mFoldersWithUnread.count() );
337 if(mNewMessagePopupId != -1)
339 mPopupMenu->removeItem(mNewMessagePopupId);
342 if(mFoldersWithUnread.count() > 0)
344 TDEPopupMenu *newMessagesPopup =
new TDEPopupMenu();
346 TQMap<TQGuardedPtr<KMFolder>,
int>::Iterator it = mFoldersWithUnread.begin();
347 for(uint i=0; it != mFoldersWithUnread.end(); ++i)
349 kdDebug(5006) <<
"Adding folder" << endl;
350 mPopupFolders.append( it.key() );
351 TQString item =
prettyName(it.key()) +
" (" + TQString::number(it.data()) +
")";
352 newMessagesPopup->insertItem(item,
this, TQ_SLOT(selectedAccount(
int)), 0, i);
356 mNewMessagePopupId = mPopupMenu->insertItem(i18n(
"New Messages In"),
357 newMessagesPopup, mNewMessagePopupId, 3);
359 kdDebug(5006) <<
"Folders added" << endl;
362 mPopupMenu->popup(e->globalPos());
373 TQString rvalue = fldr->
label();
376 KMFolderImap * imap =
dynamic_cast<KMFolderImap*
> (fldr->storage());
379 if((imap->account() != 0) &&
380 (imap->account()->name() != 0) )
382 kdDebug(5006) <<
"IMAP folder, prepend label with type" << endl;
383 rvalue = imap->account()->name() +
"->" + rvalue;
387 kdDebug(5006) <<
"Got label " << rvalue << endl;
393 bool KMSystemTray::mainWindowIsOnCurrentDesktop()
395 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
399 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
403 return KWin::windowInfo( mainWin->winId(),
404 NET::WMDesktop ).isOnCurrentDesktop();
413 if (!kmkernel->getKMMainWidget())
415 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
419 KWin::WindowInfo cur = KWin::windowInfo( mainWin->winId(), NET::WMDesktop );
420 if ( cur.valid() ) mDesktopOfMainWin = cur.desktop();
422 if ( mDesktopOfMainWin != NET::OnAllDesktops )
423 KWin::setCurrentDesktop( mDesktopOfMainWin );
424 if ( !mParentVisible ) {
425 if ( mDesktopOfMainWin == NET::OnAllDesktops )
426 KWin::setOnAllDesktops( mainWin->winId(),
true );
427 mainWin->move( mPosOfMainWin );
430 KWin::activateWindow( mainWin->winId() );
431 mParentVisible =
true;
439 void KMSystemTray::hideKMail()
441 if (!kmkernel->getKMMainWidget())
443 TQWidget *mainWin = kmkernel->getKMMainWidget()->topLevelWidget();
447 mDesktopOfMainWin = KWin::windowInfo( mainWin->winId(),
448 NET::WMDesktop ).desktop();
449 mPosOfMainWin = mainWin->pos();
451 KWin::iconifyWindow( mainWin->winId() );
453 mParentVisible =
false;
463 void KMSystemTray::updateNewMessageNotification(
KMFolder * fldr)
474 mPendingUpdates[ fldr ] =
true;
475 if ( time( 0 ) - mLastUpdate > 2 ) {
476 mUpdateTimer->stop();
480 mUpdateTimer->start(150,
true);
484 void KMSystemTray::updateNewMessages()
486 for ( TQMap<TQGuardedPtr<KMFolder>,
bool>::Iterator it = mPendingUpdates.begin();
487 it != mPendingUpdates.end(); ++it)
496 TQMap<TQGuardedPtr<KMFolder>,
int>::Iterator unread_it =
497 mFoldersWithUnread.find(fldr);
498 bool unmapped = (unread_it == mFoldersWithUnread.end());
502 if(unmapped) mCount += unread;
507 int diff = unread - unread_it.data();
514 mFoldersWithUnread.insert(fldr, unread);
526 if(unread == 0)
continue;
529 if ( ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread )
539 kdDebug(5006) <<
"Removing folder from internal store " << fldr->name() << endl;
542 mFoldersWithUnread.remove(fldr);
545 if(mFoldersWithUnread.count() == 0)
547 mPopupFolders.clear();
548 disconnect(
this, TQ_SLOT(selectedAccount(
int)));
552 if ( mMode == GlobalSettings::EnumSystemTrayPolicy::ShowOnUnread ) {
560 mPendingUpdates.clear();
564 TQToolTip::remove(
this);
565 TQToolTip::add(
this, mCount == 0 ?
566 i18n(
"There are no unread messages")
567 : i18n(
"There is 1 unread message.",
568 "There are %n unread messages.",
571 mLastUpdate = time( 0 );
579 void KMSystemTray::selectedAccount(
int id)
583 KMMainWidget * mainWidget = kmkernel->getKMMainWidget();
586 kmkernel->openReader();
587 mainWidget = kmkernel->getKMMainWidget();
593 KMFolder * fldr = mPopupFolders.at(
id);
595 KMFolderTree * ft = mainWidget->folderTree();
597 TQListViewItem * fldrIdx = ft->indexOfFolder(fldr);
600 ft->setCurrentItem(fldrIdx);
601 ft->selectCurrentFolder();
604 bool KMSystemTray::hasUnreadMail()
const
606 return ( mCount != 0 );
609 #include "kmsystemtray.moc"
int countUnread()
Number of new or unread messages in this folder.
virtual TQString label() const
Returns the label of the folder for visualization.
bool ignoreNewMail() const
Returns true if the user doesn't want to get notified about new mail in this folder.
bool isSystemFolder() const
Returns true if the folder is a kmail system folder.
KMFolderType folderType() const
Returns the type of this folder.
TQString prettyName(KMFolder *)
Return the name of the folder in which the mail is deposited, prepended with the account name if the ...
void updateCount()
Update the count of unread messages.
void foldersChanged()
Refreshes the list of folders we are monitoring.
~KMSystemTray()
destructor
KMSystemTray(TQWidget *parent=0, const char *name=0)
construtor
void mousePressEvent(TQMouseEvent *)
On left mouse click, switch focus to the first KMMainWidget.
void showKMail()
Shows and raises the first KMMainWidget and switches to the appropriate virtual desktop.
The account manager is responsible for creating accounts of various types via the factory method crea...