summaryrefslogtreecommitdiff
path: root/navit/messages.c
blob: 80b17236add821dd27301e01a05015ba8a6efe4b (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
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib.h>
#include <time.h>
#include "messages.h"
#include "callback.h"
#include "event.h"
#include "attr.h"

struct messagelist {
	struct message *messages;	/**< All the messages that can currently be shown */
	int last_mid;							/**< Last Message ID */
	int maxage;								/**< Maximum age of messages */
	int maxnum;								/**< Maximum number of messages */
	struct callback *msg_cleanup_cb;			/**< Callback to clean up the messages */
	struct event_timeout *msg_cleanup_to;		/**< Idle event to clean up the messages */
};

int
message_new(struct messagelist *this_, const char *message)
{
	struct message *msg;

	msg = g_new0(struct message, 1);
	msg->text = g_strdup(message);
	msg->id = ++(this_->last_mid);
	msg->time = time(NULL);

	msg->next = this_->messages;
	this_->messages = msg;

	return msg->id;
}

int
message_delete(struct messagelist *this_, int mid)
{
	struct message *msg,*last;;

	msg = this_->messages;
	last = NULL;

	while (msg) {
		if (msg->id == mid) {
			break;
		}

		last = msg;
		msg = msg->next;
	}

	if (msg) {
		if (last) {
			last->next = msg->next;
		} else {
			this_->messages = msg->next;
		}

		g_free(msg->text);
		g_free(msg);

		return 1;
	} else {
		return 0;
	}
}

static void 
message_cleanup(struct messagelist *this_)
{
	struct message *msg,*next,*prev=NULL;
	int i;
	time_t now;

	msg = this_->messages;

	now = time(NULL);

	i = 0;
	while (msg && (i < this_->maxnum)) {
		if ((this_->maxage > 0) && (now - msg->time) > this_->maxage) {
			break;
		}

		i++;
		prev = msg;
		msg = msg->next;
	}

	if (prev) {
		prev->next = NULL;
	} else {
		this_->messages = NULL;
	}

	while (msg) {
		next = msg->next;

		g_free(msg->text);
		g_free(msg);

		msg = next;
	}
}

struct messagelist
*messagelist_new(struct attr **attrs)
{
	struct messagelist *this = g_new0(struct messagelist, 1);
	struct attr num_attr,age_attr;

	if (attr_generic_get_attr(attrs, NULL, attr_message_maxage, &age_attr, NULL)) {
		this->maxage = age_attr.u.num;
	} else {
		this->maxage = 10;
	}

	if (attr_generic_get_attr(attrs, NULL, attr_message_maxnum, &num_attr, NULL)) {
		this->maxnum = num_attr.u.num;
	} else {
		this->maxnum = 3;
	}

	return this;
}

void
messagelist_init(struct messagelist *this_)
{
	if (!event_system())
		return;
	this_->msg_cleanup_cb = callback_new_1(callback_cast(message_cleanup), this_);
	this_->msg_cleanup_to = event_add_timeout(1000, 1, this_->msg_cleanup_cb);
}

struct message 
*message_get(struct messagelist *this_)
{
	return this_->messages;
}