summaryrefslogtreecommitdiff
path: root/src/notification_list.c
blob: 2dc8269f130c1a55c8a48a0102bc102432a6df4e (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (c) 2000 - 2017 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 <stdlib.h>

#include <tizen.h>

#include <notification.h>
#include <notification_list.h>
#include <notification_noti.h>
#include <notification_debug.h>
#include <notification_private.h>
#include <notification_ipc.h>


struct _notification_list {
	notification_list_h prev;
	notification_list_h next;

	notification_h noti;
};

notification_list_h _notification_list_create(void)
{
	notification_list_h list = NULL;

	list = (notification_list_h) malloc(sizeof(struct _notification_list));
	if (list == NULL) {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("Failed to alloc memory");
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	list->prev = NULL;
	list->next = NULL;

	list->noti = NULL;

	return list;
}

EXPORT_API notification_list_h notification_list_get_head(notification_list_h list)
{
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("Invalid parameter");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	cur_list = list;

	while (cur_list->prev != NULL)
		cur_list = cur_list->prev;

	set_last_result(NOTIFICATION_ERROR_NONE);
	return cur_list;
}

EXPORT_API notification_list_h notification_list_get_tail(notification_list_h list)
{
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("INVALID DATA : list == NULL");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	cur_list = list;

	while (cur_list->next != NULL)
		cur_list = cur_list->next;

	set_last_result(NOTIFICATION_ERROR_NONE);
	return cur_list;
}

EXPORT_API notification_list_h notification_list_get_prev(notification_list_h list)
{
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("INVALID DATA : list == NULL");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	cur_list = list;

	set_last_result(NOTIFICATION_ERROR_NONE);
	return cur_list->prev;
}

EXPORT_API notification_list_h notification_list_get_next(notification_list_h list)
{
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("INVALID DATA : list == NULL");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	cur_list = list;

	set_last_result(NOTIFICATION_ERROR_NONE);
	return cur_list->next;
}

EXPORT_API notification_h notification_list_get_data(notification_list_h list)
{
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("INVALID DATA : list == NULL");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	cur_list = list;

	set_last_result(NOTIFICATION_ERROR_NONE);
	return cur_list->noti;
}

EXPORT_API int notification_list_get_count(notification_list_h list)
{
	int count = 0;
	notification_list_h cur_list = NULL;

	if (list == NULL) {
		NOTIFICATION_ERR("Invalid paramter");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return 0;
	}

	cur_list = notification_list_get_head(list);

	while (cur_list != NULL) {
		count++;
		cur_list = cur_list->next;
	}

	set_last_result(NOTIFICATION_ERROR_NONE);
	return count;
}

EXPORT_API notification_list_h notification_list_append(notification_list_h list,
		notification_h noti)
{
	notification_list_h new_list = NULL;
	notification_list_h cur_list = NULL;

	if (noti == NULL) {
		NOTIFICATION_ERR("Invalid parameter");
		set_last_result(NOTIFICATION_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	if (list != NULL) {
		cur_list = notification_list_get_tail(list);

		new_list = _notification_list_create();
		if (new_list == NULL) {
			/* LCOV_EXCL_START */
			NOTIFICATION_ERR("Failed to alloc memory");
			set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
			return NULL;
			/* LCOV_EXCL_STOP */
		}

		cur_list->next = new_list;
		new_list->prev = cur_list;

		new_list->noti = noti;
	} else {
		cur_list = _notification_list_create();
		if (cur_list == NULL) {
			/* LCOV_EXCL_START */
			NOTIFICATION_ERR("Failed to alloc memory");
			set_last_result(NOTIFICATION_ERROR_OUT_OF_MEMORY);
			return NULL;
			/* LCOV_EXCL_STOP */
		}

		new_list = cur_list;
		new_list->noti = noti;
	}

	set_last_result(NOTIFICATION_ERROR_NONE);
	return new_list;
}

EXPORT_API notification_list_h notification_list_remove(notification_list_h list,
		notification_h noti)
{
	notification_list_h cur_list = NULL;
	notification_list_h prev_list = NULL;
	notification_list_h next_list = NULL;

	cur_list = notification_list_get_head(list);
	while (cur_list != NULL) {
		if (cur_list->noti == noti) {
			/* remove */
			prev_list = cur_list->prev;
			next_list = cur_list->next;

			if (next_list != NULL) {
				if (prev_list != NULL) {
					prev_list->next = next_list;
					next_list->prev = prev_list;
				} else {
					next_list->prev = NULL;
				}
			} else {
				if (prev_list != NULL)
					prev_list->next = NULL;
			}

			free(cur_list);
			break;
		}

		cur_list = cur_list->next;
	}

	if (prev_list != NULL)
		return notification_list_get_head(prev_list);
	else if (next_list != NULL)
		return next_list;

	return NULL;
}

EXPORT_API int notification_get_list_for_uid(notification_type_e type,
		int count,
		notification_list_h *list, uid_t uid)
{
	notification_list_h get_list = NULL;
	int ret = 0;

	if (list == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	ret = notification_ipc_request_load_noti_grouping_list(type, count, &get_list, uid);
	if (ret != NOTIFICATION_ERROR_NONE)
		return ret;

	if (get_list != NULL)
		*list = notification_list_get_head(get_list);

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API int notification_get_list(notification_type_e type,
		int count,
		notification_list_h *list)
{
	return notification_get_list_for_uid(type, count, list, getuid());
}

EXPORT_API int notification_get_detail_list_for_uid(const char *app_id,
		int group_id,
		int priv_id,
		int count,
		notification_list_h *list,
		uid_t uid)
{
	notification_list_h get_list = NULL;
	int ret = 0;

	if (list == NULL || app_id == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	ret = notification_ipc_request_load_noti_detail_list(app_id, group_id, priv_id, count,
			&get_list, uid);
	if (ret != NOTIFICATION_ERROR_NONE)
		return ret;

	if (get_list != NULL)
		*list = notification_list_get_head(get_list);

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API int notification_get_detail_list(const char *app_id,
		int group_id,
		int priv_id,
		int count,
		notification_list_h *list)
{
	return notification_get_detail_list_for_uid(app_id, group_id,
			priv_id, count, list, getuid());
}

EXPORT_API int notification_free_list(notification_list_h list)
{
	notification_list_h cur_list = NULL;
	notification_h noti = NULL;

	if (list == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	cur_list = notification_list_get_head(list);

	while (cur_list != NULL) {
		noti = notification_list_get_data(cur_list);
		cur_list = notification_list_remove(cur_list, noti);

		notification_free(noti);
	}

	return NOTIFICATION_ERROR_NONE;
}