-rw-r--r-- | library/mimetype.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/library/mimetype.cpp b/library/mimetype.cpp index ef5d619..d0a578e 100644 --- a/library/mimetype.cpp +++ b/library/mimetype.cpp | |||
@@ -121,107 +121,165 @@ MimeType::Private& MimeType::data() | |||
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 | /*! | ||
134 | \class MimeType mimetype.h | ||
135 | \brief The MimeType class provides MIME type information. | ||
136 | |||
137 | A MimeType object is a light-weight value which | ||
138 | provides information about a MIME type. | ||
139 | |||
140 | \ingroup qtopiaemb | ||
141 | */ | ||
142 | |||
143 | /*! | ||
144 | Constructs a MimeType. | ||
145 | Normally, \a ext_or_id is a MIME type, | ||
146 | but if \a ext_or_id starts with / or contains no /, | ||
147 | it is interpretted as a filename and the | ||
148 | extension (eg. .txt) is used as the | ||
149 | MIME type. | ||
150 | */ | ||
133 | MimeType::MimeType( const QString& ext_or_id ) | 151 | MimeType::MimeType( const QString& ext_or_id ) |
134 | { | 152 | { |
135 | init(ext_or_id); | 153 | init(ext_or_id); |
136 | } | 154 | } |
137 | 155 | ||
156 | /*! | ||
157 | Constructs a MimeType from the type() of \a lnk. | ||
158 | */ | ||
138 | MimeType::MimeType( const DocLnk& lnk ) | 159 | MimeType::MimeType( const DocLnk& lnk ) |
139 | { | 160 | { |
140 | init(lnk.type()); | 161 | init(lnk.type()); |
141 | } | 162 | } |
142 | 163 | ||
164 | /*! | ||
165 | Returns the MIME type identifier. | ||
166 | */ | ||
143 | QString MimeType::id() const | 167 | QString MimeType::id() const |
144 | { | 168 | { |
145 | return i; | 169 | return i; |
146 | } | 170 | } |
147 | 171 | ||
172 | /*! | ||
173 | Returns a description of the MIME Type. This is usually based | ||
174 | on the application() associated with the type. | ||
175 | */ | ||
148 | QString MimeType::description() const | 176 | QString MimeType::description() const |
149 | { | 177 | { |
150 | MimeTypeData* d = data(i); | 178 | MimeTypeData* d = data(i); |
151 | return d ? d->description() : QString::null; | 179 | return d ? d->description() : QString::null; |
152 | } | 180 | } |
153 | 181 | ||
182 | /*! | ||
183 | Returns a small QPixmap appropriate for the MIME type. | ||
184 | */ | ||
154 | QPixmap MimeType::pixmap() const | 185 | QPixmap MimeType::pixmap() const |
155 | { | 186 | { |
156 | MimeTypeData* d = data(i); | 187 | MimeTypeData* d = data(i); |
157 | return d ? d->regIcon() : QPixmap(); | 188 | return d ? d->regIcon() : QPixmap(); |
158 | } | 189 | } |
159 | 190 | ||
191 | /*! | ||
192 | \internal | ||
193 | This function is not generally available. | ||
194 | */ | ||
160 | QString MimeType::extension() const | 195 | QString MimeType::extension() const |
161 | { | 196 | { |
162 | return extensions().first(); | 197 | return extensions().first(); |
163 | } | 198 | } |
164 | 199 | ||
200 | |||
201 | /*! | ||
202 | \internal | ||
203 | This function is not generally available. | ||
204 | */ | ||
165 | QStringList MimeType::extensions() const | 205 | QStringList MimeType::extensions() const |
166 | { | 206 | { |
167 | loadExtensions(); | 207 | loadExtensions(); |
168 | return *(*extFor).find(i); | 208 | return *(*extFor).find(i); |
169 | } | 209 | } |
170 | 210 | ||
211 | /*! | ||
212 | Returns a larger QPixmap appropriate for the MIME type. | ||
213 | */ | ||
171 | QPixmap MimeType::bigPixmap() const | 214 | QPixmap MimeType::bigPixmap() const |
172 | { | 215 | { |
173 | MimeTypeData* d = data(i); | 216 | MimeTypeData* d = data(i); |
174 | return d ? d->bigIcon() : QPixmap(); | 217 | return d ? d->bigIcon() : QPixmap(); |
175 | } | 218 | } |
176 | 219 | ||
220 | /*! | ||
221 | Returns the AppLnk defining the application associated | ||
222 | with this MIME type, or 0 if none is associated. | ||
223 | |||
224 | The caller must not retain the pointer, | ||
225 | but of course you can dereference it to take a copy if needed. | ||
226 | |||
227 | \sa Service::binding() | ||
228 | */ | ||
177 | const AppLnk* MimeType::application() const | 229 | const AppLnk* MimeType::application() const |
178 | { | 230 | { |
179 | MimeTypeData* d = data(i); | 231 | MimeTypeData* d = data(i); |
180 | return d ? d->apps.first() : 0; | 232 | return d ? d->apps.first() : 0; |
181 | } | 233 | } |
182 | 234 | ||
183 | static QString serviceBinding(const QString& service) | 235 | static QString serviceBinding(const QString& service) |
184 | { | 236 | { |
185 | // Copied from qtopiaservices | 237 | // Copied from qtopiaservices |
186 | QString svrc = service; | 238 | QString svrc = service; |
187 | for (int i=0; i<(int)svrc.length(); i++) | 239 | for (int i=0; i<(int)svrc.length(); i++) |
188 | if ( svrc[i]=='/' ) svrc[i] = '-'; | 240 | if ( svrc[i]=='/' ) svrc[i] = '-'; |
189 | return "Service-"+svrc; | 241 | return "Service-"+svrc; |
190 | } | 242 | } |
191 | 243 | ||
244 | /*! | ||
245 | \internal | ||
246 | */ | ||
192 | void MimeType::registerApp( const AppLnk& lnk ) | 247 | void MimeType::registerApp( const AppLnk& lnk ) |
193 | { | 248 | { |
194 | QStringList list = lnk.mimeTypes(); | 249 | QStringList list = lnk.mimeTypes(); |
195 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { | 250 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { |
196 | MimeTypeData* cur = data()[*it]; | 251 | MimeTypeData* cur = data()[*it]; |
197 | AppLnk* l = new AppLnk(lnk); | 252 | AppLnk* l = new AppLnk(lnk); |
198 | if ( !cur ) { | 253 | if ( !cur ) { |
199 | cur = new MimeTypeData( *it ); | 254 | cur = new MimeTypeData( *it ); |
200 | data().insert( *it, cur ); | 255 | data().insert( *it, cur ); |
201 | cur->apps.append(l); | 256 | cur->apps.append(l); |
202 | } else if ( cur->apps.count() ) { | 257 | } else if ( cur->apps.count() ) { |
203 | Config binding(serviceBinding("Open/"+*it)); | 258 | Config binding(serviceBinding("Open/"+*it)); |
204 | binding.setGroup("Service"); | 259 | binding.setGroup("Service"); |
205 | QString def = binding.readEntry("default"); | 260 | QString def = binding.readEntry("default"); |
206 | if ( l->exec() == def ) | 261 | if ( l->exec() == def ) |
207 | cur->apps.prepend(l); | 262 | cur->apps.prepend(l); |
208 | else | 263 | else |
209 | cur->apps.append(l); | 264 | cur->apps.append(l); |
210 | } else { | 265 | } else { |
211 | cur->apps.append(l); | 266 | cur->apps.append(l); |
212 | } | 267 | } |
213 | } | 268 | } |
214 | } | 269 | } |
215 | 270 | ||
271 | /*! | ||
272 | \internal | ||
273 | */ | ||
216 | void MimeType::clear() | 274 | void MimeType::clear() |
217 | { | 275 | { |
218 | delete d; | 276 | delete d; |
219 | d = 0; | 277 | d = 0; |
220 | } | 278 | } |
221 | 279 | ||
222 | void MimeType::loadExtensions() | 280 | void MimeType::loadExtensions() |
223 | { | 281 | { |
224 | if ( !typeFor ) { | 282 | if ( !typeFor ) { |
225 | extFor = new QMap<QString,QStringList>; | 283 | extFor = new QMap<QString,QStringList>; |
226 | typeFor = new QMap<QString,QString>; | 284 | typeFor = new QMap<QString,QString>; |
227 | loadExtensions("/etc/mime.types"); | 285 | loadExtensions("/etc/mime.types"); |
@@ -277,29 +335,35 @@ void MimeType::init( const QString& ext_or_id ) | |||
277 | 335 | ||
278 | MimeTypeData* MimeType::data(const QString& id) | 336 | MimeTypeData* MimeType::data(const QString& id) |
279 | { | 337 | { |
280 | MimeTypeData* d = data()[id]; | 338 | MimeTypeData* d = data()[id]; |
281 | if ( !d ) { | 339 | if ( !d ) { |
282 | int s = id.find('/'); | 340 | int s = id.find('/'); |
283 | QString idw = id.left(s)+"/*"; | 341 | QString idw = id.left(s)+"/*"; |
284 | d = data()[idw]; | 342 | d = data()[idw]; |
285 | } | 343 | } |
286 | return d; | 344 | return d; |
287 | } | 345 | } |
288 | 346 | ||
347 | /*! | ||
348 | Returns a Qtopia folder containing application definitions. | ||
349 | */ | ||
289 | QString MimeType::appsFolderName() | 350 | QString MimeType::appsFolderName() |
290 | { | 351 | { |
291 | return QPEApplication::qpeDir() + "apps"; | 352 | return QPEApplication::qpeDir() + "apps"; |
292 | } | 353 | } |
293 | 354 | ||
355 | /*! | ||
356 | Reloads application definitions. | ||
357 | */ | ||
294 | void MimeType::updateApplications() | 358 | void MimeType::updateApplications() |
295 | { | 359 | { |
296 | clear(); | 360 | clear(); |
297 | AppLnkSet apps( appsFolderName() ); | 361 | AppLnkSet apps( appsFolderName() ); |
298 | updateApplications(&apps); | 362 | updateApplications(&apps); |
299 | } | 363 | } |
300 | 364 | ||
301 | void MimeType::updateApplications(AppLnkSet* folder) | 365 | void MimeType::updateApplications(AppLnkSet* folder) |
302 | { | 366 | { |
303 | for ( QListIterator<AppLnk> it( folder->children() ); it.current(); ++it ) { | 367 | for ( QListIterator<AppLnk> it( folder->children() ); it.current(); ++it ) { |
304 | registerApp(*it.current()); | 368 | registerApp(*it.current()); |
305 | } | 369 | } |