0.8.8claws26
[claws.git] / src / automaton.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <gdk/gdk.h>
22 #include <gtk/gtkmain.h>
23
24 #include "automaton.h"
25
26 Automaton *automaton_create(gint num)
27 {
28         Automaton *atm;
29
30         g_return_val_if_fail(num > 0, NULL);
31
32         atm = g_new0(Automaton, 1);
33         atm->max = num - 1;
34         atm->state = g_new0(AtmState, num);
35
36         return atm;
37 }
38
39 void automaton_destroy(Automaton *atm)
40 {
41         if (!atm) return;
42
43         g_free(atm->state);
44         g_free(atm);
45 }
46
47 gboolean automaton_input_cb(GIOChannel *channel,
48                         GIOCondition condition,
49                         gpointer data)
50 {
51         Automaton *atm = (Automaton *)data;
52         SockInfo *sock;
53         gint next;
54
55         /* We get out sockinfo from the atm context and not from the
56          * passed file descriptor because we can't map that one back
57          * to the sockinfo */
58         sock = atm->help_sock;
59         g_return_val_if_fail(sock->sock == g_io_channel_unix_get_fd(channel), TRUE);
60
61         if (atm->timeout_tag > 0) {
62                 gtk_timeout_remove(atm->timeout_tag);
63                 atm->timeout_tag = 0;
64                 atm->elapsed = 0;
65         }
66         gdk_input_remove(atm->tag);
67         atm->tag = 0;
68
69         if (atm->cancelled) {
70                 atm->terminate(sock, data);
71                 return TRUE;
72         }
73
74         if (atm->ui_func)
75                 atm->ui_func(atm->data, atm->num);
76         next = atm->state[atm->num].handler(sock, atm->data);
77
78         if (atm->terminated)
79                 return TRUE;
80         if (atm->cancelled) {
81                 atm->terminate(sock, data);
82                 return TRUE;
83         }
84
85         if (next >= 0 && next <= atm->max && next != atm->num) {
86                 atm->num = next;
87                 atm->tag = sock_input_add(sock,
88                                           atm->state[atm->num].condition,       
89                                           automaton_input_cb,
90                                           data);
91         } else {
92                 atm->terminate(sock, data);
93         }
94
95         return TRUE;
96 }