• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.3 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • util
kautostart.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 
21 #include "kautostart.h"
22 
23 #include "kaboutdata.h"
24 #include "kglobal.h"
25 #include "kcomponentdata.h"
26 #include "kdesktopfile.h"
27 #include "kstandarddirs.h"
28 #include "kconfiggroup.h"
29 
30 #include <QtCore/QFile>
31 #include <QStringList>
32 
33 class KAutostart::Private
34 {
35  public:
36  Private()
37  : df(0),
38  copyIfNeededChecked(false)
39  {
40  }
41 
42  ~Private()
43  {
44  delete df;
45  }
46 
47  void copyIfNeeded();
48 
49  QString name;
50  KDesktopFile *df;
51  bool copyIfNeededChecked;
52 };
53 
54 void KAutostart::Private::copyIfNeeded()
55 {
56  if (copyIfNeededChecked) {
57  return;
58  }
59 
60  const QString local = KGlobal::dirs()->locateLocal("autostart", name);
61 
62  if (!QFile::exists(local)) {
63  const QString global = KGlobal::dirs()->locate("autostart", name);
64  if (!global.isEmpty()) {
65  KDesktopFile *newDf = df->copyTo(local); Q_UNUSED(newDf)
66  delete df;
67  delete newDf; //Force sync-to-disk
68  df = new KDesktopFile("autostart", name); //Recreate from disk
69  }
70  }
71 
72  copyIfNeededChecked = true;
73 }
74 
75 KAutostart::KAutostart(const QString& entryName, QObject* parent)
76  : QObject(parent),
77  d(new Private)
78 {
79  if (entryName.isEmpty()) {
80  d->name = KGlobal::mainComponent().aboutData()->appName();
81  } else {
82  d->name = entryName;
83  }
84 
85  if (!d->name.endsWith(QLatin1String(".desktop"))) {
86  d->name.append(QString::fromLatin1(".desktop"));
87  }
88 
89  const QString path = KGlobal::dirs()->locate("autostart", d->name);
90  if (path.isEmpty()) {
91  // just a new KDesktopFile, since we have nothing to use
92  d->df = new KDesktopFile("autostart", d->name);
93  d->copyIfNeededChecked = true;
94  } else {
95  d->df = new KDesktopFile("autostart", path);
96  }
97 }
98 
99 KAutostart::~KAutostart()
100 {
101  delete d;
102 }
103 
104 void KAutostart::setAutostarts(bool autostart)
105 {
106  bool currentAutostartState = !d->df->desktopGroup().readEntry("Hidden", false);
107  if (currentAutostartState == autostart) {
108  return;
109  }
110 
111  d->copyIfNeeded();
112  d->df->desktopGroup().writeEntry("Hidden", !autostart);
113 }
114 
115 bool KAutostart::autostarts(const QString& environment, Conditions check) const
116 {
117  // check if this is actually a .desktop file
118  bool starts = d->df->desktopGroup().exists();
119 
120  // check the hidden field
121  starts = starts && !d->df->desktopGroup().readEntry("Hidden", false);
122 
123  if (!environment.isEmpty()) {
124  starts = starts && checkAllowedEnvironment(environment);
125  }
126 
127  if (check & CheckCommand) {
128  starts = starts && d->df->tryExec();
129  }
130 
131  if (check & CheckCondition) {
132  starts = starts && checkStartCondition();
133  }
134 
135  return starts;
136 }
137 
138 bool KAutostart::checkStartCondition() const
139 {
140  QString condition = d->df->desktopGroup().readEntry("X-KDE-autostart-condition");
141  if (condition.isEmpty())
142  return true;
143 
144  const QStringList list = condition.split(QLatin1Char(':'));
145  if (list.count() < 4) {
146  return true;
147  }
148 
149  if (list[0].isEmpty() || list[2].isEmpty()) {
150  return true;
151  }
152 
153  KConfig config(list[0], KConfig::NoGlobals);
154  KConfigGroup cg(&config, list[1]);
155 
156  const bool defaultValue = (list[3].toLower() == QLatin1String("true"));
157  return cg.readEntry(list[2], defaultValue);
158 }
159 
160 bool KAutostart::checkAllowedEnvironment(const QString& environment) const
161 {
162  const QStringList allowed = allowedEnvironments();
163  if (!allowed.isEmpty()) {
164  return allowed.contains(environment);
165  }
166 
167  const QStringList excluded = excludedEnvironments();
168  if (!excluded.isEmpty()) {
169  return !excluded.contains( environment );
170  }
171 
172  return true;
173 }
174 
175 QString KAutostart::command() const
176 {
177  return d->df->desktopGroup().readEntry("Exec", QString());
178 }
179 
180 void KAutostart::setCommand(const QString &command)
181 {
182  if (d->df->desktopGroup().readEntry("Exec", QString()) == command) {
183  return;
184  }
185 
186  d->copyIfNeeded();
187  d->df->desktopGroup().writeEntry("Exec", command);
188 }
189 
190 QString KAutostart::visibleName() const
191 {
192  return d->df->readName();
193 }
194 
195 void KAutostart::setVisibleName(const QString &name)
196 {
197  if (d->df->desktopGroup().readEntry("Name", QString()) == name) {
198  return;
199  }
200 
201  d->copyIfNeeded();
202  d->df->desktopGroup().writeEntry("Name", name);
203 }
204 
205 bool KAutostart::isServiceRegistered(const QString& entryName)
206 {
207  return QFile::exists(KStandardDirs::locate("autostart", entryName + QString::fromLatin1(".desktop")));
208 }
209 
210 QString KAutostart::commandToCheck() const
211 {
212  return d->df->desktopGroup().readPathEntry("TryExec", QString());
213 }
214 
215 void KAutostart::setCommandToCheck(const QString &exec)
216 {
217  if (d->df->desktopGroup().readEntry("TryExec", QString()) == exec) {
218  return;
219  }
220 
221  d->copyIfNeeded();
222  d->df->desktopGroup().writePathEntry("TryExec", exec);
223 }
224 
225 // do not specialize the readEntry template -
226 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100911
227 KAutostart::StartPhase readEntry(const KConfigGroup &group, const char* key, const KAutostart::StartPhase& aDefault)
228 {
229  const QByteArray data = group.readEntry(key, QByteArray());
230 
231  if (data.isNull()) {
232  return aDefault;
233  }
234 
235  if (data == "0" || data == "BaseDesktop") {
236  return KAutostart::BaseDesktop;
237  } else if (data == "1" || data == "DesktopServices") {
238  return KAutostart::DesktopServices;
239  } else if (data == "2" || data == "Applications") {
240  return KAutostart::Applications;
241  }
242 
243  return aDefault;
244 }
245 
246 KAutostart::StartPhase KAutostart::startPhase() const
247 {
248  return readEntry(d->df->desktopGroup(), "X-KDE-autostart-phase", Applications);
249 }
250 
251 void KAutostart::setStartPhase(KAutostart::StartPhase phase)
252 {
253  QString data = QString::fromLatin1("Applications");
254 
255  switch (phase) {
256  case BaseDesktop:
257  data = QString::fromLatin1("BaseDesktop");
258  break;
259  case DesktopServices:
260  data = QString::fromLatin1("DesktopServices");
261  break;
262  case Applications: // This is the default
263  break;
264  }
265 
266  if (d->df->desktopGroup().readEntry("X-KDE-autostart-phase", QString()) == data) {
267  return;
268  }
269 
270  d->copyIfNeeded();
271  d->df->desktopGroup().writeEntry("X-KDE-autostart-phase", data);
272 }
273 
274 QStringList KAutostart::allowedEnvironments() const
275 {
276  return d->df->desktopGroup().readXdgListEntry("OnlyShowIn");
277 }
278 
279 void KAutostart::setAllowedEnvironments(const QStringList& environments)
280 {
281  if (d->df->desktopGroup().readEntry("OnlyShowIn", QStringList()) == environments) {
282  return;
283  }
284 
285  d->copyIfNeeded();
286  d->df->desktopGroup().writeXdgListEntry("OnlyShowIn", environments);
287 }
288 
289 void KAutostart::addToAllowedEnvironments(const QString& environment)
290 {
291  QStringList envs = allowedEnvironments();
292 
293  if (envs.contains(environment)) {
294  return;
295  }
296 
297  envs.append(environment);
298  setAllowedEnvironments(envs);
299 }
300 
301 void KAutostart::removeFromAllowedEnvironments(const QString& environment)
302 {
303  QStringList envs = allowedEnvironments();
304  int index = envs.indexOf(environment);
305 
306  if (index < 0) {
307  return;
308  }
309 
310  envs.removeAt(index);
311  setAllowedEnvironments(envs);
312 }
313 
314 QStringList KAutostart::excludedEnvironments() const
315 {
316  return d->df->desktopGroup().readXdgListEntry("NotShowIn");
317 }
318 
319 void KAutostart::setExcludedEnvironments(const QStringList& environments)
320 {
321  if (d->df->desktopGroup().readEntry("NotShowIn", QStringList()) == environments) {
322  return;
323  }
324 
325  d->copyIfNeeded();
326  d->df->desktopGroup().writeXdgListEntry("NotShowIn", environments);
327 }
328 
329 void KAutostart::addToExcludedEnvironments(const QString& environment)
330 {
331  QStringList envs = excludedEnvironments();
332 
333  if (envs.contains(environment)) {
334  return;
335  }
336 
337  envs.append(environment);
338  setExcludedEnvironments(envs);
339 }
340 
341 void KAutostart::removeFromExcludedEnvironments(const QString& environment)
342 {
343  QStringList envs = excludedEnvironments();
344  int index = envs.indexOf(environment);
345 
346  if (index < 0) {
347  return;
348  }
349 
350  envs.removeAt(index);
351  setExcludedEnvironments(envs);
352 }
353 
354 QString KAutostart::startAfter() const
355 {
356  return d->df->desktopGroup().readEntry("X-KDE-autostart-after");
357 }
358 
359 #include "kautostart.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat May 18 2013 11:36:07 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.3 API Reference

Skip menu "kdelibs-4.10.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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