SVN_SILENT made messages (.desktop file)
[kdepim.git] / kalarm / preferences.cpp
blob029df45ab45c395e3d826359e0b123e3ff7e4187
1 /*
2 * preferences.cpp - program preference settings
3 * Program: kalarm
4 * Copyright © 2001-2011 by David Jarvie <djarvie@kde.org>
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.
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.
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.
21 #include "kalarm.h"
23 #include "functions.h"
24 #include "kamail.h"
25 #include "messagebox.h"
26 #include "preferences.moc"
28 #include <kalarmcal/identities.h>
30 #include <kpimidentities/identity.h>
31 #include <kpimidentities/identitymanager.h>
32 #include <kholidays/holidays.h>
34 #include <kglobal.h>
35 #include <kconfiggroup.h>
36 #include <kmessagebox.h>
37 #include <ksystemtimezone.h>
38 #include <kdebug.h>
40 #include <QByteArray>
42 #include <time.h>
43 #include <unistd.h>
45 using namespace KHolidays;
46 using namespace KAlarmCal;
48 // Config file entry names
49 static const char* GENERAL_SECTION = "General";
51 // Config file entry name for temporary use
52 static const char* TEMP = "Temp";
54 // Values for EmailFrom entry
55 static const QString FROM_SYS_SETTINGS = QLatin1String("@SystemSettings");
56 static const QString FROM_KMAIL = QLatin1String("@KMail");
58 // Config file entry names for notification messages
59 const QLatin1String Preferences::QUIT_WARN("QuitWarn");
60 const QLatin1String Preferences::ASK_AUTO_START("AskAutoStart");
61 const QLatin1String Preferences::CONFIRM_ALARM_DELETION("ConfirmAlarmDeletion");
62 const QLatin1String Preferences::EMAIL_QUEUED_NOTIFY("EmailQueuedNotify");
63 const bool default_quitWarn = true;
64 const bool default_emailQueuedNotify = false;
65 const bool default_confirmAlarmDeletion = true;
67 static QString translateXTermPath(const QString& cmdline, bool write);
70 Preferences* Preferences::mInstance = 0;
71 bool Preferences::mUsingDefaults = false;
72 KTimeZone Preferences::mSystemTimeZone;
73 HolidayRegion* Preferences::mHolidays = 0; // always non-null after Preferences initialisation
74 QString Preferences::mPreviousVersion;
75 Preferences::Backend Preferences::mPreviousBackend;
76 // Change tracking
77 bool Preferences::mAutoStartChangedByUser = false;
80 Preferences* Preferences::self()
82 if (!mInstance)
84 // Set the default button for the Quit warning message box to Cancel
85 KAMessageBox::setContinueDefault(QUIT_WARN, KMessageBox::Cancel);
86 KAMessageBox::setDefaultShouldBeShownContinue(QUIT_WARN, default_quitWarn);
87 KAMessageBox::setDefaultShouldBeShownContinue(EMAIL_QUEUED_NOTIFY, default_emailQueuedNotify);
88 KAMessageBox::setDefaultShouldBeShownContinue(CONFIRM_ALARM_DELETION, default_confirmAlarmDeletion);
90 mInstance = new Preferences;
92 return mInstance;
95 Preferences::Preferences()
97 QObject::connect(this, SIGNAL(base_StartOfDayChanged(QDateTime)), SLOT(startDayChange(QDateTime)));
98 QObject::connect(this, SIGNAL(base_TimeZoneChanged(QString)), SLOT(timeZoneChange(QString)));
99 QObject::connect(this, SIGNAL(base_HolidayRegionChanged(QString)), SLOT(holidaysChange(QString)));
100 QObject::connect(this, SIGNAL(base_WorkTimeChanged(QDateTime,QDateTime,int)), SLOT(workTimeChange(QDateTime,QDateTime,int)));
102 readConfig();
103 // Fetch the KAlarm version and backend which wrote the previous config file
104 mPreviousVersion = version();
105 mPreviousBackend = backend();
106 // Update the KAlarm version in the config file, but don't call
107 // writeConfig() here - leave it to be written only if the config file
108 // is updated with other data.
109 setVersion(QLatin1String(KALARM_VERSION));
112 void Preferences::setAskAutoStart(bool yes)
114 KAMessageBox::saveDontShowAgainYesNo(ASK_AUTO_START, !yes);
117 /******************************************************************************
118 * Get the user's time zone, or if none has been chosen, the system time zone.
119 * The system time zone is cached, and the cached value will be returned unless
120 * 'reload' is true, in which case the value is re-read from the system.
122 KTimeZone Preferences::timeZone(bool reload)
124 if (reload)
125 mSystemTimeZone = KTimeZone();
126 QString timeZone = self()->mBase_TimeZone;
127 KTimeZone tz;
128 if (!timeZone.isEmpty())
129 tz = KSystemTimeZones::zone(timeZone);
130 if (!tz.isValid())
132 if (!mSystemTimeZone.isValid())
133 mSystemTimeZone = KSystemTimeZones::local();
134 tz = mSystemTimeZone;
136 return tz;
139 void Preferences::setTimeZone(const KTimeZone& tz)
141 self()->setBase_TimeZone(tz.isValid() ? tz.name() : QString());
144 void Preferences::timeZoneChange(const QString& zone)
146 Q_UNUSED(zone);
147 emit mInstance->timeZoneChanged(timeZone(false));
150 const HolidayRegion& Preferences::holidays()
152 QString regionCode = self()->mBase_HolidayRegion;
153 if (!mHolidays || mHolidays->regionCode() != regionCode)
155 delete mHolidays;
156 mHolidays = new HolidayRegion(regionCode);
158 return *mHolidays;
161 void Preferences::setHolidayRegion(const QString& regionCode)
163 self()->setBase_HolidayRegion(regionCode);
166 void Preferences::holidaysChange(const QString& regionCode)
168 Q_UNUSED(regionCode);
169 emit mInstance->holidaysChanged(holidays());
172 void Preferences::setStartOfDay(const QTime& t)
174 if (t != self()->mBase_StartOfDay.time())
176 self()->setBase_StartOfDay(QDateTime(QDate(1900,1,1), t));
177 emit mInstance->startOfDayChanged(t);
181 // Called when the start of day value has changed in the config file
182 void Preferences::startDayChange(const QDateTime& dt)
184 emit mInstance->startOfDayChanged(dt.time());
187 QBitArray Preferences::workDays()
189 unsigned days = self()->base_WorkDays();
190 QBitArray dayBits(7);
191 for (int i = 0; i < 7; ++i)
192 dayBits.setBit(i, days & (1 << i));
193 return dayBits;
196 void Preferences::setWorkDays(const QBitArray& dayBits)
198 unsigned days = 0;
199 for (int i = 0; i < 7; ++i)
200 if (dayBits.testBit(i))
201 days |= 1 << i;
202 self()->setBase_WorkDays(days);
205 void Preferences::workTimeChange(const QDateTime& start, const QDateTime& end, int days)
207 QBitArray dayBits(7);
208 for (int i = 0; i < 7; ++i)
209 if (days & (1 << i))
210 dayBits.setBit(i);
211 emit mInstance->workTimeChanged(start.time(), end.time(), dayBits);
214 Preferences::MailFrom Preferences::emailFrom()
216 QString from = self()->mBase_EmailFrom;
217 if (from == FROM_KMAIL)
218 return MAIL_FROM_KMAIL;
219 if (from == FROM_SYS_SETTINGS)
220 return MAIL_FROM_SYS_SETTINGS;
221 return MAIL_FROM_ADDR;
224 /******************************************************************************
225 * Get user's default 'From' email address.
227 QString Preferences::emailAddress()
229 QString from = self()->mBase_EmailFrom;
230 if (from == FROM_KMAIL)
231 return Identities::identityManager()->defaultIdentity().fullEmailAddr();
232 if (from == FROM_SYS_SETTINGS)
233 return KAMail::controlCentreAddress();
234 return from;
237 void Preferences::setEmailAddress(Preferences::MailFrom from, const QString& address)
239 QString out;
240 switch (from)
242 case MAIL_FROM_KMAIL: out = FROM_KMAIL; break;
243 case MAIL_FROM_SYS_SETTINGS: out = FROM_SYS_SETTINGS; break;
244 case MAIL_FROM_ADDR: out = address; break;
245 default: return;
247 self()->setBase_EmailFrom(out);
250 Preferences::MailFrom Preferences::emailBccFrom()
252 QString from = self()->mBase_EmailBccAddress;
253 if (from == FROM_SYS_SETTINGS)
254 return MAIL_FROM_SYS_SETTINGS;
255 return MAIL_FROM_ADDR;
258 QString Preferences::emailBccAddress()
260 QString from = self()->mBase_EmailBccAddress;
261 if (from == FROM_SYS_SETTINGS)
262 return KAMail::controlCentreAddress();
263 return from;
266 bool Preferences::emailBccUseSystemSettings()
268 return self()->mBase_EmailBccAddress == FROM_SYS_SETTINGS;
271 void Preferences::setEmailBccAddress(bool useSystemSettings, const QString& address)
273 QString out;
274 if (useSystemSettings)
275 out = FROM_SYS_SETTINGS;
276 else
277 out = address;
278 self()->setBase_EmailBccAddress(out);
281 QString Preferences::cmdXTermCommand()
283 return translateXTermPath(self()->mBase_CmdXTermCommand, false);
286 void Preferences::setCmdXTermCommand(const QString& cmd)
288 self()->setBase_CmdXTermCommand(translateXTermPath(cmd, true));
292 void Preferences::connect(const char* signal, const QObject* receiver, const char* member)
294 QObject::connect(self(), signal, receiver, member);
297 /******************************************************************************
298 * Called to allow or suppress output of the specified message dialog, where the
299 * dialog has a checkbox to turn notification off.
301 void Preferences::setNotify(const QString& messageID, bool notify)
303 KAMessageBox::saveDontShowAgainContinue(messageID, !notify);
306 /******************************************************************************
307 * Return whether the specified message dialog is output, where the dialog has
308 * a checkbox to turn notification off.
309 * Reply = false if message has been suppressed (by preferences or by selecting
310 * "don't ask again")
311 * = true in all other cases.
313 bool Preferences::notifying(const QString& messageID)
315 return KAMessageBox::shouldBeShownContinue(messageID);
318 /******************************************************************************
319 * Translate an X terminal command path to/from config file format.
320 * Note that only a home directory specification at the start of the path is
321 * translated, so there's no need to worry about missing out some of the
322 * executable's path due to quotes etc.
323 * N.B. Calling KConfig::read/writePathEntry() on the entire command line
324 * causes a crash on some systems, so it's necessary to extract the
325 * executable path first before processing.
327 QString translateXTermPath(const QString& cmdline, bool write)
329 QString params;
330 QString cmd = cmdline;
331 if (cmdline.isEmpty())
332 return cmdline;
333 // Strip any leading quote
334 QChar quote = cmdline[0];
335 char q = quote.toLatin1();
336 bool quoted = (q == '"' || q == '\'');
337 if (quoted)
338 cmd = cmdline.mid(1);
339 // Split the command at the first non-escaped space
340 for (int i = 0, count = cmd.length(); i < count; ++i)
342 switch (cmd[i].toLatin1())
344 case '\\':
345 ++i;
346 continue;
347 case '"':
348 case '\'':
349 if (cmd[i] != quote)
350 continue;
351 // fall through to ' '
352 case ' ':
353 params = cmd.mid(i);
354 cmd = cmd.left(i);
355 break;
356 default:
357 continue;
359 break;
361 // Translate any home directory specification at the start of the
362 // executable's path.
363 KConfigGroup group(KGlobal::config(), GENERAL_SECTION);
364 if (write)
366 group.writePathEntry(TEMP, cmd);
367 cmd = group.readEntry(TEMP, QString());
369 else
371 group.writeEntry(TEMP, cmd);
372 cmd = group.readPathEntry(TEMP, QString());
374 group.deleteEntry(TEMP);
375 if (quoted)
376 return quote + cmd + params;
377 else
378 return cmd + params;
381 // vim: et sw=4: