kalarm

kalarmapp.cpp

00001 /*
00002  *  kalarmapp.cpp  -  the KAlarm application object
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2007 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 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"
00022 
00023 #include <stdlib.h>
00024 #include <ctype.h>
00025 #include <iostream>
00026 
00027 #include <qobjectlist.h>
00028 #include <qtimer.h>
00029 #include <qregexp.h>
00030 #include <qfile.h>
00031 
00032 #include <kcmdlineargs.h>
00033 #include <klocale.h>
00034 #include <kstandarddirs.h>
00035 #include <kconfig.h>
00036 #include <kaboutdata.h>
00037 #include <dcopclient.h>
00038 #include <kprocess.h>
00039 #include <ktempfile.h>
00040 #include <kfileitem.h>
00041 #include <kstdguiitem.h>
00042 #include <ktrader.h>
00043 #include <kstaticdeleter.h>
00044 #include <kdebug.h>
00045 
00046 #include <libkcal/calformat.h>
00047 
00048 #include <kalarmd/clientinfo.h>
00049 
00050 #include "alarmcalendar.h"
00051 #include "alarmlistview.h"
00052 #include "editdlg.h"
00053 #include "daemon.h"
00054 #include "dcophandler.h"
00055 #include "functions.h"
00056 #include "kamail.h"
00057 #include "karecurrence.h"
00058 #include "mainwindow.h"
00059 #include "messagebox.h"
00060 #include "messagewin.h"
00061 #include "preferences.h"
00062 #include "prefdlg.h"
00063 #include "shellprocess.h"
00064 #include "traywindow.h"
00065 #include "kalarmapp.moc"
00066 
00067 #include <netwm.h>
00068 
00069 
00070 static bool convWakeTime(const QCString& timeParam, QDateTime&, bool& noTime);
00071 static bool convInterval(const QCString& timeParam, KARecurrence::Type&, int& timeInterval, bool allowMonthYear = true);
00072 
00073 /******************************************************************************
00074 * Find the maximum number of seconds late which a late-cancel alarm is allowed
00075 * to be. This is calculated as the alarm daemon's check interval, plus a few
00076 * seconds leeway to cater for any timing irregularities.
00077 */
00078 static inline int maxLateness(int lateCancel)
00079 {
00080     static const int LATENESS_LEEWAY = 5;
00081     int lc = (lateCancel >= 1) ? (lateCancel - 1)*60 : 0;
00082     return Daemon::maxTimeSinceCheck() + LATENESS_LEEWAY + lc;
00083 }
00084 
00085 
00086 KAlarmApp*  KAlarmApp::theInstance  = 0;
00087 int         KAlarmApp::mActiveCount = 0;
00088 int         KAlarmApp::mFatalError  = 0;
00089 QString     KAlarmApp::mFatalMessage;
00090 
00091 
00092 /******************************************************************************
00093 * Construct the application.
00094 */
00095 KAlarmApp::KAlarmApp()
00096     : KUniqueApplication(),
00097       mInitialised(false),
00098       mDcopHandler(new DcopHandler()),
00099 #ifdef OLD_DCOP
00100       mDcopHandlerOld(new DcopHandlerOld()),
00101 #endif
00102       mTrayWindow(0),
00103       mPendingQuit(false),
00104       mProcessingQueue(false),
00105       mCheckingSystemTray(false),
00106       mSessionClosingDown(false),
00107       mRefreshExpiredAlarms(false),
00108       mSpeechEnabled(false)
00109 {
00110     Preferences::initialise();
00111     Preferences::connect(SIGNAL(preferencesChanged()), this, SLOT(slotPreferencesChanged()));
00112     KCal::CalFormat::setApplication(aboutData()->programName(), AlarmCalendar::icalProductId());
00113     KARecurrence::setDefaultFeb29Type(Preferences::defaultFeb29Type());
00114 
00115     // Check if the system tray is supported by this window manager
00116     mHaveSystemTray = true;   // assume yes in lieu of a test which works
00117 
00118     if (AlarmCalendar::initialiseCalendars())
00119     {
00120         connect(AlarmCalendar::expiredCalendar(), SIGNAL(purged()), SLOT(slotExpiredPurged()));
00121 
00122         KConfig* config = kapp->config();
00123         config->setGroup(QString::fromLatin1("General"));
00124         mNoSystemTray           = config->readBoolEntry(QString::fromLatin1("NoSystemTray"), false);
00125         mSavedNoSystemTray      = mNoSystemTray;
00126         mOldRunInSystemTray     = wantRunInSystemTray();
00127         mDisableAlarmsIfStopped = mOldRunInSystemTray && !mNoSystemTray && Preferences::disableAlarmsIfStopped();
00128         mStartOfDay             = Preferences::startOfDay();
00129         if (Preferences::hasStartOfDayChanged())
00130             mStartOfDay.setHMS(100,0,0);    // start of day time has changed: flag it as invalid
00131         DateTime::setStartOfDay(mStartOfDay);
00132         mPrefsExpiredColour   = Preferences::expiredColour();
00133         mPrefsExpiredKeepDays = Preferences::expiredKeepDays();
00134         mPrefsShowTime        = Preferences::showAlarmTime();
00135         mPrefsShowTimeTo      = Preferences::showTimeToAlarm();
00136     }
00137 
00138     // Check if the speech synthesis daemon is installed
00139     mSpeechEnabled = (KTrader::self()->query("DCOP/Text-to-Speech", "Name == 'KTTSD'").count() > 0);
00140     if (!mSpeechEnabled)
00141         kdDebug(5950) << "KAlarmApp::KAlarmApp(): speech synthesis disabled (KTTSD not found)" << endl;
00142     // Check if KOrganizer is installed
00143     QString korg = QString::fromLatin1("korganizer");
00144     mKOrganizerEnabled = !locate("exe", korg).isNull()  ||  !KStandardDirs::findExe(korg).isNull();
00145     if (!mKOrganizerEnabled)
00146         kdDebug(5950) << "KAlarmApp::KAlarmApp(): KOrganizer options disabled (KOrganizer not found)" << endl;
00147 }
00148 
00149 /******************************************************************************
00150 */
00151 KAlarmApp::~KAlarmApp()
00152 {
00153     while (!mCommandProcesses.isEmpty())
00154     {
00155         ProcData* pd = mCommandProcesses.first();
00156         mCommandProcesses.pop_front();
00157         delete pd;
00158     }
00159     AlarmCalendar::terminateCalendars();
00160 }
00161 
00162 /******************************************************************************
00163 * Return the one and only KAlarmApp instance.
00164 * If it doesn't already exist, it is created first.
00165 */
00166 KAlarmApp* KAlarmApp::getInstance()
00167 {
00168     if (!theInstance)
00169     {
00170         theInstance = new KAlarmApp;
00171 
00172         if (mFatalError)
00173             theInstance->quitFatal();
00174         else
00175         {
00176             // This is here instead of in the constructor to avoid recursion
00177             Daemon::initialise();    // calendars must be initialised before calling this
00178         }
00179     }
00180     return theInstance;
00181 }
00182 
00183 /******************************************************************************
00184 * Restore the saved session if required.
00185 */
00186 bool KAlarmApp::restoreSession()
00187 {
00188     if (!isRestored())
00189         return false;
00190     if (mFatalError)
00191     {
00192         quitFatal();
00193         return false;
00194     }
00195 
00196     // Process is being restored by session management.
00197     kdDebug(5950) << "KAlarmApp::restoreSession(): Restoring\n";
00198     ++mActiveCount;
00199     if (!initCheck(true))     // open the calendar file (needed for main windows)
00200     {
00201         --mActiveCount;
00202         quitIf(1, true);    // error opening the main calendar - quit
00203         return true;
00204     }
00205     MainWindow* trayParent = 0;
00206     for (int i = 1;  KMainWindow::canBeRestored(i);  ++i)
00207     {
00208         QString type = KMainWindow::classNameOfToplevel(i);
00209         if (type == QString::fromLatin1("MainWindow"))
00210         {
00211             MainWindow* win = MainWindow::create(true);
00212             win->restore(i, false);
00213             if (win->isHiddenTrayParent())
00214                 trayParent = win;
00215             else
00216                 win->show();
00217         }
00218         else if (type == QString::fromLatin1("MessageWin"))
00219         {
00220             MessageWin* win = new MessageWin;
00221             win->restore(i, false);
00222             if (win->isValid())
00223                 win->show();
00224             else
00225                 delete win;
00226         }
00227     }
00228     initCheck();           // register with the alarm daemon
00229 
00230     // Try to display the system tray icon if it is configured to be autostarted,
00231     // or if we're in run-in-system-tray mode.
00232     if (Preferences::autostartTrayIcon()
00233     ||  MainWindow::count()  &&  wantRunInSystemTray())
00234     {
00235         displayTrayIcon(true, trayParent);
00236         // Occasionally for no obvious reason, the main main window is
00237         // shown when it should be hidden, so hide it just to be sure.
00238         if (trayParent)
00239             trayParent->hide();
00240     }
00241 
00242     --mActiveCount;
00243     quitIf(0);           // quit if no windows are open
00244     return true;
00245 }
00246 
00247 /******************************************************************************
00248 * Called for a KUniqueApplication when a new instance of the application is
00249 * started.
00250 */
00251 int KAlarmApp::newInstance()
00252 {
00253     kdDebug(5950)<<"KAlarmApp::newInstance()\n";
00254     if (mFatalError)
00255     {
00256         quitFatal();
00257         return 1;
00258     }
00259     ++mActiveCount;
00260     int exitCode = 0;               // default = success
00261     static bool firstInstance = true;
00262     bool dontRedisplay = false;
00263     if (!firstInstance || !isRestored())
00264     {
00265         QString usage;
00266         KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00267 
00268         // Use a 'do' loop which is executed only once to allow easy error exits.
00269         // Errors use 'break' to skip to the end of the function.
00270 
00271         // Note that DCOP handling is only set up once the command line parameters
00272         // have been checked, since we mustn't register with the alarm daemon only
00273         // to quit immediately afterwards.
00274         do
00275         {
00276             #define USAGE(message)  { usage = message; break; }
00277             if (args->isSet("stop"))
00278             {
00279                 // Stop the alarm daemon
00280                 kdDebug(5950)<<"KAlarmApp::newInstance(): stop\n";
00281                 args->clear();         // free up memory
00282                 if (!Daemon::stop())
00283                 {
00284                     exitCode = 1;
00285                     break;
00286                 }
00287                 dontRedisplay = true;  // exit program if no other instances running
00288             }
00289             else
00290             if (args->isSet("reset"))
00291             {
00292                 // Reset the alarm daemon, if it's running.
00293                 // (If it's not running, it will reset automatically when it eventually starts.)
00294                 kdDebug(5950)<<"KAlarmApp::newInstance(): reset\n";
00295                 args->clear();         // free up memory
00296                 Daemon::reset();
00297                 dontRedisplay = true;  // exit program if no other instances running
00298             }
00299             else
00300             if (args->isSet("tray"))
00301             {
00302                 // Display only the system tray icon
00303                 kdDebug(5950)<<"KAlarmApp::newInstance(): tray\n";
00304                 args->clear();      // free up memory
00305                 if (!mHaveSystemTray)
00306                 {
00307                     exitCode = 1;
00308                     break;
00309                 }
00310                 if (!initCheck())   // open the calendar, register with daemon
00311                 {
00312                     exitCode = 1;
00313                     break;
00314                 }
00315                 if (!displayTrayIcon(true))
00316                 {
00317                     exitCode = 1;
00318                     break;
00319                 }
00320             }
00321             else
00322             if (args->isSet("handleEvent")  ||  args->isSet("triggerEvent")  ||  args->isSet("cancelEvent")  ||  args->isSet("calendarURL"))
00323             {
00324                 // Display or delete the event with the specified event ID
00325                 kdDebug(5950)<<"KAlarmApp::newInstance(): handle event\n";
00326                 EventFunc function = EVENT_HANDLE;
00327                 int count = 0;
00328                 const char* option = 0;
00329                 if (args->isSet("handleEvent"))   { function = EVENT_HANDLE;   option = "handleEvent";   ++count; }
00330                 if (args->isSet("triggerEvent"))  { function = EVENT_TRIGGER;  option = "triggerEvent";  ++count; }
00331                 if (args->isSet("cancelEvent"))   { function = EVENT_CANCEL;   option = "cancelEvent";   ++count; }
00332                 if (!count)
00333                     USAGE(i18n("%1 requires %2, %3 or %4").arg(QString::fromLatin1("--calendarURL")).arg(QString::fromLatin1("--handleEvent")).arg(QString::fromLatin1("--triggerEvent")).arg(QString::fromLatin1("--cancelEvent")))
00334                 if (count > 1)
00335                     USAGE(i18n("%1, %2, %3 mutually exclusive").arg(QString::fromLatin1("--handleEvent")).arg(QString::fromLatin1("--triggerEvent")).arg(QString::fromLatin1("--cancelEvent")));
00336                 if (!initCheck(true))   // open the calendar, don't register with daemon yet
00337                 {
00338                     exitCode = 1;
00339                     break;
00340                 }
00341                 if (args->isSet("calendarURL"))
00342                 {
00343                     QString calendarUrl = args->getOption("calendarURL");
00344                     if (KURL(calendarUrl).url() != AlarmCalendar::activeCalendar()->urlString())
00345                         USAGE(i18n("%1: wrong calendar file").arg(QString::fromLatin1("--calendarURL")))
00346                 }
00347                 QString eventID = args->getOption(option);
00348                 args->clear();      // free up memory
00349                 if (eventID.startsWith(QString::fromLatin1("ad:")))
00350                 {
00351                     // It's a notification from the alarm deamon
00352                     eventID = eventID.mid(3);
00353                     Daemon::queueEvent(eventID);
00354                 }
00355                 setUpDcop();        // start processing DCOP calls
00356                 if (!handleEvent(eventID, function))
00357                 {
00358                     exitCode = 1;
00359                     break;
00360                 }
00361             }
00362             else
00363             if (args->isSet("edit"))
00364             {
00365                 QString eventID = args->getOption("edit");
00366                 if (!initCheck())
00367                 {
00368                     exitCode = 1;
00369                     break;
00370                 }
00371                 if (!KAlarm::edit(eventID))
00372                 {
00373                     USAGE(i18n("%1: Event %2 not found, or not editable").arg(QString::fromLatin1("--edit")).arg(eventID))
00374                     exitCode = 1;
00375                     break;
00376                 }
00377             }
00378             else
00379             if (args->isSet("edit-new")  ||  args->isSet("edit-new-preset"))
00380             {
00381                 QString templ;
00382                 if (args->isSet("edit-new-preset"))
00383                     templ = args->getOption("edit-new-preset");
00384                 if (!initCheck())
00385                 {
00386                     exitCode = 1;
00387                     break;
00388                 }
00389                 KAlarm::editNew(templ);
00390             }
00391             else
00392             if (args->isSet("file")  ||  args->isSet("exec")  ||  args->isSet("mail")  ||  args->count())
00393             {
00394                 // Display a message or file, execute a command, or send an email
00395                 KAEvent::Action action = KAEvent::MESSAGE;
00396                 QCString         alMessage;
00397                 QCString         alFromID;
00398                 EmailAddressList alAddresses;
00399                 QStringList      alAttachments;
00400                 QCString         alSubject;
00401                 if (args->isSet("file"))
00402                 {
00403                     kdDebug(5950)<<"KAlarmApp::newInstance(): file\n";
00404                     if (args->isSet("exec"))
00405                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--exec")).arg(QString::fromLatin1("--file")))
00406                     if (args->isSet("mail"))
00407                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--mail")).arg(QString::fromLatin1("--file")))
00408                     if (args->count())
00409                         USAGE(i18n("message incompatible with %1").arg(QString::fromLatin1("--file")))
00410                     alMessage = args->getOption("file");
00411                     action = KAEvent::FILE;
00412                 }
00413                 else if (args->isSet("exec"))
00414                 {
00415                     kdDebug(5950)<<"KAlarmApp::newInstance(): exec\n";
00416                     if (args->isSet("mail"))
00417                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--mail")).arg(QString::fromLatin1("--exec")))
00418                     alMessage = args->getOption("exec");
00419                     int n = args->count();
00420                     for (int i = 0;  i < n;  ++i)
00421                     {
00422                         alMessage += ' ';
00423                         alMessage += args->arg(i);
00424                     }
00425                     action = KAEvent::COMMAND;
00426                 }
00427                 else if (args->isSet("mail"))
00428                 {
00429                     kdDebug(5950)<<"KAlarmApp::newInstance(): mail\n";
00430                     if (args->isSet("subject"))
00431                         alSubject = args->getOption("subject");
00432                     if (args->isSet("from-id"))
00433                         alFromID = args->getOption("from-id");
00434                     QCStringList params = args->getOptionList("mail");
00435                     for (QCStringList::Iterator i = params.begin();  i != params.end();  ++i)
00436                     {
00437                         QString addr = QString::fromLocal8Bit(*i);
00438                         if (!KAMail::checkAddress(addr))
00439                             USAGE(i18n("%1: invalid email address").arg(QString::fromLatin1("--mail")))
00440                         alAddresses += KCal::Person(QString::null, addr);
00441                     }
00442                     params = args->getOptionList("attach");
00443                     for (QCStringList::Iterator i = params.begin();  i != params.end();  ++i)
00444                         alAttachments += QString::fromLocal8Bit(*i);
00445                     alMessage = args->arg(0);
00446                     action = KAEvent::EMAIL;
00447                 }
00448                 else
00449                 {
00450                     kdDebug(5950)<<"KAlarmApp::newInstance(): message\n";
00451                     alMessage = args->arg(0);
00452                 }
00453 
00454                 if (action != KAEvent::EMAIL)
00455                 {
00456                     if (args->isSet("subject"))
00457                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--subject")).arg(QString::fromLatin1("--mail")))
00458                     if (args->isSet("from-id"))
00459                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--from-id")).arg(QString::fromLatin1("--mail")))
00460                     if (args->isSet("attach"))
00461                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--attach")).arg(QString::fromLatin1("--mail")))
00462                     if (args->isSet("bcc"))
00463                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--bcc")).arg(QString::fromLatin1("--mail")))
00464                 }
00465 
00466                 bool      alarmNoTime = false;
00467                 QDateTime alarmTime, endTime;
00468                 QColor    bgColour = Preferences::defaultBgColour();
00469                 QColor    fgColour = Preferences::defaultFgColour();
00470                 KARecurrence recurrence;
00471                 int       repeatCount    = 0;
00472                 int       repeatInterval = 0;
00473                 if (args->isSet("color"))
00474                 {
00475                     // Background colour is specified
00476                     QCString colourText = args->getOption("color");
00477                     if (static_cast<const char*>(colourText)[0] == '0'
00478                     &&  tolower(static_cast<const char*>(colourText)[1]) == 'x')
00479                         colourText.replace(0, 2, "#");
00480                     bgColour.setNamedColor(colourText);
00481                     if (!bgColour.isValid())
00482                         USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--color")))
00483                 }
00484                 if (args->isSet("colorfg"))
00485                 {
00486                     // Foreground colour is specified
00487                     QCString colourText = args->getOption("colorfg");
00488                     if (static_cast<const char*>(colourText)[0] == '0'
00489                     &&  tolower(static_cast<const char*>(colourText)[1]) == 'x')
00490                         colourText.replace(0, 2, "#");
00491                     fgColour.setNamedColor(colourText);
00492                     if (!fgColour.isValid())
00493                         USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--colorfg")))
00494                 }
00495 
00496                 if (args->isSet("time"))
00497                 {
00498                     QCString dateTime = args->getOption("time");
00499                     if (!convWakeTime(dateTime, alarmTime, alarmNoTime))
00500                         USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--time")))
00501                 }
00502                 else
00503                     alarmTime = QDateTime::currentDateTime();
00504 
00505                 bool haveRecurrence = args->isSet("recurrence");
00506                 if (haveRecurrence)
00507                 {
00508                     if (args->isSet("login"))
00509                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--login")).arg(QString::fromLatin1("--recurrence")))
00510                     if (args->isSet("until"))
00511                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--until")).arg(QString::fromLatin1("--recurrence")))
00512                     QCString rule = args->getOption("recurrence");
00513                     recurrence.set(QString::fromLocal8Bit(static_cast<const char*>(rule)));
00514                 }
00515                 if (args->isSet("interval"))
00516                 {
00517                     // Repeat count is specified
00518                     int count;
00519                     if (args->isSet("login"))
00520                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--login")).arg(QString::fromLatin1("--interval")))
00521                     bool ok;
00522                     if (args->isSet("repeat"))
00523                     {
00524                         count = args->getOption("repeat").toInt(&ok);
00525                         if (!ok || !count || count < -1 || (count < 0 && haveRecurrence))
00526                             USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--repeat")))
00527                     }
00528                     else if (haveRecurrence)
00529                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--interval")).arg(QString::fromLatin1("--repeat")))
00530                     else if (args->isSet("until"))
00531                     {
00532                         count = 0;
00533                         QCString dateTime = args->getOption("until");
00534                         if (!convWakeTime(dateTime, endTime, alarmNoTime))
00535                             USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--until")))
00536                         if (endTime < alarmTime)
00537                             USAGE(i18n("%1 earlier than %2").arg(QString::fromLatin1("--until")).arg(QString::fromLatin1("--time")))
00538                     }
00539                     else
00540                         count = -1;
00541 
00542                     // Get the recurrence interval
00543                     int interval;
00544                     KARecurrence::Type recurType;
00545                     if (!convInterval(args->getOption("interval"), recurType, interval, !haveRecurrence)
00546                     ||  interval < 0)
00547                         USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--interval")))
00548                     if (alarmNoTime  &&  recurType == KARecurrence::MINUTELY)
00549                         USAGE(i18n("Invalid %1 parameter for date-only alarm").arg(QString::fromLatin1("--interval")))
00550 
00551                     if (haveRecurrence)
00552                     {
00553                         // There is a also a recurrence specified, so set up a simple repetition
00554                         int longestInterval = recurrence.longestInterval();
00555                         if (count * interval > longestInterval)
00556                             USAGE(i18n("Invalid %1 and %2 parameters: repetition is longer than %3 interval").arg(QString::fromLatin1("--interval")).arg(QString::fromLatin1("--repeat")).arg(QString::fromLatin1("--recurrence")));
00557                         repeatCount    = count;
00558                         repeatInterval = interval;
00559                     }
00560                     else
00561                     {
00562                         // There is no other recurrence specified, so convert the repetition
00563                         // parameters into a KCal::Recurrence
00564                         recurrence.set(recurType, interval, count, DateTime(alarmTime, alarmNoTime), endTime);
00565                     }
00566                 }
00567                 else
00568                 {
00569                     if (args->isSet("repeat"))
00570                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--repeat")).arg(QString::fromLatin1("--interval")))
00571                     if (args->isSet("until"))
00572                         USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--until")).arg(QString::fromLatin1("--interval")))
00573                 }
00574 
00575                 QCString audioFile;
00576                 float    audioVolume = -1;
00577 #ifdef WITHOUT_ARTS
00578                 bool     audioRepeat = false;
00579 #else
00580                 bool     audioRepeat = args->isSet("play-repeat");
00581 #endif
00582                 if (audioRepeat  ||  args->isSet("play"))
00583                 {
00584                     // Play a sound with the alarm
00585                     if (audioRepeat  &&  args->isSet("play"))
00586                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--play")).arg(QString::fromLatin1("--play-repeat")))
00587                     if (args->isSet("beep"))
00588                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--beep")).arg(QString::fromLatin1(audioRepeat ? "--play-repeat" : "--play")))
00589                     if (args->isSet("speak"))
00590                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--speak")).arg(QString::fromLatin1(audioRepeat ? "--play-repeat" : "--play")))
00591                     audioFile = args->getOption(audioRepeat ? "play-repeat" : "play");
00592 #ifndef WITHOUT_ARTS
00593                     if (args->isSet("volume"))
00594                     {
00595                         bool ok;
00596                         int volumepc = args->getOption("volume").toInt(&ok);
00597                         if (!ok  ||  volumepc < 0  ||  volumepc > 100)
00598                             USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("--volume")))
00599                         audioVolume = static_cast<float>(volumepc) / 100;
00600                     }
00601 #endif
00602                 }
00603 #ifndef WITHOUT_ARTS
00604                 else if (args->isSet("volume"))
00605                     USAGE(i18n("%1 requires %2 or %3").arg(QString::fromLatin1("--volume")).arg(QString::fromLatin1("--play")).arg(QString::fromLatin1("--play-repeat")))
00606 #endif
00607                 if (args->isSet("speak"))
00608                 {
00609                     if (args->isSet("beep"))
00610                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--beep")).arg(QString::fromLatin1("--speak")))
00611                     if (!mSpeechEnabled)
00612                         USAGE(i18n("%1 requires speech synthesis to be configured using KTTSD").arg(QString::fromLatin1("--speak")))
00613                 }
00614                 int reminderMinutes = 0;
00615                 bool onceOnly = args->isSet("reminder-once");
00616                 if (args->isSet("reminder")  ||  onceOnly)
00617                 {
00618                     // Issue a reminder alarm in advance of the main alarm
00619                     if (onceOnly  &&  args->isSet("reminder"))
00620                         USAGE(i18n("%1 incompatible with %2").arg(QString::fromLatin1("--reminder")).arg(QString::fromLatin1("--reminder-once")))
00621                     QString opt = onceOnly ? QString::fromLatin1("--reminder-once") : QString::fromLatin1("--reminder");
00622                     if (args->isSet("exec"))
00623                         USAGE(i18n("%1 incompatible with %2").arg(opt).arg(QString::fromLatin1("--exec")))
00624                     if (args->isSet("mail"))
00625                         USAGE(i18n("%1 incompatible with %2").arg(opt).arg(QString::fromLatin1("--mail")))
00626                     KARecurrence::Type recurType;
00627                     QString optval = args->getOption(onceOnly ? "reminder-once" : "reminder");
00628                     bool ok = convInterval(args->getOption(onceOnly ? "reminder-once" : "reminder"), recurType, reminderMinutes);
00629                     if (ok)
00630                     {
00631                         switch (recurType)
00632                         {
00633                             case KARecurrence::MINUTELY:
00634                                 if (alarmNoTime)
00635                                     USAGE(i18n("Invalid %1 parameter for date-only alarm").arg(opt))
00636                                 break;
00637                             case KARecurrence::DAILY:     reminderMinutes *= 1440;  break;
00638                             case KARecurrence::WEEKLY:    reminderMinutes *= 7*1440;  break;
00639                             default:   ok = false;  break;
00640                         }
00641                     }
00642                     if (!ok)
00643                         USAGE(i18n("Invalid %1 parameter").arg(opt))
00644                 }
00645 
00646                 int lateCancel = 0;
00647                 if (args->isSet("late-cancel"))
00648                 {
00649                     KARecurrence::Type recurType;
00650                     bool ok = convInterval(args->getOption("late-cancel"), recurType, lateCancel, false);
00651                     if (!ok  ||  lateCancel <= 0)
00652                         USAGE(i18n("Invalid %1 parameter").arg(QString::fromLatin1("late-cancel")))
00653                 }
00654                 else if (args->isSet("auto-close"))
00655                     USAGE(i18n("%1 requires %2").arg(QString::fromLatin1("--auto-close")).arg(QString::fromLatin1("--late-cancel")))
00656 
00657                 int flags = KAEvent::DEFAULT_FONT;
00658                 if (args->isSet("ack-confirm"))
00659                     flags |= KAEvent::CONFIRM_ACK;
00660                 if (args->isSet("auto-close"))
00661                     flags |= KAEvent::AUTO_CLOSE;
00662                 if (args->isSet("beep"))
00663                     flags |= KAEvent::BEEP;
00664                 if (args->isSet("speak"))
00665                     flags |= KAEvent::SPEAK;
00666                 if (args->isSet("korganizer"))
00667                     flags |= KAEvent::COPY_KORGANIZER;
00668                 if (args->isSet("disable"))
00669                     flags |= KAEvent::DISABLED;
00670                 if (audioRepeat)
00671                     flags |= KAEvent::REPEAT_SOUND;
00672                 if (args->isSet("login"))
00673                     flags |= KAEvent::REPEAT_AT_LOGIN;
00674                 if (args->isSet("bcc"))
00675                     flags |= KAEvent::EMAIL_BCC;
00676                 if (alarmNoTime)
00677                     flags |= KAEvent::ANY_TIME;
00678                 args->clear();      // free up memory
00679 
00680                 // Display or schedule the event
00681                 if (!initCheck())
00682                 {
00683                     exitCode = 1;
00684                     break;
00685                 }
00686                 if (!scheduleEvent(action, alMessage, alarmTime, lateCancel, flags, bgColour, fgColour, QFont(), audioFile,
00687                                    audioVolume, reminderMinutes, recurrence, repeatInterval, repeatCount,
00688                                    alFromID, alAddresses, alSubject, alAttachments))
00689                 {
00690                     exitCode = 1;
00691                     break;
00692                 }
00693             }
00694             else
00695             {
00696                 // No arguments - run interactively & display the main window
00697                 kdDebug(5950)<<"KAlarmApp::newInstance(): interactive\n";
00698                 if (args->isSet("ack-confirm"))
00699                     usage += QString::fromLatin1("--ack-confirm ");
00700                 if (args->isSet("attach"))
00701                     usage += QString::fromLatin1("--attach ");
00702                 if (args->isSet("auto-close"))
00703                     usage += QString::fromLatin1("--auto-close ");
00704                 if (args->isSet("bcc"))
00705                     usage += QString::fromLatin1("--bcc ");
00706                 if (args->isSet("beep"))
00707                     usage += QString::fromLatin1("--beep ");
00708                 if (args->isSet("color"))
00709                     usage += QString::fromLatin1("--color ");
00710                 if (args->isSet("colorfg"))
00711                     usage += QString::fromLatin1("--colorfg ");
00712                 if (args->isSet("disable"))
00713                     usage += QString::fromLatin1("--disable ");
00714                 if (args->isSet("from-id"))
00715                     usage += QString::fromLatin1("--from-id ");
00716                 if (args->isSet("korganizer"))
00717                     usage += QString::fromLatin1("--korganizer ");
00718                 if (args->isSet("late-cancel"))
00719                     usage += QString::fromLatin1("--late-cancel ");
00720                 if (args->isSet("login"))
00721                     usage += QString::fromLatin1("--login ");
00722                 if (args->isSet("play"))
00723                     usage += QString::fromLatin1("--play ");
00724 #ifndef WITHOUT_ARTS
00725                 if (args->isSet("play-repeat"))
00726                     usage += QString::fromLatin1("--play-repeat ");
00727 #endif
00728                 if (args->isSet("reminder"))
00729                     usage += QString::fromLatin1("--reminder ");
00730                 if (args->isSet("reminder-once"))
00731                     usage += QString::fromLatin1("--reminder-once ");
00732                 if (args->isSet("speak"))
00733                     usage += QString::fromLatin1("--speak ");
00734                 if (args->isSet("subject"))
00735                     usage += QString::fromLatin1("--subject ");
00736                 if (args->isSet("time"))
00737                     usage += QString::fromLatin1("--time ");
00738 #ifndef WITHOUT_ARTS
00739                 if (args->isSet("volume"))
00740                     usage += QString::fromLatin1("--volume ");
00741 #endif
00742                 if (!usage.isEmpty())
00743                 {
00744                     usage += i18n(": option(s) only valid with a message/%1/%2").arg(QString::fromLatin1("--file")).arg(QString::fromLatin1("--exec"));
00745                     break;
00746                 }
00747 
00748                 args->clear();      // free up memory
00749                 /* Instead of calling initCheck (), call initCheck (true) to check the calendar only.
00750                  * This works because it doesn't change the return value:
00751                  * If the return value is false, it doesn't matter because we exit.
00752                  * If the return value is true, the first thing MainWindow() does, is to call initCheck(), so we do the same tests. 
00753                  *              Jonas Wustrack - jwustrack@mandriva.com                        */
00754                 if (!initCheck(true))
00755                 {
00756                     exitCode = 1;
00757                     break;
00758                 }
00759 
00760                 (MainWindow::create())->show();
00761             }
00762         } while (0);    // only execute once
00763 
00764         if (!usage.isEmpty())
00765         {
00766             // Note: we can't use args->usage() since that also quits any other
00767             // running 'instances' of the program.
00768             std::cerr << usage.local8Bit().data()
00769                       << i18n("\nUse --help to get a list of available command line options.\n").local8Bit().data();
00770             exitCode = 1;
00771         }
00772     }
00773     if (firstInstance  &&  !dontRedisplay  &&  !exitCode)
00774         redisplayAlarms();
00775 
00776     --mActiveCount;
00777     firstInstance = false;
00778 
00779     // Quit the application if this was the last/only running "instance" of the program.
00780     // Executing 'return' doesn't work very well since the program continues to
00781     // run if no windows were created.
00782     quitIf(exitCode);
00783     return exitCode;
00784 }
00785 
00786 /******************************************************************************
00787 * Quit the program, optionally only if there are no more "instances" running.
00788 */
00789 void KAlarmApp::quitIf(int exitCode, bool force)
00790 {
00791     if (force)
00792     {
00793         // Quit regardless, except for message windows
00794         MainWindow::closeAll();
00795         displayTrayIcon(false);
00796         if (MessageWin::instanceCount())
00797             return;
00798     }
00799     else
00800     {
00801         // Quit only if there are no more "instances" running
00802         mPendingQuit = false;
00803         if (mActiveCount > 0  ||  MessageWin::instanceCount())
00804             return;
00805         int mwcount = MainWindow::count();
00806         MainWindow* mw = mwcount ? MainWindow::firstWindow() : 0;
00807         if (mwcount > 1  ||  mwcount && (!mw->isHidden() || !mw->isTrayParent()))
00808             return;
00809         // There are no windows left except perhaps a main window which is a hidden tray icon parent
00810         if (mTrayWindow)
00811         {
00812             // There is a system tray icon.
00813             // Don't exit unless the system tray doesn't seem to exist.
00814             if (checkSystemTray())
00815                 return;
00816         }
00817         if (!mDcopQueue.isEmpty()  ||  !mCommandProcesses.isEmpty())
00818         {
00819             // Don't quit yet if there are outstanding actions on the DCOP queue
00820             mPendingQuit = true;
00821             mPendingQuitCode = exitCode;
00822             return;
00823         }
00824     }
00825 
00826     // This was the last/only running "instance" of the program, so exit completely.
00827     kdDebug(5950) << "KAlarmApp::quitIf(" << exitCode << "): quitting" << endl;
00828     exit(exitCode);
00829 }
00830 
00831 /******************************************************************************
00832 * Called when the Quit menu item is selected.
00833 * Closes the system tray window and all main windows, but does not exit the
00834 * program if other windows are still open.
00835 */
00836 void KAlarmApp::doQuit(QWidget* parent)
00837 {
00838     kdDebug(5950) << "KAlarmApp::doQuit()\n";
00839     if (mDisableAlarmsIfStopped
00840     &&  MessageBox::warningContinueCancel(parent, KMessageBox::Cancel,
00841                                           i18n("Quitting will disable alarms\n(once any alarm message windows are closed)."),
00842                                           QString::null, KStdGuiItem::quit(), Preferences::QUIT_WARN
00843                                          ) != KMessageBox::Yes)
00844         return;
00845     quitIf(0, true);
00846 }
00847 
00848 /******************************************************************************
00849 * Called when the session manager is about to close down the application.
00850 */
00851 void KAlarmApp::commitData(QSessionManager& sm)
00852 {
00853     mSessionClosingDown = true;
00854     KUniqueApplication::commitData(sm);
00855     mSessionClosingDown = false;         // reset in case shutdown is cancelled
00856 }
00857 
00858 /******************************************************************************
00859 * Display an error message for a fatal error. Prevent further actions since
00860 * the program state is unsafe.
00861 */
00862 void KAlarmApp::displayFatalError(const QString& message)
00863 {
00864     if (!mFatalError)
00865     {
00866         mFatalError = 1;
00867         mFatalMessage = message;
00868         if (theInstance)
00869             QTimer::singleShot(0, theInstance, SLOT(quitFatal()));
00870     }
00871 }
00872 
00873 /******************************************************************************
00874 * Quit the program, once the fatal error message has been acknowledged.
00875 */
00876 void KAlarmApp::quitFatal()
00877 {
00878     switch (mFatalError)
00879     {
00880         case 0:
00881         case 2:
00882             return;
00883         case 1:
00884             mFatalError = 2;
00885             KMessageBox::error(0, mFatalMessage);
00886             mFatalError = 3;
00887             // fall through to '3'
00888         case 3:
00889             if (theInstance)
00890                 theInstance->quitIf(1, true);
00891             break;
00892     }
00893     QTimer::singleShot(1000, this, SLOT(quitFatal()));
00894 }
00895 
00896 /******************************************************************************
00897 * The main processing loop for KAlarm.
00898 * All KAlarm operations involving opening or updating calendar files are called
00899 * from this loop to ensure that only one operation is active at any one time.
00900 * This precaution is necessary because KAlarm's activities are mostly
00901 * asynchronous, being in response to DCOP calls from the alarm daemon (or other
00902 * programs) or timer events, any of which can be received in the middle of
00903 * performing another operation. If a calendar file is opened or updated while
00904 * another calendar operation is in progress, the program has been observed to
00905 * hang, or the first calendar call has failed with data loss - clearly
00906 * unacceptable!!
00907 */
00908 void KAlarmApp::processQueue()
00909 {
00910     if (mInitialised  &&  !mProcessingQueue)
00911     {
00912         kdDebug(5950) << "KAlarmApp::processQueue()\n";
00913         mProcessingQueue = true;
00914 
00915         // Reset the alarm daemon if it's been queued
00916         KAlarm::resetDaemonIfQueued();
00917 
00918         // Process DCOP calls
00919         while (!mDcopQueue.isEmpty())
00920         {
00921             DcopQEntry& entry = mDcopQueue.first();
00922             if (entry.eventId.isEmpty())
00923             {
00924                 // It's a new alarm
00925                 switch (entry.function)
00926                 {
00927                 case EVENT_TRIGGER:
00928                     execAlarm(entry.event, entry.event.firstAlarm(), false);
00929                     break;
00930                 case EVENT_HANDLE:
00931                     KAlarm::addEvent(entry.event, 0);
00932                     break;
00933                 case EVENT_CANCEL:
00934                     break;
00935                 }
00936             }
00937             else
00938                 handleEvent(entry.eventId, entry.function);
00939             mDcopQueue.pop_front();
00940         }
00941 
00942         // Purge the expired alarms calendar if it's time to do so
00943         AlarmCalendar::expiredCalendar()->purgeIfQueued();
00944 
00945         // Now that the queue has been processed, quit if a quit was queued
00946         if (mPendingQuit)
00947             quitIf(mPendingQuitCode);
00948 
00949         mProcessingQueue = false;
00950     }
00951 }
00952 
00953 /******************************************************************************
00954 *  Redisplay alarms which were being shown when the program last exited.
00955 *  Normally, these alarms will have been displayed by session restoration, but
00956 *  if the program crashed or was killed, we can redisplay them here so that
00957 *  they won't be lost.
00958 */
00959 void KAlarmApp::redisplayAlarms()
00960 {
00961     AlarmCalendar* cal = AlarmCalendar::displayCalendar();
00962     if (cal->isOpen())
00963     {
00964         KCal::Event::List events = cal->events();
00965         for (KCal::Event::List::ConstIterator it = events.begin();  it != events.end();  ++it)
00966         {
00967                         KCal::Event* kcalEvent = *it;
00968             KAEvent event(*kcalEvent);
00969             event.setUid(KAEvent::ACTIVE);
00970             if (!MessageWin::findEvent(event.id()))
00971             {
00972                 // This event should be displayed, but currently isn't being
00973                 kdDebug(5950) << "KAlarmApp::redisplayAlarms(): " << event.id() << endl;
00974                 KAAlarm alarm = event.convertDisplayingAlarm();
00975                 (new MessageWin(event, alarm, false, !alarm.repeatAtLogin()))->show();
00976             }
00977         }
00978     }
00979 }
00980 
00981 /******************************************************************************
00982 * Called when the system tray main window is closed.
00983 */
00984 void KAlarmApp::removeWindow(TrayWindow*)
00985 {
00986     mTrayWindow = 0;
00987     quitIf();
00988 }
00989 
00990 /******************************************************************************
00991 *  Display or close the system tray icon.
00992 */
00993 bool KAlarmApp::displayTrayIcon(bool show, MainWindow* parent)
00994 {
00995     static bool creating = false;
00996     if (show)
00997     {
00998         if (!mTrayWindow  &&  !creating)
00999         {
01000             if (!mHaveSystemTray)
01001                 return false;
01002             if (!MainWindow::count()  &&  wantRunInSystemTray())
01003             {
01004                 creating = true;    // prevent main window constructor from creating an additional tray icon
01005                 parent = MainWindow::create();
01006                 creating = false;
01007             }
01008             mTrayWindow = new TrayWindow(parent ? parent : MainWindow::firstWindow());
01009             connect(mTrayWindow, SIGNAL(deleted()), SIGNAL(trayIconToggled()));
01010             mTrayWindow->show();
01011             emit trayIconToggled();
01012 
01013             // Set up a timer so that we can check after all events in the window system's
01014             // event queue have been processed, whether the system tray actually exists
01015             mCheckingSystemTray = true;
01016             mSavedNoSystemTray  = mNoSystemTray;
01017             mNoSystemTray       = false;
01018             QTimer::singleShot(0, this, SLOT(slotSystemTrayTimer()));
01019         }
01020     }
01021     else if (mTrayWindow)
01022     {
01023         delete mTrayWindow;
01024         mTrayWindow = 0;
01025     }
01026     return true;
01027 }
01028 
01029 /******************************************************************************
01030 *  Called by a timer to check whether the system tray icon has been housed in
01031 *  the system tray. Because there is a delay between the system tray icon show
01032 *  event and the icon being reparented by the system tray, we have to use a
01033 *  timer to check whether the system tray has actually grabbed it, or whether
01034 *  the system tray probably doesn't exist.
01035 */
01036 void KAlarmApp::slotSystemTrayTimer()
01037 {
01038     mCheckingSystemTray = false;
01039     if (!checkSystemTray())
01040         quitIf(0);    // exit the application if there are no open windows
01041 }
01042 
01043 /******************************************************************************
01044 *  Check whether the system tray icon has been housed in the system tray.
01045 *  If the system tray doesn't seem to exist, tell the alarm daemon to notify us
01046 *  of alarms regardless of whether we're running.
01047 */
01048 bool KAlarmApp::checkSystemTray()
01049 {
01050     if (mCheckingSystemTray  ||  !mTrayWindow)
01051         return true;
01052     if (mTrayWindow->inSystemTray() != !mSavedNoSystemTray)
01053     {
01054         kdDebug(5950) << "KAlarmApp::checkSystemTray(): changed -> " << mSavedNoSystemTray << endl;
01055         mNoSystemTray = mSavedNoSystemTray = !mSavedNoSystemTray;
01056 
01057         // Store the new setting in the config file, so that if KAlarm exits and is then
01058         // next activated by the daemon to display a message, it will register with the
01059         // daemon with the correct NOTIFY type. If that happened when there was no system
01060         // tray and alarms are disabled when KAlarm is not running, registering with
01061         // NO_START_NOTIFY could result in alarms never being seen.
01062         KConfig* config = kapp->config();
01063         config->setGroup(QString::fromLatin1("General"));
01064         config->writeEntry(QString::fromLatin1("NoSystemTray"), mNoSystemTray);
01065         config->sync();
01066 
01067         // Update other settings and reregister with the alarm daemon
01068         slotPreferencesChanged();
01069     }
01070     else
01071     {
01072         kdDebug(5950) << "KAlarmApp::checkSystemTray(): no change = " << !mSavedNoSystemTray << endl;
01073         mNoSystemTray = mSavedNoSystemTray;
01074     }
01075     return !mNoSystemTray;
01076 }
01077 
01078 /******************************************************************************
01079 * Return the main window associated with the system tray icon.
01080 */
01081 MainWindow* KAlarmApp::trayMainWindow() const
01082 {
01083     return mTrayWindow ? mTrayWindow->assocMainWindow() : 0;
01084 }
01085 
01086 /******************************************************************************
01087 *  Called when KAlarm preferences have changed.
01088 */
01089 void KAlarmApp::slotPreferencesChanged()
01090 {
01091     bool newRunInSysTray = wantRunInSystemTray();
01092     if (newRunInSysTray != mOldRunInSystemTray)
01093     {
01094         // The system tray run mode has changed
01095         ++mActiveCount;         // prevent the application from quitting
01096         MainWindow* win = mTrayWindow ? mTrayWindow->assocMainWindow() : 0;
01097         delete mTrayWindow;     // remove the system tray icon if it is currently shown
01098         mTrayWindow = 0;
01099         mOldRunInSystemTray = newRunInSysTray;
01100         if (!newRunInSysTray)
01101         {
01102             if (win  &&  win->isHidden())
01103                 delete win;
01104         }
01105         displayTrayIcon(true);
01106         --mActiveCount;
01107     }
01108 
01109     bool newDisableIfStopped = wantRunInSystemTray() && !mNoSystemTray && Preferences::disableAlarmsIfStopped();
01110     if (newDisableIfStopped != mDisableAlarmsIfStopped)
01111     {
01112         mDisableAlarmsIfStopped = newDisableIfStopped;    // N.B. this setting is used by Daemon::reregister()
01113         Preferences::setQuitWarn(true);   // since mode has changed, re-allow warning messages on Quit
01114         Daemon::reregister();           // re-register with the alarm daemon
01115     }
01116 
01117     // Change alarm times for date-only alarms if the start of day time has changed
01118     if (Preferences::startOfDay() != mStartOfDay)
01119         changeStartOfDay();
01120 
01121     // In case the date for February 29th recurrences has changed
01122     KARecurrence::setDefaultFeb29Type(Preferences::defaultFeb29Type());
01123 
01124     if (Preferences::showAlarmTime()   != mPrefsShowTime
01125     ||  Preferences::showTimeToAlarm() != mPrefsShowTimeTo)
01126     {
01127         // The default alarm list time columns selection has changed
01128         MainWindow::updateTimeColumns(mPrefsShowTime, mPrefsShowTimeTo);
01129         mPrefsShowTime   = Preferences::showAlarmTime();
01130         mPrefsShowTimeTo = Preferences::showTimeToAlarm();
01131     }
01132 
01133     if (Preferences::expiredColour() != mPrefsExpiredColour)
01134     {
01135         // The expired alarms text colour has changed
01136         mRefreshExpiredAlarms = true;
01137         mPrefsExpiredColour = Preferences::expiredColour();
01138     }
01139 
01140     if (Preferences::expiredKeepDays() != mPrefsExpiredKeepDays)
01141     {
01142         // How long expired alarms are being kept has changed.
01143         // N.B. This also adjusts for any change in start-of-day time.
01144         mPrefsExpiredKeepDays = Preferences::expiredKeepDays();
01145         AlarmCalendar::expiredCalendar()->setPurgeDays(mPrefsExpiredKeepDays);
01146     }
01147 
01148     if (mRefreshExpiredAlarms)
01149     {
01150         mRefreshExpiredAlarms = false;
01151         MainWindow::updateExpired();
01152     }
01153 }
01154 
01155 /******************************************************************************
01156 *  Change alarm times for date-only alarms after the start of day time has changed.
01157 */
01158 void KAlarmApp::changeStartOfDay()
01159 {
01160     QTime sod = Preferences::startOfDay();
01161     DateTime::setStartOfDay(sod);
01162     AlarmCalendar* cal = AlarmCalendar::activeCalendar();
01163     if (KAEvent::adjustStartOfDay(cal->events()))
01164         cal->save();
01165     Preferences::updateStartOfDayCheck();  // now that calendar is updated, set OK flag in config file
01166     mStartOfDay = sod;
01167 }
01168 
01169 /******************************************************************************
01170 *  Called when the expired alarms calendar has been purged.
01171 *  Updates the alarm list in all main windows.
01172 */
01173 void KAlarmApp::slotExpiredPurged()
01174 {
01175     mRefreshExpiredAlarms = false;
01176     MainWindow::updateExpired();
01177 }
01178 
01179 /******************************************************************************
01180 *  Return whether the program is configured to be running in the system tray.
01181 */
01182 bool KAlarmApp::wantRunInSystemTray() const
01183 {
01184     return Preferences::runInSystemTray()  &&  mHaveSystemTray;
01185 }
01186 
01187 /******************************************************************************
01188 * Called to schedule a new alarm, either in response to a DCOP notification or
01189 * to command line options.
01190 * Reply = true unless there was a parameter error or an error opening calendar file.
01191 */
01192 bool KAlarmApp::scheduleEvent(KAEvent::Action action, const QString& text, const QDateTime& dateTime,
01193                               int lateCancel, int flags, const QColor& bg, const QColor& fg, const QFont& font,
01194                               const QString& audioFile, float audioVolume, int reminderMinutes,
01195                               const KARecurrence& recurrence, int repeatInterval, int repeatCount,
01196                               const QString& mailFromID, const EmailAddressList& mailAddresses,
01197                               const QString& mailSubject, const QStringList& mailAttachments)
01198 {
01199     kdDebug(5950) << "KAlarmApp::scheduleEvent(): " << text << endl;
01200     if (!dateTime.isValid())
01201         return false;
01202     QDateTime now = QDateTime::currentDateTime();
01203     if (lateCancel  &&  dateTime < now.addSecs(-maxLateness(lateCancel)))
01204         return true;               // alarm time was already expired too long ago
01205     QDateTime alarmTime = dateTime;
01206     // Round down to the nearest minute to avoid scheduling being messed up
01207     alarmTime.setTime(QTime(alarmTime.time().hour(), alarmTime.time().minute(), 0));
01208 
01209     KAEvent event(alarmTime, text, bg, fg, font, action, lateCancel, flags);
01210     if (reminderMinutes)
01211     {
01212         bool onceOnly = (reminderMinutes < 0);
01213         event.setReminder((onceOnly ? -reminderMinutes : reminderMinutes), onceOnly);
01214     }
01215     if (!audioFile.isEmpty())
01216         event.setAudioFile(audioFile, audioVolume, -1, 0);
01217     if (!mailAddresses.isEmpty())
01218         event.setEmail(mailFromID, mailAddresses, mailSubject, mailAttachments);
01219     event.setRecurrence(recurrence);
01220     event.setFirstRecurrence();
01221     event.setRepetition(repeatInterval, repeatCount - 1);
01222     if (alarmTime <= now)
01223     {
01224         // Alarm is due for display already.
01225         // First execute it once without adding it to the calendar file.
01226         if (!mInitialised)
01227             mDcopQueue.append(DcopQEntry(event, EVENT_TRIGGER));
01228         else
01229             execAlarm(event, event.firstAlarm(), false);
01230         // If it's a recurring alarm, reschedule it for its next occurrence
01231         if (!event.recurs()
01232         ||  event.setNextOccurrence(now) == KAEvent::NO_OCCURRENCE)
01233             return true;
01234         // It has recurrences in the future
01235     }
01236 
01237     // Queue the alarm for insertion into the calendar file
01238     mDcopQueue.append(DcopQEntry(event));
01239     if (mInitialised)
01240         QTimer::singleShot(0, this, SLOT(processQueue()));
01241     return true;
01242 }
01243 
01244 /******************************************************************************
01245 * Called in response to a DCOP notification by the alarm daemon that an event
01246 * should be handled, i.e. displayed or cancelled.
01247 * Optionally display the event. Delete the event from the calendar file and
01248 * from every main window instance.
01249 */
01250 bool KAlarmApp::handleEvent(const QString& urlString, const QString& eventID, EventFunc function)
01251 {
01252     kdDebug(5950) << "KAlarmApp::handleEvent(DCOP): " << eventID << endl;
01253     AlarmCalendar* cal = AlarmCalendar::activeCalendar();     // this can be called before calendars have been initialised
01254     if (cal  &&  KURL(urlString).url() != cal->urlString())
01255     {
01256         kdError(5950) << "KAlarmApp::handleEvent(DCOP): wrong calendar file " << urlString << endl;
01257         Daemon::eventHandled(eventID, false);
01258         return false;
01259     }
01260     mDcopQueue.append(DcopQEntry(function, eventID));
01261     if (mInitialised)
01262         QTimer::singleShot(0, this, SLOT(processQueue()));
01263     return true;
01264 }
01265 
01266 /******************************************************************************
01267 * Either:
01268 * a) Display the event and then delete it if it has no outstanding repetitions.
01269 * b) Delete the event.
01270 * c) Reschedule the event for its next repetition. If none remain, delete it.
01271 * If the event is deleted, it is removed from the calendar file and from every
01272 * main window instance.
01273 */
01274 bool KAlarmApp::handleEvent(const QString& eventID, EventFunc function)
01275 {
01276     kdDebug(5950) << "KAlarmApp::handleEvent(): " << eventID << ", " << (function==EVENT_TRIGGER?"TRIGGER":function==EVENT_CANCEL?"CANCEL":function==EVENT_HANDLE?"HANDLE":"?") << endl;
01277     KCal::Event* kcalEvent = AlarmCalendar::activeCalendar()->event(eventID);
01278     if (!kcalEvent)
01279     {
01280         kdError(5950) << "KAlarmApp::handleEvent(): event ID not found: " << eventID << endl;
01281         Daemon::eventHandled(eventID, false);
01282         return false;
01283     }
01284     KAEvent event(*kcalEvent);
01285     switch (function)
01286     {
01287         case EVENT_CANCEL:
01288             KAlarm::deleteEvent(event, true);
01289             break;
01290 
01291         case EVENT_TRIGGER:    // handle it if it's due, else execute it regardless
01292         case EVENT_HANDLE:     // handle it if it's due
01293         {
01294             QDateTime now = QDateTime::currentDateTime();
01295             DateTime  repeatDT;
01296             bool updateCalAndDisplay = false;
01297             bool alarmToExecuteValid = false;
01298             KAAlarm alarmToExecute;
01299             // Check all the alarms in turn.
01300             // Note that the main alarm is fetched before any other alarms.
01301             for (KAAlarm alarm = event.firstAlarm();  alarm.valid();  alarm = event.nextAlarm(alarm))
01302             {
01303                 if (alarm.deferred()  &&  event.repeatCount()
01304                 &&  repeatDT.isValid()  &&  alarm.dateTime() > repeatDT)
01305                 {
01306                     // This deferral of a repeated alarm is later than the last previous
01307                     // occurrence of the main alarm, so use the deferral alarm instead.
01308                     // If the deferral is not yet due, this prevents the main alarm being
01309                     // triggered repeatedly. If the deferral is due, this triggers it
01310                     // in preference to the main alarm.
01311                     alarmToExecute      = KAAlarm();
01312                     alarmToExecuteValid = false;
01313                     updateCalAndDisplay = false;
01314                 }
01315                 // Check if the alarm is due yet.
01316                 int secs = alarm.dateTime(true).secsTo(now);
01317                 if (secs < 0)
01318                 {
01319                     // This alarm is definitely not due yet
01320                     kdDebug(5950) << "KAlarmApp::handleEvent(): alarm " << alarm.type() << ": not due\n";
01321                     continue;
01322                 }
01323                 if (alarm.repeatAtLogin())
01324                 {
01325                     // Alarm is to be displayed at every login.
01326                     // Check if the alarm has only just been set up.
01327                     // (The alarm daemon will immediately notify that it is due
01328                     //  since it is set up with a time in the past.)
01329                     kdDebug(5950) << "KAlarmApp::handleEvent(): REPEAT_AT_LOGIN\n";
01330                     if (secs < maxLateness(1))
01331                         continue;
01332 
01333                     // Check if the main alarm is already being displayed.
01334                     // (We don't want to display both at the same time.)
01335                     if (alarmToExecute.valid())
01336                         continue;
01337 
01338                     // Set the time to display if it's a display alarm
01339                     alarm.setTime(now);
01340                 }
01341                 if (alarm.lateCancel())
01342                 {
01343                     // Alarm is due, and it is to be cancelled if too late.
01344                     kdDebug(5950) << "KAlarmApp::handleEvent(): LATE_CANCEL\n";
01345                     bool late = false;
01346                     bool cancel = false;
01347                     if (alarm.dateTime().isDateOnly())
01348                     {
01349                         // The alarm has no time, so cancel it if its date is too far past
01350                         int maxlate = alarm.lateCancel() / 1440;    // maximum lateness in days
01351                         QDateTime limit(alarm.date().addDays(maxlate + 1), Preferences::startOfDay());
01352                         if (now >= limit)
01353                         {
01354                             // It's too late to display the scheduled occurrence.
01355                             // Find the last previous occurrence of the alarm.
01356                             DateTime next;
01357                             KAEvent::OccurType type = event.previousOccurrence(now, next, true);
01358                             switch (type & ~KAEvent::OCCURRENCE_REPEAT)
01359                             {
01360                                 case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
01361                                 case KAEvent::RECURRENCE_DATE:
01362                                 case KAEvent::RECURRENCE_DATE_TIME:
01363                                 case KAEvent::LAST_RECURRENCE:
01364                                     limit.setDate(next.date().addDays(maxlate + 1));
01365                                     limit.setTime(Preferences::startOfDay());
01366                                     if (now >= limit)
01367                                     {
01368                                         if (type == KAEvent::LAST_RECURRENCE
01369                                         ||  type == KAEvent::FIRST_OR_ONLY_OCCURRENCE && !event.recurs())
01370                                             cancel = true;   // last occurrence (and there are no repetitions)
01371                                         else
01372                                             late = true;
01373                                     }
01374                                     break;
01375                                 case KAEvent::NO_OCCURRENCE:
01376                                 default:
01377                                     late = true;
01378                                     break;
01379                             }
01380                         }
01381                     }
01382                     else
01383                     {
01384                         // The alarm is timed. Allow it to be the permitted amount late before cancelling it.
01385                         int maxlate = maxLateness(alarm.lateCancel());
01386                         if (secs > maxlate)
01387                         {
01388                             // It's over the maximum interval late.
01389                             // Find the most recent occurrence of the alarm.
01390                             DateTime next;
01391                             KAEvent::OccurType type = event.previousOccurrence(now, next, true);
01392                             switch (type & ~KAEvent::OCCURRENCE_REPEAT)
01393                             {
01394                                 case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
01395                                 case KAEvent::RECURRENCE_DATE:
01396                                 case KAEvent::RECURRENCE_DATE_TIME:
01397                                 case KAEvent::LAST_RECURRENCE:
01398                                     if (next.dateTime().secsTo(now) > maxlate)
01399                                     {
01400                                         if (type == KAEvent::LAST_RECURRENCE
01401                                         ||  type == KAEvent::FIRST_OR_ONLY_OCCURRENCE && !event.recurs())
01402                                             cancel = true;   // last occurrence (and there are no repetitions)
01403                                         else
01404                                             late = true;
01405                                     }
01406                                     break;
01407                                 case KAEvent::NO_OCCURRENCE:
01408                                 default:
01409                                     late = true;
01410                                     break;
01411                             }
01412                         }
01413                     }
01414 
01415                     if (cancel)
01416                     {
01417                         // All recurrences are finished, so cancel the event
01418                         event.setArchive();
01419                         cancelAlarm(event, alarm.type(), false);
01420                         updateCalAndDisplay = true;
01421                         continue;
01422                     }
01423                     if (late)
01424                     {
01425                         // The latest repetition was too long ago, so schedule the next one
01426                         rescheduleAlarm(event, alarm, false);
01427                         updateCalAndDisplay = true;
01428                         continue;
01429                     }
01430                 }
01431                 if (!alarmToExecuteValid)
01432                 {
01433                     kdDebug(5950) << "KAlarmApp::handleEvent(): alarm " << alarm.type() << ": execute\n";
01434                     alarmToExecute = alarm;             // note the alarm to be executed
01435                     alarmToExecuteValid = true;         // only trigger one alarm for the event
01436                 }
01437                 else
01438                     kdDebug(5950) << "KAlarmApp::handleEvent(): alarm " << alarm.type() << ": skip\n";
01439             }
01440 
01441             // If there is an alarm to execute, do this last after rescheduling/cancelling
01442             // any others. This ensures that the updated event is only saved once to the calendar.
01443             if (alarmToExecute.valid())
01444                 execAlarm(event, alarmToExecute, true, !alarmToExecute.repeatAtLogin());
01445             else
01446             {
01447                 if (function == EVENT_TRIGGER)
01448                 {
01449                     // The alarm is to be executed regardless of whether it's due.
01450                     // Only trigger one alarm from the event - we don't want multiple
01451                     // identical messages, for example.
01452                     KAAlarm alarm = event.firstAlarm();
01453                     if (alarm.valid())
01454                         execAlarm(event, alarm, false);
01455                 }
01456                 if (updateCalAndDisplay)
01457                     KAlarm::updateEvent(event, 0);     // update the window lists and calendar file
01458                 else if (function != EVENT_TRIGGER)
01459                 {
01460                     kdDebug(5950) << "KAlarmApp::handleEvent(): no action\n";
01461                     Daemon::eventHandled(eventID, false);
01462                 }
01463             }
01464             break;
01465         }
01466     }
01467     return true;
01468 }
01469 
01470 /******************************************************************************
01471 * Called when an alarm is currently being displayed, to store a copy of the
01472 * alarm in the displaying calendar, and to reschedule it for its next repetition.
01473 * If no repetitions remain, cancel it.
01474 */
01475 void KAlarmApp::alarmShowing(KAEvent& event, KAAlarm::Type alarmType, const DateTime& alarmTime)
01476 {
01477     kdDebug(5950) << "KAlarmApp::alarmShowing(" << event.id() << ", " << KAAlarm::debugType(alarmType) << ")\n";
01478     KCal::Event* kcalEvent = AlarmCalendar::activeCalendar()->event(event.id());
01479     if (!kcalEvent)
01480         kdError(5950) << "KAlarmApp::alarmShowing(): event ID not found: " << event.id() << endl;
01481     else
01482     {
01483         KAAlarm alarm = event.alarm(alarmType);
01484         if (!alarm.valid())
01485             kdError(5950) << "KAlarmApp::alarmShowing(): alarm type not found: " << event.id() << ":" << alarmType << endl;
01486         else
01487         {
01488             // Copy the alarm to the displaying calendar in case of a crash, etc.
01489             KAEvent dispEvent;
01490             dispEvent.setDisplaying(event, alarmType, alarmTime.dateTime());
01491             AlarmCalendar* cal = AlarmCalendar::displayCalendarOpen();
01492             if (cal)
01493             {
01494                 cal->deleteEvent(dispEvent.id());   // in case it already exists
01495                 cal->addEvent(dispEvent);
01496                 cal->save();
01497             }
01498 
01499             rescheduleAlarm(event, alarm, true);
01500             return;
01501         }
01502     }
01503     Daemon::eventHandled(event.id(), false);
01504 }
01505 
01506 /******************************************************************************
01507 * Called when an alarm action has completed, to perform any post-alarm actions.
01508 */
01509 void KAlarmApp::alarmCompleted(const KAEvent& event)
01510 {
01511     if (!event.postAction().isEmpty()  &&  ShellProcess::authorised())
01512     {
01513         QString command = event.postAction();
01514         kdDebug(5950) << "KAlarmApp::alarmCompleted(" << event.id() << "): " << command << endl;
01515         doShellCommand(command, event, 0, ProcData::POST_ACTION);
01516     }
01517 }
01518 
01519 /******************************************************************************
01520 * Reschedule the alarm for its next recurrence. If none remain, delete it.
01521 * If the alarm is deleted and it is the last alarm for its event, the event is
01522 * removed from the calendar file and from every main window instance.
01523 */
01524 void KAlarmApp::rescheduleAlarm(KAEvent& event, const KAAlarm& alarm, bool updateCalAndDisplay)
01525 {
01526     kdDebug(5950) << "KAlarmApp::rescheduleAlarm()" << endl;
01527     bool update = false;
01528     if (alarm.reminder()  ||  alarm.deferred())
01529     {
01530         // It's an advance warning alarm or an extra deferred alarm, so delete it
01531         event.removeExpiredAlarm(alarm.type());
01532         update = true;
01533     }
01534     else if (alarm.repeatAtLogin())
01535     {
01536         // Leave an alarm which repeats at every login until its main alarm is deleted
01537         if (updateCalAndDisplay  &&  event.updated())
01538             update = true;
01539     }
01540     else
01541     {
01542         // Reschedule the alarm for its next recurrence.
01543         KAEvent::OccurType type = event.setNextOccurrence(QDateTime::currentDateTime());
01544         switch (type)
01545         {
01546             case KAEvent::NO_OCCURRENCE:
01547                 // All repetitions are finished, so cancel the event
01548                 cancelAlarm(event, alarm.type(), updateCalAndDisplay);
01549                 break;
01550             default:
01551                 if (!(type & KAEvent::OCCURRENCE_REPEAT))
01552                     break;
01553                 // Next occurrence is a repeat, so fall through to recurrence handling
01554             case KAEvent::RECURRENCE_DATE:
01555             case KAEvent::RECURRENCE_DATE_TIME:
01556             case KAEvent::LAST_RECURRENCE:
01557                 // The event is due by now and repetitions still remain, so rewrite the event
01558                 if (updateCalAndDisplay)
01559                     update = true;
01560                 else
01561                 {
01562                     event.cancelCancelledDeferral();
01563                     event.setUpdated();    // note that the calendar file needs to be updated
01564                 }
01565                 break;
01566             case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
01567                 // The first occurrence is still due?!?, so don't do anything
01568                 break;
01569         }
01570         if (event.deferred())
01571         {
01572             // Just in case there's also a deferred alarm, ensure it's removed
01573             event.removeExpiredAlarm(KAAlarm::DEFERRED_ALARM);
01574             update = true;
01575         }
01576     }
01577     if (update)
01578     {
01579         event.cancelCancelledDeferral();
01580         KAlarm::updateEvent(event, 0);     // update the window lists and calendar file
01581     }
01582 }
01583 
01584 /******************************************************************************
01585 * Delete the alarm. If it is the last alarm for its event, the event is removed
01586 * from the calendar file and from every main window instance.
01587 */
01588 void KAlarmApp::cancelAlarm(KAEvent& event, KAAlarm::Type alarmType, bool updateCalAndDisplay)
01589 {
01590     kdDebug(5950) << "KAlarmApp::cancelAlarm()" << endl;
01591     event.cancelCancelledDeferral();
01592     if (alarmType == KAAlarm::MAIN_ALARM  &&  !event.displaying()  &&  event.toBeArchived())
01593     {
01594         // The event is being deleted. Save it in the expired calendar file first.
01595         QString id = event.id();    // save event ID since KAlarm::addExpiredEvent() changes it
01596         KAlarm::addExpiredEvent(event);
01597         event.setEventID(id);       // restore event ID
01598     }
01599     event.removeExpiredAlarm(alarmType);
01600     if (!event.alarmCount())
01601         KAlarm::deleteEvent(event, false);
01602     else if (updateCalAndDisplay)
01603         KAlarm::updateEvent(event, 0);    // update the window lists and calendar file
01604 }
01605 
01606 /******************************************************************************
01607 * Execute an alarm by displaying its message or file, or executing its command.
01608 * Reply = ShellProcess instance if a command alarm
01609 *       != 0 if successful
01610 *       = 0 if the alarm is disabled, or if an error message was output.
01611 */
01612 void* KAlarmApp::execAlarm(KAEvent& event, const KAAlarm& alarm, bool reschedule, bool allowDefer, bool noPreAction)
01613 {
01614     if (!event.enabled())
01615     {
01616         // The event is disabled.
01617         if (reschedule)
01618             rescheduleAlarm(event, alarm, true);
01619         return 0;
01620     }
01621 
01622     void* result = (void*)1;
01623     event.setArchive();
01624     switch (alarm.action())
01625     {
01626         case KAAlarm::MESSAGE:
01627         case KAAlarm::FILE:
01628         {
01629             // Display a message or file, provided that the same event isn't already being displayed
01630             MessageWin* win = MessageWin::findEvent(event.id());
01631             // Find if we're changing a reminder message to the real message
01632             bool reminder = (alarm.type() & KAAlarm::REMINDER_ALARM);
01633             bool replaceReminder = !reminder && win && (win->alarmType() & KAAlarm::REMINDER_ALARM);
01634             if (!reminder  &&  !event.deferred()
01635             &&  (replaceReminder || !win)  &&  !noPreAction
01636             &&  !event.preAction().isEmpty()  &&  ShellProcess::authorised())
01637             {
01638                 // It's not a reminder or a deferred alarm, and there is no message window
01639                 // (other than a reminder window) currently displayed for this alarm,
01640                 // and we need to execute a command before displaying the new window.
01641                 // Check whether the command is already being executed for this alarm.
01642                 for (QValueList<ProcData*>::Iterator it = mCommandProcesses.begin();  it != mCommandProcesses.end();  ++it)
01643                 {
01644                     ProcData* pd = *it;
01645                     if (pd->event->id() == event.id()  &&  (pd->flags & ProcData::PRE_ACTION))
01646                     {
01647                         kdDebug(5950) << "KAlarmApp::execAlarm(): already executing pre-DISPLAY command" << endl;
01648                         return pd->process;   // already executing - don't duplicate the action
01649                     }
01650                 }
01651 
01652                 QString command = event.preAction();
01653                 kdDebug(5950) << "KAlarmApp::execAlarm(): pre-DISPLAY command: " << command << endl;
01654                 int flags = (reschedule ? ProcData::RESCHEDULE : 0) | (allowDefer ? ProcData::ALLOW_DEFER : 0);
01655                 if (doShellCommand(command, event, &alarm, (flags | ProcData::PRE_ACTION)))
01656                     return result;     // display the message after the command completes
01657                 // Error executing command - display the message even though it failed
01658             }
01659             if (!event.enabled())
01660                 delete win;        // event is disabled - close its window
01661             else if (!win
01662                  ||  !win->hasDefer() && !alarm.repeatAtLogin()
01663                  ||  replaceReminder)
01664             {
01665                 // Either there isn't already a message for this event,
01666                 // or there is a repeat-at-login message with no Defer
01667                 // button, which needs to be replaced with a new message,
01668                 // or the caption needs to be changed from "Reminder" to "Message".
01669                 if (win)
01670                     win->setRecreating();    // prevent post-alarm actions
01671                 delete win;
01672                 (new MessageWin(event, alarm, reschedule, allowDefer))->show();
01673             }
01674             else
01675             {
01676                 // Raise the existing message window and replay any sound
01677                 win->repeat(alarm);    // N.B. this reschedules the alarm
01678             }
01679             break;
01680         }
01681         case KAAlarm::COMMAND:
01682         {
01683             int flags = event.commandXterm() ? ProcData::EXEC_IN_XTERM : 0;
01684             QString command = event.cleanText();
01685             if (event.commandScript())
01686             {
01687                 // Store the command script in a temporary file for execution
01688                 kdDebug(5950) << "KAlarmApp::execAlarm(): COMMAND: (script)" << endl;
01689                 QString tmpfile = createTempScriptFile(command, false, event, alarm);
01690                 if (tmpfile.isEmpty())
01691                     result = 0;
01692                 else
01693                     result = doShellCommand(tmpfile, event, &alarm, (flags | ProcData::TEMP_FILE));
01694             }
01695             else
01696             {
01697                 kdDebug(5950) << "KAlarmApp::execAlarm(): COMMAND: " << command << endl;
01698                 result = doShellCommand(command, event, &alarm, flags);
01699             }
01700             if (reschedule)
01701                 rescheduleAlarm(event, alarm, true);
01702             break;
01703         }
01704         case KAAlarm::EMAIL:
01705         {
01706             kdDebug(5950) << "KAlarmApp::execAlarm(): EMAIL to: " << event.emailAddresses(", ") << endl;
01707             QStringList errmsgs;
01708             if (!KAMail::send(event, errmsgs, (reschedule || allowDefer)))
01709                 result = 0;
01710             if (!errmsgs.isEmpty())
01711             {
01712                 // Some error occurred, although the email may have been sent successfully
01713                 if (result)
01714                     kdDebug(5950) << "KAlarmApp::execAlarm(): copy error: " << errmsgs[1] << endl;
01715                 else
01716                     kdDebug(5950) << "KAlarmApp::execAlarm(): failed: " << errmsgs[1] << endl;
01717                 (new MessageWin(event, alarm.dateTime(), errmsgs))->show();
01718             }
01719             if (reschedule)
01720                 rescheduleAlarm(event, alarm, true);
01721             break;
01722         }
01723         default:
01724             return 0;
01725     }
01726     return result;
01727 }
01728 
01729 /******************************************************************************
01730 * Execute a shell command line specified by an alarm.
01731 * If the PRE_ACTION bit of 'flags' is set, the alarm will be executed via
01732 * execAlarm() once the command completes, the execAlarm() parameters being
01733 * derived from the remaining bits in 'flags'.
01734 */
01735 ShellProcess* KAlarmApp::doShellCommand(const QString& command, const KAEvent& event, const KAAlarm* alarm, int flags)
01736 {
01737     kdDebug(5950) << "KAlarmApp::doShellCommand(" << command << ", " << event.id() << ")" << endl;
01738     KProcess::Communication comms = KProcess::NoCommunication;
01739     QString cmd;
01740     QString tmpXtermFile;
01741     if (flags & ProcData::EXEC_IN_XTERM)
01742     {
01743         // Execute the command in a terminal window.
01744         cmd = Preferences::cmdXTermCommand();
01745         cmd.replace("%t", aboutData()->programName());     // set the terminal window title
01746         if (cmd.find("%C") >= 0)
01747         {
01748             // Execute the command from a temporary script file
01749             if (flags & ProcData::TEMP_FILE)
01750                 cmd.replace("%C", command);    // the command is already calling a temporary file
01751             else
01752             {
01753                 tmpXtermFile = createTempScriptFile(command, true, event, *alarm);
01754                 if (tmpXtermFile.isEmpty())
01755                     return 0;
01756                 cmd.replace("%C", tmpXtermFile);    // %C indicates where to insert the command
01757             }
01758         }
01759         else if (cmd.find("%W") >= 0)
01760         {
01761             // Execute the command from a temporary script file,
01762             // with a sleep after the command is executed
01763             tmpXtermFile = createTempScriptFile(command + QString::fromLatin1("\nsleep 86400\n"), true, event, *alarm);
01764             if (tmpXtermFile.isEmpty())
01765                 return 0;
01766             cmd.replace("%W", tmpXtermFile);    // %w indicates where to insert the command
01767         }
01768         else if (cmd.find("%w") >= 0)
01769         {
01770             // Append a sleep to the command.
01771             // Quote the command in case it contains characters such as [>|;].
01772             QString exec = KShellProcess::quote(command + QString::fromLatin1("; sleep 86400"));
01773             cmd.replace("%w", exec);    // %w indicates where to insert the command string
01774         }
01775         else
01776         {
01777             // Set the command to execute.
01778             // Put it in quotes in case it contains characters such as [>|;].
01779             QString exec = KShellProcess::quote(command);
01780             if (cmd.find("%c") >= 0)
01781                 cmd.replace("%c", exec);    // %c indicates where to insert the command string
01782             else
01783                 cmd.append(exec);           // otherwise, simply append the command string
01784         }
01785     }
01786     else
01787     {
01788         cmd = command;
01789         comms = KProcess::AllOutput;
01790     }
01791     ShellProcess* proc = new ShellProcess(cmd);
01792     connect(proc, SIGNAL(shellExited(ShellProcess*)), SLOT(slotCommandExited(ShellProcess*)));
01793     QGuardedPtr<ShellProcess> logproc = 0;
01794     if (comms == KProcess::AllOutput  &&  !event.logFile().isEmpty())
01795     {
01796         // Output is to be appended to a log file.
01797         // Set up a logging process to write the command's output to.
01798         connect(proc, SIGNAL(receivedStdout(KProcess*,char*,int)), SLOT(slotCommandOutput(KProcess*,char*,int)));
01799         connect(proc, SIGNAL(receivedStderr(KProcess*,char*,int)), SLOT(slotCommandOutput(KProcess*,char*,int)));
01800         logproc = new ShellProcess(QString::fromLatin1("cat >>%1").arg(event.logFile()));
01801         connect(logproc, SIGNAL(shellExited(ShellProcess*)), SLOT(slotLogProcExited(ShellProcess*)));
01802         logproc->start(KProcess::Stdin);
01803         QCString heading;
01804         if (alarm  &&  alarm->dateTime().isValid())
01805         {
01806             QString dateTime = alarm->dateTime().isDateOnly()
01807                              ? KGlobal::locale()->formatDate(alarm->dateTime().date(), true)
01808                              : KGlobal::locale()->formatDateTime(alarm->dateTime().dateTime());
01809             heading.sprintf("\n******* KAlarm %s *******\n", dateTime.latin1());
01810         }
01811         else
01812             heading = "\n******* KAlarm *******\n";
01813         logproc->writeStdin(heading, heading.length()+1);
01814     }
01815     ProcData* pd = new ProcData(proc, logproc, new KAEvent(event), (alarm ? new KAAlarm(*alarm) : 0), flags);
01816     if (flags & ProcData::TEMP_FILE)
01817         pd->tempFiles += command;
01818     if (!tmpXtermFile.isEmpty())
01819         pd->tempFiles += tmpXtermFile;
01820     mCommandProcesses.append(pd);
01821     if (proc->start(comms))
01822         return proc;
01823 
01824     // Error executing command - report it
01825     kdError(5950) << "KAlarmApp::doShellCommand(): command failed to start\n";
01826     commandErrorMsg(proc, event, alarm, flags);
01827     mCommandProcesses.remove(pd);
01828     delete pd;
01829     return 0;
01830 }
01831 
01832 /******************************************************************************
01833 * Create a temporary script file containing the specified command string.
01834 * Reply = path of temporary file, or null string if error.
01835 */
01836 QString KAlarmApp::createTempScriptFile(const QString& command, bool insertShell, const KAEvent& event, const KAAlarm& alarm)
01837 {
01838     KTempFile tmpFile(QString::null, QString::null, 0700);
01839     tmpFile.setAutoDelete(false);     // don't delete file when it is destructed
01840     QTextStream* stream = tmpFile.textStream();
01841     if (!stream)
01842         kdError(5950) << "KAlarmApp::createTempScript(): Unable to create a temporary script file" << endl;
01843     else
01844     {
01845         if (insertShell)
01846             *stream << "#!" << ShellProcess::shellPath() << "\n";
01847         *stream << command;
01848         tmpFile.close();
01849         if (tmpFile.status())
01850             kdError(5950) << "KAlarmApp::createTempScript(): Error " << tmpFile.status() << " writing to temporary script file" << endl;
01851         else
01852             return tmpFile.name();
01853     }
01854 
01855     QStringList errmsgs(i18n("Error creating temporary script file"));
01856     (new MessageWin(event, alarm.dateTime(), errmsgs))->show();
01857     return QString::null;
01858 }
01859 
01860 /******************************************************************************
01861 * Called when an executing command alarm sends output to stdout or stderr.
01862 */
01863 void KAlarmApp::slotCommandOutput(KProcess* proc, char* buffer, int bufflen)
01864 {
01865 //kdDebug(5950) << "KAlarmApp::slotCommandOutput(): '" << QCString(buffer, bufflen+1) << "'\n";
01866     // Find this command in the command list
01867     for (QValueList<ProcData*>::Iterator it = mCommandProcesses.begin();  it != mCommandProcesses.end();  ++it)
01868     {
01869         ProcData* pd = *it;
01870         if (pd->process == proc  &&  pd->logProcess)
01871         {
01872             pd->logProcess->writeStdin(buffer, bufflen);
01873             break;
01874         }
01875     }
01876 }
01877 
01878 /******************************************************************************
01879 * Called when a logging process completes.
01880 */
01881 void KAlarmApp::slotLogProcExited(ShellProcess* proc)
01882 {
01883     // Because it's held as a guarded pointer in the ProcData structure,
01884     // we don't need to set any pointers to zero.
01885     delete proc;
01886 }
01887 
01888 /******************************************************************************
01889 * Called when a command alarm's execution completes.
01890 */
01891 void KAlarmApp::slotCommandExited(ShellProcess* proc)
01892 {
01893     kdDebug(5950) << "KAlarmApp::slotCommandExited()\n";
01894     // Find this command in the command list
01895     for (QValueList<ProcData*>::Iterator it = mCommandProcesses.begin();  it != mCommandProcesses.end();  ++it)
01896     {
01897         ProcData* pd = *it;
01898         if (pd->process == proc)
01899         {
01900             // Found the command
01901             if (pd->logProcess)
01902                 pd->logProcess->stdinExit();   // terminate the logging process
01903 
01904             // Check its exit status
01905             if (!proc->normalExit())
01906             {
01907                 QString errmsg = proc->errorMessage();
01908                 kdWarning(5950) << "KAlarmApp::slotCommandExited(" << pd->event->cleanText() << "): " << errmsg << endl;
01909                 if (pd->messageBoxParent)
01910                 {
01911                     // Close the existing informational KMessageBox for this process
01912                     QObjectList* dialogs = pd->messageBoxParent->queryList("KDialogBase", 0, false, true);
01913                     KDialogBase* dialog = (KDialogBase*)dialogs->getFirst();
01914                     delete dialog;
01915                     delete dialogs;
01916                     if (!pd->tempFile())
01917                     {
01918                         errmsg += '\n';
01919                         errmsg += proc->command();
01920                     }
01921                     KMessageBox::error(pd->messageBoxParent, errmsg);
01922                 }
01923                 else
01924                     commandErrorMsg(proc, *pd->event, pd->alarm, pd->flags);
01925             }
01926             if (pd->preAction())
01927                 execAlarm(*pd->event, *pd->alarm, pd->reschedule(), pd->allowDefer(), true);
01928             mCommandProcesses.remove(it);
01929             delete pd;
01930             break;
01931         }
01932     }
01933 
01934     // If there are now no executing shell commands, quit if a quit was queued
01935     if (mPendingQuit  &&  mCommandProcesses.isEmpty())
01936         quitIf(mPendingQuitCode);
01937 }
01938 
01939 /******************************************************************************
01940 * Output an error message for a shell command.
01941 */
01942 void KAlarmApp::commandErrorMsg(const ShellProcess* proc, const KAEvent& event, const KAAlarm* alarm, int flags)
01943 {
01944     QStringList errmsgs;
01945     if (flags & ProcData::PRE_ACTION)
01946         errmsgs += i18n("Pre-alarm action:");
01947     else if (flags & ProcData::POST_ACTION)
01948         errmsgs += i18n("Post-alarm action:");
01949     errmsgs += proc->errorMessage();
01950     if (!(flags & ProcData::TEMP_FILE))
01951         errmsgs += proc->command();
01952     (new MessageWin(event, (alarm ? alarm->dateTime() : DateTime()), errmsgs))->show();
01953 }
01954 
01955 /******************************************************************************
01956 * Notes that an informational KMessageBox is displayed for this process.
01957 */
01958 void KAlarmApp::commandMessage(ShellProcess* proc, QWidget* parent)
01959 {
01960     // Find this command in the command list
01961     for (QValueList<ProcData*>::Iterator it = mCommandProcesses.begin();  it != mCommandProcesses.end();  ++it)
01962     {
01963         ProcData* pd = *it;
01964         if (pd->process == proc)
01965         {
01966             pd->messageBoxParent = parent;
01967             break;
01968         }
01969     }
01970 }
01971 
01972 /******************************************************************************
01973 * Set up remaining DCOP handlers and start processing DCOP calls.
01974 */
01975 void KAlarmApp::setUpDcop()
01976 {
01977     if (!mInitialised)
01978     {
01979         mInitialised = true;      // we're now ready to handle DCOP calls
01980         Daemon::createDcopHandler();
01981         QTimer::singleShot(0, this, SLOT(processQueue()));    // process anything already queued
01982     }
01983 }
01984 
01985 /******************************************************************************
01986 * If this is the first time through, open the calendar file, optionally start
01987 * the alarm daemon and register with it, and set up the DCOP handler.
01988 */
01989 bool KAlarmApp::initCheck(bool calendarOnly)
01990 {
01991     bool startdaemon;
01992     AlarmCalendar* cal = AlarmCalendar::activeCalendar();
01993     if (!cal->isOpen())
01994     {
01995         kdDebug(5950) << "KAlarmApp::initCheck(): opening active calendar\n";
01996 
01997         // First time through. Open the calendar file.
01998         if (!cal->open())
01999             return false;
02000 
02001         if (!mStartOfDay.isValid())
02002             changeStartOfDay();     // start of day time has changed, so adjust date-only alarms
02003 
02004         /* Need to open the display calendar now, since otherwise if the daemon
02005          * immediately notifies display alarms, they will often be processed while
02006          * redisplayAlarms() is executing open() (but before open() completes),
02007          * which causes problems!!
02008          */
02009         AlarmCalendar::displayCalendar()->open();
02010 
02011         /* Need to open the expired alarm calendar now, since otherwise if the daemon
02012          * immediately notifies multiple alarms, the second alarm is likely to be
02013          * processed while the calendar is executing open() (but before open() completes),
02014          * which causes a hang!!
02015          */
02016         AlarmCalendar::expiredCalendar()->open();
02017         AlarmCalendar::expiredCalendar()->setPurgeDays(theInstance->mPrefsExpiredKeepDays);
02018 
02019         startdaemon = true;
02020     }
02021     else
02022         startdaemon = !Daemon::isRegistered();
02023 
02024     if (!calendarOnly)
02025     {
02026         setUpDcop();      // start processing DCOP calls
02027         if (startdaemon)
02028             Daemon::start();  // make sure the alarm daemon is running
02029     }
02030     return true;
02031 }
02032 
02033 /******************************************************************************
02034 *  Convert the --time parameter string into a date/time or date value.
02035 *  The parameter is in the form [[[yyyy-]mm-]dd-]hh:mm or yyyy-mm-dd.
02036 *  Reply = true if successful.
02037 */
02038 static bool convWakeTime(const QCString& timeParam, QDateTime& dateTime, bool& noTime)
02039 {
02040     if (timeParam.length() > 19)
02041         return false;
02042     char timeStr[20];
02043     strcpy(timeStr, timeParam);
02044     int dt[5] = { -1, -1, -1, -1, -1 };
02045     char* s;
02046     char* end;
02047     // Get the minute value
02048     if ((s = strchr(timeStr, ':')) == 0)
02049         noTime = true;
02050     else
02051     {
02052         noTime = false;
02053         *s++ = 0;
02054         dt[4] = strtoul(s, &end, 10);
02055         if (end == s  ||  *end  ||  dt[4] >= 60)
02056             return false;
02057         // Get the hour value
02058         if ((s = strrchr(timeStr, '-')) == 0)
02059             s = timeStr;
02060         else
02061             *s++ = 0;
02062         dt[3] = strtoul(s, &end, 10);
02063         if (end == s  ||  *end  ||  dt[3] >= 24)
02064             return false;
02065     }
02066     bool dateSet = false;
02067     if (s != timeStr)
02068     {
02069         dateSet = true;
02070         // Get the day value
02071         if ((s = strrchr(timeStr, '-')) == 0)
02072             s = timeStr;
02073         else
02074             *s++ = 0;
02075         dt[2] = strtoul(s, &end, 10);
02076         if (end == s  ||  *end  ||  dt[2] == 0  ||  dt[2] > 31)
02077             return false;
02078         if (s != timeStr)
02079         {
02080             // Get the month value
02081             if ((s = strrchr(timeStr, '-')) == 0)
02082                 s = timeStr;
02083             else
02084                 *s++ = 0;
02085             dt[1] = strtoul(s, &end, 10);
02086             if (end == s  ||  *end  ||  dt[1] == 0  ||  dt[1] > 12)
02087                 return false;
02088             if (s != timeStr)
02089             {
02090                 // Get the year value
02091                 dt[0] = strtoul(timeStr, &end, 10);
02092                 if (end == timeStr  ||  *end)
02093                     return false;
02094             }
02095         }
02096     }
02097 
02098     QDate date(dt[0], dt[1], dt[2]);
02099     QTime time(0, 0, 0);
02100     if (noTime)
02101     {
02102         // No time was specified, so the full date must have been specified
02103         if (dt[0] < 0)
02104             return false;
02105     }
02106     else
02107     {
02108         // Compile the values into a date/time structure
02109         QDateTime now = QDateTime::currentDateTime();
02110         if (dt[0] < 0)
02111             date.setYMD(now.date().year(),
02112                         (dt[1] < 0 ? now.date().month() : dt[1]),
02113                         (dt[2] < 0 ? now.date().day() : dt[2]));
02114         time.setHMS(dt[3], dt[4], 0);
02115         if (!dateSet  &&  time < now.time())
02116             date = date.addDays(1);
02117     }
02118     if (!date.isValid())
02119         return false;
02120     dateTime.setDate(date);
02121     dateTime.setTime(time);
02122     return true;
02123 }
02124 
02125 /******************************************************************************
02126 *  Convert a time interval command line parameter.
02127 *  Reply = true if successful.
02128 */
02129 static bool convInterval(const QCString& timeParam, KARecurrence::Type& recurType, int& timeInterval, bool allowMonthYear)
02130 {
02131     QCString timeString = timeParam;
02132     // Get the recurrence interval
02133     bool ok = true;
02134     uint interval = 0;
02135     bool negative = (timeString[0] == '-');
02136     if (negative)
02137         timeString = timeString.right(1);
02138     uint length = timeString.length();
02139     switch (timeString[length - 1])
02140     {
02141         case 'Y':
02142             if (!allowMonthYear)
02143                 ok = false;
02144             recurType = KARecurrence::ANNUAL_DATE;
02145             timeString = timeString.left(length - 1);
02146             break;
02147         case 'W':
02148             recurType = KARecurrence::WEEKLY;
02149             timeString = timeString.left(length - 1);
02150             break;
02151         case 'D':
02152             recurType = KARecurrence::DAILY;
02153             timeString = timeString.left(length - 1);
02154             break;
02155         case 'M':
02156         {
02157             int i = timeString.find('H');
02158             if (i < 0)
02159             {
02160                 if (!allowMonthYear)
02161                     ok = false;
02162                 recurType = KARecurrence::MONTHLY_DAY;
02163                 timeString = timeString.left(length - 1);
02164             }
02165             else
02166             {
02167                 recurType = KARecurrence::MINUTELY;
02168                 interval = timeString.left(i).toUInt(&ok) * 60;
02169                 timeString = timeString.mid(i + 1, length - i - 2);
02170             }
02171             break;
02172         }
02173         default:       // should be a digit
02174             recurType = KARecurrence::MINUTELY;
02175             break;
02176     }
02177     if (ok)
02178         interval += timeString.toUInt(&ok);
02179     timeInterval = static_cast<int>(interval);
02180     if (negative)
02181         timeInterval = -timeInterval;
02182     return ok;
02183 }
02184 
02185 KAlarmApp::ProcData::ProcData(ShellProcess* p, ShellProcess* logp, KAEvent* e, KAAlarm* a, int f)
02186     : process(p),
02187       logProcess(logp),
02188       event(e),
02189       alarm(a),
02190       messageBoxParent(0),
02191       flags(f)
02192 { }
02193 
02194 KAlarmApp::ProcData::~ProcData()
02195 {
02196     while (!tempFiles.isEmpty())
02197     {
02198         // Delete the temporary file called by the XTerm command
02199         QFile f(tempFiles.first());
02200         f.remove();
02201         tempFiles.remove(tempFiles.begin());
02202     }
02203     delete process;
02204     delete event;
02205     delete alarm;
02206 }
KDE Home | KDE Accessibility Home | Description of Access Keys