xref: /haiku/headers/private/storage/mime/Database.h (revision ee2974dadb9c6c99b8ad9b393f992fae42e4bc39)
1 /*
2  * Copyright 2002-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Tyler Dauwalder
7  */
8 #ifndef _MIME_DATABASE_H
9 #define _MIME_DATABASE_H
10 
11 #include <map>
12 #include <set>
13 #include <string>
14 
15 #include <List.h>
16 #include <Locker.h>
17 #include <Mime.h>
18 #include <Messenger.h>
19 #include <StorageDefs.h>
20 
21 #include <mime/AssociatedTypes.h>
22 #include <mime/InstalledTypes.h>
23 #include <mime/SnifferRules.h>
24 #include <mime/SupportingApps.h>
25 
26 
27 class BNode;
28 class BBitmap;
29 class BMessage;
30 class BString;
31 
32 struct entry_ref;
33 
34 namespace BPrivate {
35 namespace Storage {
36 namespace Mime {
37 
38 
39 class DatabaseLocation;
40 class MimeSniffer;
41 
42 
43 // types of mime update functions that may be run asynchronously
44 typedef enum {
45 	B_REG_UPDATE_MIME_INFO,
46 	B_REG_CREATE_APP_META_MIME,
47 } mime_update_function;
48 
49 class Database {
50 	public:
51 		class NotificationListener;
52 
53 	public:
54 		Database(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer,
55 			NotificationListener* notificationListener);
56 		~Database();
57 
58 		status_t InitCheck() const;
59 
Location()60 		DatabaseLocation* Location() const	{ return fLocation; }
61 
62 		// Type management
63 		status_t Install(const char *type);
64 		status_t Delete(const char *type);
65 
66 		// Set()
67 		status_t SetAppHint(const char *type, const entry_ref *ref);
68 		status_t SetAttrInfo(const char *type, const BMessage *info);
69 		status_t SetShortDescription(const char *type, const char *description);
70 		status_t SetLongDescription(const char *type, const char *description);
71 		status_t SetFileExtensions(const char *type, const BMessage *extensions);
72 		status_t SetIcon(const char* type, const BBitmap* icon,
73 					icon_size which);
74 		status_t SetIcon(const char *type, const void *data, size_t dataSize,
75 					icon_size which);
76 		status_t SetIcon(const char *type, const void *data, size_t dataSize);
77 		status_t SetIconForType(const char* type, const char* fileType,
78 					const BBitmap* icon, icon_size which);
79 		status_t SetIconForType(const char *type, const char *fileType,
80 					const void *data, size_t dataSize, icon_size which);
81 		status_t SetIconForType(const char *type, const char *fileType,
82 					const void *data, size_t dataSize);
83 		status_t SetPreferredApp(const char *type, const char *signature,
84 					app_verb verb = B_OPEN);
85 		status_t SetSnifferRule(const char *type, const char *rule);
86 		status_t SetSupportedTypes(const char *type, const BMessage *types, bool fullSync);
87 
88 		// Non-atomic Get()
89 		status_t GetInstalledSupertypes(BMessage *super_types);
90 		status_t GetInstalledTypes(BMessage *types);
91 		status_t GetInstalledTypes(const char *super_type,
92 					BMessage *subtypes);
93 		status_t GetSupportingApps(const char *type, BMessage *signatures);
94 		status_t GetAssociatedTypes(const char *extension, BMessage *types);
95 
96 		// Sniffer
97 		status_t GuessMimeType(const entry_ref *file, BString *result);
98 		status_t GuessMimeType(const void *buffer, int32 length, BString *result);
99 		status_t GuessMimeType(const char *filename, BString *result);
100 
101 		// Monitor
102 		status_t StartWatching(BMessenger target);
103 		status_t StopWatching(BMessenger target);
104 
105 		// Delete()
106 		status_t DeleteAppHint(const char *type);
107 		status_t DeleteAttrInfo(const char *type);
108 		status_t DeleteShortDescription(const char *type);
109 		status_t DeleteLongDescription(const char *type);
110 		status_t DeleteFileExtensions(const char *type);
111 		status_t DeleteIcon(const char *type, icon_size size);
112 		status_t DeleteIcon(const char *type);
113 		status_t DeleteIconForType(const char *type, const char *fileType,
114 					icon_size which);
115 		status_t DeleteIconForType(const char *type, const char *fileType);
116 		status_t DeletePreferredApp(const char *type, app_verb verb = B_OPEN);
117 		status_t DeleteSnifferRule(const char *type);
118 		status_t DeleteSupportedTypes(const char *type, bool fullSync);
119 
120 		// deferred notifications
121 		void	DeferInstallNotification(const char* type);
122 		void	UndeferInstallNotification(const char* type);
123 
124 	private:
125 		struct DeferredInstallNotification {
126 			char	type[B_MIME_TYPE_LENGTH];
127 			bool	notify;
128 		};
129 
130 		status_t _SetStringValue(const char *type, int32 what,
131 					const char* attribute, type_code attributeType,
132 					size_t maxLength, const char *value);
133 
134 		// Functions to send monitor notifications
135 		status_t _SendInstallNotification(const char *type);
136 		status_t _SendDeleteNotification(const char *type);
137 		status_t _SendMonitorUpdate(int32 which, const char *type,
138 					const char *extraType, bool largeIcon, int32 action);
139 		status_t _SendMonitorUpdate(int32 which, const char *type,
140 					const char *extraType, int32 action);
141 		status_t _SendMonitorUpdate(int32 which, const char *type,
142 					bool largeIcon, int32 action);
143 		status_t _SendMonitorUpdate(int32 which, const char *type,
144 					int32 action);
145 		status_t _SendMonitorUpdate(BMessage &msg);
146 
147 		DeferredInstallNotification* _FindDeferredInstallNotification(
148 			const char* type, bool remove = false);
149 		bool _CheckDeferredInstallNotification(int32 which, const char* type);
150 
151 	private:
152 		status_t fStatus;
153 		DatabaseLocation* fLocation;
154 		NotificationListener* fNotificationListener;
155 		std::set<BMessenger> fMonitorMessengers;
156 		AssociatedTypes fAssociatedTypes;
157 		InstalledTypes fInstalledTypes;
158 		SnifferRules fSnifferRules;
159 		SupportingApps fSupportingApps;
160 
161 		BLocker	fDeferredInstallNotificationsLocker;
162 		BList	fDeferredInstallNotifications;
163 };
164 
165 class InstallNotificationDeferrer {
166 	public:
InstallNotificationDeferrer(Database * database,const char * type)167 		InstallNotificationDeferrer(Database* database, const char* type)
168 			:
169 			fDatabase(database),
170 			fType(type)
171 		{
172 			if (fDatabase != NULL && fType != NULL)
173 				fDatabase->DeferInstallNotification(fType);
174 		}
175 
~InstallNotificationDeferrer()176 		~InstallNotificationDeferrer()
177 		{
178 			if (fDatabase != NULL && fType != NULL)
179 				fDatabase->UndeferInstallNotification(fType);
180 		}
181 
182 	private:
183 		Database*	fDatabase;
184 		const char*	fType;
185 };
186 
187 
188 class Database::NotificationListener {
189 public:
190 	virtual						~NotificationListener();
191 
192 	virtual	status_t			Notify(BMessage* message,
193 									const BMessenger& target) = 0;
194 };
195 
196 
197 } // namespace Mime
198 } // namespace Storage
199 } // namespace BPrivate
200 
201 #endif	// _MIME_DATABASE_H
202