xref: /haiku/headers/private/storage/AddOnMonitorHandler.h (revision 2beda3bb5be8191b672688bed7ddcadd2b17dc41)
1 /*
2  * Copyright 2004-2010, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _ADD_ON_MONITOR_HANDLER_H
6 #define _ADD_ON_MONITOR_HANDLER_H
7 
8 
9 #include <list>
10 
11 #include "NodeMonitorHandler.h"
12 
13 
14 namespace BPrivate {
15 namespace Storage {
16 
17 
18 struct add_on_entry_info {
19 	char name[B_FILE_NAME_LENGTH];
20 	node_ref nref;
21 	node_ref dir_nref;
22 	node_ref addon_nref;
23 };
24 
25 
26 class AddOnMonitorHandler : public NodeMonitorHandler {
27 public:
28 								AddOnMonitorHandler(const char* name = NULL);
29 	virtual						~AddOnMonitorHandler();
30 
31 	virtual	void				MessageReceived(BMessage* message);
32 
33 	// Supply the add-on directories here, in the order you want them checked.
34 	// Add-ons in directories added earlier will shadow add-ons in directories
35 	// added later, if they share the same file name. If an add-on is removed
36 	// from or renamed in a directory and it has previously shadowed another
37 	// add-on, the previously shadowed add-on shall become enabled
38 	// (AddOnEnabled()). If an add-on appears in a directory, or is renamed,
39 	// it can cause another add-on to become disabled, if it has the same name.
40 	// Note that directories are not watched recursively, and all entries
41 	// are reported as add-ons regardless of their node type (files,
42 	// directories, symlinks).
43 	// If sync is true all pending add-on entries are handled immediately.
44 	// Including entries from other directories.
45 	virtual	status_t			AddDirectory(const node_ref* nref,
46 									bool sync = false);
47 
48 protected:
49 	// hooks for sub-class
50 	virtual	void				AddOnCreated(
51 									const add_on_entry_info* entryInfo);
52 	virtual	void				AddOnEnabled(
53 									const add_on_entry_info* entryInfo);
54 	virtual	void				AddOnDisabled(
55 									const add_on_entry_info* entryInfo);
56 									// name field will be invalid!
57 	virtual	void				AddOnRemoved(
58 									const add_on_entry_info* entryInfo);
59 									// name field will be invalid!
60 
61 protected:
62 	virtual	void				EntryCreated(const char* name, ino_t directory,
63 									dev_t device, ino_t node);
64 	virtual	void				EntryRemoved(const char *name, ino_t directory,
65 									dev_t device, ino_t node);
66 	virtual	void				EntryMoved(const char *name,
67 									const char *fromName, ino_t fromDirectory,
68 									ino_t toDirectory, dev_t device,
69 									ino_t node, dev_t nodeDevice);
70 	virtual	void				StatChanged(ino_t node, dev_t device,
71 									int32 statFields);
72 
73 private:
74 			void				_HandlePendingEntries();
75 			void				_EntryCreated(add_on_entry_info& info);
76 
77 			typedef NodeMonitorHandler inherited;
78 			typedef std::list<add_on_entry_info> EntryList;
79 
80 			struct add_on_directory_info {
81 				node_ref		nref;
82 				EntryList		entries;
83 			};
84 
85 			typedef std::list<add_on_directory_info> DirectoryList;
86 
87 			bool				_FindEntry(const node_ref& entry,
88 									const EntryList& list,
89 									EntryList::iterator& it) const;
90 			bool				_FindEntry(const char* name,
91 									const EntryList& list,
92 									EntryList::iterator& it) const;
93 
94 			bool				_HasEntry(const node_ref& entry,
95 									EntryList& list) const;
96 			bool				_HasEntry(const char* name,
97 									EntryList& list) const;
98 
99 			bool				_FindDirectory(ino_t directory, dev_t device,
100 									DirectoryList::iterator& it) const;
101 			bool				_FindDirectory(
102 									const node_ref& directoryNodeRef,
103 									DirectoryList::iterator& it) const;
104 			bool				_FindDirectory(ino_t directory, dev_t device,
105 									DirectoryList::iterator& it,
106 									const DirectoryList::const_iterator& end)
107 										const;
108 			bool				_FindDirectory(
109 									const node_ref& directoryNodeRef,
110 									DirectoryList::iterator& it,
111 									const DirectoryList::const_iterator& end)
112 										const;
113 
114 			void				_AddNewEntry(EntryList& list,
115 									add_on_entry_info& info);
116 
117 private:
118 			DirectoryList		fDirectories;
119 			EntryList			fPendingEntries;
120 };
121 
122 
123 }; // namespace Storage
124 }; // namespace BPrivate
125 
126 
127 using namespace BPrivate::Storage;
128 
129 
130 #endif	// _ADD_ON_MONITOR_HANDLER_H
131