Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / shell / profile.cpp
blob38a4d8152cefef27996169b5d196ad4b6af3e792
1 /***************************************************************************
2 * Copyright 2004 Alexander Dymo <adymo@kdevelop.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU Library General Public License as *
6 * published by the Free Software Foundation; either version 2 of the *
7 * License, or (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19 #include "profile.h"
21 #include <QDir>
22 #include <QFileInfo>
24 #include <kdebug.h>
25 #include <kstandarddirs.h>
26 #include <kconfig.h>
27 #include <kio/job.h>
28 #include <kconfiggroup.h>
29 #include <kio/netaccess.h>
30 namespace KDevelop
33 class ProfilePrivate
35 public:
36 Profile *m_parent;
37 QList<Profile*> m_children;
39 QString m_name;
41 QString m_genericName;
42 QString m_description;
44 QStringList m_properties;
45 QStringList m_explicitEnable;
46 QStringList m_explicitDisable;
49 Profile::Profile(Profile *parent, const QString &name)
50 :d(new ProfilePrivate)
52 d->m_parent = parent;
53 d->m_name = name;
54 if (d->m_parent)
55 d->m_parent->addChildProfile(this);
57 QString profileConfig = KStandardDirs::locate("data", "kdevplatform/profiles" + dirName() + "/profile.config");
58 KConfig config(profileConfig);
60 KConfigGroup group = config.group("Information");
61 d->m_genericName = group.readEntry("GenericName");
62 d->m_description = group.readEntry("Description");
64 group = config.group("Properties");
65 d->m_properties = group.readEntry("List", QStringList());
67 group = config.group("Enable");
68 d->m_explicitEnable = group.readEntry("List", QStringList());
70 group = config.group("Disable");
71 d->m_explicitDisable = group.readEntry("List", QStringList());
74 Profile::Profile(Profile *parent, const QString &name, const QString &genericName, const QString &description)
75 :d(new ProfilePrivate)
77 d->m_parent = parent;
78 d->m_name = name;
79 d->m_genericName = genericName;
80 d->m_description = description;
81 if (d->m_parent)
82 d->m_parent->addChildProfile(this);
83 save();
86 Profile::~Profile()
88 for (QList<Profile*>::iterator it = d->m_children.begin(); it != d->m_children.end(); ++it)
89 delete *it;
90 delete d;
93 void Profile::addChildProfile(Profile *profile)
95 d->m_children.append(profile);
98 void Profile::removeChildProfile(Profile *profile)
100 d->m_children.removeAll(profile);
103 QString Profile::dirName() const
105 if (d->m_parent)
106 return d->m_parent->dirName() + '/' + d->m_name;
107 else
108 return "/"/* + d->m_name*/;
111 void Profile::save()
113 QString profileConfig = KStandardDirs::locateLocal("data", "kdevplatform/profiles" + dirName() + "/profile.config");
114 KConfig config(profileConfig);
116 KConfigGroup group = config.group("Information");
117 group.writeEntry("GenericName", d->m_genericName);
118 group.writeEntry("Description", d->m_description);
120 group = config.group("Properties");
121 group.writeEntry("List", d->m_properties);
123 group = config.group("Enable");
124 group.writeEntry("List", d->m_explicitEnable);
126 group = config.group("Disable");
127 group.writeEntry("List", d->m_explicitDisable);
129 config.sync();
132 Profile::EntryList Profile::list(List type)
134 EntryList parentList;
135 if (d->m_parent)
136 parentList = d->m_parent->list(type);
137 EntryList list = parentList;
138 for (EntryList::iterator it = list.begin(); it != list.end(); ++it)
139 (*it).derived = true;
140 QStringList &personalList = listByType(type);
141 for (QStringList::const_iterator it = personalList.begin(); it != personalList.end(); ++it)
142 list.append(Entry(*it, false));
143 return list;
146 void Profile::addEntry(List type, const QString &value)
148 QStringList &list = listByType(type);
149 if (!list.contains(value))
150 list.append(value);
153 void Profile::removeEntry(List type, const QString &value)
155 QStringList &list = listByType(type);
156 list.removeAll(value);
159 QStringList &Profile::listByType(List type)
161 static QStringList dummy;
163 switch (type) {
164 case Properties:
165 return d->m_properties;
166 case ExplicitEnable:
167 return d->m_explicitEnable;
168 case ExplicitDisable:
169 return d->m_explicitDisable;
171 return dummy;
174 bool Profile::hasInEntryList(EntryList &list, const QString& value)
176 for (EntryList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
177 if ((*it).name == value)
178 return true;
179 return false;
182 bool Profile::remove()
184 QStringList dirs = KGlobal::dirs()->findDirs("data", "kdevplatform/profiles" + dirName());
185 if ((dirs.count() == 1) && dirs[0].startsWith(QDir::homePath()))
186 return KIO::NetAccess::del(KUrl(dirs[0]), 0);
187 return false;
190 void Profile::detachFromParent()
192 if (d->m_parent)
193 d->m_parent->removeChildProfile(this);
196 KUrl::List Profile::resources(const QString &nameFilter)
198 QStringList resources;
199 QStringList resourceDirs = KGlobal::dirs()->resourceDirs("data");
200 for (QStringList::const_iterator it = resourceDirs.begin(); it != resourceDirs.end(); ++it)
202 QString dir = *it;
203 dir = dir + "kdevplatform/profiles" + dirName();
205 QDir d(dir);
206 const QFileInfoList infoList = d.entryInfoList(QStringList(nameFilter), QDir::Files);
207 for (int i = 0; i < infoList.count(); ++i)
208 resources.append(infoList.at(i).absoluteFilePath());
211 return KUrl::List(resources);
214 void Profile::addResource(const KUrl &url)
216 QString saveLocation = KGlobal::dirs()->saveLocation("data", "kdevplatform/profiles"+dirName(), true);
217 KIO::FileCopyJob * job = KIO::file_copy(url, KUrl(saveLocation), -1, KIO::Overwrite);
218 job->exec();
221 QList<Profile*> Profile::children() const
223 return d->m_children;
226 Profile *Profile::parent() const
228 return d->m_parent;
231 QString Profile::name() const
233 return d->m_name;
235 QString Profile::genericName() const
237 return d->m_genericName;
239 QString Profile::description() const
241 return d->m_description;