24 #include <tqiconset.h>
26 #include <kstandarddirs.h>
27 #include <tdeconfig.h>
28 #include <tdeaboutdata.h>
29 #include <tdemessagebox.h>
30 #include <dcopclient.h>
33 #include "kalarmd/kalarmd.h"
34 #include "kalarmd/alarmdaemoniface.h"
35 #include "kalarmd/alarmdaemoniface_stub.h"
36 #include "kalarmd/alarmguiiface.h"
38 #include "alarmcalendar.h"
40 #include "preferences.h"
44 static const int REGISTER_TIMEOUT = 20;
45 static const char* NOTIFY_DCOP_OBJECT =
"notify";
47 static TQString expandURL(
const TQString& urlString);
55 class NotificationHandler :
public TQObject,
virtual public AlarmGuiIface
58 NotificationHandler();
61 void alarmDaemonUpdate(
int calendarStatus,
const TQString& calendarURL);
62 void handleEvent(
const TQString& calendarURL,
const TQString& eventID);
63 void registered(
bool reregister,
int result,
int version);
67 Daemon* Daemon::mInstance = 0;
68 NotificationHandler* Daemon::mDcopHandler = 0;
69 TQValueList<TQString> Daemon::mQueuedEvents;
70 TQValueList<TQString> Daemon::mSavingEvents;
71 TQTimer* Daemon::mStartTimer = 0;
72 TQTimer* Daemon::mRegisterTimer = 0;
73 TQTimer* Daemon::mStatusTimer = 0;
74 int Daemon::mStatusTimerCount = 0;
75 int Daemon::mStatusTimerInterval;
76 int Daemon::mStartTimeout = 0;
77 Daemon::Status Daemon::mStatus = Daemon::STOPPED;
78 bool Daemon::mRunning =
false;
79 bool Daemon::mCalendarDisabled =
false;
80 bool Daemon::mEnableCalPending =
false;
81 bool Daemon::mRegisterFailMsg =
false;
86 static const int startCheckInterval = 500;
94 void Daemon::initialise()
97 mInstance =
new Daemon();
98 connect(AlarmCalendar::activeCalendar(), TQ_SIGNAL(calendarSaved(
AlarmCalendar*)), mInstance, TQ_SLOT(slotCalendarSaved(
AlarmCalendar*)));
104 void Daemon::createDcopHandler()
108 mDcopHandler =
new NotificationHandler();
111 mRunning = isRunning(
false);
113 mStatusTimerInterval = Preferences::daemonTrayCheckInterval();
114 Preferences::connect(TQ_SIGNAL(preferencesChanged()), mInstance, TQ_SLOT(slotPreferencesChanged()));
116 mStatusTimer =
new TQTimer(mInstance);
117 connect(mStatusTimer, TQ_SIGNAL(timeout()), mInstance, TQ_SLOT(timerCheckIfRunning()));
118 mStatusTimer->start(mStatusTimerInterval * 1000);
127 kdDebug(5950) <<
"Daemon::start()\n";
128 updateRegisteredStatus();
137 TQString execStr = locate(
"exe", TQString::fromLatin1(DAEMON_APP_NAME));
138 if (execStr.isEmpty())
140 KMessageBox::error(0, i18n(
"Alarm daemon not found."));
141 kdError() <<
"Daemon::startApp(): " DAEMON_APP_NAME
" not found" << endl;
144 TDEApplication::tdeinitExec(execStr);
145 kdDebug(5950) <<
"Daemon::start(): Alarm daemon started" << endl;
146 mStartTimeout = 5000/startCheckInterval + 1;
147 mStartTimer =
new TQTimer(mInstance);
148 connect(mStartTimer, TQ_SIGNAL(timeout()), mInstance, TQ_SLOT(checkIfStarted()));
149 mStartTimer->start(startCheckInterval);
150 mInstance->checkIfStarted();
157 if (!registerWith(
false))
172 bool Daemon::registerWith(
bool reregister)
189 bool disabledIfStopped = theApp()->alarmsDisabledIfStopped();
190 kdDebug(5950) << (reregister ?
"Daemon::reregisterWith(): " :
"Daemon::registerWith(): ") << (disabledIfStopped ?
"NO_START" :
"COMMAND_LINE") << endl;
191 TQCString appname = kapp->aboutData()->appName();
192 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
194 s.registerChange(appname, !disabledIfStopped);
196 s.registerApp(appname, kapp->aboutData()->programName(), TQCString(NOTIFY_DCOP_OBJECT), AlarmCalendar::activeCalendar()->urlString(), !disabledIfStopped);
199 kdError(5950) <<
"Daemon::registerWith(" << reregister <<
"): DCOP error" << endl;
200 registrationResult(reregister, KAlarmd::FAILURE);
203 mRegisterTimer =
new TQTimer(mInstance);
204 connect(mRegisterTimer, TQ_SIGNAL(timeout()), mInstance, TQ_SLOT(registerTimerExpired()));
205 mRegisterTimer->start(REGISTER_TIMEOUT * 1000);
212 void Daemon::registrationResult(
bool reregister,
int result,
int version)
214 kdDebug(5950) <<
"Daemon::registrationResult(" << reregister <<
") version: " << version << endl;
215 delete mRegisterTimer;
219 if (version && version != DAEMON_VERSION_NUM)
222 kdError(5950) <<
"Daemon::registrationResult(" << reregister <<
"): kalarmd reports incompatible version " << version << endl;
223 errmsg = i18n(
"Cannot enable alarms.\nInstallation or configuration error: Alarm Daemon (%1) version is incompatible.")
224 .arg(TQString::fromLatin1(DAEMON_APP_NAME));
230 case KAlarmd::SUCCESS:
232 case KAlarmd::NOT_FOUND:
236 kdError(5950) <<
"Daemon::registrationResult(" << reregister <<
"): registerApp dcop call: " << kapp->aboutData()->appName() <<
" not found\n";
237 KMessageBox::error(0, i18n(
"Alarms will be disabled if you stop KAlarm.\n"
238 "(Installation or configuration error: %1 cannot locate %2 executable.)")
239 .arg(TQString::fromLatin1(DAEMON_APP_NAME))
240 .arg(kapp->aboutData()->appName()));
242 case KAlarmd::FAILURE:
246 kdError(5950) <<
"Daemon::registrationResult(" << reregister <<
"): registerApp dcop call failed -> " << result << endl;
250 errmsg = i18n(
"Cannot enable alarms:\nFailed to register with Alarm Daemon (%1)")
251 .arg(TQString::fromLatin1(DAEMON_APP_NAME));
259 if (!errmsg.isEmpty())
261 if (mStatus == REGISTERED)
263 if (!mRegisterFailMsg)
265 mRegisterFailMsg =
true;
266 KMessageBox::error(0, errmsg);
275 mStatus = REGISTERED;
276 mRegisterFailMsg =
false;
277 kdDebug(5950) <<
"Daemon::start(): daemon startup complete" << endl;
284 void Daemon::checkIfStarted()
286 updateRegisteredStatus();
291 if (--mStartTimeout > 0)
306 kdError(5950) <<
"Daemon::checkIfStarted(): failed to start daemon" << endl;
307 KMessageBox::error(0, i18n(
"Cannot enable alarms:\nFailed to start Alarm Daemon (%1)").arg(TQString::fromLatin1(DAEMON_APP_NAME)));
315 void Daemon::updateRegisteredStatus(
bool timeout)
317 if (!kapp->dcopClient()->isApplicationRegistered(DAEMON_APP_NAME))
320 mRegisterFailMsg =
false;
330 TQTimer::singleShot(startCheckInterval, mInstance, TQ_SLOT(slotStarted()));
344 kdDebug(5950) <<
"Daemon::updateRegisteredStatus() -> " << mStatus << endl;
352 kdDebug(5950) <<
"Daemon::stop()" << endl;
353 if (kapp->dcopClient()->isApplicationRegistered(DAEMON_APP_NAME))
355 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
359 kdError(5950) <<
"Daemon::stop(): dcop call failed" << endl;
373 kdDebug(5950) <<
"Daemon::reset()" << endl;
374 if (!kapp->dcopClient()->isApplicationRegistered(DAEMON_APP_NAME))
376 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
377 s.resetCalendar(TQCString(kapp->aboutData()->appName()), AlarmCalendar::activeCalendar()->urlString());
379 kdError(5950) <<
"Daemon::reset(): resetCalendar dcop send failed" << endl;
386 void Daemon::reload()
388 kdDebug(5950) <<
"Daemon::reload()\n";
389 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
390 s.reloadCalendar(TQCString(kapp->aboutData()->appName()), AlarmCalendar::activeCalendar()->urlString());
392 kdError(5950) <<
"Daemon::reload(): reloadCalendar dcop send failed" << endl;
398 void Daemon::enableCalendar(
bool enable)
400 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
401 s.enableCalendar(AlarmCalendar::activeCalendar()->urlString(), enable);
402 mEnableCalPending =
false;
408 void Daemon::enableAutoStart(
bool enable)
411 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
412 s.enableAutoStart(enable);
416 TDEConfig adconfig(locate(
"config", DAEMON_APP_NAME
"rc"));
417 adconfig.setGroup(TQString::fromLatin1(DAEMON_AUTOSTART_SECTION));
418 adconfig.writeEntry(TQString::fromLatin1(DAEMON_AUTOSTART_KEY), enable);
426 void Daemon::notifyTimeChanged()
428 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
429 s.timeConfigChanged();
431 kdError(5950) <<
"Daemon::timeConfigChanged(): dcop send failed" << endl;
437 bool Daemon::autoStart()
439 TDEConfig adconfig(locate(
"config", DAEMON_APP_NAME
"rc"));
440 adconfig.setGroup(TQString::fromLatin1(DAEMON_AUTOSTART_SECTION));
441 return adconfig.readBoolEntry(TQString::fromLatin1(DAEMON_AUTOSTART_KEY),
true);
448 void Daemon::calendarIsEnabled(
bool enabled)
450 mCalendarDisabled = !enabled;
451 emit mInstance->daemonRunning(enabled);
458 void Daemon::setAlarmsEnabled(
bool enable)
460 kdDebug(5950) <<
"Daemon::setAlarmsEnabled(" << enable <<
")\n";
461 if (enable && !checkIfRunning())
466 emit daemonRunning(
false);
469 mEnableCalPending =
true;
474 if (checkIfRunning())
475 enableCalendar(enable);
481 bool Daemon::monitoringAlarms()
483 bool ok = !mCalendarDisabled && isRunning();
484 emit mInstance->daemonRunning(ok);
491 bool Daemon::isRunning(
bool startdaemon)
493 static bool runState =
false;
494 updateRegisteredStatus();
495 bool newRunState = (mStatus == READY || mStatus == REGISTERED);
496 if (newRunState != runState)
499 runState = newRunState;
500 if (runState && startdaemon)
503 return runState && (mStatus == REGISTERED);
509 void Daemon::timerCheckIfRunning()
513 if (mStatusTimerCount > 0 && --mStatusTimerCount <= 0)
514 mStatusTimer->changeInterval(mStatusTimerInterval * 1000);
521 bool Daemon::checkIfRunning()
523 bool newstatus = isRunning();
524 if (newstatus != mRunning)
526 mRunning = newstatus;
527 int status = mRunning && !mCalendarDisabled;
528 emit mInstance->daemonRunning(status);
529 mStatusTimer->changeInterval(mStatusTimerInterval * 1000);
530 mStatusTimerCount = 0;
534 if (mEnableCalPending)
535 enableCalendar(
true);
544 void Daemon::setFastCheck()
546 mStatusTimer->start(500);
547 mStatusTimerCount = 20;
554 void Daemon::slotPreferencesChanged()
556 int newInterval = Preferences::daemonTrayCheckInterval();
557 if (newInterval != mStatusTimerInterval)
560 mStatusTimerInterval = newInterval;
561 if (mStatusTimerCount <= 0)
562 mStatusTimer->changeInterval(mStatusTimerInterval * 1000);
569 AlarmEnableAction* Daemon::createAlarmEnableAction(TDEActionCollection* actions,
const char* name)
571 AlarmEnableAction* a =
new AlarmEnableAction(0, actions, name);
572 connect(a, TQ_SIGNAL(userClicked(
bool)), mInstance, TQ_SLOT(setAlarmsEnabled(
bool)));
573 connect(mInstance, TQ_SIGNAL(daemonRunning(
bool)), a, TQ_SLOT(setCheckedActual(
bool)));
583 if (cal == AlarmCalendar::activeCalendar())
585 int n = mSavingEvents.count();
590 for (
int i = 0; i < n - 1; ++i)
591 notifyEventHandled(mSavingEvents[i],
false);
592 notifyEventHandled(mSavingEvents[n - 1],
true);
593 mSavingEvents.clear();
603 void Daemon::queueEvent(
const TQString& eventId)
605 mQueuedEvents += eventId;
612 void Daemon::savingEvent(
const TQString& eventId)
614 if (mQueuedEvents.remove(eventId) > 0)
615 mSavingEvents += eventId;
622 void Daemon::eventHandled(
const TQString& eventId,
bool reloadCal)
624 if (mQueuedEvents.remove(eventId) > 0)
625 notifyEventHandled(eventId, reloadCal);
634 void Daemon::notifyEventHandled(
const TQString& eventId,
bool reloadCal)
636 kdDebug(5950) <<
"Daemon::notifyEventHandled(" << eventId << (reloadCal ?
"): reload" :
")") << endl;
637 AlarmDaemonIface_stub s(DAEMON_APP_NAME, DAEMON_DCOP_OBJECT);
638 s.eventHandled(TQCString(kapp->aboutData()->appName()), AlarmCalendar::activeCalendar()->urlString(), eventId, reloadCal);
640 kdError(5950) <<
"Daemon::notifyEventHandled(): eventHandled dcop send failed" << endl;
647 int Daemon::maxTimeSinceCheck()
649 return DAEMON_CHECK_INTERVAL;
657 NotificationHandler::NotificationHandler()
658 : DCOPObject(NOTIFY_DCOP_OBJECT),
661 kdDebug(5950) <<
"NotificationHandler::NotificationHandler()\n";
669 void NotificationHandler::alarmDaemonUpdate(
int calendarStatus,
const TQString& calendarURL)
671 kdDebug(5950) <<
"NotificationHandler::alarmDaemonUpdate(" << calendarStatus <<
")\n";
672 KAlarmd::CalendarStatus status = KAlarmd::CalendarStatus(calendarStatus);
673 if (expandURL(calendarURL) != AlarmCalendar::activeCalendar()->urlString())
675 bool enabled =
false;
678 case KAlarmd::CALENDAR_UNAVAILABLE:
680 kdDebug(5950) <<
"NotificationHandler::alarmDaemonUpdate(CALENDAR_UNAVAILABLE)\n";
682 case KAlarmd::CALENDAR_DISABLED:
684 kdDebug(5950) <<
"NotificationHandler::alarmDaemonUpdate(DISABLE_CALENDAR)\n";
686 case KAlarmd::CALENDAR_ENABLED:
688 kdDebug(5950) <<
"NotificationHandler::alarmDaemonUpdate(ENABLE_CALENDAR)\n";
694 Daemon::calendarIsEnabled(enabled);
700 void NotificationHandler::handleEvent(
const TQString& url,
const TQString& eventId)
702 TQString
id = eventId;
703 if (
id.startsWith(TQString::fromLatin1(
"ad:")))
707 Daemon::queueEvent(
id);
709 theApp()->handleEvent(url,
id);
716 void NotificationHandler::registered(
bool reregister,
int result,
int version)
718 Daemon::registrationResult(reregister, result, version);
726 AlarmEnableAction::AlarmEnableAction(
int accel, TQObject* parent,
const char* name)
727 : TDEToggleAction(i18n(
"Enable &Alarms"), accel, parent, name),
730 setCheckedState(i18n(
"Disable &Alarms"));
731 setCheckedActual(
false);
738 void AlarmEnableAction::setCheckedActual(
bool running)
740 kdDebug(5950) <<
"AlarmEnableAction::setCheckedActual(" << running <<
")\n";
741 if (running != isChecked() || !mInitialised)
743 TDEToggleAction::setChecked(running);
744 emit switched(running);
752 void AlarmEnableAction::setChecked(
bool check)
754 kdDebug(5950) <<
"AlarmEnableAction::setChecked(" << check <<
")\n";
755 if (check != isChecked())
758 Daemon::allowRegisterFailMsg();
759 emit userClicked(check);
769 TQString expandURL(
const TQString& urlString)
771 if (urlString.isEmpty())
773 return KURL(urlString).url();
Provides read and write access to calendar files.
the KAlarm application object