1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
4 *
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.
8 *
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.
13 *
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 **/
19
20#define QT_NO_CAST_FROM_ASCII
21#include "autostart.h"
22
23#include <kautostart.h>
24#include <kglobal.h>
25#include <kstandarddirs.h>
26
27class AutoStartItem
28{
29public:
30 QString name;
31 QString service;
32 QString startAfter;
33 int phase;
34};
35
36AutoStart::AutoStart()
37 : m_phase(-1), m_phasedone(false)
38{
39 m_startList = new AutoStartList;
40 KGlobal::dirs()->addResourceType("xdgconf-autostart", NULL, "autostart/"); // xdg ones
41 KGlobal::dirs()->addResourceType("autostart", "xdgconf-autostart", "/"); // merge them
42 KGlobal::dirs()->addResourceType("autostart", 0, "share/autostart"); // KDE ones are higher priority
43}
44
45AutoStart::~AutoStart()
46{
47 qDeleteAll(*m_startList);
48 m_startList->clear();
49 delete m_startList;
50}
51
52void
53AutoStart::setPhase(int phase)
54{
55 if (phase > m_phase)
56 {
57 m_phase = phase;
58 m_phasedone = false;
59 }
60}
61
62void AutoStart::setPhaseDone()
63{
64 m_phasedone = true;
65}
66
67static QString extractName(QString path) // krazy:exclude=passbyvalue
68{
69 int i = path.lastIndexOf(QLatin1Char('/'));
70 if (i >= 0)
71 path = path.mid(i+1);
72 i = path.lastIndexOf(QLatin1Char('.'));
73 if (i >= 0)
74 path = path.left(i);
75 return path;
76}
77
78void
79AutoStart::loadAutoStartList()
80{
81 const QStringList files = KGlobal::dirs()->findAllResources("autostart",
82 QString::fromLatin1("*.desktop"),
83 KStandardDirs::NoDuplicates);
84
85 for(QStringList::ConstIterator it = files.begin();
86 it != files.end();
87 ++it)
88 {
89 KAutostart config(*it);
90 if( !config.autostarts(QString::fromLatin1("KDE"), KAutostart::CheckAll))
91 continue;
92
93 AutoStartItem *item = new AutoStartItem;
94 item->name = extractName(*it);
95 item->service = *it;
96 item->startAfter = config.startAfter();
97 item->phase = config.startPhase();
98 if (item->phase < 0)
99 item->phase = 0;
100 m_startList->append(item);
101 }
102}
103
104QString
105AutoStart::startService()
106{
107 if (m_startList->isEmpty())
108 return QString();
109
110 while(!m_started.isEmpty())
111 {
112
113 // Check for items that depend on previously started items
114 QString lastItem = m_started[0];
115 QMutableListIterator<AutoStartItem *> it(*m_startList);
116 while (it.hasNext())
117 {
118 AutoStartItem *item = it.next();
119 if (item->phase == m_phase
120 && item->startAfter == lastItem)
121 {
122 m_started.prepend(item->name);
123 QString service = item->service;
124 it.remove();
125 delete item;
126 return service;
127 }
128 }
129 m_started.removeFirst();
130 }
131
132 // Check for items that don't depend on anything
133 AutoStartItem *item;
134 QMutableListIterator<AutoStartItem *> it(*m_startList);
135 while (it.hasNext())
136 {
137 item = it.next();
138 if (item->phase == m_phase
139 && item->startAfter.isEmpty())
140 {
141 m_started.prepend(item->name);
142 QString service = item->service;
143 it.remove();
144 delete item;
145 return service;
146 }
147 }
148
149 // Just start something in this phase
150 it = *m_startList;
151 while (it.hasNext())
152 {
153 item = it.next();
154 if (item->phase == m_phase)
155 {
156 m_started.prepend(item->name);
157 QString service = item->service;
158 it.remove();
159 delete item;
160 return service;
161 }
162 }
163
164 return QString();
165}
166