kalarm

preferences.cpp
1 /*
2  * preferences.cpp - program preference settings
3  * Program: kalarm
4  * Copyright © 2001-2008 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 
23 #include <tdeglobal.h>
24 #include <tdeconfig.h>
25 #include <kstandarddirs.h>
26 #include <tdeapplication.h>
27 #include <tdeglobalsettings.h>
28 #include <tdemessagebox.h>
29 
30 #include <libkpimidentities/identity.h>
31 #include <libkpimidentities/identitymanager.h>
32 
33 #include "daemon.h"
34 #include "functions.h"
35 #include "kamail.h"
36 #include "messagebox.h"
37 #include "preferences.moc"
38 
39 
40 static TQString translateXTermPath(TDEConfig*, const TQString& cmdline, bool write);
41 
42 Preferences* Preferences::mInstance = 0;
43 
44 // Default config file settings
45 TQColor defaultMessageColours[] = { TQt::red, TQt::green, TQt::blue, TQt::cyan, TQt::magenta, TQt::yellow, TQt::white, TQt::lightGray, TQt::black, TQColor() };
46 const ColourList Preferences::default_messageColours(defaultMessageColours);
47 const TQColor Preferences::default_defaultBgColour(TQt::red);
48 const TQColor Preferences::default_defaultFgColour(TQt::black);
49 TQFont Preferences::mDefault_messageFont; // initialised in constructor
50 const TQTime Preferences::default_startOfDay(0, 0);
51 const bool Preferences::default_runInSystemTray = true;
52 const bool Preferences::default_disableAlarmsIfStopped = true;
53 const bool Preferences::default_quitWarn = true;
54 const bool Preferences::default_autostartTrayIcon = true;
55 const bool Preferences::default_confirmAlarmDeletion = true;
56 const bool Preferences::default_modalMessages = true;
57 const int Preferences::default_messageButtonDelay = 0; // (seconds)
58 const int Preferences::default_tooltipAlarmCount = 5;
59 const bool Preferences::default_showTooltipAlarmTime = true;
60 const bool Preferences::default_showTooltipTimeToAlarm = true;
61 const TQString Preferences::default_tooltipTimeToPrefix = TQString::fromLatin1("+");
62 const int Preferences::default_daemonTrayCheckInterval = 10; // (seconds)
63 const bool Preferences::default_emailCopyToKMail = false;
64 const bool Preferences::default_emailQueuedNotify = false;
65 const TQColor Preferences::default_disabledColour(TQt::lightGray);
66 const TQColor Preferences::default_expiredColour(TQt::darkRed);
67 const int Preferences::default_expiredKeepDays = 7;
68 const TQString Preferences::default_defaultSoundFile = TQString();
69 const float Preferences::default_defaultSoundVolume = -1;
70 const int Preferences::default_defaultLateCancel = 0;
71 const bool Preferences::default_defaultAutoClose = false;
72 const bool Preferences::default_defaultCopyToKOrganizer = false;
73 const bool Preferences::default_defaultSoundRepeat = false;
74 const SoundPicker::Type Preferences::default_defaultSoundType = SoundPicker::NONE;
75 const bool Preferences::default_defaultConfirmAck = false;
76 const bool Preferences::default_defaultCmdScript = false;
77 const EditAlarmDlg::CmdLogType Preferences::default_defaultCmdLogType = EditAlarmDlg::DISCARD_OUTPUT;
78 const bool Preferences::default_defaultEmailBcc = false;
79 const TQString Preferences::default_emailAddress = TQString();
80 const TQString Preferences::default_emailBccAddress = TQString();
81 const Preferences::MailClient Preferences::default_emailClient = KMAIL;
82 const Preferences::MailFrom Preferences::default_emailBccFrom = MAIL_FROM_CONTROL_CENTRE;
83 const RecurrenceEdit::RepeatType Preferences::default_defaultRecurPeriod = RecurrenceEdit::NO_RECUR;
84 const KARecurrence::Feb29Type Preferences::default_defaultFeb29Type = KARecurrence::FEB29_MAR1;
85 const TimePeriod::Units Preferences::default_defaultReminderUnits = TimePeriod::HOURS_MINUTES;
86 const TQString Preferences::default_defaultPreAction;
87 const TQString Preferences::default_defaultPostAction;
88 
89 Preferences::MailFrom Preferences::default_emailFrom()
90 {
91  return KAMail::identitiesExist() ? MAIL_FROM_KMAIL : MAIL_FROM_CONTROL_CENTRE;
92 }
93 
94 // Active config file settings
95 ColourList Preferences::mMessageColours;
96 TQColor Preferences::mDefaultBgColour;
97 TQFont Preferences::mMessageFont;
98 TQTime Preferences::mStartOfDay;
99 bool Preferences::mRunInSystemTray;
100 bool Preferences::mDisableAlarmsIfStopped;
101 bool Preferences::mAutostartTrayIcon;
102 KARecurrence::Feb29Type Preferences::mDefaultFeb29Type;
103 bool Preferences::mModalMessages;
104 int Preferences::mMessageButtonDelay;
105 int Preferences::mTooltipAlarmCount;
106 bool Preferences::mShowTooltipAlarmTime;
107 bool Preferences::mShowTooltipTimeToAlarm;
108 TQString Preferences::mTooltipTimeToPrefix;
109 int Preferences::mDaemonTrayCheckInterval;
110 TQString Preferences::mEmailAddress;
111 TQString Preferences::mEmailBccAddress;
112 Preferences::MailClient Preferences::mEmailClient;
113 Preferences::MailFrom Preferences::mEmailFrom;
114 Preferences::MailFrom Preferences::mEmailBccFrom;
115 bool Preferences::mEmailCopyToKMail;
116 TQString Preferences::mCmdXTermCommand;
117 TQColor Preferences::mDisabledColour;
118 TQColor Preferences::mExpiredColour;
119 int Preferences::mExpiredKeepDays;
120 // Default settings for Edit Alarm dialog
121 TQString Preferences::mDefaultSoundFile;
122 float Preferences::mDefaultSoundVolume;
123 int Preferences::mDefaultLateCancel;
124 bool Preferences::mDefaultAutoClose;
125 bool Preferences::mDefaultCopyToKOrganizer;
126 SoundPicker::Type Preferences::mDefaultSoundType;
127 bool Preferences::mDefaultSoundRepeat;
128 bool Preferences::mDefaultConfirmAck;
129 bool Preferences::mDefaultEmailBcc;
130 bool Preferences::mDefaultCmdScript;
131 EditAlarmDlg::CmdLogType Preferences::mDefaultCmdLogType;
132 TQString Preferences::mDefaultCmdLogFile;
133 RecurrenceEdit::RepeatType Preferences::mDefaultRecurPeriod;
134 TimePeriod::Units Preferences::mDefaultReminderUnits;
135 TQString Preferences::mDefaultPreAction;
136 TQString Preferences::mDefaultPostAction;
137 // Change tracking
138 TQTime Preferences::mOldStartOfDay;
139 bool Preferences::mStartOfDayChanged;
140 
141 
142 static const TQString defaultFeb29RecurType = TQString::fromLatin1("Mar1");
143 static const TQString defaultEmailClient = TQString::fromLatin1("kmail");
144 
145 // Config file entry names
146 static const TQString GENERAL_SECTION = TQString::fromLatin1("General");
147 static const TQString VERSION_NUM = TQString::fromLatin1("Version");
148 static const TQString MESSAGE_COLOURS = TQString::fromLatin1("MessageColours");
149 static const TQString MESSAGE_BG_COLOUR = TQString::fromLatin1("MessageBackgroundColour");
150 static const TQString MESSAGE_FONT = TQString::fromLatin1("MessageFont");
151 static const TQString RUN_IN_SYSTEM_TRAY = TQString::fromLatin1("RunInSystemTray");
152 static const TQString DISABLE_IF_STOPPED = TQString::fromLatin1("DisableAlarmsIfStopped");
153 static const TQString AUTOSTART_TRAY = TQString::fromLatin1("AutostartTray");
154 static const TQString FEB29_RECUR_TYPE = TQString::fromLatin1("Feb29Recur");
155 static const TQString MODAL_MESSAGES = TQString::fromLatin1("ModalMessages");
156 static const TQString MESSAGE_BUTTON_DELAY = TQString::fromLatin1("MessageButtonDelay");
157 static const TQString TOOLTIP_ALARM_COUNT = TQString::fromLatin1("TooltipAlarmCount");
158 static const TQString TOOLTIP_ALARM_TIME = TQString::fromLatin1("ShowTooltipAlarmTime");
159 static const TQString TOOLTIP_TIME_TO_ALARM = TQString::fromLatin1("ShowTooltipTimeToAlarm");
160 static const TQString TOOLTIP_TIME_TO_PREFIX = TQString::fromLatin1("TooltipTimeToPrefix");
161 static const TQString DAEMON_TRAY_INTERVAL = TQString::fromLatin1("DaemonTrayCheckInterval");
162 static const TQString EMAIL_CLIENT = TQString::fromLatin1("EmailClient");
163 static const TQString EMAIL_COPY_TO_KMAIL = TQString::fromLatin1("EmailCopyToKMail");
164 static const TQString EMAIL_FROM = TQString::fromLatin1("EmailFrom");
165 static const TQString EMAIL_BCC_ADDRESS = TQString::fromLatin1("EmailBccAddress");
166 static const TQString CMD_XTERM_COMMAND = TQString::fromLatin1("CmdXTerm");
167 static const TQString START_OF_DAY = TQString::fromLatin1("StartOfDay");
168 static const TQString START_OF_DAY_CHECK = TQString::fromLatin1("Sod");
169 static const TQString DISABLED_COLOUR = TQString::fromLatin1("DisabledColour");
170 static const TQString EXPIRED_COLOUR = TQString::fromLatin1("ExpiredColour");
171 static const TQString EXPIRED_KEEP_DAYS = TQString::fromLatin1("ExpiredKeepDays");
172 static const TQString DEFAULTS_SECTION = TQString::fromLatin1("Defaults");
173 static const TQString DEF_LATE_CANCEL = TQString::fromLatin1("DefLateCancel");
174 static const TQString DEF_AUTO_CLOSE = TQString::fromLatin1("DefAutoClose");
175 static const TQString DEF_CONFIRM_ACK = TQString::fromLatin1("DefConfirmAck");
176 static const TQString DEF_COPY_TO_KORG = TQString::fromLatin1("DefCopyKOrg");
177 static const TQString DEF_SOUND_TYPE = TQString::fromLatin1("DefSoundType");
178 static const TQString DEF_SOUND_FILE = TQString::fromLatin1("DefSoundFile");
179 static const TQString DEF_SOUND_VOLUME = TQString::fromLatin1("DefSoundVolume");
180 static const TQString DEF_SOUND_REPEAT = TQString::fromLatin1("DefSoundRepeat");
181 static const TQString DEF_CMD_SCRIPT = TQString::fromLatin1("DefCmdScript");
182 static const TQString DEF_CMD_LOG_TYPE = TQString::fromLatin1("DefCmdLogType");
183 static const TQString DEF_LOG_FILE = TQString::fromLatin1("DefLogFile");
184 static const TQString DEF_EMAIL_BCC = TQString::fromLatin1("DefEmailBcc");
185 static const TQString DEF_RECUR_PERIOD = TQString::fromLatin1("DefRecurPeriod");
186 static const TQString DEF_REMIND_UNITS = TQString::fromLatin1("RemindUnits");
187 static const TQString DEF_PRE_ACTION = TQString::fromLatin1("DefPreAction");
188 static const TQString DEF_POST_ACTION = TQString::fromLatin1("DefPostAction");
189 
190 // Config file entry name for temporary use
191 static const TQString TEMP = TQString::fromLatin1("Temp");
192 
193 // Values for EmailFrom entry
194 static const TQString FROM_CONTROL_CENTRE = TQString::fromLatin1("@ControlCenter");
195 static const TQString FROM_KMAIL = TQString::fromLatin1("@KMail");
196 
197 // Config file entry names for notification messages
198 const TQString Preferences::QUIT_WARN = TQString::fromLatin1("QuitWarn");
199 const TQString Preferences::CONFIRM_ALARM_DELETION = TQString::fromLatin1("ConfirmAlarmDeletion");
200 const TQString Preferences::EMAIL_QUEUED_NOTIFY = TQString::fromLatin1("EmailQueuedNotify");
201 
202 static const int SODxor = 0x82451630;
203 inline int Preferences::startOfDayCheck()
204 {
205  // Combine with a 'random' constant to prevent 'clever' people fiddling the
206  // value, and thereby screwing things up.
207  return TQTime().msecsTo(mStartOfDay) ^ SODxor;
208 }
209 
210 
211 void Preferences::initialise()
212 {
213  if (!mInstance)
214  {
215  // Initialise static variables here to avoid static initialisation
216  // sequencing errors.
217  mDefault_messageFont = TQFont(TDEGlobalSettings::generalFont().family(), 16, TQFont::Bold);
218 
219  mInstance = new Preferences;
220 
221  convertOldPrefs(); // convert preferences written by previous KAlarm versions
222  read();
223 
224  // Set the default button for the Quit warning message box to Cancel
225  MessageBox::setContinueDefault(QUIT_WARN, KMessageBox::Cancel);
226  MessageBox::setDefaultShouldBeShownContinue(QUIT_WARN, default_quitWarn);
227  MessageBox::setDefaultShouldBeShownContinue(EMAIL_QUEUED_NOTIFY, default_emailQueuedNotify);
228  MessageBox::setDefaultShouldBeShownContinue(CONFIRM_ALARM_DELETION, default_confirmAlarmDeletion);
229  }
230 }
231 
232 void Preferences::connect(const char* signal, const TQObject* receiver, const char* member)
233 {
234  initialise();
235  TQObject::connect(mInstance, signal, receiver, member);
236 }
237 
238 void Preferences::emitStartOfDayChanged()
239 {
240  emit startOfDayChanged(mOldStartOfDay);
241 }
242 
243 void Preferences::emitPreferencesChanged()
244 {
245  emit preferencesChanged();
246 }
247 
248 /******************************************************************************
249 * Read preference values from the config file.
250 */
251 void Preferences::read()
252 {
253  initialise();
254 
255  TDEConfig* config = TDEGlobal::config();
256  config->setGroup(GENERAL_SECTION);
257  TQStringList cols = config->readListEntry(MESSAGE_COLOURS);
258  if (!cols.count())
259  mMessageColours = default_messageColours;
260  else
261  {
262  mMessageColours.clear();
263  for (TQStringList::Iterator it = cols.begin(); it != cols.end(); ++it)
264  {
265  TQColor c((*it));
266  if (c.isValid())
267  mMessageColours.insert(c);
268  }
269  }
270  mDefaultBgColour = config->readColorEntry(MESSAGE_BG_COLOUR, &default_defaultBgColour);
271  mMessageFont = config->readFontEntry(MESSAGE_FONT, &mDefault_messageFont);
272  mRunInSystemTray = config->readBoolEntry(RUN_IN_SYSTEM_TRAY, default_runInSystemTray);
273  mDisableAlarmsIfStopped = config->readBoolEntry(DISABLE_IF_STOPPED, default_disableAlarmsIfStopped);
274  mAutostartTrayIcon = config->readBoolEntry(AUTOSTART_TRAY, default_autostartTrayIcon);
275  mModalMessages = config->readBoolEntry(MODAL_MESSAGES, default_modalMessages);
276  mMessageButtonDelay = config->readNumEntry(MESSAGE_BUTTON_DELAY, default_messageButtonDelay);
277  if (mMessageButtonDelay > 10)
278  mMessageButtonDelay = 10; // prevent windows being unusable for a long time
279  if (mMessageButtonDelay < -1)
280  mMessageButtonDelay = -1;
281  mTooltipAlarmCount = static_cast<int>(config->readUnsignedNumEntry(TOOLTIP_ALARM_COUNT, default_tooltipAlarmCount));
282  if (mTooltipAlarmCount < 1)
283  mTooltipAlarmCount = 1;
284  mShowTooltipAlarmTime = config->readBoolEntry(TOOLTIP_ALARM_TIME, default_showTooltipAlarmTime);
285  mShowTooltipTimeToAlarm = config->readBoolEntry(TOOLTIP_TIME_TO_ALARM, default_showTooltipTimeToAlarm);
286  mTooltipTimeToPrefix = config->readEntry(TOOLTIP_TIME_TO_PREFIX, default_tooltipTimeToPrefix);
287  mDaemonTrayCheckInterval = static_cast<int>(config->readUnsignedNumEntry(DAEMON_TRAY_INTERVAL, default_daemonTrayCheckInterval));
288  if (mDaemonTrayCheckInterval < 1)
289  mDaemonTrayCheckInterval = 1;
290  TQCString client = config->readEntry(EMAIL_CLIENT, defaultEmailClient).local8Bit(); // don't use readPathEntry() here (values are hard-coded)
291  mEmailClient = (client == "sendmail" ? SENDMAIL : KMAIL);
292  mEmailCopyToKMail = config->readBoolEntry(EMAIL_COPY_TO_KMAIL, default_emailCopyToKMail);
293  TQString from = config->readEntry(EMAIL_FROM, emailFrom(default_emailFrom(), false, false));
294  mEmailFrom = emailFrom(from);
295  TQString bccFrom = config->readEntry(EMAIL_BCC_ADDRESS, emailFrom(default_emailBccFrom, false, true));
296  mEmailBccFrom = emailFrom(bccFrom);
297  if (mEmailFrom == MAIL_FROM_CONTROL_CENTRE || mEmailBccFrom == MAIL_FROM_CONTROL_CENTRE)
298  mEmailAddress = mEmailBccAddress = KAMail::controlCentreAddress();
299  if (mEmailFrom == MAIL_FROM_ADDR)
300  mEmailAddress = from;
301  if (mEmailBccFrom == MAIL_FROM_ADDR)
302  mEmailBccAddress = bccFrom;
303  mCmdXTermCommand = translateXTermPath(config, config->readEntry(CMD_XTERM_COMMAND), false);
304  TQDateTime defStartOfDay(TQDate(1900,1,1), default_startOfDay);
305  mStartOfDay = config->readDateTimeEntry(START_OF_DAY, &defStartOfDay).time();
306  mOldStartOfDay.setHMS(0,0,0);
307  int sod = config->readNumEntry(START_OF_DAY_CHECK, 0);
308  if (sod)
309  mOldStartOfDay = mOldStartOfDay.addMSecs(sod ^ SODxor);
310  mDisabledColour = config->readColorEntry(DISABLED_COLOUR, &default_disabledColour);
311  mExpiredColour = config->readColorEntry(EXPIRED_COLOUR, &default_expiredColour);
312  mExpiredKeepDays = config->readNumEntry(EXPIRED_KEEP_DAYS, default_expiredKeepDays);
313 
314  config->setGroup(DEFAULTS_SECTION);
315  mDefaultLateCancel = static_cast<int>(config->readUnsignedNumEntry(DEF_LATE_CANCEL, default_defaultLateCancel));
316  mDefaultAutoClose = config->readBoolEntry(DEF_AUTO_CLOSE, default_defaultAutoClose);
317  mDefaultConfirmAck = config->readBoolEntry(DEF_CONFIRM_ACK, default_defaultConfirmAck);
318  mDefaultCopyToKOrganizer = config->readBoolEntry(DEF_COPY_TO_KORG, default_defaultCopyToKOrganizer);
319  int soundType = config->readNumEntry(DEF_SOUND_TYPE, default_defaultSoundType);
320  mDefaultSoundType = (soundType < 0 || soundType > SoundPicker::SPEAK)
321  ? default_defaultSoundType : (SoundPicker::Type)soundType;
322  mDefaultSoundVolume = static_cast<float>(config->readDoubleNumEntry(DEF_SOUND_VOLUME, default_defaultSoundVolume));
323 #ifdef WITHOUT_ARTS
324  mDefaultSoundRepeat = false;
325 #else
326  mDefaultSoundRepeat = config->readBoolEntry(DEF_SOUND_REPEAT, default_defaultSoundRepeat);
327 #endif
328  mDefaultSoundFile = config->readPathEntry(DEF_SOUND_FILE);
329  mDefaultCmdScript = config->readBoolEntry(DEF_CMD_SCRIPT, default_defaultCmdScript);
330  int logType = config->readNumEntry(DEF_CMD_LOG_TYPE, default_defaultCmdLogType);
331  mDefaultCmdLogType = (logType < EditAlarmDlg::DISCARD_OUTPUT || logType > EditAlarmDlg::EXEC_IN_TERMINAL)
332  ? default_defaultCmdLogType : (EditAlarmDlg::CmdLogType)logType;
333  mDefaultCmdLogFile = config->readPathEntry(DEF_LOG_FILE);
334  mDefaultEmailBcc = config->readBoolEntry(DEF_EMAIL_BCC, default_defaultEmailBcc);
335  int recurPeriod = config->readNumEntry(DEF_RECUR_PERIOD, default_defaultRecurPeriod);
336  mDefaultRecurPeriod = (recurPeriod < RecurrenceEdit::SUBDAILY || recurPeriod > RecurrenceEdit::ANNUAL)
337  ? default_defaultRecurPeriod : (RecurrenceEdit::RepeatType)recurPeriod;
338  TQCString feb29 = config->readEntry(FEB29_RECUR_TYPE, defaultFeb29RecurType).local8Bit();
339  mDefaultFeb29Type = (feb29 == "Mar1") ? KARecurrence::FEB29_MAR1 : (feb29 == "Feb28") ? KARecurrence::FEB29_FEB28 : KARecurrence::FEB29_FEB29;
340  TQString remindUnits = config->readEntry(DEF_REMIND_UNITS);
341  mDefaultReminderUnits = (remindUnits == TQString::fromLatin1("Minutes")) ? TimePeriod::MINUTES
342  : (remindUnits == TQString::fromLatin1("HoursMinutes")) ? TimePeriod::HOURS_MINUTES
343  : (remindUnits == TQString::fromLatin1("Days")) ? TimePeriod::DAYS
344  : (remindUnits == TQString::fromLatin1("Weeks")) ? TimePeriod::WEEKS : default_defaultReminderUnits;
345  mDefaultPreAction = config->readEntry(DEF_PRE_ACTION, default_defaultPreAction);
346  mDefaultPostAction = config->readEntry(DEF_POST_ACTION, default_defaultPostAction);
347  mInstance->emitPreferencesChanged();
348  mStartOfDayChanged = (mStartOfDay != mOldStartOfDay);
349  if (mStartOfDayChanged)
350  {
351  mInstance->emitStartOfDayChanged();
352  mOldStartOfDay = mStartOfDay;
353  }
354 }
355 
356 /******************************************************************************
357 * Save preference values to the config file.
358 */
359 void Preferences::save(bool syncToDisc)
360 {
361  TDEConfig* config = TDEGlobal::config();
362  config->setGroup(GENERAL_SECTION);
363  config->writeEntry(VERSION_NUM, KALARM_VERSION);
364  TQStringList colours;
365  for (ColourList::const_iterator it = mMessageColours.begin(); it != mMessageColours.end(); ++it)
366  colours.append(TQColor(*it).name());
367  config->writeEntry(MESSAGE_COLOURS, colours);
368  config->writeEntry(MESSAGE_BG_COLOUR, mDefaultBgColour);
369  config->writeEntry(MESSAGE_FONT, mMessageFont);
370  config->writeEntry(RUN_IN_SYSTEM_TRAY, mRunInSystemTray);
371  config->writeEntry(DISABLE_IF_STOPPED, mDisableAlarmsIfStopped);
372  config->writeEntry(AUTOSTART_TRAY, mAutostartTrayIcon);
373  config->writeEntry(MODAL_MESSAGES, mModalMessages);
374  config->writeEntry(MESSAGE_BUTTON_DELAY, mMessageButtonDelay);
375  config->writeEntry(TOOLTIP_ALARM_COUNT, mTooltipAlarmCount);
376  config->writeEntry(TOOLTIP_ALARM_TIME, mShowTooltipAlarmTime);
377  config->writeEntry(TOOLTIP_TIME_TO_ALARM, mShowTooltipTimeToAlarm);
378  config->writeEntry(TOOLTIP_TIME_TO_PREFIX, mTooltipTimeToPrefix);
379  config->writeEntry(DAEMON_TRAY_INTERVAL, mDaemonTrayCheckInterval);
380  config->writeEntry(EMAIL_CLIENT, (mEmailClient == SENDMAIL ? "sendmail" : "kmail"));
381  config->writeEntry(EMAIL_COPY_TO_KMAIL, mEmailCopyToKMail);
382  config->writeEntry(EMAIL_FROM, emailFrom(mEmailFrom, true, false));
383  config->writeEntry(EMAIL_BCC_ADDRESS, emailFrom(mEmailBccFrom, true, true));
384  config->writeEntry(CMD_XTERM_COMMAND, translateXTermPath(config, mCmdXTermCommand, true));
385  config->writeEntry(START_OF_DAY, TQDateTime(TQDate(1900,1,1), mStartOfDay));
386  // Start-of-day check value is only written once the start-of-day time has been processed.
387  config->writeEntry(DISABLED_COLOUR, mDisabledColour);
388  config->writeEntry(EXPIRED_COLOUR, mExpiredColour);
389  config->writeEntry(EXPIRED_KEEP_DAYS, mExpiredKeepDays);
390 
391  config->setGroup(DEFAULTS_SECTION);
392  config->writeEntry(DEF_LATE_CANCEL, mDefaultLateCancel);
393  config->writeEntry(DEF_AUTO_CLOSE, mDefaultAutoClose);
394  config->writeEntry(DEF_CONFIRM_ACK, mDefaultConfirmAck);
395  config->writeEntry(DEF_COPY_TO_KORG, mDefaultCopyToKOrganizer);
396  config->writeEntry(DEF_SOUND_TYPE, mDefaultSoundType);
397  config->writePathEntry(DEF_SOUND_FILE, mDefaultSoundFile);
398  config->writeEntry(DEF_SOUND_VOLUME, static_cast<double>(mDefaultSoundVolume));
399  config->writeEntry(DEF_SOUND_REPEAT, mDefaultSoundRepeat);
400  config->writeEntry(DEF_CMD_SCRIPT, mDefaultCmdScript);
401  config->writeEntry(DEF_CMD_LOG_TYPE, mDefaultCmdLogType);
402  config->writePathEntry(DEF_LOG_FILE, mDefaultCmdLogFile);
403  config->writeEntry(DEF_EMAIL_BCC, mDefaultEmailBcc);
404  config->writeEntry(DEF_RECUR_PERIOD, mDefaultRecurPeriod);
405  config->writeEntry(FEB29_RECUR_TYPE, (mDefaultFeb29Type == KARecurrence::FEB29_MAR1 ? "Mar1" : mDefaultFeb29Type == KARecurrence::FEB29_FEB28 ? "Feb28" : "None"));
406  TQString value;
407  switch (mDefaultReminderUnits)
408  {
409  case TimePeriod::MINUTES: value = TQString::fromLatin1("Minutes"); break;
410  case TimePeriod::HOURS_MINUTES: value = TQString::fromLatin1("HoursMinutes"); break;
411  case TimePeriod::DAYS: value = TQString::fromLatin1("Days"); break;
412  case TimePeriod::WEEKS: value = TQString::fromLatin1("Weeks"); break;
413  default: value = TQString(); break;
414  }
415  config->writeEntry(DEF_REMIND_UNITS, value);
416  config->writeEntry(DEF_PRE_ACTION, mDefaultPreAction);
417  config->writeEntry(DEF_POST_ACTION, mDefaultPostAction);
418 
419  if (syncToDisc)
420  config->sync();
421  mInstance->emitPreferencesChanged();
422  if (mStartOfDay != mOldStartOfDay)
423  {
424  mInstance->emitStartOfDayChanged();
425  mOldStartOfDay = mStartOfDay;
426  }
427 }
428 
429 void Preferences::syncToDisc()
430 {
431  TDEGlobal::config()->sync();
432 }
433 
434 void Preferences::updateStartOfDayCheck()
435 {
436  TDEConfig* config = TDEGlobal::config();
437  config->setGroup(GENERAL_SECTION);
438  config->writeEntry(START_OF_DAY_CHECK, startOfDayCheck());
439  config->sync();
440  mStartOfDayChanged = false;
441 }
442 
443 TQString Preferences::emailFrom(Preferences::MailFrom from, bool useAddress, bool bcc)
444 {
445  switch (from)
446  {
447  case MAIL_FROM_KMAIL:
448  return FROM_KMAIL;
449  case MAIL_FROM_CONTROL_CENTRE:
450  return FROM_CONTROL_CENTRE;
451  case MAIL_FROM_ADDR:
452  return useAddress ? (bcc ? mEmailBccAddress : mEmailAddress) : TQString();
453  default:
454  return TQString();
455  }
456 }
457 
458 Preferences::MailFrom Preferences::emailFrom(const TQString& str)
459 {
460  if (str == FROM_KMAIL)
461  return MAIL_FROM_KMAIL;
462  if (str == FROM_CONTROL_CENTRE)
463  return MAIL_FROM_CONTROL_CENTRE;
464  return MAIL_FROM_ADDR;
465 }
466 
467 /******************************************************************************
468 * Get user's default 'From' email address.
469 */
470 TQString Preferences::emailAddress()
471 {
472  switch (mEmailFrom)
473  {
474  case MAIL_FROM_KMAIL:
475  return KAMail::identityManager()->defaultIdentity().fullEmailAddr();
476  case MAIL_FROM_CONTROL_CENTRE:
477  return KAMail::controlCentreAddress();
478  case MAIL_FROM_ADDR:
479  return mEmailAddress;
480  default:
481  return TQString();
482  }
483 }
484 
485 TQString Preferences::emailBccAddress()
486 {
487  switch (mEmailBccFrom)
488  {
489  case MAIL_FROM_CONTROL_CENTRE:
490  return KAMail::controlCentreAddress();
491  case MAIL_FROM_ADDR:
492  return mEmailBccAddress;
493  default:
494  return TQString();
495  }
496 }
497 
498 void Preferences::setEmailAddress(Preferences::MailFrom from, const TQString& address)
499 {
500  switch (from)
501  {
502  case MAIL_FROM_KMAIL:
503  break;
504  case MAIL_FROM_CONTROL_CENTRE:
505  mEmailAddress = KAMail::controlCentreAddress();
506  break;
507  case MAIL_FROM_ADDR:
508  mEmailAddress = address;
509  break;
510  default:
511  return;
512  }
513  mEmailFrom = from;
514 }
515 
516 void Preferences::setEmailBccAddress(bool useControlCentre, const TQString& address)
517 {
518  if (useControlCentre)
519  mEmailBccAddress = KAMail::controlCentreAddress();
520  else
521  mEmailBccAddress = address;
522  mEmailBccFrom = useControlCentre ? MAIL_FROM_CONTROL_CENTRE : MAIL_FROM_ADDR;
523 }
524 
525 /******************************************************************************
526 * Called to allow or suppress output of the specified message dialog, where the
527 * dialog has a checkbox to turn notification off.
528 */
529 void Preferences::setNotify(const TQString& messageID, bool notify)
530 {
531  MessageBox::saveDontShowAgainContinue(messageID, !notify);
532 }
533 
534 /******************************************************************************
535 * Return whether the specified message dialog is output, where the dialog has
536 * a checkbox to turn notification off.
537 * Reply = false if message has been suppressed (by preferences or by selecting
538 * "don't ask again")
539 * = true in all other cases.
540 */
541 bool Preferences::notifying(const TQString& messageID)
542 {
543  return MessageBox::shouldBeShownContinue(messageID);
544 }
545 
546 /******************************************************************************
547 * If the preferences were written by a previous version of KAlarm, do any
548 * necessary conversions.
549 */
550 void Preferences::convertOldPrefs()
551 {
552  TDEConfig* config = TDEGlobal::config();
553  config->setGroup(GENERAL_SECTION);
554  int version = KAlarm::getVersionNumber(config->readEntry(VERSION_NUM));
555  if (version >= KAlarm::Version(1,4,22))
556  return; // config format is up to date
557 
558  if (version <= KAlarm::Version(1,4,21))
559  {
560  // Convert KAlarm 1.4.21 preferences
561  static const TQString OLD_REMIND_UNITS = TQString::fromLatin1("DefRemindUnits");
562  config->setGroup(DEFAULTS_SECTION);
563  int intUnit = config->readNumEntry(OLD_REMIND_UNITS, 0);
564  TQString strUnit = (intUnit == 1) ? TQString::fromLatin1("Days")
565  : (intUnit == 2) ? TQString::fromLatin1("Weeks")
566  : TQString::fromLatin1("HoursMinutes");
567  config->deleteEntry(OLD_REMIND_UNITS);
568  config->writeEntry(DEF_REMIND_UNITS, strUnit);
569  }
570 
571  if (version <= KAlarm::Version(1,4,20))
572  {
573  // Convert KAlarm 1.4.20 preferences
574  static const TQString VIEW_SECTION = TQString::fromLatin1("View");
575  static const TQString SHOW_ARCHIVED_ALARMS = TQString::fromLatin1("ShowArchivedAlarms");
576  static const TQString SHOW_EXPIRED_ALARMS = TQString::fromLatin1("ShowExpiredAlarms");
577  static const TQString SHOW_ALARM_TIME = TQString::fromLatin1("ShowAlarmTime");
578  static const TQString SHOW_TIME_TO_ALARM = TQString::fromLatin1("ShowTimeToAlarm");
579  config->setGroup(GENERAL_SECTION);
580  bool showExpired = config->readBoolEntry(SHOW_EXPIRED_ALARMS, false);
581  bool showTime = config->readBoolEntry(SHOW_ALARM_TIME, true);
582  bool showTimeTo = config->readBoolEntry(SHOW_TIME_TO_ALARM, false);
583  config->deleteEntry(SHOW_EXPIRED_ALARMS);
584  config->deleteEntry(SHOW_ALARM_TIME);
585  config->deleteEntry(SHOW_TIME_TO_ALARM);
586  config->setGroup(VIEW_SECTION);
587  config->writeEntry(SHOW_ARCHIVED_ALARMS, showExpired);
588  config->writeEntry(SHOW_ALARM_TIME, showTime);
589  config->writeEntry(SHOW_TIME_TO_ALARM, showTimeTo);
590  }
591 
592  if (version <= KAlarm::Version(1,4,5))
593  {
594  // Convert KAlarm 1.4.5 preferences
595  static const TQString DEF_SOUND = TQString::fromLatin1("DefSound");
596  config->setGroup(DEFAULTS_SECTION);
597  bool sound = config->readBoolEntry(DEF_SOUND, false);
598  if (!sound)
599  config->writeEntry(DEF_SOUND_TYPE, SoundPicker::NONE);
600  config->deleteEntry(DEF_SOUND);
601  }
602 
603  if (version < KAlarm::Version(1,3,0))
604  {
605  // Convert KAlarm pre-1.3 preferences
606  static const TQString EMAIL_ADDRESS = TQString::fromLatin1("EmailAddress");
607  static const TQString EMAIL_USE_CTRL_CENTRE = TQString::fromLatin1("EmailUseControlCenter");
608  static const TQString EMAIL_BCC_USE_CTRL_CENTRE = TQString::fromLatin1("EmailBccUseControlCenter");
609  TQMap<TQString, TQString> entries = config->entryMap(GENERAL_SECTION);
610  if (entries.find(EMAIL_FROM) == entries.end()
611  && entries.find(EMAIL_USE_CTRL_CENTRE) != entries.end())
612  {
613  // Preferences were written by KAlarm pre-1.2.1
614  config->setGroup(GENERAL_SECTION);
615  bool useCC = false;
616  bool bccUseCC = false;
617  const bool default_emailUseControlCentre = true;
618  const bool default_emailBccUseControlCentre = true;
619  useCC = config->readBoolEntry(EMAIL_USE_CTRL_CENTRE, default_emailUseControlCentre);
620  // EmailBccUseControlCenter was missing in preferences written by KAlarm pre-0.9.5
621  bccUseCC = config->hasKey(EMAIL_BCC_USE_CTRL_CENTRE)
622  ? config->readBoolEntry(EMAIL_BCC_USE_CTRL_CENTRE, default_emailBccUseControlCentre)
623  : useCC;
624  config->writeEntry(EMAIL_FROM, (useCC ? FROM_CONTROL_CENTRE : config->readEntry(EMAIL_ADDRESS)));
625  config->writeEntry(EMAIL_BCC_ADDRESS, (bccUseCC ? FROM_CONTROL_CENTRE : config->readEntry(EMAIL_BCC_ADDRESS)));
626  config->deleteEntry(EMAIL_ADDRESS);
627  config->deleteEntry(EMAIL_BCC_USE_CTRL_CENTRE);
628  config->deleteEntry(EMAIL_USE_CTRL_CENTRE);
629  }
630  // Convert KAlarm 1.2 preferences
631  static const TQString DEF_CMD_XTERM = TQString::fromLatin1("DefCmdXterm");
632  config->setGroup(DEFAULTS_SECTION);
633  if (config->hasKey(DEF_CMD_XTERM))
634  {
635  config->writeEntry(DEF_CMD_LOG_TYPE,
636  (config->readBoolEntry(DEF_CMD_XTERM, false) ? EditAlarmDlg::EXEC_IN_TERMINAL : EditAlarmDlg::DISCARD_OUTPUT));
637  config->deleteEntry(DEF_CMD_XTERM);
638  }
639  }
640  config->setGroup(GENERAL_SECTION);
641  config->writeEntry(VERSION_NUM, KALARM_VERSION);
642  config->sync();
643 }
644 
645 /******************************************************************************
646 * Translate an X terminal command path to/from config file format.
647 * Note that only a home directory specification at the start of the path is
648 * translated, so there's no need to worry about missing out some of the
649 * executable's path due to quotes etc.
650 * N.B. Calling TDEConfig::read/writePathEntry() on the entire command line
651 * causes a crash on some systems, so it's necessary to extract the
652 * executable path first before processing.
653 */
654 TQString translateXTermPath(TDEConfig* config, const TQString& cmdline, bool write)
655 {
656  TQString params;
657  TQString cmd = cmdline;
658  if (cmdline.isEmpty())
659  return cmdline;
660  // Strip any leading quote
661  TQChar quote = cmdline[0];
662  char q = static_cast<char>(quote);
663  bool quoted = (q == '"' || q == '\'');
664  if (quoted)
665  cmd = cmdline.mid(1);
666  // Split the command at the first non-escaped space
667  for (int i = 0, count = cmd.length(); i < count; ++i)
668  {
669  switch (cmd[i].latin1())
670  {
671  case '\\':
672  ++i;
673  continue;
674  case '"':
675  case '\'':
676  if (cmd[i] != quote)
677  continue;
678  // fall through to ' '
679  case ' ':
680  params = cmd.mid(i);
681  cmd = cmd.left(i);
682  break;
683  default:
684  continue;
685  }
686  break;
687  }
688  // Translate any home directory specification at the start of the
689  // executable's path.
690  if (write)
691  {
692  config->writePathEntry(TEMP, cmd);
693  cmd = config->readEntry(TEMP);
694  }
695  else
696  {
697  config->writeEntry(TEMP, cmd);
698  cmd = config->readPathEntry(TEMP);
699  }
700  config->deleteEntry(TEMP);
701  if (quoted)
702  return quote + cmd + params;
703  else
704  return cmd + params;
705 }
miscellaneous functions