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
39
40////////////////////////////////////////////////////////////////////////////////
41/// Create a mime type cache. Read the mime types file "filename" and
42/// built a list of mime types.
43
45{
46 char line[1024] = {0};
47 char mime[1024] = {0};
48 char pattern[256] = {0};
49 char icon[256] = {0};
50 char sicon[256] = {0};
51 char action[256] = {0};
52 char *s;
53
54 fClient = client;
57 fList = new TOrdCollection(50);
58
59 FILE *mfp;
60 mfp = fopen(filename, "r");
61 if (!mfp) {
62 Warning("TGMimeTypes", "error opening mime type file %s", filename);
63 return;
64 }
65
66 int cnt = 0;
67 while (fgets(line, 1024, mfp)) {
68 s = line;
69 s[strlen(line)-1] = 0; // strip off trailing \n
70 while (*s == ' ') s++; // strip leading blanks
71 if (*s == '#') continue; // skip comments
72 if (!s[0]) continue; // skip empty lines
73
74 if (*s == '[') {
75 strlcpy(mime, line, 1024);
76 cnt = 0;
77 continue;
78 }
79 if (!strncmp(s, "pattern", 7)) {
80 if (!(s = strchr(line, '='))) {
81 Error("TGMimeTypes", "malformed pattern line, = missing");
82 pattern[0] = 0;
83 } else {
84 s++;
85 s = Strip(s);
86 strlcpy(pattern, s, 256);
87 delete [] s;
88 }
89 cnt++;
90 } else if (!strncmp(s, "icon", 4)) {
91 if (!(s = strchr(line, '='))) {
92 Error("TGMimeTypes", "malformed icon line, = missing");
93 icon[0] = 0;
94 } else {
95 s++;
96 s = Strip(s);
97 char *s2;
98 if ((s2 = strchr(s, ' '))) {
99 *s2 = 0;
100 strlcpy(icon, s, 256);
101 s2++;
102 s2 = Strip(s2);
103 strlcpy(sicon, s2, 256);
104 delete [] s2;
105 } else {
106 strlcpy(icon, s, 256);
107 strlcpy(sicon, s, 256);
108 }
109 delete [] s;
110 }
111 cnt++;
112 } else if (!strncmp(s, "action", 6)) {
113 if (!(s = strchr(line, '='))) {
114 Error("TGMimeTypes", "malformed action line, = missing");
115 action[0] = 0;
116 } else {
117 s++;
118 s = Strip(s);
119 strlcpy(action, s, 256);
120 delete [] s;
121 }
122 cnt++;
123 }
124
125 if (cnt == 3) {
126 if (strchr(pattern, ' ')) {
127 char *tmppattern = strtok(pattern, " ");
128 while (tmppattern && (*tmppattern != ' ')) {
130 tmppattern = strtok(0, " ");
131 }
132 } else {
133 AddType(mime, pattern, icon, sicon, action);
134 }
135 }
136 }
137
138 fclose(mfp);
139
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Delete mime type pool.
145
147{
148 if (fChanged) SaveMimes();
149 fList->Delete();
150 delete fList;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Copy constructor
155
157 TObject(gmt),
158 fClient(gmt.fClient),
159 fFilename(gmt.fFilename),
160 fChanged(gmt.fChanged),
161 fList(gmt.fList)
162{
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Assignment operator
167
169{
170 if(this!=&gmt) {
172 fClient=gmt.fClient;
173 fFilename=gmt.fFilename;
174 fChanged=gmt.fChanged;
175 fList=gmt.fList;
176 }
177 return *this;
178}
179
180
181////////////////////////////////////////////////////////////////////////////////
182/// Given a filename find the matching mime type object.
183
185{
186 if (!filename) return 0;
187
189
190 TGMime *mime;
191 TIter next(fList);
192
193 while ((mime = (TGMime *) next()))
194 if (fn.Index(*(mime->fReg)) != kNPOS) return mime;
195
196 return 0;
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Return icon belonging to mime type of filename.
201
203{
204 TGMime *mime;
205 const TGPicture *mypic = 0;
206
207 if ((mime = Find(filename))) {
208 Bool_t thumb = (mime->fType == "[thumbnail]");
209 if (small_icon) {
210 mypic = thumb ? fClient->GetPicture(mime->fSIcon.Data(), 32, 32) :
211 fClient->GetPicture(mime->fSIcon.Data(), 16, 16);
212 } else {
213 mypic = thumb ? fClient->GetPicture(mime->fIcon.Data(), 64, 64) :
214 fClient->GetPicture(mime->fIcon.Data(), 32, 32);
215 }
216 return mypic;
217 }
218 return 0;
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Return in action the mime action string belonging to filename.
223
225{
226 TGMime *mime;
227
228 action[0] = 0;
229 if ((mime = Find(filename))) {
230 strlcpy(action, mime->fAction.Data(), 512);
231 return (strlen(action) > 0);
232 }
233 return kFALSE;
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// Return in type the mime type belonging to filename.
238
240{
241 TGMime *mime;
242
243 memset(type, 0, strlen(type));
244 if ((mime = Find(filename))) {
245 strlcpy(type, mime->fType.Data(), 256);
246 return (strlen(type) > 0);
247 }
248 return kFALSE;
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Print list of mime types.
253
255{
256 TGMime *m;
257 TIter next(fList);
258
259 while ((m = (TGMime *) next())) {
260 printf("Type: %s\n", m->fType.Data());
261 printf("Pattern: %s\n", m->fPattern.Data());
262 if (m->fIcon != m->fSIcon)
263 printf("Icon: %s %s\n", m->fIcon.Data(), m->fSIcon.Data());
264 else
265 printf("Icon: %s\n", m->fIcon.Data());
266 printf("Action: %s\n", m->fAction.Data());
267 printf("------------\n\n");
268 }
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Save mime types in user's mime type file.
273
275{
277#ifdef WIN32
278 filename.Form("%s\\.root.mimes", gSystem->HomeDirectory());
279#else
280 filename.Form("%s/.root.mimes", gSystem->HomeDirectory());
281#endif
282
283 FILE *fp = fopen(filename.Data(), "wt");
284
285 if (!fp) {
286 Error("SaveMimes", "can not open %s to store mime types", filename.Data());
287 return;
288 }
289
290 TDatime dt;
291 fprintf(fp, "# %s written on %s\n\n", filename.Data(), dt.AsString());
292
293 TGMime *m;
294 TIter next(fList);
295
296 while ((m = (TGMime *) next())) {
297 fprintf(fp, "%s\n", m->fType.Data());
298 fprintf(fp, "pattern = %s\n", m->fPattern.Data());
299 if (m->fIcon != m->fSIcon)
300 fprintf(fp, "icon = %s %s\n", m->fIcon.Data(), m->fSIcon.Data());
301 else
302 fprintf(fp, "icon = %s\n", m->fIcon.Data());
303 fprintf(fp, "action = %s\n\n", m->fAction.Data());
304 }
305
306 fclose(fp);
307
309}
310
311////////////////////////////////////////////////////////////////////////////////
312/// Add a mime type to the list of mime types.
313
314void TGMimeTypes::AddType(const char *type, const char *pattern, const char *icon,
315 const char *sicon, const char *action)
316{
317 TGMime *mime = new TGMime;
318
319 mime->fType = type;
320 mime->fPattern = pattern;
321 mime->fIcon = icon;
322 mime->fSIcon = sicon;
323 mime->fAction = action;
324
325 mime->fReg = new TRegexp(pattern, kTRUE);
326
328
329 fChanged = kTRUE;
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Delete mime object.
334
336{
337 delete fReg;
338}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:131
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:2527
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition TDatime.h:37
Window client.
Definition TGClient.h:37
const TGPicture * GetPicture(const char *name)
Get picture from the picture pool.
Definition TGClient.cxx:288
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
TRegexp * fReg
pattern regular expression
Definition TGMimeTypes.h:39
~TGMime() override
Delete mime object.
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) noexcept
TObject assignment operator.
Definition TObject.h:299
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
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:138
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:897
TLine * line
TMarker m
Definition textangle.C:8