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