Apply tizen coding rule
[platform/core/api/messages.git] / test / messages_incoming_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <dlog.h>
4
5 #include <glib.h>
6
7 #include <messages.h>
8
9 #define ERROR_CHECK(ret) \
10         if (MESSAGES_ERROR_NONE != ret) { \
11                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "%d: error, ret=%d \n", __LINE__, ret); \
12                 return(0); \
13         }
14
15 static GMainLoop *mainloop;
16
17 static void sig_quit(int signo)
18 {
19         if (mainloop)
20                 g_main_loop_quit(mainloop);
21 }
22
23 void incoming_cb(messages_message_h msg, void *user_data)
24 {
25         int ret;
26         messages_message_type_e msgType;
27         char *text;
28         int i, nAddr;
29         char *addr = NULL;
30         time_t time;
31         struct tm ptm;
32
33         dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Incoming Message\n");
34
35         messages_get_address_count(msg, &nAddr);
36         for (i = 0; i < nAddr; i++) {
37                 ret = messages_get_address(msg, i, &addr, NULL);
38                 if (MESSAGES_ERROR_NONE == ret) {
39                         dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Address[%d]: %s\n", i, addr);
40                         free(addr);
41                 }
42         }
43
44         ret = messages_get_text(msg, &text);
45         if (MESSAGES_ERROR_NONE == ret) {
46                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Text: %s\n", text);
47                 free(text);
48         } else {
49                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "%d: error, ret=%d\n", __LINE__, ret);
50         }
51
52         messages_get_message_type(msg, &msgType);
53         switch (msgType) {
54         case MESSAGES_TYPE_SMS:
55                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Type: SMS\n");
56                 break;
57         case MESSAGES_TYPE_MMS:
58                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Type: MMS\n");
59                 break;
60         default:
61                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Type: Unknown\n");
62                 break;
63         }
64
65         messages_get_time(msg, &time);
66         char buf[50];
67         if (ctime_r(&time, buf))
68                 dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "Time: %d, %s", (int)time, ctime_r(&time, buf));
69
70         gmtime_r(&time, &ptm);
71         dlog_print(DLOG_DEBUG, "MESSAGE_TEST", "gmtime test: %d.%d.%d %d:%d:%d \n", ptm.tm_year, ptm.tm_mon, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
72 }
73
74 int main(int argc, char *argv[])
75 {
76         int ret;
77         messages_service_h svc;
78
79         signal(SIGINT, sig_quit);
80         signal(SIGTERM, sig_quit);
81         signal(SIGQUIT, sig_quit);
82         mainloop = g_main_loop_new(NULL, FALSE);
83
84         ret = messages_open_service(&svc);
85         ERROR_CHECK(ret);
86
87         messages_set_message_incoming_cb(svc, incoming_cb, NULL);
88
89         g_main_loop_run(mainloop);
90         g_main_loop_unref(mainloop);
91
92         messages_close_service(svc);
93
94         return 0;
95 }