~ubuntu-branches/ubuntu/intrepid/gwenview/intrepid

« back to all changes in this revision

Viewing changes to src/gvexternaltoolmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher Martin
  • Date: 2005-04-06 11:33:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050406113306-7zovl7z0io5bacpd
Tags: 1.2.0-1
* New upstream release.
  + Fixes crashes when using "Back" to navigate. (Closes: #301811)
* Enable KIPI support.
* Add a doc-base file for the handbook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
 
2
/*
 
3
Gwenview - A simple image viewer for KDE
 
4
Copyright 2000-2004 Aur�lien G�teau
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
// Qt
 
22
#include <qdir.h>
 
23
 
 
24
// KDE
 
25
#include <kdebug.h>
 
26
#include <kdesktopfile.h>
 
27
#include <kglobal.h>
 
28
#include <kservice.h>
 
29
#include <kstandarddirs.h>
 
30
#include <kurl.h>
 
31
 
 
32
// Local
 
33
#include "gvexternaltoolcontext.h"
 
34
#include "gvexternaltoolmanager.h"
 
35
 
 
36
//#define ENABLE_LOG
 
37
#ifdef ENABLE_LOG
 
38
#define LOG(x) kdDebug() << k_funcinfo << x << endl
 
39
#else
 
40
#define LOG(x) ;
 
41
#endif
 
42
 
 
43
// Helper functions for createContextInternal
 
44
inline bool mimeTypeMatches(const QString& candidate, const QString& reference) {
 
45
        if (reference=="*") return true;
 
46
 
 
47
        if (reference.right(1)=="*") {
 
48
                return candidate.startsWith(reference.left(reference.length()-1));
 
49
        } else {
 
50
                return candidate==reference;
 
51
        }
 
52
}
 
53
 
 
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();
 
59
 
 
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)) {
 
65
                                matchFound=true;
 
66
                                break;
 
67
                        }
 
68
                }
 
69
                if (!matchFound) {
 
70
                        return false;
 
71
                }
 
72
        }
 
73
        return true;
 
74
}
 
75
 
 
76
 
 
77
struct GVExternalToolManagerPrivate {
 
78
        QDict<KDesktopFile> mDesktopFiles;
 
79
        QPtrList<KService> mServices;
 
80
        QString mUserToolDir;
 
81
 
 
82
        
 
83
        GVExternalToolContext* createContextInternal(
 
84
                QObject* parent, const KURL::List& urls, const QStringList& mimeTypes)
 
85
        {
 
86
                bool onlyOneURL=urls.size()==1;
 
87
                
 
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()) {
 
95
                                continue;
 
96
                        }
 
97
                        
 
98
                        QStringList serviceTypes=service->serviceTypes();
 
99
                        if (isSubSetOf(mimeTypes, serviceTypes)) {
 
100
                                selectionServices.append(service);
 
101
                        }
 
102
                }
 
103
                
 
104
                return new GVExternalToolContext(parent, selectionServices, urls);
 
105
        }
 
106
 
 
107
};
 
108
 
 
109
 
 
110
// Helper function for ctor
 
111
void loadDesktopFiles(QDict<KDesktopFile>& dict, const QString& dirString) {
 
112
        QDir dir(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);
 
118
        }
 
119
}
 
120
 
 
121
inline QString addSlash(const QString& _str) {
 
122
        QString str(_str);
 
123
        if (str.right(1)!="/") str.append('/');
 
124
        return str;
 
125
}
 
126
 
 
127
GVExternalToolManager::GVExternalToolManager() {
 
128
        d=new GVExternalToolManagerPrivate;
 
129
 
 
130
        // Getting dirs
 
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);
 
135
        
 
136
        QStringList dirs=KGlobal::dirs()->findDirs("appdata", "tools");
 
137
        LOG("dirs:" << dirs.join(","));
 
138
 
 
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);
 
145
                        continue;
 
146
                }
 
147
                LOG("loading system desktop files from " << *it);
 
148
                loadDesktopFiles(systemDesktopFiles, *it);
 
149
        }
 
150
        QDict<KDesktopFile> userDesktopFiles;
 
151
        loadDesktopFiles(userDesktopFiles, d->mUserToolDir);
 
152
 
 
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);
 
157
        
 
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);
 
163
                }
 
164
                if (df->readBoolEntry("Hidden")) {
 
165
                        delete df;
 
166
                } else {
 
167
                        d->mDesktopFiles.insert(name, df);
 
168
                }
 
169
        }
 
170
 
 
171
        d->mServices.setAutoDelete(true);
 
172
        updateServices();
 
173
}
 
174
 
 
175
 
 
176
GVExternalToolManager::~GVExternalToolManager() {
 
177
        delete d;
 
178
}
 
179
 
 
180
        
 
181
GVExternalToolManager* GVExternalToolManager::instance() {
 
182
        static GVExternalToolManager manager;
 
183
        return &manager;
 
184
}
 
185
 
 
186
 
 
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
 
193
                desktopFile->sync();
 
194
                KService* service=new KService(desktopFile);
 
195
                d->mServices.append(service);
 
196
        }
 
197
}
 
198
 
 
199
 
 
200
QDict<KDesktopFile>& GVExternalToolManager::desktopFiles() const {
 
201
        return d->mDesktopFiles;
 
202
}
 
203
 
 
204
 
 
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);
 
209
        
 
210
        if (desktopFile->isReadOnly()) {
 
211
                delete desktopFile;
 
212
                desktopFile=new KDesktopFile(
 
213
                        QString("%1/%1").arg(d->mUserToolDir).arg(name), false);
 
214
        }
 
215
        desktopFile->writeEntry("Hidden", true);
 
216
        desktopFile->sync();
 
217
        delete desktopFile;
 
218
}
 
219
 
 
220
 
 
221
KDesktopFile* GVExternalToolManager::editSystemDesktopFile(const KDesktopFile* desktopFile) {
 
222
        Q_ASSERT(desktopFile);
 
223
        QFileInfo fi(desktopFile->fileName());
 
224
 
 
225
        QString name=fi.baseName(true);
 
226
        d->mDesktopFiles.remove(QString("%1.desktop").arg(name));
 
227
        
 
228
        return createUserDesktopFile(name);
 
229
}
 
230
 
 
231
 
 
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);  
 
237
 
 
238
        return desktopFile;
 
239
}
 
240
 
 
241
 
 
242
GVExternalToolContext* GVExternalToolManager::createContext(
 
243
        QObject* parent, const KFileItemList* items)
 
244
{
 
245
        KURL::List urls;
 
246
        QStringList mimeTypes;
 
247
 
 
248
        // Create our URL list and a list of the different mime types present in
 
249
        // the selection
 
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);
 
256
                }
 
257
        }
 
258
 
 
259
        return d->createContextInternal(parent, urls, mimeTypes);
 
260
}
 
261
 
 
262
 
 
263
GVExternalToolContext* GVExternalToolManager::createContext(
 
264
        QObject* parent, const KURL& url)
 
265
{
 
266
        KURL::List urls;
 
267
        QStringList mimeTypes;
 
268
        
 
269
        urls.append(url);
 
270
        QString mimeType=KMimeType::findByURL(url, 0, url.isLocalFile(), true)->name();
 
271
        mimeTypes.append(mimeType);
 
272
        
 
273
        return d->createContextInternal(parent, urls, mimeTypes);
 
274
}
 
275