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 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
00116 if (grp.hasKey("OnlyShowIn"))
00117 {
00118 if (!grp.readXdgListEntry("OnlyShowIn").contains("KDE"))
00119 continue;
00120 }
00121 if (grp.hasKey("NotShowIn"))
00122 {
00123 if (grp.readXdgListEntry("NotShowIn").contains("KDE"))
00124 continue;
00125 }
00126
00127 AutoStartItem *item = new AutoStartItem;
00128 item->name = extractName(*it);
00129 item->service = *it;
00130 item->startAfter = grp.readEntry("X-KDE-autostart-after");
00131 item->phase = grp.readEntry("X-KDE-autostart-phase", 2);
00132 if (item->phase < 0)
00133 item->phase = 0;
00134 m_startList->append(item);
00135 }
00136 }
00137
00138 QString
00139 AutoStart::startService()
00140 {
00141 if (m_startList->isEmpty())
00142 return 0;
00143
00144 while(!m_started.isEmpty())
00145 {
00146
00147
00148 QString lastItem = m_started[0];
00149 QMutableListIterator<AutoStartItem *> it(*m_startList);
00150 while (it.hasNext())
00151 {
00152 AutoStartItem *item = it.next();
00153 if (item->phase == m_phase
00154 && item->startAfter == lastItem)
00155 {
00156 m_started.prepend(item->name);
00157 QString service = item->service;
00158 it.remove();
00159 delete item;
00160 return service;
00161 }
00162 }
00163 m_started.removeFirst();
00164 }
00165
00166
00167 AutoStartItem *item;
00168 QMutableListIterator<AutoStartItem *> it(*m_startList);
00169 while (it.hasNext())
00170 {
00171 item = it.next();
00172 if (item->phase == m_phase
00173 && item->startAfter.isEmpty())
00174 {
00175 m_started.prepend(item->name);
00176 QString service = item->service;
00177 it.remove();
00178 delete item;
00179 return service;
00180 }
00181 }
00182
00183
00184 it = *m_startList;
00185 while (it.hasNext())
00186 {
00187 item = it.next();
00188 if (item->phase == m_phase)
00189 {
00190 m_started.prepend(item->name);
00191 QString service = item->service;
00192 it.remove();
00193 delete item;
00194 return service;
00195 }
00196 }
00197
00198 return 0;
00199 }