Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / shell / profileengine.cpp
blob5a7eea2eaf24ccc7d92f1db53af4375d9aed2225
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 "profileengine.h"
21 #include <QtCore/QDir>
23 #include <kurl.h>
24 #include <kdebug.h>
25 #include <kglobal.h>
26 #include <kstandarddirs.h>
28 #include <interfaces/iplugin.h>
29 #include "plugincontroller.h"
31 namespace KDevelop
34 class ProfileEnginePrivate
36 public:
37 Profile *m_rootProfile;
40 ProfileEngine::ProfileEngine()
41 : d(new ProfileEnginePrivate)
43 QStringList dirs = KGlobal::dirs()->findDirs("data", "kdevplatform/profiles");
45 d->m_rootProfile = new Profile(0, "KDevelop");
47 QString currPath = "/";
48 QMap<QString, Profile*> passedPaths;
50 for (QStringList::const_iterator it = dirs.constBegin(); it != dirs.constEnd(); ++it)
51 processDir(*it, currPath, passedPaths, d->m_rootProfile);
54 ProfileEngine::~ProfileEngine()
56 delete d->m_rootProfile;
59 void ProfileEngine::processDir(const QString &dir, const QString &currPath, QMap<QString, Profile*> &passedPaths, Profile *root)
61 // kDebug() << "processDir:" << dir << "" << currPath;
63 QDir qDir(dir);
64 QStringList entryList = qDir.entryList(QDir::Dirs);
65 for (QStringList::const_iterator eit = entryList.constBegin(); eit != entryList.constEnd(); ++eit)
67 if ((*eit != "..") && (*eit != "."))
69 QString dirName = *eit;
70 Profile *profile = 0;
71 if (passedPaths.contains(currPath + dirName))
72 profile = passedPaths[currPath + dirName];
73 else
75 profile = new Profile(root, dirName);
76 passedPaths[currPath + dirName] = profile;
78 processDir(dir + *eit + '/', currPath + dirName, passedPaths, profile);
83 KPluginInfo::List ProfileEngine::offers(const QString &profileName, PluginController::PluginType offerType)
85 ProfileListing listing;
86 Profile *profile = 0;
87 getProfileWithListing(listing, &profile, profileName);
89 if (!profile)
90 return KPluginInfo::List();
92 QString constraint = QString::fromLatin1("[X-KDevelop-Version] == %1").arg(KDEVELOP_PLUGIN_VERSION);
93 switch (offerType) {
94 case PluginController::Global:
95 constraint += QString::fromLatin1(" and ( [X-KDevelop-Category] == 'Global'");
96 constraint += QString::fromLatin1(" or [X-KDevelop-Category] == 'Core')");
97 break;
98 case PluginController::Project:
99 constraint += QString::fromLatin1(" and [X-KDevelop-Category] == 'Project'");
100 break;
102 QString constraint_add = "";
103 Profile::EntryList properties = profile->list(Profile::Properties);
104 int i = 0;
105 for (Profile::EntryList::const_iterator it = properties.begin(); it != properties.end(); ++it)
106 constraint_add += QString::fromLatin1(" %1 '%2' in [X-KDevelop-Properties]").
107 arg((i++)==0?"":"or").arg((*it).name);
108 if (!constraint_add.isEmpty())
109 constraint += " and ( " + constraint_add + " ) ";
111 // //BEGIN debug
112 // kDebug() << "Query for Profile:" << endl
113 // << constraint << endl << endl;
114 // //END debug
116 KService::List list = KServiceTypeTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"), constraint);
117 QStringList names;
119 Profile::EntryList disableList = profile->list(Profile::ExplicitDisable);
120 // for (KServiceOfferList::iterator it = list.begin(); it != list.end(); ++it)
121 KService::List::iterator it = list.begin();
122 while (it != list.end())
124 QString name = (*it)->desktopEntryName();
125 names.append(name);
126 if (profile->hasInEntryList(disableList, name))
128 it = list.erase(it);
129 continue;
131 ++it;
134 Profile::EntryList enableList = profile->list(Profile::ExplicitEnable);
135 for (Profile::EntryList::const_iterator it = enableList.begin(); it != enableList.end(); ++it)
137 if (names.contains((*it).name))
138 continue;
139 QString constraint = QString::fromLatin1("[X-KDevelop-Version] == %1").arg(KDEVELOP_PLUGIN_VERSION);
140 constraint += QString::fromLatin1("and [X-KDE-PluginInfo-Name] == '%1'").arg((*it).name);
141 KService::List enable = KServiceTypeTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"), constraint);
142 list += enable;
145 //BEGIN debug
146 // kDebug() << "=============" << endl
147 // << " =============" << endl
148 // << " ============= Plugins for Profile:" << endl;
149 // for (KService::List::const_iterator it = list.begin(); it != list.end(); ++it)
150 // kDebug() << " " << (*it)->name();
151 // kDebug();
152 //END debug
153 KPluginInfo::List pluginList = KPluginInfo::fromServices( list );
154 return pluginList;
157 KPluginInfo::List ProfileEngine::allOffers(PluginController::PluginType offerType)
159 QString constraint = QString::fromLatin1("[X-KDevelop-Version] == %1").arg(KDEVELOP_PLUGIN_VERSION);
160 switch (offerType) {
161 case PluginController::Global: //core and global have been combined
162 constraint += QLatin1String(" and ( [X-KDevelop-Category] == 'Global'");
163 constraint += QLatin1String(" or [X-KDevelop-Category] == 'Core' )");
164 break;
165 case PluginController::Project:
166 constraint += QString::fromLatin1(" and [X-KDevelop-Category] == 'Project'");
167 break;
169 KService::List list = KServiceTypeTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"), constraint);
170 KPluginInfo::List pluginList = KPluginInfo::fromServices( list );
171 return pluginList;
174 void ProfileEngine::getProfileWithListing(ProfileListing &listing, Profile **profile,
175 const QString &profileName)
177 if (profileName == "KDevelop")
178 *profile = d->m_rootProfile;
179 else
181 walkProfiles<ProfileListing>(listing, d->m_rootProfile);
182 *profile = listing.profiles[profileName];
186 KUrl::List ProfileEngine::resources(const QString &profileName, const QString &nameFilter)
188 ProfileListing listing;
189 Profile *profile = 0;
190 getProfileWithListing(listing, &profile, profileName);
192 if (!profile)
193 return KUrl::List();
195 return resources(profile, nameFilter);
198 KUrl::List ProfileEngine::resources(Profile *profile, const QString &nameFilter)
200 return profile->resources(nameFilter);
203 KUrl::List ProfileEngine::resourcesRecursive(const QString &profileName, const QString &nameFilter)
205 ProfileListing listing;
206 Profile *profile = 0;
207 getProfileWithListing(listing, &profile, profileName);
208 KUrl::List resources = profile->resources(nameFilter);
210 ProfileListingEx listingEx(nameFilter);
211 walkProfiles<ProfileListingEx>(listingEx, profile);
213 resources += listingEx.resourceList;
214 return resources;
217 void ProfileEngine::diffProfiles(PluginController::PluginType offerType, const QString &profile1,
218 const QString &profile2, QStringList &unload, KPluginInfo::List &load)
220 KPluginInfo::List offers1 = offers(profile1, offerType);
221 KPluginInfo::List offers2 = offers(profile2, offerType);
223 QStringList offers1List;
224 for (KPluginInfo::List::const_iterator it = offers1.constBegin();
225 it != offers1.constEnd(); ++it)
226 offers1List.append(it->entryPath());
227 QMap<QString, KPluginInfo> offers2List;
229 for (KPluginInfo::List::const_iterator it = offers2.constBegin();
230 it != offers2.constEnd(); ++it)
231 offers2List[it->entryPath()] = *it;
233 // kDebug() << "OLD PROFILE:" << offers1List;
234 // kDebug() << "NEW PROFILE:" << offers2List;
236 for (QStringList::const_iterator it = offers1List.constBegin();
237 it != offers1List.constEnd(); ++it)
239 // kDebug() << "checking:" << *it;
240 if (offers2List.contains(*it))
242 // kDebug() << " keep";
243 offers2.removeAll(offers2List[*it]);
245 else
247 // kDebug() << " unload";
248 unload.append(*it);
251 load = offers2;
254 Profile *ProfileEngine::findProfile(const QString & profileName)
256 Profile *profile;
257 ProfileListing listing;
258 getProfileWithListing(listing, &profile, profileName);
259 return profile;
262 void ProfileEngine::addResource(const QString &profileName, const KUrl &url)
264 ProfileListing listing;
265 Profile *profile = 0;
266 getProfileWithListing(listing, &profile, profileName);
268 if (!profile)
269 return;
271 profile->addResource(url);
274 Profile *ProfileEngine::rootProfile() const
276 return d->m_rootProfile;