KInit
autostart.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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/");
00042 KGlobal::dirs()->addResourceType("autostart", "xdgconf-autostart", "/");
00043 KGlobal::dirs()->addResourceType("autostart", 0, "share/autostart");
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)
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 const 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())
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
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
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
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 }