summaryrefslogtreecommitdiff
path: root/src/notification_status.c
blob: 97fbe419cf3e92a7a4517f68bc25b29e861552e6 (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
/*
 *  libnotification
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@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 <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <vconf.h>

#include <notification.h>
#include <notification_db.h>
#include <notification_noti.h>
#include <notification_debug.h>
#include <notification_internal.h>
#include <notification_status.h>

#define NOTIFICATION_STATUS_MESSAGE_KEY "memory/private/libstatus/message"

struct _message_cb_data {
	notification_status_message_cb callback;
	void *data;
};

static struct _message_cb_data md;

static void __notification_status_message_change_cb(keynode_t *node, void *user_data)
{
	int type;
	char *message = NULL;

	if (!node) {
		NOTIFICATION_ERR("key node is NULL");
		return;
	}

	if (!md.callback)
		return;

	type = vconf_keynode_get_type(node);
	if (type != VCONF_TYPE_STRING) {
		NOTIFICATION_ERR("invail node type : %d", type);
		return;
	}

	message = vconf_keynode_get_str(node);
	if (!message) {
		NOTIFICATION_ERR("fail to get message");
		return;
	}

	if (strlen(message) <= 0){
		NOTIFICATION_DBG("message has only NULL");
		return;
	}

	NOTIFICATION_DBG("message : %s", message);
	md.callback(message, md.data);
}

EXPORT_API
int notification_status_message_post(const char *message)
{
	int ret = 0;

	if (!message) {
		NOTIFICATION_ERR("message is NULL");
		return NOTIFICATION_ERROR_INVALID_DATA;
	}

	if (strlen(message) <= 0) {
		NOTIFICATION_ERR("message has only NULL");
		return NOTIFICATION_ERROR_INVALID_DATA;
	}


	ret = vconf_set_str(NOTIFICATION_STATUS_MESSAGE_KEY, message);
	if (ret) {
		NOTIFICATION_ERR("fail to set message [%s]", message);
		return NOTIFICATION_ERROR_IO;
	}

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API
int notification_status_monitor_message_cb_set(notification_status_message_cb callback, void *user_data)
{
	int ret = 0;
	if (!callback)
		return NOTIFICATION_ERROR_INVALID_DATA;

	ret = vconf_notify_key_changed(NOTIFICATION_STATUS_MESSAGE_KEY,
			__notification_status_message_change_cb, NULL);
	if (ret && errno != EALREADY) {
		NOTIFICATION_ERR("fail to set message cb");
		return NOTIFICATION_ERROR_IO;
	}

	md.callback = callback;
	md.data = user_data;

	return NOTIFICATION_ERROR_NONE;
}

EXPORT_API
int notification_status_monitor_message_cb_unset(void)
{
	vconf_ignore_key_changed(NOTIFICATION_STATUS_MESSAGE_KEY,
			__notification_status_message_change_cb);
	md.callback = NULL;
	md.data = NULL;

	return NOTIFICATION_ERROR_NONE;
}