kinit
autostart.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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()->addResourceDir("autostart", "/etc/xdg/autostart");
00051 KGlobal::dirs()->addResourceType("autostart", "share/autostart");
00052 }
00053
00054 AutoStart::~AutoStart()
00055 {
00056 delete m_startList;
00057 }
00058
00059 void
00060 AutoStart::setPhase(int phase)
00061 {
00062 if (phase > m_phase)
00063 {
00064 m_phase = phase;
00065 m_phasedone = false;
00066 }
00067 }
00068
00069 void AutoStart::setPhaseDone()
00070 {
00071 m_phasedone = true;
00072 }
00073
00074 static QString extractName(QString path)
00075 {
00076 int i = path.findRev('/');
00077 if (i >= 0)
00078 path = path.mid(i+1);
00079 i = path.findRev('.');
00080 if (i >= 0)
00081 path = path.left(i);
00082 return path;
00083 }
00084
00085 static bool startCondition(const QString &condition)
00086 {
00087 if (condition.isEmpty())
00088 return true;
00089
00090 QStringList list = QStringList::split(':', condition, true);
00091 if (list.count() < 4)
00092 return true;
00093 if (list[0].isEmpty() || list[2].isEmpty())
00094 return true;
00095
00096 KConfig config(list[0], true, false);
00097 if (!list[1].isEmpty())
00098 config.setGroup(list[1]);
00099
00100 bool defaultValue = (list[3].lower() == "true");
00101
00102 return config.readBoolEntry(list[2], defaultValue);
00103 }
00104
00105 void
00106 AutoStart::loadAutoStartList()
00107 {
00108 QStringList files = KGlobal::dirs()->findAllResources("autostart", "*.desktop", false, true);
00109
00110 for(QStringList::ConstIterator it = files.begin();
00111 it != files.end();
00112 ++it)
00113 {
00114 KDesktopFile config(*it, true);
00115 if (!startCondition(config.readEntry("X-KDE-autostart-condition")))
00116 continue;
00117 if (!config.tryExec())
00118 continue;
00119 if (config.readBoolEntry("Hidden", false))
00120 continue;
00121
00122 if (config.hasKey("OnlyShowIn"))
00123 {
00124 if (!config.readListEntry("OnlyShowIn", ';').contains("KDE"))
00125 continue;
00126 }
00127 if (config.hasKey("NotShowIn"))
00128 {
00129 if (config.readListEntry("NotShowIn", ';').contains("KDE"))
00130 continue;
00131 }
00132
00133 AutoStartItem *item = new AutoStartItem;
00134 item->name = extractName(*it);
00135 item->service = *it;
00136 item->startAfter = config.readEntry("X-KDE-autostart-after");
00137 if( m_newStartup )
00138 {
00139 item->phase = config.readNumEntry("X-KDE-autostart-phase", 2);
00140 if (item->phase < 0)
00141 item->phase = 0;
00142 }
00143 else
00144 {
00145 item->phase = config.readNumEntry("X-KDE-autostart-phase", 1);
00146 if (item->phase < 1)
00147 item->phase = 1;
00148 }
00149 m_startList->append(item);
00150 }
00151 }
00152
00153 QString
00154 AutoStart::startService()
00155 {
00156 if (m_startList->isEmpty())
00157 return 0;
00158
00159 while(!m_started.isEmpty())
00160 {
00161
00162
00163 QString lastItem = m_started[0];
00164 for(AutoStartItem *item = m_startList->first();
00165 item; item = m_startList->next())
00166 {
00167 if (item->phase == m_phase
00168 && item->startAfter == lastItem)
00169 {
00170 m_started.prepend(item->name);
00171 QString service = item->service;
00172 m_startList->remove();
00173 return service;
00174 }
00175 }
00176 m_started.remove(m_started.begin());
00177 }
00178
00179
00180 AutoStartItem *item;
00181 for(item = m_startList->first();
00182 item; item = m_startList->next())
00183 {
00184 if (item->phase == m_phase
00185 && item->startAfter.isEmpty())
00186 {
00187 m_started.prepend(item->name);
00188 QString service = item->service;
00189 m_startList->remove();
00190 return service;
00191 }
00192 }
00193
00194
00195 for(item = m_startList->first();
00196 item; item = m_startList->next())
00197 {
00198 if (item->phase == m_phase)
00199 {
00200 m_started.prepend(item->name);
00201 QString service = item->service;
00202 m_startList->remove();
00203 return service;
00204 }
00205 }
00206
00207 return 0;
00208 }