Debian Codegraph @

src/support/notification_osx.mm

raw
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2018 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <Cocoa/Cocoa.h>
14
15 #include <IOKit/IOKitLib.h>
16 #include <IOKit/storage/IOBlockStorageDevice.h>
17
18 #include <support/notification.h>
19
20 #include <boca.h>
21
22 using namespace BoCA;
23 using namespace BoCA::AS;
24
25 @interface NotificationReceiver : NSObject
26 {
27 @private
28 Array<String> knownDevices;
29 }
30
31 - (id) init;
32
33 - (void) didMount: (NSNotification *) notification;
34 - (void) didUnmount: (NSNotification *) notification;
35
36 - (void) updateKnownDevices;
37 @end
38
39 @implementation NotificationReceiver
40 - (id) init
41 {
42 [super init];
43
44 [self updateKnownDevices];
45
46 return self;
47 }
48
49 - (void) didMount: (NSNotification *) notification
50 {
51 /* 100ms delay to allow the system to update the media list
52 * (without this, the mounted medium might not be reported yet).
53 */
54 S::System::System::Sleep(100);
55
56 /* Compare known devices to new device list to find mounted device.
57 */
58 Registry &boca = Registry::Get();
59 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
60
61 if (info != NIL)
62 {
63 if (info->GetNumberOfDevices() > knownDevices.Length())
64 {
65 for (Int drive = 0; drive < info->GetNumberOfDevices(); drive++)
66 {
67 const Device &device = info->GetNthDeviceInfo(drive);
68
69 if (device.path != knownDevices.GetNth(drive))
70 {
71 freac::Notification::Get()->onDriveChange.Emit();
72 freac::Notification::Get()->onDiscInsert.Emit(drive);
73
74 break;
75 }
76 }
77 }
78
79 boca.DeleteComponent(info);
80 }
81
82 [self updateKnownDevices];
83 }
84
85 - (void) didUnmount: (NSNotification *) notification
86 {
87 /* 100ms delay to allow the system to update the media list
88 * (without this, the unmounted medium might still be reported).
89 */
90 S::System::System::Sleep(100);
91
92 /* Compare known devices to new device list to find unmounted device.
93 */
94 Registry &boca = Registry::Get();
95 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
96
97 if (info != NIL)
98 {
99 if (info->GetNumberOfDevices() < knownDevices.Length())
100 {
101 for (Int drive = 0; drive <= info->GetNumberOfDevices(); drive++)
102 {
103 if (drive == info->GetNumberOfDevices())
104 {
105 freac::Notification::Get()->onDiscRemove.Emit(drive);
106 freac::Notification::Get()->onDriveChange.Emit();
107
108 break;
109 }
110
111 const Device &device = info->GetNthDeviceInfo(drive);
112
113 if (device.path != knownDevices.GetNth(drive))
114 {
115 freac::Notification::Get()->onDiscRemove.Emit(drive);
116 freac::Notification::Get()->onDriveChange.Emit();
117
118 break;
119 }
120 }
121 }
122
123 boca.DeleteComponent(info);
124 }
125
126 [self updateKnownDevices];
127 }
128
129 - (void) updateKnownDevices
130 {
131 knownDevices.RemoveAll();
132
133 /* Get list of devices known to
134 * the device info component.
135 */
136 Registry &boca = Registry::Get();
137 DeviceInfoComponent *info = boca.CreateDeviceInfoComponent();
138
139 if (info != NIL)
140 {
141 for (Int drive = 0; drive < info->GetNumberOfDevices(); drive++)
142 {
143 const Device &device = info->GetNthDeviceInfo(drive);
144
145 knownDevices.Add(device.path);
146 }
147
148 boca.DeleteComponent(info);
149 }
150 }
151 @end
152
153 freac::Notification *freac::Notification::instance = NIL;
154
155 freac::Notification::Notification()
156 {
157 /* Create notification receiver instance.
158 */
159 privateData = [[NotificationReceiver alloc] init];
160
161 /* Register for mount and unmount notifications.
162 */
163 NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
164
165 [notificationCenter addObserver: (id) privateData
166 selector: @selector(didMount:)
167 name: NSWorkspaceDidMountNotification
168 object: [NSWorkspace sharedWorkspace]];
169
170 [notificationCenter addObserver: (id) privateData
171 selector: @selector(didUnmount:)
172 name: NSWorkspaceDidUnmountNotification
173 object: [NSWorkspace sharedWorkspace]];
174 }
175
176 freac::Notification::~Notification()
177 {
178 /* Unregister mount and unmount notification handlers.
179 */
180 NSNotificationCenter *notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
181
182 [notificationCenter removeObserver: (id) privateData
183 name: NSWorkspaceDidMountNotification
184 object: [NSWorkspace sharedWorkspace]];
185
186 [notificationCenter removeObserver: (id) privateData
187 name: NSWorkspaceDidUnmountNotification
188 object: [NSWorkspace sharedWorkspace]];
189
190 /* Release notification receiver instance.
191 */
192 [(NotificationReceiver *) privateData release];
193 }
194
195 freac::Notification *freac::Notification::Get()
196 {
197 if (instance == NIL)
198 {
199 instance = new Notification();
200 }
201
202 return instance;
203 }
204
205 Void freac::Notification::Free()
206 {
207 if (instance != NIL)
208 {
209 delete instance;
210
211 instance = NIL;
212 }
213 }
214
215 Bool freac::Notification::IsNotificationAvailable() const
216 {
217 return True;
218 }
219
220 Void freac::Notification::ProcessSystemMessage(Int message, Int wParam, Int lParam)
221 {
222 }