autostart.cpp

00001 /*
00002  *
00003  *  This file is part of the KDE libraries
00004  *  Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
00005  *
00006  * $Id: autostart.cpp 465272 2005-09-29 09:47:40Z mueller $
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Library General Public
00010  *  License version 2 as published by the Free Software Foundation.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  *  Boston, MA 02110-1301, USA.
00021  **/
00022 
00023 #include "autostart.h"
00024 
00025 #include <kconfig.h>
00026 #include <kdesktopfile.h>
00027 #include <kglobal.h>
00028 #include <kstandarddirs.h>
00029 
00030 class AutoStartItem
00031 {
00032 public:
00033    QString name;
00034    QString service;
00035    QString startAfter;
00036    int     phase;
00037 };
00038 
00039 class AutoStartList: public QPtrList<AutoStartItem>
00040 {
00041 public:
00042    AutoStartList() { }
00043 };
00044 
00045 AutoStart::AutoStart()
00046   : m_phase(0), m_phasedone(false)
00047 {
00048   m_startList = new AutoStartList;
00049   m_startList->setAutoDelete(true);
00050   KGlobal::dirs()->addResourceType("autostart", "share/autostart");
00051 }
00052 
00053 AutoStart::~AutoStart()
00054 {
00055     delete m_startList;
00056 }
00057 
00058 void
00059 AutoStart::setPhase(int phase)
00060 {
00061    if (phase > m_phase)
00062    {
00063       m_phase = phase;
00064       m_phasedone = false;
00065    }
00066 }
00067 
00068 void AutoStart::setPhaseDone()
00069 {
00070    m_phasedone = true;
00071 }
00072 
00073 static QString extractName(QString path)
00074 {
00075   int i = path.findRev('/');
00076   if (i >= 0)
00077      path = path.mid(i+1);
00078   i = path.findRev('.');
00079   if (i >= 0)
00080      path = path.left(i);
00081   return path;
00082 }
00083 
00084 static bool startCondition(const QString &condition)
00085 {
00086   if (condition.isEmpty())
00087      return true;
00088 
00089   QStringList list = QStringList::split(':', condition, true);
00090   if (list.count() < 4) 
00091      return true;
00092   if (list[0].isEmpty() || list[2].isEmpty()) 
00093      return true;
00094 
00095   KConfig config(list[0], true, false);
00096   if (!list[1].isEmpty())
00097      config.setGroup(list[1]);
00098 
00099   bool defaultValue = (list[3].lower() == "true");
00100 
00101   return config.readBoolEntry(list[2], defaultValue);
00102 }
00103 
00104 void
00105 AutoStart::loadAutoStartList()
00106 {
00107    QStringList files = KGlobal::dirs()->findAllResources("autostart", "*.desktop", false, true);
00108    
00109    for(QStringList::ConstIterator it = files.begin();
00110        it != files.end();
00111        ++it)
00112    {
00113        KDesktopFile config(*it, true);
00114        if (!startCondition(config.readEntry("X-KDE-autostart-condition")))
00115           continue;
00116        if (!config.tryExec())
00117           continue;
00118        if (config.readBoolEntry("Hidden", false))
00119           continue;
00120        
00121        AutoStartItem *item = new AutoStartItem;
00122        item->name = extractName(*it);
00123        item->service = *it;
00124        item->startAfter = config.readEntry("X-KDE-autostart-after");
00125        item->phase = config.readNumEntry("X-KDE-autostart-phase", 1);
00126        if (item->phase < 1)
00127           item->phase = 1;
00128        m_startList->append(item);
00129    }
00130 } 
00131 
00132 QString
00133 AutoStart::startService()
00134 {
00135    if (m_startList->isEmpty())
00136       return 0;
00137 
00138    while(!m_started.isEmpty())
00139    {
00140 
00141      // Check for items that depend on previously started items
00142      QString lastItem = m_started[0];
00143      for(AutoStartItem *item = m_startList->first(); 
00144          item; item = m_startList->next())
00145      {
00146         if (item->phase == m_phase
00147         &&  item->startAfter == lastItem)
00148         {
00149            m_started.prepend(item->name);
00150            QString service = item->service;
00151            m_startList->remove();
00152            return service;
00153         }
00154      }
00155      m_started.remove(m_started.begin());
00156    }
00157    
00158    // Check for items that don't depend on anything
00159    AutoStartItem *item;
00160    for(item = m_startList->first();
00161        item; item = m_startList->next())
00162    {
00163       if (item->phase == m_phase
00164       &&  item->startAfter.isEmpty())
00165       {
00166          m_started.prepend(item->name);
00167          QString service = item->service;
00168          m_startList->remove();
00169          return service;
00170       }
00171    }
00172 
00173    // Just start something in this phase
00174    for(item = m_startList->first();
00175        item; item = m_startList->next())
00176    {
00177       if (item->phase == m_phase)
00178       {
00179          m_started.prepend(item->name);
00180          QString service = item->service;
00181          m_startList->remove();
00182          return service;
00183       }
00184    }
00185 
00186    return 0;
00187 }
KDE Home | KDE Accessibility Home | Description of Access Keys