xref: /haiku/src/servers/notification/NotificationServer.cpp (revision 44d19f4d32b8f7e9c01f00294c87ca5cc2e057f7)
1 /*
2  * Copyright 2010-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
7  */
8 
9 
10 #include "NotificationServer.h"
11 
12 #include <stdlib.h>
13 
14 #include <Beep.h>
15 #include <Notifications.h>
16 #include <PropertyInfo.h>
17 
18 #include "NotificationWindow.h"
19 
20 
21 const char* kSoundNames[] = {
22 	"Information notification",
23 	"Important notification",
24 	"Error notification",
25 	"Progress notification",
26 	NULL
27 };
28 
29 
30 NotificationServer::NotificationServer(status_t& error)
31 	:
32 	BServer(kNotificationServerSignature, true, &error)
33 {
34 }
35 
36 
37 NotificationServer::~NotificationServer()
38 {
39 }
40 
41 
42 void
43 NotificationServer::ReadyToRun()
44 {
45 	fWindow = new NotificationWindow();
46 }
47 
48 
49 void
50 NotificationServer::MessageReceived(BMessage* message)
51 {
52 	switch (message->what) {
53 		case kNotificationMessage:
54 		{
55 			// Skip this message if we don't have the window
56 			if (!fWindow)
57 				return;
58 
59 			int32 type = 0;
60 
61 			// Emit a sound for this event
62 			if (message->FindInt32("type", &type) == B_OK) {
63 				if (type < (int32)(sizeof(kSoundNames) / sizeof(const char*)))
64 					system_beep(kSoundNames[type]);
65 			}
66 
67 			// Let the notification window handle this message
68 			BMessenger(fWindow).SendMessage(message);
69 			break;
70 		}
71 		default:
72 			BApplication::MessageReceived(message);
73 	}
74 }
75 
76 
77 status_t
78 NotificationServer::GetSupportedSuites(BMessage* msg)
79 {
80 	msg->AddString("suites", "suite/x-vnd.Haiku-notification_server");
81 
82 	BPropertyInfo info(main_prop_list);
83 	msg->AddFlat("messages", &info);
84 
85 	return BApplication::GetSupportedSuites(msg);
86 }
87 
88 
89 BHandler*
90 NotificationServer::ResolveSpecifier(BMessage* msg, int32 index,
91 	BMessage* spec, int32 from, const char* prop)
92 {
93 	BPropertyInfo info(main_prop_list);
94 
95 	if (strcmp(prop, "message") == 0) {
96 		BMessenger messenger(fWindow);
97 		messenger.SendMessage(msg, fWindow);
98 		return NULL;
99 	}
100 
101 	return BApplication::ResolveSpecifier(msg, index, spec, from, prop);
102 }
103 
104 
105 // #pragma mark -
106 
107 
108 int
109 main(int argc, char* argv[])
110 {
111 	int32 i = 0;
112 
113 	// Add system sounds
114 	while (kSoundNames[i] != NULL)
115 		add_system_beep_event(kSoundNames[i++], 0);
116 
117 	// Start!
118 	status_t error;
119 	NotificationServer server(error);
120 	if (error == B_OK)
121 		server.Run();
122 
123 	return error == B_OK ? EXIT_SUCCESS : EXIT_FAILURE;
124 }
125