-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 | |||
@@ -1,179 +1,192 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of 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 | #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 { |
33 | public: | 34 | public: |
34 | MimeTypeData(const QString& i, const AppLnk& lnk, const QString& icon ) : | 35 | MimeTypeData(const QString& i, const AppLnk& lnk, const QString& icon ) : |
35 | id(i), | 36 | id(i), |
36 | desc(lnk.name()+" document"), | 37 | desc(lnk.name()+" document"), |
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 | { |
74 | init(ext_or_id); | 77 | init(ext_or_id); |
75 | } | 78 | } |
76 | 79 | ||
77 | MimeType::MimeType( const DocLnk& lnk ) | 80 | MimeType::MimeType( const DocLnk& lnk ) |
78 | { | 81 | { |
79 | init(lnk.type()); | 82 | init(lnk.type()); |
80 | } | 83 | } |
81 | 84 | ||
82 | QString MimeType::id() const | 85 | QString MimeType::id() const |
83 | { | 86 | { |
84 | return i; | 87 | return i; |
85 | } | 88 | } |
86 | 89 | ||
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 | ||
111 | void MimeType::registerApp( const AppLnk& lnk ) | 120 | void MimeType::registerApp( const AppLnk& lnk ) |
112 | { | 121 | { |
113 | QStringList list = lnk.mimeTypes(); | 122 | QStringList list = lnk.mimeTypes(); |
114 | QStringList icons = lnk.mimeTypeIcons(); | 123 | QStringList icons = lnk.mimeTypeIcons(); |
115 | QStringList::ConstIterator icon = icons.begin(); | 124 | QStringList::ConstIterator icon = icons.begin(); |
116 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it, ++icon) { | 125 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it, ++icon) { |
117 | MimeTypeData *item = new MimeTypeData( *it, lnk, *icon ); | 126 | MimeTypeData *item = new MimeTypeData( *it, lnk, *icon ); |
118 | dict().replace( *it, item ); | 127 | dict().replace( *it, item ); |
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"; |
168 | } | 181 | } |
169 | static bool appsUpdated = FALSE; | 182 | static bool appsUpdated = FALSE; |
170 | if ( !appsUpdated ) { | 183 | if ( !appsUpdated ) { |
171 | appsUpdated = TRUE; | 184 | appsUpdated = TRUE; |
172 | updateApplications(); | 185 | updateApplications(); |
173 | } | 186 | } |
174 | } | 187 | } |
175 | 188 | ||
176 | MimeTypeData* MimeType::data(const QString& id) | 189 | MimeTypeData* MimeType::data(const QString& id) |
177 | { | 190 | { |
178 | MimeTypeData* d = dict()[id]; | 191 | MimeTypeData* d = dict()[id]; |
179 | if ( !d ) { | 192 | if ( !d ) { |