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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • 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"
KAutostart::setCommand
void setCommand(const QString &command)
Set the associated command for this autostart service.
Definition: kautostart.cpp:180
KAutostart::~KAutostart
~KAutostart()
Definition: kautostart.cpp:99
KAutostart::removeFromAllowedEnvironments
void removeFromAllowedEnvironments(const QString &environment)
Removes an environment to the list of environments this service may start in.
Definition: kautostart.cpp:301
QString::append
QString & append(QChar ch)
readEntry
KAutostart::StartPhase readEntry(const KConfigGroup &group, const char *key, const KAutostart::StartPhase &aDefault)
Definition: kautostart.cpp:227
KAboutData::appName
QString appName() const
Returns the application's internal name.
Definition: kaboutdata.cpp:678
QByteArray
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KAutostart::Applications
everything else that doesn't belong in the above two categories, including most system tray applicati...
Definition: kautostart.h:121
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
This function is just for convenience.
Definition: kstandarddirs.cpp:2085
KAutostart::addToAllowedEnvironments
void addToAllowedEnvironments(const QString &environment)
Adds an environment to the list of environments this service may start in.
Definition: kautostart.cpp:289
QByteArray::isNull
bool isNull() const
QList::removeAt
void removeAt(int i)
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
kautostart.h
KComponentData::aboutData
const KAboutData * aboutData() const
Returns the about data of this component.
Definition: kcomponentdata.cpp:215
QFile::exists
bool exists() const
KAutostart::setAllowedEnvironments
void setAllowedEnvironments(const QStringList &environments)
Sets the environments this service is allowed to start in.
Definition: kautostart.cpp:279
KAutostart::setVisibleName
void setVisibleName(const QString &entryName)
Sets the user-visible name for this autostart service.
Definition: kautostart.cpp:195
kdesktopfile.h
KAutostart::removeFromExcludedEnvironments
void removeFromExcludedEnvironments(const QString &environment)
Removes an environment to the list of environments this service may not be autostarted in...
Definition: kautostart.cpp:341
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:139
KAutostart::CheckCommand
an executable that is checked for existence by name
Definition: kautostart.h:86
KAutostart::setExcludedEnvironments
void setExcludedEnvironments(const QStringList &environments)
Sets the environments this service is not allowed to start in.
Definition: kautostart.cpp:319
QObject::name
const char * name() const
KAutostart::setCommandToCheck
void setCommandToCheck(const QString &exec)
Sets the executable to check for the existence of when autostarting this service. ...
Definition: kautostart.cpp:215
kglobal.h
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KAutostart::DesktopServices
services that should be available before most interactive applications start but that aren't part of ...
Definition: kautostart.h:115
KConfig::NoGlobals
Cascade to system settings, but omit user's globals.
Definition: kconfig.h:98
KDesktopFile::copyTo
KDesktopFile * copyTo(const QString &file) const
Copies all entries from this config object to a new KDesktopFile object that will save itself to file...
Definition: kdesktopfile.cpp:372
QObject
KAutostart::command
QString command() const
Returns the associated command for this autostart service.
Definition: kautostart.cpp:175
KAutostart::checkAllowedEnvironment
bool checkAllowedEnvironment(const QString &environment) const
Checks whether autostart is allowed in the given environment, depending on allowedEnvironments() and ...
Definition: kautostart.cpp:160
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KAutostart
KAutostart provides a programmatic means to control the state of autostart services on a per-user bas...
Definition: kautostart.h:49
QString
KAutostart::BaseDesktop
the essential desktop services such as panels and window managers
Definition: kautostart.h:108
KAutostart::visibleName
QString visibleName() const
Returns the user-visible name this autostart service is registered as.
Definition: kautostart.cpp:190
QStringList
KAutostart::allowedEnvironments
QStringList allowedEnvironments() const
Returns the list of environments (e.g.
Definition: kautostart.cpp:274
KAutostart::addToExcludedEnvironments
void addToExcludedEnvironments(const QString &environment)
Adds an environment to the list of environments this service may not be autostarted in...
Definition: kautostart.cpp:329
QLatin1Char
KAutostart::startAfter
QString startAfter() const
Returns the name of another service that should be autostarted before this one (if that service would...
Definition: kautostart.cpp:354
KDesktopFile
KDE Desktop File Management.
Definition: kdesktopfile.h:38
KAutostart::StartPhase
StartPhase
Enumerates the various autostart phases that occur during start-up.
Definition: kautostart.h:103
KAutostart::CheckCondition
autostart condition will be checked too (KDE-specific)
Definition: kautostart.h:91
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:53
KConfig
The central class of the KDE configuration data system.
Definition: kconfig.h:70
QLatin1String
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
This function is much like locate.
Definition: kstandarddirs.cpp:2091
KAutostart::autostarts
bool autostarts(const QString &environment=QString(), Conditions check=NoConditions) const
Returns whether or not the service represented by entryName in the autostart system is set to autosta...
Definition: kautostart.cpp:115
kstandarddirs.h
KAutostart::setAutostarts
void setAutostarts(bool autostart)
Sets the given exec to start automatically at login.
Definition: kautostart.cpp:104
KAutostart::setStartPhase
void setStartPhase(StartPhase phase)
Sets the service (by name) this service should be started after.
Definition: kautostart.cpp:251
KGlobal::mainComponent
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:145
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QStringList::indexOf
int indexOf(const QRegExp &rx, int from) const
kaboutdata.h
kcomponentdata.h
KAutostart::isServiceRegistered
static bool isServiceRegistered(const QString &entryName)
Checks whether or not a service by the given name entryName is registered with the autostart system...
Definition: kautostart.cpp:205
KAutostart::commandToCheck
QString commandToCheck() const
Returns the executable to check for when attempting to autostart this service.
Definition: kautostart.cpp:210
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:248
kconfiggroup.h
defaultValue
QString defaultValue(const QString &t)
Definition: kconfig_compiler.cpp:950
KAutostart::excludedEnvironments
QStringList excludedEnvironments() const
Returns the list of environments this service is explicitly not allowed to start in.
Definition: kautostart.cpp:314
KAutostart::startPhase
StartPhase startPhase() const
Returns the autostart phase this service is started in.
Definition: kautostart.cpp:246
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:10 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

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