update tizen source
[framework/messaging/msg-service.git] / framework / storage-handler / MsgStorageSim.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 #include <queue>
32
33 #include "MsgDebug.h"
34 #include "MsgUtilFile.h"
35 #include "MsgContact.h"
36 #include "MsgUtilStorage.h"
37 #include "MsgGconfWrapper.h"
38 #include "MsgSqliteWrapper.h"
39 #include "MsgPluginManager.h"
40 #include "MsgStorageHandler.h"
41
42 using namespace std;
43
44
45 /*==================================================================================================
46                                      VARIABLES
47 ==================================================================================================*/
48 extern MsgDbHandler dbHandle;
49
50
51 /*==================================================================================================
52                                      FUNCTION IMPLEMENTATION
53 ==================================================================================================*/
54 MSG_ERROR_T MsgInitSimMessage(MSG_SIM_STATUS_T SimStatus)
55 {
56         MSG_DEBUG("Start SIM Message Initialization");
57
58         MSG_ERROR_T err = MSG_SUCCESS;
59
60         // Set SIM count of vconf to 0
61         if (MsgSettingSetInt(SIM_USED_COUNT, 0) != MSG_SUCCESS)
62         {
63                 MSG_DEBUG("Error to set config data [%s]", SIM_USED_COUNT);
64         }
65
66         if (MsgSettingSetInt(SIM_TOTAL_COUNT, 0) != MSG_SUCCESS)
67         {
68                 MSG_DEBUG("Error to set config data [%s]", SIM_TOTAL_COUNT);
69         }
70
71         if (SimStatus != MSG_SIM_STATUS_NOT_FOUND)
72         {
73                 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
74                 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
75
76                 if (plg == NULL)
77                 {
78                         MSG_DEBUG("No plugin for %d type", mainType);
79                         return MSG_ERR_INVALID_PLUGIN_HANDLE;
80                 }
81
82                 // Check SIM Status
83                 MSG_DEBUG(" ** SIM is available - status : [%d] ** ", SimStatus);
84
85                 err = plg->initSimMessage();
86         }
87
88         return err;
89 }
90
91
92 MSG_ERROR_T MsgStoClearSimMessageInDB()
93 {
94         MSG_ERROR_T err = MSG_SUCCESS;
95
96         char sqlQuery[MAX_QUERY_LEN+1];
97
98         queue<MSG_THREAD_ID_T> threadList;
99
100         // Get Address ID of SIM Message
101         memset(sqlQuery, 0x00, sizeof(sqlQuery));
102
103         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(B.ADDRESS_ID) \
104                                         FROM %s A, %s B \
105                                      WHERE A.ADDRESS_ID = B.ADDRESS_ID \
106                                           AND A.STORAGE_ID = %d",
107                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MSG_STORAGE_SIM);
108
109         int rowCnt = 0;
110
111         err = dbHandle.getTable(sqlQuery, &rowCnt);
112
113         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD)
114         {
115                 MSG_DEBUG("sql query is %s.", sqlQuery);
116
117                 dbHandle.freeTable();
118                 return err;
119         }
120
121         if (rowCnt <= 0)
122         {
123                 dbHandle.freeTable();
124                 return MSG_SUCCESS;
125         }
126
127         for (int i = 1; i <= rowCnt; i++)
128         {
129                 MSG_DEBUG("thread ID : %d", dbHandle.getColumnToInt(i));
130                 threadList.push((MSG_THREAD_ID_T)dbHandle.getColumnToInt(i));
131         }
132
133         dbHandle.freeTable();
134
135         const char* tableList[] = {MSGFW_SMS_SENDOPT_TABLE_NAME, MSGFW_SIM_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME};
136
137         int listCnt = sizeof(tableList)/sizeof(char*);
138
139         dbHandle.beginTrans();
140
141         for (int i = 0; i < listCnt; i++)
142         {
143                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
144
145                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID IN \
146                                                                                 (SELECT MSG_ID FROM %s WHERE STORAGE_ID = %d);",
147                                                 tableList[i], MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
148
149                 // Delete SIM Message in tables
150                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
151                 {
152                         dbHandle.endTrans(false);
153                         return MSG_ERR_DB_EXEC;
154                 }
155         }
156
157         // Clear Address table
158         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS)
159         {
160                 dbHandle.endTrans(false);
161                 return MSG_ERR_DB_EXEC;
162         }
163
164         // Update Address
165         while (!threadList.empty())
166         {
167                 err = MsgStoUpdateAddress(&dbHandle, threadList.front());
168
169                 threadList.pop();
170
171                 if (err != MSG_SUCCESS)
172                 {
173                         dbHandle.endTrans(false);
174                         return err;
175                 }
176         }
177
178         dbHandle.endTrans(true);
179
180         return MSG_SUCCESS;
181 }
182