update tizen source
[framework/messaging/msg-service.git] / framework / storage-handler / MsgStorageFolder.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 "MsgDebug.h"
32 #include "MsgUtilStorage.h"
33 #include "MsgSqliteWrapper.h"
34 #include "MsgStorageHandler.h"
35
36
37 /*==================================================================================================
38                                      VARIABLES
39 ==================================================================================================*/
40 extern MsgDbHandler dbHandle;
41
42
43 /*==================================================================================================
44                                      FUNCTION IMPLEMENTATION
45 ==================================================================================================*/
46 MSG_ERROR_T MsgStoAddFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
47 {
48         MSG_ERROR_T err = MSG_SUCCESS;
49
50         unsigned int rowId = 0;
51
52         char sqlQuery[MAX_QUERY_LEN+1];
53
54         err = dbHandle.getRowId(MSGFW_FOLDER_TABLE_NAME, &rowId);
55
56         if (err != MSG_SUCCESS)
57                 return err;
58
59         // Add Folder
60         memset(sqlQuery, 0x00, sizeof(sqlQuery));
61         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', %d);",
62                                 MSGFW_FOLDER_TABLE_NAME, rowId, pFolderInfo->folderName, pFolderInfo->folderType);
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 MsgStoUpdateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
72 {
73         char sqlQuery[MAX_QUERY_LEN+1];
74
75         // Update Folder
76         memset(sqlQuery, 0x00, sizeof(sqlQuery));
77         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_NAME = '%s', FOLDER_TYPE = %d WHERE FOLDER_ID = %d;",
78                                 MSGFW_FOLDER_TABLE_NAME, pFolderInfo->folderName, pFolderInfo->folderType, pFolderInfo->folderId);
79
80         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
81                 return MSG_ERR_DB_EXEC;
82
83         return MSG_SUCCESS;
84 }
85
86
87 MSG_ERROR_T MsgStoDeleteFolder(const MSG_FOLDER_ID_T FolderId)
88 {
89         char sqlQuery[MAX_QUERY_LEN+1];
90
91         dbHandle.beginTrans();
92
93         memset(sqlQuery, 0x00, sizeof(sqlQuery));
94         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
95                                         MSGFW_MESSAGE_TABLE_NAME, FolderId);
96
97         // Delete Message in the folder from msg table
98         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
99         {
100                 dbHandle.endTrans(false);
101                 return MSG_ERR_DB_EXEC;
102         }
103
104         memset(sqlQuery, 0x00, sizeof(sqlQuery));
105         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
106                                         MSGFW_FOLDER_TABLE_NAME, FolderId);
107
108         // Delete Message in the folder from msg table
109         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
110         {
111                 dbHandle.endTrans(false);
112                 return MSG_ERR_DB_EXEC;
113         }
114
115         // Clear Address table
116         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS)
117         {
118                 dbHandle.endTrans(false);
119                 return MSG_ERR_DB_EXEC;
120         }
121
122         dbHandle.endTrans(true);
123
124         return MSG_SUCCESS;
125 }
126
127
128 MSG_ERROR_T MsgStoGetFolderList(MSG_FOLDER_LIST_S *pFolderList)
129 {
130         if (pFolderList == NULL)
131         {
132                 MSG_DEBUG("pFolderList is NULL");
133                 return MSG_ERR_NULL_POINTER;
134         }
135
136         int rowCnt = 0;
137         int index = 3;
138
139         char sqlQuery[MAX_QUERY_LEN+1];
140
141         memset(sqlQuery, 0x00, sizeof(sqlQuery));
142         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
143                 MSGFW_FOLDER_TABLE_NAME);
144
145         if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS)
146         {
147                 dbHandle.freeTable();
148                 return MSG_ERR_DB_GETTABLE;
149         }
150
151         pFolderList->nCount = rowCnt;
152
153         MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);
154
155         pFolderList->folderInfo = (MSG_FOLDER_INFO_S*)new char[sizeof(MSG_FOLDER_INFO_S)*rowCnt];
156
157         MSG_FOLDER_INFO_S* pTmp = pFolderList->folderInfo;
158
159         for (int i = 0; i < rowCnt; i++)
160         {
161                 pTmp->folderId = dbHandle.getColumnToInt(index++);
162
163                 pTmp->folderType = dbHandle.getColumnToInt(index++);
164
165                 memset(pTmp->folderName, 0x00, sizeof(pTmp->folderName));
166                 dbHandle.getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pTmp->folderName);
167
168                 pTmp++;
169         }
170
171         dbHandle.freeTable();
172
173         return MSG_SUCCESS;
174 }
175