summaryrefslogtreecommitdiff
path: root/framework/storage-handler/MsgStorageFolder.cpp
blob: 1688fec35c6011efc6ead7374f35a5a8111f983b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "MsgDebug.h"
#include "MsgUtilStorage.h"
#include "MsgSqliteWrapper.h"
#include "MsgStorageHandler.h"


/*==================================================================================================
                                     VARIABLES
==================================================================================================*/

/*==================================================================================================
                                     FUNCTION IMPLEMENTATION
==================================================================================================*/
msg_error_t MsgStoAddFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
{
	msg_error_t err = MSG_SUCCESS;
	MsgDbHandler *dbHandle = getDbHandle();
	unsigned int rowId = 0;

	char sqlQuery[MAX_QUERY_LEN+1];

	err = dbHandle->getRowId(MSGFW_FOLDER_TABLE_NAME, &rowId);

	if (err != MSG_SUCCESS)
		return err;

	// Add Folder
	memset(sqlQuery, 0x00, sizeof(sqlQuery));
	snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', %d);",
			MSGFW_FOLDER_TABLE_NAME, rowId, pFolderInfo->folderName, pFolderInfo->folderType);

	if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
		return MSG_ERR_DB_EXEC;

	return MSG_SUCCESS;
}


msg_error_t MsgStoUpdateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
{
	char sqlQuery[MAX_QUERY_LEN+1];
	MsgDbHandler *dbHandle = getDbHandle();
	// Update Folder
	memset(sqlQuery, 0x00, sizeof(sqlQuery));
	snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_NAME = '%s', FOLDER_TYPE = %d WHERE FOLDER_ID = %d;",
			MSGFW_FOLDER_TABLE_NAME, pFolderInfo->folderName, pFolderInfo->folderType, pFolderInfo->folderId);

	if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
		return MSG_ERR_DB_EXEC;

	return MSG_SUCCESS;
}


msg_error_t MsgStoDeleteFolder(const msg_folder_id_t folderId)
{
	char sqlQuery[MAX_QUERY_LEN+1];
	MsgDbHandler *dbHandle = getDbHandle();
	dbHandle->beginTrans();

	memset(sqlQuery, 0x00, sizeof(sqlQuery));
	snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
			MSGFW_MESSAGE_TABLE_NAME, folderId);

	// Delete Message in the folder from msg table
	if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
		dbHandle->endTrans(false);
		return MSG_ERR_DB_EXEC;
	}

	memset(sqlQuery, 0x00, sizeof(sqlQuery));
	snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
			MSGFW_FOLDER_TABLE_NAME, folderId);

	// Delete Message in the folder from msg table
	if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
		dbHandle->endTrans(false);
		return MSG_ERR_DB_EXEC;
	}

	// Clear Conversation table
	if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
		dbHandle->endTrans(false);
		return MSG_ERR_DB_EXEC;
	}

	dbHandle->endTrans(true);

	return MSG_SUCCESS;
}


msg_error_t MsgStoGetFolderList(msg_struct_list_s *pFolderList)
{
	if (pFolderList == NULL) {
		MSG_DEBUG("pFolderList is NULL");
		return MSG_ERR_NULL_POINTER;
	}
	MsgDbHandler *dbHandle = getDbHandle();

	int rowCnt = 0, index = 0;

	char sqlQuery[MAX_QUERY_LEN+1];

	memset(sqlQuery, 0x00, sizeof(sqlQuery));
	snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
			MSGFW_FOLDER_TABLE_NAME);

	if (dbHandle->getTable(sqlQuery, &rowCnt, &index) != MSG_SUCCESS) {
		dbHandle->freeTable();
		return MSG_ERR_DB_GETTABLE;
	}

	pFolderList->nCount = rowCnt;

	MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);

	pFolderList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_FOLDER_INFO_S *));

	msg_struct_s* pTmp = NULL;

	for (int i = 0; i < rowCnt; i++) {
		pFolderList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
		pTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
		pTmp->type = MSG_STRUCT_FOLDER_INFO;
		pTmp->data = new MSG_FOLDER_INFO_S;
		MSG_FOLDER_INFO_S * pFolder = (MSG_FOLDER_INFO_S *)pTmp->data;
		memset(pFolder, 0x00, sizeof(MSG_FOLDER_INFO_S));

		pFolder->folderId = dbHandle->getColumnToInt(index++);

		pFolder->folderType = dbHandle->getColumnToInt(index++);

		memset(pFolder->folderName, 0x00, sizeof(pFolder->folderName));
		dbHandle->getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pFolder->folderName);
	}

	dbHandle->freeTable();

	return MSG_SUCCESS;
}