• Skip to content
  • Skip to link menu
KDE 4.2 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     KGlobal::dirs()->addResourceType("xdgconf-autostart", NULL, "autostart/"); // xdg ones
00055     KGlobal::dirs()->addResourceType("autostart", "xdgconf-autostart", "/"); // merge them
00056     KGlobal::dirs()->addResourceType("autostart", NULL, "share/autostart"); // KDE ones are higher priority
00057     if (entryName.isEmpty())
00058     {
00059         // XXX sure that the mainComponent is available at this point?
00060         d->name = KGlobal::mainComponent().aboutData()->appName();
00061     }
00062     else
00063     {
00064         d->name = entryName;
00065     }
00066 
00067     if (!d->name.endsWith(QLatin1String(".desktop")))
00068     {
00069         d->name.append(".desktop");
00070     }
00071 
00072     d->df = new KDesktopFile( "autostart", d->name);
00073 }
00074 
00075 KAutostart::~KAutostart()
00076 {
00077     delete d;
00078 }
00079 
00080 void KAutostart::setAutostarts(bool autostart)
00081 {
00082     d->df->desktopGroup().writeEntry("Hidden", !autostart);
00083 }
00084 
00085 bool KAutostart::autostarts(const QString& environment,
00086                             Conditions check) const
00087 {
00088     // check if this is actually a .desktop file
00089     bool starts = d->df->desktopGroup().exists();
00090 
00091     // check the hidden field
00092     starts &= !d->df->desktopGroup().readEntry("Hidden", false);
00093 
00094     if (!environment.isEmpty())
00095     {
00096         starts &= (allowedEnvironments().indexOf(environment) != -1);
00097     }
00098 
00099     if (check == CheckCommand)
00100     {
00101         starts &= d->df->tryExec();
00102     }
00103 
00104     return starts;
00105 }
00106 
00107 QString KAutostart::command() const
00108 {
00109     return d->df->desktopGroup().readEntry( "Exec", QString() );
00110 }
00111 
00112 void KAutostart::setCommand(const QString& command)
00113 {
00114     d->df->desktopGroup().writeEntry( "Exec", command );
00115 }
00116 
00117 QString KAutostart::visibleName() const
00118 {
00119     return d->df->readName();
00120 }
00121 
00122 void KAutostart::setVisibleName(const QString& name)
00123 {
00124     d->df->desktopGroup().writeEntry( "Name", name );
00125 }
00126 
00127 bool KAutostart::isServiceRegistered(const QString& entryName)
00128 {
00129     return QFile::exists(KStandardDirs::locate("autostart", entryName + ".desktop"));
00130 }
00131 
00132 QString KAutostart::commandToCheck() const
00133 {
00134     return d->df->desktopGroup().readPathEntry( "TryExec", QString() );
00135 }
00136 
00137 void KAutostart::setCommandToCheck(const QString& exec)
00138 {
00139     d->df->desktopGroup().writePathEntry( "TryExec", exec );
00140 }
00141 
00142 // do not specialize the readEntry template -
00143 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100911
00144 KAutostart::StartPhase readEntry(const KConfigGroup &group, const char* key, const KAutostart::StartPhase& aDefault)
00145 {
00146     const QByteArray data = group.readEntry(key, QByteArray());
00147 
00148     if (data.isNull())
00149         return aDefault;
00150 
00151     if (data == "0" || data == "BaseDesktop")
00152         return KAutostart::BaseDesktop;
00153     else if (data == "1" || data == "DesktopServices")
00154         return KAutostart::DesktopServices;
00155     else if (data == "2" || data == "Applications")
00156         return KAutostart::Applications;
00157 
00158     return aDefault;
00159 }
00160 
00161 KAutostart::StartPhase KAutostart::startPhase() const
00162 {
00163     return readEntry(d->df->desktopGroup(), "X-KDE-autostart-phase", Applications);
00164 }
00165 
00166 void KAutostart::setStartPhase(KAutostart::StartPhase phase)
00167 {
00168     QByteArray data = "Applications";
00169 
00170     switch (phase) {
00171         case BaseDesktop:
00172             data = "BaseDesktop";
00173             break;
00174         case DesktopServices:
00175             data = "DesktopServices";
00176             break;
00177         case Applications: // This is the default
00178             break;
00179     }
00180     d->df->desktopGroup().writeEntry( "X-KDE-autostart-phase", data );
00181 }
00182 
00183 QStringList KAutostart::allowedEnvironments() const
00184 {
00185     return d->df->desktopGroup().readXdgListEntry( "OnlyShowIn" );
00186 }
00187 
00188 void KAutostart::setAllowedEnvironments(const QStringList& environments)
00189 {
00190     d->df->desktopGroup().writeXdgListEntry( "OnlyShowIn", environments );
00191 }
00192 
00193 void KAutostart::addToAllowedEnvironments(const QString& environment)
00194 {
00195     QStringList envs = allowedEnvironments();
00196 
00197     if (envs.contains(environment))
00198     {
00199         return;
00200     }
00201 
00202     envs.append(environment);
00203     setAllowedEnvironments(envs);
00204 }
00205 
00206 void KAutostart::removeFromAllowedEnvironments(const QString& environment)
00207 {
00208     QStringList envs = allowedEnvironments();
00209     int index = envs.indexOf(environment);
00210 
00211     if (index < 0)
00212     {
00213         return;
00214     }
00215 
00216     envs.removeAt(index);
00217     setAllowedEnvironments(envs);
00218 }
00219 
00220 QStringList KAutostart::excludedEnvironments() const
00221 {
00222     return d->df->desktopGroup().readXdgListEntry("NotShowIn");
00223 }
00224 
00225 void KAutostart::setExcludedEnvironments(const QStringList& environments)
00226 {
00227     d->df->desktopGroup().writeXdgListEntry("NotShowIn", environments);
00228 }
00229 
00230 void KAutostart::addToExcludedEnvironments(const QString& environment)
00231 {
00232     QStringList envs = excludedEnvironments();
00233 
00234     if (envs.contains(environment))
00235     {
00236         return;
00237     }
00238 
00239     envs.append(environment);
00240     setExcludedEnvironments(envs);
00241 }
00242 
00243 void KAutostart::removeFromExcludedEnvironments(const QString& environment)
00244 {
00245     QStringList envs = excludedEnvironments();
00246     int index = envs.indexOf(environment);
00247 
00248     if (index < 0)
00249     {
00250         return;
00251     }
00252 
00253     envs.removeAt(index);
00254     setExcludedEnvironments(envs);
00255 }
00256 
00257 #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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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