kalarm Library API Documentation

daemongui.cpp

00001 /*
00002  *  daemongui.cpp  -  handler for the alarm daemon GUI interface
00003  *  Program:  kalarm
00004  *  (C) 2002 by David Jarvie  software@astrojar.org.uk
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
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <stdlib.h>
00024 
00025 #include <qfile.h>
00026 
00027 #include <kaboutdata.h>
00028 #include <klocale.h>
00029 #include <kurl.h>
00030 #include <kstandarddirs.h>
00031 #include <kmessagebox.h>
00032 #include <dcopclient.h>
00033 #include <kdebug.h>
00034 
00035 #include <kalarmd/alarmdaemoniface_stub.h>
00036 #include "kalarmapp.h"
00037 #include "prefsettings.h"
00038 #include "alarmcalendar.h"
00039 #include "daemongui.h"
00040 #include "daemongui.moc"
00041 
00042 
00043 
00044 DaemonGuiHandler::DaemonGuiHandler(const char *name)
00045         : QObject(),
00046           DCOPObject(name),
00047           mDaemonStatusTimer(this),
00048           mDaemonStatusTimerCount(0),
00049           mCalendarDisabled(false),
00050           mEnableCalPending(false)
00051 {
00052         kdDebug(5950) << "DaemonGuiHandler::DaemonGuiHandler()\n";
00053         mDaemonRunning = theApp()->isDaemonRunning();
00054 
00055         mDaemonStatusTimerInterval = theApp()->settings()->daemonTrayCheckInterval();
00056         connect(theApp()->settings(), SIGNAL(settingsChanged()), this, SLOT(slotSettingsChanged()));
00057         connect(&mDaemonStatusTimer, SIGNAL(timeout()), SLOT(timerCheckDaemonRunning()));
00058         mDaemonStatusTimer.start(mDaemonStatusTimerInterval * 1000);  // check regularly if daemon is running
00059 
00060         registerGuiWithDaemon();
00061 }
00062 
00063 /******************************************************************************
00064  * DCOP call from the alarm daemon to notify a change.
00065  * The daemon notifies calendar statuses when we first register as a GUI, and whenever
00066  * a calendar status changes. So we don't need to read its config files.
00067  */
00068 void DaemonGuiHandler::alarmDaemonUpdate(int alarmGuiChangeType,
00069                                         const QString& calendarURL, const QCString& /*appName*/)
00070 {
00071         kdDebug(5950) << "DaemonGuiHandler::alarmDaemonUpdate(" << alarmGuiChangeType << ")\n";
00072         AlarmGuiChangeType changeType = AlarmGuiChangeType(alarmGuiChangeType);
00073         switch (changeType)
00074         {
00075                 case CHANGE_STATUS:    // daemon status change
00076                         theApp()->readDaemonCheckInterval();
00077                         return;
00078                 case CHANGE_CLIENT:    // change to daemon's client application list
00079                         return;
00080                 default:
00081                 {
00082                         // It must be a calendar-related change
00083                         if (expandURL(calendarURL) != theApp()->getCalendar().urlString())
00084                                 return;     // it's not a notification about KAlarm's calendar
00085                         switch (changeType)
00086                         {
00087                                 case DELETE_CALENDAR:
00088                                         kdDebug(5950) << "DaemonGuiHandler::alarmDaemonUpdate(DELETE_CALENDAR)\n";
00089                                         mCalendarDisabled = true;
00090                                         break;
00091                                 case CALENDAR_UNAVAILABLE:
00092                                         // Calendar is not available for monitoring
00093                                         kdDebug(5950) << "DaemonGuiHandler::alarmDaemonUpdate(CALENDAR_UNAVAILABLE)\n";
00094                                         mCalendarDisabled = true;
00095                                         break;
00096                                 case DISABLE_CALENDAR:
00097                                         // Calendar is available for monitoring but is not currently being monitored
00098                                         kdDebug(5950) << "DaemonGuiHandler::alarmDaemonUpdate(DISABLE_CALENDAR)\n";
00099                                         mCalendarDisabled = true;
00100                                         break;
00101                                 case ENABLE_CALENDAR:
00102                                         // Calendar is currently being monitored
00103                                         kdDebug(5950) << "DaemonGuiHandler::alarmDaemonUpdate(ENABLE_CALENDAR)\n";
00104                                         mCalendarDisabled = false;
00105                                         break;
00106                                 case ADD_CALENDAR:        // add a KOrganizer-type calendar
00107                                 case ADD_MSG_CALENDAR:    // add a KAlarm-type calendar
00108                                 default:
00109                                         return;
00110                         }
00111                         theApp()->actionAlarmEnable()->setAlarmsEnabled(!mCalendarDisabled);
00112                         break;
00113                 }
00114         }
00115 }
00116 
00117 // Dummy handler functions which KAlarm doesn't use
00118 void DaemonGuiHandler::handleEvent(const QString&, const QString&)
00119 { }
00120 void DaemonGuiHandler::handleEvent(const QString&)
00121 { }
00122 
00123 /******************************************************************************
00124 * Register as a GUI with the alarm daemon.
00125 */
00126 void DaemonGuiHandler::registerGuiWithDaemon()
00127 {
00128         kdDebug(5950) << "KAlarmApp::registerGuiWithDaemon()\n";
00129         AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
00130         s.registerGui(kapp->aboutData()->appName(), GUI_DCOP_OBJECT_NAME);
00131 }
00132 
00133 /******************************************************************************
00134 * Return whether the alarm daemon is monitoring alarms.
00135 */
00136 bool DaemonGuiHandler::monitoringAlarms()
00137 {
00138         bool ok = !mCalendarDisabled  &&  theApp()->isDaemonRunning();
00139         theApp()->actionAlarmEnable()->setAlarmsEnabled(ok);
00140         return ok;
00141 }
00142 
00143 /******************************************************************************
00144 * Tell the alarm daemon to stop or start monitoring the calendar file as
00145 * appropriate.
00146 */
00147 void DaemonGuiHandler::setAlarmsEnabled(bool enable)
00148 {
00149         if (enable  &&  !checkIfDaemonRunning())
00150         {
00151                 // The daemon is not running, so start it
00152                 QString execStr = locate("exe", DAEMON_APP_NAME);
00153                 if (execStr.isEmpty())
00154                 {
00155                         KMessageBox::error(0, i18n("Alarm Daemon not found"),
00156                                            i18n("%1 Error").arg(kapp->aboutData()->programName()));
00157                         kdError() << "TrayWindow::toggleAlarmsEnabled(): kalarmd not found" << endl;
00158                         return;
00159                 }
00160                 system(QFile::encodeName(execStr));
00161                 mEnableCalPending = true;
00162                 setFastDaemonCheck();
00163         }
00164 
00165         // If the daemon is now running, tell it to enable/disable the calendar
00166         if (checkIfDaemonRunning())
00167                 daemonEnableCalendar(enable);
00168 }
00169 
00170 /******************************************************************************
00171 * Tell the alarm daemon to enable/disable monitoring of the calendar file.
00172 */
00173 void DaemonGuiHandler::daemonEnableCalendar(bool enable)
00174 {
00175         AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
00176         s.enableCal(theApp()->getCalendar().urlString(), enable);
00177         mEnableCalPending = false;
00178 }
00179 
00180 /******************************************************************************
00181 * Called by the timer to check whether the daemon is running.
00182 */
00183 void DaemonGuiHandler::timerCheckDaemonRunning()
00184 {
00185         checkIfDaemonRunning();
00186         // Limit how long we check at the fast rate
00187         if (mDaemonStatusTimerCount > 0  &&  --mDaemonStatusTimerCount <= 0)
00188                 mDaemonStatusTimer.changeInterval(mDaemonStatusTimerInterval * 1000);
00189 }
00190 
00191 /******************************************************************************
00192 * Check whether the alarm daemon is currently running.
00193 * If its status has changed, trigger GUI updates.
00194 */
00195 bool DaemonGuiHandler::checkIfDaemonRunning()
00196 {
00197         bool newstatus = theApp()->isDaemonRunning();
00198         if (newstatus != mDaemonRunning)
00199         {
00200                 mDaemonRunning = newstatus;
00201                 int status = mDaemonRunning  &&  !mCalendarDisabled;
00202                 theApp()->actionAlarmEnable()->setAlarmsEnabled(status);
00203                 mDaemonStatusTimer.changeInterval(mDaemonStatusTimerInterval * 1000);   // exit from fast checking
00204                 mDaemonStatusTimerCount = 0;
00205                 if (mDaemonRunning)
00206                 {
00207                         // The alarm daemon has started up
00208                         registerGuiWithDaemon();
00209                         if (mEnableCalPending)
00210                                 daemonEnableCalendar(true);  // tell it to monitor the calendar, if appropriate
00211                 }
00212         }
00213         return mDaemonRunning;
00214 }
00215 
00216 /******************************************************************************
00217 * Starts checking at a faster rate whether the daemon is running.
00218 */
00219 void DaemonGuiHandler::setFastDaemonCheck()
00220 {
00221         mDaemonStatusTimer.start(500);     // check new status every half second
00222         mDaemonStatusTimerCount = 20;      // don't check at this rate for more than 10 seconds
00223 }
00224 
00225 /******************************************************************************
00226 * Called when a program setting has changed.
00227 * If the system tray icon update interval has changed, reset the timer.
00228 */
00229 void DaemonGuiHandler::slotSettingsChanged()
00230 {
00231         int newInterval = theApp()->settings()->daemonTrayCheckInterval();
00232         if (newInterval != mDaemonStatusTimerInterval)
00233         {
00234                 // Daemon check interval has changed
00235                 mDaemonStatusTimerInterval = newInterval;
00236                 if (mDaemonStatusTimerCount <= 0)   // don't change if on fast rate
00237                         mDaemonStatusTimer.changeInterval(mDaemonStatusTimerInterval * 1000);
00238         }
00239 }
00240 
00241 /******************************************************************************
00242  * Expand a DCOP call parameter URL to a full URL.
00243  * (We must store full URLs in the calendar data since otherwise later calls to
00244  *  reload or remove calendars won't necessarily find a match.)
00245  */
00246 QString DaemonGuiHandler::expandURL(const QString& urlString)
00247 {
00248         if (urlString.isEmpty())
00249                 return QString();
00250         return KURL(urlString).url();
00251 }
00252 
00253 
00254 /*=============================================================================
00255 =  Class: ActionAlarmsEnabled
00256 =============================================================================*/
00257 
00258 ActionAlarmsEnabled::ActionAlarmsEnabled(int accel, const QObject* receiver, const char* slot, QObject* parent, const char* name)
00259         : KAction(QString::null, accel, receiver, slot, parent, name),
00260           mAlarmsEnabled(true)
00261 {
00262         setAlarmsEnabled(false);
00263 }
00264 
00265 /******************************************************************************
00266 *  Set the correct text for the Alarms Enabled action.
00267 */
00268 void ActionAlarmsEnabled::setAlarmsEnabled(bool status)
00269 {
00270         if (status != mAlarmsEnabled)
00271         {
00272                 mAlarmsEnabled = status;
00273                 setText(mAlarmsEnabled ? i18n("Alarms &Enabled") : i18n("&Enable Alarms"));
00274                 emit alarmsEnabledChange(mAlarmsEnabled);
00275         }
00276 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:26 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001