Logo ROOT  
Reference Guide
TGMimeTypes.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id: c44ba45210ec143ec5bf9aa1708855c60088e954 $
2// Author: Fons Rademakers 18/01/98
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11/**************************************************************************
12
13 This source is based on Xclass95, a Win95-looking GUI toolkit.
14 Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15
16 Xclass95 is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Library General Public
18 License as published by the Free Software Foundation; either
19 version 2 of the License, or (at your option) any later version.
20
21**************************************************************************/
22
23//////////////////////////////////////////////////////////////////////////
24// //
25// TGMimeTypes and TGMime //
26// //
27// This class handles mime types, used by browsers to map file types //
28// to applications and icons. TGMime is internally used by TGMimeType. //
29// //
30//////////////////////////////////////////////////////////////////////////
31
32#include "TGMimeTypes.h"
33#include "TOrdCollection.h"
34#include "TSystem.h"
35#include "TDatime.h"
36#include "TRegexp.h"
37
38
40
41////////////////////////////////////////////////////////////////////////////////
42/// Create a mime type cache. Read the mime types file "filename" and
43/// built a list of mime types.
44
45TGMimeTypes::TGMimeTypes(TGClient *client, const char *filename)
46{
47 char line[1024] = {0};
48 char mime[1024] = {0};
49 char pattern[256] = {0};
50 char icon[256] = {0};
51 char sicon[256] = {0};
52 char action[256] = {0};
53 char *s;
54
55 fClient = client;
56 fFilename = filename;
58 fList = new TOrdCollection(50);
59
60 FILE *mfp;
61 mfp = fopen(filename, "r");
62 if (!mfp) {
63 Warning("TGMimeTypes", "error opening mime type file %s", filename);
64 return;
65 }
66
67 int cnt = 0;
68 while (fgets(line, 1024, mfp)) {
69 s = line;
70 s[strlen(line)-1] = 0; // strip off trailing \n
71 while (*s == ' ') s++; // strip leading blanks
72 if (*s == '#') continue; // skip comments
73 if (!s[0]) continue; // skip empty lines
74
75 if (*s == '[') {
76 strlcpy(mime, line, 1024);
77 cnt = 0;
78 continue;
79 }
80 if (!strncmp(s, "pattern", 7)) {
81 if (!(s = strchr(line, '='))) {
82 Error("TGMimeTypes", "malformed pattern line, = missing");
83 pattern[0] = 0;
84 } else {
85 s++;
86 s = Strip(s);
87 strlcpy(pattern, s, 256);
88 delete [] s;
89 }
90 cnt++;
91 } else if (!strncmp(s, "icon", 4)) {
92 if (!(s = strchr(line, '='))) {
93 Error("TGMimeTypes", "malformed icon line, = missing");
94 icon[0] = 0;
95 } else {
96 s++;
97 s = Strip(s);
98 char *s2;
99 if ((s2 = strchr(s, ' '))) {
100 *s2 = 0;
101 strlcpy(icon, s, 256);
102 s2++;
103 s2 = Strip(s2);
104 strlcpy(sicon, s2, 256);
105 delete [] s2;
106 } else {
107 strlcpy(icon, s, 256);
108 strlcpy(sicon, s, 256);
109 }
110 delete [] s;
111 }
112 cnt++;
113 } else if (!strncmp(s, "action", 6)) {
114 if (!(s = strchr(line, '='))) {
115 Error("TGMimeTypes", "malformed action line, = missing");
116 action[0] = 0;
117 } else {
118 s++;
119 s = Strip(s);
120 strlcpy(action, s, 256);
121 delete [] s;
122 }
123 cnt++;
124 }
125
126 if (cnt == 3) {
127 if (strchr(pattern, ' ')) {
128 char *tmppattern = strtok(pattern, " ");
129 while (tmppattern && (*tmppattern != ' ')) {
130 AddType(mime, tmppattern, icon, sicon, action);
131 tmppattern = strtok(0, " ");
132 }
133 } else {
134 AddType(mime, pattern, icon, sicon, action);
135 }
136 }
137 }
138
139 fclose(mfp);
140
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Delete mime type pool.
146
148{
149 if (fChanged) SaveMimes();
150 fList->Delete();
151 delete fList;
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Copy constructor
156
158 TObject(gmt),
159 fClient(gmt.fClient),
160 fFilename(gmt.fFilename),
161 fChanged(gmt.fChanged),
162 fList(gmt.fList)
163{
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Assignment operator
168
170{
171 if(this!=&gmt) {
173 fClient=gmt.fClient;
175 fChanged=gmt.fChanged;
176 fList=gmt.fList;
177 }
178 return *this;
179}
180
181
182////////////////////////////////////////////////////////////////////////////////
183/// Given a filename find the matching mime type object.
184
185TGMime *TGMimeTypes::Find(const char *filename)
186{
187 if (!filename) return 0;
188
189 TString fn = filename;
190
191 TGMime *mime;
192 TIter next(fList);
193
194 while ((mime = (TGMime *) next()))
195 if (fn.Index(*(mime->fReg)) != kNPOS) return mime;
196
197 return 0;
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// Return icon belonging to mime type of filename.
202
203const TGPicture *TGMimeTypes::GetIcon(const char *filename, Bool_t small_icon)
204{
205 TGMime *mime;
206 const TGPicture *mypic = 0;
207
208 if ((mime = Find(filename))) {
209 Bool_t thumb = (mime->fType == "[thumbnail]");
210 if (small_icon) {
211 mypic = thumb ? fClient->GetPicture(mime->fSIcon.Data(), 32, 32) :
212 fClient->GetPicture(mime->fSIcon.Data(), 16, 16);
213 } else {
214 mypic = thumb ? fClient->GetPicture(mime->fIcon.Data(), 64, 64) :
215 fClient->GetPicture(mime->fIcon.Data(), 32, 32);
216 }
217 return mypic;
218 }
219 return 0;
220}
221
222////////////////////////////////////////////////////////////////////////////////
223/// Return in action the mime action string belonging to filename.
224
225Bool_t TGMimeTypes::GetAction(const char *filename, char *action)
226{
227 TGMime *mime;
228
229 action[0] = 0;
230 if ((mime = Find(filename))) {
231 strlcpy(action, mime->fAction.Data(), 512);
232 return (strlen(action) > 0);
233 }
234 return kFALSE;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Return in type the mime type belonging to filename.
239
240Bool_t TGMimeTypes::GetType(const char *filename, char *type)
241{
242 TGMime *mime;
243
244 memset(type, 0, strlen(type));
245 if ((mime = Find(filename))) {
246 strlcpy(type, mime->fType.Data(), 256);
247 return (strlen(type) > 0);
248 }
249 return kFALSE;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Print list of mime types.
254
256{
257 TGMime *m;
258 TIter next(fList);
259
260 while ((m = (TGMime *) next())) {
261 printf("Type: %s\n", m->fType.Data());
262 printf("Pattern: %s\n", m->fPattern.Data());
263 if (m->fIcon != m->fSIcon)
264 printf("Icon: %s %s\n", m->fIcon.Data(), m->fSIcon.Data());
265 else
266 printf("Icon: %s\n", m->fIcon.Data());
267 printf("Action: %s\n", m->fAction.Data());
268 printf("------------\n\n");
269 }
270}
271
272////////////////////////////////////////////////////////////////////////////////
273/// Save mime types in user's mime type file.
274
276{
277 TString filename;
278#ifdef WIN32
279 filename.Form("%s\\.root.mimes", gSystem->HomeDirectory());
280#else
281 filename.Form("%s/.root.mimes", gSystem->HomeDirectory());
282#endif
283
284 FILE *fp = fopen(filename.Data(), "wt");
285
286 if (!fp) {
287 Error("SaveMimes", "can not open %s to store mime types", filename.Data());
288 return;
289 }
290
291 TDatime dt;
292 fprintf(fp, "# %s written on %s\n\n", filename.Data(), dt.AsString());
293
294 TGMime *m;
295 TIter next(fList);
296
297 while ((m = (TGMime *) next())) {
298 fprintf(fp, "%s\n", m->fType.Data());
299 fprintf(fp, "pattern = %s\n", m->fPattern.Data());
300 if (m->fIcon != m->fSIcon)
301 fprintf(fp, "icon = %s %s\n", m->fIcon.Data(), m->fSIcon.Data());
302 else
303 fprintf(fp, "icon = %s\n", m->fIcon.Data());
304 fprintf(fp, "action = %s\n\n", m->fAction.Data());
305 }
306
307 fclose(fp);
308
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// Add a mime type to the list of mime types.
314
315void TGMimeTypes::AddType(const char *type, const char *pattern, const char *icon,
316 const char *sicon, const char *action)
317{
318 TGMime *mime = new TGMime;
319
320 mime->fType = type;
321 mime->fPattern = pattern;
322 mime->fIcon = icon;
323 mime->fSIcon = sicon;
324 mime->fAction = action;
325
326 mime->fReg = new TRegexp(pattern, kTRUE);
327
328 fList->AddFirst(mime);
329
330 fChanged = kTRUE;
331}
332
333////////////////////////////////////////////////////////////////////////////////
334/// Delete mime object.
335
337{
338 delete fReg;
339}
const Ssiz_t kNPOS
Definition: RtypesCore.h:113
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
int type
Definition: TGX11.cxx:120
char * Strip(const char *str, char c=' ')
Strip leading and trailing c (blanks by default) from a string.
Definition: TString.cxx:2454
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
const char * AsString() const
Return the date & time as a string (ctime() format).
Definition: TDatime.cxx:101
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition: TGClient.cxx:289
TGMimeTypes & operator=(const TGMimeTypes &gmt)
Assignment operator.
void Print(Option_t *option="") const
Print list of mime types.
Bool_t fChanged
Definition: TGMimeTypes.h:55
void SaveMimes()
Save mime types in user's mime type file.
virtual ~TGMimeTypes()
Delete mime type pool.
TGMime * Find(const char *filename)
Given a filename find the matching mime type object.
void AddType(const char *type, const char *pat, const char *icon, const char *sicon, const char *action)
Add a mime type to the list of mime types.
TOrdCollection * fList
Definition: TGMimeTypes.h:56
TGMimeTypes(const TGMimeTypes &gmt)
Copy constructor.
TGClient * fClient
Definition: TGMimeTypes.h:53
const TGPicture * GetIcon(const char *filename, Bool_t small_icon)
Return icon belonging to mime type of filename.
Bool_t GetAction(const char *filename, char *action)
Return in action the mime action string belonging to filename.
Bool_t GetType(const char *filename, char *type)
Return in type the mime type belonging to filename.
TString fFilename
Definition: TGMimeTypes.h:54
~TGMime()
Delete mime object.
TString fType
Definition: TGMimeTypes.h:37
TString fPattern
Definition: TGMimeTypes.h:38
TRegexp * fReg
Definition: TGMimeTypes.h:42
TString fIcon
Definition: TGMimeTypes.h:40
TString fAction
Definition: TGMimeTypes.h:39
TString fSIcon
Definition: TGMimeTypes.h:41
Mother of all ROOT objects.
Definition: TObject.h:37
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:283
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:877
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
Ordered collection.
void AddFirst(TObject *obj)
Insert object at beginning of collection.
void Delete(Option_t *option="")
Remove all objects from the collection AND delete all heap based objects.
Regular expression class.
Definition: TRegexp.h:31
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:634
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition: TSystem.cxx:883
TLine * line
static const std::string pattern("pattern")
static constexpr double s
const char * cnt
Definition: TXMLSetup.cxx:74
auto * m
Definition: textangle.C:8