• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • 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() : df(0)
00037         {
00038         }
00039 
00040         ~Private()
00041         {
00042             delete df;
00043         }
00044 
00045         QString name;
00046         KDesktopFile* df;
00047 };
00048 
00049 KAutostart::KAutostart(const QString& entryName,
00050            QObject* parent)
00051     : QObject(parent),
00052       d(new Private)
00053 {
00054     if (entryName.isEmpty())
00055     {
00056         // XXX sure that the mainComponent is available at this point?
00057         d->name = KGlobal::mainComponent().aboutData()->appName();
00058     }
00059     else
00060     {
00061         d->name = entryName;
00062     }
00063 
00064     if (!d->name.endsWith(QLatin1String(".desktop")))
00065     {
00066         d->name.append(".desktop");
00067     }
00068 
00069     d->df = new KDesktopFile( "autostart", d->name);
00070 }
00071 
00072 KAutostart::~KAutostart()
00073 {
00074     delete d;
00075 }
00076 
00077 void KAutostart::setAutostarts(bool autostart)
00078 {
00079     d->df->desktopGroup().writeEntry("Hidden", !autostart);
00080 }
00081 
00082 bool KAutostart::autostarts(const QString& environment,
00083                             Conditions check) const
00084 {
00085     // check if this is actually a .desktop file
00086     bool starts = d->df->desktopGroup().exists();
00087 
00088     // check the hidden field
00089     starts = starts && !d->df->desktopGroup().readEntry("Hidden", false);
00090 
00091     if (!environment.isEmpty())
00092     {
00093         starts = starts && checkAllowedEnvironment(environment);
00094     }
00095 
00096     if (check & CheckCommand)
00097     {
00098         starts = starts && d->df->tryExec();
00099     }
00100     if (check & CheckCondition)
00101     {
00102         starts = starts && checkStartCondition();
00103     }
00104 
00105     return starts;
00106 }
00107 
00108 bool KAutostart::checkStartCondition() const
00109 {
00110     QString condition = d->df->desktopGroup().readEntry("X-KDE-autostart-condition");
00111     if (condition.isEmpty())
00112         return true;
00113 
00114     QStringList list = condition.split(':');
00115     if (list.count() < 4)
00116         return true;
00117     if (list[0].isEmpty() || list[2].isEmpty())
00118         return true;
00119 
00120     KConfig config(list[0], KConfig::NoGlobals);
00121     KConfigGroup cg(&config, list[1]);
00122 
00123     bool defaultValue = (list[3].toLower() == "true");
00124 
00125     return cg.readEntry(list[2], defaultValue);
00126 }
00127 
00128 bool KAutostart::checkAllowedEnvironment( const QString& environment ) const
00129 {
00130     QStringList allowed = allowedEnvironments();
00131     if( !allowed.isEmpty())
00132         return allowed.contains( environment );
00133     QStringList excluded = excludedEnvironments();
00134     if( !excluded.isEmpty())
00135         return !excluded.contains( environment );
00136     return true;
00137 }
00138 
00139 QString KAutostart::command() const
00140 {
00141     return d->df->desktopGroup().readEntry( "Exec", QString() );
00142 }
00143 
00144 void KAutostart::setCommand(const QString& command)
00145 {
00146     d->df->desktopGroup().writeEntry( "Exec", command );
00147 }
00148 
00149 QString KAutostart::visibleName() const
00150 {
00151     return d->df->readName();
00152 }
00153 
00154 void KAutostart::setVisibleName(const QString& name)
00155 {
00156     d->df->desktopGroup().writeEntry( "Name", name );
00157 }
00158 
00159 bool KAutostart::isServiceRegistered(const QString& entryName)
00160 {
00161     return QFile::exists(KStandardDirs::locate("autostart", entryName + ".desktop"));
00162 }
00163 
00164 QString KAutostart::commandToCheck() const
00165 {
00166     return d->df->desktopGroup().readPathEntry( "TryExec", QString() );
00167 }
00168 
00169 void KAutostart::setCommandToCheck(const QString& exec)
00170 {
00171     d->df->desktopGroup().writePathEntry( "TryExec", exec );
00172 }
00173 
00174 // do not specialize the readEntry template -
00175 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100911
00176 KAutostart::StartPhase readEntry(const KConfigGroup &group, const char* key, const KAutostart::StartPhase& aDefault)
00177 {
00178     const QByteArray data = group.readEntry(key, QByteArray());
00179 
00180     if (data.isNull())
00181         return aDefault;
00182 
00183     if (data == "0" || data == "BaseDesktop")
00184         return KAutostart::BaseDesktop;
00185     else if (data == "1" || data == "DesktopServices")
00186         return KAutostart::DesktopServices;
00187     else if (data == "2" || data == "Applications")
00188         return KAutostart::Applications;
00189 
00190     return aDefault;
00191 }
00192 
00193 KAutostart::StartPhase KAutostart::startPhase() const
00194 {
00195     return readEntry(d->df->desktopGroup(), "X-KDE-autostart-phase", Applications);
00196 }
00197 
00198 void KAutostart::setStartPhase(KAutostart::StartPhase phase)
00199 {
00200     QByteArray data = "Applications";
00201 
00202     switch (phase) {
00203         case BaseDesktop:
00204             data = "BaseDesktop";
00205             break;
00206         case DesktopServices:
00207             data = "DesktopServices";
00208             break;
00209         case Applications: // This is the default
00210             break;
00211     }
00212     d->df->desktopGroup().writeEntry( "X-KDE-autostart-phase", data );
00213 }
00214 
00215 QStringList KAutostart::allowedEnvironments() const
00216 {
00217     return d->df->desktopGroup().readXdgListEntry( "OnlyShowIn" );
00218 }
00219 
00220 void KAutostart::setAllowedEnvironments(const QStringList& environments)
00221 {
00222     d->df->desktopGroup().writeXdgListEntry( "OnlyShowIn", environments );
00223 }
00224 
00225 void KAutostart::addToAllowedEnvironments(const QString& environment)
00226 {
00227     QStringList envs = allowedEnvironments();
00228 
00229     if (envs.contains(environment))
00230     {
00231         return;
00232     }
00233 
00234     envs.append(environment);
00235     setAllowedEnvironments(envs);
00236 }
00237 
00238 void KAutostart::removeFromAllowedEnvironments(const QString& environment)
00239 {
00240     QStringList envs = allowedEnvironments();
00241     int index = envs.indexOf(environment);
00242 
00243     if (index < 0)
00244     {
00245         return;
00246     }
00247 
00248     envs.removeAt(index);
00249     setAllowedEnvironments(envs);
00250 }
00251 
00252 QStringList KAutostart::excludedEnvironments() const
00253 {
00254     return d->df->desktopGroup().readXdgListEntry("NotShowIn");
00255 }
00256 
00257 void KAutostart::setExcludedEnvironments(const QStringList& environments)
00258 {
00259     d->df->desktopGroup().writeXdgListEntry("NotShowIn", environments);
00260 }
00261 
00262 void KAutostart::addToExcludedEnvironments(const QString& environment)
00263 {
00264     QStringList envs = excludedEnvironments();
00265 
00266     if (envs.contains(environment))
00267     {
00268         return;
00269     }
00270 
00271     envs.append(environment);
00272     setExcludedEnvironments(envs);
00273 }
00274 
00275 void KAutostart::removeFromExcludedEnvironments(const QString& environment)
00276 {
00277     QStringList envs = excludedEnvironments();
00278     int index = envs.indexOf(environment);
00279 
00280     if (index < 0)
00281     {
00282         return;
00283     }
00284 
00285     envs.removeAt(index);
00286     setExcludedEnvironments(envs);
00287 }
00288 
00289 QString KAutostart::startAfter() const
00290 {
00291     return d->df->desktopGroup().readEntry("X-KDE-autostart-after");
00292 }
00293 
00294 #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
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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