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

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
preferences.cpp
Go to the documentation of this file.
1 /*
2  * preferences.cpp - program preference settings
3  * Program: kalarm
4  * Copyright © 2001-2016 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.h"
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 <kstandarddirs.h>
39 #include <kdebug.h>
40 
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 * Set the NoAutoStart condition.
119 * On KDE desktops, the "X-KDE-autostart-condition" entry in
120 * kalarm.autostart.desktop references this to determine whether to autostart KAlarm.
121 * On non-KDE desktops, the "X-KDE-autostart-condition" entry in
122 * kalarm.autostart.desktop doesn't have any effect, so that KAlarm will be
123 * autostarted even if it is set not to autostart. Adding a "Hidden" entry to a
124 * user-modifiable copy of the file fixes this.
125 */
126 void Preferences::setNoAutoStart(bool yes)
127 {
128  // Find the existing kalarm.autostart.desktop file, and whether it's writable.
129  QString autostartFile = KStandardDirs::locate("autostart", QLatin1String("kalarm.autostart.desktop"));
130  bool existsRW = (!autostartFile.isEmpty() && KStandardDirs::checkAccess(autostartFile, R_OK | W_OK));
131 
132  // If the existing file isn't writable, create a writable file and get its path.
133  if (!existsRW)
134  autostartFile = KStandardDirs::locateLocal("autostart", QLatin1String("kalarm.autostart.desktop"));
135 
136  // Read the writable file and remove any "Hidden=" entries
137  QFile file(autostartFile);
138  if (file.open(QIODevice::ReadWrite))
139  {
140  bool update = false;
141  QStringList lines;
142  QTextStream stream(&file);
143  stream.setCodec("UTF-8");
144  stream.setAutoDetectUnicode(true);
145  if (existsRW)
146  {
147  lines = stream.readAll().split(QLatin1Char('\n'));
148  for (int i = 0; i < lines.size(); ++i)
149  {
150  const QString line = lines[i].trimmed();
151  if (line.isEmpty())
152  {
153  lines.removeAt(i);
154  --i;
155  }
156  else if (line.startsWith(QLatin1String("Hidden=")))
157  {
158  lines.removeAt(i);
159  update = true;
160  --i;
161  }
162  }
163  }
164 
165  if (yes)
166  {
167  // If a local kalarm.autostart.desktop file exists, add a "Hidden"
168  // entry to prevent autostart from happening. Otherwise, create a file
169  // with only a "Hidden" entry.
170  lines += QLatin1String("Hidden=true");
171  update = true;
172  }
173  else
174  {
175  // Remove any local kalarm.autostart.desktop "Hidden" entry, and if
176  // that was the only entry in the file, remove the file.
177  if (lines.isEmpty())
178  {
179  // The file only contains a "Hidden=" entry.
180  if (file.remove())
181  update = false;
182  }
183  }
184  if (update)
185  {
186  // Write the updated file
187  file.resize(0);
188  stream << lines.join(QLatin1String("\n")) << "\n";
189  }
190  }
191  self()->setBase_NoAutoStart(yes);
192 }
193 
194 /******************************************************************************
195 * Get the user's time zone, or if none has been chosen, the system time zone.
196 * The system time zone is cached, and the cached value will be returned unless
197 * 'reload' is true, in which case the value is re-read from the system.
198 */
199 KTimeZone Preferences::timeZone(bool reload)
200 {
201  if (reload)
202  mSystemTimeZone = KTimeZone();
203  QString timeZone = self()->mBase_TimeZone;
204  KTimeZone tz;
205  if (!timeZone.isEmpty())
206  tz = KSystemTimeZones::zone(timeZone);
207  if (!tz.isValid())
208  {
209  if (!mSystemTimeZone.isValid())
210  mSystemTimeZone = KSystemTimeZones::local();
211  tz = mSystemTimeZone;
212  }
213  return tz;
214 }
215 
216 void Preferences::setTimeZone(const KTimeZone& tz)
217 {
218  self()->setBase_TimeZone(tz.isValid() ? tz.name() : QString());
219 }
220 
221 void Preferences::timeZoneChange(const QString& zone)
222 {
223  Q_UNUSED(zone);
224  emit mInstance->timeZoneChanged(timeZone(false));
225 }
226 
227 const HolidayRegion& Preferences::holidays()
228 {
229  QString regionCode = self()->mBase_HolidayRegion;
230  if (!mHolidays || mHolidays->regionCode() != regionCode)
231  {
232  delete mHolidays;
233  mHolidays = new HolidayRegion(regionCode);
234  }
235  return *mHolidays;
236 }
237 
238 void Preferences::setHolidayRegion(const QString& regionCode)
239 {
240  self()->setBase_HolidayRegion(regionCode);
241 }
242 
243 void Preferences::holidaysChange(const QString& regionCode)
244 {
245  Q_UNUSED(regionCode);
246  emit mInstance->holidaysChanged(holidays());
247 }
248 
249 void Preferences::setStartOfDay(const QTime& t)
250 {
251  if (t != self()->mBase_StartOfDay.time())
252  {
253  self()->setBase_StartOfDay(QDateTime(QDate(1900,1,1), t));
254  emit mInstance->startOfDayChanged(t);
255  }
256 }
257 
258 // Called when the start of day value has changed in the config file
259 void Preferences::startDayChange(const QDateTime& dt)
260 {
261  emit mInstance->startOfDayChanged(dt.time());
262 }
263 
264 QBitArray Preferences::workDays()
265 {
266  unsigned days = self()->base_WorkDays();
267  QBitArray dayBits(7);
268  for (int i = 0; i < 7; ++i)
269  dayBits.setBit(i, days & (1 << i));
270  return dayBits;
271 }
272 
273 void Preferences::setWorkDays(const QBitArray& dayBits)
274 {
275  unsigned days = 0;
276  for (int i = 0; i < 7; ++i)
277  if (dayBits.testBit(i))
278  days |= 1 << i;
279  self()->setBase_WorkDays(days);
280 }
281 
282 void Preferences::workTimeChange(const QDateTime& start, const QDateTime& end, int days)
283 {
284  QBitArray dayBits(7);
285  for (int i = 0; i < 7; ++i)
286  if (days & (1 << i))
287  dayBits.setBit(i);
288  emit mInstance->workTimeChanged(start.time(), end.time(), dayBits);
289 }
290 
291 Preferences::MailFrom Preferences::emailFrom()
292 {
293  QString from = self()->mBase_EmailFrom;
294  if (from == FROM_KMAIL)
295  return MAIL_FROM_KMAIL;
296  if (from == FROM_SYS_SETTINGS)
297  return MAIL_FROM_SYS_SETTINGS;
298  return MAIL_FROM_ADDR;
299 }
300 
301 /******************************************************************************
302 * Get user's default 'From' email address.
303 */
304 QString Preferences::emailAddress()
305 {
306  QString from = self()->mBase_EmailFrom;
307  if (from == FROM_KMAIL)
308  return Identities::identityManager()->defaultIdentity().fullEmailAddr();
309  if (from == FROM_SYS_SETTINGS)
310  return KAMail::controlCentreAddress();
311  return from;
312 }
313 
314 void Preferences::setEmailAddress(Preferences::MailFrom from, const QString& address)
315 {
316  QString out;
317  switch (from)
318  {
319  case MAIL_FROM_KMAIL: out = FROM_KMAIL; break;
320  case MAIL_FROM_SYS_SETTINGS: out = FROM_SYS_SETTINGS; break;
321  case MAIL_FROM_ADDR: out = address; break;
322  default: return;
323  }
324  self()->setBase_EmailFrom(out);
325 }
326 
327 Preferences::MailFrom Preferences::emailBccFrom()
328 {
329  QString from = self()->mBase_EmailBccAddress;
330  if (from == FROM_SYS_SETTINGS)
331  return MAIL_FROM_SYS_SETTINGS;
332  return MAIL_FROM_ADDR;
333 }
334 
335 QString Preferences::emailBccAddress()
336 {
337  QString from = self()->mBase_EmailBccAddress;
338  if (from == FROM_SYS_SETTINGS)
339  return KAMail::controlCentreAddress();
340  return from;
341 }
342 
343 bool Preferences::emailBccUseSystemSettings()
344 {
345  return self()->mBase_EmailBccAddress == FROM_SYS_SETTINGS;
346 }
347 
348 void Preferences::setEmailBccAddress(bool useSystemSettings, const QString& address)
349 {
350  QString out;
351  if (useSystemSettings)
352  out = FROM_SYS_SETTINGS;
353  else
354  out = address;
355  self()->setBase_EmailBccAddress(out);
356 }
357 
358 QString Preferences::cmdXTermCommand()
359 {
360  return translateXTermPath(self()->mBase_CmdXTermCommand, false);
361 }
362 
363 void Preferences::setCmdXTermCommand(const QString& cmd)
364 {
365  self()->setBase_CmdXTermCommand(translateXTermPath(cmd, true));
366 }
367 
368 
369 void Preferences::connect(const char* signal, const QObject* receiver, const char* member)
370 {
371  QObject::connect(self(), signal, receiver, member);
372 }
373 
374 /******************************************************************************
375 * Called to allow or suppress output of the specified message dialog, where the
376 * dialog has a checkbox to turn notification off.
377 */
378 void Preferences::setNotify(const QString& messageID, bool notify)
379 {
380  KAMessageBox::saveDontShowAgainContinue(messageID, !notify);
381 }
382 
383 /******************************************************************************
384 * Return whether the specified message dialog is output, where the dialog has
385 * a checkbox to turn notification off.
386 * Reply = false if message has been suppressed (by preferences or by selecting
387 * "don't ask again")
388 * = true in all other cases.
389 */
390 bool Preferences::notifying(const QString& messageID)
391 {
392  return KAMessageBox::shouldBeShownContinue(messageID);
393 }
394 
395 /******************************************************************************
396 * Translate an X terminal command path to/from config file format.
397 * Note that only a home directory specification at the start of the path is
398 * translated, so there's no need to worry about missing out some of the
399 * executable's path due to quotes etc.
400 * N.B. Calling KConfig::read/writePathEntry() on the entire command line
401 * causes a crash on some systems, so it's necessary to extract the
402 * executable path first before processing.
403 */
404 QString translateXTermPath(const QString& cmdline, bool write)
405 {
406  QString params;
407  QString cmd = cmdline;
408  if (cmdline.isEmpty())
409  return cmdline;
410  // Strip any leading quote
411  QChar quote = cmdline[0];
412  char q = quote.toLatin1();
413  bool quoted = (q == '"' || q == '\'');
414  if (quoted)
415  cmd = cmdline.mid(1);
416  // Split the command at the first non-escaped space
417  for (int i = 0, count = cmd.length(); i < count; ++i)
418  {
419  switch (cmd[i].toLatin1())
420  {
421  case '\\':
422  ++i;
423  continue;
424  case '"':
425  case '\'':
426  if (cmd[i] != quote)
427  continue;
428  // fall through to ' '
429  case ' ':
430  params = cmd.mid(i);
431  cmd = cmd.left(i);
432  break;
433  default:
434  continue;
435  }
436  break;
437  }
438  // Translate any home directory specification at the start of the
439  // executable's path.
440  KConfigGroup group(KGlobal::config(), GENERAL_SECTION);
441  if (write)
442  {
443  group.writePathEntry(TEMP, cmd);
444  cmd = group.readEntry(TEMP, QString());
445  }
446  else
447  {
448  group.writeEntry(TEMP, cmd);
449  cmd = group.readPathEntry(TEMP, QString());
450  }
451  group.deleteEntry(TEMP);
452  if (quoted)
453  return quote + cmd + params;
454  else
455  return cmd + params;
456 }
457 #include "moc_preferences.cpp"
458 // vim: et sw=4:
QTextStream::setCodec
void setCodec(QTextCodec *codec)
Preferences::setEmailAddress
static void setEmailAddress(MailFrom, const QString &address)
Definition: preferences.cpp:314
QTextStream::setAutoDetectUnicode
void setAutoDetectUnicode(bool enabled)
Preferences::setCmdXTermCommand
static void setCmdXTermCommand(const QString &cmd)
Definition: preferences.cpp:363
GENERAL_SECTION
static const char * GENERAL_SECTION
Definition: preferences.cpp:49
KAMail::controlCentreAddress
static QString controlCentreAddress()
Definition: kamail.cpp:471
Preferences::setStartOfDay
static void setStartOfDay(const QTime &)
Definition: preferences.cpp:249
Preferences::MailFrom
MailFrom
Definition: preferences.h:40
QFile::resize
bool resize(qint64 sz)
QFile::remove
bool remove()
TEMP
static const char * TEMP
Definition: preferences.cpp:52
QBitArray::setBit
void setBit(int i)
QChar
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
Preferences::setNoAutoStart
static void setNoAutoStart(bool yes)
Definition: preferences.cpp:126
default_confirmAlarmDeletion
const bool default_confirmAlarmDeletion
Definition: preferences.cpp:65
Preferences::setTimeZone
static void setTimeZone(const KTimeZone &)
Definition: preferences.cpp:216
QList::removeAt
void removeAt(int i)
KALARM_VERSION
#define KALARM_VERSION
Definition: kalarm.h:31
QDateTime::time
QTime time() const
QStringList::join
QString join(const QString &separator) const
from
QString from() const
QTime
FROM_KMAIL
static const QString FROM_KMAIL
Definition: preferences.cpp:56
Preferences::setAskAutoStart
static void setAskAutoStart(bool yes)
Definition: preferences.cpp:112
QFile
QTextStream
QList::size
int size() const
default_quitWarn
const bool default_quitWarn
Definition: preferences.cpp:63
Preferences::cmdXTermCommand
static QString cmdXTermCommand()
Definition: preferences.cpp:358
Preferences::setHolidayRegion
static void setHolidayRegion(const QString &regionCode)
Definition: preferences.cpp:238
Preferences::emailBccFrom
static MailFrom emailBccFrom()
Definition: preferences.cpp:327
Preferences::timeZone
static KTimeZone timeZone(bool reload=false)
Definition: preferences.cpp:199
QObject
Preferences::QUIT_WARN
static const QLatin1String QUIT_WARN
Definition: preferences.h:86
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
Preferences::ASK_AUTO_START
static const QLatin1String ASK_AUTO_START
Definition: preferences.h:87
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QDate
translateXTermPath
static QString translateXTermPath(const QString &cmdline, bool write)
Definition: preferences.cpp:404
messagebox.h
QString
QBitArray
KAMessageBox::setContinueDefault
static void setContinueDefault(const QString &dontAskAgainName, ButtonCode defaultButton)
Preferences::EMAIL_QUEUED_NOTIFY
static const QLatin1String EMAIL_QUEUED_NOTIFY
Definition: preferences.h:89
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QLatin1Char
preferences.h
Preferences::connect
static void connect(const char *signal, const QObject *receiver, const char *member)
Definition: preferences.cpp:369
KAMessageBox::shouldBeShownContinue
static bool shouldBeShownContinue(const QString &dontShowAgainName)
QChar::toLatin1
char toLatin1() const
default_emailQueuedNotify
const bool default_emailQueuedNotify
Definition: preferences.cpp:64
Preferences::holidays
static const KHolidays::HolidayRegion & holidays()
Definition: preferences.cpp:227
Preferences::workDays
static QBitArray workDays()
Definition: preferences.cpp:264
Preferences::CONFIRM_ALARM_DELETION
static const QLatin1String CONFIRM_ALARM_DELETION
Definition: preferences.h:88
kamail.h
Preferences::setWorkDays
static void setWorkDays(const QBitArray &)
Definition: preferences.cpp:273
QString::mid
QString mid(int position, int n) const
Preferences::emailBccAddress
static QString emailBccAddress()
Definition: preferences.cpp:335
FROM_SYS_SETTINGS
static const QString FROM_SYS_SETTINGS
Definition: preferences.cpp:55
QLatin1String
functions.h
miscellaneous functions
Preferences::emailAddress
static QString emailAddress()
Definition: preferences.cpp:304
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)
QString::length
int length() const
QString::left
QString left(int n) const
Preferences::setEmailBccAddress
static void setEmailBccAddress(bool useSystemSettings, const QString &address)
Definition: preferences.cpp:348
Preferences::self
static Preferences * self()
Definition: preferences.cpp:80
Preferences
Definition: preferences.h:36
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QTextStream::readAll
QString readAll()
QBitArray::testBit
bool testBit(int i) const
Preferences::emailBccUseSystemSettings
static bool emailBccUseSystemSettings()
Definition: preferences.cpp:343
QDateTime
Preferences::emailFrom
static MailFrom emailFrom()
Definition: preferences.cpp:291
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 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
  • pimprint

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