Fix build dependency (notification-service-devel)
[platform/core/appfw/notification-service.git] / send_notification.c
1 #include <Ecore.h>
2 #include <Ecore_Getopt.h>
3 #include <notification.h>
4 #include <unistd.h>
5
6 const char *error_to_string(notification_error_e error)
7 {
8     if (error == NOTIFICATION_ERROR_INVALID_DATA)
9         return "NOTIFICATION_ERROR_INVALID_DATA";
10     if (error == NOTIFICATION_ERROR_NO_MEMORY)
11         return "NOTIFICATION_ERROR_NO_MEMORY";
12     if (error == NOTIFICATION_ERROR_FROM_DB)
13         return "NOTIFICATION_ERROR_FROM_DB";
14     if (error == NOTIFICATION_ERROR_ALREADY_EXIST_ID)
15         return "NOTIFICATION_ERROR_ALREADY_EXIST_ID";
16     if (error == NOTIFICATION_ERROR_FROM_DBUS)
17         return "NOTIFICATION_ERROR_FROM_DBUS";
18     if (error == NOTIFICATION_ERROR_NOT_EXIST_ID)
19         return "NOTIFICATION_ERROR_NOT_EXIST_ID";
20     if (error == NOTIFICATION_ERROR_IO)
21         return "NOTIFICATION_ERROR_IO";
22     if (error == NOTIFICATION_ERROR_SERVICE_NOT_READY)
23         return "NOTIFICATION_ERROR_SERVICE_NOT_READY";
24     if (error == NOTIFICATION_ERROR_NONE)
25         return "NOTIFICATION_ERROR_NONE";
26
27     return "UNHANDLED ERROR";
28 }
29
30 static Eina_Bool remove_all(const Ecore_Getopt *parser,
31                             const Ecore_Getopt_Desc *desc,
32                             const char *str,
33                             void *data,
34                             Ecore_Getopt_Value *storage)
35 {
36     notification_error_e err = NOTIFICATION_ERROR_NONE;
37
38     err = notification_delete_all_by_type("SEND_TEST_PKG", NOTIFICATION_TYPE_NOTI);
39     if (err != NOTIFICATION_ERROR_NONE) {
40         fprintf(stderr, "Unable to remove notifications: %s\n", error_to_string(err));
41         exit(-1);
42     }
43
44     exit(0);
45
46     // will never reach here
47     return 0;
48 }
49
50 static const Ecore_Getopt optdesc = {
51     "send notification test utility",
52     NULL,
53     "0.0",
54     "(C) 2013 Intel Corp",
55     "Flora",
56     "Test utility for sending notifications",
57     0,
58     {
59         ECORE_GETOPT_STORE_STR('t', "title", "Title"),
60         ECORE_GETOPT_STORE_STR('c', "content", "Content"),
61         ECORE_GETOPT_STORE_STR('i', "icon", "Path to icon"),
62         ECORE_GETOPT_STORE_STR('m', "image", "Path to image"),
63         ECORE_GETOPT_STORE_INT('y', "imagetype", "Image type enum value"),
64         ECORE_GETOPT_CALLBACK_NOARGS('r', "removeall", "Remove all notifications", remove_all, NULL),
65         ECORE_GETOPT_HELP('h', "help"),
66         ECORE_GETOPT_SENTINEL
67     }
68 };
69
70 int
71 main(int argc, char **argv)
72 {
73     char *title = NULL;
74     char *content = NULL;
75     char *icon = NULL;
76     char *image = NULL;
77     int imageType = 0;
78     Eina_Bool remove = EINA_FALSE;
79     notification_h noti = NULL;
80     notification_error_e err = NOTIFICATION_ERROR_NONE;
81
82     Ecore_Getopt_Value values[] = {
83         ECORE_GETOPT_VALUE_STR(title),
84         ECORE_GETOPT_VALUE_STR(content),
85         ECORE_GETOPT_VALUE_STR(icon),
86         ECORE_GETOPT_VALUE_STR(image),
87         ECORE_GETOPT_VALUE_INT(imageType),
88         ECORE_GETOPT_VALUE_NONE
89     };
90
91     if (!ecore_init()) {
92         fprintf(stderr, "ERROR: Cannot init Ecore!\n");
93         return -1;
94     }
95
96     if (ecore_getopt_parse(&optdesc, values, argc, argv) < 0) {
97         fprintf(stderr, "Parsing arguments failed!\n");
98         return -1;
99     }
100
101     noti = notification_new(NOTIFICATION_TYPE_NOTI,
102                             NOTIFICATION_GROUP_ID_NONE,
103                             NOTIFICATION_PRIV_ID_NONE);
104     if (noti == NULL) {
105         fprintf(stderr, "Failed to create notification: %s\n", error_to_string(err));
106         return -1;
107     }
108
109     err = notification_set_pkgname(noti, "SEND_TEST_PKG");
110     if (err != NOTIFICATION_ERROR_NONE) {
111         fprintf(stderr, "Unable to set pkgname: %s\n", error_to_string(err));
112         return -1;
113     }
114
115     err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
116                                 title ? title : "Default Title",
117                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
118     if (err != NOTIFICATION_ERROR_NONE) {
119         fprintf(stderr, "Unable to set notification title: %s\n", error_to_string(err));
120         return -1;
121     }
122
123     err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
124                                 content ? content : "Default Content",
125                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
126     if (err != NOTIFICATION_ERROR_NONE) {
127         fprintf(stderr, "Unable to set notification content: %s\n", error_to_string(err));
128         return -1;
129     }
130
131     if (icon) {
132         err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon);
133         if (err != NOTIFICATION_ERROR_NONE) {
134             fprintf(stderr, "Unable to set notification icon path: %s\n", error_to_string(err));
135             return -1;
136         }
137     }
138
139     if (image) {
140         err = notification_set_image(noti, imageType, image);
141         if (err != NOTIFICATION_ERROR_NONE) {
142             fprintf(stderr, "Unable to set notification image path: %s\n", error_to_string(err));
143             return -1;
144         }
145     }
146
147     err = notification_insert(noti, NULL);
148     if (err != NOTIFICATION_ERROR_NONE) {
149         fprintf(stderr, "Unable to insert notification: %s\n", error_to_string(err));
150         return -1;
151     }
152
153     fprintf(stdout, "Sent Notification > %s : %s : %s : %s : %i\n",
154             title, content, icon, image, imageType);
155     return 0;
156 }
157