summaryrefslogtreecommitdiff
path: root/ao-tools/ao-view/aoview_file.c
blob: 292887a02a5e6f97a7c9a3db11ea55f112f3c6a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "aoview.h"

char *aoview_file_dir;

#define ALTOS_DIR_PATH	"/apps/aoview/log_dir"
#define DEFAULT_DIR	"AltOS"

struct aoview_file {
	char	*ext;
	FILE	*file;
	char	*name;
	int	failed;
	int	serial;
	int	flight;
	int	sequence;
};

static void
aoview_file_save_conf(void)
{
	GConfClient	*gconf_client;

	gconf_client = gconf_client_get_default();
	if (gconf_client)
	{
		gconf_client_set_string(gconf_client,
					ALTOS_DIR_PATH,
					aoview_file_dir,
					NULL);
		g_object_unref(G_OBJECT(gconf_client));
	}
}

static void
aoview_file_configure(GtkWidget *widget, gpointer data)
{
	GtkFileChooser *chooser = data;
	aoview_file_dir = gtk_file_chooser_get_filename(chooser);
	aoview_file_save_conf();
	gtk_widget_hide(GTK_WIDGET(chooser));
}

void
aoview_file_finish(struct aoview_file *file)
{
	if (file->file) {
		fclose(file->file);
		file->file = NULL;
		free(file->name);
		file->name = NULL;
	}
	file->failed = 0;
}

const char *
aoview_file_name(struct aoview_file *file)
{
	return file->name;
}

static GtkMessageDialog *file_fail_dialog;

static void
aoview_file_open_failed(char *name)
{
	char	*utf8_file;
	utf8_file = g_filename_to_utf8(name, -1, NULL, NULL, NULL);
	if (!utf8_file)
		utf8_file = name;
	gtk_message_dialog_format_secondary_text(file_fail_dialog,
						 "\"%s\"", utf8_file);
	if (utf8_file != name)
		g_free(utf8_file);
	gtk_widget_show(GTK_WIDGET(file_fail_dialog));
}

gboolean
aoview_file_start(struct aoview_file *file)
{
	char		base[50];
	char		seq[20];
	struct tm	tm;
	time_t		now;
	char		*full;
	int		r;

	if (file->file)
		return TRUE;

	if (file->failed)
		return FALSE;

	full = cc_make_filename(file->serial, file->flight, file->ext);
	file->file = fopen(full, "w");
	if (!file->file) {
		aoview_file_open_failed(full);
		free(full);
		file->failed = 1;
		return FALSE;
	} else {
		setlinebuf(file->file);
		file->name = full;
		return TRUE;
	}
}

void
aoview_file_vprintf(struct aoview_file *file, char *format, va_list ap)
{
	if (!aoview_file_start(file))
		return;
	vfprintf(file->file, format, ap);
}

void
aoview_file_printf(struct aoview_file *file, char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	aoview_file_vprintf(file, format, ap);
	va_end(ap);
}

struct aoview_file *
aoview_file_new(char *ext)
{
	struct aoview_file	*file;

	file = calloc (1, sizeof (struct aoview_file));
	if (!file)
		return NULL;
	file->ext = strdup(ext);
	if (!file->ext) {
		free(file);
		return NULL;
	}
	return file;
}

void
aoview_file_destroy(struct aoview_file *file)
{
	if (file->file)
		fclose(file->file);
	if (file->name)
		free(file->name);
	free(file->ext);
	free(file);
}

void
aoview_file_set_serial(struct aoview_file *file, int serial)
{
	if (serial != file->serial)
		aoview_file_finish(file);
	file->serial = serial;
}

int
aoview_file_get_serial(struct aoview_file *file)
{
	return file->serial;
}

void
aoview_file_set_flight(struct aoview_file *file, int flight)
{
	if (flight != file->flight)
		aoview_file_finish(file);
	file->flight = flight;
}

int
aoview_file_get_flight(struct aoview_file *file)
{
	return file->flight;
}

void
aoview_file_init(GladeXML *xml)
{
	GConfClient	*gconf_client;
	char		*file_dir = NULL;
	GtkFileChooser	*file_chooser_dialog;
	GtkWidget	*file_configure_ok;

	g_type_init();
	gconf_client = gconf_client_get_default();
	if (gconf_client)
	{
		file_dir = gconf_client_get_string(gconf_client,
						   ALTOS_DIR_PATH,
						   NULL);
		g_object_unref(G_OBJECT(gconf_client));
	}
	if (!file_dir) {
		aoview_file_dir = aoview_fullname(getenv("HOME"), DEFAULT_DIR);
		aoview_file_save_conf();
	} else {
		aoview_file_dir = strdup(file_dir);
	}

	file_chooser_dialog = GTK_FILE_CHOOSER(glade_xml_get_widget(xml, "file_chooser_dialog"));
	assert(file_chooser_dialog);
	gtk_file_chooser_set_filename(file_chooser_dialog, aoview_file_dir);

	file_configure_ok = glade_xml_get_widget(xml, "file_configure_ok");
	assert(file_configure_ok);

	g_signal_connect(G_OBJECT(file_configure_ok), "clicked",
			 G_CALLBACK(aoview_file_configure),
			 file_chooser_dialog);


	file_fail_dialog = GTK_MESSAGE_DIALOG(glade_xml_get_widget(xml, "file_fail_dialog"));
	assert(file_fail_dialog);
}