• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kalarm

traywindow.cpp

Go to the documentation of this file.
00001 /*
00002  *  traywindow.cpp  -  the KDE system tray applet
00003  *  Program:  kalarm
00004  *  Copyright © 2002-2008 by David Jarvie <djarvie@kde.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"   //krazy:exclude=includes (kalarm.h must be first)
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 = Class: TrayWindow
00067 = The KDE system tray window.
00068 =============================================================================*/
00069 
00070 TrayWindow::TrayWindow(MainWindow* parent)
00071     : KSystemTrayIcon(parent),
00072       mAssocMainWindow(parent)
00073 {
00074     kDebug();
00075     // Set up GUI icons
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     //setAcceptDrops(true);         // allow drag-and-drop onto this window
00090 
00091     // Set up the context menu
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     // Replace the default handler for the Quit context menu item
00109     const char* quitName = KStandardAction::name(KStandardAction::Quit);
00110     delete actions->action(quitName);
00111     KStandardAction::quit(this, SLOT(slotQuit()), actions);
00112 
00113     // Set icon to correspond with the alarms enabled menu status
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();   // initialise action states
00119 }
00120 
00121 TrayWindow::~TrayWindow()
00122 {
00123     kDebug();
00124     theApp()->removeWindow(this);
00125     emit deleted();
00126 }
00127 
00128 /******************************************************************************
00129 * Called when the status of a resource has changed.
00130 * Enable or disable actions appropriately.
00131 */
00132 void TrayWindow::slotResourceStatusChanged()
00133 {
00134     // Find whether there are any writable active alarm resources
00135     bool active = AlarmResources::instance()->activeCount(AlarmResource::ACTIVE, true);
00136     mActionNew->setEnabled(active);
00137     mActionNewFromTemplate->setEnabled(active);
00138 }
00139 
00140 /******************************************************************************
00141 *  Called when the "New Alarm" menu item is selected to edit a new alarm.
00142 */
00143 void TrayWindow::slotNewAlarm(EditAlarmDlg::Type type)
00144 {
00145     KAlarm::editNewAlarm(type);
00146 }
00147 
00148 /******************************************************************************
00149 *  Called when the "New Alarm" menu item is selected to edit a new alarm.
00150 */
00151 void TrayWindow::slotNewFromTemplate(const KAEvent* event)
00152 {
00153     KAlarm::editNewAlarm(event);
00154 }
00155 
00156 /******************************************************************************
00157 *  Called when the "Configure KAlarm" menu item is selected.
00158 */
00159 void TrayWindow::slotPreferences()
00160 {
00161     KAlarmPrefDlg::display();
00162 }
00163 
00164 /******************************************************************************
00165 * Called when the Quit context menu item is selected.
00166 */
00167 void TrayWindow::slotQuit()
00168 {
00169     theApp()->doQuit(parentWidget());
00170 }
00171 
00172 /******************************************************************************
00173 * Called when the Alarms Enabled action status has changed.
00174 * Updates the alarms enabled menu item check state, and the icon pixmap.
00175 */
00176 void TrayWindow::setEnabledStatus(bool status)
00177 {
00178     kDebug() << (int)status;
00179     setIcon(status ? mIconEnabled : mIconDisabled);
00180 }
00181 
00182 /******************************************************************************
00183 *  Called when the mouse is clicked over the panel icon.
00184 *  A left click displays the KAlarm main window.
00185 *  A middle button click displays the New Alarm window.
00186 */
00187 void TrayWindow::slotActivated(QSystemTrayIcon::ActivationReason reason)
00188 {
00189     if (reason == QSystemTrayIcon::Trigger)
00190     {
00191         // Left click: display/hide the first main window
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();    // display a New Alarm dialog
00202     }
00203 }
00204 
00205 /******************************************************************************
00206 *  Called when the drag cursor enters the panel icon.
00207 */
00208 void TrayWindow::dragEnterEvent(QDragEnterEvent* e)
00209 {
00210     MainWindow::executeDragEnterEvent(e, 0);
00211 }
00212 
00213 /******************************************************************************
00214 *  Called when an object is dropped on the panel icon.
00215 *  If the object is recognised, the edit alarm dialog is opened appropriately.
00216 */
00217 void TrayWindow::dropEvent(QDropEvent* e)
00218 {
00219     MainWindow::executeDropEvent(0, e);
00220 }
00221 
00222 /******************************************************************************
00223 *  Called when any event occurs.
00224 *  If it's a tooltip event, display the tooltip text showing alarms due in the
00225 *  next 24 hours. The limit of 24 hours is because only times, not dates, are
00226 *  displayed.
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 *  Return the tooltip text showing alarms due in the next 24 hours.
00249 *  The limit of 24 hours is because only times, not dates, are displayed.
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     // Get today's and tomorrow's alarms, sorted in time order
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;   // ignore alarms after tomorrow at the current clock time
00272             item.dateTime = dateTime;
00273 
00274             // The alarm is due today, or early tomorrow
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             // Insert the item into the list in time-sorted order
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 * Called when the associated main window is closed.
00321 */
00322 void TrayWindow::removeWindow(MainWindow* win)
00323 {
00324     if (win == mAssocMainWindow)
00325         mAssocMainWindow = 0;
00326 }

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal