25 #include <tqtooltip.h>
27 #include <tdeapplication.h>
28 #include <tdelocale.h>
29 #include <tdeaboutdata.h>
30 #include <tdepopupmenu.h>
31 #include <tdemessagebox.h>
32 #include <kstandarddirs.h>
33 #include <kstdaction.h>
34 #include <kstdguiitem.h>
36 #include <tdeconfig.h>
39 #include "alarmcalendar.h"
40 #include "alarmlistview.h"
41 #include "alarmtext.h"
48 #include "preferences.h"
49 #include "templatemenuaction.h"
50 #include "traywindow.moc"
53 class TrayTooltip :
public TQToolTip
56 TrayTooltip(TQWidget* parent) : TQToolTip(parent) { }
57 virtual ~TrayTooltip() {}
59 virtual void maybeTip(
const TQPoint&);
74 TrayWindow::TrayWindow(MainWindow* parent,
const char* name)
75 : KSystemTray((theApp()->wantRunInSystemTray() ? parent : 0), name),
76 mAssocMainWindow(parent)
78 kdDebug(5950) <<
"TrayWindow::TrayWindow()\n";
80 mPixmapEnabled = loadIcon(
"kalarm");
81 mPixmapDisabled = loadIcon(
"kalarm_disabled");
82 if (mPixmapEnabled.isNull() || mPixmapDisabled.isNull())
83 KMessageBox::sorry(
this, i18n(
"Cannot load system tray icon."));
87 TDEActionCollection* actcol = actionCollection();
88 AlarmEnableAction* a = Daemon::createAlarmEnableAction(actcol,
"tAlarmEnable");
89 a->plug(contextMenu());
90 connect(a, TQ_SIGNAL(switched(
bool)), TQ_SLOT(setEnabledStatus(
bool)));
91 KAlarm::createNewAlarmAction(i18n(
"&New Alarm..."),
this, TQ_SLOT(slotNewAlarm()), actcol,
"tNew")->plug(contextMenu());
92 KAlarm::createNewFromTemplateAction(i18n(
"New Alarm From &Template"),
this, TQ_SLOT(slotNewFromTemplate(
const KAEvent&)), actcol,
"tNewFromTempl")->plug(contextMenu());
93 KStdAction::preferences(
this, TQ_SLOT(slotPreferences()), actcol)->plug(contextMenu());
96 const char* quitName = KStdAction::name(KStdAction::Quit);
97 actcol->remove(actcol->action(quitName));
98 actcol->tdeaccel()->remove(quitName);
99 KStdAction::quit(
this, TQ_SLOT(slotQuit()), actcol);
102 Daemon::checkStatus();
103 setEnabledStatus(Daemon::monitoringAlarms());
105 mTooltip =
new TrayTooltip(
this);
108 TrayWindow::~TrayWindow()
110 kdDebug(5950) <<
"TrayWindow::~TrayWindow()\n";
113 theApp()->removeWindow(
this);
121 void TrayWindow::contextMenuAboutToShow(TDEPopupMenu* menu)
123 KSystemTray::contextMenuAboutToShow(menu);
124 Daemon::checkStatus();
130 void TrayWindow::slotNewAlarm()
132 MainWindow::executeNew();
138 void TrayWindow::slotNewFromTemplate(
const KAEvent& event)
140 MainWindow::executeNew(event);
146 void TrayWindow::slotPreferences()
148 KAlarmPrefDlg::display();
154 void TrayWindow::slotQuit()
156 theApp()->doQuit(
this);
163 void TrayWindow::setEnabledStatus(
bool status)
165 kdDebug(5950) <<
"TrayWindow::setEnabledStatus(" << (int)status <<
")\n";
166 setPixmap(status ? mPixmapEnabled : mPixmapDisabled);
174 void TrayWindow::mousePressEvent(TQMouseEvent* e)
176 if (e->button() == TQt::LeftButton && !theApp()->wantRunInSystemTray())
179 mAssocMainWindow = MainWindow::toggleWindow(mAssocMainWindow);
181 else if (e->button() == TQt::MidButton)
182 MainWindow::executeNew();
184 KSystemTray::mousePressEvent(e);
192 void TrayWindow::mouseReleaseEvent(TQMouseEvent* e)
194 if (e->button() == TQt::LeftButton && mAssocMainWindow && mAssocMainWindow->isVisible())
196 mAssocMainWindow->raise();
197 mAssocMainWindow->setActiveWindow();
200 KSystemTray::mouseReleaseEvent(e);
206 void TrayWindow::dragEnterEvent(TQDragEnterEvent* e)
208 MainWindow::executeDragEnterEvent(e);
215 void TrayWindow::dropEvent(TQDropEvent* e)
217 MainWindow::executeDropEvent(0, e);
224 void TrayWindow::tooltipAlarmText(TQString& text)
const
227 const TQString& prefix = Preferences::tooltipTimeToPrefix();
228 int maxCount = Preferences::tooltipAlarmCount();
229 TQDateTime now = TQDateTime::currentDateTime();
232 TQValueList<TipItem> items;
233 TQValueList<TipItem>::Iterator iit;
234 KCal::Event::List events = AlarmCalendar::activeCalendar()->eventsWithAlarms(now.date(), now.addDays(1));
235 for (KCal::Event::List::ConstIterator it = events.begin(); it != events.end(); ++it)
238 event.set(*kcalEvent);
239 if (event.enabled() && !event.expired() && event.action() == KAEvent::MESSAGE)
242 DateTime dateTime =
event.displayDateTime();
243 if (dateTime.date() > now.date())
246 if (dateTime.date() != now.date().addDays(1)
247 || dateTime.time() >= now.time())
250 item.dateTime = dateTime.dateTime();
254 if (Preferences::showTooltipAlarmTime())
256 item.text += TDEGlobal::locale()->formatTime(item.dateTime.time());
260 if (Preferences::showTooltipTimeToAlarm())
262 int mins = (now.secsTo(item.dateTime) + 59) / 60;
265 char minutes[3] =
"00";
266 minutes[0] = (mins%60) / 10 +
'0';
267 minutes[1] = (mins%60) % 10 +
'0';
268 if (Preferences::showTooltipAlarmTime())
269 item.text += i18n(
"prefix + hours:minutes",
"(%1%2:%3)").arg(prefix).arg(mins/60).arg(minutes);
271 item.text += i18n(
"prefix + hours:minutes",
"%1%2:%3").arg(prefix).arg(mins/60).arg(minutes);
277 item.text += AlarmText::summary(event);
280 for (iit = items.begin(); iit != items.end(); ++iit)
282 if (item.dateTime <= (*iit).dateTime)
285 items.insert(iit, item);
288 kdDebug(5950) <<
"TrayWindow::tooltipAlarmText():\n";
290 for (iit = items.begin(); iit != items.end(); ++iit)
292 kdDebug(5950) <<
"-- " << (count+1) <<
") " << (*iit).text << endl;
295 if (++count == maxCount)
303 void TrayWindow::removeWindow(MainWindow* win)
305 if (win == mAssocMainWindow)
306 mAssocMainWindow = 0;
310 #ifdef HAVE_X11_HEADERS
312 #include <X11/Xlib.h>
313 #include <X11/Xutil.h>
324 bool TrayWindow::inSystemTray()
const
326 #ifdef HAVE_X11_HEADERS
329 Window* children = 0;
330 unsigned int nchildren;
332 if (!XQueryTree(tqt_xdisplay(), winId(), &root, &xParent, &children, &nchildren))
339 return xParent != root;
349 void TrayTooltip::maybeTip(
const TQPoint&)
351 TrayWindow* parent = (TrayWindow*)parentWidget();
353 if (Daemon::monitoringAlarms())
354 text = kapp->aboutData()->programName();
356 text = i18n(
"%1 - disabled").arg(kapp->aboutData()->programName());
357 kdDebug(5950) <<
"TrayTooltip::maybeTip(): " << text << endl;
358 if (Preferences::tooltipAlarmCount())
359 parent->tooltipAlarmText(text);
360 tip(parent->rect(), text);
KAEvent corresponds to a KCal::Event instance.
the KAlarm application object
displays an alarm message