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