xref: /haiku/src/kits/storage/AddOnMonitor.cpp (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2004-2010, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Andrew Bachmann
7  */
8 
9 
10 #include "AddOnMonitor.h"
11 #include "AddOnMonitorHandler.h"
12 #include <Message.h>
13 #include <MessageRunner.h>
14 #include <Messenger.h>
15 #include <stdio.h>
16 
17 
18 AddOnMonitor::AddOnMonitor()
19 	:
20 	BLooper("AddOnMonitor"),
21 	fInitCheck(B_NO_INIT),
22 	fPulseRunner(NULL)
23 {
24 }
25 
26 
27 AddOnMonitor::AddOnMonitor(AddOnMonitorHandler* handler)
28 	:
29 	BLooper("AddOnMonitor"),
30 	fInitCheck(B_NO_INIT),
31 	fPulseRunner(NULL)
32 {
33 	SetHandler(handler);
34 
35 	thread_id id = Run();
36 	if (id < 0) {
37 		fInitCheck = (status_t)id;
38 		fprintf(stderr, "AddOnMonitor() : bad id returned by Run()\n");
39 		return;
40 	}
41 }
42 
43 
44 AddOnMonitor::~AddOnMonitor()
45 {
46 	delete fPulseRunner;
47 }
48 
49 
50 status_t
51 AddOnMonitor::InitCheck()
52 {
53 	return fInitCheck;
54 }
55 
56 
57 void
58 AddOnMonitor::SetHandler(AddOnMonitorHandler* handler)
59 {
60 	if (handler == NULL)
61 		return;
62 
63 	AddHandler(handler);
64 	SetPreferredHandler(handler);
65 
66 	delete fPulseRunner;
67 	fPulseRunner = NULL;
68 
69 	status_t status;
70 	BMessenger messenger(handler, this, &status);
71 	if (status != B_OK) {
72 		fInitCheck = status;
73 		return;
74 	}
75 
76 	BMessage pulseMessage(B_PULSE);
77 	fPulseRunner = new(std::nothrow) BMessageRunner(messenger, &pulseMessage,
78 		1000000);
79 	if (fPulseRunner == NULL) {
80 		fInitCheck = B_NO_MEMORY;
81 		return;
82 	}
83 
84 	status = fPulseRunner->InitCheck();
85 	if (status != B_OK) {
86 		fInitCheck = status;
87 		fprintf(stderr, "AddOnMonitor() : bad status returned by "
88 			"fPulseRunner->InitCheck()\n");
89 		return;
90 	}
91 
92 	// Send an initial message to process added directories immediately
93 	messenger.SendMessage(&pulseMessage);
94 
95 	fInitCheck = B_OK;
96 }
97