00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kalarm.h"
00022 #include "traywindow.moc"
00023
00024 #include "alarmcalendar.h"
00025 #include "alarmlistview.h"
00026 #include "alarmresources.h"
00027 #include "alarmtext.h"
00028 #include "functions.h"
00029 #include "kalarmapp.h"
00030 #include "mainwindow.h"
00031 #include "messagewin.h"
00032 #include "newalarmaction.h"
00033 #include "prefdlg.h"
00034 #include "preferences.h"
00035 #include "templatemenuaction.h"
00036
00037 #include <stdlib.h>
00038
00039 #include <QToolTip>
00040 #include <QMouseEvent>
00041 #include <QList>
00042
00043 #include <kactioncollection.h>
00044 #include <ktoggleaction.h>
00045 #include <kapplication.h>
00046 #include <klocale.h>
00047 #include <kaboutdata.h>
00048 #include <kmenu.h>
00049 #include <kmessagebox.h>
00050 #include <kstandarddirs.h>
00051 #include <kstandardaction.h>
00052 #include <kstandardguiitem.h>
00053 #include <kiconeffect.h>
00054 #include <kconfig.h>
00055 #include <kdebug.h>
00056
00057
00058 struct TipItem
00059 {
00060 QDateTime dateTime;
00061 QString text;
00062 };
00063
00064
00065
00066
00067
00068
00069
00070 TrayWindow::TrayWindow(MainWindow* parent)
00071 : KSystemTrayIcon(parent),
00072 mAssocMainWindow(parent)
00073 {
00074 kDebug();
00075
00076 mIconEnabled = loadIcon("kalarm");
00077 if (mIconEnabled.isNull())
00078 KMessageBox::sorry(parent, i18nc("@info", "Cannot load system tray icon."));
00079 else
00080 {
00081 KIconLoader loader;
00082 QImage iconDisabled = mIconEnabled.pixmap(loader.currentSize(KIconLoader::Panel)).toImage();
00083 KIconEffect::toGray(iconDisabled, 1.0);
00084 mIconDisabled = QIcon(QPixmap::fromImage(iconDisabled));
00085 }
00086 #ifdef __GNUC__
00087 #warning How to implement drag-and-drop?
00088 #endif
00089
00090
00091
00092 KActionCollection* actions = actionCollection();
00093 KAction* a = KAlarm::createAlarmEnableAction(this);
00094 actions->addAction(QLatin1String("tAlarmsEnable"), a);
00095 contextMenu()->addAction(a);
00096 connect(theApp(), SIGNAL(alarmEnabledToggled(bool)), SLOT(setEnabledStatus(bool)));
00097
00098 mActionNew = new NewAlarmAction(false, i18nc("@action", "&New Alarm"), this);
00099 actions->addAction(QLatin1String("tNew"), mActionNew);
00100 contextMenu()->addAction(mActionNew);
00101 connect(mActionNew, SIGNAL(selected(EditAlarmDlg::Type)), SLOT(slotNewAlarm(EditAlarmDlg::Type)));
00102
00103 mActionNewFromTemplate = KAlarm::createNewFromTemplateAction(i18nc("@action", "New Alarm From &Template"), actions, QLatin1String("tNewFromTempl"));
00104 contextMenu()->addAction(mActionNewFromTemplate);
00105 connect(mActionNewFromTemplate, SIGNAL(selected(const KAEvent*)), SLOT(slotNewFromTemplate(const KAEvent*)));
00106 contextMenu()->addAction(KStandardAction::preferences(this, SLOT(slotPreferences()), actions));
00107
00108
00109 const char* quitName = KStandardAction::name(KStandardAction::Quit);
00110 delete actions->action(quitName);
00111 KStandardAction::quit(this, SLOT(slotQuit()), actions);
00112
00113
00114 setEnabledStatus(theApp()->alarmsEnabled());
00115
00116 connect(AlarmResources::instance(), SIGNAL(resourceStatusChanged(AlarmResource*, AlarmResources::Change)), SLOT(slotResourceStatusChanged()));
00117 connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(slotActivated(QSystemTrayIcon::ActivationReason)));
00118 slotResourceStatusChanged();
00119 }
00120
00121 TrayWindow::~TrayWindow()
00122 {
00123 kDebug();
00124 theApp()->removeWindow(this);
00125 emit deleted();
00126 }
00127
00128
00129
00130
00131
00132 void TrayWindow::slotResourceStatusChanged()
00133 {
00134
00135 bool active = AlarmResources::instance()->activeCount(AlarmResource::ACTIVE, true);
00136 mActionNew->setEnabled(active);
00137 mActionNewFromTemplate->setEnabled(active);
00138 }
00139
00140
00141
00142
00143 void TrayWindow::slotNewAlarm(EditAlarmDlg::Type type)
00144 {
00145 KAlarm::editNewAlarm(type);
00146 }
00147
00148
00149
00150
00151 void TrayWindow::slotNewFromTemplate(const KAEvent* event)
00152 {
00153 KAlarm::editNewAlarm(event);
00154 }
00155
00156
00157
00158
00159 void TrayWindow::slotPreferences()
00160 {
00161 KAlarmPrefDlg::display();
00162 }
00163
00164
00165
00166
00167 void TrayWindow::slotQuit()
00168 {
00169 theApp()->doQuit(parentWidget());
00170 }
00171
00172
00173
00174
00175
00176 void TrayWindow::setEnabledStatus(bool status)
00177 {
00178 kDebug() << (int)status;
00179 setIcon(status ? mIconEnabled : mIconDisabled);
00180 }
00181
00182
00183
00184
00185
00186
00187 void TrayWindow::slotActivated(QSystemTrayIcon::ActivationReason reason)
00188 {
00189 if (reason == QSystemTrayIcon::Trigger)
00190 {
00191
00192 if (mAssocMainWindow && mAssocMainWindow->isVisible())
00193 {
00194 mAssocMainWindow->raise();
00195 mAssocMainWindow->activateWindow();
00196 }
00197 }
00198 else if (reason == QSystemTrayIcon::MiddleClick)
00199 {
00200 if (mActionNew->isEnabled())
00201 mActionNew->trigger();
00202 }
00203 }
00204
00205
00206
00207
00208 void TrayWindow::dragEnterEvent(QDragEnterEvent* e)
00209 {
00210 MainWindow::executeDragEnterEvent(e, 0);
00211 }
00212
00213
00214
00215
00216
00217 void TrayWindow::dropEvent(QDropEvent* e)
00218 {
00219 MainWindow::executeDropEvent(0, e);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 bool TrayWindow::event(QEvent* e)
00229 {
00230 if (e->type() != QEvent::ToolTip)
00231 return KSystemTrayIcon::event(e);
00232 QHelpEvent* he = (QHelpEvent*)e;
00233 bool enabled = theApp()->alarmsEnabled();
00234 QString altext;
00235 if (enabled && Preferences::tooltipAlarmCount())
00236 altext = tooltipAlarmText();
00237 QString text;
00238 if (enabled)
00239 text = i18nc("@info:tooltip", "%1%2", KGlobal::mainComponent().aboutData()->programName(), altext);
00240 else
00241 text = i18nc("@info:tooltip 'KAlarm - disabled'", "%1 - disabled", KGlobal::mainComponent().aboutData()->programName());
00242 kDebug() << text;
00243 QToolTip::showText(he->globalPos(), text);
00244 return true;
00245 }
00246
00247
00248
00249
00250
00251 QString TrayWindow::tooltipAlarmText() const
00252 {
00253 KAEvent event;
00254 const QString& prefix = Preferences::tooltipTimeToPrefix();
00255 int maxCount = Preferences::tooltipAlarmCount();
00256 KDateTime now = KDateTime::currentLocalDateTime();
00257 KDateTime tomorrow = now.addDays(1);
00258
00259
00260 int i, iend;
00261 QList<TipItem> items;
00262 KAEvent::List events = AlarmCalendar::resources()->events(KDateTime(now.date(), QTime(0,0,0), KDateTime::LocalZone), tomorrow, KCalEvent::ACTIVE);
00263 for (i = 0, iend = events.count(); i < iend; ++i)
00264 {
00265 KAEvent* event = events[i];
00266 if (event->enabled() && !event->expired() && event->action() == KAEvent::MESSAGE)
00267 {
00268 TipItem item;
00269 QDateTime dateTime = event->nextTrigger(KAEvent::DISPLAY_TRIGGER).effectiveKDateTime().toLocalZone().dateTime();
00270 if (dateTime > tomorrow.dateTime())
00271 continue;
00272 item.dateTime = dateTime;
00273
00274
00275 if (Preferences::showTooltipAlarmTime())
00276 {
00277 item.text += KGlobal::locale()->formatTime(item.dateTime.time());
00278 item.text += QLatin1Char(' ');
00279 }
00280 if (Preferences::showTooltipTimeToAlarm())
00281 {
00282 int mins = (now.dateTime().secsTo(item.dateTime) + 59) / 60;
00283 if (mins < 0)
00284 mins = 0;
00285 char minutes[3] = "00";
00286 minutes[0] = (mins%60) / 10 + '0';
00287 minutes[1] = (mins%60) % 10 + '0';
00288 if (Preferences::showTooltipAlarmTime())
00289 item.text += i18nc("@info/plain prefix + hours:minutes", "(%1%2:%3)", prefix, mins/60, minutes);
00290 else
00291 item.text += i18nc("@info/plain prefix + hours:minutes", "%1%2:%3", prefix, mins/60, minutes);
00292 item.text += QLatin1Char(' ');
00293 }
00294 item.text += AlarmText::summary(event);
00295
00296
00297 int it = 0;
00298 for (int itend = items.count(); it < itend; ++it)
00299 {
00300 if (item.dateTime <= items[it].dateTime)
00301 break;
00302 }
00303 items.insert(it, item);
00304 }
00305 }
00306 kDebug();
00307 QString text;
00308 int count = 0;
00309 for (i = 0, iend = items.count(); i < iend; ++i)
00310 {
00311 kDebug() << "--" << (count+1) << ")" << items[i].text;
00312 text += "<br />" + items[i].text;
00313 if (++count == maxCount)
00314 break;
00315 }
00316 return text;
00317 }
00318
00319
00320
00321
00322 void TrayWindow::removeWindow(MainWindow* win)
00323 {
00324 if (win == mAssocMainWindow)
00325 mAssocMainWindow = 0;
00326 }