Modify flora license version.
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageSim.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.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 #include <queue>
18
19 #include "MsgDebug.h"
20 #include "MsgUtilFile.h"
21 #include "MsgContact.h"
22 #include "MsgUtilStorage.h"
23 #include "MsgGconfWrapper.h"
24 #include "MsgSqliteWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgStorageHandler.h"
27
28 using namespace std;
29
30
31 /*==================================================================================================
32                                      VARIABLES
33 ==================================================================================================*/
34 extern MsgDbHandler dbHandle;
35
36
37 /*==================================================================================================
38                                      FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40 msg_error_t MsgInitSimMessage(MSG_SIM_STATUS_T SimStatus)
41 {
42         MSG_DEBUG("Start SIM Message Initialization");
43
44         msg_error_t err = MSG_SUCCESS;
45
46         // Set SIM count of vconf to 0
47         if (MsgSettingSetInt(SIM_USED_COUNT, 0) != MSG_SUCCESS) {
48                 MSG_DEBUG("Error to set config data [%s]", SIM_USED_COUNT);
49         }
50
51         if (MsgSettingSetInt(SIM_TOTAL_COUNT, 0) != MSG_SUCCESS) {
52                 MSG_DEBUG("Error to set config data [%s]", SIM_TOTAL_COUNT);
53         }
54
55         if (SimStatus != MSG_SIM_STATUS_NOT_FOUND) {
56                 MSG_MAIN_TYPE_T mainType = MSG_SMS_TYPE;
57                 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
58
59                 if (plg == NULL) {
60                         MSG_DEBUG("No plugin for %d type", mainType);
61                         return MSG_ERR_INVALID_PLUGIN_HANDLE;
62                 }
63
64                 // Check SIM Status
65                 MSG_DEBUG(" ** SIM is available - status : [%d] ** ", SimStatus);
66
67                 err = plg->initSimMessage();
68         }
69
70         return err;
71 }
72
73
74 msg_error_t MsgStoClearSimMessageInDB()
75 {
76         msg_error_t err = MSG_SUCCESS;
77
78         char sqlQuery[MAX_QUERY_LEN+1];
79
80         queue<msg_thread_id_t> threadList;
81
82         // Get Address ID of SIM Message
83         memset(sqlQuery, 0x00, sizeof(sqlQuery));
84
85         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(CONV_ID) FROM %s WHERE STORAGE_ID = %d",
86                         MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
87
88         int rowCnt = 0;
89
90         err = dbHandle.getTable(sqlQuery, &rowCnt);
91
92         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
93                 MSG_DEBUG("sql query is %s.", sqlQuery);
94
95                 dbHandle.freeTable();
96                 return err;
97         }
98
99         if (rowCnt <= 0) {
100                 dbHandle.freeTable();
101                 return MSG_SUCCESS;
102         }
103
104         for (int i = 1; i <= rowCnt; i++)
105         {
106                 MSG_DEBUG("thread ID : %d", dbHandle.getColumnToInt(i));
107                 threadList.push((msg_thread_id_t)dbHandle.getColumnToInt(i));
108         }
109
110         dbHandle.freeTable();
111
112         const char* tableList[] = {MSGFW_SMS_SENDOPT_TABLE_NAME, MSGFW_SIM_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME};
113
114         int listCnt = sizeof(tableList)/sizeof(char*);
115
116         dbHandle.beginTrans();
117
118         for (int i = 0; i < listCnt; i++)
119         {
120                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
121
122                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID IN \
123                                 (SELECT MSG_ID FROM %s WHERE STORAGE_ID = %d);",
124                                 tableList[i], MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
125
126                 // Delete SIM Message in tables
127                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
128                         dbHandle.endTrans(false);
129                         return MSG_ERR_DB_EXEC;
130                 }
131         }
132
133         // Clear Conversation table
134         if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
135                 dbHandle.endTrans(false);
136                 return MSG_ERR_DB_EXEC;
137         }
138
139         // Update Address
140         while (!threadList.empty())
141         {
142                 err = MsgStoUpdateConversation(&dbHandle, threadList.front());
143
144                 threadList.pop();
145
146                 if (err != MSG_SUCCESS) {
147                         dbHandle.endTrans(false);
148                         return err;
149                 }
150         }
151
152         dbHandle.endTrans(true);
153
154         return MSG_SUCCESS;
155 }
156