1*5c9672edSIngo Weinhold /*
2*5c9672edSIngo Weinhold * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3*5c9672edSIngo Weinhold * Distributed under the terms of the MIT License.
4*5c9672edSIngo Weinhold */
5*5c9672edSIngo Weinhold
6*5c9672edSIngo Weinhold
7*5c9672edSIngo Weinhold #include "PackageWatchingManager.h"
8*5c9672edSIngo Weinhold
9*5c9672edSIngo Weinhold #include <new>
10*5c9672edSIngo Weinhold
11*5c9672edSIngo Weinhold #include <package/PackageRoster.h>
12*5c9672edSIngo Weinhold
13*5c9672edSIngo Weinhold #include <RegistrarDefs.h>
14*5c9672edSIngo Weinhold
15*5c9672edSIngo Weinhold #include "Debug.h"
16*5c9672edSIngo Weinhold #include "EventMaskWatcher.h"
17*5c9672edSIngo Weinhold
18*5c9672edSIngo Weinhold
19*5c9672edSIngo Weinhold using namespace BPackageKit;
20*5c9672edSIngo Weinhold using namespace BPrivate;
21*5c9672edSIngo Weinhold
22*5c9672edSIngo Weinhold
PackageWatchingManager()23*5c9672edSIngo Weinhold PackageWatchingManager::PackageWatchingManager()
24*5c9672edSIngo Weinhold {
25*5c9672edSIngo Weinhold }
26*5c9672edSIngo Weinhold
27*5c9672edSIngo Weinhold
~PackageWatchingManager()28*5c9672edSIngo Weinhold PackageWatchingManager::~PackageWatchingManager()
29*5c9672edSIngo Weinhold {
30*5c9672edSIngo Weinhold }
31*5c9672edSIngo Weinhold
32*5c9672edSIngo Weinhold
33*5c9672edSIngo Weinhold void
HandleStartStopWatching(BMessage * request)34*5c9672edSIngo Weinhold PackageWatchingManager::HandleStartStopWatching(BMessage* request)
35*5c9672edSIngo Weinhold {
36*5c9672edSIngo Weinhold status_t error = request->what == B_REG_PACKAGE_START_WATCHING
37*5c9672edSIngo Weinhold ? _AddWatcher(request) : _RemoveWatcher(request);
38*5c9672edSIngo Weinhold
39*5c9672edSIngo Weinhold if (error == B_OK) {
40*5c9672edSIngo Weinhold BMessage reply(B_REG_SUCCESS);
41*5c9672edSIngo Weinhold request->SendReply(&reply);
42*5c9672edSIngo Weinhold } else {
43*5c9672edSIngo Weinhold BMessage reply(B_REG_ERROR);
44*5c9672edSIngo Weinhold reply.AddInt32("error", error);
45*5c9672edSIngo Weinhold request->SendReply(&reply);
46*5c9672edSIngo Weinhold }
47*5c9672edSIngo Weinhold }
48*5c9672edSIngo Weinhold
49*5c9672edSIngo Weinhold
50*5c9672edSIngo Weinhold void
NotifyWatchers(BMessage * message)51*5c9672edSIngo Weinhold PackageWatchingManager::NotifyWatchers(BMessage* message)
52*5c9672edSIngo Weinhold {
53*5c9672edSIngo Weinhold int32 event;
54*5c9672edSIngo Weinhold if (message->FindInt32("event", &event) != B_OK) {
55*5c9672edSIngo Weinhold WARNING("No event field in notification message\n");
56*5c9672edSIngo Weinhold return;
57*5c9672edSIngo Weinhold }
58*5c9672edSIngo Weinhold
59*5c9672edSIngo Weinhold uint32 eventMask;
60*5c9672edSIngo Weinhold switch (event) {
61*5c9672edSIngo Weinhold case B_INSTALLATION_LOCATION_PACKAGES_CHANGED:
62*5c9672edSIngo Weinhold eventMask = B_WATCH_PACKAGE_INSTALLATION_LOCATIONS;
63*5c9672edSIngo Weinhold break;
64*5c9672edSIngo Weinhold default:
65*5c9672edSIngo Weinhold WARNING("Invalid event: %" B_PRId32 "\n", event);
66*5c9672edSIngo Weinhold return;
67*5c9672edSIngo Weinhold }
68*5c9672edSIngo Weinhold
69*5c9672edSIngo Weinhold EventMaskWatcherFilter filter(eventMask);
70*5c9672edSIngo Weinhold fWatchingService.NotifyWatchers(message, &filter);
71*5c9672edSIngo Weinhold }
72*5c9672edSIngo Weinhold
73*5c9672edSIngo Weinhold
74*5c9672edSIngo Weinhold status_t
_AddWatcher(const BMessage * request)75*5c9672edSIngo Weinhold PackageWatchingManager::_AddWatcher(const BMessage* request)
76*5c9672edSIngo Weinhold {
77*5c9672edSIngo Weinhold BMessenger target;
78*5c9672edSIngo Weinhold uint32 eventMask;
79*5c9672edSIngo Weinhold status_t error;
80*5c9672edSIngo Weinhold if ((error = request->FindMessenger("target", &target)) != B_OK
81*5c9672edSIngo Weinhold || (error = request->FindUInt32("events", &eventMask)) != B_OK) {
82*5c9672edSIngo Weinhold return error;
83*5c9672edSIngo Weinhold }
84*5c9672edSIngo Weinhold
85*5c9672edSIngo Weinhold Watcher* watcher = new(std::nothrow) EventMaskWatcher(target, eventMask);
86*5c9672edSIngo Weinhold if (watcher == NULL || !fWatchingService.AddWatcher(watcher)) {
87*5c9672edSIngo Weinhold delete watcher;
88*5c9672edSIngo Weinhold return B_NO_MEMORY;
89*5c9672edSIngo Weinhold }
90*5c9672edSIngo Weinhold
91*5c9672edSIngo Weinhold return B_OK;
92*5c9672edSIngo Weinhold }
93*5c9672edSIngo Weinhold
94*5c9672edSIngo Weinhold
95*5c9672edSIngo Weinhold status_t
_RemoveWatcher(const BMessage * request)96*5c9672edSIngo Weinhold PackageWatchingManager::_RemoveWatcher(const BMessage* request)
97*5c9672edSIngo Weinhold {
98*5c9672edSIngo Weinhold BMessenger target;
99*5c9672edSIngo Weinhold status_t error;
100*5c9672edSIngo Weinhold if ((error = request->FindMessenger("target", &target)) != B_OK)
101*5c9672edSIngo Weinhold return error;
102*5c9672edSIngo Weinhold
103*5c9672edSIngo Weinhold if (!fWatchingService.RemoveWatcher(target))
104*5c9672edSIngo Weinhold return B_BAD_VALUE;
105*5c9672edSIngo Weinhold
106*5c9672edSIngo Weinhold return B_OK;
107*5c9672edSIngo Weinhold }
108