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

Contents of /navit/navit/messages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (10 years, 9 months ago) by zoff99
File MIME type: text/plain
File size: 3306 byte(s)
lots of new stuff, tranlsations, bug fixes ...
1 zoff99 2 /**
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 zoff99 27 while (msg)
61     {
62     if (msg->id == mid)
63     {
64 zoff99 2 break;
65     }
66    
67     last = msg;
68     msg = msg->next;
69     }
70    
71 zoff99 27 if (msg)
72     {
73     if (last)
74     {
75 zoff99 2 last->next = msg->next;
76 zoff99 27 }
77     else
78     {
79 zoff99 2 this_->messages = msg->next;
80     }
81    
82     g_free(msg->text);
83     g_free(msg);
84    
85     return 1;
86 zoff99 27 }
87     else
88     {
89 zoff99 2 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 zoff99 27 while (msg && (i < this_->maxnum))
106     {
107     if ((this_->maxage > 0) && (now - msg->time) > this_->maxage)
108     {
109 zoff99 2 break;
110     }
111    
112     i++;
113     prev = msg;
114     msg = msg->next;
115     }
116    
117 zoff99 27 if (prev)
118     {
119 zoff99 2 prev->next = NULL;
120 zoff99 27 }
121     else
122     {
123 zoff99 2 this_->messages = NULL;
124     }
125    
126 zoff99 27 while (msg)
127     {
128 zoff99 2 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 zoff99 27 if (attr_generic_get_attr(attrs, NULL, attr_message_maxage, &age_attr, NULL))
144     {
145 zoff99 2 this->maxage = age_attr.u.num;
146 zoff99 27 }
147     else
148     {
149 zoff99 2 this->maxage = 10;
150     }
151    
152 zoff99 27 if (attr_generic_get_attr(attrs, NULL, attr_message_maxnum, &num_attr, NULL))
153     {
154 zoff99 2 this->maxnum = num_attr.u.num;
155 zoff99 27 }
156     else
157     {
158 zoff99 2 this->maxnum = 3;
159     }
160    
161     return this;
162     }
163    
164     void
165     messagelist_init(struct messagelist *this_)
166     {
167     if (!event_system())
168 zoff99 27 {
169 zoff99 2 return;
170 zoff99 27 }
171 zoff99 2 this_->msg_cleanup_cb = callback_new_1(callback_cast(message_cleanup), this_);
172 zoff99 27 // 10 secs. delay
173     this_->msg_cleanup_to = event_add_timeout(10000, 1, this_->msg_cleanup_cb);
174 zoff99 2 }
175    
176     struct message
177     *message_get(struct messagelist *this_)
178     {
179     return this_->messages;
180     }
181 zoff99 27

   
Visit the ZANavi Wiki