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