summaryrefslogtreecommitdiff
path: root/siv.c
blob: e9927326fbb0a1235eeccc94ad89cd341d991498 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "siv.h"

#define _(a) a

struct App
{
    int		n_windows;

    GtkWidget *	chooser;
}; 

void
app_show_warning (GtkWidget *parent_window,
		  const gchar *secondary,
		  const gchar *format,
		  ...)
{
    va_list args;
    char *message;
    GtkWidget *dialog;
    
    va_start (args, format);
    g_vasprintf (&message, format, args);
    va_end (args);
    
    dialog = gtk_message_dialog_new_with_markup (
	parent_window ? GTK_WINDOW (parent_window) : NULL,
	GTK_DIALOG_DESTROY_WITH_PARENT,
	GTK_MESSAGE_WARNING,
	GTK_BUTTONS_OK, message);

    if (secondary)
    {
	gtk_message_dialog_format_secondary_markup (
	    GTK_MESSAGE_DIALOG (dialog), "%s", secondary);
    }
    
    g_free (message);
    
    gtk_window_set_title (GTK_WINDOW (dialog), APPLICATION_NAME " Warning");
    
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

void
app_show_could_not_open (GtkWidget  *parent_window,
			 int	     n_files,
			 gchar     **files)
{
    const char *header = NULL;
    GString *text;
    int i;
    
    if (n_files == 0)
    {
	return;
    }
    else if (n_files == 1)
    {
	header = "<b>Could not open this file:</b>";
    }
    else
    {
	header = "<b>Could not open these files:</b>";
    }

    text = g_string_new (header);
    
    for (i = 0; i < n_files; ++i)
	g_string_append_printf (text, "\n%s", files[i]);
    
    app_show_warning (NULL, NULL, "%s", text->str);

    g_string_free (text, TRUE);
}

void
app_register_window (App       *app,
		     SivWindow *window)
{
    ++app->n_windows;
}

void
app_unregister_window (App *app, SivWindow *window)
{
    if (--app->n_windows == 0)
	gtk_main_quit ();
}

GtkWidget *
app_get_open_chooser (App *app)
{
    if (!app->chooser)
    {
	app->chooser =
	    gtk_file_chooser_dialog_new ("Open",
					 NULL,
					 GTK_FILE_CHOOSER_ACTION_OPEN,
					 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					 NULL);
    }

    return app->chooser;
}

static gchar *
encode (const char *filename)
{
    GString *result = g_string_new (NULL);
    int i;

    for (i = 0; filename[i] != '\0'; ++i)
    {
	char c = filename[i];
	
	if (!g_ascii_isprint (c) || c == '@' || c == '[' || c == ']')
	    g_string_append_printf (result, "@%x", c);
	else
	    g_string_append_c (result, c);
    }

    return g_string_free (result, FALSE);
}

static gboolean
get_int (GKeyFile *keyfile, const char *group, const char *key, int *result)
{
    GError *err = NULL;
    int d;

    d = g_key_file_get_integer (keyfile, group, key, &err);

    if (err)
    {
	g_error_free (err);
	return FALSE;
    }

    if (result)
	*result = d;

    return TRUE;
}

static gchar *
make_filename (void)
{
    return g_build_filename (
	g_get_home_dir(), ".gnome2", "siv.metadata", NULL);
}

static GHashTable *
load_meta_data (void)
{
    GKeyFile *keyfile;
    char *filename;
    GHashTable *result;

    keyfile = g_key_file_new ();
    filename = make_filename();

    result = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
    
    if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
    {
	gsize n_groups;
	char **groups = g_key_file_get_groups (keyfile, &n_groups);
	int i;

	for (i = 0; i < n_groups; ++i)
	{
	    MetaData *data = g_new0 (MetaData, 1);
	    char *group = groups[i];
	    int b;
	    
	    if (!get_int (keyfile, group, "window_x", &(data->window_x)))
		data->window_x = 0;
	    
	    if (!get_int (keyfile, group, "window_y", &(data->window_y)))
		data->window_y = 0;
	    
	    if (!get_int (keyfile, group, "window_width", &(data->window_width)))
		data->window_width = 0;
	    
	    if (!get_int (keyfile, group, "window_height", &(data->window_height)))
		data->window_height = 0;
	    
	    if (!get_int (keyfile, group, "background", &b))
		data->background = BG_NONE;
	    else
		data->background = b;
	    
	    if (data->background >= BG_LAST || data->background < BG_FIRST)
		data->background = BG_NONE;
	    
	    if (!get_int (keyfile, group, "smooth_image", &(data->smooth_image)))
		data->smooth_image = TRUE;
	    
	    if (!get_int (keyfile, group, "zoom_level", &(data->zoom_level)))
		data->zoom_level = 0;

	    if (!get_int (keyfile, group, "vadj", &(data->vadj)))
		data->vadj = 0;

	    if (!get_int (keyfile, group, "hadj", &(data->hadj)))
		data->hadj = 0;
	    
	    g_hash_table_insert (result, g_strdup (group), data);
	}
	
	g_strfreev (groups);
    }

    g_free (filename);    
    g_key_file_free (keyfile);
    
    return result;
}

gboolean
app_get_meta_data (App            *app,
		   const char     *file,
		   MetaData       *data)
{
    char *encoded = encode (file);
    GHashTable *table = load_meta_data ();
    MetaData *d = g_hash_table_lookup (table, encoded);
    gboolean result = FALSE;
    
    if (d)
    {
	if (data)
	    *data = *d;
	
	result = TRUE;
    }

    g_free (encoded);
    g_hash_table_destroy (table);

    return result;
}

static void
foreach (gpointer key, gpointer value, gpointer user_data)
{
    gchar *filename = key;
    MetaData *meta = value;
    GKeyFile *key_file = user_data;

    g_key_file_set_integer (key_file, filename, "window_x", meta->window_x);
    g_key_file_set_integer (key_file, filename, "window_y", meta->window_y);
    g_key_file_set_integer (key_file, filename, "window_width", meta->window_width);
    g_key_file_set_integer (key_file, filename, "window_height", meta->window_height);
    g_key_file_set_integer (key_file, filename, "background", meta->background);
    g_key_file_set_integer (key_file, filename, "smooth_image", meta->smooth_image);
    g_key_file_set_integer (key_file, filename, "zoom_level", meta->zoom_level);
    g_key_file_set_integer (key_file, filename, "hadj", meta->hadj);
    g_key_file_set_integer (key_file, filename, "vadj", meta->vadj);
}

void
app_set_meta_data (App            *app,
		   const char     *filename,
		   int             window_x,
		   int             window_y,
		   int             window_width,
		   int             window_height,
		   gboolean        smooth_image,
		   BackgroundType  background,
		   int             zoom_level,
		   int             vadj,
		   int             hadj)
{
    GKeyFile *keyfile = g_key_file_new ();
    char *encoded = encode (filename);
    GHashTable *table = load_meta_data ();
    MetaData *data = g_hash_table_lookup (table, encoded);
    char *key_filename = make_filename();
    char *output;

    if (!data)
    {
	data = g_new0 (MetaData, 1);

	g_hash_table_insert (table, g_strdup (encoded), data);
    }

    data->window_x = window_x;
    data->window_y = window_y;
    data->window_width = window_width;
    data->window_height = window_height;
    data->background = background;
    data->smooth_image = smooth_image;
    data->zoom_level = zoom_level;
    data->hadj = hadj;
    data->vadj = vadj;

    g_hash_table_foreach (table, foreach, keyfile);

    output = g_key_file_to_data (keyfile, NULL, NULL);
    if (output)
    {
	g_file_set_contents (key_filename, output, -1, NULL);
	g_free (output);
    }

    g_key_file_free (keyfile);
    g_free (key_filename);
    g_free (encoded);

    g_hash_table_destroy (table);
}


static void
app_new (int argc, char **argv)
{
    GList *filenames = NULL;
    GList *list;
    App *app;
    int i;
    GPtrArray *err_files = g_ptr_array_new ();

    for (i = 1; i < argc; ++i)
    {
	char *option = argv[i];
	char *name;

	if (strcmp (option, "--version") == 0	||
	    strcmp (option, "-v") == 0)
	{
	    g_print ("%s %s\n", APPLICATION_NAME, PACKAGE_VERSION);
	    
	    exit (1);
	    return;
	}

	if (g_path_is_absolute (option))
	{
	    name = g_strdup (option);
	}
	else
	{
	    name = g_build_filename (g_get_current_dir(), option, NULL);
	}

	filenames = g_list_prepend (filenames, name);
    }

    app = g_new0 (App, 1);
	
    filenames = g_list_reverse (filenames);
    
    /* When a window is created, it increases the app->n_windows counter;
     * when it is destroyed it decreases it. The problem is if the first
     * window fails to load the file, it will cause the counter to reach
     * zero, which will cause the application to exit.
     *
     * So set it to 1 here and decrement it after loading all the files to
     * prevent that from happening.
     */
    app->n_windows = 1;
    
    if (filenames)
    {
	int i;

	i = 0;
	for (list = filenames; list != NULL; list = list->next)
	{
	    SivWindow *window;
	    GError *err = NULL;
	    char *filename = list->data;

	    /* Don't open more than 32 windows */
	    if (++i > 32)
		break;
	    
	    window = window_new (app);
	    
	    if (!window_load_file (window, list->data, &err))
	    {
		g_ptr_array_add (err_files, g_strdup (filename));
		g_error_free (err);
		
		window_free (window);
	    }
	    else
	    {
		window_show (window, GDK_CURRENT_TIME);
	    }
	}

	for (list = filenames; list != NULL; list = list->next)
	    g_free (list->data);
	
	g_list_free (filenames);
    }
    else
    {
	window_show (window_new (app), GDK_CURRENT_TIME);
    }

    app_show_could_not_open (NULL, err_files->len, (char **)err_files->pdata);

    g_ptr_array_foreach (err_files, (GFunc)g_free, NULL);
    g_ptr_array_free (err_files, TRUE);

    if (app->n_windows == 1)
	exit (1);
    else
	--app->n_windows;
}

int
main (int    argc,
      char **argv)
{
    /* Disable gslice, since it
     *
     *  - confuses valgrind
     *  - caches too much memory
     *  - hides memory access bugs
     *  - is not faster than malloc
     *
     * Note that g_slice_set_config() is broken in some versions of
     * GLib (and 'declared internal' according to Tim), so we use the
     * environment variable instead.
     */
    if (!getenv ("G_SLICE"))
	putenv ("G_SLICE=always_malloc");
    
    gtk_init (&argc, &argv);

    if (!g_file_test (GLADE_FILE, G_FILE_TEST_EXISTS))
    {
	app_show_warning (
	    NULL,
	    "<i>Running \"make install\" may solve this problem.</i>",
	    "<b>"APPLICATION_NAME
	    " was not compiled or installed correctly.</b>");
	
	return FALSE;
    }

    app_new (argc, argv);

    gtk_main ();
    
    return 0;
}