1/*
2 * Copyright 2002-2006 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _MIME_TYPE_H
6#define _MIME_TYPE_H
7
8
9#include <SupportDefs.h>
10#include <StorageDefs.h>
11
12#include <Messenger.h>
13#include <Mime.h>
14#include <File.h>
15#include <Entry.h>
16
17
18class BBitmap;
19class BResources;
20class BAppFileInfo;
21class BMessenger;
22
23namespace BPrivate {
24	class MimeDatabase;
25	namespace Storage {
26		namespace Mime {
27			class CreateAppMetaMimeThread;
28		}
29	}
30}
31
32enum app_verb {
33	B_OPEN
34};
35
36extern const char* B_APP_MIME_TYPE;		// platform dependent
37extern const char* B_PEF_APP_MIME_TYPE;	// "application/x-be-executable"
38extern const char* B_PE_APP_MIME_TYPE;	// "application/x-vnd.be-peexecutable"
39extern const char* B_ELF_APP_MIME_TYPE;	// "application/x-vnd.be-elfexecutable"
40extern const char* B_RESOURCE_MIME_TYPE;// "application/x-be-resource"
41extern const char* B_FILE_MIME_TYPE;	// "application/octet-stream"
42
43/* ------------------------------------------------------------- */
44
45// MIME Monitor BMessage::what value
46enum {
47	B_META_MIME_CHANGED = 'MMCH'
48};
49
50// MIME Monitor "be:which" values
51enum {
52	B_ICON_CHANGED					= 0x00000001,
53	B_PREFERRED_APP_CHANGED			= 0x00000002,
54	B_ATTR_INFO_CHANGED				= 0x00000004,
55	B_FILE_EXTENSIONS_CHANGED		= 0x00000008,
56	B_SHORT_DESCRIPTION_CHANGED		= 0x00000010,
57	B_LONG_DESCRIPTION_CHANGED		= 0x00000020,
58	B_ICON_FOR_TYPE_CHANGED			= 0x00000040,
59	B_APP_HINT_CHANGED				= 0x00000080,
60	B_MIME_TYPE_CREATED				= 0x00000100,
61	B_MIME_TYPE_DELETED				= 0x00000200,
62	B_SNIFFER_RULE_CHANGED			= 0x00000400,
63	B_SUPPORTED_TYPES_CHANGED		= 0x00000800,
64
65	B_EVERYTHING_CHANGED			= (int)0xFFFFFFFF
66};
67
68// MIME Monitor "be:action" values
69enum {
70	B_META_MIME_MODIFIED	= 'MMMD',
71	B_META_MIME_DELETED 	= 'MMDL',
72};
73
74class BMimeType	{
75public:
76	BMimeType();
77	BMimeType(const char* mimeType);
78	virtual ~BMimeType();
79
80	status_t SetTo(const char* mimeType);
81	void Unset();
82	status_t InitCheck() const;
83
84	/* these functions simply perform string manipulations*/
85	const char* Type() const;
86	bool IsValid() const;
87	bool IsSupertypeOnly() const;
88	status_t GetSupertype(BMimeType* supertype) const;
89
90	bool operator==(const BMimeType &type) const;
91	bool operator==(const char* type) const;
92
93	bool Contains(const BMimeType* type) const;
94
95	/* These functions are for managing data in the meta mime file */
96	status_t Install();
97	status_t Delete();
98	bool IsInstalled() const;
99	status_t GetIcon(BBitmap* icon, icon_size size) const;
100	status_t GetIcon(uint8** _data, size_t* _size) const;
101	status_t GetPreferredApp(char* signature, app_verb verb = B_OPEN) const;
102	status_t GetAttrInfo(BMessage* info) const;
103	status_t GetFileExtensions(BMessage* extensions) const;
104	status_t GetShortDescription(char* description) const;
105	status_t GetLongDescription(char* description) const;
106	status_t GetSupportingApps(BMessage* signatures) const;
107
108	status_t SetIcon(const BBitmap* icon, icon_size size);
109	status_t SetIcon(const uint8* data, size_t size);
110	status_t SetPreferredApp(const char* signature, app_verb verb = B_OPEN);
111	status_t SetAttrInfo(const BMessage* info);
112	status_t SetFileExtensions(const BMessage* extensions);
113	status_t SetShortDescription(const char* description);
114	status_t SetLongDescription(const char* description);
115
116	static status_t GetInstalledSupertypes(BMessage* supertypes);
117	static status_t GetInstalledTypes(BMessage* types);
118	static status_t GetInstalledTypes(const char* supertype,
119						BMessage* subtypes);
120	static status_t GetWildcardApps(BMessage* wildcardApps);
121	static bool IsValid(const char* mimeType);
122
123	status_t GetAppHint(entry_ref* ref) const;
124	status_t SetAppHint(const entry_ref* ref);
125
126	/* for application signatures only. */
127	status_t GetIconForType(const char* type, BBitmap* icon,
128				icon_size which) const;
129	status_t GetIconForType(const char* type, uint8** _data,
130				size_t* _size) const;
131	status_t SetIconForType(const char* type, const BBitmap* icon,
132				icon_size which);
133	status_t SetIconForType(const char* type, const uint8* data,
134						size_t size);
135
136	/* sniffer rule manipulation */
137	status_t GetSnifferRule(BString* result) const;
138	status_t SetSnifferRule(const char*);
139	static status_t CheckSnifferRule(const char* rule, BString* parseError);
140
141	/* calls to ask the sniffer to identify the MIME type of a file or data in
142	   memory */
143	static status_t GuessMimeType(const entry_ref* file, BMimeType* type);
144	static status_t GuessMimeType(const void* buffer, int32 length,
145						BMimeType* type);
146	static status_t GuessMimeType(const char* filename, BMimeType* type);
147
148	static status_t StartWatching(BMessenger target);
149	static status_t StopWatching(BMessenger target);
150
151	/* Deprecated. Use SetTo() instead. */
152	status_t SetType(const char* mimeType);
153
154private:
155	BMimeType(const char* mimeType, const char* mimePath);
156		// if mimePath is NULL, defaults to "/boot/home/config/settings/beos_mime/"
157
158	friend class MimeTypeTest;
159		// for testing only
160
161	friend class BAppFileInfo;
162
163	virtual void _ReservedMimeType1();
164	virtual void _ReservedMimeType2();
165	virtual void _ReservedMimeType3();
166
167	BMimeType& operator=(const BMimeType& source);
168	BMimeType(const BMimeType& source);
169
170	status_t GetSupportedTypes(BMessage* types);
171	status_t SetSupportedTypes(const BMessage* types, bool fullSync = true);
172
173	static status_t GetAssociatedTypes(const char* extension, BMessage* types);
174
175private:
176	char*		fType;
177	BFile*		fMeta;
178	void*		_unused;
179	entry_ref	fRef;
180	status_t	fCStatus;
181	uint32		_reserved[4];
182};
183
184
185#endif	// _MIME_TYPE_H
186