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