Modify filter related operations.
[platform/core/messaging/msg-service.git] / framework / main.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20 #include "MsgDebug.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgMemory.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgSettingHandler.h"
27 #include "MsgStorageHandler.h"
28 #include "MsgSubmitHandler.h"
29 #include "MsgDeliverHandler.h"
30 #include "MsgTransManager.h"
31 #include "MsgStorageTypes.h"
32 #include "MsgSoundPlayer.h"
33
34 #include <errno.h>
35 #include <glib.h>
36 #include <dbus/dbus-glib.h>
37 #include <sys/stat.h>
38 #include <wait.h>
39
40 static GMainLoop* mainloop = NULL;
41
42
43 /*==================================================================================================
44                                      DEFINES
45 ==================================================================================================*/
46 #define MSG_MOBILE_TRACKER_MSG "Mobile Tracker Alert"
47
48
49 /*==================================================================================================
50                                      FUNCTION IMPLEMENTATION
51 ==================================================================================================*/
52 msg_error_t InitMmsDir()
53 {
54         if (mkdir(MSG_DATA_ROOT_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
55         {
56                 if (errno == EEXIST)
57                 {
58                         MSG_DEBUG("The %s already exists", MSG_DATA_ROOT_PATH);
59                 }
60                 else
61                 {
62                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_ROOT_PATH);
63                         return MSG_ERR_DB_MAKE_DIR;
64                 }
65         }
66
67         if (mkdir(MSG_SMIL_FILE_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
68         {
69                 if (errno == EEXIST)
70                 {
71                         MSG_DEBUG("The %s already exists", MSG_SMIL_FILE_PATH);
72                 }
73                 else
74                 {
75                         MSG_DEBUG("Error while mkdir %s", MSG_SMIL_FILE_PATH);
76                         return MSG_ERR_DB_MAKE_DIR;
77                 }
78         }
79
80         if (mkdir(MSG_DATA_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0)
81         {
82                 if (errno == EEXIST)
83                 {
84                         MSG_DEBUG("The %s already exists", MSG_DATA_PATH);
85                 }
86                 else
87                 {
88                         MSG_DEBUG("Error while mkdir %s", MSG_DATA_PATH);
89                         return MSG_ERR_DB_MAKE_DIR;
90                 }
91         }
92
93         if (mkdir(MSG_THUMBNAIL_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
94                 if (errno == EEXIST) {
95                         MSG_DEBUG("The %s already exists.", MSG_THUMBNAIL_PATH);
96                 } else {
97                         MSG_DEBUG(" Error while mkdir %s", MSG_THUMBNAIL_PATH);
98                         return MSG_ERR_DB_MAKE_DIR;
99                 }
100         }
101
102         if (mkdir(MSG_IPC_DATA_PATH, S_IRWXU ) < 0)
103         {
104                 if (errno == EEXIST)
105                 {
106                         MSG_DEBUG("The %s already exists", MSG_IPC_DATA_PATH);
107
108                         // if IPC data path is already exist, clear all files in folder.
109                         char exeStr[1024];
110                         snprintf(exeStr, 1024, "rm %s*.DATA", MSG_IPC_DATA_PATH);
111                         system(exeStr);
112                 }
113                 else
114                 {
115                         MSG_DEBUG("Error while mkdir %s", MSG_IPC_DATA_PATH);
116                         return MSG_ERR_DB_MAKE_DIR;
117                 }
118         }
119
120         chmod( MSG_IPC_DATA_PATH, S_IRWXU | S_IRWXG ); //public shared file: pass data by file
121         chown( MSG_IPC_DATA_PATH, 0, 6502 );
122
123         return MSG_SUCCESS;
124 }
125
126
127 void SendMobileTrackerMsg()
128 {
129         MSG_BEGIN();
130
131         bool bEnabled = false;
132
133         if (MsgSettingGetBool(VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL, &bEnabled) < 0)
134         {
135                 MSG_DEBUG("Can't read VCONFKEY_SETAPPL_FIND_MY_MOBILE_SIM_CHANGE_ALERT_BOOL");
136                 return;
137         }
138
139         if (bEnabled == false)
140         {
141                 MSG_DEBUG("Mobile Tracker Option [%d]", bEnabled);
142                 return;
143         }
144         // to wait modem init
145         // temporary code.
146         else
147         {
148                 MSG_DEBUG("Waiting for modem ready, 22 sec.");
149                 sleep(22);
150         }
151
152         MSG_REQUEST_INFO_S req = {0};
153
154         req.sendOptInfo.bSetting = false;
155
156         req.msgInfo.msgId = 0;
157         req.msgInfo.threadId = 0;
158         req.msgInfo.folderId = MSG_DRAFT_ID;
159         req.msgInfo.msgType.mainType = MSG_SMS_TYPE;
160         req.msgInfo.msgType.subType = MSG_NORMAL_SMS;
161         req.msgInfo.msgType.classType = MSG_CLASS_NONE;
162         req.msgInfo.storageId = MSG_STORAGE_PHONE;
163         req.msgInfo.displayTime = 0;
164         req.msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
165         req.msgInfo.encodeType = MSG_ENCODE_AUTO;
166         req.msgInfo.bRead = false;
167         req.msgInfo.bProtected = false;
168         req.msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL;
169         req.msgInfo.direction = MSG_DIRECTION_TYPE_MO;
170         req.msgInfo.msgPort.valid = false;
171         req.msgInfo.bTextSms = true;
172
173         // Get Message Text
174         char* msgText = NULL;
175
176         msgText = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_MESSAGE_STR);
177
178         if (msgText == NULL)
179         {
180                 MSG_DEBUG("Assign Default Msg");
181                 msgText = strdup(MSG_MOBILE_TRACKER_MSG);
182         }
183
184         MSG_DEBUG("mobile tracker msg : [%s]", msgText);
185
186         req.msgInfo.dataSize = strlen(msgText);
187         strncpy(req.msgInfo.msgText, msgText, req.msgInfo.dataSize);
188
189         // Get Address List
190         char *orgRecipientList = NULL;
191
192         orgRecipientList = MsgSettingGetString(VCONFKEY_SETAPPL_FIND_MY_MOBILE_RECIPIENTS_STR);
193
194         if (orgRecipientList == NULL)
195         {
196                 MSG_DEBUG("recipient list is NULL");
197                 return;
198         }
199
200         int len = strlen(orgRecipientList);
201         char recipientList[len + 1];
202
203         memset(recipientList, 0, len + 1);
204         memcpy(recipientList, orgRecipientList, len);
205
206         MSG_DEBUG("recipient list : [%s]", recipientList);
207
208         req.msgInfo.nAddressCnt = 1;
209
210         char *token;
211
212         token = strtok(recipientList, "|");
213
214         MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
215         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
216
217         msg_error_t err = MSG_SUCCESS;
218
219         while (token)
220         {
221                 req.msgInfo.addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
222                 req.msgInfo.addressList[0].recipientType = MSG_RECIPIENTS_TYPE_TO;
223
224                 memset(req.msgInfo.addressList[0].addressVal, 0x00, MAX_ADDRESS_VAL_LEN);
225                 strncpy(req.msgInfo.addressList[0].addressVal, token, MAX_ADDRESS_VAL_LEN);
226
227                 MSG_DEBUG("address : [%s]", req.msgInfo.addressList[0].addressVal);
228
229                 if (plg != NULL)
230                 {
231                         MSG_DEBUG("mobile tracker msg : [%s]", msgText);
232
233                         err = plg->submitReq(&req);
234
235                         if (err != MSG_SUCCESS)
236                         {
237                                 MSG_DEBUG("fail to send mobile tracker msg : [%d]", err);
238                                 break;
239                         }
240                 }
241
242                 token = strtok(NULL, "|");
243         }
244
245         if (msgText) {
246                 free(msgText);
247                 msgText = NULL;
248         }
249
250         if (orgRecipientList) {
251                 free(orgRecipientList);
252                 orgRecipientList = NULL;
253         }
254
255
256         MSG_END();
257
258         return;
259 }
260
261
262 void* StartMsgServer(void*)
263 {
264         try
265         {
266                 MsgTransactionManager::instance()->run();
267         }
268         catch (MsgException& e)
269         {
270                 MSG_FATAL("%s", e.what());
271         }
272         catch (exception& e)
273         {
274                 MSG_FATAL("%s", e.what());
275         }
276
277         if (g_main_loop_is_running(mainloop))
278                 g_main_loop_quit(mainloop);
279
280         return (void*)0;
281 }
282
283
284 void* InitMsgServer(void*)
285 {
286         msg_error_t err = MSG_SUCCESS;
287
288         MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
289         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
290
291         // storage handler initialize
292         err = MsgStoInitDB(false);
293
294         if (err != MSG_SUCCESS) {
295                 MSG_DEBUG("FAIL TO INITIALIZE STORAGE HANDLER [%d]", err);
296         }
297
298         // Set Msg FW Ready Flag
299         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true);
300         MSG_DEBUG("### VCONFKEY_MSG_SERVER_READY ###");
301
302         if (plg == NULL) {
303                 MSG_DEBUG("No plugin for %d type", mainType);
304
305                 MsgReleaseMemory();
306                 return (void*)0;
307         }
308
309         MSG_SIM_STATUS_T simStatus = MSG_SIM_STATUS_NORMAL;
310
311         // Check Sim Status
312         if (plg->checkSimStatus(&simStatus) == MSG_SUCCESS) {
313
314                 // Add the change of SIM to vconf
315                 if (MsgSettingSetInt(MSG_SIM_CHANGED, (int)simStatus) != MSG_SUCCESS) {
316                         MSG_DEBUG("Error to set config data [%s]", MSG_SIM_CHANGED);
317                 }
318
319                 if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
320                         // Check Device Status
321                         if (plg->checkDeviceStatus() != MSG_SUCCESS) {
322                                 MSG_DEBUG("checkDeviceStatus() error");
323
324                                 MsgReleaseMemory();
325
326                                 return (void*)0;
327                         }
328                 }
329
330                 // Init SIM Message
331                 if (MsgInitSimMessage(simStatus) != MSG_SUCCESS) {
332                         MSG_DEBUG("Fail to Initialize SIM Message");
333                 }
334
335                 // Init SIM Configuration
336                 if (MsgInitSimConfig(simStatus) != MSG_SUCCESS) {
337                         MSG_DEBUG("Fail to Initialize SIM Configuration");
338                 }
339
340                 // Add SendMobileTrackerMsg() to GMainLoop
341                 if (simStatus == MSG_SIM_STATUS_CHANGED) {
342                         MSG_DEBUG("Send Mobile Tracker Message");
343
344                         SendMobileTrackerMsg();
345                 }
346         } else {
347                 MSG_DEBUG("checkSimStatus() error");
348         }
349
350         MsgReleaseMemory();
351
352         // Try to connect contact server  if it is not opened.
353         MsgOpenContactSvc();
354
355         // Register Callback to get the change of contact
356         MsgInitContactSvc(&MsgContactChangedCallback);
357
358         MsgSoundInitRepeatAlarm();
359
360         return (void*)0;
361 }
362
363
364 static gboolean InitThreadFunc(void* pData)
365 {
366         MSG_BEGIN();
367
368         pthread_t initThreadId;
369
370         // initialize msg fw
371         if (pthread_create(&initThreadId, NULL, InitMsgServer, NULL) != 0)
372         {
373                 MSG_DEBUG("InitMsgFw not invoked: %s", strerror(errno));
374                 return -1;
375         }
376
377         pthread_detach(initThreadId);
378
379         MSG_END();
380
381         return FALSE;
382 }
383
384
385 int main(void)
386 {
387 #if !GLIB_CHECK_VERSION(2, 31, 0)
388         g_thread_init(NULL);
389 #endif
390         dbus_g_thread_init();
391
392 ////////////////////////////////////
393
394 /// set to ignore child process terminated signal.
395 signal( SIGCHLD, SIG_IGN );
396
397 ////////////////////////////////////
398
399
400         MSG_DEBUG("===========START MESSAGING FRAMEWORK==========");
401
402         // Reset message server ready flag
403         MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, false);
404
405         // Connect to DB
406         //      MsgStoConnectDB();
407
408         // Open Contact Service
409         MsgOpenContactSvc();
410
411         // Clean up mms dir
412         InitMmsDir();
413
414         // Regist vconf CB.
415         MsgSettingRegVconfCB();
416
417         try
418         {
419                 // plugin manager initialize
420                 MsgPluginManager::instance()->initialize();
421         }
422         catch (MsgException& e)
423         {
424                 MSG_FATAL("%s", e.what());
425         }
426         catch (exception& e)
427         {
428                 MSG_FATAL("%s", e.what());
429         }
430
431         pthread_t startThreadId;
432
433         // start transaction manager
434         if (pthread_create(&startThreadId, NULL, StartMsgServer, NULL) != 0)
435         {
436                 MSG_DEBUG("StartMsgServer not invoked: %s", strerror(errno));
437                 return -1;
438         }
439
440         MsgTransactionManager::instance()->getTMStatus();
441
442         mainloop = g_main_loop_new(NULL, FALSE);
443
444         g_type_init();
445
446         g_idle_add(InitThreadFunc, NULL);
447
448         if (mainloop != NULL)
449         {
450                 MSG_DEBUG("Start Messaging Framework!!!");
451
452                 // Run GMainLoop
453                 g_main_loop_run(mainloop);
454         }
455         else
456         {
457                 MSG_DEBUG("Fail to start Messaging Framework!!!");
458         }
459
460         // Remove vconf CB
461         MsgSettingRemoveVconfCB();
462
463         // Close Contact Sevice
464         MsgCloseContactSvc();
465
466         // Disconnect to DB
467         MsgStoDisconnectDB();
468
469         return 0;
470 }
471