author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /library/mimetype.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
-rw-r--r-- | library/mimetype.cpp | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/library/mimetype.cpp b/library/mimetype.cpp new file mode 100644 index 0000000..c6a4453 --- a/dev/null +++ b/library/mimetype.cpp | |||
@@ -0,0 +1,204 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
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 | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
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. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "mimetype.h" | ||
22 | #include "applnk.h" | ||
23 | #include "resource.h" | ||
24 | #include "qpeapplication.h" | ||
25 | #include <qfile.h> | ||
26 | #include <qdict.h> | ||
27 | #include <qregexp.h> | ||
28 | #include <qstringlist.h> | ||
29 | #include <qtextstream.h> | ||
30 | #include <qmap.h> | ||
31 | |||
32 | class MimeTypeData { | ||
33 | public: | ||
34 | MimeTypeData(const QString& i, const AppLnk& lnk, const QString& icon ) : | ||
35 | id(i), | ||
36 | desc(lnk.name()+" document"), | ||
37 | app(lnk) | ||
38 | { | ||
39 | if ( icon.isEmpty() ) { | ||
40 | regIcon = lnk.pixmap(); | ||
41 | bigIcon = lnk.bigPixmap(); | ||
42 | } else { | ||
43 | QImage unscaledIcon = Resource::loadImage( icon ); | ||
44 | regIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | ||
45 | bigIcon.convertFromImage( unscaledIcon.smoothScale( AppLnk::bigIconSize(), AppLnk::bigIconSize() ) ); | ||
46 | } | ||
47 | } | ||
48 | QString id; | ||
49 | QString desc; | ||
50 | QPixmap regIcon; | ||
51 | QPixmap bigIcon; | ||
52 | AppLnk app; | ||
53 | }; | ||
54 | |||
55 | class MimeType::Dict : public QDict<MimeTypeData> { | ||
56 | public: | ||
57 | Dict() {} | ||
58 | }; | ||
59 | |||
60 | MimeType::Dict* MimeType::d=0; | ||
61 | static QMap<QString,QString> *typeFor = 0; | ||
62 | |||
63 | MimeType::Dict& MimeType::dict() | ||
64 | { | ||
65 | if ( !d ) { | ||
66 | d = new Dict; | ||
67 | d->setAutoDelete(TRUE); | ||
68 | } | ||
69 | return *d; | ||
70 | } | ||
71 | |||
72 | MimeType::MimeType( const QString& ext_or_id ) | ||
73 | { | ||
74 | init(ext_or_id); | ||
75 | } | ||
76 | |||
77 | MimeType::MimeType( const DocLnk& lnk ) | ||
78 | { | ||
79 | init(lnk.type()); | ||
80 | } | ||
81 | |||
82 | QString MimeType::id() const | ||
83 | { | ||
84 | return i; | ||
85 | } | ||
86 | |||
87 | QString MimeType::description() const | ||
88 | { | ||
89 | MimeTypeData* d = data(i); | ||
90 | return d ? d->desc : QString::null; | ||
91 | } | ||
92 | |||
93 | QPixmap MimeType::pixmap() const | ||
94 | { | ||
95 | MimeTypeData* d = data(i); | ||
96 | return d ? d->regIcon : QPixmap(); | ||
97 | } | ||
98 | |||
99 | QPixmap MimeType::bigPixmap() const | ||
100 | { | ||
101 | MimeTypeData* d = data(i); | ||
102 | return d ? d->bigIcon : QPixmap(); | ||
103 | } | ||
104 | |||
105 | const AppLnk* MimeType::application() const | ||
106 | { | ||
107 | MimeTypeData* d = data(i); | ||
108 | return d ? &d->app : 0; | ||
109 | } | ||
110 | |||
111 | void MimeType::registerApp( const AppLnk& lnk ) | ||
112 | { | ||
113 | QStringList list = lnk.mimeTypes(); | ||
114 | QStringList icons = lnk.mimeTypeIcons(); | ||
115 | QStringList::ConstIterator icon = icons.begin(); | ||
116 | for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it, ++icon) { | ||
117 | MimeTypeData *item = new MimeTypeData( *it, lnk, *icon ); | ||
118 | dict().replace( *it, item ); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | void MimeType::clear() | ||
123 | { | ||
124 | delete d; | ||
125 | d = 0; | ||
126 | } | ||
127 | |||
128 | void MimeType::loadExtensions() | ||
129 | { | ||
130 | if ( !typeFor ) { | ||
131 | typeFor = new QMap<QString,QString>; | ||
132 | loadExtensions("/etc/mime.types"); | ||
133 | loadExtensions(QPEApplication::qpeDir()+"etc/mime.types"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | void MimeType::loadExtensions(const QString& filename) | ||
138 | { | ||
139 | QFile file(filename); | ||
140 | if ( file.open(IO_ReadOnly) ) { | ||
141 | QTextStream in(&file); | ||
142 | QRegExp space("[ \t]+"); | ||
143 | while (!in.atEnd()) { | ||
144 | QStringList tokens = QStringList::split(space, in.readLine()); | ||
145 | QStringList::ConstIterator it = tokens.begin(); | ||
146 | if ( it != tokens.end() ) { | ||
147 | QString id = *it; ++it; | ||
148 | while (it != tokens.end()) { | ||
149 | (*typeFor)[*it] = id; | ||
150 | ++it; | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | void MimeType::init( const QString& ext_or_id ) | ||
158 | { | ||
159 | if ( ext_or_id[0] != '/' && ext_or_id.contains('/') ) { | ||
160 | i = ext_or_id.lower(); | ||
161 | } else { | ||
162 | loadExtensions(); | ||
163 | int dot = ext_or_id.findRev('.'); | ||
164 | QString ext = dot >= 0 ? ext_or_id.mid(dot+1) : ext_or_id; | ||
165 | i = (*typeFor)[ext.lower()]; | ||
166 | if ( i.isNull() ) | ||
167 | i = "application/octet-stream"; | ||
168 | } | ||
169 | static bool appsUpdated = FALSE; | ||
170 | if ( !appsUpdated ) { | ||
171 | appsUpdated = TRUE; | ||
172 | updateApplications(); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | MimeTypeData* MimeType::data(const QString& id) | ||
177 | { | ||
178 | MimeTypeData* d = dict()[id]; | ||
179 | if ( !d ) { | ||
180 | int s = id.find('/'); | ||
181 | QString idw = id.left(s)+"/*"; | ||
182 | d = dict()[idw]; | ||
183 | } | ||
184 | return d; | ||
185 | } | ||
186 | |||
187 | QString MimeType::appsFolderName() | ||
188 | { | ||
189 | return QPEApplication::qpeDir() + "apps"; | ||
190 | } | ||
191 | |||
192 | void MimeType::updateApplications() | ||
193 | { | ||
194 | clear(); | ||
195 | AppLnkSet apps( appsFolderName() ); | ||
196 | updateApplications(&apps); | ||
197 | } | ||
198 | |||
199 | void MimeType::updateApplications(AppLnkSet* folder) | ||
200 | { | ||
201 | for ( QListIterator<AppLnk> it( folder->children() ); it.current(); ++it ) { | ||
202 | registerApp(*it.current()); | ||
203 | } | ||
204 | } | ||