Fix assert when Mail-Followup-To contains two emails, as Ossi's Mutt does
[kdepim.git] / ktimetracker / tray.cpp
blob3327981a629f5c1d4c4b585e8f47dbbf8ec8bc9a
1 /*
2 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the
16 * Free Software Foundation, Inc.
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301 USA.
23 * TrayIcon.
25 * This implements the functionality of the little icon in the kpanel
26 * tray. Among which are tool tips and the running clock animated icon
29 #include "tray.h"
31 #include <QPixmap>
32 #include <QString>
33 #include <QTimer>
34 #include <QToolTip>
35 #include <QMenu>
37 #include <KAction>
38 #include <KGlobalSettings>
39 #include <KLocale>
40 #include <KMenu>
41 #include "mainwindow.h"
42 #include "task.h"
43 #include "timetrackerwidget.h"
45 QVector<QPixmap*> *TrayIcon::icons = 0;
47 TrayIcon::TrayIcon(MainWindow* parent)
48 : KStatusNotifierItem(parent)
50 setObjectName( "Ktimetracker Tray" );
51 // the timer that updates the "running" icon in the tray
52 _taskActiveTimer = new QTimer(this);
53 connect( _taskActiveTimer, SIGNAL(timeout()), this,
54 SLOT(advanceClock()) );
56 if (icons == 0)
58 icons = new QVector<QPixmap*>(8);
59 for (int i=0; i<8; i++)
61 QPixmap *icon = new QPixmap();
62 QString name;
63 name.sprintf("active-icon-%d.xpm",i);
64 *icon = UserIcon(name);
65 icons->insert(i,icon);
68 TimetrackerWidget *timetrackerWidget = static_cast< TimetrackerWidget * >( parent->centralWidget() );
69 if ( timetrackerWidget )
71 KAction *action = timetrackerWidget->action( "configure_ktimetracker" );
72 if ( action ) contextMenu()->addAction( action );
73 action = timetrackerWidget->action( "stopAll" );
74 if ( action ) contextMenu()->addAction( action );
76 resetClock();
77 initToolTip();
80 TrayIcon::TrayIcon(ktimetrackerpart *)
81 : KStatusNotifierItem( 0 )
83 setObjectName( "Ktimetracker Tray" );
84 // it is not convenient if every kpart gets an icon in the systray.
85 _taskActiveTimer = 0;
88 TrayIcon::TrayIcon()
89 : KStatusNotifierItem( 0 )
90 // will display nothing at all
92 setObjectName( "Ktimetracker Tray" );
93 _taskActiveTimer = 0;
96 TrayIcon::~TrayIcon()
100 void TrayIcon::startClock()
102 kDebug(5970) << "Entering function";
103 if ( _taskActiveTimer )
105 _taskActiveTimer->start(1000);
106 setIconByPixmap( *(*icons)[_activeIcon] );
108 kDebug(5970) << "Leaving function";
111 void TrayIcon::stopClock()
113 kDebug(5970) << "Entering function";
114 if ( _taskActiveTimer )
116 _taskActiveTimer->stop();
118 kDebug(5970) << "Leaving function";
121 void TrayIcon::advanceClock()
123 _activeIcon = (_activeIcon+1) % 8;
124 setIconByPixmap( *(*icons)[_activeIcon]);
127 void TrayIcon::resetClock()
129 _activeIcon = 0;
130 setIconByPixmap( *(*icons)[_activeIcon]);
133 void TrayIcon::initToolTip()
135 updateToolTip(QList<Task*> ());
138 void TrayIcon::updateToolTip(QList<Task*> activeTasks)
140 if ( activeTasks.isEmpty() )
142 this->setToolTip( "ktimetracker", "ktimetracker", i18n("No active tasks") );
143 return;
146 QFontMetrics fm( QToolTip::font() );
147 const QString continued = i18n( ", ..." );
148 const int buffer = fm.boundingRect( continued ).width();
149 const int desktopWidth = KGlobalSettings::desktopGeometry(associatedWidget()).width();
150 const int maxWidth = desktopWidth - buffer;
152 QString qTip;
153 QString s;
155 // Build the tool tip with all of the names of the active tasks.
156 // If at any time the width of the tool tip is larger than the desktop,
157 // stop building it.
159 for ( int i = 0; i < activeTasks.count(); ++i )
161 Task* task = activeTasks.at( i );
162 if ( i > 0 )
163 s += i18n( ", " ) + task->name();
164 else
165 s += task->name();
166 int width = fm.boundingRect( s ).width();
167 if ( width > maxWidth )
169 qTip += continued;
170 break;
172 qTip = s;
174 this->setToolTip( "ktimetracker", "ktimetracker", qTip );
177 #include "tray.moc"