Fix the list emptying logic.
[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 quit = EINA_FALSE;
79     Eina_Bool remove = EINA_FALSE;
80     notification_h noti = NULL;
81     notification_error_e err = NOTIFICATION_ERROR_NONE;
82
83     Ecore_Getopt_Value values[] = {
84         ECORE_GETOPT_VALUE_STR(title),
85         ECORE_GETOPT_VALUE_STR(content),
86         ECORE_GETOPT_VALUE_STR(icon),
87         ECORE_GETOPT_VALUE_STR(image),
88         ECORE_GETOPT_VALUE_INT(imageType),
89         ECORE_GETOPT_VALUE_NONE,
90         ECORE_GETOPT_VALUE_BOOL(quit)
91     };
92
93     if (!ecore_init()) {
94         fprintf(stderr, "ERROR: Cannot init Ecore!\n");
95         return -1;
96     }
97
98     if (ecore_getopt_parse(&optdesc, values, argc, argv) < 0) {
99         fprintf(stderr, "Parsing arguments failed!\n");
100         return -1;
101     }
102
103     if (quit)
104        return 0;
105
106     noti = notification_new(NOTIFICATION_TYPE_NOTI,
107                             NOTIFICATION_GROUP_ID_NONE,
108                             NOTIFICATION_PRIV_ID_NONE);
109     if (noti == NULL) {
110         fprintf(stderr, "Failed to create notification: %s\n", error_to_string(err));
111         return -1;
112     }
113
114     err = notification_set_pkgname(noti, "SEND_TEST_PKG");
115     if (err != NOTIFICATION_ERROR_NONE) {
116         fprintf(stderr, "Unable to set pkgname: %s\n", error_to_string(err));
117         return -1;
118     }
119
120     err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
121                                 title ? title : "Default Title",
122                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
123     if (err != NOTIFICATION_ERROR_NONE) {
124         fprintf(stderr, "Unable to set notification title: %s\n", error_to_string(err));
125         return -1;
126     }
127
128     err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
129                                 content ? content : "Default Content",
130                                 NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
131     if (err != NOTIFICATION_ERROR_NONE) {
132         fprintf(stderr, "Unable to set notification content: %s\n", error_to_string(err));
133         return -1;
134     }
135
136     if (icon) {
137         err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, icon);
138         if (err != NOTIFICATION_ERROR_NONE) {
139             fprintf(stderr, "Unable to set notification icon path: %s\n", error_to_string(err));
140             return -1;
141         }
142     }
143
144     if (image) {
145         err = notification_set_image(noti, imageType, image);
146         if (err != NOTIFICATION_ERROR_NONE) {
147             fprintf(stderr, "Unable to set notification image path: %s\n", error_to_string(err));
148             return -1;
149         }
150     }
151
152     err = notification_insert(noti, NULL);
153     if (err != NOTIFICATION_ERROR_NONE) {
154         fprintf(stderr, "Unable to insert notification: %s\n", error_to_string(err));
155         return -1;
156     }
157
158     fprintf(stdout, "Sent Notification > %s : %s : %s : %s : %i\n",
159             title, content, icon, image, imageType);
160     return 0;
161 }
162