xref: /haiku/headers/private/storage/AddOnMonitorHandler.h (revision 675ffabd70492a962f8c0288a32208c22ce5de18)
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(ino_t directory, dev_t device,
65 									ino_t node);
66 	virtual	void				EntryMoved(const char* name,
67 									ino_t fromDirectory, ino_t toDirectory,
68 									dev_t device, ino_t node);
69 	virtual	void				StatChanged(ino_t node, dev_t device);
70 
71 private:
72 			void				_HandlePendingEntries();
73 			void				_EntryCreated(add_on_entry_info& info);
74 
75 			typedef NodeMonitorHandler inherited;
76 			typedef std::list<add_on_entry_info> EntryList;
77 
78 			struct add_on_directory_info {
79 				node_ref		nref;
80 				EntryList		entries;
81 			};
82 
83 			typedef std::list<add_on_directory_info> DirectoryList;
84 
85 			bool				_FindEntry(const node_ref& entry,
86 									const EntryList& list,
87 									EntryList::iterator& it) const;
88 			bool				_FindEntry(const char* name,
89 									const EntryList& list,
90 									EntryList::iterator& it) const;
91 
92 			bool				_HasEntry(const node_ref& entry,
93 									EntryList& list) const;
94 			bool				_HasEntry(const char* name,
95 									EntryList& list) const;
96 
97 			bool				_FindDirectory(ino_t directory, dev_t device,
98 									DirectoryList::iterator& it) const;
99 			bool				_FindDirectory(
100 									const node_ref& directoryNodeRef,
101 									DirectoryList::iterator& it) const;
102 			bool				_FindDirectory(ino_t directory, dev_t device,
103 									DirectoryList::iterator& it,
104 									const DirectoryList::const_iterator& end)
105 										const;
106 			bool				_FindDirectory(
107 									const node_ref& directoryNodeRef,
108 									DirectoryList::iterator& it,
109 									const DirectoryList::const_iterator& end)
110 										const;
111 
112 			void				_AddNewEntry(EntryList& list,
113 									add_on_entry_info& info);
114 
115 private:
116 			DirectoryList		fDirectories;
117 			EntryList			fPendingEntries;
118 };
119 
120 
121 }; // namespace Storage
122 }; // namespace BPrivate
123 
124 
125 using namespace BPrivate::Storage;
126 
127 
128 #endif	// _ADD_ON_MONITOR_HANDLER_H
129