author | zecke <zecke> | 2002-09-10 12:09:49 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-09-10 12:09:49 (UTC) |
commit | 6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4 (patch) (unidiff) | |
tree | 6ebc93c6432f4ed9d00ef1448b6a047ef522a79a /library/mimetype.cpp | |
parent | d10cddb3c9ce75bc90b14add14bc133737fe35aa (diff) | |
download | opie-6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4.zip opie-6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4.tar.gz opie-6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4.tar.bz2 |
Qtopia1-6 merge
still to test
bic changes to be resolved
more changes to be made?
-rw-r--r-- | library/mimetype.cpp | 165 |
1 files changed, 128 insertions, 37 deletions
diff --git a/library/mimetype.cpp b/library/mimetype.cpp index 9fab160..e2e0dab 100644 --- a/library/mimetype.cpp +++ b/library/mimetype.cpp | |||
@@ -1,12 +1,12 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of 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. |
@@ -18,61 +18,119 @@ | |||
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" | ||
27 | |||
26 | #include <qfile.h> | 28 | #include <qfile.h> |
27 | #include <qdict.h> | 29 | #include <qdict.h> |
28 | #include <qregexp.h> | 30 | #include <qregexp.h> |
29 | #include <qstringlist.h> | 31 | #include <qstringlist.h> |
30 | #include <qtextstream.h> | 32 | #include <qtextstream.h> |
31 | #include <qmap.h> | 33 | #include <qmap.h> |
32 | 34 | ||
35 | |||
36 | static void cleanupMime() | ||
37 | { | ||
38 | MimeType::clear(); | ||
39 | } | ||
40 | |||
33 | class MimeTypeData { | 41 | class MimeTypeData { |
34 | public: | 42 | public: |
35 | MimeTypeData(const QString& i, const AppLnk& lnk, const QString& icon ) : | 43 | MimeTypeData(const QString& i) : |
36 | id(i), | 44 | id(i) |
37 | desc(lnk.name()+" document"), | ||
38 | app(lnk) | ||
39 | { | 45 | { |
40 | if ( icon.isEmpty() ) { | 46 | apps.setAutoDelete(TRUE); |
41 | regIcon = lnk.pixmap(); | ||
42 | bigIcon = lnk.bigPixmap(); | ||
43 | } else { | ||
44 | QImage unscaledIcon = Resource::loadImage( icon ); | ||
45 | regIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | ||
46 | bigIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); | ||
47 | } | ||
48 | } | 47 | } |
49 | QString id; | 48 | QString id; |
50 | QString extension; | 49 | QString extension; |
50 | QList<AppLnk> apps; | ||
51 | |||
52 | QString description() | ||
53 | { | ||
54 | if ( desc.isEmpty() ) | ||
55 | desc = QPEApplication::tr("%1 document").arg(apps.first()->name()); | ||
56 | return desc; | ||
57 | } | ||
58 | |||
59 | QPixmap regIcon() | ||
60 | { | ||
61 | if ( regicon.isNull() ) | ||
62 | loadPixmaps(); | ||
63 | return regicon; | ||
64 | } | ||
65 | |||
66 | QPixmap bigIcon() | ||
67 | { | ||
68 | if ( bigicon.isNull() ) | ||
69 | loadPixmaps(); | ||
70 | return bigicon; | ||
71 | } | ||
72 | |||
73 | private: | ||
74 | void loadPixmaps() | ||
75 | { | ||
76 | if ( apps.count() ) { | ||
77 | QString icon; | ||
78 | for (AppLnk* lnk = apps.first(); icon.isNull() && lnk; lnk=apps.next()) { | ||
79 | QStringList icons = lnk->mimeTypeIcons(); | ||
80 | if ( icons.count() ) { | ||
81 | QStringList types = lnk->mimeTypes(); | ||
82 | for (QStringList::ConstIterator t=types.begin(),i=icons.begin(); t!=types.end() && i!=icons.end(); ++i,++t) { | ||
83 | if ( *t == id ) { | ||
84 | icon = *i; | ||
85 | break; | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | if ( icon.isNull() ) { | ||
91 | AppLnk* lnk = apps.first(); | ||
92 | regicon = lnk->pixmap(); | ||
93 | bigicon = lnk->bigPixmap(); | ||
94 | } else { | ||
95 | QImage unscaledIcon = Resource::loadImage( icon ); | ||
96 | regicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | ||
97 | bigicon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | QPixmap regicon; | ||
103 | QPixmap bigicon; | ||
51 | QString desc; | 104 | QString desc; |
52 | QPixmap regIcon; | ||
53 | QPixmap bigIcon; | ||
54 | AppLnk app; | ||
55 | }; | 105 | }; |
56 | 106 | ||
57 | class MimeType::Dict : public QDict<MimeTypeData> { | 107 | class MimeType::Private : public QDict<MimeTypeData> { |
58 | public: | 108 | public: |
59 | Dict() {} | 109 | Private() {} |
110 | ~Private() {} | ||
111 | |||
112 | // ... | ||
60 | }; | 113 | }; |
61 | 114 | ||
62 | MimeType::Dict* MimeType::d=0; | 115 | MimeType::Private* MimeType::d=0; |
63 | static QMap<QString,QString> *typeFor = 0; | 116 | static QMap<QString,QString> *typeFor = 0; |
64 | static QMap<QString,QString> *extFor = 0; | 117 | static QMap<QString,QStringList> *extFor = 0; |
65 | 118 | ||
66 | MimeType::Dict& MimeType::dict() | 119 | MimeType::Private& MimeType::data() |
67 | { | 120 | { |
68 | if ( !d ) { | 121 | if ( !d ) { |
69 | d = new Dict; | 122 | d = new Private; |
70 | d->setAutoDelete(TRUE); | 123 | d->setAutoDelete(TRUE); |
124 | static bool setCleanup = FALSE; | ||
125 | if ( !setCleanup ) { | ||
126 | qAddPostRoutine( cleanupMime ); | ||
127 | setCleanup = TRUE; | ||
128 | } | ||
71 | } | 129 | } |
72 | return *d; | 130 | return *d; |
73 | } | 131 | } |
74 | 132 | ||
75 | MimeType::MimeType( const QString& ext_or_id ) | 133 | MimeType::MimeType( const QString& ext_or_id ) |
76 | { | 134 | { |
77 | init(ext_or_id); | 135 | init(ext_or_id); |
78 | } | 136 | } |
@@ -85,64 +143,91 @@ MimeType::MimeType( const DocLnk& lnk ) | |||
85 | QString MimeType::id() const | 143 | QString MimeType::id() const |
86 | { | 144 | { |
87 | return i; | 145 | return i; |
88 | } | 146 | } |
89 | 147 | ||
90 | QString MimeType::description() const | 148 | QString MimeType::description() const |
91 | { | 149 | { |
92 | MimeTypeData* d = data(i); | 150 | MimeTypeData* d = data(i); |
93 | return d ? d->desc : QString::null; | 151 | return d ? d->description() : QString::null; |
94 | } | 152 | } |
95 | 153 | ||
96 | QPixmap MimeType::pixmap() const | 154 | QPixmap MimeType::pixmap() const |
97 | { | 155 | { |
98 | MimeTypeData* d = data(i); | 156 | MimeTypeData* d = data(i); |
99 | return d ? d->regIcon : QPixmap(); | 157 | return d ? d->regIcon() : QPixmap(); |
100 | } | 158 | } |
101 | 159 | ||
102 | QString MimeType::extension() const | 160 | QString MimeType::extension() const |
103 | { | 161 | { |
162 | return extensions().first(); | ||
163 | } | ||
164 | |||
165 | QStringList MimeType::extensions() const | ||
166 | { | ||
104 | loadExtensions(); | 167 | loadExtensions(); |
105 | return *(*extFor).find(i); | 168 | return *(*extFor).find(i); |
106 | } | 169 | } |
107 | 170 | ||
108 | QPixmap MimeType::bigPixmap() const | 171 | QPixmap MimeType::bigPixmap() const |
109 | { | 172 | { |
110 | MimeTypeData* d = data(i); | 173 | MimeTypeData* d = data(i); |
111 | return d ? d->bigIcon : QPixmap(); | 174 | return d ? d->bigIcon() : QPixmap(); |
112 | } | 175 | } |
113 | 176 | ||
114 | const AppLnk* MimeType::application() const | 177 | const AppLnk* MimeType::application() const |
115 | { | 178 | { |
116 | MimeTypeData* d = data(i); | 179 | MimeTypeData* d = data(i); |
117 | return d ? &d->app : 0; | 180 | return d ? d->apps.first() : 0; |
181 | } | ||
182 | |||
183 | static QString serviceBinding(const QString& service) | ||
184 | { | ||
185 | // Copied from qtopiaservices | ||
186 | QString svrc = service; | ||
187 | for (int i=0; i<(int)svrc.length(); i++) | ||
188 | if ( svrc[i]=='/' ) svrc[i] = '-'; | ||
189 | return "Service-"+svrc; | ||
118 | } | 190 | } |
119 | 191 | ||
120 | void MimeType::registerApp( const AppLnk& lnk ) | 192 | void MimeType::registerApp( const AppLnk& lnk ) |
121 | { | 193 | { |
122 | QStringList list = lnk.mimeTypes(); | 194 | QStringList list = lnk.mimeTypes(); |
123 | QStringList icons = lnk.mimeTypeIcons(); | 195 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { |
124 | QStringList::ConstIterator icon = icons.begin(); | 196 | MimeTypeData* cur = data()[*it]; |
125 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it, ++icon) { | 197 | AppLnk* l = new AppLnk(lnk); |
126 | MimeTypeData *item = new MimeTypeData( *it, lnk, *icon ); | 198 | if ( !cur ) { |
127 | dict().replace( *it, item ); | 199 | cur = new MimeTypeData( *it ); |
200 | data().insert( *it, cur ); | ||
201 | cur->apps.append(l); | ||
202 | } else if ( cur->apps.count() ) { | ||
203 | Config binding(serviceBinding("Open/"+*it)); | ||
204 | binding.setGroup("Service"); | ||
205 | QString def = binding.readEntry("default"); | ||
206 | if ( l->exec() == def ) | ||
207 | cur->apps.prepend(l); | ||
208 | else | ||
209 | cur->apps.append(l); | ||
210 | } else { | ||
211 | cur->apps.append(l); | ||
212 | } | ||
128 | } | 213 | } |
129 | } | 214 | } |
130 | 215 | ||
131 | void MimeType::clear() | 216 | void MimeType::clear() |
132 | { | 217 | { |
133 | delete d; | 218 | delete d; |
134 | d = 0; | 219 | d = 0; |
135 | } | 220 | } |
136 | 221 | ||
137 | void MimeType::loadExtensions() | 222 | void MimeType::loadExtensions() |
138 | { | 223 | { |
139 | if ( !typeFor ) { | 224 | if ( !typeFor ) { |
140 | extFor = new QMap<QString,QString>; | 225 | extFor = new QMap<QString,QStringList>; |
141 | typeFor = new QMap<QString,QString>; | 226 | typeFor = new QMap<QString,QString>; |
142 | loadExtensions("/etc/mime.types"); | 227 | loadExtensions("/etc/mime.types"); |
143 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); | 228 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); |
144 | } | 229 | } |
145 | } | 230 | } |
146 | 231 | ||
147 | void MimeType::loadExtensions(const QString& filename) | 232 | void MimeType::loadExtensions(const QString& filename) |
148 | { | 233 | { |
@@ -150,23 +235,29 @@ void MimeType::loadExtensions(const QString& filename) | |||
150 | if ( file.open(IO_ReadOnly) ) { | 235 | if ( file.open(IO_ReadOnly) ) { |
151 | QTextStream in(&file); | 236 | QTextStream in(&file); |
152 | QRegExp space("[ \t]+"); | 237 | QRegExp space("[ \t]+"); |
153 | while (!in.atEnd()) { | 238 | while (!in.atEnd()) { |
154 | QStringList tokens = QStringList::split(space, in.readLine()); | 239 | QStringList tokens = QStringList::split(space, in.readLine()); |
155 | QStringList::ConstIterator it = tokens.begin(); | 240 | QStringList::ConstIterator it = tokens.begin(); |
156 | if ( it != tokens.end() ) { | 241 | if ( it != tokens.end() ) { |
157 | QString id = *it; ++it; | 242 | QString id = *it; ++it; |
243 | // new override old (though left overrides right) | ||
244 | QStringList exts = (*extFor)[id]; | ||
245 | QStringList newexts; | ||
158 | if ( it != tokens.end() ) { | 246 | if ( it != tokens.end() ) { |
159 | (*extFor)[id] = *it; | 247 | exts.remove(*it); |
248 | if ( !newexts.contains(*it) ) | ||
249 | newexts.append(*it); | ||
160 | while (it != tokens.end()) { | 250 | while (it != tokens.end()) { |
161 | (*typeFor)[*it] = id; | 251 | (*typeFor)[*it] = id; |
162 | ++it; | 252 | ++it; |
163 | } | 253 | } |
164 | } | 254 | } |
255 | (*extFor)[id] = newexts + exts; | ||
165 | } | 256 | } |
166 | } | 257 | } |
167 | } | 258 | } |
168 | } | 259 | } |
169 | 260 | ||
170 | void MimeType::init( const QString& ext_or_id ) | 261 | void MimeType::init( const QString& ext_or_id ) |
171 | { | 262 | { |
172 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { | 263 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { |
@@ -183,21 +274,21 @@ void MimeType::init( const QString& ext_or_id ) | |||
183 | if ( !appsUpdated ) { | 274 | if ( !appsUpdated ) { |
184 | appsUpdated = TRUE; | 275 | appsUpdated = TRUE; |
185 | updateApplications(); | 276 | updateApplications(); |
186 | } | 277 | } |
187 | } | 278 | } |
188 | 279 | ||
189 | MimeTypeData* MimeType::data(const QString& id) | 280 | MimeTypeData* MimeType::data(const QString& id) |
190 | { | 281 | { |
191 | MimeTypeData* d = dict()[id]; | 282 | MimeTypeData* d = data()[id]; |
192 | if ( !d ) { | 283 | if ( !d ) { |
193 | int s = id.find('/'); | 284 | int s = id.find('/'); |
194 | QString idw = id.left(s)+"/*"; | 285 | QString idw = id.left(s)+"/*"; |
195 | d = dict()[idw]; | 286 | d = data()[idw]; |
196 | } | 287 | } |
197 | return d; | 288 | return d; |
198 | } | 289 | } |
199 | 290 | ||
200 | QString MimeType::appsFolderName() | 291 | QString MimeType::appsFolderName() |
201 | { | 292 | { |
202 | return QPEApplication::qpeDir() + "apps"; | 293 | return QPEApplication::qpeDir() + "apps"; |
203 | } | 294 | } |