xref: /haiku/headers/private/storage/mime/Database.h (revision b94857b322093c296230a18d47f6a98a2896b062)
19cda9c99SIngo Weinhold /*
29cda9c99SIngo Weinhold  * Copyright 2002-2006, Haiku.
39cda9c99SIngo Weinhold  * Distributed under the terms of the MIT License.
49cda9c99SIngo Weinhold  *
59cda9c99SIngo Weinhold  * Authors:
69cda9c99SIngo Weinhold  *		Tyler Dauwalder
79cda9c99SIngo Weinhold  */
89cda9c99SIngo Weinhold #ifndef _MIME_DATABASE_H
99cda9c99SIngo Weinhold #define _MIME_DATABASE_H
109cda9c99SIngo Weinhold 
119cda9c99SIngo Weinhold #include <map>
129cda9c99SIngo Weinhold #include <set>
139cda9c99SIngo Weinhold #include <string>
149cda9c99SIngo Weinhold 
159cda9c99SIngo Weinhold #include <List.h>
169cda9c99SIngo Weinhold #include <Locker.h>
179cda9c99SIngo Weinhold #include <Mime.h>
189cda9c99SIngo Weinhold #include <Messenger.h>
199cda9c99SIngo Weinhold #include <StorageDefs.h>
209cda9c99SIngo Weinhold 
219cda9c99SIngo Weinhold #include <mime/AssociatedTypes.h>
229cda9c99SIngo Weinhold #include <mime/InstalledTypes.h>
239cda9c99SIngo Weinhold #include <mime/SnifferRules.h>
249cda9c99SIngo Weinhold #include <mime/SupportingApps.h>
259cda9c99SIngo Weinhold 
269cda9c99SIngo Weinhold 
279cda9c99SIngo Weinhold class BNode;
289cda9c99SIngo Weinhold class BBitmap;
299cda9c99SIngo Weinhold class BMessage;
309cda9c99SIngo Weinhold class BString;
319cda9c99SIngo Weinhold 
329cda9c99SIngo Weinhold struct entry_ref;
339cda9c99SIngo Weinhold 
349cda9c99SIngo Weinhold namespace BPrivate {
359cda9c99SIngo Weinhold namespace Storage {
369cda9c99SIngo Weinhold namespace Mime {
379cda9c99SIngo Weinhold 
38*b94857b3SIngo Weinhold 
39*b94857b3SIngo Weinhold class DatabaseLocation;
409cda9c99SIngo Weinhold class MimeSniffer;
419cda9c99SIngo Weinhold 
42*b94857b3SIngo Weinhold 
439cda9c99SIngo Weinhold // types of mime update functions that may be run asynchronously
449cda9c99SIngo Weinhold typedef enum {
459cda9c99SIngo Weinhold 	B_REG_UPDATE_MIME_INFO,
469cda9c99SIngo Weinhold 	B_REG_CREATE_APP_META_MIME,
479cda9c99SIngo Weinhold } mime_update_function;
489cda9c99SIngo Weinhold 
499cda9c99SIngo Weinhold class Database {
509cda9c99SIngo Weinhold 	public:
519cda9c99SIngo Weinhold 		class NotificationListener;
529cda9c99SIngo Weinhold 
539cda9c99SIngo Weinhold 	public:
54*b94857b3SIngo Weinhold 		Database(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer,
559cda9c99SIngo Weinhold 			NotificationListener* notificationListener);
569cda9c99SIngo Weinhold 		~Database();
579cda9c99SIngo Weinhold 
589cda9c99SIngo Weinhold 		status_t InitCheck() const;
599cda9c99SIngo Weinhold 
60*b94857b3SIngo Weinhold 		DatabaseLocation* Location() const	{ return fLocation; }
61*b94857b3SIngo Weinhold 
629cda9c99SIngo Weinhold 		// Type management
639cda9c99SIngo Weinhold 		status_t Install(const char *type);
649cda9c99SIngo Weinhold 		status_t Delete(const char *type);
659cda9c99SIngo Weinhold 
669cda9c99SIngo Weinhold 		// Set()
679cda9c99SIngo Weinhold 		status_t SetAppHint(const char *type, const entry_ref *ref);
689cda9c99SIngo Weinhold 		status_t SetAttrInfo(const char *type, const BMessage *info);
699cda9c99SIngo Weinhold 		status_t SetShortDescription(const char *type, const char *description);
709cda9c99SIngo Weinhold 		status_t SetLongDescription(const char *type, const char *description);
719cda9c99SIngo Weinhold 		status_t SetFileExtensions(const char *type, const BMessage *extensions);
729cda9c99SIngo Weinhold 		status_t SetIcon(const char *type, const void *data, size_t dataSize,
739cda9c99SIngo Weinhold 					icon_size which);
749cda9c99SIngo Weinhold 		status_t SetIcon(const char *type, const void *data, size_t dataSize);
759cda9c99SIngo Weinhold 		status_t SetIconForType(const char *type, const char *fileType,
769cda9c99SIngo Weinhold 					const void *data, size_t dataSize, icon_size which);
779cda9c99SIngo Weinhold 		status_t SetIconForType(const char *type, const char *fileType,
789cda9c99SIngo Weinhold 					const void *data, size_t dataSize);
799cda9c99SIngo Weinhold 		status_t SetPreferredApp(const char *type, const char *signature,
809cda9c99SIngo Weinhold 					app_verb verb = B_OPEN);
819cda9c99SIngo Weinhold 		status_t SetSnifferRule(const char *type, const char *rule);
829cda9c99SIngo Weinhold 		status_t SetSupportedTypes(const char *type, const BMessage *types, bool fullSync);
839cda9c99SIngo Weinhold 
849cda9c99SIngo Weinhold 		// Non-atomic Get()
859cda9c99SIngo Weinhold 		status_t GetInstalledSupertypes(BMessage *super_types);
869cda9c99SIngo Weinhold 		status_t GetInstalledTypes(BMessage *types);
879cda9c99SIngo Weinhold 		status_t GetInstalledTypes(const char *super_type,
889cda9c99SIngo Weinhold 					BMessage *subtypes);
899cda9c99SIngo Weinhold 		status_t GetSupportingApps(const char *type, BMessage *signatures);
909cda9c99SIngo Weinhold 		status_t GetAssociatedTypes(const char *extension, BMessage *types);
919cda9c99SIngo Weinhold 
929cda9c99SIngo Weinhold 		// Sniffer
939cda9c99SIngo Weinhold 		status_t GuessMimeType(const entry_ref *file, BString *result);
949cda9c99SIngo Weinhold 		status_t GuessMimeType(const void *buffer, int32 length, BString *result);
959cda9c99SIngo Weinhold 		status_t GuessMimeType(const char *filename, BString *result);
969cda9c99SIngo Weinhold 
979cda9c99SIngo Weinhold 		// Monitor
989cda9c99SIngo Weinhold 		status_t StartWatching(BMessenger target);
999cda9c99SIngo Weinhold 		status_t StopWatching(BMessenger target);
1009cda9c99SIngo Weinhold 
1019cda9c99SIngo Weinhold 		// Delete()
1029cda9c99SIngo Weinhold 		status_t DeleteAppHint(const char *type);
1039cda9c99SIngo Weinhold 		status_t DeleteAttrInfo(const char *type);
1049cda9c99SIngo Weinhold 		status_t DeleteShortDescription(const char *type);
1059cda9c99SIngo Weinhold 		status_t DeleteLongDescription(const char *type);
1069cda9c99SIngo Weinhold 		status_t DeleteFileExtensions(const char *type);
1079cda9c99SIngo Weinhold 		status_t DeleteIcon(const char *type, icon_size size);
1089cda9c99SIngo Weinhold 		status_t DeleteIcon(const char *type);
1099cda9c99SIngo Weinhold 		status_t DeleteIconForType(const char *type, const char *fileType,
1109cda9c99SIngo Weinhold 					icon_size which);
1119cda9c99SIngo Weinhold 		status_t DeleteIconForType(const char *type, const char *fileType);
1129cda9c99SIngo Weinhold 		status_t DeletePreferredApp(const char *type, app_verb verb = B_OPEN);
1139cda9c99SIngo Weinhold 		status_t DeleteSnifferRule(const char *type);
1149cda9c99SIngo Weinhold 		status_t DeleteSupportedTypes(const char *type, bool fullSync);
1159cda9c99SIngo Weinhold 
1169cda9c99SIngo Weinhold 		// deferred notifications
1179cda9c99SIngo Weinhold 		void	DeferInstallNotification(const char* type);
1189cda9c99SIngo Weinhold 		void	UndeferInstallNotification(const char* type);
1199cda9c99SIngo Weinhold 
1209cda9c99SIngo Weinhold 	private:
1219cda9c99SIngo Weinhold 		struct DeferredInstallNotification {
1229cda9c99SIngo Weinhold 			char	type[B_MIME_TYPE_LENGTH];
1239cda9c99SIngo Weinhold 			bool	notify;
1249cda9c99SIngo Weinhold 		};
1259cda9c99SIngo Weinhold 
1269cda9c99SIngo Weinhold 		status_t _SetStringValue(const char *type, int32 what,
1279cda9c99SIngo Weinhold 					const char* attribute, type_code attributeType,
1289cda9c99SIngo Weinhold 					size_t maxLength, const char *value);
1299cda9c99SIngo Weinhold 
1309cda9c99SIngo Weinhold 		// Functions to send monitor notifications
1319cda9c99SIngo Weinhold 		status_t _SendInstallNotification(const char *type);
1329cda9c99SIngo Weinhold 		status_t _SendDeleteNotification(const char *type);
1339cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1349cda9c99SIngo Weinhold 					const char *extraType, bool largeIcon, int32 action);
1359cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1369cda9c99SIngo Weinhold 					const char *extraType, int32 action);
1379cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1389cda9c99SIngo Weinhold 					bool largeIcon, int32 action);
1399cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1409cda9c99SIngo Weinhold 					int32 action);
1419cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(BMessage &msg);
1429cda9c99SIngo Weinhold 
1439cda9c99SIngo Weinhold 		DeferredInstallNotification* _FindDeferredInstallNotification(
1449cda9c99SIngo Weinhold 			const char* type, bool remove = false);
1459cda9c99SIngo Weinhold 		bool _CheckDeferredInstallNotification(int32 which, const char* type);
1469cda9c99SIngo Weinhold 
1479cda9c99SIngo Weinhold 	private:
1489cda9c99SIngo Weinhold 		status_t fStatus;
149*b94857b3SIngo Weinhold 		DatabaseLocation* fLocation;
1509cda9c99SIngo Weinhold 		NotificationListener* fNotificationListener;
1519cda9c99SIngo Weinhold 		std::set<BMessenger> fMonitorMessengers;
1529cda9c99SIngo Weinhold 		AssociatedTypes fAssociatedTypes;
1539cda9c99SIngo Weinhold 		InstalledTypes fInstalledTypes;
1549cda9c99SIngo Weinhold 		SnifferRules fSnifferRules;
1559cda9c99SIngo Weinhold 		SupportingApps fSupportingApps;
1569cda9c99SIngo Weinhold 
1579cda9c99SIngo Weinhold 		BLocker	fDeferredInstallNotificationsLocker;
1589cda9c99SIngo Weinhold 		BList	fDeferredInstallNotifications;
1599cda9c99SIngo Weinhold };
1609cda9c99SIngo Weinhold 
1619cda9c99SIngo Weinhold class InstallNotificationDeferrer {
1629cda9c99SIngo Weinhold 	public:
1639cda9c99SIngo Weinhold 		InstallNotificationDeferrer(Database* database, const char* type)
1649cda9c99SIngo Weinhold 			:
1659cda9c99SIngo Weinhold 			fDatabase(database),
1669cda9c99SIngo Weinhold 			fType(type)
1679cda9c99SIngo Weinhold 		{
1689cda9c99SIngo Weinhold 			if (fDatabase != NULL && fType != NULL)
1699cda9c99SIngo Weinhold 				fDatabase->DeferInstallNotification(fType);
1709cda9c99SIngo Weinhold 		}
1719cda9c99SIngo Weinhold 
1729cda9c99SIngo Weinhold 		~InstallNotificationDeferrer()
1739cda9c99SIngo Weinhold 		{
1749cda9c99SIngo Weinhold 			if (fDatabase != NULL && fType != NULL)
1759cda9c99SIngo Weinhold 				fDatabase->UndeferInstallNotification(fType);
1769cda9c99SIngo Weinhold 		}
1779cda9c99SIngo Weinhold 
1789cda9c99SIngo Weinhold 	private:
1799cda9c99SIngo Weinhold 		Database*	fDatabase;
1809cda9c99SIngo Weinhold 		const char*	fType;
1819cda9c99SIngo Weinhold };
1829cda9c99SIngo Weinhold 
1839cda9c99SIngo Weinhold 
1849cda9c99SIngo Weinhold class Database::NotificationListener {
1859cda9c99SIngo Weinhold public:
1869cda9c99SIngo Weinhold 	virtual						~NotificationListener();
1879cda9c99SIngo Weinhold 
1889cda9c99SIngo Weinhold 	virtual	status_t			Notify(BMessage* message,
1899cda9c99SIngo Weinhold 									const BMessenger& target) = 0;
1909cda9c99SIngo Weinhold };
1919cda9c99SIngo Weinhold 
1929cda9c99SIngo Weinhold 
1939cda9c99SIngo Weinhold } // namespace Mime
1949cda9c99SIngo Weinhold } // namespace Storage
1959cda9c99SIngo Weinhold } // namespace BPrivate
1969cda9c99SIngo Weinhold 
1979cda9c99SIngo Weinhold #endif	// _MIME_DATABASE_H
198