-rw-r--r-- | library/mimetype.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/library/mimetype.cpp b/library/mimetype.cpp index c6a4453..9fab160 100644 --- a/library/mimetype.cpp +++ b/library/mimetype.cpp | |||
@@ -9,24 +9,25 @@ | |||
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 | #include "mimetype.h" | 22 | #include "mimetype.h" |
22 | #include "applnk.h" | 23 | #include "applnk.h" |
23 | #include "resource.h" | 24 | #include "resource.h" |
24 | #include "qpeapplication.h" | 25 | #include "qpeapplication.h" |
25 | #include <qfile.h> | 26 | #include <qfile.h> |
26 | #include <qdict.h> | 27 | #include <qdict.h> |
27 | #include <qregexp.h> | 28 | #include <qregexp.h> |
28 | #include <qstringlist.h> | 29 | #include <qstringlist.h> |
29 | #include <qtextstream.h> | 30 | #include <qtextstream.h> |
30 | #include <qmap.h> | 31 | #include <qmap.h> |
31 | 32 | ||
32 | class MimeTypeData { | 33 | class MimeTypeData { |
@@ -37,37 +38,39 @@ public: | |||
37 | app(lnk) | 38 | app(lnk) |
38 | { | 39 | { |
39 | if ( icon.isEmpty() ) { | 40 | if ( icon.isEmpty() ) { |
40 | regIcon = lnk.pixmap(); | 41 | regIcon = lnk.pixmap(); |
41 | bigIcon = lnk.bigPixmap(); | 42 | bigIcon = lnk.bigPixmap(); |
42 | } else { | 43 | } else { |
43 | QImage unscaledIcon = Resource::loadImage( icon ); | 44 | QImage unscaledIcon = Resource::loadImage( icon ); |
44 | regIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | 45 | regIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); |
45 | bigIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); | 46 | bigIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); |
46 | } | 47 | } |
47 | } | 48 | } |
48 | QString id; | 49 | QString id; |
50 | QString extension; | ||
49 | QString desc; | 51 | QString desc; |
50 | QPixmap regIcon; | 52 | QPixmap regIcon; |
51 | QPixmap bigIcon; | 53 | QPixmap bigIcon; |
52 | AppLnk app; | 54 | AppLnk app; |
53 | }; | 55 | }; |
54 | 56 | ||
55 | class MimeType::Dict : public QDict<MimeTypeData> { | 57 | class MimeType::Dict : public QDict<MimeTypeData> { |
56 | public: | 58 | public: |
57 | Dict() {} | 59 | Dict() {} |
58 | }; | 60 | }; |
59 | 61 | ||
60 | MimeType::Dict* MimeType::d=0; | 62 | MimeType::Dict* MimeType::d=0; |
61 | static QMap<QString,QString> *typeFor = 0; | 63 | static QMap<QString,QString> *typeFor = 0; |
64 | static QMap<QString,QString> *extFor = 0; | ||
62 | 65 | ||
63 | MimeType::Dict& MimeType::dict() | 66 | MimeType::Dict& MimeType::dict() |
64 | { | 67 | { |
65 | if ( !d ) { | 68 | if ( !d ) { |
66 | d = new Dict; | 69 | d = new Dict; |
67 | d->setAutoDelete(TRUE); | 70 | d->setAutoDelete(TRUE); |
68 | } | 71 | } |
69 | return *d; | 72 | return *d; |
70 | } | 73 | } |
71 | 74 | ||
72 | MimeType::MimeType( const QString& ext_or_id ) | 75 | MimeType::MimeType( const QString& ext_or_id ) |
73 | { | 76 | { |
@@ -87,24 +90,30 @@ QString MimeType::id() const | |||
87 | QString MimeType::description() const | 90 | QString MimeType::description() const |
88 | { | 91 | { |
89 | MimeTypeData* d = data(i); | 92 | MimeTypeData* d = data(i); |
90 | return d ? d->desc : QString::null; | 93 | return d ? d->desc : QString::null; |
91 | } | 94 | } |
92 | 95 | ||
93 | QPixmap MimeType::pixmap() const | 96 | QPixmap MimeType::pixmap() const |
94 | { | 97 | { |
95 | MimeTypeData* d = data(i); | 98 | MimeTypeData* d = data(i); |
96 | return d ? d->regIcon : QPixmap(); | 99 | return d ? d->regIcon : QPixmap(); |
97 | } | 100 | } |
98 | 101 | ||
102 | QString MimeType::extension() const | ||
103 | { | ||
104 | loadExtensions(); | ||
105 | return *(*extFor).find(i); | ||
106 | } | ||
107 | |||
99 | QPixmap MimeType::bigPixmap() const | 108 | QPixmap MimeType::bigPixmap() const |
100 | { | 109 | { |
101 | MimeTypeData* d = data(i); | 110 | MimeTypeData* d = data(i); |
102 | return d ? d->bigIcon : QPixmap(); | 111 | return d ? d->bigIcon : QPixmap(); |
103 | } | 112 | } |
104 | 113 | ||
105 | const AppLnk* MimeType::application() const | 114 | const AppLnk* MimeType::application() const |
106 | { | 115 | { |
107 | MimeTypeData* d = data(i); | 116 | MimeTypeData* d = data(i); |
108 | return d ? &d->app : 0; | 117 | return d ? &d->app : 0; |
109 | } | 118 | } |
110 | 119 | ||
@@ -119,49 +128,53 @@ void MimeType::registerApp( const AppLnk& lnk ) | |||
119 | } | 128 | } |
120 | } | 129 | } |
121 | 130 | ||
122 | void MimeType::clear() | 131 | void MimeType::clear() |
123 | { | 132 | { |
124 | delete d; | 133 | delete d; |
125 | d = 0; | 134 | d = 0; |
126 | } | 135 | } |
127 | 136 | ||
128 | void MimeType::loadExtensions() | 137 | void MimeType::loadExtensions() |
129 | { | 138 | { |
130 | if ( !typeFor ) { | 139 | if ( !typeFor ) { |
140 | extFor = new QMap<QString,QString>; | ||
131 | typeFor = new QMap<QString,QString>; | 141 | typeFor = new QMap<QString,QString>; |
132 | loadExtensions("/etc/mime.types"); | 142 | loadExtensions("/etc/mime.types"); |
133 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); | 143 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); |
134 | } | 144 | } |
135 | } | 145 | } |
136 | 146 | ||
137 | void MimeType::loadExtensions(const QString& filename) | 147 | void MimeType::loadExtensions(const QString& filename) |
138 | { | 148 | { |
139 | QFile file(filename); | 149 | QFile file(filename); |
140 | if ( file.open(IO_ReadOnly) ) { | 150 | if ( file.open(IO_ReadOnly) ) { |
141 | QTextStream in(&file); | 151 | QTextStream in(&file); |
142 | QRegExp space("[ \t]+"); | 152 | QRegExp space("[ \t]+"); |
143 | while (!in.atEnd()) { | 153 | while (!in.atEnd()) { |
144 | QStringList tokens = QStringList::split(space, in.readLine()); | 154 | QStringList tokens = QStringList::split(space, in.readLine()); |
145 | QStringList::ConstIterator it = tokens.begin(); | 155 | QStringList::ConstIterator it = tokens.begin(); |
146 | if ( it != tokens.end() ) { | 156 | if ( it != tokens.end() ) { |
147 | QString id = *it; ++it; | 157 | QString id = *it; ++it; |
158 | if ( it != tokens.end() ) { | ||
159 | (*extFor)[id] = *it; | ||
148 | while (it != tokens.end()) { | 160 | while (it != tokens.end()) { |
149 | (*typeFor)[*it] = id; | 161 | (*typeFor)[*it] = id; |
150 | ++it; | 162 | ++it; |
151 | } | 163 | } |
152 | } | 164 | } |
153 | } | 165 | } |
154 | } | 166 | } |
155 | } | 167 | } |
168 | } | ||
156 | 169 | ||
157 | void MimeType::init( const QString& ext_or_id ) | 170 | void MimeType::init( const QString& ext_or_id ) |
158 | { | 171 | { |
159 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { | 172 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { |
160 | i = ext_or_id.lower(); | 173 | i = ext_or_id.lower(); |
161 | } else { | 174 | } else { |
162 | loadExtensions(); | 175 | loadExtensions(); |
163 | int dot = ext_or_id.findRev('.'); | 176 | int dot = ext_or_id.findRev('.'); |
164 | QString ext = dot >= 0 ? ext_or_id.mid(dot+1) : ext_or_id; | 177 | QString ext = dot >= 0 ? ext_or_id.mid(dot+1) : ext_or_id; |
165 | i = (*typeFor)[ext.lower()]; | 178 | i = (*typeFor)[ext.lower()]; |
166 | if ( i.isNull() ) | 179 | if ( i.isNull() ) |
167 | i = "application/octet-stream"; | 180 | i = "application/octet-stream"; |