1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
#include <glib.h>
|
21 |
#include <time.h>
|
22 |
#include "messages.h"
|
23 |
#include "callback.h"
|
24 |
#include "event.h"
|
25 |
#include "attr.h"
|
26 |
|
27 |
struct messagelist {
|
28 |
struct message *messages; /**< All the messages that can currently be shown */
|
29 |
int last_mid; /**< Last Message ID */
|
30 |
int maxage; /**< Maximum age of messages */
|
31 |
int maxnum; /**< Maximum number of messages */
|
32 |
struct callback *msg_cleanup_cb; /**< Callback to clean up the messages */
|
33 |
struct event_timeout *msg_cleanup_to; /**< Idle event to clean up the messages */
|
34 |
};
|
35 |
|
36 |
int
|
37 |
message_new(struct messagelist *this_, char *message)
|
38 |
{
|
39 |
struct message *msg;
|
40 |
|
41 |
msg = g_new0(struct message, 1);
|
42 |
msg->text = g_strdup(message);
|
43 |
msg->id = ++(this_->last_mid);
|
44 |
msg->time = time(NULL);
|
45 |
|
46 |
msg->next = this_->messages;
|
47 |
this_->messages = msg;
|
48 |
|
49 |
return msg->id;
|
50 |
}
|
51 |
|
52 |
int
|
53 |
message_delete(struct messagelist *this_, int mid)
|
54 |
{
|
55 |
struct message *msg,*last;;
|
56 |
|
57 |
msg = this_->messages;
|
58 |
last = NULL;
|
59 |
|
60 |
while (msg)
|
61 |
{
|
62 |
if (msg->id == mid)
|
63 |
{
|
64 |
break;
|
65 |
}
|
66 |
|
67 |
last = msg;
|
68 |
msg = msg->next;
|
69 |
}
|
70 |
|
71 |
if (msg)
|
72 |
{
|
73 |
if (last)
|
74 |
{
|
75 |
last->next = msg->next;
|
76 |
}
|
77 |
else
|
78 |
{
|
79 |
this_->messages = msg->next;
|
80 |
}
|
81 |
|
82 |
g_free(msg->text);
|
83 |
g_free(msg);
|
84 |
|
85 |
return 1;
|
86 |
}
|
87 |
else
|
88 |
{
|
89 |
return 0;
|
90 |
}
|
91 |
}
|
92 |
|
93 |
static void
|
94 |
message_cleanup(struct messagelist *this_)
|
95 |
{
|
96 |
struct message *msg,*next,*prev=NULL;
|
97 |
int i;
|
98 |
time_t now;
|
99 |
|
100 |
msg = this_->messages;
|
101 |
|
102 |
now = time(NULL);
|
103 |
|
104 |
i = 0;
|
105 |
while (msg && (i < this_->maxnum))
|
106 |
{
|
107 |
if ((this_->maxage > 0) && (now - msg->time) > this_->maxage)
|
108 |
{
|
109 |
break;
|
110 |
}
|
111 |
|
112 |
i++;
|
113 |
prev = msg;
|
114 |
msg = msg->next;
|
115 |
}
|
116 |
|
117 |
if (prev)
|
118 |
{
|
119 |
prev->next = NULL;
|
120 |
}
|
121 |
else
|
122 |
{
|
123 |
this_->messages = NULL;
|
124 |
}
|
125 |
|
126 |
while (msg)
|
127 |
{
|
128 |
next = msg->next;
|
129 |
|
130 |
g_free(msg->text);
|
131 |
g_free(msg);
|
132 |
|
133 |
msg = next;
|
134 |
}
|
135 |
}
|
136 |
|
137 |
struct messagelist
|
138 |
*messagelist_new(struct attr **attrs)
|
139 |
{
|
140 |
struct messagelist *this = g_new0(struct messagelist, 1);
|
141 |
struct attr num_attr,age_attr;
|
142 |
|
143 |
if (attr_generic_get_attr(attrs, NULL, attr_message_maxage, &age_attr, NULL))
|
144 |
{
|
145 |
this->maxage = age_attr.u.num;
|
146 |
}
|
147 |
else
|
148 |
{
|
149 |
this->maxage = 10;
|
150 |
}
|
151 |
|
152 |
if (attr_generic_get_attr(attrs, NULL, attr_message_maxnum, &num_attr, NULL))
|
153 |
{
|
154 |
this->maxnum = num_attr.u.num;
|
155 |
}
|
156 |
else
|
157 |
{
|
158 |
this->maxnum = 3;
|
159 |
}
|
160 |
|
161 |
return this;
|
162 |
}
|
163 |
|
164 |
void
|
165 |
messagelist_init(struct messagelist *this_)
|
166 |
{
|
167 |
if (!event_system())
|
168 |
{
|
169 |
return;
|
170 |
}
|
171 |
this_->msg_cleanup_cb = callback_new_1(callback_cast(message_cleanup), this_);
|
172 |
// 10 secs. delay
|
173 |
this_->msg_cleanup_to = event_add_timeout(10000, 1, this_->msg_cleanup_cb);
|
174 |
}
|
175 |
|
176 |
struct message
|
177 |
*message_get(struct messagelist *this_)
|
178 |
{
|
179 |
return this_->messages;
|
180 |
}
|
181 |
|