summaryrefslogtreecommitdiff
path: root/library/mimetype.cpp
authorzecke <zecke>2002-09-10 12:09:49 (UTC)
committer zecke <zecke>2002-09-10 12:09:49 (UTC)
commit6b77a1cdb9536b1c135eb86d53a6b2c22c19b0a4 (patch) (unidiff)
tree6ebc93c6432f4ed9d00ef1448b6a047ef522a79a /library/mimetype.cpp
parentd10cddb3c9ce75bc90b14add14bc133737fe35aa (diff)
downloadopie-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?
Diffstat (limited to 'library/mimetype.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/mimetype.cpp165
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,7 +1,7 @@
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
@@ -23,51 +23,109 @@
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
36static void cleanupMime()
37{
38 MimeType::clear();
39}
40
33class MimeTypeData { 41class MimeTypeData {
34public: 42public:
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
73private:
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
57class MimeType::Dict : public QDict<MimeTypeData> { 107class MimeType::Private : public QDict<MimeTypeData> {
58public: 108public:
59 Dict() {} 109 Private() {}
110 ~Private() {}
111
112 // ...
60}; 113};
61 114
62MimeType::Dict* MimeType::d=0; 115MimeType::Private* MimeType::d=0;
63static QMap<QString,QString> *typeFor = 0; 116static QMap<QString,QString> *typeFor = 0;
64static QMap<QString,QString> *extFor = 0; 117static QMap<QString,QStringList> *extFor = 0;
65 118
66MimeType::Dict& MimeType::dict() 119MimeType::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}
@@ -90,17 +148,22 @@ QString MimeType::id() const
90QString MimeType::description() const 148QString 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
96QPixmap MimeType::pixmap() const 154QPixmap 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
102QString MimeType::extension() const 160QString MimeType::extension() const
103{ 161{
162 return extensions().first();
163}
164
165QStringList MimeType::extensions() const
166{
104 loadExtensions(); 167 loadExtensions();
105 return *(*extFor).find(i); 168 return *(*extFor).find(i);
106} 169}
@@ -108,23 +171,45 @@ QString MimeType::extension() const
108QPixmap MimeType::bigPixmap() const 171QPixmap 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
114const AppLnk* MimeType::application() const 177const 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
183static 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
120void MimeType::registerApp( const AppLnk& lnk ) 192void 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
@@ -137,7 +222,7 @@ void MimeType::clear()
137void MimeType::loadExtensions() 222void 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");
@@ -155,13 +240,19 @@ void MimeType::loadExtensions(const QString& filename)
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 }
@@ -188,11 +279,11 @@ void MimeType::init( const QString& ext_or_id )
188 279
189MimeTypeData* MimeType::data(const QString& id) 280MimeTypeData* 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}