f2e71140080dd5e9bb8b901c684cfc587b0a91a1
[framework/messaging/msg-service.git] / framework / storage-handler / MsgStorageFolder.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 #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         {
86                 dbHandle.endTrans(false);
87                 return MSG_ERR_DB_EXEC;
88         }
89
90         memset(sqlQuery, 0x00, sizeof(sqlQuery));
91         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
92                                         MSGFW_FOLDER_TABLE_NAME, FolderId);
93
94         // Delete Message in the folder from msg table
95         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
96         {
97                 dbHandle.endTrans(false);
98                 return MSG_ERR_DB_EXEC;
99         }
100
101         // Clear Address table
102         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS)
103         {
104                 dbHandle.endTrans(false);
105                 return MSG_ERR_DB_EXEC;
106         }
107
108         dbHandle.endTrans(true);
109
110         return MSG_SUCCESS;
111 }
112
113
114 MSG_ERROR_T MsgStoGetFolderList(MSG_FOLDER_LIST_S *pFolderList)
115 {
116         if (pFolderList == NULL)
117         {
118                 MSG_DEBUG("pFolderList is NULL");
119                 return MSG_ERR_NULL_POINTER;
120         }
121
122         int rowCnt = 0;
123         int index = 3;
124
125         char sqlQuery[MAX_QUERY_LEN+1];
126
127         memset(sqlQuery, 0x00, sizeof(sqlQuery));
128         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
129                 MSGFW_FOLDER_TABLE_NAME);
130
131         if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS)
132         {
133                 dbHandle.freeTable();
134                 return MSG_ERR_DB_GETTABLE;
135         }
136
137         pFolderList->nCount = rowCnt;
138
139         MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);
140
141         pFolderList->folderInfo = (MSG_FOLDER_INFO_S*)new char[sizeof(MSG_FOLDER_INFO_S)*rowCnt];
142
143         MSG_FOLDER_INFO_S* pTmp = pFolderList->folderInfo;
144
145         for (int i = 0; i < rowCnt; i++)
146         {
147                 pTmp->folderId = dbHandle.getColumnToInt(index++);
148
149                 pTmp->folderType = dbHandle.getColumnToInt(index++);
150
151                 memset(pTmp->folderName, 0x00, sizeof(pTmp->folderName));
152                 dbHandle.getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pTmp->folderName);
153
154                 pTmp++;
155         }
156
157         dbHandle.freeTable();
158
159         return MSG_SUCCESS;
160 }
161