• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.12
  • kdepim
  • kalarm
preferences.cpp
Go to the documentation of this file.
1 /*
2  * preferences.cpp - program preference settings
3  * Program: kalarm
4  * Copyright © 2001-2011 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 "functions.h"
24 #include "kamail.h"
25 #include "messagebox.h"
26 #include "preferences.moc"
27 
28 #include <kalarmcal/identities.h>
29 
30 #include <kpimidentities/identity.h>
31 #include <kpimidentities/identitymanager.h>
32 #include <kholidays/holidays.h>
33 
34 #include <kglobal.h>
35 #include <kconfiggroup.h>
36 #include <kmessagebox.h>
37 #include <ksystemtimezone.h>
38 #include <kdebug.h>
39 
40 #include <QByteArray>
41 
42 #include <time.h>
43 #include <unistd.h>
44 
45 using namespace KHolidays;
46 using namespace KAlarmCal;
47 
48 // Config file entry names
49 static const char* GENERAL_SECTION = "General";
50 
51 // Config file entry name for temporary use
52 static const char* TEMP = "Temp";
53 
54 // Values for EmailFrom entry
55 static const QString FROM_SYS_SETTINGS = QLatin1String("@SystemSettings");
56 static const QString FROM_KMAIL = QLatin1String("@KMail");
57 
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;
66 
67 static QString translateXTermPath(const QString& cmdline, bool write);
68 
69 
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;
78 
79 
80 Preferences* Preferences::self()
81 {
82  if (!mInstance)
83  {
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);
89 
90  mInstance = new Preferences;
91  }
92  return mInstance;
93 }
94 
95 Preferences::Preferences()
96 {
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)));
101 
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));
110 }
111 
112 void Preferences::setAskAutoStart(bool yes)
113 {
114  KAMessageBox::saveDontShowAgainYesNo(ASK_AUTO_START, !yes);
115 }
116 
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.
121 */
122 KTimeZone Preferences::timeZone(bool reload)
123 {
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())
131  {
132  if (!mSystemTimeZone.isValid())
133  mSystemTimeZone = KSystemTimeZones::local();
134  tz = mSystemTimeZone;
135  }
136  return tz;
137 }
138 
139 void Preferences::setTimeZone(const KTimeZone& tz)
140 {
141  self()->setBase_TimeZone(tz.isValid() ? tz.name() : QString());
142 }
143 
144 void Preferences::timeZoneChange(const QString& zone)
145 {
146  Q_UNUSED(zone);
147  emit mInstance->timeZoneChanged(timeZone(false));
148 }
149 
150 const HolidayRegion& Preferences::holidays()
151 {
152  QString regionCode = self()->mBase_HolidayRegion;
153  if (!mHolidays || mHolidays->regionCode() != regionCode)
154  {
155  delete mHolidays;
156  mHolidays = new HolidayRegion(regionCode);
157  }
158  return *mHolidays;
159 }
160 
161 void Preferences::setHolidayRegion(const QString& regionCode)
162 {
163  self()->setBase_HolidayRegion(regionCode);
164 }
165 
166 void Preferences::holidaysChange(const QString& regionCode)
167 {
168  Q_UNUSED(regionCode);
169  emit mInstance->holidaysChanged(holidays());
170 }
171 
172 void Preferences::setStartOfDay(const QTime& t)
173 {
174  if (t != self()->mBase_StartOfDay.time())
175  {
176  self()->setBase_StartOfDay(QDateTime(QDate(1900,1,1), t));
177  emit mInstance->startOfDayChanged(t);
178  }
179 }
180 
181 // Called when the start of day value has changed in the config file
182 void Preferences::startDayChange(const QDateTime& dt)
183 {
184  emit mInstance->startOfDayChanged(dt.time());
185 }
186 
187 QBitArray Preferences::workDays()
188 {
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;
194 }
195 
196 void Preferences::setWorkDays(const QBitArray& dayBits)
197 {
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);
203 }
204 
205 void Preferences::workTimeChange(const QDateTime& start, const QDateTime& end, int days)
206 {
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);
212 }
213 
214 Preferences::MailFrom Preferences::emailFrom()
215 {
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;
222 }
223 
224 /******************************************************************************
225 * Get user's default 'From' email address.
226 */
227 QString Preferences::emailAddress()
228 {
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;
235 }
236 
237 void Preferences::setEmailAddress(Preferences::MailFrom from, const QString& address)
238 {
239  QString out;
240  switch (from)
241  {
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;
246  }
247  self()->setBase_EmailFrom(out);
248 }
249 
250 Preferences::MailFrom Preferences::emailBccFrom()
251 {
252  QString from = self()->mBase_EmailBccAddress;
253  if (from == FROM_SYS_SETTINGS)
254  return MAIL_FROM_SYS_SETTINGS;
255  return MAIL_FROM_ADDR;
256 }
257 
258 QString Preferences::emailBccAddress()
259 {
260  QString from = self()->mBase_EmailBccAddress;
261  if (from == FROM_SYS_SETTINGS)
262  return KAMail::controlCentreAddress();
263  return from;
264 }
265 
266 bool Preferences::emailBccUseSystemSettings()
267 {
268  return self()->mBase_EmailBccAddress == FROM_SYS_SETTINGS;
269 }
270 
271 void Preferences::setEmailBccAddress(bool useSystemSettings, const QString& address)
272 {
273  QString out;
274  if (useSystemSettings)
275  out = FROM_SYS_SETTINGS;
276  else
277  out = address;
278  self()->setBase_EmailBccAddress(out);
279 }
280 
281 QString Preferences::cmdXTermCommand()
282 {
283  return translateXTermPath(self()->mBase_CmdXTermCommand, false);
284 }
285 
286 void Preferences::setCmdXTermCommand(const QString& cmd)
287 {
288  self()->setBase_CmdXTermCommand(translateXTermPath(cmd, true));
289 }
290 
291 
292 void Preferences::connect(const char* signal, const QObject* receiver, const char* member)
293 {
294  QObject::connect(self(), signal, receiver, member);
295 }
296 
297 /******************************************************************************
298 * Called to allow or suppress output of the specified message dialog, where the
299 * dialog has a checkbox to turn notification off.
300 */
301 void Preferences::setNotify(const QString& messageID, bool notify)
302 {
303  KAMessageBox::saveDontShowAgainContinue(messageID, !notify);
304 }
305 
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.
312 */
313 bool Preferences::notifying(const QString& messageID)
314 {
315  return KAMessageBox::shouldBeShownContinue(messageID);
316 }
317 
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.
326 */
327 QString translateXTermPath(const QString& cmdline, bool write)
328 {
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)
341  {
342  switch (cmd[i].toLatin1())
343  {
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;
358  }
359  break;
360  }
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)
365  {
366  group.writePathEntry(TEMP, cmd);
367  cmd = group.readEntry(TEMP, QString());
368  }
369  else
370  {
371  group.writeEntry(TEMP, cmd);
372  cmd = group.readPathEntry(TEMP, QString());
373  }
374  group.deleteEntry(TEMP);
375  if (quoted)
376  return quote + cmd + params;
377  else
378  return cmd + params;
379 }
380 
381 // vim: et sw=4:
Preferences::setEmailAddress
static void setEmailAddress(MailFrom, const QString &address)
Definition: preferences.cpp:237
Preferences::setCmdXTermCommand
static void setCmdXTermCommand(const QString &cmd)
Definition: preferences.cpp:286
GENERAL_SECTION
static const char * GENERAL_SECTION
Definition: preferences.cpp:49
KAMail::controlCentreAddress
static QString controlCentreAddress()
Definition: kamail.cpp:467
Preferences::setStartOfDay
static void setStartOfDay(const QTime &)
Definition: preferences.cpp:172
Preferences::MailFrom
MailFrom
Definition: preferences.h:40
TEMP
static const char * TEMP
Definition: preferences.cpp:52
default_confirmAlarmDeletion
const bool default_confirmAlarmDeletion
Definition: preferences.cpp:65
Preferences::setTimeZone
static void setTimeZone(const KTimeZone &)
Definition: preferences.cpp:139
KALARM_VERSION
#define KALARM_VERSION
Definition: kalarm.h:31
PreferencesBase::Backend
Backend
Definition: kalarmconfig.h:20
from
QString from() const
QObject
FROM_KMAIL
static const QString FROM_KMAIL
Definition: preferences.cpp:56
Preferences::setAskAutoStart
static void setAskAutoStart(bool yes)
Definition: preferences.cpp:112
default_quitWarn
const bool default_quitWarn
Definition: preferences.cpp:63
Preferences::cmdXTermCommand
static QString cmdXTermCommand()
Definition: preferences.cpp:281
Preferences::setHolidayRegion
static void setHolidayRegion(const QString &regionCode)
Definition: preferences.cpp:161
Preferences::emailBccFrom
static MailFrom emailBccFrom()
Definition: preferences.cpp:250
Preferences::timeZone
static KTimeZone timeZone(bool reload=false)
Definition: preferences.cpp:122
Preferences::QUIT_WARN
static const QLatin1String QUIT_WARN
Definition: preferences.h:84
Preferences::ASK_AUTO_START
static const QLatin1String ASK_AUTO_START
Definition: preferences.h:85
translateXTermPath
static QString translateXTermPath(const QString &cmdline, bool write)
Definition: preferences.cpp:327
messagebox.h
KAMessageBox::setContinueDefault
static void setContinueDefault(const QString &dontAskAgainName, ButtonCode defaultButton)
Preferences::EMAIL_QUEUED_NOTIFY
static const QLatin1String EMAIL_QUEUED_NOTIFY
Definition: preferences.h:87
Preferences::connect
static void connect(const char *signal, const QObject *receiver, const char *member)
Definition: preferences.cpp:292
KAMessageBox::shouldBeShownContinue
static bool shouldBeShownContinue(const QString &dontShowAgainName)
default_emailQueuedNotify
const bool default_emailQueuedNotify
Definition: preferences.cpp:64
Preferences::holidays
static const KHolidays::HolidayRegion & holidays()
Definition: preferences.cpp:150
Preferences::workDays
static QBitArray workDays()
Definition: preferences.cpp:187
Preferences::CONFIRM_ALARM_DELETION
static const QLatin1String CONFIRM_ALARM_DELETION
Definition: preferences.h:86
kamail.h
Preferences::setWorkDays
static void setWorkDays(const QBitArray &)
Definition: preferences.cpp:196
Preferences::emailBccAddress
static QString emailBccAddress()
Definition: preferences.cpp:258
FROM_SYS_SETTINGS
static const QString FROM_SYS_SETTINGS
Definition: preferences.cpp:55
functions.h
miscellaneous functions
Preferences::emailAddress
static QString emailAddress()
Definition: preferences.cpp:227
kalarm.h
KAMessageBox::saveDontShowAgainYesNo
static void saveDontShowAgainYesNo(const QString &dontShowAgainName, bool dontShow=true, ButtonCode result=No)
KAMessageBox::saveDontShowAgainContinue
static void saveDontShowAgainContinue(const QString &dontShowAgainName, bool dontShow=true)
KAMessageBox::setDefaultShouldBeShownContinue
static bool setDefaultShouldBeShownContinue(const QString &dontShowAgainName, bool defaultShow)
Preferences::setEmailBccAddress
static void setEmailBccAddress(bool useSystemSettings, const QString &address)
Definition: preferences.cpp:271
Preferences::self
static Preferences * self()
Definition: preferences.cpp:80
Preferences
Definition: preferences.h:36
Preferences::emailBccUseSystemSettings
static bool emailBccUseSystemSettings()
Definition: preferences.cpp:266
Preferences::emailFrom
static MailFrom emailFrom()
Definition: preferences.cpp:214
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal