]>
Commit | Line | Data |
---|---|---|
b13d92d1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
7dc3cc31 | 2 | // Name: unix/mimetype.cpp |
b13d92d1 VZ |
3 | // Purpose: classes and functions to manage MIME types |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 23.09.98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
6aa89a22 | 9 | // Licence: wxWindows licence (part of wxExtra library) |
b13d92d1 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2b813b73 VZ |
12 | // known bugs; there may be others!! chris elliott, biol75@york.ac.uk 27 Mar 01 |
13 | ||
14 | // 1) .mailcap and .mimetypes can be either in a netscape or metamail format | |
15 | // and entries may get confused during writing (I've tried to fix this; please let me know | |
16 | // any files that fail) | |
17 | // 2) KDE and Gnome do not yet fully support international read/write | |
18 | // 3) Gnome key lines like open.latex."LaTeX this file"=latex %f will have odd results | |
19 | // 4) writing to files comments out the existing data; I hope this avoids losing | |
20 | // any data which we could not read, and data which we did not store like test= | |
21 | // 5) results from reading files with multiple entries (especially matches with type/* ) | |
22 | // may (or may not) work for getXXX commands | |
23 | // 6) Loading the png icons in Gnome doesn't work for me... | |
24 | // 7) In Gnome, if keys.mime exists but keys.users does not, there is | |
25 | // an error message in debug mode, but the file is still written OK | |
26 | // 8) Deleting entries is only allowed from the user file; sytem wide entries | |
27 | // will be preserved during unassociate | |
678ebfcd VZ |
28 | // 9) KDE does not yet handle multiple actions; Netscape mode never will |
29 | ||
30 | /* | |
31 | TODO: this file is a mess, we need to split it and reformet/review | |
32 | everything (VZ) | |
33 | */ | |
2b813b73 | 34 | |
f6bcfd97 BP |
35 | // ============================================================================ |
36 | // declarations | |
37 | // ============================================================================ | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // headers | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
14f355c2 | 43 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
f6bcfd97 | 44 | #pragma implementation "mimetype.h" |
b13d92d1 VZ |
45 | #endif |
46 | ||
b13d92d1 VZ |
47 | // for compilers that support precompilation, includes "wx.h". |
48 | #include "wx/wxprec.h" | |
49 | ||
50 | #ifdef __BORLANDC__ | |
ce4169a4 | 51 | #pragma hdrstop |
b13d92d1 VZ |
52 | #endif |
53 | ||
b13d92d1 | 54 | #ifndef WX_PRECOMP |
ce4169a4 RR |
55 | #include "wx/defs.h" |
56 | #endif | |
57 | ||
b79e32cc | 58 | #if wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE |
ce4169a4 RR |
59 | |
60 | #ifndef WX_PRECOMP | |
61 | #include "wx/string.h" | |
b13d92d1 VZ |
62 | #endif //WX_PRECOMP |
63 | ||
3d05544e | 64 | |
b13d92d1 | 65 | #include "wx/log.h" |
ce4169a4 | 66 | #include "wx/file.h" |
b13d92d1 VZ |
67 | #include "wx/intl.h" |
68 | #include "wx/dynarray.h" | |
2432b92d | 69 | #include "wx/confbase.h" |
b13d92d1 | 70 | |
7dc3cc31 VS |
71 | #include "wx/ffile.h" |
72 | #include "wx/textfile.h" | |
73 | #include "wx/dir.h" | |
74 | #include "wx/utils.h" | |
75 | #include "wx/tokenzr.h" | |
c41dbc20 | 76 | #include "wx/iconloc.h" |
1d529ef7 RR |
77 | #include "wx/filename.h" |
78 | #include "wx/fileconf.h" | |
b13d92d1 | 79 | |
7dc3cc31 | 80 | #include "wx/unix/mimetype.h" |
b13d92d1 VZ |
81 | |
82 | // other standard headers | |
83 | #include <ctype.h> | |
84 | ||
2900bd1c JJ |
85 | #ifdef __VMS |
86 | /* silence warnings for comparing unsigned int's <0 */ | |
87 | # pragma message disable unscomzer | |
88 | #endif | |
89 | ||
678ebfcd VZ |
90 | // wxMimeTypeCommands stores the verbs defined for the given MIME type with |
91 | // their values | |
92 | class wxMimeTypeCommands | |
2b813b73 VZ |
93 | { |
94 | public: | |
678ebfcd VZ |
95 | wxMimeTypeCommands() { } |
96 | ||
97 | wxMimeTypeCommands(const wxArrayString& verbs, | |
98 | const wxArrayString& commands) | |
99 | : m_verbs(verbs), | |
100 | m_commands(commands) | |
2b813b73 | 101 | { |
2b813b73 VZ |
102 | } |
103 | ||
678ebfcd VZ |
104 | // add a new verb with the command or replace the old value |
105 | void AddOrReplaceVerb(const wxString& verb, const wxString& cmd) | |
2b813b73 | 106 | { |
678ebfcd VZ |
107 | int n = m_verbs.Index(verb, FALSE /* ignore case */); |
108 | if ( n == wxNOT_FOUND ) | |
2b813b73 | 109 | { |
678ebfcd VZ |
110 | m_verbs.Add(verb); |
111 | m_commands.Add(cmd); | |
112 | } | |
113 | else | |
114 | { | |
115 | m_commands[n] = cmd; | |
2b813b73 | 116 | } |
2b813b73 VZ |
117 | } |
118 | ||
678ebfcd | 119 | void Add(const wxString& s) |
2b813b73 | 120 | { |
678ebfcd VZ |
121 | m_verbs.Add(s.BeforeFirst(_T('='))); |
122 | m_commands.Add(s.AfterFirst(_T('='))); | |
2b813b73 VZ |
123 | } |
124 | ||
678ebfcd VZ |
125 | // access the commands |
126 | size_t GetCount() const { return m_verbs.GetCount(); } | |
127 | const wxString& GetVerb(size_t n) const { return m_verbs[n]; } | |
128 | const wxString& GetCmd(size_t n) const { return m_commands[n]; } | |
129 | ||
130 | bool HasVerb(const wxString& verb) const | |
131 | { return m_verbs.Index(verb) != wxNOT_FOUND; } | |
132 | ||
133 | wxString GetCommandForVerb(const wxString& verb, size_t *idx = NULL) const | |
2b813b73 | 134 | { |
678ebfcd VZ |
135 | wxString s; |
136 | ||
137 | int n = m_verbs.Index(verb); | |
138 | if ( n != wxNOT_FOUND ) | |
139 | { | |
140 | s = m_commands[(size_t)n]; | |
141 | if ( idx ) | |
142 | *idx = n; | |
143 | } | |
144 | ||
145 | return s; | |
2b813b73 VZ |
146 | } |
147 | ||
678ebfcd VZ |
148 | // get a "verb=command" string |
149 | wxString GetVerbCmd(size_t n) const | |
2b813b73 | 150 | { |
678ebfcd | 151 | return m_verbs[n] + _T('=') + m_commands[n]; |
2b813b73 | 152 | } |
678ebfcd VZ |
153 | |
154 | private: | |
155 | wxArrayString m_verbs, | |
156 | m_commands; | |
2b813b73 VZ |
157 | }; |
158 | ||
678ebfcd VZ |
159 | // this class extends wxTextFile |
160 | // | |
161 | // VZ: ??? | |
2b813b73 VZ |
162 | class wxMimeTextFile : public wxTextFile |
163 | { | |
164 | public: | |
165 | // constructors | |
166 | wxMimeTextFile () : wxTextFile () {}; | |
167 | wxMimeTextFile (const wxString& strFile) : wxTextFile (strFile) { }; | |
168 | ||
169 | int pIndexOf(const wxString & sSearch, bool bIncludeComments = FALSE, int iStart = 0) | |
170 | { | |
171 | size_t i = iStart; | |
172 | int nResult = wxNOT_FOUND; | |
173 | if (i>=GetLineCount()) return wxNOT_FOUND; | |
174 | ||
175 | wxString sTest = sSearch; | |
176 | sTest.MakeLower(); | |
177 | wxString sLine; | |
178 | ||
179 | if (bIncludeComments) | |
180 | { | |
181 | while ( (i < GetLineCount()) ) | |
182 | { | |
183 | sLine = GetLine (i); | |
184 | sLine.MakeLower(); | |
185 | if (sLine.Contains(sTest)) nResult = (int) i; | |
186 | i++; | |
187 | } | |
188 | } | |
189 | else | |
190 | { | |
191 | while ( (i < GetLineCount()) ) | |
192 | { | |
193 | sLine = GetLine (i); | |
194 | sLine.MakeLower(); | |
195 | if ( ! sLine.StartsWith(wxT("#"))) | |
196 | { | |
197 | if (sLine.Contains(sTest)) nResult = (int) i; | |
198 | } | |
199 | i++; | |
200 | } | |
201 | } | |
202 | return nResult; | |
203 | } | |
204 | ||
205 | bool CommentLine(int nIndex) | |
206 | { | |
207 | if (nIndex <0) return FALSE; | |
208 | if (nIndex >= (int)GetLineCount() ) return FALSE; | |
209 | GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#")); | |
210 | return TRUE; | |
211 | } | |
212 | ||
213 | bool CommentLine(const wxString & sTest) | |
214 | { | |
215 | int nIndex = pIndexOf(sTest); | |
216 | if (nIndex <0) return FALSE; | |
217 | if (nIndex >= (int)GetLineCount() ) return FALSE; | |
218 | GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#")); | |
219 | return TRUE; | |
220 | } | |
221 | ||
222 | wxString GetVerb (size_t i) | |
223 | { | |
2b813b73 VZ |
224 | if (i > GetLineCount() ) return wxEmptyString; |
225 | wxString sTmp = GetLine(i).BeforeFirst(wxT('=')); | |
226 | return sTmp; | |
227 | } | |
228 | ||
229 | wxString GetCmd (size_t i) | |
230 | { | |
2b813b73 VZ |
231 | if (i > GetLineCount() ) return wxEmptyString; |
232 | wxString sTmp = GetLine(i).AfterFirst(wxT('=')); | |
233 | return sTmp; | |
234 | } | |
235 | }; | |
236 | ||
b12915c1 VZ |
237 | // in case we're compiling in non-GUI mode |
238 | class WXDLLEXPORT wxIcon; | |
239 | ||
f6bcfd97 BP |
240 | // ---------------------------------------------------------------------------- |
241 | // constants | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | // MIME code tracing mask | |
245 | #define TRACE_MIME _T("mime") | |
246 | ||
678ebfcd VZ |
247 | // give trace messages about the results of mailcap tests |
248 | #define TRACE_MIME_TEST _T("mimetest") | |
249 | ||
f6bcfd97 BP |
250 | // ---------------------------------------------------------------------------- |
251 | // private functions | |
252 | // ---------------------------------------------------------------------------- | |
253 | ||
254 | // there are some fields which we don't understand but for which we don't give | |
255 | // warnings as we know that they're not important - this function is used to | |
256 | // test for them | |
257 | static bool IsKnownUnimportantField(const wxString& field); | |
258 | ||
b13d92d1 VZ |
259 | // ---------------------------------------------------------------------------- |
260 | // private classes | |
261 | // ---------------------------------------------------------------------------- | |
262 | ||
2b813b73 | 263 | |
a6c65e88 | 264 | // This class uses both mailcap and mime.types to gather information about file |
b13d92d1 VZ |
265 | // types. |
266 | // | |
a6c65e88 VZ |
267 | // The information about mailcap file was extracted from metamail(1) sources |
268 | // and documentation and subsequently revised when I found the RFC 1524 | |
269 | // describing it. | |
b13d92d1 VZ |
270 | // |
271 | // Format of mailcap file: spaces are ignored, each line is either a comment | |
272 | // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>. | |
273 | // A backslash can be used to quote semicolons and newlines (and, in fact, | |
274 | // anything else including itself). | |
275 | // | |
276 | // The first field is always the MIME type in the form of type/subtype (see RFC | |
277 | // 822) where subtype may be '*' meaning "any". Following metamail, we accept | |
278 | // "type" which means the same as "type/*", although I'm not sure whether this | |
279 | // is standard. | |
280 | // | |
281 | // The second field is always the command to run. It is subject to | |
282 | // parameter/filename expansion described below. | |
283 | // | |
284 | // All the following fields are optional and may not be present at all. If | |
285 | // they're present they may appear in any order, although each of them should | |
286 | // appear only once. The optional fields are the following: | |
287 | // * notes=xxx is an uninterpreted string which is silently ignored | |
288 | // * test=xxx is the command to be used to determine whether this mailcap line | |
289 | // applies to our data or not. The RHS of this field goes through the | |
290 | // parameter/filename expansion (as the 2nd field) and the resulting string | |
291 | // is executed. The line applies only if the command succeeds, i.e. returns 0 | |
292 | // exit code. | |
293 | // * print=xxx is the command to be used to print (and not view) the data of | |
294 | // this type (parameter/filename expansion is done here too) | |
295 | // * edit=xxx is the command to open/edit the data of this type | |
a6c65e88 VZ |
296 | // * needsterminal means that a new interactive console must be created for |
297 | // the viewer | |
b13d92d1 VZ |
298 | // * copiousoutput means that the viewer doesn't interact with the user but |
299 | // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a | |
300 | // good example), thus it might be a good idea to use some kind of paging | |
301 | // mechanism. | |
302 | // * textualnewlines means not to perform CR/LF translation (not honored) | |
303 | // * compose and composetyped fields are used to determine the program to be | |
304 | // called to create a new message pert in the specified format (unused). | |
305 | // | |
a6c65e88 | 306 | // Parameter/filename expansion: |
b13d92d1 VZ |
307 | // * %s is replaced with the (full) file name |
308 | // * %t is replaced with MIME type/subtype of the entry | |
309 | // * for multipart type only %n is replaced with the nnumber of parts and %F is | |
310 | // replaced by an array of (content-type, temporary file name) pairs for all | |
311 | // message parts (TODO) | |
312 | // * %{parameter} is replaced with the value of parameter taken from | |
313 | // Content-type header line of the message. | |
314 | // | |
b13d92d1 VZ |
315 | // |
316 | // There are 2 possible formats for mime.types file, one entry per line (used | |
a6c65e88 VZ |
317 | // for global mime.types and called Mosaic format) and "expanded" format where |
318 | // an entry takes multiple lines (used for users mime.types and called | |
319 | // Netscape format). | |
b13d92d1 VZ |
320 | // |
321 | // For both formats spaces are ignored and lines starting with a '#' are | |
322 | // comments. Each record has one of two following forms: | |
323 | // a) for "brief" format: | |
324 | // <mime type> <space separated list of extensions> | |
ec4768ef | 325 | // b) for "expanded" format: |
1457c903 VZ |
326 | // type=<mime type> BACKSLASH |
327 | // desc="<description>" BACKSLASH | |
a6c65e88 | 328 | // exts="<comma separated list of extensions>" |
b13d92d1 | 329 | // |
1457c903 VZ |
330 | // (where BACKSLASH is a literal '\\' which we can't put here because cpp |
331 | // misinterprets it) | |
332 | // | |
b13d92d1 VZ |
333 | // We try to autodetect the format of mime.types: if a non-comment line starts |
334 | // with "type=" we assume the second format, otherwise the first one. | |
335 | ||
336 | // there may be more than one entry for one and the same mime type, to | |
337 | // choose the right one we have to run the command specified in the test | |
338 | // field on our data. | |
b9517a0a VZ |
339 | |
340 | // ---------------------------------------------------------------------------- | |
2b813b73 | 341 | // wxGNOME |
b9517a0a VZ |
342 | // ---------------------------------------------------------------------------- |
343 | ||
344 | // GNOME stores the info we're interested in in several locations: | |
345 | // 1. xxx.keys files under /usr/share/mime-info | |
346 | // 2. xxx.keys files under ~/.gnome/mime-info | |
347 | // | |
348 | // The format of xxx.keys file is the following: | |
349 | // | |
350 | // mimetype/subtype: | |
351 | // field=value | |
352 | // | |
353 | // with blank lines separating the entries and indented lines starting with | |
354 | // TABs. We're interested in the field icon-filename whose value is the path | |
355 | // containing the icon. | |
afaee315 VZ |
356 | // |
357 | // Update (Chris Elliott): apparently there may be an optional "[lang]" prefix | |
358 | // just before the field name. | |
b9517a0a | 359 | |
b9517a0a | 360 | |
2b813b73 | 361 | bool wxMimeTypesManagerImpl::CheckGnomeDirsExist () |
678ebfcd | 362 | { |
2b813b73 VZ |
363 | wxString gnomedir; |
364 | wxGetHomeDir( &gnomedir ); | |
365 | wxString sTmp = gnomedir; | |
2b5f62a0 | 366 | sTmp = sTmp + wxT("/.gnome"); |
2b813b73 | 367 | if (! wxDir::Exists ( sTmp ) ) |
678ebfcd | 368 | { |
2b813b73 | 369 | if (!wxMkdir ( sTmp )) |
678ebfcd | 370 | { |
2b5f62a0 | 371 | wxLogError(_("Failed to create directory %s/.gnome."), sTmp.c_str()); |
2b813b73 | 372 | return FALSE; |
b9517a0a | 373 | } |
678ebfcd | 374 | } |
2b5f62a0 | 375 | sTmp = sTmp + wxT("/mime-info"); |
2b813b73 | 376 | if (! wxDir::Exists ( sTmp ) ) |
678ebfcd | 377 | { |
2b813b73 VZ |
378 | if (!wxMkdir ( sTmp )) |
379 | { | |
2b5f62a0 | 380 | wxLogError(_("Failed to create directory %s/mime-info."), sTmp.c_str()); |
2b813b73 | 381 | return FALSE; |
b9517a0a | 382 | } |
2b813b73 VZ |
383 | } |
384 | return TRUE; | |
b9517a0a | 385 | |
2b813b73 VZ |
386 | } |
387 | ||
388 | ||
389 | ||
390 | bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index, bool delete_index) | |
678ebfcd | 391 | { |
2b813b73 VZ |
392 | wxString gnomedir; |
393 | wxGetHomeDir( &gnomedir ); | |
394 | ||
2b5f62a0 | 395 | wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.keys")); |
2b813b73 VZ |
396 | // if this fails probably Gnome is not installed ?? |
397 | // create it anyway as a private mime store | |
398 | ||
2b5f62a0 VZ |
399 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
400 | if (! outfile.Open ( wxConvUTF8) ) | |
401 | #else | |
2b813b73 | 402 | if (! outfile.Open () ) |
2b5f62a0 | 403 | #endif |
2b813b73 VZ |
404 | { |
405 | if (delete_index) return FALSE; | |
406 | if (!CheckGnomeDirsExist() ) return FALSE; | |
407 | outfile.Create (); | |
408 | } | |
409 | ||
410 | wxString sTmp, strType = m_aTypes[index]; | |
411 | int nIndex = outfile.pIndexOf(strType); | |
412 | if ( nIndex == wxNOT_FOUND ) | |
678ebfcd | 413 | { |
2b813b73 VZ |
414 | outfile.AddLine ( strType + wxT(':') ); |
415 | // see file:/usr/doc/gnome-libs-devel-1.0.40/devel-docs/mime-type-handling.txt | |
416 | // as this does not deal with internationalisation | |
417 | // wxT( "\t[en_US]") + verb + wxT ('=') + cmd + wxT(" %f"); | |
678ebfcd VZ |
418 | wxMimeTypeCommands * entries = m_aEntries[index]; |
419 | size_t count = entries->GetCount(); | |
420 | for ( size_t i = 0; i < count; i++ ) | |
421 | { | |
422 | sTmp = entries->GetVerbCmd(i); | |
2b813b73 VZ |
423 | sTmp.Replace( wxT("%s"), wxT("%f") ); |
424 | sTmp = wxT ( "\t") + sTmp; | |
425 | outfile.AddLine ( sTmp ); | |
678ebfcd | 426 | } |
2b813b73 VZ |
427 | //for international use do something like this |
428 | //outfile.AddLine ( wxString( "\t[en_US]icon-filename=") + cmd ); | |
429 | outfile.AddLine ( wxT( "\ticon-filename=") + m_aIcons[index] ); | |
678ebfcd VZ |
430 | } |
431 | else | |
432 | { | |
433 | if (delete_index) | |
434 | outfile.CommentLine(nIndex); | |
435 | ||
436 | wxMimeTypeCommands sOld; | |
2b813b73 VZ |
437 | size_t nOld = nIndex + 1; |
438 | bool oldEntryEnd = FALSE; | |
439 | while ( (nOld < outfile.GetLineCount() )&& (oldEntryEnd == FALSE )) | |
678ebfcd | 440 | { |
2b813b73 VZ |
441 | sTmp = outfile.GetLine(nOld); |
442 | if ( (sTmp[0u] == wxT('\t')) || (sTmp[0u] == wxT('#')) ) | |
678ebfcd | 443 | { |
2b813b73 VZ |
444 | // we have another line to deal with |
445 | outfile.CommentLine(nOld); | |
446 | nOld ++; | |
447 | // add the line to our store | |
678ebfcd VZ |
448 | if ((!delete_index) && (sTmp[0u] == wxT('\t'))) |
449 | sOld.Add(sTmp); | |
b9517a0a | 450 | } |
678ebfcd VZ |
451 | // next mimetpye ??or blank line |
452 | else | |
453 | oldEntryEnd = TRUE; | |
454 | } | |
2b813b73 VZ |
455 | // list of entries in our data; these should all be in sOld, |
456 | // though sOld may also contain other entries , eg flags | |
457 | if (!delete_index) | |
678ebfcd VZ |
458 | { |
459 | wxMimeTypeCommands * entries = m_aEntries[index]; | |
2b813b73 VZ |
460 | size_t i; |
461 | for (i=0; i < entries->GetCount(); i++) | |
678ebfcd | 462 | { |
2b813b73 | 463 | // replace any entries in sold that match verbs we know |
678ebfcd | 464 | sOld.AddOrReplaceVerb ( entries->GetVerb(i), entries->GetCmd (i) ); |
b9517a0a | 465 | } |
2b813b73 | 466 | //sOld should also contain the icon |
678ebfcd | 467 | if ( !m_aIcons[index].empty() ) |
2b5f62a0 | 468 | sOld.AddOrReplaceVerb ( wxT("icon-filename"), m_aIcons[index] ); |
b9517a0a | 469 | |
2b813b73 | 470 | for (i=0; i < sOld.GetCount(); i++) |
678ebfcd VZ |
471 | { |
472 | sTmp = sOld.GetVerbCmd(i); | |
2b813b73 | 473 | sTmp.Replace( wxT("%s"), wxT("%f") ); |
2b5f62a0 | 474 | sTmp = wxT("\t") + sTmp; |
2b813b73 VZ |
475 | nIndex ++; |
476 | outfile.InsertLine ( sTmp, nIndex ); | |
2b813b73 VZ |
477 | } |
478 | } | |
678ebfcd | 479 | } |
2b813b73 VZ |
480 | bool bTmp = outfile.Write (); |
481 | return bTmp; | |
678ebfcd | 482 | } |
b9517a0a | 483 | |
b9517a0a | 484 | |
2b813b73 | 485 | bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index) |
678ebfcd | 486 | { |
2b813b73 VZ |
487 | wxString gnomedir; |
488 | wxGetHomeDir( &gnomedir ); | |
afaee315 | 489 | |
2b5f62a0 | 490 | wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.mime")); |
2b813b73 VZ |
491 | // if this fails probably Gnome is not installed ?? |
492 | // create it anyway as a private mime store | |
493 | if (! outfile.Open () ) | |
494 | { | |
495 | if (delete_index) return FALSE; | |
496 | if (!CheckGnomeDirsExist() ) return FALSE; | |
497 | outfile.Create (); | |
498 | } | |
499 | wxString strType = m_aTypes[index]; | |
500 | int nIndex = outfile.pIndexOf(strType); | |
501 | if ( nIndex == wxNOT_FOUND ) | |
678ebfcd | 502 | { |
2b813b73 | 503 | outfile.AddLine ( strType ); |
2b5f62a0 | 504 | outfile.AddLine ( wxT("\text:") + m_aExtensions.Item(index) ); |
678ebfcd | 505 | } |
2b813b73 | 506 | else |
678ebfcd | 507 | { |
2b813b73 | 508 | if (delete_index) |
678ebfcd | 509 | { |
2b813b73 VZ |
510 | outfile.CommentLine(nIndex); |
511 | outfile.CommentLine(nIndex+1); | |
b9517a0a VZ |
512 | } |
513 | else | |
678ebfcd | 514 | {// check for next line being the right one to replace ?? |
2b813b73 | 515 | wxString sOld = outfile.GetLine(nIndex+1); |
2b5f62a0 | 516 | if (sOld.Contains( wxT("\text: "))) |
678ebfcd | 517 | { |
2b5f62a0 | 518 | outfile.GetLine(nIndex+1) = wxT("\text: ") + m_aExtensions.Item(index); |
678ebfcd | 519 | } |
2b813b73 | 520 | else |
b9517a0a | 521 | { |
2b5f62a0 | 522 | outfile.InsertLine( wxT("\text: ") + m_aExtensions.Item(index), nIndex + 1 ); |
b9517a0a | 523 | } |
4d2976ad VS |
524 | } |
525 | } | |
2b813b73 VZ |
526 | bool bTmp = outfile.Write (); |
527 | return bTmp; | |
4d2976ad VS |
528 | } |
529 | ||
2b813b73 | 530 | |
25d599ae VS |
531 | void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename, |
532 | const wxArrayString& dirs) | |
4d2976ad | 533 | { |
2b813b73 | 534 | wxTextFile textfile(filename); |
2b5f62a0 VZ |
535 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
536 | if ( !textfile.Open( wxConvUTF8) ) | |
537 | #else | |
2b813b73 | 538 | if ( !textfile.Open() ) |
2b5f62a0 | 539 | #endif |
2b813b73 VZ |
540 | return; |
541 | wxLogTrace(TRACE_MIME, wxT("--- Opened Gnome file %s ---"), | |
678ebfcd | 542 | filename.c_str()); |
4d2976ad | 543 | |
2b813b73 VZ |
544 | // values for the entry being parsed |
545 | wxString curMimeType, curIconFile; | |
678ebfcd | 546 | wxMimeTypeCommands * entry = new wxMimeTypeCommands; |
4d2976ad | 547 | |
2b813b73 VZ |
548 | // these are always empty in this file |
549 | wxArrayString strExtensions; | |
550 | wxString strDesc; | |
4d2976ad | 551 | |
2b813b73 VZ |
552 | const wxChar *pc; |
553 | size_t nLineCount = textfile.GetLineCount(); | |
554 | size_t nLine = 0; | |
555 | while ( nLine < nLineCount) | |
678ebfcd | 556 | { |
2b813b73 VZ |
557 | pc = textfile[nLine].c_str(); |
558 | if ( *pc != _T('#') ) | |
678ebfcd | 559 | { |
4d2976ad | 560 | |
2b813b73 | 561 | wxLogTrace(TRACE_MIME, wxT("--- Reading from Gnome file %s '%s' ---"), |
678ebfcd | 562 | filename.c_str(),pc); |
4d2976ad | 563 | |
2b813b73 VZ |
564 | wxString sTmp(pc); |
565 | if (sTmp.Contains(wxT("=")) ) | |
566 | { | |
25d599ae | 567 | // GNOME 1: |
678ebfcd | 568 | if (sTmp.Contains( wxT("icon-filename=") ) ) |
2b813b73 | 569 | { |
678ebfcd | 570 | curIconFile = sTmp.AfterFirst(wxT('=')); |
2b813b73 | 571 | } |
25d599ae VS |
572 | // GNOME 2: |
573 | else if (sTmp.Contains( wxT("icon_filename=") ) ) | |
574 | { | |
575 | curIconFile = sTmp.AfterFirst(wxT('=')); | |
10274336 | 576 | |
25d599ae VS |
577 | if (!wxFileExists(curIconFile)) |
578 | { | |
579 | size_t nDirs = dirs.GetCount(); | |
580 | for (size_t nDir = 0; nDir < nDirs; nDir++) | |
581 | { | |
1d529ef7 | 582 | wxFileName newFile( curIconFile ); |
10274336 RR |
583 | newFile.SetPath( dirs[nDir] ); |
584 | newFile.AppendDir( wxT("pixmaps") ); | |
585 | newFile.AppendDir( wxT("document-icons") ); | |
586 | newFile.SetExt( wxT("png") ); | |
1d529ef7 RR |
587 | if (newFile.FileExists()) |
588 | curIconFile = newFile.GetFullPath(); | |
25d599ae VS |
589 | } |
590 | } | |
591 | } | |
678ebfcd VZ |
592 | else //: some other field, |
593 | { | |
594 | //may contain lines like this (RH7) | |
595 | // \t[lang]open.tex."TeX this file"=tex %f | |
596 | // \tflags.tex.flags=needsterminal | |
597 | // \topen.latex."LaTeX this file"=latex %f | |
598 | // \tflags.latex.flags=needsterminal | |
599 | ||
600 | // \topen=xdvi %f | |
601 | // \tview=xdvi %f | |
602 | // \topen.convert.Convert file to Postscript=dvips %f -o `basename %f .dvi`.ps | |
603 | ||
604 | // for now ignore lines with flags in...FIX | |
605 | sTmp = sTmp.AfterLast(wxT(']')); | |
606 | sTmp = sTmp.AfterLast(wxT('\t')); | |
607 | sTmp.Trim(FALSE).Trim(); | |
608 | if (0 == sTmp.Replace ( wxT("%f"), wxT("%s") )) sTmp = sTmp + wxT(" %s"); | |
609 | entry->Add(sTmp); | |
4d2976ad | 610 | |
2b813b73 VZ |
611 | } |
612 | ||
613 | } // emd of has an equals sign | |
614 | else | |
678ebfcd | 615 | { |
2b813b73 VZ |
616 | // not a comment and not an equals sign |
617 | if (sTmp.Contains(wxT('/'))) | |
678ebfcd | 618 | { |
2b813b73 VZ |
619 | // this is the start of the new mimetype |
620 | // overwrite any existing data | |
678ebfcd VZ |
621 | if (! curMimeType.empty()) |
622 | { | |
2b813b73 VZ |
623 | AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc); |
624 | ||
625 | // now get ready for next bit | |
678ebfcd | 626 | entry = new wxMimeTypeCommands; |
2b813b73 | 627 | } |
678ebfcd VZ |
628 | curMimeType = sTmp.BeforeFirst(wxT(':')); |
629 | } | |
630 | } | |
2b813b73 | 631 | } // end of not a comment |
678ebfcd VZ |
632 | // ignore blank lines |
633 | nLine ++; | |
2b813b73 | 634 | } // end of while, save any data |
1d529ef7 | 635 | |
678ebfcd | 636 | if (! curMimeType.empty()) |
2b813b73 | 637 | AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc); |
4d2976ad VS |
638 | } |
639 | ||
640 | ||
2b813b73 VZ |
641 | |
642 | void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& filename) | |
4d2976ad VS |
643 | { |
644 | wxTextFile textfile(filename); | |
645 | if ( !textfile.Open() ) | |
646 | return; | |
2b6d8c00 VZ |
647 | |
648 | wxLogTrace(TRACE_MIME, | |
649 | wxT("--- Opened Gnome file %s ---"), | |
650 | filename.c_str()); | |
4d2976ad VS |
651 | |
652 | // values for the entry being parsed | |
653 | wxString curMimeType, curExtList; | |
654 | ||
655 | const wxChar *pc; | |
656 | size_t nLineCount = textfile.GetLineCount(); | |
678ebfcd | 657 | for ( size_t nLine = 0;; nLine++ ) |
4d2976ad VS |
658 | { |
659 | if ( nLine < nLineCount ) | |
660 | { | |
661 | pc = textfile[nLine].c_str(); | |
2b5f62a0 | 662 | if ( *pc == wxT('#') ) |
4d2976ad VS |
663 | { |
664 | // skip comments | |
665 | continue; | |
666 | } | |
667 | } | |
668 | else | |
669 | { | |
670 | // so that we will fall into the "if" below | |
671 | pc = NULL; | |
672 | } | |
b9517a0a | 673 | |
4d2976ad VS |
674 | if ( !pc || !*pc ) |
675 | { | |
676 | // end of the entry | |
677 | if ( !!curMimeType && !!curExtList ) | |
b9517a0a | 678 | { |
2b6d8c00 VZ |
679 | wxLogTrace(TRACE_MIME, |
680 | wxT("--- At end of Gnome file finding mimetype %s ---"), | |
681 | curMimeType.c_str()); | |
10274336 | 682 | |
2b813b73 | 683 | AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString); |
4d2976ad | 684 | } |
b9517a0a | 685 | |
4d2976ad VS |
686 | if ( !pc ) |
687 | { | |
688 | // the end - this can only happen if nLine == nLineCount | |
b9517a0a VZ |
689 | break; |
690 | } | |
4d2976ad VS |
691 | |
692 | curExtList.Empty(); | |
693 | ||
694 | continue; | |
695 | } | |
696 | ||
697 | // what do we have here? | |
2b5f62a0 | 698 | if ( *pc == wxT('\t') ) |
4d2976ad VS |
699 | { |
700 | // this is a field=value ling | |
701 | pc++; // skip leading TAB | |
702 | ||
2b6d8c00 VZ |
703 | static const int lenField = 5; // strlen("ext: ") |
704 | if ( wxStrncmp(pc, wxT("ext: "), lenField) == 0 ) | |
4d2976ad | 705 | { |
2b6d8c00 VZ |
706 | // skip it and take everything left until the end of line |
707 | curExtList = pc + lenField; | |
4d2976ad VS |
708 | } |
709 | //else: some other field, we don't care | |
710 | } | |
711 | else | |
712 | { | |
713 | // this is the start of the new section | |
2b6d8c00 VZ |
714 | wxLogTrace(TRACE_MIME, |
715 | wxT("--- In Gnome file finding mimetype %s ---"), | |
716 | curMimeType.c_str()); | |
2b813b73 | 717 | |
678ebfcd VZ |
718 | if (! curMimeType.empty()) |
719 | AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString); | |
2b813b73 | 720 | |
4d2976ad VS |
721 | curMimeType.Empty(); |
722 | ||
2b5f62a0 | 723 | while ( *pc != wxT(':') && *pc != wxT('\0') ) |
4d2976ad VS |
724 | { |
725 | curMimeType += *pc++; | |
726 | } | |
b9517a0a VZ |
727 | } |
728 | } | |
729 | } | |
730 | ||
4d2976ad | 731 | |
25d599ae VS |
732 | void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir( |
733 | const wxString& dirbase, const wxArrayString& dirs) | |
b9517a0a VZ |
734 | { |
735 | wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase), | |
736 | _T("base directory shouldn't end with a slash") ); | |
737 | ||
738 | wxString dirname = dirbase; | |
2b5f62a0 | 739 | dirname << wxT("/mime-info"); |
1d529ef7 | 740 | |
b9517a0a VZ |
741 | if ( !wxDir::Exists(dirname) ) |
742 | return; | |
743 | ||
744 | wxDir dir(dirname); | |
745 | if ( !dir.IsOpened() ) | |
746 | return; | |
747 | ||
748 | // we will concatenate it with filename to get the full path below | |
2b5f62a0 | 749 | dirname += wxT('/'); |
b9517a0a VZ |
750 | |
751 | wxString filename; | |
1d529ef7 RR |
752 | bool cont; |
753 | cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES); | |
b9517a0a VZ |
754 | while ( cont ) |
755 | { | |
2b813b73 | 756 | LoadGnomeMimeTypesFromMimeFile(dirname + filename); |
b9517a0a VZ |
757 | |
758 | cont = dir.GetNext(&filename); | |
759 | } | |
fb5ddb4c | 760 | |
2b813b73 VZ |
761 | cont = dir.GetFirst(&filename, _T("*.keys"), wxDIR_FILES); |
762 | while ( cont ) | |
b9517a0a | 763 | { |
25d599ae | 764 | LoadGnomeDataFromKeyFile(dirname + filename, dirs); |
b9517a0a | 765 | |
2b813b73 VZ |
766 | cont = dir.GetNext(&filename); |
767 | } | |
b9517a0a | 768 | |
1d529ef7 RR |
769 | // Hack alert: We scan all icons and deduce the |
770 | // mime-type from the file name. | |
771 | dirname = dirbase; | |
772 | dirname << wxT("/pixmaps/document-icons"); | |
773 | ||
774 | // these are always empty in this file | |
775 | wxArrayString strExtensions; | |
776 | wxString strDesc; | |
777 | ||
778 | if ( !wxDir::Exists(dirname) ) | |
ca06ee0d RR |
779 | { |
780 | // Jst test for default GPE dir also | |
781 | dirname = wxT("/usr/share/gpe/pixmaps/default/filemanager/document-icons"); | |
782 | ||
783 | if ( !wxDir::Exists(dirname) ) | |
784 | return; | |
785 | } | |
4d2976ad | 786 | |
1d529ef7 | 787 | wxDir dir2( dirname ); |
2b813b73 | 788 | |
1d529ef7 RR |
789 | cont = dir2.GetFirst(&filename, wxT("gnome-*.png"), wxDIR_FILES); |
790 | while ( cont ) | |
791 | { | |
10274336 RR |
792 | wxString mimeType = filename; |
793 | mimeType.Remove( 0, 6 ); // remove "gnome-" | |
794 | mimeType.Remove( mimeType.Len()-4, 4 ); // remove ".png" | |
795 | int pos = mimeType.Find( wxT("-") ); | |
796 | if (pos != wxNOT_FOUND) | |
797 | { | |
798 | mimeType.SetChar( pos, wxT('/') ); | |
799 | wxString iconFile = dirname; | |
800 | iconFile << wxT("/"); | |
801 | iconFile << filename; | |
1d529ef7 | 802 | AddToMimeData ( mimeType, iconFile, NULL, strExtensions, strDesc, TRUE ); |
10274336 | 803 | } |
1d529ef7 RR |
804 | |
805 | cont = dir2.GetNext(&filename); | |
806 | } | |
807 | } | |
2b813b73 VZ |
808 | |
809 | void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir) | |
4d2976ad | 810 | { |
4d2976ad | 811 | wxArrayString dirs; |
10274336 RR |
812 | |
813 | wxString gnomedir = wxGetenv( wxT("GNOMEDIR") );; | |
814 | if (!gnomedir.empty()) | |
815 | { | |
816 | gnomedir << wxT("/share"); | |
817 | dirs.Add( gnomedir ); | |
818 | } | |
819 | ||
2b5f62a0 VZ |
820 | dirs.Add(wxT("/usr/share")); |
821 | dirs.Add(wxT("/usr/local/share")); | |
10274336 RR |
822 | |
823 | gnomedir = wxGetHomeDir(); | |
824 | gnomedir << wxT("/.gnome"); | |
4d2976ad | 825 | dirs.Add( gnomedir ); |
10274336 | 826 | |
678ebfcd | 827 | if (!sExtraDir.empty()) dirs.Add( sExtraDir ); |
4d2976ad VS |
828 | |
829 | size_t nDirs = dirs.GetCount(); | |
830 | for ( size_t nDir = 0; nDir < nDirs; nDir++ ) | |
831 | { | |
25d599ae | 832 | LoadGnomeMimeFilesFromDir(dirs[nDir], dirs); |
4d2976ad VS |
833 | } |
834 | } | |
835 | ||
b9517a0a | 836 | // ---------------------------------------------------------------------------- |
678ebfcd | 837 | // KDE |
b9517a0a VZ |
838 | // ---------------------------------------------------------------------------- |
839 | ||
678ebfcd | 840 | |
b9517a0a VZ |
841 | // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype |
842 | // may be found in either of the following locations | |
843 | // | |
509a6196 | 844 | // 1. $KDEDIR/share/mimelnk/mimetype/subtype.kdelnk |
b9517a0a VZ |
845 | // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk |
846 | // | |
847 | // The format of a .kdelnk file is almost the same as the one used by | |
848 | // wxFileConfig, i.e. there are groups, comments and entries. The icon is the | |
849 | // value for the entry "Type" | |
850 | ||
2b813b73 VZ |
851 | // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD |
852 | // for now write to .kdelnk but should eventually do .desktop instead (in preference??) | |
853 | ||
10274336 RR |
854 | bool wxMimeTypesManagerImpl::CheckKDEDirsExist ( const wxString &sOK, const wxString &sTest ) |
855 | { | |
678ebfcd | 856 | if (sTest.empty()) |
10274336 RR |
857 | { |
858 | if (wxDir::Exists(sOK)) | |
859 | return TRUE; | |
860 | else | |
861 | return FALSE; | |
862 | } | |
2b813b73 | 863 | else |
10274336 RR |
864 | { |
865 | wxString sStart = sOK + wxT("/") + sTest.BeforeFirst(wxT('/')); | |
866 | if (!wxDir::Exists(sStart)) wxMkdir(sStart); | |
867 | wxString sEnd = sTest.AfterFirst(wxT('/')); | |
868 | return CheckKDEDirsExist(sStart, sEnd); | |
2b813b73 VZ |
869 | } |
870 | } | |
871 | ||
872 | bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index, bool delete_index) | |
873 | { | |
874 | wxMimeTextFile appoutfile, mimeoutfile; | |
875 | wxString sHome = wxGetHomeDir(); | |
876 | wxString sTmp = wxT(".kde/share/mimelnk/"); | |
678ebfcd | 877 | wxString sMime = m_aTypes[index]; |
2b813b73 VZ |
878 | CheckKDEDirsExist (sHome, sTmp + sMime.BeforeFirst(wxT('/')) ); |
879 | sTmp = sHome + wxT('/') + sTmp + sMime + wxT(".kdelnk"); | |
880 | ||
881 | bool bTemp; | |
882 | bool bMimeExists = mimeoutfile.Open (sTmp); | |
883 | if (!bMimeExists) | |
884 | { | |
678ebfcd VZ |
885 | bTemp = mimeoutfile.Create (sTmp); |
886 | // some unknown error eg out of disk space | |
887 | if (!bTemp) return FALSE; | |
2b813b73 VZ |
888 | } |
889 | ||
890 | sTmp = wxT(".kde/share/applnk/"); | |
891 | CheckKDEDirsExist (sHome, sTmp + sMime.AfterFirst(wxT('/')) ); | |
892 | sTmp = sHome + wxT('/') + sTmp + sMime.AfterFirst(wxT('/')) + wxT(".kdelnk"); | |
893 | ||
894 | bool bAppExists; | |
895 | bAppExists = appoutfile.Open (sTmp); | |
896 | if (!bAppExists) | |
897 | { | |
898 | bTemp = appoutfile.Create (sTmp); | |
899 | // some unknown error eg out of disk space | |
900 | if (!bTemp) return FALSE; | |
901 | } | |
902 | ||
903 | // fixed data; write if new file | |
904 | if (!bMimeExists) | |
905 | { | |
906 | mimeoutfile.AddLine(wxT("#KDE Config File")); | |
907 | mimeoutfile.AddLine(wxT("[KDE Desktop Entry]")); | |
908 | mimeoutfile.AddLine(wxT("Version=1.0")); | |
909 | mimeoutfile.AddLine(wxT("Type=MimeType")); | |
910 | mimeoutfile.AddLine(wxT("MimeType=") + sMime); | |
911 | } | |
912 | ||
913 | if (!bAppExists) | |
914 | { | |
915 | mimeoutfile.AddLine(wxT("#KDE Config File")); | |
916 | mimeoutfile.AddLine(wxT("[KDE Desktop Entry]")); | |
917 | appoutfile.AddLine(wxT("Version=1.0")); | |
918 | appoutfile.AddLine(wxT("Type=Application")); | |
919 | appoutfile.AddLine(wxT("MimeType=") + sMime + wxT(';')); | |
920 | } | |
921 | ||
922 | // variable data | |
923 | // ignore locale | |
924 | mimeoutfile.CommentLine(wxT("Comment=")); | |
925 | if (!delete_index) | |
926 | mimeoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]); | |
927 | appoutfile.CommentLine(wxT("Name=")); | |
928 | if (!delete_index) | |
929 | appoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]); | |
930 | ||
931 | sTmp = m_aIcons[index]; | |
932 | // we can either give the full path, or the shortfilename if its in | |
933 | // one of the directories we search | |
934 | mimeoutfile.CommentLine(wxT("Icon=") ); | |
935 | if (!delete_index) mimeoutfile.AddLine(wxT("Icon=") + sTmp ); | |
936 | appoutfile.CommentLine(wxT("Icon=") ); | |
937 | if (!delete_index) appoutfile.AddLine(wxT("Icon=") + sTmp ); | |
938 | ||
939 | sTmp = wxT(" ") + m_aExtensions[index]; | |
940 | ||
941 | wxStringTokenizer tokenizer(sTmp, _T(" ")); | |
942 | sTmp = wxT("Patterns="); | |
943 | mimeoutfile.CommentLine(sTmp); | |
944 | while ( tokenizer.HasMoreTokens() ) | |
945 | { | |
946 | // holds an extension; need to change it to *.ext; | |
947 | wxString e = wxT("*.") + tokenizer.GetNextToken() + wxT(";"); | |
948 | sTmp = sTmp + e; | |
678ebfcd | 949 | } |
2b813b73 VZ |
950 | if (!delete_index) mimeoutfile.AddLine(sTmp); |
951 | ||
678ebfcd | 952 | wxMimeTypeCommands * entries = m_aEntries[index]; |
2b813b73 | 953 | // if we don't find open just have an empty string ... FIX this |
678ebfcd | 954 | sTmp = entries->GetCommandForVerb(_T("open")); |
2b813b73 VZ |
955 | sTmp.Replace( wxT("%s"), wxT("%f") ); |
956 | ||
957 | mimeoutfile.CommentLine(wxT("DefaultApp=") ); | |
958 | if (!delete_index) mimeoutfile.AddLine(wxT("DefaultApp=") + sTmp); | |
959 | ||
960 | sTmp.Replace( wxT("%f"), wxT("") ); | |
961 | appoutfile.CommentLine(wxT("Exec=")); | |
962 | if (!delete_index) appoutfile.AddLine(wxT("Exec=") + sTmp); | |
963 | ||
964 | if (entries->GetCount() > 1) | |
678ebfcd VZ |
965 | { |
966 | //other actions as well as open | |
2b813b73 | 967 | |
678ebfcd | 968 | } |
2b813b73 VZ |
969 | bTemp = FALSE; |
970 | if (mimeoutfile.Write ()) bTemp = TRUE; | |
971 | mimeoutfile.Close (); | |
972 | if (appoutfile.Write ()) bTemp = TRUE; | |
973 | appoutfile.Close (); | |
974 | ||
975 | return bTemp; | |
2b813b73 VZ |
976 | } |
977 | ||
978 | void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase, | |
b9517a0a | 979 | const wxString& subdir, |
cdf339c9 VS |
980 | const wxString& filename, |
981 | const wxArrayString& icondirs) | |
b9517a0a | 982 | { |
2b813b73 VZ |
983 | wxMimeTextFile file; |
984 | if ( !file.Open(dirbase + filename) ) return; | |
b9517a0a | 985 | |
25d599ae VS |
986 | wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"), |
987 | (dirbase+filename).c_str()); | |
988 | ||
678ebfcd | 989 | wxMimeTypeCommands * entry = new wxMimeTypeCommands; |
2b813b73 VZ |
990 | wxArrayString sExts; |
991 | wxString mimetype, mime_desc, strIcon; | |
992 | ||
2b5f62a0 | 993 | int nIndex = file.pIndexOf( wxT("MimeType=") ); |
2b813b73 | 994 | if (nIndex == wxNOT_FOUND) |
678ebfcd VZ |
995 | { |
996 | // construct mimetype from the directory name and the basename of the | |
997 | // file (it always has .kdelnk extension) | |
2b5f62a0 | 998 | mimetype << subdir << wxT('/') << filename.BeforeLast( wxT('.') ); |
678ebfcd | 999 | } |
2b813b73 | 1000 | else mimetype = file.GetCmd (nIndex); |
b9517a0a | 1001 | |
276c7463 VZ |
1002 | // first find the description string: it is the value in either "Comment=" |
1003 | // line or "Comment[<locale_name>]=" one | |
2b813b73 | 1004 | nIndex = wxNOT_FOUND; |
276c7463 VZ |
1005 | |
1006 | wxString comment; | |
1007 | #if wxUSE_INTL | |
1008 | wxLocale *locale = wxGetLocale(); | |
1009 | if ( locale ) | |
1010 | { | |
1011 | // try "Comment[locale name]" first | |
1012 | comment << _T("Comment[") + locale->GetName() + _T("]="); | |
2b813b73 | 1013 | nIndex = file.pIndexOf(comment); |
276c7463 VZ |
1014 | } |
1015 | #endif // wxUSE_INTL | |
cdf339c9 | 1016 | |
2b813b73 | 1017 | if ( nIndex == wxNOT_FOUND ) |
276c7463 VZ |
1018 | { |
1019 | comment = _T("Comment="); | |
2b813b73 | 1020 | nIndex = file.pIndexOf(comment); |
276c7463 VZ |
1021 | } |
1022 | ||
2b813b73 | 1023 | if ( nIndex != wxNOT_FOUND ) mime_desc = file.GetCmd(nIndex); |
276c7463 | 1024 | //else: no description |
cdf339c9 | 1025 | |
276c7463 VZ |
1026 | // next find the extensions |
1027 | wxString mime_extension; | |
1028 | ||
2b813b73 VZ |
1029 | nIndex = file.pIndexOf(_T("Patterns=")); |
1030 | if ( nIndex != wxNOT_FOUND ) | |
678ebfcd VZ |
1031 | { |
1032 | wxString exts = file.GetCmd (nIndex);; | |
5bd3a2da | 1033 | |
276c7463 VZ |
1034 | wxStringTokenizer tokenizer(exts, _T(";")); |
1035 | while ( tokenizer.HasMoreTokens() ) | |
cdf339c9 | 1036 | { |
276c7463 VZ |
1037 | wxString e = tokenizer.GetNextToken(); |
1038 | if ( e.Left(2) != _T("*.") ) | |
1039 | continue; // don't support too difficult patterns | |
1040 | ||
1041 | if ( !mime_extension.empty() ) | |
1042 | { | |
1043 | // separate from the previous ext | |
1044 | mime_extension << _T(' '); | |
1045 | } | |
1046 | ||
cdf339c9 | 1047 | mime_extension << e.Mid(2); |
cdf339c9 | 1048 | } |
cdf339c9 | 1049 | } |
2b813b73 | 1050 | sExts.Add(mime_extension); |
5bd3a2da | 1051 | |
cdf339c9 VS |
1052 | // ok, now we can take care of icon: |
1053 | ||
2b813b73 VZ |
1054 | nIndex = file.pIndexOf(_T("Icon=")); |
1055 | if ( nIndex != wxNOT_FOUND ) | |
b9517a0a | 1056 | { |
2b813b73 | 1057 | strIcon = file.GetCmd(nIndex); |
25d599ae | 1058 | wxLogTrace(TRACE_MIME, wxT(" icon %s"), strIcon.c_str()); |
2b813b73 | 1059 | //it could be the real path, but more often a short name |
10274336 RR |
1060 | |
1061 | ||
2b813b73 | 1062 | if (!wxFileExists(strIcon)) |
678ebfcd | 1063 | { |
2b813b73 | 1064 | // icon is just the short name |
678ebfcd | 1065 | if ( !strIcon.empty() ) |
cdf339c9 | 1066 | { |
678ebfcd VZ |
1067 | // we must check if the file exists because it may be stored |
1068 | // in many locations, at least ~/.kde and $KDEDIR | |
1069 | size_t nDir, nDirs = icondirs.GetCount(); | |
1070 | for ( nDir = 0; nDir < nDirs; nDir++ ) | |
10274336 | 1071 | { |
3d780434 RR |
1072 | wxFileName fnameIcon( strIcon ); |
1073 | wxFileName fname( icondirs[nDir], fnameIcon.GetName() ); | |
10274336 | 1074 | fname.SetExt( wxT("png") ); |
1d529ef7 | 1075 | if (fname.FileExists()) |
678ebfcd | 1076 | { |
1d529ef7 | 1077 | strIcon = fname.GetFullPath(); |
25d599ae | 1078 | wxLogTrace(TRACE_MIME, wxT(" iconfile %s"), strIcon.c_str()); |
678ebfcd VZ |
1079 | break; |
1080 | } | |
10274336 | 1081 | } |
2b813b73 | 1082 | } |
678ebfcd VZ |
1083 | } |
1084 | } | |
2b813b73 VZ |
1085 | // now look for lines which know about the application |
1086 | // exec= or DefaultApp= | |
1087 | ||
1088 | nIndex = file.pIndexOf(wxT("DefaultApp")); | |
b9517a0a | 1089 | |
2b813b73 | 1090 | if ( nIndex == wxNOT_FOUND ) |
678ebfcd | 1091 | { |
2b813b73 VZ |
1092 | // no entry try exec |
1093 | nIndex = file.pIndexOf(wxT("Exec")); | |
678ebfcd | 1094 | } |
2b813b73 VZ |
1095 | |
1096 | if ( nIndex != wxNOT_FOUND ) | |
678ebfcd | 1097 | { |
2b813b73 VZ |
1098 | wxString sTmp = file.GetCmd(nIndex); |
1099 | // we expect %f; others including %F and %U and %u are possible | |
678ebfcd VZ |
1100 | if (0 == sTmp.Replace ( wxT("%f"), wxT("%s") )) |
1101 | sTmp = sTmp + wxT(" %s"); | |
1102 | entry->AddOrReplaceVerb (wxString(wxT("open")), sTmp ); | |
b9517a0a | 1103 | } |
2b813b73 VZ |
1104 | |
1105 | AddToMimeData (mimetype, strIcon, entry, sExts, mime_desc); | |
b9517a0a VZ |
1106 | } |
1107 | ||
2b813b73 | 1108 | void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase, |
cdf339c9 VS |
1109 | const wxString& subdir, |
1110 | const wxArrayString& icondirs) | |
b9517a0a VZ |
1111 | { |
1112 | wxString dirname = dirbase; | |
1113 | dirname += subdir; | |
1114 | wxDir dir(dirname); | |
1115 | if ( !dir.IsOpened() ) | |
1116 | return; | |
1117 | ||
25d599ae VS |
1118 | wxLogTrace(TRACE_MIME, wxT("--- Loading from KDE directory %s ---"), |
1119 | dirname.c_str()); | |
1120 | ||
b9517a0a VZ |
1121 | dirname += _T('/'); |
1122 | ||
1123 | wxString filename; | |
1124 | bool cont = dir.GetFirst(&filename, _T("*.kdelnk"), wxDIR_FILES); | |
1125 | while ( cont ) | |
1126 | { | |
2b813b73 VZ |
1127 | LoadKDELinksForMimeSubtype(dirname, subdir, filename, icondirs); |
1128 | ||
1129 | cont = dir.GetNext(&filename); | |
1130 | } | |
1131 | // new standard for Gnome and KDE | |
1132 | cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES); | |
1133 | while ( cont ) | |
1134 | { | |
1135 | LoadKDELinksForMimeSubtype(dirname, subdir, filename, icondirs); | |
b9517a0a VZ |
1136 | |
1137 | cont = dir.GetNext(&filename); | |
1138 | } | |
1139 | } | |
1140 | ||
2b813b73 | 1141 | void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString& dirbase, |
cdf339c9 | 1142 | const wxArrayString& icondirs) |
b9517a0a VZ |
1143 | { |
1144 | wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase), | |
1145 | _T("base directory shouldn't end with a slash") ); | |
1146 | ||
1147 | wxString dirname = dirbase; | |
1148 | dirname << _T("/mimelnk"); | |
1149 | ||
1150 | if ( !wxDir::Exists(dirname) ) | |
1151 | return; | |
1152 | ||
1153 | wxDir dir(dirname); | |
1154 | if ( !dir.IsOpened() ) | |
1155 | return; | |
1156 | ||
1157 | // we will concatenate it with dir name to get the full path below | |
1158 | dirname += _T('/'); | |
1159 | ||
1160 | wxString subdir; | |
1161 | bool cont = dir.GetFirst(&subdir, wxEmptyString, wxDIR_DIRS); | |
1162 | while ( cont ) | |
1163 | { | |
2b813b73 | 1164 | LoadKDELinksForMimeType(dirname, subdir, icondirs); |
b9517a0a VZ |
1165 | |
1166 | cont = dir.GetNext(&subdir); | |
1167 | } | |
1168 | } | |
1169 | ||
2b813b73 | 1170 | void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir) |
b9517a0a VZ |
1171 | { |
1172 | wxArrayString dirs; | |
cdf339c9 | 1173 | wxArrayString icondirs; |
1d529ef7 | 1174 | |
10274336 RR |
1175 | // the variable $KDEDIR is set when KDE is running |
1176 | wxString kdedir = wxGetenv( wxT("KDEDIR") ); | |
1d529ef7 | 1177 | |
10274336 | 1178 | if (!kdedir.empty()) |
1d529ef7 | 1179 | { |
10274336 RR |
1180 | // $(KDEDIR)/share/config/kdeglobals holds info |
1181 | // the current icons theme | |
1182 | wxFileName configFile( kdedir, wxEmptyString ); | |
1183 | configFile.AppendDir( wxT("share") ); | |
1184 | configFile.AppendDir( wxT("config") ); | |
1185 | configFile.SetName( wxT("kdeglobals") ); | |
1186 | ||
1187 | if (configFile.FileExists()) | |
1188 | { | |
1189 | wxFileConfig config( wxEmptyString, wxEmptyString, configFile.GetFullPath() ); | |
1190 | // $(KDEDIR)/share/config -> $(KDEDIR)/share | |
1191 | configFile.RemoveDir( configFile.GetDirCount()-1 ); | |
1192 | // $(KDEDIR)/share/ -> $(KDEDIR)/share/icons | |
1193 | configFile.AppendDir( wxT("icons") ); | |
1194 | ||
1195 | // Check for entry | |
1196 | config.SetPath( wxT("Icons") ); | |
1197 | wxString theme; | |
1198 | if (config.Read( wxT("Theme"), &theme )) | |
1199 | configFile.AppendDir( theme ); | |
1200 | else | |
1201 | configFile.AppendDir( wxT("default.kde") ); | |
1202 | } | |
1203 | else | |
1204 | { | |
1205 | // $(KDEDIR)/share/config -> $(KDEDIR)/share | |
1206 | configFile.RemoveDir( configFile.GetDirCount()-1 ); | |
1207 | // $(KDEDIR)/share/ -> $(KDEDIR)/share/icons | |
1208 | configFile.AppendDir( wxT("icons") ); | |
1209 | // $(KDEDIR)/share/icons -> $(KDEDIR)/share/icons/default.kde | |
1210 | configFile.AppendDir( wxT("default.kde") ); | |
1211 | } | |
1212 | ||
1213 | configFile.SetName( wxEmptyString ); | |
16c587ca RR |
1214 | configFile.AppendDir( wxT("32x32") ); |
1215 | configFile.AppendDir( wxT("mimetypes") ); | |
10274336 RR |
1216 | |
1217 | // Just try a few likely icons theme names | |
16c587ca RR |
1218 | |
1219 | int pos = configFile.GetDirCount()-3; | |
1220 | ||
10274336 RR |
1221 | if (!wxDir::Exists(configFile.GetPath())) |
1222 | { | |
16c587ca RR |
1223 | configFile.RemoveDir( pos ); |
1224 | configFile.InsertDir( pos, wxT("default.kde") ); | |
10274336 RR |
1225 | } |
1226 | ||
1227 | if (!wxDir::Exists(configFile.GetPath())) | |
1228 | { | |
16c587ca RR |
1229 | configFile.RemoveDir( pos ); |
1230 | configFile.InsertDir( pos, wxT("default") ); | |
10274336 RR |
1231 | } |
1232 | ||
1233 | if (!wxDir::Exists(configFile.GetPath())) | |
1234 | { | |
16c587ca RR |
1235 | configFile.RemoveDir( pos ); |
1236 | configFile.InsertDir( pos, wxT("crystalsvg") ); | |
10274336 RR |
1237 | } |
1238 | ||
1239 | if (!wxDir::Exists(configFile.GetPath())) | |
1240 | { | |
16c587ca RR |
1241 | configFile.RemoveDir( pos ); |
1242 | configFile.InsertDir( pos, wxT("crystal") ); | |
10274336 RR |
1243 | } |
1244 | ||
1245 | if (wxDir::Exists(configFile.GetPath())) | |
10274336 | 1246 | icondirs.Add( configFile.GetFullPath() ); |
1d529ef7 | 1247 | } |
cdf339c9 VS |
1248 | |
1249 | // settings in ~/.kde have maximal priority | |
2b5f62a0 VZ |
1250 | dirs.Add(wxGetHomeDir() + wxT("/.kde/share")); |
1251 | icondirs.Add(wxGetHomeDir() + wxT("/.kde/share/icons/")); | |
509a6196 | 1252 | |
10274336 | 1253 | if (kdedir) |
509a6196 | 1254 | { |
2b5f62a0 VZ |
1255 | dirs.Add( wxString(kdedir) + wxT("/share") ); |
1256 | icondirs.Add( wxString(kdedir) + wxT("/share/icons/") ); | |
509a6196 VZ |
1257 | } |
1258 | else | |
1259 | { | |
1260 | // try to guess KDEDIR | |
1261 | dirs.Add(_T("/usr/share")); | |
1262 | dirs.Add(_T("/opt/kde/share")); | |
cdf339c9 | 1263 | icondirs.Add(_T("/usr/share/icons/")); |
13e3b45a | 1264 | icondirs.Add(_T("/usr/X11R6/share/icons/")); // Debian/Corel linux |
cdf339c9 | 1265 | icondirs.Add(_T("/opt/kde/share/icons/")); |
509a6196 VZ |
1266 | } |
1267 | ||
678ebfcd | 1268 | if (!sExtraDir.empty()) dirs.Add (sExtraDir); |
2b813b73 VZ |
1269 | icondirs.Add(sExtraDir + wxT("/icons")); |
1270 | ||
b9517a0a VZ |
1271 | size_t nDirs = dirs.GetCount(); |
1272 | for ( size_t nDir = 0; nDir < nDirs; nDir++ ) | |
1273 | { | |
2b813b73 | 1274 | LoadKDELinkFilesFromDir(dirs[nDir], icondirs); |
b9517a0a | 1275 | } |
b9517a0a VZ |
1276 | } |
1277 | ||
2b813b73 VZ |
1278 | // ---------------------------------------------------------------------------- |
1279 | // wxFileTypeImpl (Unix) | |
1280 | // ---------------------------------------------------------------------------- | |
1281 | ||
2b813b73 | 1282 | wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const |
b9517a0a | 1283 | { |
2b813b73 VZ |
1284 | wxString sTmp; |
1285 | size_t i = 0; | |
678ebfcd | 1286 | while ( (i < m_index.GetCount() ) && sTmp.empty() ) |
b9517a0a | 1287 | { |
2b813b73 VZ |
1288 | sTmp = m_manager->GetCommand ( verb, m_index[i] ); |
1289 | i ++; | |
b9517a0a VZ |
1290 | } |
1291 | ||
2b813b73 VZ |
1292 | return wxFileType::ExpandCommand(sTmp, params); |
1293 | } | |
b9517a0a | 1294 | |
da0766ab | 1295 | bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const |
2b813b73 VZ |
1296 | |
1297 | { | |
1298 | wxString sTmp; | |
1299 | size_t i = 0; | |
678ebfcd | 1300 | while ( (i < m_index.GetCount() ) && sTmp.empty() ) |
c786f763 VZ |
1301 | { |
1302 | sTmp = m_manager->m_aIcons[m_index[i]]; | |
1303 | i ++; | |
1304 | } | |
da0766ab VZ |
1305 | if ( sTmp.empty () ) |
1306 | return FALSE; | |
b9517a0a | 1307 | |
da0766ab | 1308 | if ( iconLoc ) |
c786f763 | 1309 | { |
a49686b4 | 1310 | iconLoc->SetFileName(sTmp); |
2b813b73 | 1311 | } |
c786f763 | 1312 | |
da0766ab | 1313 | return TRUE; |
c786f763 | 1314 | } |
2b813b73 VZ |
1315 | |
1316 | ||
1317 | bool | |
1318 | wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const | |
1319 | { | |
1320 | mimeTypes.Clear(); | |
1321 | for (size_t i = 0; i < m_index.GetCount(); i++) | |
1322 | mimeTypes.Add(m_manager->m_aTypes[m_index[i]]); | |
1323 | return TRUE; | |
1324 | } | |
1325 | ||
1326 | ||
1327 | size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs, | |
1328 | wxArrayString *commands, | |
1329 | const wxFileType::MessageParameters& params) const | |
1330 | { | |
1331 | ||
1332 | wxString vrb, cmd, sTmp; | |
1333 | size_t count = 0; | |
678ebfcd | 1334 | wxMimeTypeCommands * sPairs; |
2b813b73 VZ |
1335 | |
1336 | // verbs and commands have been cleared already in mimecmn.cpp... | |
1337 | // if we find no entries in the exact match, try the inexact match | |
1338 | for (size_t n = 0; ((count ==0) && (n < m_index.GetCount())); n++) | |
1339 | { | |
1340 | // list of verb = command pairs for this mimetype | |
1341 | sPairs = m_manager->m_aEntries [m_index[n]]; | |
1342 | size_t i; | |
678ebfcd | 1343 | for ( i = 0; i < sPairs->GetCount (); i++ ) |
2b813b73 VZ |
1344 | { |
1345 | vrb = sPairs->GetVerb(i); | |
1346 | // some gnome entries have . inside | |
1347 | vrb = vrb.AfterLast(wxT('.')); | |
1348 | cmd = sPairs->GetCmd (i); | |
678ebfcd | 1349 | if (! cmd.empty() ) |
2b813b73 VZ |
1350 | { |
1351 | cmd = wxFileType::ExpandCommand(cmd, params); | |
1352 | count ++; | |
1353 | if ( vrb.IsSameAs (wxT("open"))) | |
1354 | { | |
1355 | verbs->Insert(vrb,0u); | |
1356 | commands ->Insert(cmd,0u); | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | verbs->Add (vrb); | |
1361 | commands->Add (cmd); | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | } | |
1366 | ||
1367 | } | |
1368 | return count; | |
1369 | ||
1370 | } | |
1371 | ||
1372 | bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) | |
1373 | { | |
1374 | wxString strExtensions = m_manager->GetExtension(m_index[0]); | |
1375 | extensions.Empty(); | |
1376 | ||
1377 | // one extension in the space or comma delimitid list | |
1378 | wxString strExt; | |
678ebfcd | 1379 | for ( const wxChar *p = strExtensions;; p++ ) { |
2b813b73 | 1380 | if ( *p == wxT(' ') || *p == wxT(',') || *p == wxT('\0') ) { |
678ebfcd | 1381 | if ( !strExt.empty() ) { |
2b813b73 VZ |
1382 | extensions.Add(strExt); |
1383 | strExt.Empty(); | |
1384 | } | |
1385 | //else: repeated spaces (shouldn't happen, but it's not that | |
1386 | // important if it does happen) | |
1387 | ||
1388 | if ( *p == wxT('\0') ) | |
1389 | break; | |
1390 | } | |
1391 | else if ( *p == wxT('.') ) { | |
1392 | // remove the dot from extension (but only if it's the first char) | |
678ebfcd | 1393 | if ( !strExt.empty() ) { |
2b813b73 VZ |
1394 | strExt += wxT('.'); |
1395 | } | |
1396 | //else: no, don't append it | |
1397 | } | |
1398 | else { | |
1399 | strExt += *p; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | return TRUE; | |
1404 | } | |
1405 | ||
1406 | // set an arbitrary command, | |
1407 | // could adjust the code to ask confirmation if it already exists and | |
1408 | // overwriteprompt is TRUE, but this is currently ignored as *Associate* has | |
1409 | // no overwrite prompt | |
1410 | bool wxFileTypeImpl::SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt /*= TRUE*/) | |
d84afea9 | 1411 | { |
2b813b73 | 1412 | wxArrayString strExtensions; |
678ebfcd | 1413 | wxString strDesc, strIcon; |
2b813b73 | 1414 | |
678ebfcd | 1415 | wxMimeTypeCommands *entry = new wxMimeTypeCommands (); |
2b813b73 VZ |
1416 | entry->Add(verb + wxT("=") + cmd + wxT(" %s ")); |
1417 | ||
1418 | wxArrayString strTypes; | |
1419 | GetMimeTypes (strTypes); | |
1420 | if (strTypes.GetCount() < 1) return FALSE; | |
1421 | ||
1422 | size_t i; | |
1423 | bool Ok = TRUE; | |
1424 | for (i = 0; i < strTypes.GetCount(); i++) | |
d84afea9 | 1425 | { |
2b813b73 | 1426 | if (!m_manager->DoAssociation (strTypes[i], strIcon, entry, strExtensions, strDesc)) |
d84afea9 GD |
1427 | Ok = FALSE; |
1428 | } | |
2b813b73 VZ |
1429 | |
1430 | return Ok; | |
d84afea9 | 1431 | } |
2b813b73 VZ |
1432 | |
1433 | // ignore index on the grouds that we only have one icon in a Unix file | |
1434 | bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon /*= wxEmptyString*/, int /*index = 0*/) | |
d84afea9 | 1435 | { |
678ebfcd | 1436 | if (strIcon.empty()) return FALSE; |
2b813b73 VZ |
1437 | wxArrayString strExtensions; |
1438 | wxString strDesc; | |
1439 | ||
678ebfcd | 1440 | wxMimeTypeCommands *entry = new wxMimeTypeCommands (); |
2b813b73 VZ |
1441 | |
1442 | wxArrayString strTypes; | |
1443 | GetMimeTypes (strTypes); | |
1444 | if (strTypes.GetCount() < 1) return FALSE; | |
1445 | ||
1446 | size_t i; | |
1447 | bool Ok = TRUE; | |
1448 | for (i = 0; i < strTypes.GetCount(); i++) | |
d84afea9 | 1449 | { |
2b813b73 | 1450 | if (!m_manager->DoAssociation (strTypes[i], strIcon, entry, strExtensions, strDesc)) |
d84afea9 GD |
1451 | Ok = FALSE; |
1452 | } | |
2b813b73 VZ |
1453 | |
1454 | return Ok; | |
d84afea9 GD |
1455 | } |
1456 | ||
2b813b73 VZ |
1457 | // ---------------------------------------------------------------------------- |
1458 | // wxMimeTypesManagerImpl (Unix) | |
1459 | // ---------------------------------------------------------------------------- | |
1460 | ||
1461 | ||
1462 | wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() | |
1463 | { | |
1464 | m_initialized = FALSE; | |
1465 | m_mailcapStylesInited = 0; | |
1466 | } | |
1467 | ||
1d529ef7 RR |
1468 | void wxMimeTypesManagerImpl::InitIfNeeded() |
1469 | { | |
1470 | if ( !m_initialized ) | |
1471 | { | |
1472 | // set the flag first to prevent recursion | |
1473 | m_initialized = TRUE; | |
10274336 | 1474 | |
1d529ef7 | 1475 | #if 0 |
10274336 RR |
1476 | wxString wm = wxGetenv( wxT("WINDOWMANAGER") ); |
1477 | ||
1478 | if (wm.Find( wxT("kde") ) != wxNOT_FOUND) | |
1479 | Initialize( wxMAILCAP_KDE|wxMAILCAP_STANDARD ); | |
1480 | else if (wm.Find( wxT("gnome") ) != wxNOT_FOUND) | |
1481 | Initialize( wxMAILCAP_GNOME|wxMAILCAP_STANDARD ); | |
1482 | else | |
1d529ef7 | 1483 | #endif |
10274336 | 1484 | Initialize(); |
1d529ef7 RR |
1485 | } |
1486 | } | |
1487 | ||
2b813b73 VZ |
1488 | // read system and user mailcaps and other files |
1489 | void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, | |
1490 | const wxString& sExtraDir) | |
1491 | { | |
1492 | // read mimecap amd mime.types | |
a06c1b9b VZ |
1493 | if ( (mailcapStyles & wxMAILCAP_NETSCAPE) || |
1494 | (mailcapStyles & wxMAILCAP_STANDARD) ) | |
2b813b73 VZ |
1495 | GetMimeInfo(sExtraDir); |
1496 | ||
1497 | // read GNOME tables | |
1d529ef7 | 1498 | if (mailcapStyles & wxMAILCAP_GNOME) |
2b813b73 VZ |
1499 | GetGnomeMimeInfo(sExtraDir); |
1500 | ||
1501 | // read KDE tables | |
1d529ef7 | 1502 | if (mailcapStyles & wxMAILCAP_KDE) |
2b813b73 VZ |
1503 | GetKDEMimeInfo(sExtraDir); |
1504 | ||
1505 | m_mailcapStylesInited |= mailcapStyles; | |
1506 | } | |
1507 | ||
1508 | // clear data so you can read another group of WM files | |
1509 | void wxMimeTypesManagerImpl::ClearData() | |
1510 | { | |
1511 | m_aTypes.Clear (); | |
1512 | m_aIcons.Clear (); | |
1513 | m_aExtensions.Clear (); | |
1514 | m_aDescriptions.Clear (); | |
1515 | ||
2b5f62a0 | 1516 | WX_CLEAR_ARRAY(m_aEntries); |
678ebfcd VZ |
1517 | m_aEntries.Empty(); |
1518 | ||
2b813b73 VZ |
1519 | m_mailcapStylesInited = 0; |
1520 | } | |
1521 | ||
1522 | wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl() | |
1523 | { | |
678ebfcd | 1524 | ClearData(); |
2b813b73 VZ |
1525 | } |
1526 | ||
1527 | ||
1528 | void wxMimeTypesManagerImpl::GetMimeInfo (const wxString& sExtraDir) | |
1529 | { | |
1530 | // read this for netscape or Metamail formats | |
1531 | ||
1532 | // directories where we look for mailcap and mime.types by default | |
1533 | // used by netscape and pine and other mailers, using 2 different formats! | |
1534 | ||
1535 | // (taken from metamail(1) sources) | |
1536 | // | |
1537 | // although RFC 1524 specifies the search path of | |
1538 | // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more | |
1539 | // places - OTOH, the RFC also says that this path can be changed with | |
1540 | // MAILCAPS environment variable (containing the colon separated full | |
1541 | // filenames to try) which is not done yet (TODO?) | |
1542 | ||
1543 | wxString strHome = wxGetenv(wxT("HOME")); | |
1544 | ||
1545 | wxArrayString dirs; | |
d353bd45 | 1546 | dirs.Add ( strHome + wxT("/.") ); |
2b813b73 VZ |
1547 | dirs.Add ( wxT("/etc/") ); |
1548 | dirs.Add ( wxT("/usr/etc/") ); | |
1549 | dirs.Add ( wxT("/usr/local/etc/") ); | |
1550 | dirs.Add ( wxT("/etc/mail/") ); | |
1551 | dirs.Add ( wxT("/usr/public/lib/") ); | |
678ebfcd | 1552 | if (!sExtraDir.empty()) dirs.Add ( sExtraDir + wxT("/") ); |
2b813b73 VZ |
1553 | |
1554 | size_t nDirs = dirs.GetCount(); | |
1555 | for ( size_t nDir = 0; nDir < nDirs; nDir++ ) | |
1556 | { | |
1557 | wxString file = dirs[nDir] + wxT("mailcap"); | |
1558 | if ( wxFile::Exists(file) ) { | |
1559 | ReadMailcap(file); | |
1560 | } | |
1561 | ||
1562 | file = dirs[nDir] + wxT("mime.types"); | |
1563 | if ( wxFile::Exists(file) ) { | |
1564 | ReadMimeTypes(file); | |
1565 | } | |
1566 | } | |
1567 | ||
1568 | } | |
1569 | ||
1570 | bool wxMimeTypesManagerImpl::WriteToMimeTypes (int index, bool delete_index) | |
1571 | { | |
1572 | // check we have the right manager | |
a06c1b9b | 1573 | if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD) ) |
2b813b73 VZ |
1574 | return FALSE; |
1575 | ||
1576 | bool bTemp; | |
1577 | wxString strHome = wxGetenv(wxT("HOME")); | |
1578 | ||
1579 | // and now the users mailcap | |
1580 | wxString strUserMailcap = strHome + wxT("/.mime.types"); | |
1581 | ||
1582 | wxMimeTextFile file; | |
1583 | if ( wxFile::Exists(strUserMailcap) ) | |
1584 | { | |
1585 | bTemp = file.Open(strUserMailcap); | |
1586 | } | |
1587 | else | |
1588 | { | |
1589 | if (delete_index) return FALSE; | |
1590 | bTemp = file.Create(strUserMailcap); | |
1591 | } | |
1592 | if (bTemp) | |
1593 | { | |
1594 | int nIndex; | |
1595 | // test for netscape's header and return FALSE if its found | |
1596 | nIndex = file.pIndexOf (wxT("#--Netscape")); | |
1597 | if (nIndex != wxNOT_FOUND) | |
1598 | { | |
1599 | wxASSERT_MSG(FALSE,wxT("Error in .mime.types \nTrying to mix Netscape and Metamail formats\nFile not modiifed")); | |
1600 | return FALSE; | |
1601 | } | |
1602 | // write it in alternative format | |
1603 | // get rid of unwanted entries | |
1604 | wxString strType = m_aTypes[index]; | |
1605 | nIndex = file.pIndexOf (strType); | |
1606 | // get rid of all the unwanted entries... | |
1607 | if (nIndex != wxNOT_FOUND) file.CommentLine (nIndex); | |
1608 | ||
1609 | if (!delete_index) | |
1610 | { | |
1611 | // add the new entries in | |
1612 | wxString sTmp = strType.Append (wxT(' '), 40-strType.Len() ); | |
1613 | sTmp = sTmp + m_aExtensions[index]; | |
1614 | file.AddLine (sTmp); | |
1615 | } | |
1616 | ||
1617 | ||
1618 | bTemp = file.Write (); | |
1619 | file.Close (); | |
1620 | } | |
1621 | return bTemp; | |
1622 | } | |
1623 | ||
1624 | bool wxMimeTypesManagerImpl::WriteToNSMimeTypes (int index, bool delete_index) | |
1625 | { | |
1626 | //check we have the right managers | |
1627 | if (! ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) ) | |
1628 | return FALSE; | |
1629 | ||
1630 | bool bTemp; | |
1631 | wxString strHome = wxGetenv(wxT("HOME")); | |
1632 | ||
1633 | // and now the users mailcap | |
1634 | wxString strUserMailcap = strHome + wxT("/.mime.types"); | |
1635 | ||
1636 | wxMimeTextFile file; | |
1637 | if ( wxFile::Exists(strUserMailcap) ) | |
1638 | { | |
1639 | bTemp = file.Open(strUserMailcap); | |
1640 | } | |
1641 | else | |
1642 | { | |
1643 | if (delete_index) return FALSE; | |
1644 | bTemp = file.Create(strUserMailcap); | |
1645 | } | |
1646 | if (bTemp) | |
1647 | { | |
1648 | ||
1649 | // write it in the format that Netscape uses | |
1650 | int nIndex; | |
1651 | // test for netscape's header and insert if required... | |
1652 | // this is a comment so use TRUE | |
1653 | nIndex = file.pIndexOf (wxT("#--Netscape"), TRUE); | |
1654 | if (nIndex == wxNOT_FOUND) | |
1655 | { | |
1656 | // either empty file or metamail format | |
1657 | // at present we can't cope with mixed formats, so exit to preseve | |
1658 | // metamail entreies | |
1659 | if (file.GetLineCount () > 0) | |
1660 | { | |
1661 | wxASSERT_MSG(FALSE, wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap")); | |
1662 | return FALSE; | |
1663 | } | |
1664 | file.InsertLine (wxT( "#--Netscape Communications Corporation MIME Information" ), 0); | |
1665 | nIndex = 0; | |
1666 | } | |
1667 | ||
1668 | wxString strType = wxT("type=") + m_aTypes[index]; | |
1669 | nIndex = file.pIndexOf (strType); | |
1670 | // get rid of all the unwanted entries... | |
1671 | if (nIndex != wxNOT_FOUND) | |
1672 | { | |
1673 | wxString sOld = file[nIndex]; | |
1674 | while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) ) | |
1675 | { | |
1676 | file.CommentLine(nIndex); | |
1677 | sOld = file[nIndex]; | |
1678 | wxLogTrace(TRACE_MIME, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex, sOld.c_str()); | |
1679 | nIndex ++; | |
1680 | } | |
1681 | if (nIndex < (int) file.GetLineCount()) file.CommentLine (nIndex); | |
1682 | } | |
1683 | else nIndex = (int) file.GetLineCount(); | |
1684 | ||
1685 | wxString sTmp = strType + wxT(" \\"); | |
1686 | if (!delete_index) file.InsertLine (sTmp, nIndex); | |
678ebfcd | 1687 | if ( ! m_aDescriptions.Item(index).empty() ) |
2b813b73 | 1688 | { |
678ebfcd | 1689 | sTmp = wxT("desc=\"") + m_aDescriptions[index]+ wxT("\" \\"); //.trim ?? |
2b813b73 VZ |
1690 | if (!delete_index) |
1691 | { | |
1692 | nIndex ++; | |
1693 | file.InsertLine (sTmp, nIndex); | |
1694 | } | |
1695 | } | |
1696 | wxString sExts = m_aExtensions.Item(index); | |
1697 | sTmp = wxT("exts=\"") + sExts.Trim(FALSE).Trim() + wxT("\""); | |
1698 | if (!delete_index) | |
1699 | { | |
1700 | nIndex ++; | |
1701 | file.InsertLine (sTmp, nIndex); | |
1702 | } | |
1703 | ||
1704 | bTemp = file.Write (); | |
1705 | file.Close (); | |
1706 | } | |
1707 | return bTemp; | |
1708 | } | |
1709 | ||
1710 | ||
1711 | bool wxMimeTypesManagerImpl::WriteToMailCap (int index, bool delete_index) | |
1712 | { | |
1713 | //check we have the right managers | |
1714 | if ( !( ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) || | |
a06c1b9b | 1715 | ( m_mailcapStylesInited & wxMAILCAP_STANDARD) ) ) |
2b813b73 VZ |
1716 | return FALSE; |
1717 | ||
1718 | bool bTemp; | |
1719 | wxString strHome = wxGetenv(wxT("HOME")); | |
1720 | ||
1721 | // and now the users mailcap | |
1722 | wxString strUserMailcap = strHome + wxT("/.mailcap"); | |
1723 | ||
1724 | wxMimeTextFile file; | |
1725 | if ( wxFile::Exists(strUserMailcap) ) | |
1726 | { | |
1727 | bTemp = file.Open(strUserMailcap); | |
1728 | } | |
be0a33fb | 1729 | else |
2b813b73 VZ |
1730 | { |
1731 | if (delete_index) return FALSE; | |
1732 | bTemp = file.Create(strUserMailcap); | |
1733 | } | |
1734 | if (bTemp) | |
1735 | { | |
1736 | // now got a file we can write to .... | |
678ebfcd VZ |
1737 | wxMimeTypeCommands * entries = m_aEntries[index]; |
1738 | size_t iOpen; | |
1739 | wxString sCmd = entries->GetCommandForVerb(_T("open"), &iOpen); | |
2b813b73 VZ |
1740 | wxString sTmp; |
1741 | ||
1742 | sTmp = m_aTypes[index]; | |
1743 | wxString sOld; | |
1744 | int nIndex = file.pIndexOf(sTmp); | |
1745 | // get rid of all the unwanted entries... | |
1746 | if (nIndex == wxNOT_FOUND) | |
1747 | { | |
1748 | nIndex = (int) file.GetLineCount(); | |
1749 | } | |
1750 | else | |
1751 | { | |
1752 | sOld = file[nIndex]; | |
1753 | wxLogTrace(TRACE_MIME, wxT("--- Deleting from mailcap line '%d' ---"), nIndex); | |
6dc6fda6 | 1754 | |
2b813b73 VZ |
1755 | while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) ) |
1756 | { | |
1757 | file.CommentLine(nIndex); | |
1758 | if (nIndex < (int) file.GetLineCount()) sOld = sOld + file[nIndex]; | |
1759 | } | |
1760 | if (nIndex < (int) file.GetLineCount()) file.CommentLine (nIndex); | |
1761 | } | |
6dc6fda6 | 1762 | |
678ebfcd | 1763 | sTmp = sTmp + wxT(";") + sCmd; //includes wxT(" %s "); |
b9517a0a | 1764 | |
2b813b73 | 1765 | // write it in the format that Netscape uses (default) |
a06c1b9b | 1766 | if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) ) |
2b813b73 VZ |
1767 | { |
1768 | if (! delete_index) file.InsertLine (sTmp, nIndex); | |
1769 | nIndex ++; | |
1770 | } | |
b9517a0a | 1771 | |
2b813b73 VZ |
1772 | // write extended format |
1773 | else | |
1774 | { | |
1775 | // todo FIX this code; | |
1776 | // ii) lost entries | |
1777 | // sOld holds all the entries, but our data store only has some | |
1778 | // eg test= is not stored | |
1779 | ||
1780 | // so far we have written the mimetype and command out | |
1781 | wxStringTokenizer sT (sOld, wxT(";\\")); | |
1782 | if (sT.CountTokens () > 2) | |
1783 | { | |
1784 | // first one mimetype; second one command, rest unknown... | |
1785 | wxString s; | |
1786 | s = sT.GetNextToken(); | |
1787 | s = sT.GetNextToken(); | |
1788 | ||
1789 | // first unknown | |
1790 | s = sT.GetNextToken(); | |
678ebfcd | 1791 | while ( ! s.empty() ) |
2b813b73 VZ |
1792 | { |
1793 | bool bKnownToken = FALSE; | |
1794 | if (s.Contains(wxT("description="))) bKnownToken = TRUE; | |
1795 | if (s.Contains(wxT("x11-bitmap="))) bKnownToken = TRUE; | |
1796 | size_t i; | |
1797 | for (i=0; i < entries->GetCount(); i++) | |
1798 | { | |
1799 | if (s.Contains(entries->GetVerb(i))) bKnownToken = TRUE; | |
1800 | } | |
1801 | if (!bKnownToken) | |
1802 | { | |
1803 | sTmp = sTmp + wxT("; \\"); | |
1804 | file.InsertLine (sTmp, nIndex); | |
1805 | sTmp = s; | |
1806 | } | |
1807 | s = sT.GetNextToken (); | |
1808 | } | |
cdf339c9 | 1809 | |
2b813b73 | 1810 | } |
5bd3a2da | 1811 | |
678ebfcd | 1812 | if (! m_aDescriptions[index].empty() ) |
2b813b73 VZ |
1813 | { |
1814 | sTmp = sTmp + wxT("; \\"); | |
1815 | file.InsertLine (sTmp, nIndex); | |
1816 | nIndex ++; | |
1817 | sTmp = wxT(" description=\"") + m_aDescriptions[index] + wxT("\""); | |
1818 | } | |
cdf339c9 | 1819 | |
678ebfcd | 1820 | if (! m_aIcons[index].empty() ) |
2b813b73 VZ |
1821 | { |
1822 | sTmp = sTmp + wxT("; \\"); | |
1823 | file.InsertLine (sTmp, nIndex); | |
1824 | nIndex ++; | |
1825 | sTmp = wxT(" x11-bitmap=\"") + m_aIcons[index] + wxT("\""); | |
1826 | } | |
1827 | if ( entries->GetCount() > 1 ) | |
cdf339c9 | 1828 | |
2b813b73 VZ |
1829 | { |
1830 | size_t i; | |
1831 | for (i=0; i < entries->GetCount(); i++) | |
1832 | if ( i != iOpen ) | |
1833 | { | |
1834 | sTmp = sTmp + wxT("; \\"); | |
1835 | file.InsertLine (sTmp, nIndex); | |
1836 | nIndex ++; | |
678ebfcd | 1837 | sTmp = wxT(" ") + entries->GetVerbCmd(i); |
2b813b73 VZ |
1838 | } |
1839 | } | |
b9517a0a | 1840 | |
2b813b73 VZ |
1841 | file.InsertLine (sTmp, nIndex); |
1842 | nIndex ++; | |
c93d6770 | 1843 | |
b13d92d1 | 1844 | } |
2b813b73 VZ |
1845 | bTemp = file.Write (); |
1846 | file.Close (); | |
b13d92d1 | 1847 | } |
2b813b73 | 1848 | return bTemp; |
b13d92d1 VZ |
1849 | } |
1850 | ||
2b813b73 VZ |
1851 | wxFileType * |
1852 | wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo) | |
b9517a0a | 1853 | { |
2b813b73 | 1854 | InitIfNeeded(); |
b9517a0a | 1855 | |
2b813b73 VZ |
1856 | wxString strType = ftInfo.GetMimeType (); |
1857 | wxString strDesc = ftInfo.GetDescription (); | |
1858 | wxString strIcon = ftInfo.GetIconFile (); | |
1859 | ||
678ebfcd | 1860 | wxMimeTypeCommands *entry = new wxMimeTypeCommands (); |
2b813b73 | 1861 | |
678ebfcd | 1862 | if ( ! ftInfo.GetOpenCommand().empty()) |
2b813b73 | 1863 | entry->Add(wxT("open=") + ftInfo.GetOpenCommand () + wxT(" %s ")); |
678ebfcd | 1864 | if ( ! ftInfo.GetPrintCommand ().empty()) |
2b813b73 VZ |
1865 | entry->Add(wxT("print=") + ftInfo.GetPrintCommand () + wxT(" %s ")); |
1866 | ||
1867 | // now find where these extensions are in the data store and remove them | |
1868 | wxArrayString sA_Exts = ftInfo.GetExtensions (); | |
1869 | wxString sExt, sExtStore; | |
1870 | size_t i, nIndex; | |
1871 | for (i=0; i < sA_Exts.GetCount(); i++) | |
4d2976ad | 1872 | { |
2b813b73 VZ |
1873 | sExt = sA_Exts.Item(i); |
1874 | //clean up to just a space before and after | |
1875 | sExt.Trim().Trim(FALSE); | |
1876 | sExt = wxT(' ') + sExt + wxT(' '); | |
1877 | for (nIndex = 0; nIndex < m_aExtensions.GetCount(); nIndex ++) | |
1878 | { | |
1879 | sExtStore = m_aExtensions.Item(nIndex); | |
678ebfcd | 1880 | if (sExtStore.Replace(sExt, wxT(" ") ) > 0) m_aExtensions.Item(nIndex) = sExtStore; |
b9517a0a VZ |
1881 | } |
1882 | ||
2b813b73 | 1883 | } |
b9517a0a | 1884 | |
2b813b73 VZ |
1885 | if ( !DoAssociation (strType, strIcon, entry, sA_Exts, strDesc) ) |
1886 | return NULL; | |
4d2976ad | 1887 | |
2b813b73 | 1888 | return GetFileTypeFromMimeType(strType); |
4d2976ad VS |
1889 | } |
1890 | ||
1891 | ||
2b813b73 VZ |
1892 | bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType, |
1893 | const wxString& strIcon, | |
678ebfcd | 1894 | wxMimeTypeCommands *entry, |
2b813b73 VZ |
1895 | const wxArrayString& strExtensions, |
1896 | const wxString& strDesc) | |
b13d92d1 | 1897 | { |
2b813b73 | 1898 | int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, TRUE); |
b13d92d1 | 1899 | |
2b813b73 | 1900 | if ( nIndex == wxNOT_FOUND ) |
b13d92d1 | 1901 | return FALSE; |
b13d92d1 | 1902 | |
2b813b73 | 1903 | return WriteMimeInfo (nIndex, FALSE); |
b13d92d1 VZ |
1904 | } |
1905 | ||
2b813b73 | 1906 | bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex, bool delete_mime ) |
b13d92d1 | 1907 | { |
2b813b73 | 1908 | bool ok = TRUE; |
b13d92d1 | 1909 | |
a06c1b9b | 1910 | if ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) |
2b813b73 VZ |
1911 | { |
1912 | // write in metamail format; | |
1913 | if (WriteToMimeTypes (nIndex, delete_mime) ) | |
1914 | if ( WriteToMailCap (nIndex, delete_mime) ) | |
1915 | ok = FALSE; | |
b13d92d1 | 1916 | } |
2b813b73 | 1917 | if ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE ) |
b9517a0a | 1918 | { |
2b813b73 VZ |
1919 | // write in netsacpe format; |
1920 | if (WriteToNSMimeTypes (nIndex, delete_mime) ) | |
1921 | if ( WriteToMailCap (nIndex, delete_mime) ) | |
1922 | ok = FALSE; | |
1923 | } | |
1924 | if (m_mailcapStylesInited & wxMAILCAP_GNOME) | |
1925 | { | |
1926 | // write in Gnome format; | |
1927 | if (WriteGnomeMimeFile (nIndex, delete_mime) ) | |
1928 | if (WriteGnomeKeyFile (nIndex, delete_mime) ) | |
1929 | ok = FALSE; | |
1930 | } | |
1931 | if (m_mailcapStylesInited & wxMAILCAP_KDE) | |
1932 | { | |
1933 | // write in KDE format; | |
1934 | if (WriteKDEMimeFile (nIndex, delete_mime) ) | |
1935 | ok = FALSE; | |
b9517a0a VZ |
1936 | } |
1937 | ||
2b813b73 | 1938 | return ok; |
a6c65e88 VZ |
1939 | } |
1940 | ||
2b813b73 VZ |
1941 | int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType, |
1942 | const wxString& strIcon, | |
678ebfcd | 1943 | wxMimeTypeCommands *entry, |
2b813b73 VZ |
1944 | const wxArrayString& strExtensions, |
1945 | const wxString& strDesc, | |
678ebfcd | 1946 | bool replaceExisting) |
b13d92d1 | 1947 | { |
2b813b73 | 1948 | InitIfNeeded(); |
b13d92d1 | 1949 | |
2b813b73 | 1950 | // ensure mimetype is always lower case |
678ebfcd VZ |
1951 | wxString mimeType = strType.Lower(); |
1952 | ||
1953 | // is this a known MIME type? | |
2b813b73 VZ |
1954 | int nIndex = m_aTypes.Index(mimeType); |
1955 | if ( nIndex == wxNOT_FOUND ) | |
1956 | { | |
1957 | // new file type | |
1958 | m_aTypes.Add(mimeType); | |
1959 | m_aIcons.Add(strIcon); | |
678ebfcd | 1960 | m_aEntries.Add(entry ? entry : new wxMimeTypeCommands); |
b13d92d1 | 1961 | |
678ebfcd | 1962 | // change nIndex so we can use it below to add the extensions |
df5168c4 | 1963 | m_aExtensions.Add(wxEmptyString); |
b8d61021 | 1964 | nIndex = m_aExtensions.size() - 1; |
678ebfcd VZ |
1965 | |
1966 | m_aDescriptions.Add(strDesc); | |
b13d92d1 | 1967 | } |
678ebfcd | 1968 | else // yes, we already have it |
2b813b73 | 1969 | { |
678ebfcd | 1970 | if ( replaceExisting ) |
2b813b73 VZ |
1971 | { |
1972 | // if new description change it | |
678ebfcd | 1973 | if ( !strDesc.empty()) |
2b813b73 VZ |
1974 | m_aDescriptions[nIndex] = strDesc; |
1975 | ||
1976 | // if new icon change it | |
678ebfcd | 1977 | if ( !strIcon.empty()) |
2b813b73 VZ |
1978 | m_aIcons[nIndex] = strIcon; |
1979 | ||
678ebfcd VZ |
1980 | if ( entry ) |
1981 | { | |
1982 | delete m_aEntries[nIndex]; | |
1983 | m_aEntries[nIndex] = entry; | |
1984 | } | |
2b813b73 | 1985 | } |
678ebfcd | 1986 | else // add data we don't already have ... |
2b813b73 | 1987 | { |
2b813b73 | 1988 | // if new description add only if none |
678ebfcd | 1989 | if ( m_aDescriptions[nIndex].empty() ) |
2b813b73 VZ |
1990 | m_aDescriptions[nIndex] = strDesc; |
1991 | ||
1992 | // if new icon and no existing icon | |
678ebfcd | 1993 | if ( m_aIcons[nIndex].empty () ) |
2b813b73 VZ |
1994 | m_aIcons[nIndex] = strIcon; |
1995 | ||
2b813b73 | 1996 | // add any new entries... |
678ebfcd | 1997 | if ( entry ) |
2b813b73 | 1998 | { |
678ebfcd VZ |
1999 | wxMimeTypeCommands *entryOld = m_aEntries[nIndex]; |
2000 | ||
2001 | size_t count = entry->GetCount(); | |
2002 | for ( size_t i = 0; i < count; i++ ) | |
2003 | { | |
2004 | const wxString& verb = entry->GetVerb(i); | |
2005 | if ( !entryOld->HasVerb(verb) ) | |
2006 | { | |
2007 | entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i)); | |
2008 | } | |
2009 | } | |
2b293fc7 VZ |
2010 | |
2011 | // as we don't store it anywhere, it won't be deleted later as | |
2012 | // usual -- do it immediately instead | |
2013 | delete entry; | |
2b813b73 VZ |
2014 | } |
2015 | } | |
b13d92d1 | 2016 | } |
4d2976ad | 2017 | |
678ebfcd VZ |
2018 | // always add the extensions to this mimetype |
2019 | wxString& exts = m_aExtensions[nIndex]; | |
2020 | ||
2021 | // add all extensions we don't have yet | |
2022 | size_t count = strExtensions.GetCount(); | |
2023 | for ( size_t i = 0; i < count; i++ ) | |
2024 | { | |
2025 | wxString ext = strExtensions[i] + _T(' '); | |
2026 | ||
2027 | if ( exts.Find(ext) == wxNOT_FOUND ) | |
2028 | { | |
2029 | exts += ext; | |
2030 | } | |
2031 | } | |
2032 | ||
2b813b73 VZ |
2033 | // check data integrity |
2034 | wxASSERT( m_aTypes.Count() == m_aEntries.Count() && | |
678ebfcd VZ |
2035 | m_aTypes.Count() == m_aExtensions.Count() && |
2036 | m_aTypes.Count() == m_aIcons.Count() && | |
2037 | m_aTypes.Count() == m_aDescriptions.Count() ); | |
cf471cab | 2038 | |
2b813b73 | 2039 | return nIndex; |
cf471cab VS |
2040 | } |
2041 | ||
2042 | ||
b13d92d1 VZ |
2043 | wxFileType * |
2044 | wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) | |
2045 | { | |
678ebfcd | 2046 | if (ext.empty() ) |
2b813b73 VZ |
2047 | return NULL; |
2048 | ||
a6c65e88 VZ |
2049 | InitIfNeeded(); |
2050 | ||
1ee17e1c | 2051 | size_t count = m_aExtensions.GetCount(); |
2b813b73 | 2052 | for ( size_t n = 0; n < count; n++ ) |
678ebfcd VZ |
2053 | { |
2054 | wxStringTokenizer tk(m_aExtensions[n], _T(' ')); | |
1ee17e1c | 2055 | |
678ebfcd VZ |
2056 | while ( tk.HasMoreTokens() ) |
2057 | { | |
1ee17e1c | 2058 | // consider extensions as not being case-sensitive |
678ebfcd VZ |
2059 | if ( tk.GetNextToken().IsSameAs(ext, FALSE /* no case */) ) |
2060 | { | |
1ee17e1c | 2061 | // found |
678ebfcd | 2062 | wxFileType *fileType = new wxFileType; |
1ee17e1c | 2063 | fileType->m_impl->Init(this, n); |
678ebfcd VZ |
2064 | |
2065 | return fileType; | |
1ee17e1c VZ |
2066 | } |
2067 | } | |
2068 | } | |
b13d92d1 | 2069 | |
678ebfcd | 2070 | return NULL; |
b13d92d1 VZ |
2071 | } |
2072 | ||
2073 | wxFileType * | |
2074 | wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) | |
2075 | { | |
a6c65e88 VZ |
2076 | InitIfNeeded(); |
2077 | ||
2b813b73 | 2078 | wxFileType * fileType = NULL; |
b13d92d1 VZ |
2079 | // mime types are not case-sensitive |
2080 | wxString mimetype(mimeType); | |
2081 | mimetype.MakeLower(); | |
2082 | ||
2083 | // first look for an exact match | |
2084 | int index = m_aTypes.Index(mimetype); | |
2b813b73 VZ |
2085 | if ( index != wxNOT_FOUND ) |
2086 | { | |
2087 | fileType = new wxFileType; | |
2088 | fileType->m_impl->Init(this, index); | |
2089 | } | |
2090 | ||
2091 | // then try to find "text/*" as match for "text/plain" (for example) | |
2092 | // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return | |
2093 | // the whole string - ok. | |
2094 | ||
2095 | index = wxNOT_FOUND; | |
2096 | wxString strCategory = mimetype.BeforeFirst(wxT('/')); | |
2097 | ||
2098 | size_t nCount = m_aTypes.Count(); | |
2099 | for ( size_t n = 0; n < nCount; n++ ) { | |
2100 | if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) && | |
2101 | m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") ) { | |
2102 | index = n; | |
2103 | break; | |
b13d92d1 | 2104 | } |
2b813b73 | 2105 | |
b13d92d1 VZ |
2106 | } |
2107 | ||
2b813b73 VZ |
2108 | if ( index != wxNOT_FOUND ) |
2109 | { | |
2110 | fileType = new wxFileType; | |
b13d92d1 | 2111 | fileType->m_impl->Init(this, index); |
b13d92d1 | 2112 | } |
2b813b73 VZ |
2113 | return fileType; |
2114 | } | |
2115 | ||
2116 | ||
2117 | wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const | |
2118 | { | |
2119 | wxString command, testcmd, sV, sTmp; | |
2120 | sV = verb + wxT("="); | |
2121 | // list of verb = command pairs for this mimetype | |
678ebfcd | 2122 | wxMimeTypeCommands * sPairs = m_aEntries [nIndex]; |
2b813b73 VZ |
2123 | |
2124 | size_t i; | |
678ebfcd | 2125 | for ( i = 0; i < sPairs->GetCount (); i++ ) |
2b813b73 | 2126 | { |
678ebfcd VZ |
2127 | sTmp = sPairs->GetVerbCmd (i); |
2128 | if ( sTmp.Contains(sV) ) | |
2129 | command = sTmp.AfterFirst(wxT('=')); | |
b13d92d1 | 2130 | } |
2b813b73 | 2131 | return command; |
b13d92d1 VZ |
2132 | } |
2133 | ||
8e124873 VZ |
2134 | void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype) |
2135 | { | |
a6c65e88 VZ |
2136 | InitIfNeeded(); |
2137 | ||
3f1aaa16 | 2138 | wxString extensions; |
8e124873 VZ |
2139 | const wxArrayString& exts = filetype.GetExtensions(); |
2140 | size_t nExts = exts.GetCount(); | |
2141 | for ( size_t nExt = 0; nExt < nExts; nExt++ ) { | |
2142 | if ( nExt > 0 ) { | |
223d09f6 | 2143 | extensions += wxT(' '); |
8e124873 VZ |
2144 | } |
2145 | extensions += exts[nExt]; | |
2146 | } | |
2147 | ||
2148 | AddMimeTypeInfo(filetype.GetMimeType(), | |
2149 | extensions, | |
2150 | filetype.GetDescription()); | |
2151 | ||
2152 | AddMailcapInfo(filetype.GetMimeType(), | |
2153 | filetype.GetOpenCommand(), | |
2154 | filetype.GetPrintCommand(), | |
223d09f6 | 2155 | wxT(""), |
8e124873 VZ |
2156 | filetype.GetDescription()); |
2157 | } | |
2158 | ||
2159 | void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType, | |
2160 | const wxString& strExtensions, | |
2161 | const wxString& strDesc) | |
2162 | { | |
2b813b73 VZ |
2163 | // reading mailcap may find image/* , while |
2164 | // reading mime.types finds image/gif and no match is made | |
2165 | // this means all the get functions don't work fix this | |
2166 | wxString strIcon; | |
2167 | wxString sTmp = strExtensions; | |
a6c65e88 | 2168 | |
2b813b73 VZ |
2169 | wxArrayString sExts; |
2170 | sTmp.Trim().Trim(FALSE); | |
2171 | ||
678ebfcd | 2172 | while (!sTmp.empty()) |
2b813b73 VZ |
2173 | { |
2174 | sExts.Add (sTmp.AfterLast(wxT(' '))); | |
2175 | sTmp = sTmp.BeforeLast(wxT(' ')); | |
8e124873 | 2176 | } |
2b813b73 | 2177 | |
678ebfcd | 2178 | AddToMimeData (strMimeType, strIcon, NULL, sExts, strDesc, TRUE); |
8e124873 VZ |
2179 | } |
2180 | ||
2181 | void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType, | |
2182 | const wxString& strOpenCmd, | |
2183 | const wxString& strPrintCmd, | |
2184 | const wxString& strTest, | |
2185 | const wxString& strDesc) | |
2186 | { | |
a6c65e88 VZ |
2187 | InitIfNeeded(); |
2188 | ||
678ebfcd | 2189 | wxMimeTypeCommands *entry = new wxMimeTypeCommands; |
2b813b73 VZ |
2190 | entry->Add(wxT("open=") + strOpenCmd); |
2191 | entry->Add(wxT("print=") + strPrintCmd); | |
2192 | entry->Add(wxT("test=") + strTest); | |
8e124873 | 2193 | |
2b813b73 VZ |
2194 | wxString strIcon; |
2195 | wxArrayString strExtensions; | |
2196 | ||
2197 | AddToMimeData (strType, strIcon, entry, strExtensions, strDesc, TRUE); | |
8e124873 | 2198 | |
8e124873 VZ |
2199 | } |
2200 | ||
cc385968 | 2201 | bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) |
b13d92d1 | 2202 | { |
f6bcfd97 BP |
2203 | wxLogTrace(TRACE_MIME, wxT("--- Parsing mime.types file '%s' ---"), |
2204 | strFileName.c_str()); | |
b13d92d1 VZ |
2205 | |
2206 | wxTextFile file(strFileName); | |
2b5f62a0 VZ |
2207 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
2208 | if ( !file.Open( wxConvUTF8) ) | |
2209 | #else | |
b13d92d1 | 2210 | if ( !file.Open() ) |
2b5f62a0 | 2211 | #endif |
cc385968 | 2212 | return FALSE; |
b13d92d1 VZ |
2213 | |
2214 | // the information we extract | |
2215 | wxString strMimeType, strDesc, strExtensions; | |
2216 | ||
2217 | size_t nLineCount = file.GetLineCount(); | |
50920146 | 2218 | const wxChar *pc = NULL; |
2b5f62a0 VZ |
2219 | for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) |
2220 | { | |
22b4634c VZ |
2221 | if ( pc == NULL ) { |
2222 | // now we're at the start of the line | |
2223 | pc = file[nLine].c_str(); | |
2224 | } | |
2225 | else { | |
2226 | // we didn't finish with the previous line yet | |
2227 | nLine--; | |
2228 | } | |
b13d92d1 VZ |
2229 | |
2230 | // skip whitespace | |
50920146 | 2231 | while ( wxIsspace(*pc) ) |
b13d92d1 VZ |
2232 | pc++; |
2233 | ||
54acce90 VZ |
2234 | // comment or blank line? |
2235 | if ( *pc == wxT('#') || !*pc ) { | |
22b4634c VZ |
2236 | // skip the whole line |
2237 | pc = NULL; | |
b13d92d1 | 2238 | continue; |
22b4634c | 2239 | } |
b13d92d1 VZ |
2240 | |
2241 | // detect file format | |
223d09f6 | 2242 | const wxChar *pEqualSign = wxStrchr(pc, wxT('=')); |
b13d92d1 VZ |
2243 | if ( pEqualSign == NULL ) { |
2244 | // brief format | |
2245 | // ------------ | |
2246 | ||
2247 | // first field is mime type | |
223d09f6 | 2248 | for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != wxT('\0'); pc++ ) { |
b13d92d1 VZ |
2249 | strMimeType += *pc; |
2250 | } | |
2251 | ||
2252 | // skip whitespace | |
50920146 | 2253 | while ( wxIsspace(*pc) ) |
b13d92d1 VZ |
2254 | pc++; |
2255 | ||
2256 | // take all the rest of the string | |
2257 | strExtensions = pc; | |
2258 | ||
2259 | // no description... | |
2260 | strDesc.Empty(); | |
2261 | } | |
2262 | else { | |
2263 | // expanded format | |
2264 | // --------------- | |
2265 | ||
2266 | // the string on the left of '=' is the field name | |
2267 | wxString strLHS(pc, pEqualSign - pc); | |
2268 | ||
2269 | // eat whitespace | |
50920146 | 2270 | for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ ) |
678ebfcd | 2271 | ; |
b13d92d1 | 2272 | |
50920146 | 2273 | const wxChar *pEnd; |
223d09f6 | 2274 | if ( *pc == wxT('"') ) { |
b13d92d1 | 2275 | // the string is quoted and ends at the matching quote |
223d09f6 | 2276 | pEnd = wxStrchr(++pc, wxT('"')); |
b13d92d1 VZ |
2277 | if ( pEnd == NULL ) { |
2278 | wxLogWarning(_("Mime.types file %s, line %d: unterminated " | |
2279 | "quoted string."), | |
2280 | strFileName.c_str(), nLine + 1); | |
2281 | } | |
2282 | } | |
2283 | else { | |
8862e11b VZ |
2284 | // unquoted string ends at the first space or at the end of |
2285 | // line | |
2286 | for ( pEnd = pc; *pEnd && !wxIsspace(*pEnd); pEnd++ ) | |
678ebfcd | 2287 | ; |
b13d92d1 VZ |
2288 | } |
2289 | ||
2290 | // now we have the RHS (field value) | |
2291 | wxString strRHS(pc, pEnd - pc); | |
2292 | ||
22b4634c | 2293 | // check what follows this entry |
223d09f6 | 2294 | if ( *pEnd == wxT('"') ) { |
b13d92d1 VZ |
2295 | // skip this quote |
2296 | pEnd++; | |
2297 | } | |
2298 | ||
50920146 | 2299 | for ( pc = pEnd; wxIsspace(*pc); pc++ ) |
678ebfcd | 2300 | ; |
b13d92d1 | 2301 | |
22b4634c VZ |
2302 | // if there is something left, it may be either a '\\' to continue |
2303 | // the line or the next field of the same entry | |
223d09f6 | 2304 | bool entryEnded = *pc == wxT('\0'), |
22b4634c VZ |
2305 | nextFieldOnSameLine = FALSE; |
2306 | if ( !entryEnded ) { | |
223d09f6 | 2307 | nextFieldOnSameLine = ((*pc != wxT('\\')) || (pc[1] != wxT('\0'))); |
b13d92d1 | 2308 | } |
b13d92d1 VZ |
2309 | |
2310 | // now see what we got | |
223d09f6 | 2311 | if ( strLHS == wxT("type") ) { |
b13d92d1 VZ |
2312 | strMimeType = strRHS; |
2313 | } | |
678ebfcd | 2314 | else if ( strLHS.StartsWith(wxT("desc")) ) { |
b13d92d1 VZ |
2315 | strDesc = strRHS; |
2316 | } | |
223d09f6 | 2317 | else if ( strLHS == wxT("exts") ) { |
b13d92d1 VZ |
2318 | strExtensions = strRHS; |
2319 | } | |
70247ce3 | 2320 | else if ( strLHS == _T("icon") ) |
70687b63 | 2321 | { |
a2717c3d VZ |
2322 | // this one is simply ignored: it usually refers to Netscape |
2323 | // built in icons which are useless for us anyhow | |
70687b63 VZ |
2324 | } |
2325 | else if ( !strLHS.StartsWith(_T("x-")) ) | |
2326 | { | |
2327 | // we suppose that all fields starting with "X-" are | |
2328 | // unregistered extensions according to the standard practice, | |
2329 | // but it may be worth telling the user about other junk in | |
2330 | // his mime.types file | |
2331 | wxLogWarning(_("Unknown field in file %s, line %d: '%s'."), | |
2332 | strFileName.c_str(), nLine + 1, strLHS.c_str()); | |
b13d92d1 VZ |
2333 | } |
2334 | ||
2335 | if ( !entryEnded ) { | |
22b4634c VZ |
2336 | if ( !nextFieldOnSameLine ) |
2337 | pc = NULL; | |
2338 | //else: don't reset it | |
2339 | ||
2340 | // as we don't reset strMimeType, the next field in this entry | |
b13d92d1 | 2341 | // will be interpreted correctly. |
22b4634c | 2342 | |
b13d92d1 VZ |
2343 | continue; |
2344 | } | |
2345 | } | |
2346 | ||
a6c65e88 VZ |
2347 | // depending on the format (Mosaic or Netscape) either space or comma |
2348 | // is used to separate the extensions | |
223d09f6 | 2349 | strExtensions.Replace(wxT(","), wxT(" ")); |
a1d8eaf7 VZ |
2350 | |
2351 | // also deal with the leading dot | |
678ebfcd | 2352 | if ( !strExtensions.empty() && strExtensions[0u] == wxT('.') ) |
1b986aef | 2353 | { |
a1d8eaf7 VZ |
2354 | strExtensions.erase(0, 1); |
2355 | } | |
2356 | ||
678ebfcd VZ |
2357 | wxLogTrace(TRACE_MIME, wxT("mime.types: '%s' => '%s' (%s)"), |
2358 | strExtensions.c_str(), | |
2359 | strMimeType.c_str(), | |
2360 | strDesc.c_str()); | |
2b813b73 | 2361 | |
8e124873 | 2362 | AddMimeTypeInfo(strMimeType, strExtensions, strDesc); |
22b4634c VZ |
2363 | |
2364 | // finished with this line | |
2365 | pc = NULL; | |
b13d92d1 VZ |
2366 | } |
2367 | ||
cc385968 | 2368 | return TRUE; |
b13d92d1 VZ |
2369 | } |
2370 | ||
678ebfcd VZ |
2371 | // ---------------------------------------------------------------------------- |
2372 | // UNIX mailcap files parsing | |
2373 | // ---------------------------------------------------------------------------- | |
2374 | ||
2375 | // the data for a single MIME type | |
2376 | struct MailcapLineData | |
2377 | { | |
2378 | // field values | |
2379 | wxString type, | |
2380 | cmdOpen, | |
2381 | test, | |
2382 | icon, | |
2383 | desc; | |
2384 | ||
2385 | wxArrayString verbs, | |
2386 | commands; | |
2387 | ||
2388 | // flags | |
2389 | bool testfailed, | |
2390 | needsterminal, | |
2391 | copiousoutput; | |
2392 | ||
2393 | MailcapLineData() { testfailed = needsterminal = copiousoutput = FALSE; } | |
2394 | }; | |
2395 | ||
2396 | // process a non-standard (i.e. not the first or second one) mailcap field | |
2397 | bool | |
2398 | wxMimeTypesManagerImpl::ProcessOtherMailcapField(MailcapLineData& data, | |
2399 | const wxString& curField) | |
2400 | { | |
2401 | if ( curField.empty() ) | |
2402 | { | |
2403 | // we don't care | |
2404 | return TRUE; | |
2405 | } | |
2406 | ||
2407 | // is this something of the form foo=bar? | |
2408 | const wxChar *pEq = wxStrchr(curField, wxT('=')); | |
2409 | if ( pEq != NULL ) | |
2410 | { | |
2411 | // split "LHS = RHS" in 2 | |
2412 | wxString lhs = curField.BeforeFirst(wxT('=')), | |
2413 | rhs = curField.AfterFirst(wxT('=')); | |
2414 | ||
2415 | lhs.Trim(TRUE); // from right | |
2416 | rhs.Trim(FALSE); // from left | |
2417 | ||
2418 | // it might be quoted | |
2419 | if ( !rhs.empty() && rhs[0u] == wxT('"') && rhs.Last() == wxT('"') ) | |
2420 | { | |
2421 | rhs = rhs.Mid(1, rhs.length() - 2); | |
2422 | } | |
2423 | ||
2424 | // is it a command verb or something else? | |
2425 | if ( lhs == wxT("test") ) | |
2426 | { | |
2427 | if ( wxSystem(rhs) == 0 ) | |
2428 | { | |
2429 | // ok, test passed | |
2430 | wxLogTrace(TRACE_MIME_TEST, | |
2431 | wxT("Test '%s' for mime type '%s' succeeded."), | |
2432 | rhs.c_str(), data.type.c_str()); | |
2433 | ||
2434 | } | |
2435 | else | |
2436 | { | |
2437 | wxLogTrace(TRACE_MIME_TEST, | |
2438 | wxT("Test '%s' for mime type '%s' failed, skipping."), | |
2439 | rhs.c_str(), data.type.c_str()); | |
2440 | ||
2441 | data.testfailed = TRUE; | |
2442 | } | |
2443 | } | |
2444 | else if ( lhs == wxT("desc") ) | |
2445 | { | |
2446 | data.desc = rhs; | |
2447 | } | |
2448 | else if ( lhs == wxT("x11-bitmap") ) | |
2449 | { | |
2450 | data.icon = rhs; | |
2451 | } | |
2452 | else if ( lhs == wxT("notes") ) | |
2453 | { | |
2454 | // ignore | |
2455 | } | |
2456 | else // not a (recognized) special case, must be a verb (e.g. "print") | |
2457 | { | |
2458 | data.verbs.Add(lhs); | |
2459 | data.commands.Add(rhs); | |
2460 | } | |
2461 | } | |
2462 | else // '=' not found | |
2463 | { | |
2464 | // so it must be a simple flag | |
2465 | if ( curField == wxT("needsterminal") ) | |
2466 | { | |
2467 | data.needsterminal = TRUE; | |
2468 | } | |
2469 | else if ( curField == wxT("copiousoutput")) | |
2470 | { | |
2471 | // copiousoutput impies that the viewer is a console program | |
2472 | data.needsterminal = | |
2473 | data.copiousoutput = TRUE; | |
2474 | } | |
2475 | else if ( !IsKnownUnimportantField(curField) ) | |
2476 | { | |
2477 | return FALSE; | |
2478 | } | |
2479 | } | |
2480 | ||
2481 | return TRUE; | |
2482 | } | |
2483 | ||
cc385968 VZ |
2484 | bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, |
2485 | bool fallback) | |
b13d92d1 | 2486 | { |
f6bcfd97 BP |
2487 | wxLogTrace(TRACE_MIME, wxT("--- Parsing mailcap file '%s' ---"), |
2488 | strFileName.c_str()); | |
b13d92d1 VZ |
2489 | |
2490 | wxTextFile file(strFileName); | |
2b5f62a0 VZ |
2491 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
2492 | if ( !file.Open( wxConvUTF8) ) | |
2493 | #else | |
b13d92d1 | 2494 | if ( !file.Open() ) |
2b5f62a0 | 2495 | #endif |
cc385968 | 2496 | return FALSE; |
b13d92d1 | 2497 | |
678ebfcd VZ |
2498 | // indices of MIME types (in m_aTypes) we already found in this file |
2499 | // | |
2500 | // (see the comments near the end of function for the reason we need this) | |
2501 | wxArrayInt aIndicesSeenHere; | |
2502 | ||
2503 | // accumulator for the current field | |
2504 | wxString curField; | |
2505 | curField.reserve(1024); | |
b13d92d1 VZ |
2506 | |
2507 | size_t nLineCount = file.GetLineCount(); | |
678ebfcd VZ |
2508 | for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) |
2509 | { | |
b13d92d1 | 2510 | // now we're at the start of the line |
50920146 | 2511 | const wxChar *pc = file[nLine].c_str(); |
b13d92d1 VZ |
2512 | |
2513 | // skip whitespace | |
50920146 | 2514 | while ( wxIsspace(*pc) ) |
b13d92d1 VZ |
2515 | pc++; |
2516 | ||
2517 | // comment or empty string? | |
223d09f6 | 2518 | if ( *pc == wxT('#') || *pc == wxT('\0') ) |
b13d92d1 VZ |
2519 | continue; |
2520 | ||
2521 | // no, do parse | |
678ebfcd | 2522 | // ------------ |
b13d92d1 VZ |
2523 | |
2524 | // what field are we currently in? The first 2 are fixed and there may | |
678ebfcd VZ |
2525 | // be an arbitrary number of other fields parsed by |
2526 | // ProcessOtherMailcapField() | |
2527 | // | |
2528 | // the first field is the MIME type | |
b13d92d1 VZ |
2529 | enum |
2530 | { | |
2531 | Field_Type, | |
2532 | Field_OpenCmd, | |
2533 | Field_Other | |
2534 | } currentToken = Field_Type; | |
2535 | ||
2536 | // the flags and field values on the current line | |
678ebfcd VZ |
2537 | MailcapLineData data; |
2538 | ||
6e358ae7 | 2539 | bool cont = TRUE; |
678ebfcd VZ |
2540 | while ( cont ) |
2541 | { | |
2542 | switch ( *pc ) | |
2543 | { | |
223d09f6 | 2544 | case wxT('\\'): |
b13d92d1 VZ |
2545 | // interpret the next character literally (notice that |
2546 | // backslash can be used for line continuation) | |
678ebfcd VZ |
2547 | if ( *++pc == wxT('\0') ) |
2548 | { | |
6e358ae7 | 2549 | // fetch the next line if there is one |
678ebfcd VZ |
2550 | if ( nLine == nLineCount - 1 ) |
2551 | { | |
6e358ae7 VZ |
2552 | // something is wrong, bail out |
2553 | cont = FALSE; | |
2554 | ||
2b5f62a0 | 2555 | wxLogDebug(wxT("Mailcap file %s, line %lu: " |
6e358ae7 VZ |
2556 | "'\\' on the end of the last line " |
2557 | "ignored."), | |
2558 | strFileName.c_str(), | |
2b5f62a0 | 2559 | (unsigned long)nLine + 1); |
6e358ae7 | 2560 | } |
678ebfcd VZ |
2561 | else |
2562 | { | |
6e358ae7 VZ |
2563 | // pass to the beginning of the next line |
2564 | pc = file[++nLine].c_str(); | |
2565 | ||
2566 | // skip pc++ at the end of the loop | |
2567 | continue; | |
2568 | } | |
b13d92d1 | 2569 | } |
678ebfcd VZ |
2570 | else |
2571 | { | |
b13d92d1 VZ |
2572 | // just a normal character |
2573 | curField += *pc; | |
2574 | } | |
2575 | break; | |
2576 | ||
223d09f6 | 2577 | case wxT('\0'): |
b13d92d1 VZ |
2578 | cont = FALSE; // end of line reached, exit the loop |
2579 | ||
678ebfcd | 2580 | // fall through to still process this field |
b13d92d1 | 2581 | |
223d09f6 | 2582 | case wxT(';'): |
b13d92d1 VZ |
2583 | // trim whitespaces from both sides |
2584 | curField.Trim(TRUE).Trim(FALSE); | |
2585 | ||
678ebfcd VZ |
2586 | switch ( currentToken ) |
2587 | { | |
b13d92d1 | 2588 | case Field_Type: |
678ebfcd VZ |
2589 | data.type = curField.Lower(); |
2590 | if ( data.type.empty() ) | |
2591 | { | |
6e358ae7 VZ |
2592 | // I don't think that this is a valid mailcap |
2593 | // entry, but try to interpret it somehow | |
678ebfcd | 2594 | data.type = _T('*'); |
6e358ae7 VZ |
2595 | } |
2596 | ||
678ebfcd VZ |
2597 | if ( data.type.Find(wxT('/')) == wxNOT_FOUND ) |
2598 | { | |
b13d92d1 | 2599 | // we interpret "type" as "type/*" |
678ebfcd | 2600 | data.type += wxT("/*"); |
b13d92d1 VZ |
2601 | } |
2602 | ||
2603 | currentToken = Field_OpenCmd; | |
2604 | break; | |
2605 | ||
2606 | case Field_OpenCmd: | |
678ebfcd | 2607 | data.cmdOpen = curField; |
b13d92d1 VZ |
2608 | |
2609 | currentToken = Field_Other; | |
2610 | break; | |
2611 | ||
2612 | case Field_Other: | |
678ebfcd VZ |
2613 | if ( !ProcessOtherMailcapField(data, curField) ) |
2614 | { | |
2615 | // don't flood the user with error messages if | |
2616 | // we don't understand something in his | |
2617 | // mailcap, but give them in debug mode because | |
2618 | // this might be useful for the programmer | |
2619 | wxLogDebug | |
2620 | ( | |
2b5f62a0 | 2621 | wxT("Mailcap file %s, line %lu: " |
678ebfcd VZ |
2622 | "unknown field '%s' for the " |
2623 | "MIME type '%s' ignored."), | |
2624 | strFileName.c_str(), | |
2b5f62a0 | 2625 | (unsigned long)nLine + 1, |
678ebfcd VZ |
2626 | curField.c_str(), |
2627 | data.type.c_str() | |
2628 | ); | |
2629 | } | |
2630 | else if ( data.testfailed ) | |
2631 | { | |
2632 | // skip this entry entirely | |
2633 | cont = FALSE; | |
b13d92d1 VZ |
2634 | } |
2635 | ||
2636 | // it already has this value | |
2637 | //currentToken = Field_Other; | |
2638 | break; | |
2639 | ||
2640 | default: | |
223d09f6 | 2641 | wxFAIL_MSG(wxT("unknown field type in mailcap")); |
b13d92d1 VZ |
2642 | } |
2643 | ||
2644 | // next token starts immediately after ';' | |
2645 | curField.Empty(); | |
2646 | break; | |
2647 | ||
2648 | default: | |
2649 | curField += *pc; | |
2650 | } | |
6e358ae7 VZ |
2651 | |
2652 | // continue in the same line | |
2653 | pc++; | |
b13d92d1 VZ |
2654 | } |
2655 | ||
678ebfcd VZ |
2656 | // we read the entire entry, check what have we got |
2657 | // ------------------------------------------------ | |
2658 | ||
b13d92d1 | 2659 | // check that we really read something reasonable |
678ebfcd VZ |
2660 | if ( currentToken < Field_Other ) |
2661 | { | |
b13d92d1 VZ |
2662 | wxLogWarning(_("Mailcap file %s, line %d: incomplete entry " |
2663 | "ignored."), | |
2664 | strFileName.c_str(), nLine + 1); | |
678ebfcd VZ |
2665 | |
2666 | continue; | |
b13d92d1 | 2667 | } |
e1e9ea40 | 2668 | |
678ebfcd VZ |
2669 | // if the test command failed, it's as if the entry were not there at |
2670 | // all | |
2671 | if ( data.testfailed ) | |
2672 | { | |
2673 | continue; | |
2674 | } | |
2b813b73 | 2675 | |
678ebfcd VZ |
2676 | // support for flags: |
2677 | // 1. create an xterm for 'needsterminal' | |
2678 | // 2. append "| $PAGER" for 'copiousoutput' | |
2679 | // | |
2680 | // Note that the RFC says that having both needsterminal and | |
2681 | // copiousoutput is probably a mistake, so it seems that running | |
2682 | // programs with copiousoutput inside an xterm as it is done now | |
2683 | // is a bad idea (FIXME) | |
2684 | if ( data.copiousoutput ) | |
2685 | { | |
2686 | const wxChar *p = wxGetenv(_T("PAGER")); | |
2687 | data.cmdOpen << _T(" | ") << (p ? p : _T("more")); | |
2688 | } | |
b13d92d1 | 2689 | |
678ebfcd VZ |
2690 | if ( data.needsterminal ) |
2691 | { | |
2b5f62a0 VZ |
2692 | data.cmdOpen = wxString::Format(_T("xterm -e sh -c '%s'"), |
2693 | data.cmdOpen.c_str()); | |
678ebfcd | 2694 | } |
b13d92d1 | 2695 | |
678ebfcd VZ |
2696 | if ( !data.cmdOpen.empty() ) |
2697 | { | |
2698 | data.verbs.Insert(_T("open"), 0); | |
2699 | data.commands.Insert(data.cmdOpen, 0); | |
2700 | } | |
2b813b73 | 2701 | |
678ebfcd VZ |
2702 | // we have to decide whether the new entry should replace any entries |
2703 | // for the same MIME type we had previously found or not | |
2704 | bool overwrite; | |
2705 | ||
2706 | // the fall back entries have the lowest priority, by definition | |
2707 | if ( fallback ) | |
2708 | { | |
2709 | overwrite = FALSE; | |
2710 | } | |
2711 | else | |
2712 | { | |
2713 | // have we seen this one before? | |
2714 | int nIndex = m_aTypes.Index(data.type); | |
2715 | ||
2716 | // and if we have, was it in this file? | |
2717 | overwrite = nIndex == wxNOT_FOUND || | |
2718 | aIndicesSeenHere.Index(nIndex) != wxNOT_FOUND; | |
b13d92d1 VZ |
2719 | } |
2720 | ||
678ebfcd VZ |
2721 | wxLogTrace(TRACE_MIME, _T("mailcap %s: %s [%s]"), |
2722 | data.type.c_str(), data.cmdOpen.c_str(), | |
2723 | overwrite ? _T("replace") : _T("add")); | |
2724 | ||
2725 | int n = AddToMimeData | |
2726 | ( | |
2727 | data.type, | |
2728 | data.icon, | |
2729 | new wxMimeTypeCommands(data.verbs, data.commands), | |
2730 | wxArrayString() /* extensions */, | |
2731 | data.desc, | |
2732 | overwrite | |
2733 | ); | |
2734 | ||
2735 | if ( overwrite ) | |
2736 | { | |
2737 | aIndicesSeenHere.Add(n); | |
2738 | } | |
b13d92d1 | 2739 | } |
cc385968 VZ |
2740 | |
2741 | return TRUE; | |
b13d92d1 VZ |
2742 | } |
2743 | ||
696e1ea0 | 2744 | size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) |
1b986aef | 2745 | { |
a6c65e88 VZ |
2746 | InitIfNeeded(); |
2747 | ||
54acce90 VZ |
2748 | mimetypes.Empty(); |
2749 | ||
2750 | wxString type; | |
2751 | size_t count = m_aTypes.GetCount(); | |
2752 | for ( size_t n = 0; n < count; n++ ) | |
2753 | { | |
2754 | // don't return template types from here (i.e. anything containg '*') | |
2755 | type = m_aTypes[n]; | |
2756 | if ( type.Find(_T('*')) == wxNOT_FOUND ) | |
2757 | { | |
2758 | mimetypes.Add(type); | |
2759 | } | |
2760 | } | |
1b986aef | 2761 | |
54acce90 | 2762 | return mimetypes.GetCount(); |
1b986aef VZ |
2763 | } |
2764 | ||
a6c65e88 VZ |
2765 | // ---------------------------------------------------------------------------- |
2766 | // writing to MIME type files | |
2767 | // ---------------------------------------------------------------------------- | |
2768 | ||
2b813b73 | 2769 | bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft) |
a6c65e88 | 2770 | { |
2b813b73 VZ |
2771 | wxArrayString sMimeTypes; |
2772 | ft->GetMimeTypes (sMimeTypes); | |
a6c65e88 | 2773 | |
2b813b73 VZ |
2774 | wxString sMime; |
2775 | size_t i; | |
2776 | for (i = 0; i < sMimeTypes.GetCount(); i ++) | |
2777 | { | |
2778 | sMime = sMimeTypes.Item(i); | |
2779 | int nIndex = m_aTypes.Index (sMime); | |
2780 | if ( nIndex == wxNOT_FOUND) | |
2781 | { | |
2782 | // error if we get here ?? | |
2783 | return FALSE; | |
2784 | } | |
2785 | else | |
2786 | { | |
2787 | WriteMimeInfo(nIndex, TRUE ); | |
e9d9f136 VZ |
2788 | m_aTypes.RemoveAt(nIndex); |
2789 | m_aEntries.RemoveAt(nIndex); | |
2790 | m_aExtensions.RemoveAt(nIndex); | |
2791 | m_aDescriptions.RemoveAt(nIndex); | |
2792 | m_aIcons.RemoveAt(nIndex); | |
2b813b73 VZ |
2793 | } |
2794 | } | |
2795 | // check data integrity | |
2796 | wxASSERT( m_aTypes.Count() == m_aEntries.Count() && | |
2797 | m_aTypes.Count() == m_aExtensions.Count() && | |
2798 | m_aTypes.Count() == m_aIcons.Count() && | |
2799 | m_aTypes.Count() == m_aDescriptions.Count() ); | |
2800 | ||
2801 | return TRUE; | |
a6c65e88 VZ |
2802 | } |
2803 | ||
f6bcfd97 BP |
2804 | // ---------------------------------------------------------------------------- |
2805 | // private functions | |
2806 | // ---------------------------------------------------------------------------- | |
2807 | ||
2808 | static bool IsKnownUnimportantField(const wxString& fieldAll) | |
2809 | { | |
2810 | static const wxChar *knownFields[] = | |
2811 | { | |
2812 | _T("x-mozilla-flags"), | |
2813 | _T("nametemplate"), | |
2814 | _T("textualnewlines"), | |
2815 | }; | |
2816 | ||
2817 | wxString field = fieldAll.BeforeFirst(_T('=')); | |
2818 | for ( size_t n = 0; n < WXSIZEOF(knownFields); n++ ) | |
2819 | { | |
2820 | if ( field.CmpNoCase(knownFields[n]) == 0 ) | |
2821 | return TRUE; | |
2822 | } | |
2823 | ||
2824 | return FALSE; | |
2825 | } | |
2826 | ||
8e124873 | 2827 | #endif |
b79e32cc | 2828 | // wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE |
b13d92d1 | 2829 |