]> git.saurik.com Git - wxWidgets.git/blob - include/wx/mimetype.h
Solved problem with wxhInstance name being mangled by MSVC; wxGetInstance is now...
[wxWidgets.git] / include / wx / mimetype.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/mimetype.h
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>
9 // Licence: wxWindows license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _MIMETYPE_H
13 #define _MIMETYPE_H
14
15 // fwd decls
16 class wxIcon;
17 class wxFileTypeImpl;
18 class wxMimeTypesManagerImpl;
19
20 #include "wx/defs.h"
21
22 #if wxUSE_FILE
23
24 // the things we really need
25 #include "wx/string.h"
26
27 // This class holds information about a given "file type". File type is the
28 // same as MIME type under Unix, but under Windows it corresponds more to an
29 // extension than to MIME type (in fact, several extensions may correspond to a
30 // file type). This object may be created in many different ways and depending
31 // on how it was created some fields may be unknown so the return value of all
32 // the accessors *must* be checked!
33 class WXDLLEXPORT wxFileType
34 {
35 #ifdef __MWERKS__
36 friend class wxMimeTypesManagerImpl; // it has access to m_impl
37 #else
38 friend wxMimeTypesManagerImpl; // it has access to m_impl
39 #endif
40
41 public:
42 // An object of this class must be passed to Get{Open|Print}Command. The
43 // default implementation is trivial and doesn't know anything at all about
44 // parameters, only filename and MIME type are used (so it's probably ok for
45 // Windows where %{param} is not used anyhow)
46 class MessageParameters
47 {
48 public:
49 // ctors
50 MessageParameters() { }
51 MessageParameters(const wxString& filename, const wxString& mimetype)
52 : m_filename(filename), m_mimetype(mimetype) { }
53
54 // accessors (called by GetOpenCommand)
55 // filename
56 const wxString& GetFileName() const { return m_filename; }
57 // mime type
58 const wxString& GetMimeType() const { return m_mimetype; }
59
60 // override this function in derived class
61 virtual wxString GetParamValue(const wxString& WXUNUSED(paramName)) const
62 { return ""; }
63
64 // virtual dtor as in any base class
65 virtual ~MessageParameters() { }
66
67 protected:
68 wxString m_filename, m_mimetype;
69 };
70
71 // accessors: all of them return true if the corresponding information
72 // could be retrieved/found, false otherwise (and in this case all [out]
73 // parameters are unchanged)
74 // return the MIME type for this file type
75 bool GetMimeType(wxString *mimeType) const;
76 // fill passed in array with all extensions associated with this file
77 // type
78 bool GetExtensions(wxArrayString& extensions);
79 // get the icon corresponding to this file type
80 bool GetIcon(wxIcon *icon) const;
81 // get a brief file type description ("*.txt" => "text document")
82 bool GetDescription(wxString *desc) const;
83
84 // get the command to be used to open/print the given file.
85 // get the command to execute the file of given type
86 bool GetOpenCommand(wxString *openCmd,
87 const MessageParameters& params) const;
88 // get the command to print the file of given type
89 bool GetPrintCommand(wxString *printCmd,
90 const MessageParameters& params) const;
91
92 // operations
93 // expand a string in the format of GetOpenCommand (which may contain
94 // '%s' and '%t' format specificators for the file name and mime type
95 // and %{param} constructions).
96 static wxString ExpandCommand(const wxString& command,
97 const MessageParameters& params);
98
99 // dtor (not virtual, shouldn't be derived from)
100 ~wxFileType();
101
102 private:
103 // default ctor is private because the user code never creates us
104 wxFileType();
105
106 // no copy ctor/assignment operator
107 wxFileType(const wxFileType&);
108 wxFileType& operator=(const wxFileType&);
109
110 wxFileTypeImpl *m_impl;
111 };
112
113 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
114 // just as the container for the wxFileType data.
115 class WXDLLEXPORT wxFileTypeInfo
116 {
117 public:
118 // ctors
119 // a normal item
120 wxFileTypeInfo(const char *mimeType,
121 const char *openCmd,
122 const char *printCmd,
123 const char *desc,
124 // the other parameters form a NULL terminated list of
125 // extensions
126 ...);
127
128 // invalid item - use this to terminate the array passed to
129 // wxMimeTypesManager::AddFallbacks
130 wxFileTypeInfo() { }
131
132 bool IsValid() const { return !m_mimeType.IsEmpty(); }
133
134 // accessors
135 // get the MIME type
136 const wxString& GetMimeType() const { return m_mimeType; }
137 // get the open command
138 const wxString& GetOpenCommand() const { return m_openCmd; }
139 // get the print command
140 const wxString& GetPrintCommand() const { return m_printCmd; }
141 // get the description
142 const wxString& GetDescription() const { return m_desc; }
143 // get the array of all extensions
144 const wxArrayString& GetExtensions() const { return m_exts; }
145
146 private:
147 wxString m_mimeType, // the MIME type in "type/subtype" form
148 m_openCmd, // command to use for opening the file (%s allowed)
149 m_printCmd, // command to use for printing the file (%s allowed)
150 m_desc; // a free form description of this file type
151
152 wxArrayString m_exts; // the extensions which are mapped on this filetype
153 };
154
155 // This class accesses the information about all known MIME types and allows
156 // the application to retrieve information (including how to handle data of
157 // given type) about them.
158 //
159 // NB: currently it doesn't support modifying MIME database (read-only access).
160 class WXDLLEXPORT wxMimeTypesManager
161 {
162 public:
163 // static helper functions
164 // -----------------------
165
166 // check if the given MIME type is the same as the other one: the second
167 // argument may contain wildcards ('*'), but not the first. If the
168 // types are equal or if the mimeType matches wildcard the function
169 // returns TRUE, otherwise it returns FALSE
170 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
171
172 // ctor
173 wxMimeTypesManager();
174
175 // Database lookup: all functions return a pointer to wxFileType object
176 // whose methods may be used to query it for the information you're
177 // interested in. If the return value is !NULL, caller is responsible for
178 // deleting it.
179 // get file type from file extension
180 wxFileType *GetFileTypeFromExtension(const wxString& ext);
181 // get file type from MIME type (in format <category>/<format>)
182 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
183
184 // other operations: return TRUE if there were no errors or FALSE if there
185 // were some unreckognized entries (the good entries are always read anyhow)
186 // read in additional file (the standard ones are read automatically)
187 // in mailcap format (see mimetype.cpp for description)
188 //
189 // 'fallback' parameter may be set to TRUE to avoid overriding the
190 // settings from other, previously parsed, files by this one: normally,
191 // the files read most recently would override the older files, but with
192 // fallback == TRUE this won't happen
193 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
194 // read in additional file in mime.types format
195 bool ReadMimeTypes(const wxString& filename);
196
197 // these functions can be used to provide default values for some of the
198 // MIME types inside the program itself (you may also use
199 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
200 // achieve the same goal, but this requires having this info in a file).
201 //
202 // It isn't possible (currently) to provide fallback icons using this
203 // function.
204 //
205 // The filetypes array should be terminated by a NULL entry
206 void AddFallbacks(const wxFileTypeInfo *filetypes);
207
208 // dtor (not virtual, shouldn't be derived from)
209 ~wxMimeTypesManager();
210
211 private:
212 // no copy ctor/assignment operator
213 wxMimeTypesManager(const wxMimeTypesManager&);
214 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
215
216 wxMimeTypesManagerImpl *m_impl;
217 };
218
219 #endif
220 // wxUSE_FILE
221
222 #endif
223 //_MIMETYPE_H
224
225 /* vi: set cin tw=80 ts=4 sw=4: */