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 534738 2006-04-27 18:04:45Z lunakl $
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( bool new_startup )
00046   : m_newStartup( new_startup ), m_phase( new_startup ? -1 : 0), m_phasedone(false)
00047 {
00048   m_startList = new AutoStartList;
00049   m_startList->setAutoDelete(true);
00050   KGlobal::dirs()->addResourceType("autostart", "share/autostart");
00051   QString xdgdirs = getenv("XDG_CONFIG_DIRS");
00052   if (xdgdirs.isEmpty())
00053         xdgdirs = "/etc/xdg";
00054 
00055   QStringList xdgdirslist = QStringList::split( ':', xdgdirs );
00056   for ( QStringList::Iterator itr = xdgdirslist.begin(); itr != xdgdirslist.end(); ++itr ) {
00057     KGlobal::dirs()->addResourceDir("autostart", (*itr) +"/autostart");
00058   }
00059 }
00060 
00061 AutoStart::~AutoStart()
00062 {
00063     delete m_startList;
00064 }
00065 
00066 void
00067 AutoStart::setPhase(int phase)
00068 {
00069    if (phase > m_phase)
00070    {
00071       m_phase = phase;
00072       m_phasedone = false;
00073    }
00074 }
00075 
00076 void AutoStart::setPhaseDone()
00077 {
00078    m_phasedone = true;
00079 }
00080 
00081 static QString extractName(QString path)
00082 {
00083   int i = path.findRev('/');
00084   if (i >= 0)
00085      path = path.mid(i+1);
00086   i = path.findRev('.');
00087   if (i >= 0)
00088      path = path.left(i);
00089   return path;
00090 }
00091 
00092 static bool startCondition(const QString &condition)
00093 {
00094   if (condition.isEmpty())
00095      return true;
00096 
00097   QStringList list = QStringList::split(':', condition, true);
00098   if (list.count() < 4) 
00099      return true;
00100   if (list[0].isEmpty() || list[2].isEmpty()) 
00101      return true;
00102 
00103   KConfig config(list[0], true, false);
00104   if (!list[1].isEmpty())
00105      config.setGroup(list[1]);
00106 
00107   bool defaultValue = (list[3].lower() == "true");
00108 
00109   return config.readBoolEntry(list[2], defaultValue);
00110 }
00111 
00112 void
00113 AutoStart::loadAutoStartList()
00114 {
00115    QStringList files = KGlobal::dirs()->findAllResources("autostart", "*.desktop", false, true);
00116    
00117    for(QStringList::ConstIterator it = files.begin();
00118        it != files.end();
00119        ++it)
00120    {
00121        KDesktopFile config(*it, true);
00122        if (!startCondition(config.readEntry("X-KDE-autostart-condition")))
00123           continue;
00124        if (!config.tryExec())
00125           continue;
00126        if (config.readBoolEntry("Hidden", false))
00127           continue;
00128 
00129        if (config.hasKey("OnlyShowIn"))
00130        {
00131           if (!config.readListEntry("OnlyShowIn", ';').contains("KDE"))
00132               continue;
00133        }
00134        if (config.hasKey("NotShowIn"))
00135        {
00136            if (config.readListEntry("NotShowIn", ';').contains("KDE"))
00137                continue;
00138        }
00139        
00140        AutoStartItem *item = new AutoStartItem;
00141        item->name = extractName(*it);
00142        item->service = *it;
00143        item->startAfter = config.readEntry("X-KDE-autostart-after");
00144        if( m_newStartup )
00145        {
00146           item->phase = config.readNumEntry("X-KDE-autostart-phase", 2);
00147           if (item->phase < 0)
00148              item->phase = 0;
00149        }
00150        else
00151        {
00152           item->phase = config.readNumEntry("X-KDE-autostart-phase", 1);
00153           if (item->phase < 1)
00154              item->phase = 1;
00155        }
00156        m_startList->append(item);
00157    }
00158 } 
00159 
00160 QString
00161 AutoStart::startService()
00162 {
00163    if (m_startList->isEmpty())
00164       return 0;
00165 
00166    while(!m_started.isEmpty())
00167    {
00168 
00169      // Check for items that depend on previously started items
00170      QString lastItem = m_started[0];
00171      for(AutoStartItem *item = m_startList->first(); 
00172          item; item = m_startList->next())
00173      {
00174         if (item->phase == m_phase
00175         &&  item->startAfter == lastItem)
00176         {
00177            m_started.prepend(item->name);
00178            QString service = item->service;
00179            m_startList->remove();
00180            return service;
00181         }
00182      }
00183      m_started.remove(m_started.begin());
00184    }
00185    
00186    // Check for items that don't depend on anything
00187    AutoStartItem *item;
00188    for(item = m_startList->first();
00189        item; item = m_startList->next())
00190    {
00191       if (item->phase == m_phase
00192       &&  item->startAfter.isEmpty())
00193       {
00194          m_started.prepend(item->name);
00195          QString service = item->service;
00196          m_startList->remove();
00197          return service;
00198       }
00199    }
00200 
00201    // Just start something in this phase
00202    for(item = m_startList->first();
00203        item; item = m_startList->next())
00204    {
00205       if (item->phase == m_phase)
00206       {
00207          m_started.prepend(item->name);
00208          QString service = item->service;
00209          m_startList->remove();
00210          return service;
00211       }
00212    }
00213 
00214    return 0;
00215 }
KDE Home | KDE Accessibility Home | Description of Access Keys