ROOT  6.06/09
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 
45 TGMimeTypes::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;
57  fChanged = kFALSE;
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 
141  fChanged = kFALSE;
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) {
172  TObject::operator=(gmt);
173  fClient=gmt.fClient;
174  fFilename=gmt.fFilename;
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 
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 
203 const 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 
225 Bool_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 
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 {
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 
309  fChanged = kFALSE;
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// Add a mime type to the list of mime types.
314 
315 void 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 }
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.
TString fType
Definition: TGMimeTypes.h:41
void Delete(Option_t *option="")
Remove all objects from the collection AND delete all heap based objects.
void Print(Option_t *option="") const
Print list of mime types.
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
TLine * line
const char Option_t
Definition: RtypesCore.h:62
const TGPicture * GetIcon(const char *filename, Bool_t small_icon)
Return icon belonging to mime type of filename.
void SaveMimes()
Save mime types in user's mime type file.
~TGMime()
Delete mime object.
virtual const char * HomeDirectory(const char *userName=0)
Return the user's home directory.
Definition: TSystem.cxx:881
Regular expression class.
Definition: TRegexp.h:35
TGMimeTypes & operator=(const TGMimeTypes &gmt)
Assignment operator.
static const char * filename()
TOrdCollection * fList
Definition: TGMimeTypes.h:60
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TGMimeTypes(const TGMimeTypes &gmt)
Copy constructor.
TGClient * fClient
Definition: TGMimeTypes.h:57
Bool_t GetType(const char *filename, char *type)
Return in type the mime type belonging to filename.
TString fSIcon
Definition: TGMimeTypes.h:45
TGMime * Find(const char *filename)
Given a filename find the matching mime type object.
const char * Data() const
Definition: TString.h:349
TRegexp * fReg
Definition: TGMimeTypes.h:46
static const std::string pattern("pattern")
ClassImp(TGMimeTypes) TGMimeTypes
Create a mime type cache.
Definition: TGMimeTypes.cxx:39
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.cxx:102
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void Error(const char *location, const char *msgfmt,...)
TString fFilename
Definition: TGMimeTypes.h:58
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
TString fPattern
Definition: TGMimeTypes.h:42
Bool_t fChanged
Definition: TGMimeTypes.h:59
char * Strip(const char *str, char c= ' ')
Strip leading and trailing c (blanks by default) from a string.
Definition: TString.cxx:2477
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2321
TMarker * m
Definition: textangle.C:8
void AddFirst(TObject *obj)
Insert object at beginning of collection.
void Warning(const char *location, const char *msgfmt,...)
virtual ~TGMimeTypes()
Delete mime type pool.
Bool_t GetAction(const char *filename, char *action)
Return in action the mime action string belonging to filename.
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition: TGClient.cxx:287
int type
Definition: TGX11.cxx:120
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Mother of all ROOT objects.
Definition: TObject.h:58
TString fIcon
Definition: TGMimeTypes.h:44
const Ssiz_t kNPOS
Definition: Rtypes.h:115
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
TString fAction
Definition: TGMimeTypes.h:43
const char * AsString() const
Return the date & time as a string (ctime() format).
Definition: TDatime.cxx:99
const Bool_t kTRUE
Definition: Rtypes.h:91
Ordered collection.
const char * cnt
Definition: TXMLSetup.cxx:75
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:39