update tizen source
[framework/messaging/msg-service.git] / test_app / main.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31
32 /*==================================================================================================
33                                          INCLUDE FILES
34 ==================================================================================================*/
35 #include <iostream>
36 #include <list>
37 #include <vector>
38 #include <string>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <sys/syscall.h>
43 #include <glib.h>
44
45 using namespace std;
46
47 #include "MapiControl.h"
48 #include "MapiStorage.h"
49 #include "MapiTransport.h"
50 #include "MapiMessage.h"
51 #include "MsgTestStorage.h"
52 #include "MsgTestSetting.h"
53 #include "MsgTestThreadView.h"
54 #include "MsgTestConvert.h"
55 #include "main.h"
56 #include <stdio.h>
57
58 /*==================================================================================================
59                                          VARIABLES
60 ==================================================================================================*/
61 extern MSG_FOLDER_LIST_S g_folderList;
62
63 static MSG_HANDLE_T msgHandle = NULL;
64
65 GMainLoop *mainloop;
66
67 /*==================================================================================================
68                                      FUNCTION IMPLEMENTATION
69 ==================================================================================================*/
70 int main(void)
71 {
72         print("======================================");
73         print("==== Start Messaging FW Test App. ====");
74         print("======================================");
75
76         if (init_app() != MSG_SUCCESS)
77                 return -1;
78
79         return 0;
80 }
81
82
83 void print(const char* string)
84 {
85         cout << string << endl;
86 }
87
88
89 void print(int i)
90 {
91         cout << i << endl;
92 }
93
94
95 int get_tid()
96 {
97     return syscall(__NR_gettid);
98 }
99
100
101 void printMessageS(msg_message_t pNewMsg)
102 {
103         MSG_DEBUG("msgId = %d", msg_get_message_id(pNewMsg));
104         MSG_DEBUG("folderId = %d", msg_get_folder_id(pNewMsg));
105         MSG_DEBUG("msgType = %d", msg_get_message_type(pNewMsg));
106         MSG_DEBUG("nAddressCnt = %d", msg_get_address_count(pNewMsg));
107         MSG_DEBUG("addressList[0].threadId = %d", msg_get_ith_thread_id(pNewMsg, 0));
108         MSG_DEBUG("addressList[0].addressVal = %s", msg_get_ith_address(pNewMsg, 0));
109         MSG_DEBUG("addressList[0].displayname = %s", msg_get_ith_name(pNewMsg, 0));
110         MSG_DEBUG("displayTime = %s", ctime(msg_get_time(pNewMsg)));
111         MSG_DEBUG("networkStatus = %d", msg_get_network_status(pNewMsg));
112         MSG_DEBUG("bRead = %d", msg_is_read(pNewMsg));
113         MSG_DEBUG("bProtected = %d", msg_is_protected(pNewMsg));
114         MSG_DEBUG("priority = %d", msg_get_priority_info(pNewMsg));
115         MSG_DEBUG("dataSize = %d", msg_get_message_body_size(pNewMsg));
116
117         int msgType = msg_get_message_type(pNewMsg);
118
119         if(msgType==MSG_TYPE_MMS_NOTI || msgType==MSG_TYPE_MMS_JAVA || msgType==MSG_TYPE_MMS)
120                 MSG_DEBUG("msgData = %s", msg_mms_get_text_contents(pNewMsg));
121         else
122                 MSG_DEBUG("msgData = %s", msg_sms_get_message_body(pNewMsg));
123 }
124
125
126 void sentStatusCB(MSG_HANDLE_T Handle, MSG_SENT_STATUS_S *pStatus, void *pUserParam)
127 {
128         MSG_DEBUG("Receive SentStatus !!!!!!!!!!!!!!!!!");
129
130         if (pStatus->status == MSG_NETWORK_SEND_SUCCESS)
131                 MSG_DEBUG("reqId : %d  MSG SENT SUCCESS !!! ", pStatus->reqId);
132         else
133                 MSG_DEBUG("reqId : %d  MSG SENT FAIL !!! [%d]", pStatus->reqId, pStatus->status);
134
135         MSG_DEBUG("User Param [%s]", (char*)pUserParam);
136 }
137
138
139 void incomingSmsCB(MSG_HANDLE_T Handle, msg_message_t msg, void *pUserParam)
140 {
141         MSG_DEBUG("Receive New Message !!!!!!!!!!!!!!!!!");
142
143         printMessageS(msg);
144
145         MSG_DEBUG("User Param [%s]", (char*)pUserParam);
146 }
147
148
149 void incomingMmsConfCB(MSG_HANDLE_T Handle, msg_message_t msg, void *pUserParam)
150 {
151         if ( msg_get_network_status(msg) == MSG_NETWORK_RETRIEVE_SUCCESS)
152         {
153                 MSG_DEBUG("Receive New MMS Message !!!!!!!!!!!!!!!!!msg Id [%d]", msg_get_message_id(msg));
154         }
155         else
156                 MSG_DEBUG("MMS RETRIEVE FAIL !!!!!!! [%d]", msg_get_network_status(msg));
157 }
158
159
160 void appPortCB (MSG_HANDLE_T Handle, msg_message_t msg, void *pUserParam)
161 {
162         MSG_DEBUG("Receive App Port Message - Port Number [%d] !!!!!!!!!!!!!!!!!", msg_get_dest_port(msg));
163
164         printMessageS(msg);
165 }
166
167
168 void syncMLCB(MSG_HANDLE_T hMsgHandle, MSG_SYNCML_MESSAGE_TYPE_T msgType, const char* pPushBody, int PushBodyLen, const char* wspHeader, int wspHeaderLen, void *pUserParam)
169 {
170         MSG_DEBUG("syncMLCB() called");
171
172         MSG_DEBUG("msgType [%d]", msgType);
173
174 //      MSG_DEBUG("PushHeaderLen [%d]", PushHeaderLen);
175
176 //      MSG_DEBUG("pPushHeader [%s]", pPushHeader);
177
178         MSG_DEBUG("PushBodyLen [%d]", PushBodyLen);
179
180         MSG_DEBUG("[pPushBody]");
181         for (int i = 0; i < PushBodyLen; i++)
182         {
183                 printf("[%02x]", pPushBody[i]);
184         }
185         printf("\n\n");
186
187         MSG_SYNCML_MESSAGE_S syncMLMsg;
188
189         memset(&syncMLMsg, 0x00, sizeof(MSG_SYNCML_MESSAGE_S));
190
191         syncMLMsg.extId = 11;
192         syncMLMsg.pinCode = 1111;
193
194         syncMLMsg.msg = msg_new_message();
195
196         msg_set_folder_id(syncMLMsg.msg, MSG_INBOX_ID); //      syncMLMsg.msg.folderId = 1;
197         msg_set_message_type(syncMLMsg.msg, MSG_TYPE_SMS_SYNCML);
198         msg_set_network_status(syncMLMsg.msg, MSG_NETWORK_RECEIVED);    //syncMLMsg.msg.networkStatus = MSG_NETWORK_RECEIVED;
199         msg_set_direction_info(syncMLMsg.msg, MSG_DIRECTION_TYPE_MT);
200
201         msg_add_address(syncMLMsg.msg, "+1004", MSG_RECIPIENTS_TYPE_TO);
202
203         // setting received time
204         time_t t                =       time(NULL);
205         time_t utfTime  =       time(&t);
206         msg_set_time(syncMLMsg.msg, utfTime);
207
208         char msg_body[]="SyncML Message";
209         msg_sms_set_message_body(syncMLMsg.msg, msg_body, strlen(msg_body));
210
211         int err = msg_add_syncml_message(msgHandle, &syncMLMsg);
212
213         if (err != MSG_SUCCESS)
214         {
215                 MSG_DEBUG("MsgAddSyncMLMessage() Fail [%d]", err);
216                 return;
217         }
218 }
219
220
221 void syncMLOperationCB(MSG_HANDLE_T handle, int msgId, int extId, void *user_param)
222 {
223         MSG_DEBUG("syncMLOperationCB() called");
224
225         MSG_DEBUG("msgId [%d]", msgId);
226
227         MSG_DEBUG("extId [%d]", extId);
228
229         //SyncML operation proccess hsa to be place here.
230 }
231
232
233 void lbsCB(MSG_HANDLE_T hMsgHandle, const char* pPushHeader, const char* pPushBody, int pushBodyLen, void *pUserParam)
234 {
235         MSG_DEBUG("lbsCB() called");
236
237         MSG_DEBUG("pPushHeader : [%s]", pPushHeader);
238
239         MSG_DEBUG("pushBodyLen [%d]", pushBodyLen);
240
241         MSG_DEBUG("[pPushBody]");
242         for (int i = 0; i < pushBodyLen; i++)
243         {
244                 printf("[%02x]", pPushBody[i]);
245         }
246         printf("\n\n");
247 }
248
249
250 void storageChangeCB(MSG_HANDLE_T hMsgHandle, MSG_STORAGE_CHANGE_TYPE_T storageChangeType, MSG_MSGID_LIST_S *pMsgIdList, void *pUserParam)
251 {
252         MSG_DEBUG("storageChangeCB() called");
253
254         MSG_DEBUG("storageChangeType[%d]", storageChangeType);
255
256         MSG_DEBUG("pMsgIdList->nCount [%d]", pMsgIdList->nCount);
257
258         if (pMsgIdList->nCount > 0) {
259                 for (int i = 0; i < pMsgIdList->nCount; i++)
260                         MSG_DEBUG("pMsgIdList->msgIdList[%d] [%d]", i, pMsgIdList->msgIdList[i]);
261         }
262 }
263
264
265 MSG_ERROR_T init_app()
266 {
267         MSG_ERROR_T err = MSG_SUCCESS;
268
269         char str[TEST_APP_PIRNT_STR_LEN+1] = {0,};
270
271         err = msg_open_msg_handle(&msgHandle);
272
273         if (err != MSG_SUCCESS)
274         {
275                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_open_msg_handle() Fail [%d]", err);
276                 print(str);
277
278                 return err;
279         }
280
281         err = msg_reg_sent_status_callback(msgHandle, &sentStatusCB, (void*)"sent status callback");
282
283         if (err != MSG_SUCCESS)
284         {
285                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_sent_status_callback() Fail [%d]", err);
286                 print(str);
287
288                 return err;
289         }
290
291         err = msg_reg_sms_message_callback(msgHandle, &incomingSmsCB, 0, (void*)"sms message callback");
292
293         if (err != MSG_SUCCESS)
294         {
295                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_sms_message_callback() Fail [%d]", err);
296                 print(str);
297
298                 return err;
299         }
300
301         err = msg_reg_sms_message_callback(msgHandle, &appPortCB, 4010, (void*)"sms message callback with port");
302
303         if (err != MSG_SUCCESS)
304         {
305                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_sms_message_callback() Fail [%d]", err);
306                 print(str);
307
308                 return err;
309         }
310
311         err = msg_reg_syncml_message_callback(msgHandle, &syncMLCB, NULL);
312
313         if (err != MSG_SUCCESS)
314         {
315                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_syncml_message_callback() Fail [%d]", err);
316                 print(str);
317
318                 return err;
319         }
320
321         err = msg_reg_syncml_message_operation_callback(msgHandle, &syncMLOperationCB, NULL);
322
323         if (err != MSG_SUCCESS)
324         {
325                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_syncml_message_operation_callback() Fail [%d]", err);
326                 print(str);
327
328                 return err;
329         }
330
331         err = msg_reg_lbs_message_callback(msgHandle, &lbsCB, NULL);
332
333         if (err != MSG_SUCCESS)
334         {
335                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_lbs_message_callback() Fail [%d]", err);
336                 print(str);
337
338                 return err;
339         }
340
341         err = msg_reg_mms_conf_message_callback(msgHandle, &incomingMmsConfCB, NULL, NULL);
342
343         if (err != MSG_SUCCESS)
344         {
345                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_mms_conf_message_callback() Fail [%d]", err);
346                 print(str);
347
348                 return err;
349         }
350
351         err = msg_reg_storage_change_callback(msgHandle, &storageChangeCB, NULL);
352
353         if (err != MSG_SUCCESS)
354         {
355                 snprintf(str, TEST_APP_PIRNT_STR_LEN+1, "msg_reg_storage_change_callback() Fail [%d]", err);
356                 print(str);
357
358                 return err;
359         }
360
361         pthread_t tid;
362
363         if (pthread_create(&tid, NULL, show_menu, NULL) != 0)
364         {
365                 MSG_DEBUG("MsgInitThread not invoked: %s", strerror(errno));
366                 return MSG_ERR_UNKNOWN;
367         }
368
369         mainloop = g_main_loop_new(NULL, FALSE);
370
371         MSG_DEBUG("Entering GMain Loop to Receive Notifications...");
372
373         g_main_loop_run(mainloop);
374
375         return err;
376 }
377
378
379 void* show_menu(void*)
380 {
381         MSG_ERROR_T err = MSG_SUCCESS;
382
383         char menu[3];
384
385         char strPrint[TEST_APP_PIRNT_STR_LEN+1] = {0,};
386
387         // Get Message Count
388         MSG_COUNT_INFO_S countInfo;
389
390         do
391         {
392                 msg_release_folder_list(&g_folderList);
393
394                 // Get Folder List
395                 err = msg_get_folder_list(msgHandle, &g_folderList);
396
397                 if (err != MSG_SUCCESS)
398                 {
399                         MSG_DEBUG("MsgGetFolderList() Fail [%d]", err);
400                         return (void*)1;
401                 }
402
403                 system ("clear");
404
405                 print("======================================");
406                 print("============== Box List ==============");
407                 print("======================================");
408
409                 for (int i = 0; i < g_folderList.nCount; i++)
410                 {
411                         if (msg_count_message(msgHandle, g_folderList.folderInfo[i].folderId, &countInfo) != MSG_SUCCESS)
412                                 return (void*)1;
413
414                         memset(strPrint, 0x00, sizeof(strPrint));
415                         snprintf(strPrint, TEST_APP_PIRNT_STR_LEN+1, "[%d] %s \t [%d/%d] [SMS %d/MMS %d]",
416                                 g_folderList.folderInfo[i].folderId, g_folderList.folderInfo[i].folderName, countInfo.nUnreadCnt, countInfo.nUnreadCnt + countInfo.nReadCnt, countInfo.nSms, countInfo.nMms);
417                         print(strPrint);
418                 }
419
420                 print("======================================");
421                 print("================ Menu ================");
422                 print("======================================");
423                 print("[C] Create Message");
424                 print("[S] Setting");
425                 print("[A] Add New Folder");
426                 print("[D] Delete a Folder");
427                 print("[U] Update Folder Name");
428                 print("[V] Convert(Encode/Decode) Charset");
429                 print("[M] Msg Generator");
430                 print("[T] Thread View Test");
431                 print("[G] Search Message");
432                 print("[Q] Quit");
433                 print("======================================");
434
435                 print("Input : ");
436
437                 memset(menu, 0x00, sizeof(menu));
438                 cin.getline(menu, 3);
439
440                 run_app(menu);
441         }
442         while (strcmp(menu, "Q"));
443
444         msg_release_folder_list(&g_folderList);
445
446         msg_close_msg_handle(&msgHandle);
447
448         if (msgHandle != NULL)
449         {
450                 MSG_DEBUG("msgHandle is not NULL [%p]", msgHandle);
451         }
452
453         if (g_main_loop_is_running(mainloop))
454                 g_main_loop_quit(mainloop);
455
456         print("======================================");
457         print("==== End Messaging FW Test App. Bye===");
458         print("======================================");
459
460         exit(0);
461 }
462
463
464 void run_app(char *pMenu)
465 {
466         if (!strcmp(pMenu, "C") || !strcmp(pMenu, "c"))
467         {
468                 MsgTestAddMessage(msgHandle);
469         }
470         else if (!strcmp(pMenu, "S") || !strcmp(pMenu, "s"))
471         {
472                 MsgTestSettingMain(msgHandle);
473         }
474         else if (!strcmp(pMenu, "A") || !strcmp(pMenu, "a"))
475         {
476                 MsgTestAddFolder(msgHandle);
477         }
478         else if (!strcmp(pMenu, "D") || !strcmp(pMenu, "d"))
479         {
480                 MsgTestDeleteFolder(msgHandle);
481         }
482         else if (!strcmp(pMenu, "U") || !strcmp(pMenu, "u"))
483         {
484                 MsgTestUpdateFolder(msgHandle);
485         }
486         else if (!strcmp(pMenu, "V") || !strcmp(pMenu, "v"))
487         {
488 //              MsgTestConvertMain();
489         }
490         else if (!strcmp(pMenu, "M") || !strcmp(pMenu, "m"))
491         {
492                 MsgTestMsgGen(msgHandle);
493         }
494         else if (!strcmp(pMenu, "T") || !strcmp(pMenu, "t"))
495         {
496                 MsgThreadViewMain(msgHandle);
497         }
498         else if (!strcmp(pMenu, "G") || !strcmp(pMenu, "g"))
499         {
500                 MsgSearchViewMain(msgHandle);
501         }
502         else if (!strcmp(pMenu, "Q") || !strcmp(pMenu, "q"))
503         {
504                 return;
505         }
506         else
507         {
508                 int folderId = atoi(pMenu);
509
510                 if (folderId > 0 && folderId < 100)
511                 {
512                         // Query Msg List in Msg Box
513                         MsgTestGetMessageList(msgHandle, folderId);
514                 }
515                 else
516                         print("Not Supported");
517         }
518 }
519