1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
3
Gwenview - A simple image viewer for KDE
4
Copyright 2000-2004 Aur�lien G�teau
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <kdesktopfile.h>
29
#include <kstandarddirs.h>
33
#include "gvexternaltoolcontext.h"
34
#include "gvexternaltoolmanager.h"
38
#define LOG(x) kdDebug() << k_funcinfo << x << endl
43
// Helper functions for createContextInternal
44
inline bool mimeTypeMatches(const QString& candidate, const QString& reference) {
45
if (reference=="*") return true;
47
if (reference.right(1)=="*") {
48
return candidate.startsWith(reference.left(reference.length()-1));
50
return candidate==reference;
54
inline bool isSubSetOf(const QStringList& subSet, const QStringList& set) {
55
// Simple implementation, might need some optimization
56
QStringList::ConstIterator itSubSet=subSet.begin();
57
QStringList::ConstIterator itSetBegin=set.begin();
58
QStringList::ConstIterator itSetEnd=set.end();
60
for (; itSubSet!=subSet.end(); ++itSubSet) {
61
bool matchFound=false;
62
QStringList::ConstIterator itSet=itSetBegin;
63
for (; itSet!=itSetEnd; ++itSet) {
64
if (mimeTypeMatches(*itSubSet, *itSet)) {
77
struct GVExternalToolManagerPrivate {
78
QDict<KDesktopFile> mDesktopFiles;
79
QPtrList<KService> mServices;
83
GVExternalToolContext* createContextInternal(
84
QObject* parent, const KURL::List& urls, const QStringList& mimeTypes)
86
bool onlyOneURL=urls.size()==1;
88
// Only add to selectionServices the services which can handle all the
89
// different mime types present in the selection
90
QPtrList<KService> selectionServices;
91
QPtrListIterator<KService> it(mServices);
92
for (; it.current(); ++it) {
93
KService* service=it.current();
94
if (!onlyOneURL && !service->allowMultipleFiles()) {
98
QStringList serviceTypes=service->serviceTypes();
99
if (isSubSetOf(mimeTypes, serviceTypes)) {
100
selectionServices.append(service);
104
return new GVExternalToolContext(parent, selectionServices, urls);
110
// Helper function for ctor
111
void loadDesktopFiles(QDict<KDesktopFile>& dict, const QString& dirString) {
113
QStringList list=dir.entryList("*.desktop");
114
QStringList::ConstIterator it=list.begin();
115
for (; it!=list.end(); ++it) {
116
KDesktopFile* df=new KDesktopFile( dir.filePath(*it) );
117
dict.insert(*it, df);
121
inline QString addSlash(const QString& _str) {
123
if (str.right(1)!="/") str.append('/');
127
GVExternalToolManager::GVExternalToolManager() {
128
d=new GVExternalToolManagerPrivate;
131
d->mUserToolDir=KGlobal::dirs()->saveLocation("appdata", "tools");
132
d->mUserToolDir=addSlash(d->mUserToolDir);
133
Q_ASSERT(!d->mUserToolDir.isEmpty());
134
LOG("d->mUserToolDir:" << d->mUserToolDir);
136
QStringList dirs=KGlobal::dirs()->findDirs("appdata", "tools");
137
LOG("dirs:" << dirs.join(","));
139
// Loading desktop files
140
QDict<KDesktopFile> systemDesktopFiles;
141
QStringList::ConstIterator it;
142
for (it=dirs.begin(); it!=dirs.end(); ++it) {
143
if (addSlash(*it)==d->mUserToolDir) {
144
LOG("skipping " << *it);
147
LOG("loading system desktop files from " << *it);
148
loadDesktopFiles(systemDesktopFiles, *it);
150
QDict<KDesktopFile> userDesktopFiles;
151
loadDesktopFiles(userDesktopFiles, d->mUserToolDir);
153
// Merge system and user desktop files into our KDesktopFile dictionary
154
d->mDesktopFiles=systemDesktopFiles;
155
d->mDesktopFiles.setAutoDelete(true);
156
QDictIterator<KDesktopFile> itDict(userDesktopFiles);
158
for (; itDict.current(); ++itDict) {
159
QString name=itDict.currentKey();
160
KDesktopFile* df=itDict.current();
161
if (d->mDesktopFiles.find(name)) {
162
d->mDesktopFiles.remove(name);
164
if (df->readBoolEntry("Hidden")) {
167
d->mDesktopFiles.insert(name, df);
171
d->mServices.setAutoDelete(true);
176
GVExternalToolManager::~GVExternalToolManager() {
181
GVExternalToolManager* GVExternalToolManager::instance() {
182
static GVExternalToolManager manager;
187
void GVExternalToolManager::updateServices() {
188
d->mServices.clear();
189
QDictIterator<KDesktopFile> it(d->mDesktopFiles);
190
for (; it.current(); ++it) {
191
KDesktopFile* desktopFile=it.current();
192
// If sync() is not called, KService does not read up to date content
194
KService* service=new KService(desktopFile);
195
d->mServices.append(service);
200
QDict<KDesktopFile>& GVExternalToolManager::desktopFiles() const {
201
return d->mDesktopFiles;
205
void GVExternalToolManager::hideDesktopFile(KDesktopFile* desktopFile) {
206
QFileInfo fi(desktopFile->fileName());
207
QString name=QString("%1.desktop").arg( fi.baseName(true) );
208
d->mDesktopFiles.take(name);
210
if (desktopFile->isReadOnly()) {
212
desktopFile=new KDesktopFile(
213
QString("%1/%1").arg(d->mUserToolDir).arg(name), false);
215
desktopFile->writeEntry("Hidden", true);
221
KDesktopFile* GVExternalToolManager::editSystemDesktopFile(const KDesktopFile* desktopFile) {
222
Q_ASSERT(desktopFile);
223
QFileInfo fi(desktopFile->fileName());
225
QString name=fi.baseName(true);
226
d->mDesktopFiles.remove(QString("%1.desktop").arg(name));
228
return createUserDesktopFile(name);
232
KDesktopFile* GVExternalToolManager::createUserDesktopFile(const QString& name) {
233
Q_ASSERT(!name.isEmpty());
234
KDesktopFile* desktopFile=new KDesktopFile(
235
QString("%1/%1.desktop").arg(d->mUserToolDir).arg(name), false);
236
d->mDesktopFiles.insert(QString("%1.desktop").arg(name), desktopFile);
242
GVExternalToolContext* GVExternalToolManager::createContext(
243
QObject* parent, const KFileItemList* items)
246
QStringList mimeTypes;
248
// Create our URL list and a list of the different mime types present in
250
QPtrListIterator<KFileItem> it(*items);
251
for (; it.current(); ++it) {
252
urls.append(it.current()->url());
253
QString mimeType=it.current()->mimetype();
254
if (!mimeTypes.contains(mimeType)) {
255
mimeTypes.append(mimeType);
259
return d->createContextInternal(parent, urls, mimeTypes);
263
GVExternalToolContext* GVExternalToolManager::createContext(
264
QObject* parent, const KURL& url)
267
QStringList mimeTypes;
270
QString mimeType=KMimeType::findByURL(url, 0, url.isLocalFile(), true)->name();
271
mimeTypes.append(mimeType);
273
return d->createContextInternal(parent, urls, mimeTypes);