summaryrefslogtreecommitdiff
path: root/src/notification_group.c
blob: 03c3e1478b82f8c0d9fe30d201057b7427d7f4cd (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *  libnotification
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jeonghoon Park <jh1979.park@samsung.com>, Youngjoo Park <yjoo93.park@samsung.com>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <notification_debug.h>
#include <notification_group.h>
#include <notification_db.h>

static int _notification_group_check_data_inserted(const char *pkgname,
						   int group_id, sqlite3 * db)
{
	sqlite3_stmt *stmt = NULL;
	char query[NOTIFICATION_QUERY_MAX] = { 0, };
	int ret = NOTIFICATION_ERROR_NONE, result = 0;

	snprintf(query, sizeof(query),
		 "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
		 pkgname, group_id);

	ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
	if (ret != SQLITE_OK) {
		NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
				 sqlite3_errmsg(db));
		return NOTIFICATION_ERROR_FROM_DB;
	}

	ret = sqlite3_step(stmt);
	if (ret == SQLITE_ROW) {
		result = sqlite3_column_int(stmt, 0);
	} else {
		result = 0;
	}

	NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
			  query, result);

	sqlite3_finalize(stmt);

	if (result > 0) {
		return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
	}

	return NOTIFICATION_ERROR_NONE;
}

notification_error_e notification_group_set_badge(const char *pkgname,
						  int group_id, int count)
{
	sqlite3 *db;
	sqlite3_stmt *stmt = NULL;
	char query[NOTIFICATION_QUERY_MAX] = { 0, };
	int ret = 0;
	int result = NOTIFICATION_ERROR_NONE;

	/* Open DB */
	db = notification_db_open(DBPATH);

	/* Check pkgname & group_id */
	ret = _notification_group_check_data_inserted(pkgname, group_id, db);

	/* Make query */
	if (ret == NOTIFICATION_ERROR_NONE) {
		/* Insert if does not exist */
		snprintf(query, sizeof(query), "insert into noti_group_data ("
			 "caller_pkgname, group_id, badge, content, loc_content) values ("
			 "'%s', %d, %d, '', '')", pkgname, group_id, count);

	} else {
		/* Update if exist */
		snprintf(query, sizeof(query), "update noti_group_data "
			 "set badge = %d "
			 "where caller_pkgname = '%s' and group_id = %d",
			 count, pkgname, group_id);
	}

	ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
	if (ret != SQLITE_OK) {
		NOTIFICATION_ERR("Insert Query : %s", query);
		NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
				 sqlite3_errmsg(db));
		if (stmt) {
			sqlite3_finalize(stmt);
		}

		if (db) {
			notification_db_close(&db);
		}
		return NOTIFICATION_ERROR_FROM_DB;
	}

	ret = sqlite3_step(stmt);
	if (ret == SQLITE_OK || ret == SQLITE_DONE) {
		result = NOTIFICATION_ERROR_NONE;
	} else {
		result = NOTIFICATION_ERROR_FROM_DB;
	}

	if (stmt) {
		sqlite3_finalize(stmt);
	}

	/* Close DB */
	if (db) {
		notification_db_close(&db);
	}

	return result;
}

notification_error_e notification_group_get_badge(const char *pkgname,
						  int group_id, int *count)
{
	sqlite3 *db;
	sqlite3_stmt *stmt = NULL;
	char query[NOTIFICATION_QUERY_MAX] = { 0, };
	int ret = 0;
	int col = 0;

	/* Open DB */
	db = notification_db_open(DBPATH);

	/* Make query */
	if (group_id == NOTIFICATION_GROUP_ID_NONE) {
		/* Check Group id None is exist */
		ret =
		    _notification_group_check_data_inserted(pkgname, group_id,
							    db);

		if (ret == NOTIFICATION_ERROR_NONE) {
			/* Get all of pkgname count if none group id is not exist */
			snprintf(query, sizeof(query),
				 "select sum(badge) "
				 "from noti_group_data "
				 "where caller_pkgname = '%s'", pkgname);
		} else {
			/* Get none group id count */
			snprintf(query, sizeof(query),
				 "select badge "
				 "from noti_group_data "
				 "where caller_pkgname = '%s' and group_id = %d",
				 pkgname, group_id);
		}
	} else {
		snprintf(query, sizeof(query),
			 "select badge "
			 "from noti_group_data "
			 "where caller_pkgname = '%s' and group_id = %d",
			 pkgname, group_id);
	}

	NOTIFICATION_INFO("Get badge : query[%s]", query);

	ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
	if (ret != SQLITE_OK) {
		NOTIFICATION_ERR("Select Query : %s", query);
		NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
				 sqlite3_errmsg(db));

		return NOTIFICATION_ERROR_FROM_DB;
	}

	ret = sqlite3_step(stmt);
	if (ret == SQLITE_ROW) {
		*count = sqlite3_column_int(stmt, col++);
	}

	sqlite3_finalize(stmt);

	// db close
	if (db) {
		notification_db_close(&db);
	}

	return NOTIFICATION_ERROR_NONE;
}