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