Handle disappearing serial devices
[fw/altos] / aoview / aoview_dev_dialog.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "aoview.h"
19
20 static void
21 aoview_dev_dialog_map(GtkWidget *widget, gpointer data)
22 {
23         GtkTreeView     *dev_list = data;
24         GtkListStore    *list_store;
25         GtkTreeIter     iter;
26         int             ndev, n;
27         struct usbdev   **devs;
28
29         list_store = gtk_list_store_new(3,
30                                         G_TYPE_STRING,
31                                         G_TYPE_STRING,
32                                         G_TYPE_STRING);
33
34         ndev = aoview_usb_scan(&devs);
35         for (n = 0; n < ndev; n++) {
36                 gtk_list_store_append(list_store, &iter);
37                 gtk_list_store_set(list_store, &iter,
38                                    0, devs[n]->product,
39                                    1, devs[n]->serial,
40                                    2, devs[n]->tty,
41                                    -1);
42         }
43         gtk_tree_view_set_model (dev_list, GTK_TREE_MODEL(list_store));
44         g_object_unref(G_OBJECT(list_store));
45         gtk_tree_view_columns_autosize(dev_list);
46 }
47
48 static GtkMessageDialog *dev_open_fail_dialog;
49
50 static void
51 aoview_dev_open_failed(char *name)
52 {
53         char    *utf8_file;
54         utf8_file = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
55         if (!utf8_file)
56                 utf8_file = name;
57         gtk_message_dialog_format_secondary_text(dev_open_fail_dialog,
58                                                  "\"%s\"", utf8_file);
59         if (utf8_file != name)
60                 g_free(utf8_file);
61         gtk_dialog_run(GTK_DIALOG(dev_open_fail_dialog));
62         gtk_widget_hide(GTK_WIDGET(dev_open_fail_dialog));
63 }
64
65 static void
66 aoview_dev_selected(GtkTreeModel *model,
67                     GtkTreePath *path,
68                     GtkTreeIter *iter,
69                     gpointer data)
70 {
71         gchar *string;
72         gtk_tree_model_get(model, iter,
73                            2, &string,
74                            -1);
75         if (!aoview_monitor_connect(string))
76                 aoview_dev_open_failed(string);
77 }
78
79 static GtkWidget        *dialog;
80
81 static void
82 aoview_dev_dialog_connect(GtkWidget *widget, gpointer data)
83 {
84         GtkTreeView             *dev_list = data;
85         GtkListStore            *list_store;
86         GtkTreeSelection        *tree_selection;
87
88         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(dev_list));
89         tree_selection = gtk_tree_view_get_selection(dev_list);
90         gtk_tree_selection_selected_foreach(tree_selection,
91                                             aoview_dev_selected,
92                                             data);
93
94         gtk_widget_hide(dialog);
95 }
96
97 static void
98 aoview_dev_disconnect(GtkWidget *widget)
99 {
100         aoview_monitor_disconnect();
101 }
102
103 #define _(a) a
104
105 void
106 aoview_dev_dialog_init(GladeXML *xml)
107 {
108         GtkTreeView     *dev_list;
109         GtkWidget       *connect_button;
110         GtkTreeSelection        *dev_selection;
111         GtkWidget       *ao_disconnect;
112
113         dialog = glade_xml_get_widget(xml, "device_connect_dialog");
114         assert(dialog);
115
116         dev_list = GTK_TREE_VIEW(glade_xml_get_widget(xml, "dev_list"));
117         assert(dev_list);
118
119         aoview_add_plain_text_column(dev_list, _("Product"), 0, 16);
120         aoview_add_plain_text_column(dev_list, _("Serial"),  1, 8);
121         aoview_add_plain_text_column(dev_list, _("Device"), 2, 13);
122
123         dev_selection = gtk_tree_view_get_selection(dev_list);
124         gtk_tree_selection_set_mode(dev_selection, GTK_SELECTION_SINGLE);
125
126         g_signal_connect(G_OBJECT(dialog), "map",
127                          G_CALLBACK(aoview_dev_dialog_map),
128                          dev_list);
129
130         connect_button = glade_xml_get_widget(xml, "connect_button");
131         assert(connect_button);
132
133         g_signal_connect(G_OBJECT(connect_button), "clicked",
134                          G_CALLBACK(aoview_dev_dialog_connect),
135                          dev_list);
136
137
138         ao_disconnect = glade_xml_get_widget(xml, "ao_disconnect");
139         assert(ao_disconnect);
140
141         g_signal_connect(G_OBJECT(ao_disconnect), "activate",
142                          G_CALLBACK(aoview_dev_disconnect),
143                          ao_disconnect);
144
145         dev_open_fail_dialog = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "dev_open_fail_dialog"));
146         assert(dev_open_fail_dialog);
147 }