1688fec35c6011efc6ead7374f35a5a8111f983b
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageFolder.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 "MsgDebug.h"
18 #include "MsgUtilStorage.h"
19 #include "MsgSqliteWrapper.h"
20 #include "MsgStorageHandler.h"
21
22
23 /*==================================================================================================
24                                      VARIABLES
25 ==================================================================================================*/
26
27 /*==================================================================================================
28                                      FUNCTION IMPLEMENTATION
29 ==================================================================================================*/
30 msg_error_t MsgStoAddFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
31 {
32         msg_error_t err = MSG_SUCCESS;
33         MsgDbHandler *dbHandle = getDbHandle();
34         unsigned int rowId = 0;
35
36         char sqlQuery[MAX_QUERY_LEN+1];
37
38         err = dbHandle->getRowId(MSGFW_FOLDER_TABLE_NAME, &rowId);
39
40         if (err != MSG_SUCCESS)
41                 return err;
42
43         // Add Folder
44         memset(sqlQuery, 0x00, sizeof(sqlQuery));
45         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', %d);",
46                         MSGFW_FOLDER_TABLE_NAME, rowId, pFolderInfo->folderName, pFolderInfo->folderType);
47
48         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
49                 return MSG_ERR_DB_EXEC;
50
51         return MSG_SUCCESS;
52 }
53
54
55 msg_error_t MsgStoUpdateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
56 {
57         char sqlQuery[MAX_QUERY_LEN+1];
58         MsgDbHandler *dbHandle = getDbHandle();
59         // Update Folder
60         memset(sqlQuery, 0x00, sizeof(sqlQuery));
61         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_NAME = '%s', FOLDER_TYPE = %d WHERE FOLDER_ID = %d;",
62                         MSGFW_FOLDER_TABLE_NAME, pFolderInfo->folderName, pFolderInfo->folderType, pFolderInfo->folderId);
63
64         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
65                 return MSG_ERR_DB_EXEC;
66
67         return MSG_SUCCESS;
68 }
69
70
71 msg_error_t MsgStoDeleteFolder(const msg_folder_id_t folderId)
72 {
73         char sqlQuery[MAX_QUERY_LEN+1];
74         MsgDbHandler *dbHandle = getDbHandle();
75         dbHandle->beginTrans();
76
77         memset(sqlQuery, 0x00, sizeof(sqlQuery));
78         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
79                         MSGFW_MESSAGE_TABLE_NAME, folderId);
80
81         // Delete Message in the folder from msg table
82         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
83                 dbHandle->endTrans(false);
84                 return MSG_ERR_DB_EXEC;
85         }
86
87         memset(sqlQuery, 0x00, sizeof(sqlQuery));
88         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
89                         MSGFW_FOLDER_TABLE_NAME, folderId);
90
91         // Delete Message in the folder from msg table
92         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
93                 dbHandle->endTrans(false);
94                 return MSG_ERR_DB_EXEC;
95         }
96
97         // Clear Conversation table
98         if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
99                 dbHandle->endTrans(false);
100                 return MSG_ERR_DB_EXEC;
101         }
102
103         dbHandle->endTrans(true);
104
105         return MSG_SUCCESS;
106 }
107
108
109 msg_error_t MsgStoGetFolderList(msg_struct_list_s *pFolderList)
110 {
111         if (pFolderList == NULL) {
112                 MSG_DEBUG("pFolderList is NULL");
113                 return MSG_ERR_NULL_POINTER;
114         }
115         MsgDbHandler *dbHandle = getDbHandle();
116
117         int rowCnt = 0, index = 0;
118
119         char sqlQuery[MAX_QUERY_LEN+1];
120
121         memset(sqlQuery, 0x00, sizeof(sqlQuery));
122         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
123                         MSGFW_FOLDER_TABLE_NAME);
124
125         if (dbHandle->getTable(sqlQuery, &rowCnt, &index) != MSG_SUCCESS) {
126                 dbHandle->freeTable();
127                 return MSG_ERR_DB_GETTABLE;
128         }
129
130         pFolderList->nCount = rowCnt;
131
132         MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);
133
134         pFolderList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_FOLDER_INFO_S *));
135
136         msg_struct_s* pTmp = NULL;
137
138         for (int i = 0; i < rowCnt; i++) {
139                 pFolderList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
140                 pTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
141                 pTmp->type = MSG_STRUCT_FOLDER_INFO;
142                 pTmp->data = new MSG_FOLDER_INFO_S;
143                 MSG_FOLDER_INFO_S * pFolder = (MSG_FOLDER_INFO_S *)pTmp->data;
144                 memset(pFolder, 0x00, sizeof(MSG_FOLDER_INFO_S));
145
146                 pFolder->folderId = dbHandle->getColumnToInt(index++);
147
148                 pFolder->folderType = dbHandle->getColumnToInt(index++);
149
150                 memset(pFolder->folderName, 0x00, sizeof(pFolder->folderName));
151                 dbHandle->getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pFolder->folderName);
152         }
153
154         dbHandle->freeTable();
155
156         return MSG_SUCCESS;
157 }