forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TQMimeTypes.cxx
339 lines (283 loc) · 9.61 KB
/
TQMimeTypes.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// @(#)root/qt:$Id: 98e9b85f582807ea6469888efd07a1ab980ac70a $
// Author: Valeri Fine 21/01/2003
/*************************************************************************
* Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
* Copyright (C) 2003 by Valeri Fine. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// TQMimeTypes and TQMime //
// //
// This class handles mime types, used by browsers to map file types //
// to applications and icons. TQMime is internally used by TQMimeType. //
// This class does allow the Qt-base gui to get the same Mime types //
// as ROOT mime defines //
// //
//////////////////////////////////////////////////////////////////////////
#include "TQMimeTypes.h"
#include "TOrdCollection.h"
#include "TSystem.h"
#include "TSystemFile.h"
#include "TDatime.h"
#include "TRegexp.h"
#include "TQtRConfig.h"
#include <QIcon>
#include <QFileIconProvider>
#include <QPixmap>
#include <qfileinfo.h>
ClassImp(TQMimeTypes)
QFileIconProvider *TQMimeTypes::fgDefaultProvider = 0; // Default provider of the system icons;
////////////////////////////////////////////////////////////////////////////////
QIcon
TQMimeTypes::IconProvider(const QFileInfo &info)
{
if (!fgDefaultProvider) {
fgDefaultProvider = new QFileIconProvider;
}
return fgDefaultProvider->icon(info);
}
////////////////////////////////////////////////////////////////////////////////
/// Create a mime type cache. Read the mime types file "filename" and
/// built a list of mime types.
TQMimeTypes::TQMimeTypes(const char *iconPath, const char *filename)
{
char line[1024];
char mime[1024];
char pattern[256];
char icon[256];
char sicon[256];
char action[256];
char *s;
fIconPath = iconPath;
fFilename = filename;
fChanged = kFALSE;
fList = new TOrdCollection(50);
FILE *mfp;
mfp = fopen(filename, "r");
if (!mfp) {
Warning("TQMimeTypes", "error opening mime type file %s", filename);
return;
}
int cnt = 0;
while (fgets(line, 1024, mfp)) {
s = line;
s[strlen(line)-1] = 0; // strip off trailing \n
while (*s == ' ') s++; // strip leading blanks
if (*s == '#') continue; // skip comments
if (!s[0]) continue; // skip empty lines
if (*s == '[') {
strlcpy(mime, line,1024);
cnt = 0;
continue;
}
if (!strncmp(s, "pattern", 7)) {
if (!(s = strchr(line, '='))) {
Error("TQMimeTypes", "malformed pattern line, = missing");
pattern[0] = 0;
} else {
s++;
s = Strip(s);
strlcpy(pattern, s,256);
delete [] s;
}
cnt++;
} else if (!strncmp(s, "icon", 4)) {
if (!(s = strchr(line, '='))) {
Error("TQMimeTypes", "malformed icon line, = missing");
icon[0] = 0;
} else {
s++;
s = Strip(s);
char *s2;
if ((s2 = strchr(s, ' '))) {
*s2 = 0;
strlcpy(icon, s,256);
s2++;
s2 = Strip(s2);
strlcpy(sicon, s2,256);
delete [] s2;
} else {
strlcpy(icon, s,256);
strlcpy(sicon, s,256);
}
delete [] s;
}
cnt++;
} else if (!strncmp(s, "action", 6)) {
if (!(s = strchr(line, '='))) {
Error("TQMimeTypes", "malformed action line, = missing");
action[0] = 0;
} else {
s++;
s = Strip(s);
strlcpy(action, s,256);
delete [] s;
}
cnt++;
}
if (cnt == 3) {
if (strchr(pattern, ' ')) {
char *tmppattern = strtok(pattern, " ");
while (tmppattern && (*tmppattern != ' ')) {
AddType(mime, tmppattern, icon, sicon, action);
tmppattern = strtok(0, " ");
}
} else {
AddType(mime, pattern, icon, sicon, action);
}
}
}
fclose(mfp);
fChanged = kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Delete mime type pool.
TQMimeTypes::~TQMimeTypes()
{
if (fChanged) SaveMimes();
fList->Delete();
delete fList;
}
////////////////////////////////////////////////////////////////////////////////
/// Given a filename find the matching mime type object.
TQMime *TQMimeTypes::Find(const char *filename) const
{
if (!filename) return 0;
TString fn = filename;
TQMime *mime;
TIter next(fList);
while ((mime = (TQMime *) next()))
if (fn.Index(*(mime->fReg)) != kNPOS) return mime;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Return icon belonging to mime type of filename.
const QIcon *TQMimeTypes::GetIcon(const char *filename) const
{
TQMime *mime= Find(filename);
if (mime) return mime->fIcon;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Return icon belonging to mime type of TSystemFile extension
const QIcon *TQMimeTypes::GetIcon(const TSystemFile *filename)
{
const char *name = filename->GetName();
const QIcon *set = GetIcon(name);
if (!set) set = AddType(filename);
return set;
}
////////////////////////////////////////////////////////////////////////////////
///
const QIcon *TQMimeTypes::AddType(const TSystemFile *filename)
{
QFileInfo info(filename->GetName());
const QIcon icon = IconProvider(info);
if (icon.isNull()) return 0;
// Add an artificial mime type to the list of mime types from the default system
TQMime *mime = new TQMime;
mime->fType = "system/file";
mime->fPattern = "*.";
mime->fPattern += info.suffix().toAscii().data();
mime->fIcon = 0;
mime->fIcon = new QIcon(icon) ;
#ifdef R__QTWIN32
mime->fAction = "!%s";
#else
mime->fAction = "";
#endif
mime->fReg = new TRegexp(mime->fPattern, kTRUE);
fList->Add(mime);
fChanged = kTRUE;
return mime->fIcon;
}
////////////////////////////////////////////////////////////////////////////////
/// Return in action the mime action string belonging to filename.
Bool_t TQMimeTypes::GetAction(const char *filename, char *action) const
{
TQMime *mime;
action[0] = 0;
if ((mime = Find(filename))) {
strcpy(action, mime->fAction.Data());
return (strlen(action) > 0);
}
return kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Return in type the mime type belonging to filename.
Bool_t TQMimeTypes::GetType(const char *filename, char *type) const
{
TQMime *mime;
memset(type, 0, strlen(type));
if ((mime = Find(filename))) {
strcpy(type, mime->fType.Data());
return (strlen(type) > 0);
}
return kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Print list of mime types.
void TQMimeTypes::Print(Option_t *) const
{
TQMime *m;
TIter next(fList);
while ((m = (TQMime *) next())) {
printf("Type: %s\n", m->fType.Data());
printf("Pattern: %s\n", m->fPattern.Data());
printf("Icon: %p\n", m->fIcon);
printf("Action: %s\n", m->fAction.Data());
printf("------------\n\n");
}
}
////////////////////////////////////////////////////////////////////////////////
/// Save mime types in user's mime type file.
void TQMimeTypes::SaveMimes()
{
char filename[1024];
snprintf(filename,1024, "%s/.root.mimes", gSystem->HomeDirectory());
FILE *fp = fopen(filename, "w");
if (!fp) {
Error("SaveMimes", "can not open %s to store mime types", filename);
return;
}
TDatime dt;
fprintf(fp, "# %s written on %s\n\n", filename, dt.AsString());
TQMime *m;
TIter next(fList);
while ((m = (TQMime *) next())) {
fprintf(fp, "%s\n", m->fType.Data());
fprintf(fp, "pattern = %s\n", m->fPattern.Data());
fprintf(fp, "icon = %p\n", m->fIcon);
fprintf(fp, "action = %s\n\n", m->fAction.Data());
}
fclose(fp);
fChanged = kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Add a mime type to the list of mime types.
void TQMimeTypes::AddType(const char *type, const char *pattern, const char *icon,
const char * /*sicon*/, const char *action)
{
TQMime *mime = new TQMime;
mime->fType = type;
mime->fPattern = pattern;
mime->fIcon = 0;
char *picnam = gSystem->Which(fIconPath.Data(),icon, kReadPermission);
if (picnam) mime->fIcon = new QIcon( QPixmap(picnam) ) ;
delete [] picnam;
mime->fAction = action;
mime->fReg = new TRegexp(pattern, kTRUE);
fList->Add(mime);
fChanged = kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Delete mime object.
TQMime::~TQMime()
{
delete fIcon; fIcon = 0;
delete fReg;
}