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