22 #include "traywindow.moc"
36 #include <kalarmcal/alarmtext.h>
38 #include <kactioncollection.h>
39 #include <ktoggleaction.h>
40 #include <kapplication.h>
42 #include <kaboutdata.h>
44 #include <kmessagebox.h>
45 #include <kstandarddirs.h>
46 #include <kstandardaction.h>
47 #include <kstandardguiitem.h>
48 #include <kiconeffect.h>
53 #include <QMouseEvent>
60 using namespace KAlarmCal;
76 mAssocMainWindow(parent),
80 mStatusUpdateTimer(new QTimer(this)),
81 mHaveDisabledAlarms(false)
84 setToolTipIconByName(QLatin1String(
"kalarm"));
85 setToolTipTitle(KGlobal::mainComponent().aboutData()->programName());
86 setIconByName(QLatin1String(
"kalarm"));
89 mIconDisabled.addPixmap(KIconLoader::global()->loadIcon(QLatin1String(
"kalarm-disabled"), KIconLoader::Panel));
90 setStatus(KStatusNotifierItem::Active);
93 KActionCollection* actions = actionCollection();
94 mActionEnabled = KAlarm::createAlarmEnableAction(
this);
95 actions->addAction(QLatin1String(
"tAlarmsEnable"), mActionEnabled);
96 contextMenu()->addAction(mActionEnabled);
97 connect(
theApp(), SIGNAL(alarmEnabledToggled(
bool)), SLOT(setEnabledStatus(
bool)));
98 contextMenu()->addSeparator();
100 mActionNew =
new NewAlarmAction(
false, i18nc(
"@action",
"&New Alarm"),
this);
101 actions->addAction(QLatin1String(
"tNew"), mActionNew);
102 contextMenu()->addAction(mActionNew);
104 connect(mActionNew->
fromTemplateAlarmAction(), SIGNAL(selected(
const KAEvent*)), SLOT(slotNewFromTemplate(
const KAEvent*)));
105 contextMenu()->addSeparator();
107 KAction* a = KAlarm::createStopPlayAction(
this);
108 actions->addAction(QLatin1String(
"tStopPlay"), a);
109 contextMenu()->addAction(a);
110 QObject::connect(
theApp(), SIGNAL(audioPlaying(
bool)), a, SLOT(setVisible(
bool)));
111 QObject::connect(
theApp(), SIGNAL(audioPlaying(
bool)), SLOT(updateStatus()));
113 a = KAlarm::createSpreadWindowsAction(
this);
114 actions->addAction(QLatin1String(
"tSpread"), a);
115 contextMenu()->addAction(a);
116 contextMenu()->addSeparator();
117 contextMenu()->addAction(KStandardAction::preferences(
this, SLOT(slotPreferences()), actions));
120 const char* quitName = KStandardAction::name(KStandardAction::Quit);
121 QAction* qa = actions->action(QLatin1String(quitName));
122 disconnect(qa, SIGNAL(triggered(
bool)), 0, 0);
123 connect(qa, SIGNAL(triggered(
bool)), SLOT(slotQuit()));
126 setEnabledStatus(
theApp()->alarmsEnabled());
129 connect(
this, SIGNAL(activateRequested(
bool,QPoint)), SLOT(slotActivateRequested()));
130 connect(
this, SIGNAL(secondaryActivateRequested(QPoint)), SLOT(slotSecondaryActivateRequested()));
137 mToolTipUpdateTimer =
new QTimer(
this);
138 mToolTipUpdateTimer->setInterval(0);
139 mToolTipUpdateTimer->setSingleShot(
true);
140 connect(mToolTipUpdateTimer, SIGNAL(timeout()), SLOT(updateToolTip()));
148 mToolTipUpdateTimer, SLOT(start()));
150 mToolTipUpdateTimer, SLOT(start()));
152 mToolTipUpdateTimer, SLOT(start()));
154 mToolTipUpdateTimer, SLOT(start()));
156 mToolTipUpdateTimer, SLOT(start()));
159 mToolTipUpdateTimer, SLOT(start()));
161 mToolTipUpdateTimer, SLOT(start()));
163 mToolTipUpdateTimer, SLOT(start()));
165 mToolTipUpdateTimer, SLOT(start()));
167 mToolTipUpdateTimer, SLOT(start()));
171 mStatusUpdateTimer->setSingleShot(
true);
172 connect(mStatusUpdateTimer, SIGNAL(timeout()), SLOT(updateStatus()));
193 KAlarm::editNewAlarm(type);
200 void TrayWindow::slotNewFromTemplate(
const KAEvent* event)
202 KAlarm::editNewAlarm(event);
208 void TrayWindow::slotPreferences()
221 void TrayWindow::slotQuit()
224 QTimer::singleShot(1,
this, SLOT(slotQuitAfter()));
226 void TrayWindow::slotQuitAfter()
235 void TrayWindow::setEnabledStatus(
bool status)
237 kDebug() << (int)status;
247 void TrayWindow::slotHaveDisabledAlarms(
bool haveDisabled)
249 kDebug() << haveDisabled;
250 mHaveDisabledAlarms = haveDisabled;
258 void TrayWindow::slotActivateRequested()
261 if (mAssocMainWindow && mAssocMainWindow->isVisible())
263 mAssocMainWindow->raise();
264 mAssocMainWindow->activateWindow();
271 void TrayWindow::slotSecondaryActivateRequested()
273 if (mActionNew->isEnabled())
274 mActionNew->trigger();
282 void TrayWindow::updateStatus()
284 mStatusUpdateTimer->stop();
296 active =
static_cast<bool>(event);
297 if (event && period > 0)
299 KDateTime dt =
event->nextTrigger(KAEvent::ALL_TRIGGER).effectiveKDateTime();
300 qint64 delay = KDateTime::currentLocalDateTime().secsTo_long(dt);
301 delay -=
static_cast<qint64
>(period) * 60;
302 active = (delay <= 0);
308 int delay_int =
static_cast<int>(delay);
309 if (delay_int != delay)
311 mStatusUpdateTimer->setInterval(delay_int);
312 mStatusUpdateTimer->start();
317 setStatus(active ? Active : Passive);
325 void TrayWindow::updateToolTip()
330 subTitle = tooltipAlarmText();
333 subTitle = i18n(
"Disabled");
334 else if (mHaveDisabledAlarms)
336 if (!subTitle.isEmpty())
337 subTitle += QLatin1String(
"<br/>");
338 subTitle += i18nc(
"@info:tooltip Brief: some alarms are disabled",
"(Some alarms disabled)");
340 setToolTipSubTitle(subTitle);
346 void TrayWindow::updateIcon()
348 if (
theApp()->alarmsEnabled())
349 setIconByName(mHaveDisabledAlarms ? QLatin1String(
"kalarm-partdisabled") : QLatin1String(
"kalarm"));
351 setIconByPixmap(mIconDisabled);
358 QString TrayWindow::tooltipAlarmText()
const
363 KDateTime now = KDateTime::currentLocalDateTime();
364 KDateTime tomorrow = now.addDays(1);
370 QVector<KAEvent> events = KAlarm::getSortedActiveEvents(const_cast<TrayWindow*>(
this), &mAlarmsModel);
372 KAEvent::List events = KAlarm::getSortedActiveEvents(KDateTime(now.date(), QTime(0,0,0), KDateTime::LocalZone), tomorrow);
374 for (i = 0, iend = events.count(); i < iend; ++i)
377 KAEvent*
event = &events[i];
379 KAEvent*
event = events[i];
381 if (event->actionSubType() == KAEvent::MESSAGE)
384 QDateTime dateTime =
event->nextTrigger(KAEvent::DISPLAY_TRIGGER).effectiveKDateTime().toLocalZone().dateTime();
385 if (dateTime > tomorrow.dateTime())
391 item.dateTime = dateTime;
396 item.text += KGlobal::locale()->formatTime(item.dateTime.time());
397 item.text += QLatin1Char(
' ');
401 int mins = (now.dateTime().secsTo(item.dateTime) + 59) / 60;
404 char minutes[3] =
"00";
405 minutes[0] =
static_cast<char>((mins%60) / 10 +
'0');
406 minutes[1] =
static_cast<char>((mins%60) % 10 +
'0');
408 item.text += i18nc(
"@info/plain prefix + hours:minutes",
"(%1%2:%3)", prefix, mins/60, QLatin1String(minutes));
410 item.text += i18nc(
"@info/plain prefix + hours:minutes",
"%1%2:%3", prefix, mins/60, QLatin1String(minutes));
411 item.text += QLatin1Char(
' ');
413 item.text += AlarmText::summary(*event);
417 for (
int itend = items.count(); it < itend; ++it)
419 if (item.dateTime <= items[it].dateTime)
422 items.insert(it, item);
428 for (i = 0, iend = items.count(); i < iend; ++i)
430 kDebug() <<
"--" << (count+1) <<
")" << items[i].text;
432 text += QLatin1String(
"<br />");
433 text += items[i].text;
434 if (++count == maxCount)
445 if (win == mAssocMainWindow)
446 mAssocMainWindow = 0;
static EventListModel * alarms()
bool alarmsEnabled() const
static bool isAudioPlaying()
virtual QByteArray text(quint32 serialNumber) const =0
static AlarmCalendar * resources()
static int autoHideSystemTray()
Get Auto-hide in system tray if no alarms due within period.
TemplateMenuAction * fromTemplateAlarmAction() const
static bool showTooltipAlarmTime()
Get Show alarm times in system tray tooltip.
the KAlarm application object
void removeWindow(MainWindow *)
KAEvent * earliestAlarm() const
void removeWindow(TrayWindow *)
static bool showTooltipTimeToAlarm()
Get Show time to alarms in system tray tooltip.
displays an alarm message
static void connect(QObject *receiver, const char *member)
static void connect(const char *signal, const QObject *receiver, const char *member)
TrayWindow(MainWindow *parent)
static int tooltipAlarmCount()
Get Number of alarms to show in system tray tooltip.
void doQuit(QWidget *parent)
static AlarmListModel * all()
Return the model containing all active and archived alarms.
static QString tooltipTimeToPrefix()
Get Time-to-alarm prefix in system tray tooltip.