give CSS stylesheets a higher priority in the Loader.
[kdelibs.git] / kinit / autostart.cpp
blobb242e5ab83b1cff675241dfc1d24efd9bf838ae2
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 **/
20 #include "autostart.h"
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kdesktopfile.h>
25 #include <kglobal.h>
26 #include <kstandarddirs.h>
28 class AutoStartItem
30 public:
31 QString name;
32 QString service;
33 QString startAfter;
34 int phase;
37 AutoStart::AutoStart()
38 : m_phase(-1), m_phasedone(false)
40 m_startList = new AutoStartList;
41 KGlobal::dirs()->addResourceType("xdgconf-autostart", NULL, "autostart/"); // xdg ones
42 KGlobal::dirs()->addResourceType("autostart", "xdgconf-autostart", "/"); // merge them
43 KGlobal::dirs()->addResourceType("autostart", 0, "share/autostart"); // KDE ones are higher priority
46 AutoStart::~AutoStart()
48 qDeleteAll(*m_startList);
49 m_startList->clear();
50 delete m_startList;
53 void
54 AutoStart::setPhase(int phase)
56 if (phase > m_phase)
58 m_phase = phase;
59 m_phasedone = false;
63 void AutoStart::setPhaseDone()
65 m_phasedone = true;
68 static QString extractName(QString path) // krazy:exclude=passbyvalue
70 int i = path.lastIndexOf('/');
71 if (i >= 0)
72 path = path.mid(i+1);
73 i = path.lastIndexOf('.');
74 if (i >= 0)
75 path = path.left(i);
76 return path;
79 static bool startCondition(const QString &condition)
81 if (condition.isEmpty())
82 return true;
84 QStringList list = condition.split(':');
85 if (list.count() < 4)
86 return true;
87 if (list[0].isEmpty() || list[2].isEmpty())
88 return true;
90 KConfig config(list[0], KConfig::NoGlobals);
91 KConfigGroup cg(&config, list[1]);
93 bool defaultValue = (list[3].toLower() == "true");
95 return cg.readEntry(list[2], defaultValue);
98 void
99 AutoStart::loadAutoStartList()
101 QStringList files = KGlobal::dirs()->findAllResources("autostart", "*.desktop", KStandardDirs::NoDuplicates);
103 for(QStringList::ConstIterator it = files.begin();
104 it != files.end();
105 ++it)
107 KDesktopFile config(*it);
108 const KConfigGroup grp = config.desktopGroup();
109 if (!startCondition(grp.readEntry("X-KDE-autostart-condition")))
110 continue;
111 if (!config.tryExec())
112 continue;
113 if (grp.readEntry("Hidden", false))
114 continue;
115 if (config.noDisplay()) // handles OnlyShowIn, NotShowIn (and NoDisplay, but that's not used here)
116 continue;
118 AutoStartItem *item = new AutoStartItem;
119 item->name = extractName(*it);
120 item->service = *it;
121 item->startAfter = grp.readEntry("X-KDE-autostart-after");
122 item->phase = grp.readEntry("X-KDE-autostart-phase", 2);
123 if (item->phase < 0)
124 item->phase = 0;
125 m_startList->append(item);
129 QString
130 AutoStart::startService()
132 if (m_startList->isEmpty())
133 return 0;
135 while(!m_started.isEmpty())
138 // Check for items that depend on previously started items
139 QString lastItem = m_started[0];
140 QMutableListIterator<AutoStartItem *> it(*m_startList);
141 while (it.hasNext())
143 AutoStartItem *item = it.next();
144 if (item->phase == m_phase
145 && item->startAfter == lastItem)
147 m_started.prepend(item->name);
148 QString service = item->service;
149 it.remove();
150 delete item;
151 return service;
154 m_started.removeFirst();
157 // Check for items that don't depend on anything
158 AutoStartItem *item;
159 QMutableListIterator<AutoStartItem *> it(*m_startList);
160 while (it.hasNext())
162 item = it.next();
163 if (item->phase == m_phase
164 && item->startAfter.isEmpty())
166 m_started.prepend(item->name);
167 QString service = item->service;
168 it.remove();
169 delete item;
170 return service;
174 // Just start something in this phase
175 it = *m_startList;
176 while (it.hasNext())
178 item = it.next();
179 if (item->phase == m_phase)
181 m_started.prepend(item->name);
182 QString service = item->service;
183 it.remove();
184 delete item;
185 return service;
189 return 0;