]> git.gag.com Git - fw/altos/blob - ao-tools/ao-view/aoview_file.c
altoslib: Add support for EasyMega-v2
[fw/altos] / ao-tools / ao-view / aoview_file.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 char *aoview_file_dir;
22
23 #define ALTOS_DIR_PATH  "/apps/aoview/log_dir"
24 #define DEFAULT_DIR     "AltOS"
25
26 struct aoview_file {
27         char    *ext;
28         FILE    *file;
29         char    *name;
30         int     failed;
31         int     serial;
32         int     flight;
33         int     sequence;
34 };
35
36 static void
37 aoview_file_save_conf(void)
38 {
39         GConfClient     *gconf_client;
40
41         gconf_client = gconf_client_get_default();
42         if (gconf_client)
43         {
44                 gconf_client_set_string(gconf_client,
45                                         ALTOS_DIR_PATH,
46                                         aoview_file_dir,
47                                         NULL);
48                 g_object_unref(G_OBJECT(gconf_client));
49         }
50 }
51
52 static void
53 aoview_file_configure(GtkWidget *widget, gpointer data)
54 {
55         GtkFileChooser *chooser = data;
56         aoview_file_dir = gtk_file_chooser_get_filename(chooser);
57         aoview_file_save_conf();
58         gtk_widget_hide(GTK_WIDGET(chooser));
59 }
60
61 void
62 aoview_file_finish(struct aoview_file *file)
63 {
64         if (file->file) {
65                 fclose(file->file);
66                 file->file = NULL;
67                 free(file->name);
68                 file->name = NULL;
69         }
70         file->failed = 0;
71 }
72
73 const char *
74 aoview_file_name(struct aoview_file *file)
75 {
76         return file->name;
77 }
78
79 static GtkMessageDialog *file_fail_dialog;
80
81 static void
82 aoview_file_open_failed(char *name)
83 {
84         char    *utf8_file;
85         utf8_file = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
86         if (!utf8_file)
87                 utf8_file = name;
88         gtk_message_dialog_format_secondary_text(file_fail_dialog,
89                                                  "\"%s\"", utf8_file);
90         if (utf8_file != name)
91                 g_free(utf8_file);
92         gtk_widget_show(GTK_WIDGET(file_fail_dialog));
93 }
94
95 gboolean
96 aoview_file_start(struct aoview_file *file)
97 {
98         char            base[50];
99         char            seq[20];
100         struct tm       tm;
101         time_t          now;
102         char            *full;
103         int             r;
104
105         if (file->file)
106                 return TRUE;
107
108         if (file->failed)
109                 return FALSE;
110
111         full = cc_make_filename(file->serial, file->flight, file->ext);
112         file->file = fopen(full, "w");
113         if (!file->file) {
114                 aoview_file_open_failed(full);
115                 free(full);
116                 file->failed = 1;
117                 return FALSE;
118         } else {
119                 setlinebuf(file->file);
120                 file->name = full;
121                 return TRUE;
122         }
123 }
124
125 void
126 aoview_file_vprintf(struct aoview_file *file, char *format, va_list ap)
127 {
128         if (!aoview_file_start(file))
129                 return;
130         vfprintf(file->file, format, ap);
131 }
132
133 void
134 aoview_file_printf(struct aoview_file *file, char *format, ...)
135 {
136         va_list ap;
137
138         va_start(ap, format);
139         aoview_file_vprintf(file, format, ap);
140         va_end(ap);
141 }
142
143 struct aoview_file *
144 aoview_file_new(char *ext)
145 {
146         struct aoview_file      *file;
147
148         file = calloc (1, sizeof (struct aoview_file));
149         if (!file)
150                 return NULL;
151         file->ext = strdup(ext);
152         if (!file->ext) {
153                 free(file);
154                 return NULL;
155         }
156         return file;
157 }
158
159 void
160 aoview_file_destroy(struct aoview_file *file)
161 {
162         if (file->file)
163                 fclose(file->file);
164         if (file->name)
165                 free(file->name);
166         free(file->ext);
167         free(file);
168 }
169
170 void
171 aoview_file_set_serial(struct aoview_file *file, int serial)
172 {
173         if (serial != file->serial)
174                 aoview_file_finish(file);
175         file->serial = serial;
176 }
177
178 int
179 aoview_file_get_serial(struct aoview_file *file)
180 {
181         return file->serial;
182 }
183
184 void
185 aoview_file_set_flight(struct aoview_file *file, int flight)
186 {
187         if (flight != file->flight)
188                 aoview_file_finish(file);
189         file->flight = flight;
190 }
191
192 int
193 aoview_file_get_flight(struct aoview_file *file)
194 {
195         return file->flight;
196 }
197
198 void
199 aoview_file_init(GladeXML *xml)
200 {
201         GConfClient     *gconf_client;
202         char            *file_dir = NULL;
203         GtkFileChooser  *file_chooser_dialog;
204         GtkWidget       *file_configure_ok;
205
206         g_type_init();
207         gconf_client = gconf_client_get_default();
208         if (gconf_client)
209         {
210                 file_dir = gconf_client_get_string(gconf_client,
211                                                    ALTOS_DIR_PATH,
212                                                    NULL);
213                 g_object_unref(G_OBJECT(gconf_client));
214         }
215         if (!file_dir) {
216                 aoview_file_dir = aoview_fullname(getenv("HOME"), DEFAULT_DIR);
217                 aoview_file_save_conf();
218         } else {
219                 aoview_file_dir = strdup(file_dir);
220         }
221
222         file_chooser_dialog = GTK_FILE_CHOOSER(glade_xml_get_widget(xml, "file_chooser_dialog"));
223         assert(file_chooser_dialog);
224         gtk_file_chooser_set_filename(file_chooser_dialog, aoview_file_dir);
225
226         file_configure_ok = glade_xml_get_widget(xml, "file_configure_ok");
227         assert(file_configure_ok);
228
229         g_signal_connect(G_OBJECT(file_configure_ok), "clicked",
230                          G_CALLBACK(aoview_file_configure),
231                          file_chooser_dialog);
232
233
234         file_fail_dialog = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "file_fail_dialog"));
235         assert(file_fail_dialog);
236 }