1 /***************************************************************************
2 * Copyright 2004 Alexander Dymo <adymo@kdevelop.org> *
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. *
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. *
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>
26 #include <kstandarddirs.h>
28 #include <interfaces/iplugin.h>
29 #include "plugincontroller.h"
34 class ProfileEnginePrivate
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;
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
;
71 if (passedPaths
.contains(currPath
+ dirName
))
72 profile
= passedPaths
[currPath
+ dirName
];
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
;
87 getProfileWithListing(listing
, &profile
, profileName
);
90 return KPluginInfo::List();
92 QString constraint
= QString::fromLatin1("[X-KDevelop-Version] == %1").arg(KDEVELOP_PLUGIN_VERSION
);
94 case PluginController::Global
:
95 constraint
+= QString::fromLatin1(" and ( [X-KDevelop-Category] == 'Global'");
96 constraint
+= QString::fromLatin1(" or [X-KDevelop-Category] == 'Core')");
98 case PluginController::Project
:
99 constraint
+= QString::fromLatin1(" and [X-KDevelop-Category] == 'Project'");
102 QString constraint_add
= "";
103 Profile::EntryList properties
= profile
->list(Profile::Properties
);
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
+ " ) ";
112 // kDebug() << "Query for Profile:" << endl
113 // << constraint << endl << endl;
116 KService::List list
= KServiceTypeTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"), constraint
);
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();
126 if (profile
->hasInEntryList(disableList
, name
))
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
))
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
);
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();
153 KPluginInfo::List pluginList
= KPluginInfo::fromServices( list
);
157 KPluginInfo::List
ProfileEngine::allOffers(PluginController::PluginType offerType
)
159 QString constraint
= QString::fromLatin1("[X-KDevelop-Version] == %1").arg(KDEVELOP_PLUGIN_VERSION
);
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' )");
165 case PluginController::Project
:
166 constraint
+= QString::fromLatin1(" and [X-KDevelop-Category] == 'Project'");
169 KService::List list
= KServiceTypeTrader::self()->query(QString::fromLatin1("KDevelop/Plugin"), constraint
);
170 KPluginInfo::List pluginList
= KPluginInfo::fromServices( list
);
174 void ProfileEngine::getProfileWithListing(ProfileListing
&listing
, Profile
**profile
,
175 const QString
&profileName
)
177 if (profileName
== "KDevelop")
178 *profile
= d
->m_rootProfile
;
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
);
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
;
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
]);
247 // kDebug() << " unload";
254 Profile
*ProfileEngine::findProfile(const QString
& profileName
)
257 ProfileListing listing
;
258 getProfileWithListing(listing
, &profile
, profileName
);
262 void ProfileEngine::addResource(const QString
&profileName
, const KUrl
&url
)
264 ProfileListing listing
;
265 Profile
*profile
= 0;
266 getProfileWithListing(listing
, &profile
, profileName
);
271 profile
->addResource(url
);
274 Profile
*ProfileEngine::rootProfile() const
276 return d
->m_rootProfile
;