2 * preferences.cpp - program preference settings
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.
23 #include "functions.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>
35 #include <kconfiggroup.h>
36 #include <kmessagebox.h>
37 #include <ksystemtimezone.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
;
77 bool Preferences::mAutoStartChangedByUser
= false;
80 Preferences
* Preferences::self()
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
;
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)));
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
)
125 mSystemTimeZone
= KTimeZone();
126 QString timeZone
= self()->mBase_TimeZone
;
128 if (!timeZone
.isEmpty())
129 tz
= KSystemTimeZones::zone(timeZone
);
132 if (!mSystemTimeZone
.isValid())
133 mSystemTimeZone
= KSystemTimeZones::local();
134 tz
= mSystemTimeZone
;
139 void Preferences::setTimeZone(const KTimeZone
& tz
)
141 self()->setBase_TimeZone(tz
.isValid() ? tz
.name() : QString());
144 void Preferences::timeZoneChange(const QString
& zone
)
147 emit mInstance
->timeZoneChanged(timeZone(false));
150 const HolidayRegion
& Preferences::holidays()
152 QString regionCode
= self()->mBase_HolidayRegion
;
153 if (!mHolidays
|| mHolidays
->regionCode() != regionCode
)
156 mHolidays
= new HolidayRegion(regionCode
);
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
));
196 void Preferences::setWorkDays(const QBitArray
& dayBits
)
199 for (int i
= 0; i
< 7; ++i
)
200 if (dayBits
.testBit(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
)
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();
237 void Preferences::setEmailAddress(Preferences::MailFrom from
, const QString
& address
)
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;
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();
266 bool Preferences::emailBccUseSystemSettings()
268 return self()->mBase_EmailBccAddress
== FROM_SYS_SETTINGS
;
271 void Preferences::setEmailBccAddress(bool useSystemSettings
, const QString
& address
)
274 if (useSystemSettings
)
275 out
= FROM_SYS_SETTINGS
;
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
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
)
330 QString cmd
= cmdline
;
331 if (cmdline
.isEmpty())
333 // Strip any leading quote
334 QChar quote
= cmdline
[0];
335 char q
= quote
.toLatin1();
336 bool quoted
= (q
== '"' || q
== '\'');
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())
351 // fall through to ' '
361 // Translate any home directory specification at the start of the
362 // executable's path.
363 KConfigGroup
group(KGlobal::config(), GENERAL_SECTION
);
366 group
.writePathEntry(TEMP
, cmd
);
367 cmd
= group
.readEntry(TEMP
, QString());
371 group
.writeEntry(TEMP
, cmd
);
372 cmd
= group
.readPathEntry(TEMP
, QString());
374 group
.deleteEntry(TEMP
);
376 return quote
+ cmd
+ params
;