2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 //! The backbone of the VolumeMountedEvent.
10 #include "VolumeWatcher.h"
12 #include <Application.h>
14 #include <NodeMonitor.h>
17 static BLocker
sLocker("volume watcher");
18 static VolumeWatcher
* sWatcher
;
21 VolumeListener::~VolumeListener()
29 VolumeWatcher::VolumeWatcher()
31 BHandler("volume watcher")
34 be_app
->AddHandler(this);
36 watch_node(NULL
, B_WATCH_MOUNT
, this);
42 VolumeWatcher::~VolumeWatcher()
47 be_app
->RemoveHandler(this);
54 VolumeWatcher::AddListener(VolumeListener
* listener
)
56 BAutolock
lock(sLocker
);
57 fListeners
.AddItem(listener
);
62 VolumeWatcher::RemoveListener(VolumeListener
* listener
)
64 BAutolock
lock(sLocker
);
65 fListeners
.RemoveItem(listener
);
70 VolumeWatcher::CountListeners() const
72 BAutolock
lock(sLocker
);
73 return fListeners
.CountItems();
78 VolumeWatcher::MessageReceived(BMessage
* message
)
80 switch (message
->what
) {
83 int32 opcode
= message
->GetInt32("opcode", -1);
84 if (opcode
== B_DEVICE_MOUNTED
) {
86 if (message
->FindInt32("new device", &device
) == B_OK
) {
87 BAutolock
lock(sLocker
);
88 for (int32 i
= 0; i
< fListeners
.CountItems(); i
++) {
89 fListeners
.ItemAt(i
)->VolumeMounted(device
);
92 } else if (opcode
== B_DEVICE_UNMOUNTED
) {
94 if (message
->FindInt32("device", &device
) == B_OK
) {
95 BAutolock
lock(sLocker
);
96 for (int32 i
= 0; i
< fListeners
.CountItems(); i
++) {
97 fListeners
.ItemAt(i
)->VolumeUnmounted(device
);
108 VolumeWatcher::Register(VolumeListener
* listener
)
110 BAutolock
lock(sLocker
);
111 if (sWatcher
== NULL
)
112 sWatcher
= new VolumeWatcher();
114 sWatcher
->AddListener(listener
);
119 VolumeWatcher::Unregister(VolumeListener
* listener
)
121 BAutolock
lock(sLocker
);
122 sWatcher
->RemoveListener(listener
);
124 if (sWatcher
->CountListeners() == 0)