btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / musiccollection / FileMonitor.h
blobdac34d41da22379b5910bee02f76501bda4b939a
1 /*
2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Clemens Zeidler <haiku@clemens-zeidler.de>
7 */
8 #ifndef FILE_MONITOR_H
9 #define FILE_MONITOR_H
12 #include <map>
13 #include <vector>
15 #include <Entry.h>
16 #include <Node.h>
18 #include "NodeMonitorHandler.h"
21 struct WatchedFile {
22 entry_ref entry;
23 node_ref node;
24 /*! Don't use it as the primary cookie storage. To be set in EntryCreated
25 in EntryViewInterface. */
26 void* cookie;
30 class NodeRefComp {
31 public:
32 bool
33 operator()(const node_ref& a, const node_ref& b)
35 return a.node < b.node;
40 typedef std::map<node_ref, WatchedFile, NodeRefComp> WatchedFileList;
43 class EntryViewInterface {
44 public:
45 virtual ~EntryViewInterface() {};
47 virtual void EntryCreated(WatchedFile* file) {};
48 virtual void EntryRemoved(WatchedFile* file) {};
49 virtual void EntryMoved(WatchedFile* file) {};
50 virtual void StatChanged(WatchedFile* file) {};
51 virtual void AttrChanged(WatchedFile* file) {};
53 virtual void EntriesCleared() {};
57 const uint32 kMsgAddRefs = '&adr';
58 const uint32 kMsgCleared = '&clr';
61 typedef std::vector<entry_ref> RefList;
64 class ReadThread;
67 class FileMonitor : public NodeMonitorHandler {
68 public:
69 FileMonitor(EntryViewInterface* listener);
70 ~FileMonitor();
72 void SetReadThread(ReadThread* readThread);
74 void Reset();
76 virtual void MessageReceived(BMessage* message);
78 virtual void EntryCreated(const char *name, ino_t directory,
79 dev_t device, ino_t node);
80 virtual void EntryRemoved(const char *name, ino_t directory,
81 dev_t device, ino_t node);
82 virtual void EntryMoved(const char *name,
83 const char *fromName, ino_t fromDirectory,
84 ino_t toDirectory, dev_t device,
85 ino_t node, dev_t nodeDevice);
86 virtual void StatChanged(ino_t node, dev_t device,
87 int32 statFields);
88 virtual void AttrChanged(ino_t node, dev_t device);
90 private:
91 WatchedFile* _FindFile(dev_t device, ino_t node);
93 EntryViewInterface* fListener;
94 WatchedFileList fWatchedFileList;
96 ReadThread* fReadThread;
97 RefList* fCurrentReadList;
98 uint32 fCurrentReadIndex;
102 class ReadThread {
103 public:
104 ReadThread(FileMonitor* target);
105 virtual ~ReadThread() {}
107 status_t Run();
108 bool Running();
109 status_t Wait();
111 void Stop();
112 bool Stopped();
114 RefList* ReadRefList();
115 void ReadDone();
117 protected:
118 virtual bool ReadNextEntry(entry_ref& entry) = 0;
120 int32 Process();
122 friend int32 ReadThreadFunction(void *data);
124 BHandler* fTarget;
126 RefList fRefList1;
127 RefList fRefList2;
128 RefList* fWriteRefList;
129 RefList* fReadRefList;
130 bool fReading;
132 private:
133 void _SwapLists();
134 inline void _PublishEntrys(BMessenger& messenger);
136 bool fStopped;
137 thread_id fThreadId;
139 int16 fNReaded;
140 bool fRunning;
144 #endif // FILE_MONITOR_H