/[zanavi_public1]/navit/navit/messages.c
ZANavi

Contents of /navit/navit/messages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 4 months ago) by zoff99
File MIME type: text/plain
File size: 3254 byte(s)
import files
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 if (msg->id == mid) {
62 break;
63 }
64
65 last = msg;
66 msg = msg->next;
67 }
68
69 if (msg) {
70 if (last) {
71 last->next = msg->next;
72 } else {
73 this_->messages = msg->next;
74 }
75
76 g_free(msg->text);
77 g_free(msg);
78
79 return 1;
80 } else {
81 return 0;
82 }
83 }
84
85 static void
86 message_cleanup(struct messagelist *this_)
87 {
88 struct message *msg,*next,*prev=NULL;
89 int i;
90 time_t now;
91
92 msg = this_->messages;
93
94 now = time(NULL);
95
96 i = 0;
97 while (msg && (i < this_->maxnum)) {
98 if ((this_->maxage > 0) && (now - msg->time) > this_->maxage) {
99 break;
100 }
101
102 i++;
103 prev = msg;
104 msg = msg->next;
105 }
106
107 if (prev) {
108 prev->next = NULL;
109 } else {
110 this_->messages = NULL;
111 }
112
113 while (msg) {
114 next = msg->next;
115
116 g_free(msg->text);
117 g_free(msg);
118
119 msg = next;
120 }
121 }
122
123 struct messagelist
124 *messagelist_new(struct attr **attrs)
125 {
126 struct messagelist *this = g_new0(struct messagelist, 1);
127 struct attr num_attr,age_attr;
128
129 if (attr_generic_get_attr(attrs, NULL, attr_message_maxage, &age_attr, NULL)) {
130 this->maxage = age_attr.u.num;
131 } else {
132 this->maxage = 10;
133 }
134
135 if (attr_generic_get_attr(attrs, NULL, attr_message_maxnum, &num_attr, NULL)) {
136 this->maxnum = num_attr.u.num;
137 } else {
138 this->maxnum = 3;
139 }
140
141 return this;
142 }
143
144 void
145 messagelist_init(struct messagelist *this_)
146 {
147 if (!event_system())
148 return;
149 this_->msg_cleanup_cb = callback_new_1(callback_cast(message_cleanup), this_);
150 this_->msg_cleanup_to = event_add_timeout(1000, 1, this_->msg_cleanup_cb);
151 }
152
153 struct message
154 *message_get(struct messagelist *this_)
155 {
156 return this_->messages;
157 }

   
Visit the ZANavi Wiki