27 #include <tqobjectlist.h>
32 #include <tdecmdlineargs.h>
33 #include <tdelocale.h>
34 #include <kstandarddirs.h>
35 #include <tdeconfig.h>
36 #include <tdeaboutdata.h>
37 #include <dcopclient.h>
38 #include <tdeprocess.h>
39 #include <tdetempfile.h>
40 #include <tdefileitem.h>
41 #include <kstdguiitem.h>
43 #include <kstaticdeleter.h>
46 #include <libkcal/calformat.h>
48 #include <kalarmd/clientinfo.h>
50 #include "alarmcalendar.h"
51 #include "alarmlistview.h"
52 #include "birthdaydlg.h"
55 #include "dcophandler.h"
58 #include "karecurrence.h"
60 #include "messagebox.h"
62 #include "preferences.h"
64 #include "shellprocess.h"
65 #include "traywindow.h"
66 #include "kalarmapp.moc"
71 static bool convWakeTime(
const TQCString& timeParam, TQDateTime&,
bool& noTime);
72 static bool convInterval(
const TQCString& timeParam, KARecurrence::Type&,
int& timeInterval,
bool allowMonthYear =
false);
79 static inline int maxLateness(
int lateCancel)
81 static const int LATENESS_LEEWAY = 5;
82 int lc = (lateCancel >= 1) ? (lateCancel - 1)*60 : 0;
83 return Daemon::maxTimeSinceCheck() + LATENESS_LEEWAY + lc;
87 KAlarmApp* KAlarmApp::theInstance = 0;
88 int KAlarmApp::mActiveCount = 0;
89 int KAlarmApp::mFatalError = 0;
90 TQString KAlarmApp::mFatalMessage;
96 KAlarmApp::KAlarmApp()
97 : TDEUniqueApplication(),
99 mDcopHandler(new DcopHandler()),
101 mDcopHandlerOld(new DcopHandlerOld()),
105 mProcessingQueue(false),
106 mCheckingSystemTray(false),
107 mSessionClosingDown(false),
108 mRefreshExpiredAlarms(false),
109 mSpeechEnabled(false)
111 Preferences::initialise();
112 Preferences::connect(TQ_SIGNAL(preferencesChanged()),
this, TQ_SLOT(slotPreferencesChanged()));
114 KARecurrence::setDefaultFeb29Type(Preferences::defaultFeb29Type());
117 mHaveSystemTray =
true;
119 if (AlarmCalendar::initialiseCalendars())
121 connect(AlarmCalendar::expiredCalendar(), TQ_SIGNAL(purged()), TQ_SLOT(slotExpiredPurged()));
123 TDEConfig* config = kapp->config();
124 config->setGroup(TQString::fromLatin1(
"General"));
125 mNoSystemTray = config->readBoolEntry(TQString::fromLatin1(
"NoSystemTray"),
false);
126 mSavedNoSystemTray = mNoSystemTray;
127 mOldRunInSystemTray = wantRunInSystemTray();
128 mDisableAlarmsIfStopped = mOldRunInSystemTray && !mNoSystemTray && Preferences::disableAlarmsIfStopped();
129 mStartOfDay = Preferences::startOfDay();
130 if (Preferences::hasStartOfDayChanged())
131 mStartOfDay.setHMS(100,0,0);
132 DateTime::setStartOfDay(mStartOfDay);
133 mPrefsExpiredColour = Preferences::expiredColour();
134 mPrefsExpiredKeepDays = Preferences::expiredKeepDays();
138 mSpeechEnabled = (TDETrader::self()->query(
"DCOP/Text-to-Speech",
"Name == 'KTTSD'").count() > 0);
140 kdDebug(5950) <<
"KAlarmApp::KAlarmApp(): speech synthesis disabled (KTTSD not found)" << endl;
142 TQString korg = TQString::fromLatin1(
"korganizer");
143 mKOrganizerEnabled = !locate(
"exe", korg).isNull() || !TDEStandardDirs::findExe(korg).isNull();
144 if (!mKOrganizerEnabled)
145 kdDebug(5950) <<
"KAlarmApp::KAlarmApp(): KOrganizer options disabled (KOrganizer not found)" << endl;
150 KAlarmApp::~KAlarmApp()
152 while (!mCommandProcesses.isEmpty())
154 ProcData* pd = mCommandProcesses.first();
155 mCommandProcesses.pop_front();
158 AlarmCalendar::terminateCalendars();
165 KAlarmApp* KAlarmApp::getInstance()
169 theInstance =
new KAlarmApp;
172 theInstance->quitFatal();
176 Daemon::initialise();
185 bool KAlarmApp::restoreSession()
196 kdDebug(5950) <<
"KAlarmApp::restoreSession(): Restoring\n";
198 if (!initCheck(
true))
204 MainWindow* trayParent = 0;
205 for (
int i = 1; TDEMainWindow::canBeRestored(i); ++i)
207 TQString type = TDEMainWindow::classNameOfToplevel(i);
208 if (type == TQString::fromLatin1(
"MainWindow"))
210 MainWindow* win = MainWindow::create(
true);
211 win->restore(i,
false);
212 if (win->isHiddenTrayParent())
217 else if (type == TQString::fromLatin1(
"MessageWin"))
220 win->restore(i,
false);
231 if (Preferences::autostartTrayIcon()
232 || (MainWindow::count() && wantRunInSystemTray()))
234 displayTrayIcon(
true, trayParent);
250 int KAlarmApp::newInstance()
252 kdDebug(5950)<<
"KAlarmApp::newInstance()\n";
260 static bool firstInstance =
true;
261 bool dontRedisplay =
false;
262 if (!firstInstance || !isRestored())
265 TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
275 #define USAGE(message) { usage = message; break; }
276 if (args->isSet(
"stop"))
279 kdDebug(5950)<<
"KAlarmApp::newInstance(): stop\n";
286 dontRedisplay =
true;
289 if (args->isSet(
"reset"))
293 kdDebug(5950)<<
"KAlarmApp::newInstance(): reset\n";
296 dontRedisplay =
true;
299 if (args->isSet(
"tray"))
302 kdDebug(5950)<<
"KAlarmApp::newInstance(): tray\n";
304 if (!mHaveSystemTray)
314 if (!displayTrayIcon(
true))
321 if (args->isSet(
"handleEvent") || args->isSet(
"triggerEvent") || args->isSet(
"cancelEvent") || args->isSet(
"calendarURL"))
324 kdDebug(5950)<<
"KAlarmApp::newInstance(): handle event\n";
325 EventFunc
function = EVENT_HANDLE;
327 const char* option = 0;
328 if (args->isSet(
"handleEvent")) {
function = EVENT_HANDLE; option =
"handleEvent"; ++count; }
329 if (args->isSet(
"triggerEvent")) {
function = EVENT_TRIGGER; option =
"triggerEvent"; ++count; }
330 if (args->isSet(
"cancelEvent")) {
function = EVENT_CANCEL; option =
"cancelEvent"; ++count; }
332 USAGE(i18n(
"%1 requires %2, %3 or %4").arg(TQString::fromLatin1(
"--calendarURL")).arg(TQString::fromLatin1(
"--handleEvent")).arg(TQString::fromLatin1(
"--triggerEvent")).arg(TQString::fromLatin1(
"--cancelEvent")))
334 USAGE(i18n("%1, %2, %3 mutually exclusive").arg(TQString::fromLatin1("--handleEvent")).arg(TQString::fromLatin1("--triggerEvent")).arg(TQString::fromLatin1("--cancelEvent")));
335 if (!initCheck(true))
340 if (args->isSet(
"calendarURL"))
342 TQString calendarUrl = args->getOption(
"calendarURL");
343 if (KURL(calendarUrl).url() != AlarmCalendar::activeCalendar()->urlString())
344 USAGE(i18n(
"%1: wrong calendar file").arg(TQString::fromLatin1(
"--calendarURL")))
346 TQString eventID = args->getOption(option);
348 if (eventID.startsWith(TQString::fromLatin1(
"ad:")))
351 eventID = eventID.mid(3);
352 Daemon::queueEvent(eventID);
355 if (!handleEvent(eventID,
function))
362 if (args->isSet(
"edit"))
364 TQString eventID = args->getOption(
"edit");
370 if (!KAlarm::edit(eventID))
372 USAGE(i18n(
"%1: Event %2 not found, or not editable").arg(TQString::fromLatin1(
"--edit")).arg(eventID))
378 if (args->isSet(
"edit-new") || args->isSet(
"edit-new-preset"))
381 if (args->isSet(
"edit-new-preset"))
382 templ = args->getOption(
"edit-new-preset");
388 KAlarm::editNew(templ);
391 if (args->isSet(
"file") || args->isSet(
"exec") || args->isSet(
"mail") || args->count())
394 KAEvent::Action action = KAEvent::MESSAGE;
397 EmailAddressList alAddresses;
398 TQStringList alAttachments;
400 if (args->isSet(
"file"))
402 kdDebug(5950)<<
"KAlarmApp::newInstance(): file\n";
403 if (args->isSet(
"exec"))
404 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--exec")).arg(TQString::fromLatin1(
"--file")))
405 if (args->isSet("mail"))
406 USAGE(i18n("%1 incompatible with %2").arg(TQString::fromLatin1("--mail")).arg(TQString::fromLatin1("--file")))
408 USAGE(i18n("message incompatible with %1").arg(TQString::fromLatin1("--file")))
409 alMessage = args->getOption("file");
412 else if (args->isSet("exec"))
414 kdDebug(5950)<<
"KAlarmApp::newInstance(): exec\n";
415 if (args->isSet(
"mail"))
416 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--mail")).arg(TQString::fromLatin1(
"--exec")))
417 alMessage = args->getOption("exec");
418 int n = args->count();
419 for (
int i = 0; i < n; ++i)
422 alMessage += args->arg(i);
424 action = KAEvent::COMMAND;
426 else if (args->isSet(
"mail"))
428 kdDebug(5950)<<
"KAlarmApp::newInstance(): mail\n";
429 if (args->isSet(
"subject"))
430 alSubject = args->getOption(
"subject");
431 if (args->isSet(
"from-id"))
432 alFromID = KAMail::identityUoid(args->getOption(
"from-id"));
433 QCStringList params = args->getOptionList(
"mail");
434 for (QCStringList::Iterator i = params.begin(); i != params.end(); ++i)
436 TQString addr = TQString::fromLocal8Bit(*i);
437 if (!KAMail::checkAddress(addr))
438 USAGE(i18n(
"%1: invalid email address").arg(TQString::fromLatin1(
"--mail")))
439 alAddresses += KCal::Person(TQString(), addr);
441 params = args->getOptionList("attach");
442 for (QCStringList::Iterator i = params.begin(); i != params.end(); ++i)
443 alAttachments += TQString::fromLocal8Bit(*i);
444 alMessage = args->arg(0);
449 kdDebug(5950)<<
"KAlarmApp::newInstance(): message\n";
450 alMessage = args->arg(0);
453 if (action != KAEvent::EMAIL)
455 if (args->isSet(
"subject"))
456 USAGE(i18n(
"%1 requires %2").arg(TQString::fromLatin1(
"--subject")).arg(TQString::fromLatin1(
"--mail")))
457 if (args->isSet("from-
id"))
458 USAGE(i18n("%1 requires %2").arg(TQString::fromLatin1("--from-
id")).arg(TQString::fromLatin1("--mail")))
459 if (args->isSet("attach"))
460 USAGE(i18n("%1 requires %2").arg(TQString::fromLatin1("--attach")).arg(TQString::fromLatin1("--mail")))
461 if (args->isSet("bcc"))
462 USAGE(i18n("%1 requires %2").arg(TQString::fromLatin1("--bcc")).arg(TQString::fromLatin1("--mail")))
465 bool alarmNoTime = false;
466 TQDateTime alarmTime, endTime;
467 TQColor bgColour = Preferences::defaultBgColour();
468 TQColor fgColour = Preferences::defaultFgColour();
469 KARecurrence recurrence;
471 int repeatInterval = 0;
472 if (args->isSet("color"))
475 TQCString colourText = args->getOption(
"color");
476 if (
static_cast<const char*
>(colourText)[0] ==
'0'
477 && tolower(
static_cast<const char*
>(colourText)[1]) ==
'x')
478 colourText.replace(0, 2,
"#");
479 bgColour.setNamedColor(colourText);
480 if (!bgColour.isValid())
481 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--color")))
483 if (args->isSet(
"colorfg"))
486 TQCString colourText = args->getOption(
"colorfg");
487 if (
static_cast<const char*
>(colourText)[0] ==
'0'
488 && tolower(
static_cast<const char*
>(colourText)[1]) ==
'x')
489 colourText.replace(0, 2,
"#");
490 fgColour.setNamedColor(colourText);
491 if (!fgColour.isValid())
492 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--colorfg")))
495 if (args->isSet(
"time"))
497 TQCString dateTime = args->getOption(
"time");
498 if (!convWakeTime(dateTime, alarmTime, alarmNoTime))
499 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--time")))
502 alarmTime = TQDateTime::currentDateTime();
504 bool haveRecurrence = args->isSet(
"recurrence");
507 if (args->isSet(
"login"))
508 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--login")).arg(TQString::fromLatin1(
"--recurrence")))
509 if (args->isSet("until"))
510 USAGE(i18n("%1 incompatible with %2").arg(TQString::fromLatin1("--until")).arg(TQString::fromLatin1("--recurrence")))
511 TQCString rule = args->getOption("recurrence");
512 recurrence.set(TQString::fromLocal8Bit(static_cast<const
char*>(rule)));
514 if (args->isSet("interval"))
518 if (args->isSet(
"login"))
519 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--login")).arg(TQString::fromLatin1(
"--interval")))
521 if (args->isSet("repeat"))
523 count = args->getOption(
"repeat").toInt(&ok);
524 if (!ok || !count || count < -1 || (count < 0 && haveRecurrence))
525 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--repeat")))
527 else if (haveRecurrence)
528 USAGE(i18n(
"%1 requires %2").arg(TQString::fromLatin1(
"--interval")).arg(TQString::fromLatin1(
"--repeat")))
529 else if (args->isSet("until"))
532 TQCString dateTime = args->getOption(
"until");
533 if (!convWakeTime(dateTime, endTime, alarmNoTime))
534 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--until")))
535 if (endTime < alarmTime)
536 USAGE(i18n("%1 earlier than %2").arg(TQString::fromLatin1("--until")).arg(TQString::fromLatin1("--time")))
543 KARecurrence::Type recurType;
544 if (!convInterval(args->getOption("interval"), recurType, interval, !haveRecurrence)
546 USAGE(i18n("Invalid %1 parameter").arg(TQString::fromLatin1("--interval")))
547 if (alarmNoTime && recurType == KARecurrence::MINUTELY)
548 USAGE(i18n("Invalid %1 parameter for date-only alarm").arg(TQString::fromLatin1("--interval")))
553 int longestInterval = recurrence.longestInterval();
554 if (count * interval > longestInterval)
555 USAGE(i18n(
"Invalid %1 and %2 parameters: repetition is longer than %3 interval").arg(TQString::fromLatin1(
"--interval")).arg(TQString::fromLatin1(
"--repeat")).arg(TQString::fromLatin1(
"--recurrence")));
557 repeatInterval = interval;
563 recurrence.set(recurType, interval, count, DateTime(alarmTime, alarmNoTime), endTime);
568 if (args->isSet(
"repeat"))
569 USAGE(i18n(
"%1 requires %2").arg(TQString::fromLatin1(
"--repeat")).arg(TQString::fromLatin1(
"--interval")))
570 if (args->isSet("until"))
571 USAGE(i18n("%1 requires %2").arg(TQString::fromLatin1("--until")).arg(TQString::fromLatin1("--interval")))
575 float audioVolume = -1;
577 bool audioRepeat =
false;
579 bool audioRepeat = args->isSet(
"play-repeat");
581 if (audioRepeat || args->isSet(
"play"))
584 if (audioRepeat && args->isSet(
"play"))
585 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--play")).arg(TQString::fromLatin1(
"--play-repeat")))
586 if (args->isSet("beep"))
587 USAGE(i18n("%1 incompatible with %2").arg(TQString::fromLatin1("--beep")).arg(TQString::fromLatin1(audioRepeat ? "--play-repeat" : "--play")))
588 if (args->isSet("speak"))
589 USAGE(i18n("%1 incompatible with %2").arg(TQString::fromLatin1("--speak")).arg(TQString::fromLatin1(audioRepeat ? "--play-repeat" : "--play")))
590 audioFile = args->getOption(audioRepeat ? "play-repeat" : "play");
592 if (args->isSet(
"volume"))
595 int volumepc = args->getOption(
"volume").toInt(&ok);
596 if (!ok || volumepc < 0 || volumepc > 100)
597 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"--volume")))
598 audioVolume = static_cast<
float>(volumepc) / 100;
603 else if (args->isSet(
"volume"))
604 USAGE(i18n(
"%1 requires %2 or %3").arg(TQString::fromLatin1(
"--volume")).arg(TQString::fromLatin1(
"--play")).arg(TQString::fromLatin1(
"--play-repeat")))
606 if (args->isSet(
"speak"))
608 if (args->isSet(
"beep"))
609 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--beep")).arg(TQString::fromLatin1(
"--speak")))
611 USAGE(i18n("%1 requires speech synthesis to be configured using KTTSD").arg(TQString::fromLatin1("--speak")))
613 int reminderMinutes = 0;
614 bool onceOnly = args->isSet("reminder-once");
615 if (args->isSet("reminder") || onceOnly)
618 if (onceOnly && args->isSet(
"reminder"))
619 USAGE(i18n(
"%1 incompatible with %2").arg(TQString::fromLatin1(
"--reminder")).arg(TQString::fromLatin1(
"--reminder-once")))
620 TQString opt = onceOnly ? TQString::fromLatin1("--reminder-once") : TQString::fromLatin1("--reminder");
621 if (args->isSet("exec"))
622 USAGE(i18n("%1 incompatible with %2").arg(opt).arg(TQString::fromLatin1("--exec")))
623 if (args->isSet("mail"))
624 USAGE(i18n("%1 incompatible with %2").arg(opt).arg(TQString::fromLatin1("--mail")))
625 KARecurrence::Type recurType;
626 TQString optval = args->getOption(onceOnly ? "reminder-once" : "reminder");
627 if (!convInterval(args->getOption(onceOnly ? "reminder-once" : "reminder"), recurType, reminderMinutes))
628 USAGE(i18n("Invalid %1 parameter").arg(opt))
629 if (recurType == KARecurrence::MINUTELY && alarmNoTime)
630 USAGE(i18n("Invalid %1 parameter for date-only alarm").arg(opt))
634 if (args->isSet("late-cancel"))
636 KARecurrence::Type recurType;
637 bool ok = convInterval(args->getOption(
"late-cancel"), recurType, lateCancel);
638 if (!ok || lateCancel <= 0)
639 USAGE(i18n(
"Invalid %1 parameter").arg(TQString::fromLatin1(
"late-cancel")))
641 else if (args->isSet(
"auto-close"))
642 USAGE(i18n(
"%1 requires %2").arg(TQString::fromLatin1(
"--auto-close")).arg(TQString::fromLatin1(
"--late-cancel")))
644 int flags =
KAEvent::DEFAULT_FONT;
645 if (args->isSet("ack-confirm"))
647 if (args->isSet("auto-close"))
649 if (args->isSet("beep"))
651 if (args->isSet("speak"))
653 if (args->isSet("korganizer"))
654 flags |=
KAEvent::COPY_KORGANIZER;
655 if (args->isSet("disable"))
658 flags |=
KAEvent::REPEAT_SOUND;
659 if (args->isSet("login"))
660 flags |=
KAEvent::REPEAT_AT_LOGIN;
661 if (args->isSet("bcc"))
673 if (!scheduleEvent(action, alMessage, alarmTime, lateCancel, flags, bgColour, fgColour, TQFont(), audioFile,
674 audioVolume, reminderMinutes, recurrence, repeatInterval, repeatCount,
675 alFromID, alAddresses, alSubject, alAttachments))
684 kdDebug(5950)<<
"KAlarmApp::newInstance(): interactive\n";
685 if (args->isSet(
"ack-confirm"))
686 usage += TQString::fromLatin1(
"--ack-confirm ");
687 if (args->isSet(
"attach"))
688 usage += TQString::fromLatin1(
"--attach ");
689 if (args->isSet(
"auto-close"))
690 usage += TQString::fromLatin1(
"--auto-close ");
691 if (args->isSet(
"bcc"))
692 usage += TQString::fromLatin1(
"--bcc ");
693 if (args->isSet(
"beep"))
694 usage += TQString::fromLatin1(
"--beep ");
695 if (args->isSet(
"color"))
696 usage += TQString::fromLatin1(
"--color ");
697 if (args->isSet(
"colorfg"))
698 usage += TQString::fromLatin1(
"--colorfg ");
699 if (args->isSet(
"disable"))
700 usage += TQString::fromLatin1(
"--disable ");
701 if (args->isSet(
"from-id"))
702 usage += TQString::fromLatin1(
"--from-id ");
703 if (args->isSet(
"korganizer"))
704 usage += TQString::fromLatin1(
"--korganizer ");
705 if (args->isSet(
"late-cancel"))
706 usage += TQString::fromLatin1(
"--late-cancel ");
707 if (args->isSet(
"login"))
708 usage += TQString::fromLatin1(
"--login ");
709 if (args->isSet(
"play"))
710 usage += TQString::fromLatin1(
"--play ");
712 if (args->isSet(
"play-repeat"))
713 usage += TQString::fromLatin1(
"--play-repeat ");
715 if (args->isSet(
"reminder"))
716 usage += TQString::fromLatin1(
"--reminder ");
717 if (args->isSet(
"reminder-once"))
718 usage += TQString::fromLatin1(
"--reminder-once ");
719 if (args->isSet(
"speak"))
720 usage += TQString::fromLatin1(
"--speak ");
721 if (args->isSet(
"subject"))
722 usage += TQString::fromLatin1(
"--subject ");
723 if (args->isSet(
"time"))
724 usage += TQString::fromLatin1(
"--time ");
726 if (args->isSet(
"volume"))
727 usage += TQString::fromLatin1(
"--volume ");
729 if (!usage.isEmpty())
731 usage += i18n(
": option(s) only valid with a message/%1/%2").arg(TQString::fromLatin1(
"--file")).arg(TQString::fromLatin1(
"--exec"));
742 (MainWindow::create())->show();
746 if (!usage.isEmpty())
750 std::cerr << usage.local8Bit().data()
751 << i18n(
"\nUse --help to get a list of available command line options.\n").local8Bit().data();
755 if (firstInstance && !dontRedisplay && !exitCode)
759 firstInstance =
false;
771 void KAlarmApp::quitIf(
int exitCode,
bool force)
776 MainWindow::closeAll();
777 displayTrayIcon(
false);
778 if (MessageWin::instanceCount())
784 mPendingQuit =
false;
785 if (mActiveCount > 0 || MessageWin::instanceCount())
787 int mwcount = MainWindow::count();
788 MainWindow* mw = mwcount ? MainWindow::firstWindow() : 0;
789 if (mwcount > 1 || (mwcount && (!mw->isHidden() || !mw->isTrayParent())))
796 if (checkSystemTray())
799 if (!mDcopQueue.isEmpty() || !mCommandProcesses.isEmpty())
803 mPendingQuitCode = exitCode;
809 kdDebug(5950) <<
"KAlarmApp::quitIf(" << exitCode <<
"): quitting" << endl;
810 BirthdayDlg::close();
819 void KAlarmApp::doQuit(TQWidget* parent)
821 kdDebug(5950) <<
"KAlarmApp::doQuit()\n";
822 if (mDisableAlarmsIfStopped
823 && MessageBox::warningContinueCancel(parent, KMessageBox::Cancel,
824 i18n(
"Quitting will disable alarms\n(once any alarm message windows are closed)."),
825 TQString(), KStdGuiItem::quit(), Preferences::QUIT_WARN
826 ) != KMessageBox::Yes)
834 void KAlarmApp::commitData(TQSessionManager& sm)
836 mSessionClosingDown =
true;
837 TDEUniqueApplication::commitData(sm);
838 mSessionClosingDown =
false;
845 void KAlarmApp::displayFatalError(
const TQString& message)
850 mFatalMessage = message;
852 TQTimer::singleShot(0, theInstance, TQ_SLOT(quitFatal()));
859 void KAlarmApp::quitFatal()
868 KMessageBox::error(0, mFatalMessage);
873 theInstance->quitIf(1,
true);
876 TQTimer::singleShot(1000,
this, TQ_SLOT(quitFatal()));
891 void KAlarmApp::processQueue()
893 if (mInitialised && !mProcessingQueue)
895 kdDebug(5950) <<
"KAlarmApp::processQueue()\n";
896 mProcessingQueue =
true;
899 KAlarm::resetDaemonIfQueued();
902 while (!mDcopQueue.isEmpty())
904 DcopTQEntry& entry = mDcopQueue.first();
905 if (entry.eventId.isEmpty())
908 switch (entry.function)
911 execAlarm(entry.event, entry.event.firstAlarm(),
false);
914 KAlarm::addEvent(entry.event, 0);
921 handleEvent(entry.eventId, entry.function);
922 mDcopQueue.pop_front();
926 AlarmCalendar::expiredCalendar()->purgeIfQueued();
930 quitIf(mPendingQuitCode);
932 mProcessingQueue =
false;
942 void KAlarmApp::redisplayAlarms()
947 KCal::Event::List events = cal->events();
948 for (KCal::Event::List::ConstIterator it = events.begin(); it != events.end(); ++it)
952 event.setUid(KAEvent::ACTIVE);
953 if (!MessageWin::findEvent(event.id()))
956 kdDebug(5950) <<
"KAlarmApp::redisplayAlarms(): " <<
event.id() << endl;
957 KAAlarm alarm =
event.convertDisplayingAlarm();
958 (
new MessageWin(event, alarm,
false, !alarm.repeatAtLogin()))->show();
967 void KAlarmApp::removeWindow(TrayWindow*)
976 bool KAlarmApp::displayTrayIcon(
bool show, MainWindow* parent)
978 static bool creating =
false;
981 if (!mTrayWindow && !creating)
983 if (!mHaveSystemTray)
985 if (!MainWindow::count() && wantRunInSystemTray())
988 parent = MainWindow::create();
991 mTrayWindow =
new TrayWindow(parent ? parent : MainWindow::firstWindow());
992 connect(mTrayWindow, TQ_SIGNAL(deleted()), TQ_SIGNAL(trayIconToggled()));
994 emit trayIconToggled();
998 mCheckingSystemTray =
true;
999 mSavedNoSystemTray = mNoSystemTray;
1000 mNoSystemTray =
false;
1001 TQTimer::singleShot(0,
this, TQ_SLOT(slotSystemTrayTimer()));
1004 else if (mTrayWindow)
1019 void KAlarmApp::slotSystemTrayTimer()
1021 mCheckingSystemTray =
false;
1022 if (!checkSystemTray())
1031 bool KAlarmApp::checkSystemTray()
1033 if (mCheckingSystemTray || !mTrayWindow)
1035 if (mTrayWindow->inSystemTray() != !mSavedNoSystemTray)
1037 kdDebug(5950) <<
"KAlarmApp::checkSystemTray(): changed -> " << mSavedNoSystemTray << endl;
1038 mNoSystemTray = mSavedNoSystemTray = !mSavedNoSystemTray;
1045 TDEConfig* config = kapp->config();
1046 config->setGroup(TQString::fromLatin1(
"General"));
1047 config->writeEntry(TQString::fromLatin1(
"NoSystemTray"), mNoSystemTray);
1051 slotPreferencesChanged();
1055 kdDebug(5950) <<
"KAlarmApp::checkSystemTray(): no change = " << !mSavedNoSystemTray << endl;
1056 mNoSystemTray = mSavedNoSystemTray;
1058 return !mNoSystemTray;
1064 MainWindow* KAlarmApp::trayMainWindow()
const
1066 return mTrayWindow ? mTrayWindow->assocMainWindow() : 0;
1072 void KAlarmApp::slotPreferencesChanged()
1074 bool newRunInSysTray = wantRunInSystemTray();
1075 if (newRunInSysTray != mOldRunInSystemTray)
1079 MainWindow* win = mTrayWindow ? mTrayWindow->assocMainWindow() : 0;
1082 mOldRunInSystemTray = newRunInSysTray;
1083 if (!newRunInSysTray)
1085 if (win && win->isHidden())
1088 displayTrayIcon(
true);
1092 bool newDisableIfStopped = wantRunInSystemTray() && !mNoSystemTray && Preferences::disableAlarmsIfStopped();
1093 if (newDisableIfStopped != mDisableAlarmsIfStopped)
1095 mDisableAlarmsIfStopped = newDisableIfStopped;
1096 Preferences::setQuitWarn(
true);
1097 Daemon::reregister();
1101 if (Preferences::startOfDay() != mStartOfDay)
1105 KARecurrence::setDefaultFeb29Type(Preferences::defaultFeb29Type());
1107 if (Preferences::expiredColour() != mPrefsExpiredColour)
1110 mRefreshExpiredAlarms =
true;
1111 mPrefsExpiredColour = Preferences::expiredColour();
1114 if (Preferences::expiredKeepDays() != mPrefsExpiredKeepDays)
1118 mPrefsExpiredKeepDays = Preferences::expiredKeepDays();
1119 AlarmCalendar::expiredCalendar()->setPurgeDays(mPrefsExpiredKeepDays);
1122 if (mRefreshExpiredAlarms)
1124 mRefreshExpiredAlarms =
false;
1125 MainWindow::updateExpired();
1132 void KAlarmApp::changeStartOfDay()
1134 Daemon::notifyTimeChanged();
1135 TQTime sod = Preferences::startOfDay();
1136 DateTime::setStartOfDay(sod);
1138 if (KAEvent::adjustStartOfDay(cal->events()))
1140 Preferences::updateStartOfDayCheck();
1148 void KAlarmApp::slotExpiredPurged()
1150 mRefreshExpiredAlarms =
false;
1151 MainWindow::updateExpired();
1157 bool KAlarmApp::wantRunInSystemTray()
const
1159 return Preferences::runInSystemTray() && mHaveSystemTray;
1167 bool KAlarmApp::scheduleEvent(KAEvent::Action action,
const TQString& text,
const TQDateTime& dateTime,
1168 int lateCancel,
int flags,
const TQColor& bg,
const TQColor& fg,
const TQFont& font,
1169 const TQString& audioFile,
float audioVolume,
int reminderMinutes,
1170 const KARecurrence& recurrence,
int repeatInterval,
int repeatCount,
1171 uint mailFromID,
const EmailAddressList& mailAddresses,
1172 const TQString& mailSubject,
const TQStringList& mailAttachments)
1174 kdDebug(5950) <<
"KAlarmApp::scheduleEvent(): " << text << endl;
1175 if (!dateTime.isValid())
1177 TQDateTime now = TQDateTime::currentDateTime();
1178 if (lateCancel && dateTime < now.addSecs(-maxLateness(lateCancel)))
1180 TQDateTime alarmTime = dateTime;
1182 alarmTime.setTime(TQTime(alarmTime.time().hour(), alarmTime.time().minute(), 0));
1184 KAEvent event(alarmTime, text, bg, fg, font, action, lateCancel, flags);
1185 if (reminderMinutes)
1187 bool onceOnly = (reminderMinutes < 0);
1188 event.setReminder((onceOnly ? -reminderMinutes : reminderMinutes), onceOnly);
1190 if (!audioFile.isEmpty())
1191 event.setAudioFile(audioFile, audioVolume, -1, 0);
1192 if (!mailAddresses.isEmpty())
1193 event.setEmail(mailFromID, mailAddresses, mailSubject, mailAttachments);
1194 event.setRecurrence(recurrence);
1195 event.setFirstRecurrence();
1196 event.setRepetition(repeatInterval, repeatCount - 1);
1197 if (alarmTime <= now)
1202 mDcopQueue.append(DcopTQEntry(event, EVENT_TRIGGER));
1204 execAlarm(event, event.firstAlarm(),
false);
1207 || event.setNextOccurrence(now) == KAEvent::NO_OCCURRENCE)
1213 mDcopQueue.append(DcopTQEntry(event));
1215 TQTimer::singleShot(0,
this, TQ_SLOT(processQueue()));
1225 bool KAlarmApp::handleEvent(
const TQString& urlString,
const TQString& eventID, EventFunc
function)
1227 kdDebug(5950) <<
"KAlarmApp::handleEvent(DCOP): " << eventID << endl;
1229 if (cal && KURL(urlString).url() != cal->urlString())
1231 kdError(5950) <<
"KAlarmApp::handleEvent(DCOP): wrong calendar file " << urlString << endl;
1232 Daemon::eventHandled(eventID,
false);
1235 mDcopQueue.append(DcopTQEntry(
function, eventID));
1237 TQTimer::singleShot(0,
this, TQ_SLOT(processQueue()));
1249 bool KAlarmApp::handleEvent(
const TQString& eventID, EventFunc
function)
1251 kdDebug(5950) <<
"KAlarmApp::handleEvent(): " << eventID <<
", " << (
function==EVENT_TRIGGER?
"TRIGGER":
function==EVENT_CANCEL?
"CANCEL":
function==EVENT_HANDLE?
"HANDLE":
"?") << endl;
1252 KCal::Event* kcalEvent = AlarmCalendar::activeCalendar()->event(eventID);
1255 kdError(5950) <<
"KAlarmApp::handleEvent(): event ID not found: " << eventID << endl;
1256 Daemon::eventHandled(eventID,
false);
1263 KAlarm::deleteEvent(event,
true);
1269 TQDateTime now = TQDateTime::currentDateTime();
1270 bool updateCalAndDisplay =
false;
1271 bool alarmToExecuteValid =
false;
1272 KAAlarm alarmToExecute;
1275 for (KAAlarm alarm = event.firstAlarm(); alarm.valid(); alarm = event.nextAlarm(alarm))
1278 int secs = alarm.dateTime(
true).dateTime().secsTo(now);
1282 kdDebug(5950) <<
"KAlarmApp::handleEvent(): alarm " << alarm.type() <<
": not due\n";
1285 if (alarm.repeatAtLogin())
1291 kdDebug(5950) <<
"KAlarmApp::handleEvent(): REPEAT_AT_LOGIN\n";
1292 if (secs < maxLateness(1))
1297 if (alarmToExecute.valid())
1303 if (alarm.lateCancel())
1306 kdDebug(5950) <<
"KAlarmApp::handleEvent(): LATE_CANCEL\n";
1308 bool cancel =
false;
1309 if (alarm.dateTime().isDateOnly())
1312 int maxlate = alarm.lateCancel() / 1440;
1313 TQDateTime limit(alarm.date().addDays(maxlate + 1), Preferences::startOfDay());
1319 KAEvent::OccurType type =
event.previousOccurrence(now, next,
true);
1320 switch (type & ~KAEvent::OCCURRENCE_REPEAT)
1322 case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
1323 case KAEvent::RECURRENCE_DATE:
1324 case KAEvent::RECURRENCE_DATE_TIME:
1325 case KAEvent::LAST_RECURRENCE:
1326 limit.setDate(next.date().addDays(maxlate + 1));
1327 limit.setTime(Preferences::startOfDay());
1330 if (type == KAEvent::LAST_RECURRENCE
1331 || (type == KAEvent::FIRST_OR_ONLY_OCCURRENCE && !event.recurs()))
1337 case KAEvent::NO_OCCURRENCE:
1347 int maxlate = maxLateness(alarm.lateCancel());
1353 KAEvent::OccurType type =
event.previousOccurrence(now, next,
true);
1354 switch (type & ~KAEvent::OCCURRENCE_REPEAT)
1356 case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
1357 case KAEvent::RECURRENCE_DATE:
1358 case KAEvent::RECURRENCE_DATE_TIME:
1359 case KAEvent::LAST_RECURRENCE:
1360 if (next.dateTime().secsTo(now) > maxlate)
1362 if (type == KAEvent::LAST_RECURRENCE
1363 || (type == KAEvent::FIRST_OR_ONLY_OCCURRENCE && !event.recurs()))
1369 case KAEvent::NO_OCCURRENCE:
1381 cancelAlarm(event, alarm.type(),
false);
1382 updateCalAndDisplay =
true;
1388 rescheduleAlarm(event, alarm,
false);
1389 updateCalAndDisplay =
true;
1393 if (!alarmToExecuteValid)
1395 kdDebug(5950) <<
"KAlarmApp::handleEvent(): alarm " << alarm.type() <<
": execute\n";
1396 alarmToExecute = alarm;
1397 alarmToExecuteValid =
true;
1400 kdDebug(5950) <<
"KAlarmApp::handleEvent(): alarm " << alarm.type() <<
": skip\n";
1405 if (alarmToExecute.valid())
1406 execAlarm(event, alarmToExecute,
true, !alarmToExecute.repeatAtLogin());
1409 if (
function == EVENT_TRIGGER)
1414 KAAlarm alarm =
event.firstAlarm();
1416 execAlarm(event, alarm,
false);
1418 if (updateCalAndDisplay)
1419 KAlarm::updateEvent(event, 0);
1420 else if (
function != EVENT_TRIGGER)
1422 kdDebug(5950) <<
"KAlarmApp::handleEvent(): no action\n";
1423 Daemon::eventHandled(eventID,
false);
1437 void KAlarmApp::alarmShowing(
KAEvent& event, KAAlarm::Type alarmType,
const DateTime& alarmTime)
1439 kdDebug(5950) <<
"KAlarmApp::alarmShowing(" <<
event.id() <<
", " << KAAlarm::debugType(alarmType) <<
")\n";
1440 KCal::Event* kcalEvent = AlarmCalendar::activeCalendar()->event(event.id());
1442 kdError(5950) <<
"KAlarmApp::alarmShowing(): event ID not found: " <<
event.id() << endl;
1445 KAAlarm alarm =
event.alarm(alarmType);
1447 kdError(5950) <<
"KAlarmApp::alarmShowing(): alarm type not found: " <<
event.id() <<
":" << alarmType << endl;
1452 dispEvent.setDisplaying(event, alarmType, alarmTime.dateTime());
1456 cal->deleteEvent(dispEvent.id());
1457 cal->addEvent(dispEvent);
1461 rescheduleAlarm(event, alarm,
true);
1465 Daemon::eventHandled(event.id(),
false);
1471 void KAlarmApp::alarmCompleted(
const KAEvent& event)
1473 if (!event.postAction().isEmpty() && ShellProcess::authorised())
1475 TQString command =
event.postAction();
1476 kdDebug(5950) <<
"KAlarmApp::alarmCompleted(" <<
event.id() <<
"): " << command << endl;
1477 doShellCommand(command, event, 0, ProcData::POST_ACTION);
1486 void KAlarmApp::rescheduleAlarm(
KAEvent& event,
const KAAlarm& alarm,
bool updateCalAndDisplay)
1488 kdDebug(5950) <<
"KAlarmApp::rescheduleAlarm()" << endl;
1489 bool update =
false;
1490 if (alarm.reminder() || alarm.deferred())
1493 event.removeExpiredAlarm(alarm.type());
1496 else if (alarm.repeatAtLogin())
1499 if (updateCalAndDisplay && event.updated())
1505 KAEvent::OccurType type =
event.setNextOccurrence(TQDateTime::currentDateTime());
1508 case KAEvent::NO_OCCURRENCE:
1510 cancelAlarm(event, alarm.type(), updateCalAndDisplay);
1513 if (!(type & KAEvent::OCCURRENCE_REPEAT))
1516 case KAEvent::RECURRENCE_DATE:
1517 case KAEvent::RECURRENCE_DATE_TIME:
1518 case KAEvent::LAST_RECURRENCE:
1520 if (updateCalAndDisplay)
1524 event.cancelCancelledDeferral();
1528 case KAEvent::FIRST_OR_ONLY_OCCURRENCE:
1532 if (event.deferred())
1535 event.removeExpiredAlarm(KAAlarm::DEFERRED_ALARM);
1541 event.cancelCancelledDeferral();
1542 KAlarm::updateEvent(event, 0);
1550 void KAlarmApp::cancelAlarm(
KAEvent& event, KAAlarm::Type alarmType,
bool updateCalAndDisplay)
1552 kdDebug(5950) <<
"KAlarmApp::cancelAlarm()" << endl;
1553 event.cancelCancelledDeferral();
1554 if (alarmType == KAAlarm::MAIN_ALARM && !event.displaying() && event.toBeArchived())
1557 TQString
id =
event.id();
1558 KAlarm::addExpiredEvent(event);
1559 event.setEventID(
id);
1561 event.removeExpiredAlarm(alarmType);
1562 if (!event.alarmCount())
1563 KAlarm::deleteEvent(event,
false);
1564 else if (updateCalAndDisplay)
1565 KAlarm::updateEvent(event, 0);
1574 void* KAlarmApp::execAlarm(
KAEvent& event,
const KAAlarm& alarm,
bool reschedule,
bool allowDefer,
bool noPreAction)
1576 if (!event.enabled())
1580 rescheduleAlarm(event, alarm,
true);
1584 void* result = (
void*)1;
1586 switch (alarm.action())
1588 case KAAlarm::MESSAGE:
1592 MessageWin* win = MessageWin::findEvent(event.id());
1594 bool reminder = (alarm.type() & KAAlarm::REMINDER_ALARM);
1595 bool replaceReminder = !reminder && win && (win->alarmType() & KAAlarm::REMINDER_ALARM);
1596 if (!reminder && !event.deferred()
1597 && (replaceReminder || !win) && !noPreAction
1598 && !event.preAction().isEmpty() && ShellProcess::authorised())
1604 for (TQValueList<ProcData*>::Iterator it = mCommandProcesses.begin(); it != mCommandProcesses.end(); ++it)
1607 if (pd->event->id() ==
event.id() && (pd->flags & ProcData::PRE_ACTION))
1609 kdDebug(5950) <<
"KAlarmApp::execAlarm(): already executing pre-DISPLAY command" << endl;
1614 TQString command =
event.preAction();
1615 kdDebug(5950) <<
"KAlarmApp::execAlarm(): pre-DISPLAY command: " << command << endl;
1616 int flags = (reschedule ? ProcData::RESCHEDULE : 0) | (allowDefer ? ProcData::ALLOW_DEFER : 0);
1617 if (doShellCommand(command, event, &alarm, (flags | ProcData::PRE_ACTION)))
1621 if (!event.enabled())
1624 || (!win->hasDefer() && !alarm.repeatAtLogin())
1632 win->setRecreating();
1634 (
new MessageWin(event, alarm, reschedule, allowDefer))->show();
1643 case KAAlarm::COMMAND:
1645 int flags =
event.commandXterm() ? ProcData::EXEC_IN_XTERM : 0;
1646 TQString command =
event.cleanText();
1647 if (event.commandScript())
1650 kdDebug(5950) <<
"KAlarmApp::execAlarm(): COMMAND: (script)" << endl;
1651 TQString tmpfile = createTempScriptFile(command,
false, event, alarm);
1652 if (tmpfile.isEmpty())
1655 result = doShellCommand(tmpfile, event, &alarm, (flags | ProcData::TEMP_FILE));
1659 kdDebug(5950) <<
"KAlarmApp::execAlarm(): COMMAND: " << command << endl;
1660 result = doShellCommand(command, event, &alarm, flags);
1663 rescheduleAlarm(event, alarm,
true);
1666 case KAAlarm::EMAIL:
1668 kdDebug(5950) <<
"KAlarmApp::execAlarm(): EMAIL to: " <<
event.emailAddresses(
", ") << endl;
1669 TQStringList errmsgs;
1670 if (!KAMail::send(event, errmsgs, (reschedule || allowDefer)))
1672 if (!errmsgs.isEmpty())
1676 kdDebug(5950) <<
"KAlarmApp::execAlarm(): copy error: " << errmsgs[1] << endl;
1678 kdDebug(5950) <<
"KAlarmApp::execAlarm(): failed: " << errmsgs[1] << endl;
1679 (
new MessageWin(event, alarm.dateTime(), errmsgs))->show();
1682 rescheduleAlarm(event, alarm,
true);
1697 ShellProcess* KAlarmApp::doShellCommand(
const TQString& command,
const KAEvent& event,
const KAAlarm* alarm,
int flags)
1699 kdDebug(5950) <<
"KAlarmApp::doShellCommand(" << command <<
", " <<
event.id() <<
")" << endl;
1700 TDEProcess::Communication comms = TDEProcess::NoCommunication;
1702 TQString tmpXtermFile;
1703 if (flags & ProcData::EXEC_IN_XTERM)
1706 cmd = Preferences::cmdXTermCommand();
1707 cmd.replace(
"%t", aboutData()->programName());
1708 if (cmd.find(
"%C") >= 0)
1711 if (flags & ProcData::TEMP_FILE)
1712 cmd.replace(
"%C", command);
1715 tmpXtermFile = createTempScriptFile(command,
true, event, *alarm);
1716 if (tmpXtermFile.isEmpty())
1718 cmd.replace(
"%C", tmpXtermFile);
1721 else if (cmd.find(
"%W") >= 0)
1725 tmpXtermFile = createTempScriptFile(command + TQString::fromLatin1(
"\nsleep 86400\n"),
true, event, *alarm);
1726 if (tmpXtermFile.isEmpty())
1728 cmd.replace(
"%W", tmpXtermFile);
1730 else if (cmd.find(
"%w") >= 0)
1734 TQString exec = KShellProcess::quote(command + TQString::fromLatin1(
"; sleep 86400"));
1735 cmd.replace(
"%w", exec);
1741 TQString exec = KShellProcess::quote(command);
1742 if (cmd.find(
"%c") >= 0)
1743 cmd.replace(
"%c", exec);
1751 comms = TDEProcess::AllOutput;
1753 ShellProcess* proc =
new ShellProcess(cmd);
1754 connect(proc, TQ_SIGNAL(shellExited(ShellProcess*)), TQ_SLOT(slotCommandExited(ShellProcess*)));
1755 TQGuardedPtr<ShellProcess> logproc = 0;
1756 if (comms == TDEProcess::AllOutput && !event.logFile().isEmpty())
1760 connect(proc, TQ_SIGNAL(receivedStdout(TDEProcess*,
char*,
int)), TQ_SLOT(slotCommandOutput(TDEProcess*,
char*,
int)));
1761 connect(proc, TQ_SIGNAL(receivedStderr(TDEProcess*,
char*,
int)), TQ_SLOT(slotCommandOutput(TDEProcess*,
char*,
int)));
1762 logproc =
new ShellProcess(TQString::fromLatin1(
"cat >>%1").arg(event.logFile()));
1763 connect(logproc, TQ_SIGNAL(shellExited(ShellProcess*)), TQ_SLOT(slotLogProcExited(ShellProcess*)));
1764 logproc->start(TDEProcess::Stdin);
1766 if (alarm && alarm->dateTime().isValid())
1768 TQString dateTime = alarm->dateTime().isDateOnly()
1769 ? TDEGlobal::locale()->formatDate(alarm->dateTime().date(),
true)
1770 : TDEGlobal::locale()->formatDateTime(alarm->dateTime().dateTime());
1771 heading.sprintf(
"\n******* KAlarm %s *******\n", dateTime.latin1());
1774 heading =
"\n******* KAlarm *******\n";
1775 logproc->writeStdin(heading, heading.length()+1);
1777 ProcData* pd =
new ProcData(proc, logproc,
new KAEvent(event), (alarm ?
new KAAlarm(*alarm) : 0), flags);
1778 if (flags & ProcData::TEMP_FILE)
1779 pd->tempFiles += command;
1780 if (!tmpXtermFile.isEmpty())
1781 pd->tempFiles += tmpXtermFile;
1782 mCommandProcesses.append(pd);
1783 if (proc->start(comms))
1787 kdError(5950) <<
"KAlarmApp::doShellCommand(): command failed to start\n";
1788 commandErrorMsg(proc, event, alarm, flags);
1789 mCommandProcesses.remove(pd);
1798 TQString KAlarmApp::createTempScriptFile(
const TQString& command,
bool insertShell,
const KAEvent& event,
const KAAlarm& alarm)
1800 KTempFile tmpFile(TQString(), TQString(), 0700);
1801 tmpFile.setAutoDelete(
false);
1802 TQTextStream* stream = tmpFile.textStream();
1804 kdError(5950) <<
"KAlarmApp::createTempScript(): Unable to create a temporary script file" << endl;
1808 *stream <<
"#!" << ShellProcess::shellPath() <<
"\n";
1811 if (tmpFile.status())
1812 kdError(5950) <<
"KAlarmApp::createTempScript(): Error " << tmpFile.status() <<
" writing to temporary script file" << endl;
1814 return tmpFile.name();
1817 TQStringList errmsgs(i18n(
"Error creating temporary script file"));
1818 (
new MessageWin(event, alarm.dateTime(), errmsgs))->show();
1825 void KAlarmApp::slotCommandOutput(TDEProcess* proc,
char* buffer,
int bufflen)
1829 for (TQValueList<ProcData*>::Iterator it = mCommandProcesses.begin(); it != mCommandProcesses.end(); ++it)
1832 if (pd->process == proc && pd->logProcess)
1834 pd->logProcess->writeStdin(buffer, bufflen);
1843 void KAlarmApp::slotLogProcExited(ShellProcess* proc)
1853 void KAlarmApp::slotCommandExited(ShellProcess* proc)
1855 kdDebug(5950) <<
"KAlarmApp::slotCommandExited()\n";
1857 for (TQValueList<ProcData*>::Iterator it = mCommandProcesses.begin(); it != mCommandProcesses.end(); ++it)
1860 if (pd->process == proc)
1864 pd->logProcess->stdinExit();
1867 if (!proc->normalExit())
1869 TQString errmsg = proc->errorMessage();
1870 kdWarning(5950) <<
"KAlarmApp::slotCommandExited(" << pd->event->cleanText() <<
"): " << errmsg << endl;
1871 if (pd->messageBoxParent)
1874 TQObjectList* dialogs = pd->messageBoxParent->queryList(
"KDialogBase", 0,
false,
true);
1875 KDialogBase* dialog = (KDialogBase*)dialogs->getFirst();
1878 if (!pd->tempFile())
1881 errmsg += proc->command();
1883 KMessageBox::error(pd->messageBoxParent, errmsg);
1886 commandErrorMsg(proc, *pd->event, pd->alarm, pd->flags);
1888 if (pd->preAction())
1889 execAlarm(*pd->event, *pd->alarm, pd->reschedule(), pd->allowDefer(),
true);
1890 mCommandProcesses.remove(it);
1897 if (mPendingQuit && mCommandProcesses.isEmpty())
1898 quitIf(mPendingQuitCode);
1904 void KAlarmApp::commandErrorMsg(
const ShellProcess* proc,
const KAEvent& event,
const KAAlarm* alarm,
int flags)
1906 TQStringList errmsgs;
1907 if (flags & ProcData::PRE_ACTION)
1908 errmsgs += i18n(
"Pre-alarm action:");
1909 else if (flags & ProcData::POST_ACTION)
1910 errmsgs += i18n(
"Post-alarm action:");
1911 errmsgs += proc->errorMessage();
1912 if (!(flags & ProcData::TEMP_FILE))
1913 errmsgs += proc->command();
1914 (
new MessageWin(event, (alarm ? alarm->dateTime() : DateTime()), errmsgs))->show();
1920 void KAlarmApp::commandMessage(ShellProcess* proc, TQWidget* parent)
1923 for (TQValueList<ProcData*>::Iterator it = mCommandProcesses.begin(); it != mCommandProcesses.end(); ++it)
1926 if (pd->process == proc)
1928 pd->messageBoxParent = parent;
1937 void KAlarmApp::setUpDcop()
1941 mInitialised =
true;
1942 Daemon::createDcopHandler();
1943 TQTimer::singleShot(0,
this, TQ_SLOT(processQueue()));
1951 bool KAlarmApp::initCheck(
bool calendarOnly)
1957 kdDebug(5950) <<
"KAlarmApp::initCheck(): opening active calendar\n";
1963 if (!mStartOfDay.isValid())
1971 AlarmCalendar::displayCalendar()->open();
1978 AlarmCalendar::expiredCalendar()->open();
1979 AlarmCalendar::expiredCalendar()->setPurgeDays(theInstance->mPrefsExpiredKeepDays);
1984 startdaemon = !Daemon::isRegistered();
2000 static bool convWakeTime(
const TQCString& timeParam, TQDateTime& dateTime,
bool& noTime)
2002 if (timeParam.length() > 19)
2005 strcpy(timeStr, timeParam);
2006 int dt[5] = { -1, -1, -1, -1, -1 };
2010 if ((s = strchr(timeStr,
':')) == 0)
2016 dt[4] = strtoul(s, &end, 10);
2017 if (end == s || *end || dt[4] >= 60)
2020 if ((s = strrchr(timeStr,
'-')) == 0)
2024 dt[3] = strtoul(s, &end, 10);
2025 if (end == s || *end || dt[3] >= 24)
2028 bool dateSet =
false;
2033 if ((s = strrchr(timeStr,
'-')) == 0)
2037 dt[2] = strtoul(s, &end, 10);
2038 if (end == s || *end || dt[2] == 0 || dt[2] > 31)
2043 if ((s = strrchr(timeStr,
'-')) == 0)
2047 dt[1] = strtoul(s, &end, 10);
2048 if (end == s || *end || dt[1] == 0 || dt[1] > 12)
2053 dt[0] = strtoul(timeStr, &end, 10);
2054 if (end == timeStr || *end)
2060 TQDate date(dt[0], dt[1], dt[2]);
2061 TQTime time(0, 0, 0);
2071 TQDateTime now = TQDateTime::currentDateTime();
2073 date.setYMD(now.date().year(),
2074 (dt[1] < 0 ? now.date().month() : dt[1]),
2075 (dt[2] < 0 ? now.date().day() : dt[2]));
2076 time.setHMS(dt[3], dt[4], 0);
2077 if (!dateSet && time < now.time())
2078 date = date.addDays(1);
2080 if (!date.isValid())
2082 dateTime.setDate(date);
2083 dateTime.setTime(time);
2093 static bool convInterval(
const TQCString& timeParam, KARecurrence::Type& recurType,
int& timeInterval,
bool allowMonthYear)
2095 TQCString timeString = timeParam;
2099 bool negative = (timeString[0] ==
'-');
2101 timeString = timeString.right(1);
2102 uint length = timeString.length();
2103 switch (timeString[length - 1])
2106 if (!allowMonthYear)
2108 recurType = KARecurrence::ANNUAL_DATE;
2109 timeString = timeString.left(length - 1);
2112 recurType = KARecurrence::WEEKLY;
2113 timeString = timeString.left(length - 1);
2116 recurType = KARecurrence::DAILY;
2117 timeString = timeString.left(length - 1);
2121 int i = timeString.find(
'H');
2124 if (!allowMonthYear)
2126 recurType = KARecurrence::MONTHLY_DAY;
2127 timeString = timeString.left(length - 1);
2131 recurType = KARecurrence::MINUTELY;
2132 interval = timeString.left(i).toUInt(&ok) * 60;
2133 timeString = timeString.mid(i + 1, length - i - 2);
2138 recurType = KARecurrence::MINUTELY;
2142 interval += timeString.toUInt(&ok);
2143 if (!allowMonthYear)
2148 case KARecurrence::WEEKLY:
2151 case KARecurrence::DAILY:
2158 timeInterval =
static_cast<int>(interval);
2160 timeInterval = -timeInterval;
2164 KAlarmApp::ProcData::ProcData(ShellProcess* p, ShellProcess* logp,
KAEvent* e, KAAlarm* a,
int f)
2169 messageBoxParent(0),
2173 KAlarmApp::ProcData::~ProcData()
2175 while (!tempFiles.isEmpty())
2178 TQFile f(tempFiles.first());
2180 tempFiles.remove(tempFiles.begin());
Provides read and write access to calendar files.
KAEvent corresponds to a KCal::Event instance.
MessageWin: A window to display an alarm message.
displays an alarm message