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

KInit

autostart.cpp

Go to the documentation of this file.
00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
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 #include "autostart.h"
00021 
00022 #include <kconfig.h>
00023 #include <kconfiggroup.h>
00024 #include <kdesktopfile.h>
00025 #include <kglobal.h>
00026 #include <kstandarddirs.h>
00027 
00028 class AutoStartItem
00029 {
00030 public:
00031    QString name;
00032    QString service;
00033    QString startAfter;
00034    int     phase;
00035 };
00036 
00037 AutoStart::AutoStart()
00038   : m_phase(-1), m_phasedone(false)
00039 {
00040   m_startList = new AutoStartList;
00041   KGlobal::dirs()->addResourceType("xdgconf-autostart", NULL, "autostart/"); // xdg ones
00042   KGlobal::dirs()->addResourceType("autostart", "xdgconf-autostart", "/"); // merge them
00043   KGlobal::dirs()->addResourceType("autostart", 0, "share/autostart"); // KDE ones are higher priority
00044 }
00045 
00046 AutoStart::~AutoStart()
00047 {
00048   qDeleteAll(*m_startList);
00049   m_startList->clear();
00050   delete m_startList;
00051 }
00052 
00053 void
00054 AutoStart::setPhase(int phase)
00055 {
00056    if (phase > m_phase)
00057    {
00058       m_phase = phase;
00059       m_phasedone = false;
00060    }
00061 }
00062 
00063 void AutoStart::setPhaseDone()
00064 {
00065    m_phasedone = true;
00066 }
00067 
00068 static QString extractName(QString path) // krazy:exclude=passbyvalue
00069 {
00070   int i = path.lastIndexOf('/');
00071   if (i >= 0)
00072      path = path.mid(i+1);
00073   i = path.lastIndexOf('.');
00074   if (i >= 0)
00075      path = path.left(i);
00076   return path;
00077 }
00078 
00079 static bool startCondition(const QString &condition)
00080 {
00081   if (condition.isEmpty())
00082      return true;
00083 
00084   QStringList list = condition.split(':');
00085   if (list.count() < 4)
00086      return true;
00087   if (list[0].isEmpty() || list[2].isEmpty())
00088      return true;
00089 
00090   KConfig config(list[0], KConfig::NoGlobals);
00091   KConfigGroup cg(&config, list[1]);
00092 
00093   bool defaultValue = (list[3].toLower() == "true");
00094 
00095   return cg.readEntry(list[2], defaultValue);
00096 }
00097 
00098 void
00099 AutoStart::loadAutoStartList()
00100 {
00101    QStringList files = KGlobal::dirs()->findAllResources("autostart", "*.desktop", KStandardDirs::NoDuplicates);
00102 
00103    for(QStringList::ConstIterator it = files.begin();
00104        it != files.end();
00105        ++it)
00106    {
00107        KDesktopFile config(*it);
00108        const KConfigGroup grp = config.desktopGroup();
00109        if (!startCondition(grp.readEntry("X-KDE-autostart-condition")))
00110           continue;
00111        if (!config.tryExec())
00112           continue;
00113        if (grp.readEntry("Hidden", false))
00114           continue;
00115        if (config.noDisplay()) // handles OnlyShowIn, NotShowIn (and NoDisplay, but that's not used here)
00116            continue;
00117 
00118        AutoStartItem *item = new AutoStartItem;
00119        item->name = extractName(*it);
00120        item->service = *it;
00121        item->startAfter = grp.readEntry("X-KDE-autostart-after");
00122        item->phase = grp.readEntry("X-KDE-autostart-phase", 2);
00123        if (item->phase < 0)
00124           item->phase = 0;
00125        m_startList->append(item);
00126    }
00127 }
00128 
00129 QString
00130 AutoStart::startService()
00131 {
00132    if (m_startList->isEmpty())
00133       return 0;
00134 
00135    while(!m_started.isEmpty())
00136    {
00137 
00138      // Check for items that depend on previously started items
00139      QString lastItem = m_started[0];
00140      QMutableListIterator<AutoStartItem *> it(*m_startList);
00141      while (it.hasNext())
00142      {
00143         AutoStartItem *item = it.next();
00144         if (item->phase == m_phase
00145         &&  item->startAfter == lastItem)
00146         {
00147            m_started.prepend(item->name);
00148            QString service = item->service;
00149            it.remove();
00150            delete item;
00151            return service;
00152         }
00153      }
00154      m_started.removeFirst();
00155    }
00156 
00157    // Check for items that don't depend on anything
00158    AutoStartItem *item;
00159    QMutableListIterator<AutoStartItem *> it(*m_startList);
00160    while (it.hasNext())
00161    {
00162       item = it.next();
00163       if (item->phase == m_phase
00164       &&  item->startAfter.isEmpty())
00165       {
00166          m_started.prepend(item->name);
00167          QString service = item->service;
00168          it.remove();
00169          delete item;
00170          return service;
00171       }
00172    }
00173 
00174    // Just start something in this phase
00175    it = *m_startList;
00176    while (it.hasNext())
00177    {
00178       item = it.next();
00179       if (item->phase == m_phase)
00180       {
00181          m_started.prepend(item->name);
00182          QString service = item->service;
00183          it.remove();
00184          delete item;
00185          return service;
00186       }
00187    }
00188 
00189    return 0;
00190 }

KInit

Skip menu "KInit"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • 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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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