Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class TGMimeTypes
24 \ingroup guiwidgets
25
26This class handles mime types, used by browsers to map file types
27to applications and icons.
28
29*/
30
31
32#include "TGMimeTypes.h"
33#include "TOrdCollection.h"
34#include "TSystem.h"
35#include "TDatime.h"
36#include "TRegexp.h"
37#include "strlcpy.h"
38
40
41////////////////////////////////////////////////////////////////////////////////
42/// Create a mime type cache. Read the mime types file "filename" and
43/// built a list of mime types.
44
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;
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
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
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
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}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char * Strip(const char *str, char c=' ')
Strip leading and trailing c (blanks by default) from a string.
Definition TString.cxx:2499
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
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:102
Window client.
Definition TGClient.h:37
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:289
This class handles mime types, used by browsers to map file types to applications and icons.
Definition TGMimeTypes.h:47
TGMimeTypes & operator=(const TGMimeTypes &gmt)
Assignment operator.
Bool_t fChanged
true if file has changed
Definition TGMimeTypes.h:52
void SaveMimes()
Save mime types in user's mime type file.
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
list of mime types
Definition TGMimeTypes.h:53
TGMimeTypes(const TGMimeTypes &gmt)
Copy constructor.
~TGMimeTypes() override
Delete mime type pool.
TGClient * fClient
client to which mime types belong (display server)
Definition TGMimeTypes.h:50
void Print(Option_t *option="") const override
Print list of mime types.
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
file name of mime type file
Definition TGMimeTypes.h:51
TGMime is internally used by TGMimeTypes.
Definition TGMimeTypes.h:29
TString fType
mime type
Definition TGMimeTypes.h:34
TString fPattern
filename pattern
Definition TGMimeTypes.h:35
TRegexp * fReg
pattern regular expression
Definition TGMimeTypes.h:39
TString fIcon
associated icon (32x32)
Definition TGMimeTypes.h:37
TString fAction
associated action
Definition TGMimeTypes.h:36
~TGMime() override
Delete mime object.
TString fSIcon
associated small icon (16x16)
Definition TGMimeTypes.h:38
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
Mother of all ROOT objects.
Definition TObject.h:41
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition TObject.h:296
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:973
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
Ordered collection.
void AddFirst(TObject *obj) override
Insert object at beginning of collection.
void Delete(Option_t *option="") override
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:139
const char * Data() const
Definition TString.h:380
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:874
TLine * line
TMarker m
Definition textangle.C:8