Switch from GPLv2 to GPLv2+
[fw/altos] / ao-tools / ao-view / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "aoview.h"
20
21 static void
22 aoview_dev_dialog_map(GtkWidget *widget, gpointer data)
23 {
24         GtkTreeView     *dev_list = data;
25         GtkListStore    *list_store;
26         GtkTreeIter     iter;
27         int             ndev, n;
28         struct cc_usbdevs       *devs;
29         struct cc_usbdev        *dev;
30
31         list_store = gtk_list_store_new(3,
32                                         G_TYPE_STRING,
33                                         G_TYPE_INT,
34                                         G_TYPE_STRING);
35
36         devs = cc_usbdevs_scan();
37         if (devs) {
38                 for (n = 0; n < devs->ndev; n++) {
39                         dev = devs->dev[n];
40                         gtk_list_store_append(list_store, &iter);
41                         gtk_list_store_set(list_store, &iter,
42                                            0, dev->product,
43                                            1, dev->serial,
44                                            2, dev->tty,
45                                            -1);
46                 }
47         }
48         gtk_tree_view_set_model (dev_list, GTK_TREE_MODEL(list_store));
49         g_object_unref(G_OBJECT(list_store));
50         gtk_tree_view_columns_autosize(dev_list);
51         cc_usbdevs_free(devs);
52 }
53
54 static GtkMessageDialog *dev_open_fail_dialog;
55
56 static void
57 aoview_dev_open_failed(char *name)
58 {
59         char    *utf8_file;
60         utf8_file = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
61         if (!utf8_file)
62                 utf8_file = name;
63         gtk_message_dialog_format_secondary_text(dev_open_fail_dialog,
64                                                  "\"%s\"", utf8_file);
65         if (utf8_file != name)
66                 g_free(utf8_file);
67         gtk_dialog_run(GTK_DIALOG(dev_open_fail_dialog));
68         gtk_widget_hide(GTK_WIDGET(dev_open_fail_dialog));
69 }
70
71 gboolean        dialog_save_log;
72
73 static void
74 aoview_dev_selected(GtkTreeModel *model,
75                     GtkTreePath *path,
76                     GtkTreeIter *iter,
77                     gpointer data)
78 {
79         gchar *string;
80         gtk_tree_model_get(model, iter,
81                            2, &string,
82                            -1);
83         if (dialog_save_log) {
84                 dialog_save_log = FALSE;
85                 if (!aoview_eeprom_save(string))
86                         aoview_dev_open_failed(string);
87         } else {
88                 if (!aoview_monitor_connect(string))
89                         aoview_dev_open_failed(string);
90         }
91 }
92
93 static GtkWidget        *dialog;
94
95 static void
96 aoview_dev_dialog_connect(GtkWidget *widget, gpointer data)
97 {
98         GtkTreeView             *dev_list = data;
99         GtkListStore            *list_store;
100         GtkTreeSelection        *tree_selection;
101
102         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(dev_list));
103         tree_selection = gtk_tree_view_get_selection(dev_list);
104         gtk_tree_selection_selected_foreach(tree_selection,
105                                             aoview_dev_selected,
106                                             data);
107         gtk_widget_hide(dialog);
108 }
109
110 static void
111 aoview_dev_disconnect(GtkWidget *widget)
112 {
113         aoview_monitor_disconnect();
114 }
115
116 static void
117 aoview_dev_savelog(GtkWidget *widget, gpointer data)
118 {
119         dialog_save_log = TRUE;
120         gtk_widget_show(dialog);
121 }
122
123 #define _(a) a
124
125 void
126 aoview_dev_dialog_init(GladeXML *xml)
127 {
128         GtkTreeView     *dev_list;
129         GtkWidget       *connect_button;
130         GtkTreeSelection        *dev_selection;
131         GtkWidget       *ao_disconnect;
132         GtkWidget       *ao_savelog;
133
134         dialog = glade_xml_get_widget(xml, "device_connect_dialog");
135         assert(dialog);
136
137         dev_list = GTK_TREE_VIEW(glade_xml_get_widget(xml, "dev_list"));
138         assert(dev_list);
139
140         aoview_add_plain_text_column(dev_list, _("Product"), 0, 16);
141         aoview_add_plain_text_column(dev_list, _("Serial"),  1, 8);
142         aoview_add_plain_text_column(dev_list, _("Device"), 2, 13);
143
144         dev_selection = gtk_tree_view_get_selection(dev_list);
145         gtk_tree_selection_set_mode(dev_selection, GTK_SELECTION_SINGLE);
146
147         g_signal_connect(G_OBJECT(dialog), "map",
148                          G_CALLBACK(aoview_dev_dialog_map),
149                          dev_list);
150
151         connect_button = glade_xml_get_widget(xml, "connect_button");
152         assert(connect_button);
153
154         g_signal_connect(G_OBJECT(connect_button), "clicked",
155                          G_CALLBACK(aoview_dev_dialog_connect),
156                          dev_list);
157
158
159         ao_disconnect = glade_xml_get_widget(xml, "ao_disconnect");
160         assert(ao_disconnect);
161
162         g_signal_connect(G_OBJECT(ao_disconnect), "activate",
163                          G_CALLBACK(aoview_dev_disconnect),
164                          ao_disconnect);
165
166         ao_savelog = glade_xml_get_widget(xml, "ao_savelog");
167         assert(ao_savelog);
168
169         g_signal_connect(G_OBJECT(ao_savelog), "activate",
170                          G_CALLBACK(aoview_dev_savelog),
171                          dialog);
172         dev_open_fail_dialog = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "dev_open_fail_dialog"));
173         assert(dev_open_fail_dialog);
174 }