xref: /haiku/headers/private/storage/mime/Database.h (revision ee2974dadb9c6c99b8ad9b393f992fae42e4bc39)
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 
38b94857b3SIngo Weinhold 
39b94857b3SIngo Weinhold class DatabaseLocation;
409cda9c99SIngo Weinhold class MimeSniffer;
419cda9c99SIngo Weinhold 
42b94857b3SIngo 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:
54b94857b3SIngo Weinhold 		Database(DatabaseLocation* databaseLocation, MimeSniffer* mimeSniffer,
559cda9c99SIngo Weinhold 			NotificationListener* notificationListener);
569cda9c99SIngo Weinhold 		~Database();
579cda9c99SIngo Weinhold 
589cda9c99SIngo Weinhold 		status_t InitCheck() const;
599cda9c99SIngo Weinhold 
Location()60b94857b3SIngo Weinhold 		DatabaseLocation* Location() const	{ return fLocation; }
61b94857b3SIngo 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);
72*ee2974daSIngo Weinhold 		status_t SetIcon(const char* type, const BBitmap* icon,
73*ee2974daSIngo Weinhold 					icon_size which);
749cda9c99SIngo Weinhold 		status_t SetIcon(const char *type, const void *data, size_t dataSize,
759cda9c99SIngo Weinhold 					icon_size which);
769cda9c99SIngo Weinhold 		status_t SetIcon(const char *type, const void *data, size_t dataSize);
779cda9c99SIngo Weinhold 		status_t SetIconForType(const char* type, const char* fileType,
78*ee2974daSIngo Weinhold 					const BBitmap* icon, icon_size which);
79*ee2974daSIngo Weinhold 		status_t SetIconForType(const char *type, const char *fileType,
809cda9c99SIngo Weinhold 					const void *data, size_t dataSize, icon_size which);
819cda9c99SIngo Weinhold 		status_t SetIconForType(const char *type, const char *fileType,
829cda9c99SIngo Weinhold 					const void *data, size_t dataSize);
839cda9c99SIngo Weinhold 		status_t SetPreferredApp(const char *type, const char *signature,
849cda9c99SIngo Weinhold 					app_verb verb = B_OPEN);
859cda9c99SIngo Weinhold 		status_t SetSnifferRule(const char *type, const char *rule);
869cda9c99SIngo Weinhold 		status_t SetSupportedTypes(const char *type, const BMessage *types, bool fullSync);
879cda9c99SIngo Weinhold 
889cda9c99SIngo Weinhold 		// Non-atomic Get()
899cda9c99SIngo Weinhold 		status_t GetInstalledSupertypes(BMessage *super_types);
909cda9c99SIngo Weinhold 		status_t GetInstalledTypes(BMessage *types);
919cda9c99SIngo Weinhold 		status_t GetInstalledTypes(const char *super_type,
929cda9c99SIngo Weinhold 					BMessage *subtypes);
939cda9c99SIngo Weinhold 		status_t GetSupportingApps(const char *type, BMessage *signatures);
949cda9c99SIngo Weinhold 		status_t GetAssociatedTypes(const char *extension, BMessage *types);
959cda9c99SIngo Weinhold 
969cda9c99SIngo Weinhold 		// Sniffer
979cda9c99SIngo Weinhold 		status_t GuessMimeType(const entry_ref *file, BString *result);
989cda9c99SIngo Weinhold 		status_t GuessMimeType(const void *buffer, int32 length, BString *result);
999cda9c99SIngo Weinhold 		status_t GuessMimeType(const char *filename, BString *result);
1009cda9c99SIngo Weinhold 
1019cda9c99SIngo Weinhold 		// Monitor
1029cda9c99SIngo Weinhold 		status_t StartWatching(BMessenger target);
1039cda9c99SIngo Weinhold 		status_t StopWatching(BMessenger target);
1049cda9c99SIngo Weinhold 
1059cda9c99SIngo Weinhold 		// Delete()
1069cda9c99SIngo Weinhold 		status_t DeleteAppHint(const char *type);
1079cda9c99SIngo Weinhold 		status_t DeleteAttrInfo(const char *type);
1089cda9c99SIngo Weinhold 		status_t DeleteShortDescription(const char *type);
1099cda9c99SIngo Weinhold 		status_t DeleteLongDescription(const char *type);
1109cda9c99SIngo Weinhold 		status_t DeleteFileExtensions(const char *type);
1119cda9c99SIngo Weinhold 		status_t DeleteIcon(const char *type, icon_size size);
1129cda9c99SIngo Weinhold 		status_t DeleteIcon(const char *type);
1139cda9c99SIngo Weinhold 		status_t DeleteIconForType(const char *type, const char *fileType,
1149cda9c99SIngo Weinhold 					icon_size which);
1159cda9c99SIngo Weinhold 		status_t DeleteIconForType(const char *type, const char *fileType);
1169cda9c99SIngo Weinhold 		status_t DeletePreferredApp(const char *type, app_verb verb = B_OPEN);
1179cda9c99SIngo Weinhold 		status_t DeleteSnifferRule(const char *type);
1189cda9c99SIngo Weinhold 		status_t DeleteSupportedTypes(const char *type, bool fullSync);
1199cda9c99SIngo Weinhold 
1209cda9c99SIngo Weinhold 		// deferred notifications
1219cda9c99SIngo Weinhold 		void	DeferInstallNotification(const char* type);
1229cda9c99SIngo Weinhold 		void	UndeferInstallNotification(const char* type);
1239cda9c99SIngo Weinhold 
1249cda9c99SIngo Weinhold 	private:
1259cda9c99SIngo Weinhold 		struct DeferredInstallNotification {
1269cda9c99SIngo Weinhold 			char	type[B_MIME_TYPE_LENGTH];
1279cda9c99SIngo Weinhold 			bool	notify;
1289cda9c99SIngo Weinhold 		};
1299cda9c99SIngo Weinhold 
1309cda9c99SIngo Weinhold 		status_t _SetStringValue(const char *type, int32 what,
1319cda9c99SIngo Weinhold 					const char* attribute, type_code attributeType,
1329cda9c99SIngo Weinhold 					size_t maxLength, const char *value);
1339cda9c99SIngo Weinhold 
1349cda9c99SIngo Weinhold 		// Functions to send monitor notifications
1359cda9c99SIngo Weinhold 		status_t _SendInstallNotification(const char *type);
1369cda9c99SIngo Weinhold 		status_t _SendDeleteNotification(const char *type);
1379cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1389cda9c99SIngo Weinhold 					const char *extraType, bool largeIcon, int32 action);
1399cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1409cda9c99SIngo Weinhold 					const char *extraType, int32 action);
1419cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1429cda9c99SIngo Weinhold 					bool largeIcon, int32 action);
1439cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(int32 which, const char *type,
1449cda9c99SIngo Weinhold 					int32 action);
1459cda9c99SIngo Weinhold 		status_t _SendMonitorUpdate(BMessage &msg);
1469cda9c99SIngo Weinhold 
1479cda9c99SIngo Weinhold 		DeferredInstallNotification* _FindDeferredInstallNotification(
1489cda9c99SIngo Weinhold 			const char* type, bool remove = false);
1499cda9c99SIngo Weinhold 		bool _CheckDeferredInstallNotification(int32 which, const char* type);
1509cda9c99SIngo Weinhold 
1519cda9c99SIngo Weinhold 	private:
1529cda9c99SIngo Weinhold 		status_t fStatus;
153b94857b3SIngo Weinhold 		DatabaseLocation* fLocation;
1549cda9c99SIngo Weinhold 		NotificationListener* fNotificationListener;
1559cda9c99SIngo Weinhold 		std::set<BMessenger> fMonitorMessengers;
1569cda9c99SIngo Weinhold 		AssociatedTypes fAssociatedTypes;
1579cda9c99SIngo Weinhold 		InstalledTypes fInstalledTypes;
1589cda9c99SIngo Weinhold 		SnifferRules fSnifferRules;
1599cda9c99SIngo Weinhold 		SupportingApps fSupportingApps;
1609cda9c99SIngo Weinhold 
1619cda9c99SIngo Weinhold 		BLocker	fDeferredInstallNotificationsLocker;
1629cda9c99SIngo Weinhold 		BList	fDeferredInstallNotifications;
1639cda9c99SIngo Weinhold };
1649cda9c99SIngo Weinhold 
1659cda9c99SIngo Weinhold class InstallNotificationDeferrer {
1669cda9c99SIngo Weinhold 	public:
InstallNotificationDeferrer(Database * database,const char * type)1679cda9c99SIngo Weinhold 		InstallNotificationDeferrer(Database* database, const char* type)
1689cda9c99SIngo Weinhold 			:
1699cda9c99SIngo Weinhold 			fDatabase(database),
1709cda9c99SIngo Weinhold 			fType(type)
1719cda9c99SIngo Weinhold 		{
1729cda9c99SIngo Weinhold 			if (fDatabase != NULL && fType != NULL)
1739cda9c99SIngo Weinhold 				fDatabase->DeferInstallNotification(fType);
1749cda9c99SIngo Weinhold 		}
1759cda9c99SIngo Weinhold 
~InstallNotificationDeferrer()1769cda9c99SIngo Weinhold 		~InstallNotificationDeferrer()
1779cda9c99SIngo Weinhold 		{
1789cda9c99SIngo Weinhold 			if (fDatabase != NULL && fType != NULL)
1799cda9c99SIngo Weinhold 				fDatabase->UndeferInstallNotification(fType);
1809cda9c99SIngo Weinhold 		}
1819cda9c99SIngo Weinhold 
1829cda9c99SIngo Weinhold 	private:
1839cda9c99SIngo Weinhold 		Database*	fDatabase;
1849cda9c99SIngo Weinhold 		const char*	fType;
1859cda9c99SIngo Weinhold };
1869cda9c99SIngo Weinhold 
1879cda9c99SIngo Weinhold 
1889cda9c99SIngo Weinhold class Database::NotificationListener {
1899cda9c99SIngo Weinhold public:
1909cda9c99SIngo Weinhold 	virtual						~NotificationListener();
1919cda9c99SIngo Weinhold 
1929cda9c99SIngo Weinhold 	virtual	status_t			Notify(BMessage* message,
1939cda9c99SIngo Weinhold 									const BMessenger& target) = 0;
1949cda9c99SIngo Weinhold };
1959cda9c99SIngo Weinhold 
1969cda9c99SIngo Weinhold 
1979cda9c99SIngo Weinhold } // namespace Mime
1989cda9c99SIngo Weinhold } // namespace Storage
1999cda9c99SIngo Weinhold } // namespace BPrivate
2009cda9c99SIngo Weinhold 
2019cda9c99SIngo Weinhold #endif	// _MIME_DATABASE_H
202