-rw-r--r-- | library/mimetype.cpp | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/library/mimetype.cpp b/library/mimetype.cpp index e2e0dab..92fe5e6 100644 --- a/library/mimetype.cpp +++ b/library/mimetype.cpp | |||
@@ -1,308 +1,306 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of the Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #define QTOPIA_INTERNAL_MIMEEXT | 21 | #define QTOPIA_INTERNAL_MIMEEXT |
22 | #include "mimetype.h" | 22 | #include "mimetype.h" |
23 | #include "applnk.h" | 23 | #include "applnk.h" |
24 | #include "resource.h" | 24 | #include "resource.h" |
25 | #include "qpeapplication.h" | 25 | #include "qpeapplication.h" |
26 | #include "config.h" | 26 | #include "config.h" |
27 | 27 | ||
28 | #include <qfile.h> | 28 | #include <qfile.h> |
29 | #include <qdict.h> | 29 | #include <qdict.h> |
30 | #include <qregexp.h> | 30 | #include <qregexp.h> |
31 | #include <qstringlist.h> | 31 | #include <qstringlist.h> |
32 | #include <qtextstream.h> | 32 | #include <qtextstream.h> |
33 | #include <qmap.h> | 33 | #include <qmap.h> |
34 | 34 | ||
35 | 35 | ||
36 | static void cleanupMime() | 36 | static void cleanupMime() |
37 | { | 37 | { |
38 | MimeType::clear(); | 38 | MimeType::clear(); |
39 | } | 39 | } |
40 | 40 | ||
41 | class MimeTypeData { | 41 | class MimeTypeData { |
42 | public: | 42 | public: |
43 | MimeTypeData(const QString& i) : | 43 | MimeTypeData(const QString& i) : |
44 | id(i) | 44 | id(i) |
45 | { | 45 | { |
46 | apps.setAutoDelete(TRUE); | 46 | apps.setAutoDelete(TRUE); |
47 | } | 47 | } |
48 | QString id; | 48 | QString id; |
49 | QString extension; | 49 | QString extension; |
50 | QList<AppLnk> apps; | 50 | QList<AppLnk> apps; |
51 | 51 | ||
52 | QString description() | 52 | QString description() |
53 | { | 53 | { |
54 | if ( desc.isEmpty() ) | 54 | if ( desc.isEmpty() ) |
55 | desc = QPEApplication::tr("%1 document").arg(apps.first()->name()); | 55 | desc = QPEApplication::tr("%1 document").arg(apps.first()->name()); |
56 | return desc; | 56 | return desc; |
57 | } | 57 | } |
58 | 58 | ||
59 | QPixmap regIcon() | 59 | QPixmap regIcon() |
60 | { | 60 | { |
61 | if ( regicon.isNull() ) | 61 | if ( regicon.isNull() ) |
62 | loadPixmaps(); | 62 | loadPixmaps(); |
63 | return regicon; | 63 | return regicon; |
64 | } | 64 | } |
65 | 65 | ||
66 | QPixmap bigIcon() | 66 | QPixmap bigIcon() |
67 | { | 67 | { |
68 | if ( bigicon.isNull() ) | 68 | if ( bigicon.isNull() ) |
69 | loadPixmaps(); | 69 | loadPixmaps(); |
70 | return bigicon; | 70 | return bigicon; |
71 | } | 71 | } |
72 | 72 | ||
73 | private: | 73 | private: |
74 | void loadPixmaps() | 74 | void loadPixmaps() |
75 | { | 75 | { |
76 | if ( apps.count() ) { | 76 | if ( apps.count() ) { |
77 | QString icon; | 77 | QString icon; |
78 | for (AppLnk* lnk = apps.first(); icon.isNull() && lnk; lnk=apps.next()) { | 78 | for (AppLnk* lnk = apps.first(); icon.isNull() && lnk; lnk=apps.next()) { |
79 | QStringList icons = lnk->mimeTypeIcons(); | 79 | QStringList icons = lnk->mimeTypeIcons(); |
80 | if ( icons.count() ) { | 80 | if ( icons.count() ) { |
81 | QStringList types = lnk->mimeTypes(); | 81 | QStringList types = lnk->mimeTypes(); |
82 | for (QStringList::ConstIterator t=types.begin(),i=icons.begin(); t!=types.end() && i!=icons.end(); ++i,++t) { | 82 | for (QStringList::ConstIterator t=types.begin(),i=icons.begin(); t!=types.end() && i!=icons.end(); ++i,++t) { |
83 | if ( *t == id ) { | 83 | if ( *t == id ) { |
84 | icon = *i; | 84 | icon = *i; |
85 | break; | 85 | break; |
86 | } | 86 | } |
87 | } | 87 | } |
88 | } | 88 | } |
89 | } | 89 | } |
90 | if ( icon.isNull() ) { | 90 | if ( icon.isNull() ) { |
91 | AppLnk* lnk = apps.first(); | 91 | AppLnk* lnk = apps.first(); |
92 | regicon = lnk->pixmap(); | 92 | regicon = lnk->pixmap(); |
93 | bigicon = lnk->bigPixmap(); | 93 | bigicon = lnk->bigPixmap(); |
94 | } else { | 94 | } else { |
95 | QImage unscaledIcon = Resource::loadImage( icon ); | 95 | QImage unscaledIcon = Resource::loadImage( icon ); |
96 | regicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | 96 | regicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); |
97 | bigicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); | 97 | bigicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); |
98 | } | 98 | } |
99 | } | 99 | } |
100 | } | 100 | } |
101 | 101 | ||
102 | QPixmap regicon; | 102 | QPixmap regicon; |
103 | QPixmap bigicon; | 103 | QPixmap bigicon; |
104 | QString desc; | 104 | QString desc; |
105 | }; | 105 | }; |
106 | 106 | ||
107 | class MimeType::Private : public QDict<MimeTypeData> { | 107 | class MimeType::Private : public QDict<MimeTypeData> { |
108 | public: | 108 | public: |
109 | Private() {} | 109 | Private() {} |
110 | ~Private() {} | 110 | ~Private() {} |
111 | 111 | ||
112 | // ... | 112 | // ... |
113 | }; | 113 | }; |
114 | 114 | ||
115 | MimeType::Private* MimeType::d=0; | 115 | MimeType::Private* MimeType::d=0; |
116 | static QMap<QString,QString> *typeFor = 0; | 116 | static QMap<QString,QString> *typeFor = 0; |
117 | static QMap<QString,QStringList> *extFor = 0; | 117 | static QMap<QString,QStringList> *extFor = 0; |
118 | 118 | ||
119 | MimeType::Private& MimeType::data() | 119 | MimeType::Private& MimeType::data() |
120 | { | 120 | { |
121 | if ( !d ) { | 121 | if ( !d ) { |
122 | d = new Private; | 122 | d = new Private; |
123 | d->setAutoDelete(TRUE); | 123 | d->setAutoDelete(TRUE); |
124 | static bool setCleanup = FALSE; | 124 | static bool setCleanup = FALSE; |
125 | if ( !setCleanup ) { | 125 | if ( !setCleanup ) { |
126 | qAddPostRoutine( cleanupMime ); | 126 | qAddPostRoutine( cleanupMime ); |
127 | setCleanup = TRUE; | 127 | setCleanup = TRUE; |
128 | } | 128 | } |
129 | } | 129 | } |
130 | return *d; | 130 | return *d; |
131 | } | 131 | } |
132 | 132 | ||
133 | MimeType::MimeType( const QString& ext_or_id ) | 133 | MimeType::MimeType( const QString& ext_or_id ) |
134 | { | 134 | { |
135 | init(ext_or_id); | 135 | init(ext_or_id); |
136 | } | 136 | } |
137 | 137 | ||
138 | MimeType::MimeType( const DocLnk& lnk ) | 138 | MimeType::MimeType( const DocLnk& lnk ) |
139 | { | 139 | { |
140 | init(lnk.type()); | 140 | init(lnk.type()); |
141 | } | 141 | } |
142 | 142 | ||
143 | QString MimeType::id() const | 143 | QString MimeType::id() const |
144 | { | 144 | { |
145 | return i; | 145 | return i; |
146 | } | 146 | } |
147 | 147 | ||
148 | QString MimeType::description() const | 148 | QString MimeType::description() const |
149 | { | 149 | { |
150 | MimeTypeData* d = data(i); | 150 | MimeTypeData* d = data(i); |
151 | return d ? d->description() : QString::null; | 151 | return d ? d->description() : QString::null; |
152 | } | 152 | } |
153 | 153 | ||
154 | QPixmap MimeType::pixmap() const | 154 | QPixmap MimeType::pixmap() const |
155 | { | 155 | { |
156 | MimeTypeData* d = data(i); | 156 | MimeTypeData* d = data(i); |
157 | return d ? d->regIcon() : QPixmap(); | 157 | return d ? d->regIcon() : QPixmap(); |
158 | } | 158 | } |
159 | 159 | ||
160 | QString MimeType::extension() const | 160 | QString MimeType::extension() const |
161 | { | 161 | { |
162 | return extensions().first(); | 162 | return extensions().first(); |
163 | } | 163 | } |
164 | 164 | ||
165 | QStringList MimeType::extensions() const | 165 | QStringList MimeType::extensions() const |
166 | { | 166 | { |
167 | loadExtensions(); | 167 | loadExtensions(); |
168 | return *(*extFor).find(i); | 168 | return *(*extFor).find(i); |
169 | } | 169 | } |
170 | 170 | ||
171 | QPixmap MimeType::bigPixmap() const | 171 | QPixmap MimeType::bigPixmap() const |
172 | { | 172 | { |
173 | MimeTypeData* d = data(i); | 173 | MimeTypeData* d = data(i); |
174 | return d ? d->bigIcon() : QPixmap(); | 174 | return d ? d->bigIcon() : QPixmap(); |
175 | } | 175 | } |
176 | 176 | ||
177 | const AppLnk* MimeType::application() const | 177 | const AppLnk* MimeType::application() const |
178 | { | 178 | { |
179 | MimeTypeData* d = data(i); | 179 | MimeTypeData* d = data(i); |
180 | return d ? d->apps.first() : 0; | 180 | return d ? d->apps.first() : 0; |
181 | } | 181 | } |
182 | 182 | ||
183 | static QString serviceBinding(const QString& service) | 183 | static QString serviceBinding(const QString& service) |
184 | { | 184 | { |
185 | // Copied from qtopiaservices | 185 | // Copied from qtopiaservices |
186 | QString svrc = service; | 186 | QString svrc = service; |
187 | for (int i=0; i<(int)svrc.length(); i++) | 187 | for (int i=0; i<(int)svrc.length(); i++) |
188 | if ( svrc[i]=='/' ) svrc[i] = '-'; | 188 | if ( svrc[i]=='/' ) svrc[i] = '-'; |
189 | return "Service-"+svrc; | 189 | return "Service-"+svrc; |
190 | } | 190 | } |
191 | 191 | ||
192 | void MimeType::registerApp( const AppLnk& lnk ) | 192 | void MimeType::registerApp( const AppLnk& lnk ) |
193 | { | 193 | { |
194 | QStringList list = lnk.mimeTypes(); | 194 | QStringList list = lnk.mimeTypes(); |
195 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { | 195 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { |
196 | MimeTypeData* cur = data()[*it]; | 196 | MimeTypeData* cur = data()[*it]; |
197 | AppLnk* l = new AppLnk(lnk); | 197 | AppLnk* l = new AppLnk(lnk); |
198 | if ( !cur ) { | 198 | if ( !cur ) { |
199 | cur = new MimeTypeData( *it ); | 199 | cur = new MimeTypeData( *it ); |
200 | data().insert( *it, cur ); | 200 | data().insert( *it, cur ); |
201 | cur->apps.append(l); | 201 | cur->apps.append(l); |
202 | } else if ( cur->apps.count() ) { | 202 | } else if ( cur->apps.count() ) { |
203 | Config binding(serviceBinding("Open/"+*it)); | 203 | Config binding(serviceBinding("Open/"+*it)); |
204 | binding.setGroup("Service"); | 204 | binding.setGroup("Service"); |
205 | QString def = binding.readEntry("default"); | 205 | QString def = binding.readEntry("default"); |
206 | if ( l->exec() == def ) | 206 | if ( l->exec() == def ) |
207 | cur->apps.prepend(l); | 207 | cur->apps.prepend(l); |
208 | else | 208 | else |
209 | cur->apps.append(l); | 209 | cur->apps.append(l); |
210 | } else { | 210 | } else { |
211 | cur->apps.append(l); | 211 | cur->apps.append(l); |
212 | } | 212 | } |
213 | } | 213 | } |
214 | } | 214 | } |
215 | 215 | ||
216 | void MimeType::clear() | 216 | void MimeType::clear() |
217 | { | 217 | { |
218 | delete d; | 218 | delete d; |
219 | d = 0; | 219 | d = 0; |
220 | } | 220 | } |
221 | 221 | ||
222 | void MimeType::loadExtensions() | 222 | void MimeType::loadExtensions() |
223 | { | 223 | { |
224 | if ( !typeFor ) { | 224 | if ( !typeFor ) { |
225 | extFor = new QMap<QString,QStringList>; | 225 | extFor = new QMap<QString,QStringList>; |
226 | typeFor = new QMap<QString,QString>; | 226 | typeFor = new QMap<QString,QString>; |
227 | loadExtensions("/etc/mime.types"); | 227 | loadExtensions("/etc/mime.types"); |
228 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); | 228 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); |
229 | } | 229 | } |
230 | } | 230 | } |
231 | 231 | ||
232 | void MimeType::loadExtensions(const QString& filename) | 232 | void MimeType::loadExtensions(const QString& filename) |
233 | { | 233 | { |
234 | QFile file(filename); | 234 | QFile file(filename); |
235 | if ( file.open(IO_ReadOnly) ) { | 235 | if ( file.open(IO_ReadOnly) ) { |
236 | QTextStream in(&file); | 236 | QTextStream in(&file); |
237 | QRegExp space("[ \t]+"); | 237 | QRegExp space("[ \t]+"); |
238 | while (!in.atEnd()) { | 238 | while (!in.atEnd()) { |
239 | QStringList tokens = QStringList::split(space, in.readLine()); | 239 | QStringList tokens = QStringList::split(space, in.readLine()); |
240 | QStringList::ConstIterator it = tokens.begin(); | 240 | QStringList::ConstIterator it = tokens.begin(); |
241 | if ( it != tokens.end() ) { | 241 | if ( it != tokens.end() ) { |
242 | QString id = *it; ++it; | 242 | QString id = *it; ++it; |
243 | // new override old (though left overrides right) | 243 | // new override old (though left overrides right) |
244 | QStringList exts = (*extFor)[id]; | 244 | QStringList exts = (*extFor)[id]; |
245 | QStringList newexts; | 245 | QStringList newexts; |
246 | if ( it != tokens.end() ) { | 246 | while ( it != tokens.end() ) { |
247 | exts.remove(*it); | 247 | exts.remove(*it); |
248 | if ( !newexts.contains(*it) ) | 248 | if ( !newexts.contains(*it) ) |
249 | newexts.append(*it); | 249 | newexts.append(*it); |
250 | while (it != tokens.end()) { | 250 | (*typeFor)[*it] = id; |
251 | (*typeFor)[*it] = id; | 251 | ++it; |
252 | ++it; | ||
253 | } | ||
254 | } | 252 | } |
255 | (*extFor)[id] = newexts + exts; | 253 | (*extFor)[id] = newexts + exts; |
256 | } | 254 | } |
257 | } | 255 | } |
258 | } | 256 | } |
259 | } | 257 | } |
260 | 258 | ||
261 | void MimeType::init( const QString& ext_or_id ) | 259 | void MimeType::init( const QString& ext_or_id ) |
262 | { | 260 | { |
263 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { | 261 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { |
264 | i = ext_or_id.lower(); | 262 | i = ext_or_id.lower(); |
265 | } else { | 263 | } else { |
266 | loadExtensions(); | 264 | loadExtensions(); |
267 | int dot = ext_or_id.findRev('.'); | 265 | int dot = ext_or_id.findRev('.'); |
268 | QString ext = dot >= 0 ? ext_or_id.mid(dot+1) : ext_or_id; | 266 | QString ext = dot >= 0 ? ext_or_id.mid(dot+1) : ext_or_id; |
269 | i = (*typeFor)[ext.lower()]; | 267 | i = (*typeFor)[ext.lower()]; |
270 | if ( i.isNull() ) | 268 | if ( i.isNull() ) |
271 | i = "application/octet-stream"; | 269 | i = "application/octet-stream"; |
272 | } | 270 | } |
273 | static bool appsUpdated = FALSE; | 271 | static bool appsUpdated = FALSE; |
274 | if ( !appsUpdated ) { | 272 | if ( !appsUpdated ) { |
275 | appsUpdated = TRUE; | 273 | appsUpdated = TRUE; |
276 | updateApplications(); | 274 | updateApplications(); |
277 | } | 275 | } |
278 | } | 276 | } |
279 | 277 | ||
280 | MimeTypeData* MimeType::data(const QString& id) | 278 | MimeTypeData* MimeType::data(const QString& id) |
281 | { | 279 | { |
282 | MimeTypeData* d = data()[id]; | 280 | MimeTypeData* d = data()[id]; |
283 | if ( !d ) { | 281 | if ( !d ) { |
284 | int s = id.find('/'); | 282 | int s = id.find('/'); |
285 | QString idw = id.left(s)+"/*"; | 283 | QString idw = id.left(s)+"/*"; |
286 | d = data()[idw]; | 284 | d = data()[idw]; |
287 | } | 285 | } |
288 | return d; | 286 | return d; |
289 | } | 287 | } |
290 | 288 | ||
291 | QString MimeType::appsFolderName() | 289 | QString MimeType::appsFolderName() |
292 | { | 290 | { |
293 | return QPEApplication::qpeDir() + "apps"; | 291 | return QPEApplication::qpeDir() + "apps"; |
294 | } | 292 | } |
295 | 293 | ||
296 | void MimeType::updateApplications() | 294 | void MimeType::updateApplications() |
297 | { | 295 | { |
298 | clear(); | 296 | clear(); |
299 | AppLnkSet apps( appsFolderName() ); | 297 | AppLnkSet apps( appsFolderName() ); |
300 | updateApplications(&apps); | 298 | updateApplications(&apps); |
301 | } | 299 | } |
302 | 300 | ||
303 | void MimeType::updateApplications(AppLnkSet* folder) | 301 | void MimeType::updateApplications(AppLnkSet* folder) |
304 | { | 302 | { |
305 | for ( QListIterator<AppLnk> it( folder->children() ); it.current(); ++it ) { | 303 | for ( QListIterator<AppLnk> it( folder->children() ); it.current(); ++it ) { |
306 | registerApp(*it.current()); | 304 | registerApp(*it.current()); |
307 | } | 305 | } |
308 | } | 306 | } |