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

KDECore

kautostart.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 
00021 #include "kautostart.h"
00022 
00023 #include "kaboutdata.h"
00024 #include "kglobal.h"
00025 #include "kcomponentdata.h"
00026 #include "kdesktopfile.h"
00027 #include "kstandarddirs.h"
00028 #include "kconfiggroup.h"
00029 
00030 #include <QtCore/QFile>
00031 #include <QStringList>
00032 
00033 class KAutostart::Private
00034 {
00035     public:
00036         Private()
00037             : df(0),
00038               copyIfNeededChecked(false)
00039         {
00040         }
00041 
00042         ~Private()
00043         {
00044             delete df;
00045         }
00046 
00047         void copyIfNeeded();
00048 
00049         QString name;
00050         KDesktopFile *df;
00051         bool copyIfNeededChecked;
00052 };
00053 
00054 void KAutostart::Private::copyIfNeeded()
00055 {
00056     if (copyIfNeededChecked) {
00057         return;
00058     }
00059 
00060     const QString local = KGlobal::dirs()->locateLocal("autostart", name);
00061 
00062     if (!QFile::exists(local)) {
00063         const QString global = KGlobal::dirs()->locate("autostart", name);
00064         if (!global.isEmpty()) {
00065             KDesktopFile *newDf = df->copyTo(local); Q_UNUSED(newDf)
00066             delete df;
00067             delete newDf; //Force sync-to-disk
00068             df = new KDesktopFile("autostart", name); //Recreate from disk
00069         }
00070     }
00071 
00072     copyIfNeededChecked = true;
00073 }
00074 
00075 KAutostart::KAutostart(const QString& entryName, QObject* parent)
00076     : QObject(parent),
00077       d(new Private)
00078 {
00079     if (entryName.isEmpty()) {
00080         d->name = KGlobal::mainComponent().aboutData()->appName();
00081     } else {
00082         d->name = entryName;
00083     }
00084 
00085     if (!d->name.endsWith(QLatin1String(".desktop"))) {
00086         d->name.append(QString::fromLatin1(".desktop"));
00087     }
00088 
00089     const QString path = KGlobal::dirs()->locate("autostart", d->name);
00090     if (path.isEmpty()) {
00091         // just a new KDesktopFile, since we have nothing to use
00092         d->df = new KDesktopFile("autostart", d->name);
00093         d->copyIfNeededChecked = true;
00094     } else {
00095         d->df = new KDesktopFile("autostart", path);
00096     }
00097 }
00098 
00099 KAutostart::~KAutostart()
00100 {
00101     delete d;
00102 }
00103 
00104 void KAutostart::setAutostarts(bool autostart)
00105 {
00106     bool currentAutostartState = !d->df->desktopGroup().readEntry("Hidden", false);
00107     if (currentAutostartState == autostart) {
00108         return;
00109     }
00110 
00111     d->copyIfNeeded();
00112     d->df->desktopGroup().writeEntry("Hidden", !autostart);
00113 }
00114 
00115 bool KAutostart::autostarts(const QString& environment, Conditions check) const
00116 {
00117     // check if this is actually a .desktop file
00118     bool starts = d->df->desktopGroup().exists();
00119 
00120     // check the hidden field
00121     starts = starts && !d->df->desktopGroup().readEntry("Hidden", false);
00122 
00123     if (!environment.isEmpty()) {
00124         starts = starts && checkAllowedEnvironment(environment);
00125     }
00126 
00127     if (check & CheckCommand) {
00128         starts = starts && d->df->tryExec();
00129     }
00130 
00131     if (check & CheckCondition) {
00132         starts = starts && checkStartCondition();
00133     }
00134 
00135     return starts;
00136 }
00137 
00138 bool KAutostart::checkStartCondition() const
00139 {
00140     QString condition = d->df->desktopGroup().readEntry("X-KDE-autostart-condition");
00141     if (condition.isEmpty())
00142         return true;
00143 
00144     const QStringList list = condition.split(QLatin1Char(':'));
00145     if (list.count() < 4) {
00146         return true;
00147     }
00148 
00149     if (list[0].isEmpty() || list[2].isEmpty()) {
00150         return true;
00151     }
00152 
00153     KConfig config(list[0], KConfig::NoGlobals);
00154     KConfigGroup cg(&config, list[1]);
00155 
00156     const bool defaultValue = (list[3].toLower() == QLatin1String("true"));
00157     return cg.readEntry(list[2], defaultValue);
00158 }
00159 
00160 bool KAutostart::checkAllowedEnvironment(const QString& environment) const
00161 {
00162     const QStringList allowed = allowedEnvironments();
00163     if (!allowed.isEmpty()) {
00164         return allowed.contains(environment);
00165     }
00166 
00167     const QStringList excluded = excludedEnvironments();
00168     if (!excluded.isEmpty()) {
00169         return !excluded.contains( environment );
00170     }
00171 
00172     return true;
00173 }
00174 
00175 QString KAutostart::command() const
00176 {
00177     return d->df->desktopGroup().readEntry("Exec", QString());
00178 }
00179 
00180 void KAutostart::setCommand(const QString &command)
00181 {
00182     if (d->df->desktopGroup().readEntry("Exec", QString()) == command) {
00183         return;
00184     }
00185 
00186     d->copyIfNeeded();
00187     d->df->desktopGroup().writeEntry("Exec", command);
00188 }
00189 
00190 QString KAutostart::visibleName() const
00191 {
00192     return d->df->readName();
00193 }
00194 
00195 void KAutostart::setVisibleName(const QString &name)
00196 {
00197     if (d->df->desktopGroup().readEntry("Name", QString()) == name) {
00198         return;
00199     }
00200 
00201     d->copyIfNeeded();
00202     d->df->desktopGroup().writeEntry("Name", name);
00203 }
00204 
00205 bool KAutostart::isServiceRegistered(const QString& entryName)
00206 {
00207     return QFile::exists(KStandardDirs::locate("autostart", entryName + QString::fromLatin1(".desktop")));
00208 }
00209 
00210 QString KAutostart::commandToCheck() const
00211 {
00212     return d->df->desktopGroup().readPathEntry("TryExec", QString());
00213 }
00214 
00215 void KAutostart::setCommandToCheck(const QString &exec)
00216 {
00217     if (d->df->desktopGroup().readEntry("TryExec", QString()) == exec) {
00218         return;
00219     }
00220 
00221     d->copyIfNeeded();
00222     d->df->desktopGroup().writePathEntry("TryExec", exec);
00223 }
00224 
00225 // do not specialize the readEntry template -
00226 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100911
00227 KAutostart::StartPhase readEntry(const KConfigGroup &group, const char* key, const KAutostart::StartPhase& aDefault)
00228 {
00229     const QByteArray data = group.readEntry(key, QByteArray());
00230 
00231     if (data.isNull()) {
00232         return aDefault;
00233     }
00234 
00235     if (data == "0" || data == "BaseDesktop") {
00236         return KAutostart::BaseDesktop;
00237     } else if (data == "1" || data == "DesktopServices") {
00238         return KAutostart::DesktopServices;
00239     } else if (data == "2" || data == "Applications") {
00240         return KAutostart::Applications;
00241     }
00242 
00243     return aDefault;
00244 }
00245 
00246 KAutostart::StartPhase KAutostart::startPhase() const
00247 {
00248     return readEntry(d->df->desktopGroup(), "X-KDE-autostart-phase", Applications);
00249 }
00250 
00251 void KAutostart::setStartPhase(KAutostart::StartPhase phase)
00252 {
00253     QString data = QString::fromLatin1("Applications");
00254 
00255     switch (phase) {
00256         case BaseDesktop:
00257             data = QString::fromLatin1("BaseDesktop");
00258             break;
00259         case DesktopServices:
00260             data = QString::fromLatin1("DesktopServices");
00261             break;
00262         case Applications: // This is the default
00263             break;
00264     }
00265 
00266     if (d->df->desktopGroup().readEntry("X-KDE-autostart-phase", QString()) == data) {
00267         return;
00268     }
00269 
00270     d->copyIfNeeded();
00271     d->df->desktopGroup().writeEntry("X-KDE-autostart-phase", data);
00272 }
00273 
00274 QStringList KAutostart::allowedEnvironments() const
00275 {
00276     return d->df->desktopGroup().readXdgListEntry("OnlyShowIn");
00277 }
00278 
00279 void KAutostart::setAllowedEnvironments(const QStringList& environments)
00280 {
00281     if (d->df->desktopGroup().readEntry("OnlyShowIn", QStringList()) == environments) {
00282         return;
00283     }
00284 
00285     d->copyIfNeeded();
00286     d->df->desktopGroup().writeXdgListEntry("OnlyShowIn", environments);
00287 }
00288 
00289 void KAutostart::addToAllowedEnvironments(const QString& environment)
00290 {
00291     QStringList envs = allowedEnvironments();
00292 
00293     if (envs.contains(environment)) {
00294         return;
00295     }
00296 
00297     envs.append(environment);
00298     setAllowedEnvironments(envs);
00299 }
00300 
00301 void KAutostart::removeFromAllowedEnvironments(const QString& environment)
00302 {
00303     QStringList envs = allowedEnvironments();
00304     int index = envs.indexOf(environment);
00305 
00306     if (index < 0) {
00307         return;
00308     }
00309 
00310     envs.removeAt(index);
00311     setAllowedEnvironments(envs);
00312 }
00313 
00314 QStringList KAutostart::excludedEnvironments() const
00315 {
00316     return d->df->desktopGroup().readXdgListEntry("NotShowIn");
00317 }
00318 
00319 void KAutostart::setExcludedEnvironments(const QStringList& environments)
00320 {
00321     if (d->df->desktopGroup().readEntry("NotShowIn", QStringList()) == environments) {
00322         return;
00323     }
00324 
00325     d->copyIfNeeded();
00326     d->df->desktopGroup().writeXdgListEntry("NotShowIn", environments);
00327 }
00328 
00329 void KAutostart::addToExcludedEnvironments(const QString& environment)
00330 {
00331     QStringList envs = excludedEnvironments();
00332 
00333     if (envs.contains(environment)) {
00334         return;
00335     }
00336 
00337     envs.append(environment);
00338     setExcludedEnvironments(envs);
00339 }
00340 
00341 void KAutostart::removeFromExcludedEnvironments(const QString& environment)
00342 {
00343     QStringList envs = excludedEnvironments();
00344     int index = envs.indexOf(environment);
00345 
00346     if (index < 0) {
00347         return;
00348     }
00349 
00350     envs.removeAt(index);
00351     setExcludedEnvironments(envs);
00352 }
00353 
00354 QString KAutostart::startAfter() const
00355 {
00356     return d->df->desktopGroup().readEntry("X-KDE-autostart-after");
00357 }
00358 
00359 #include "kautostart.moc"

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal