dlg-catalog-properties.c 6.96 KB
Newer Older
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2009 The Free Software Foundation, Inc.
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gtk/gtk.h>
#include "dlg-catalog-properties.h"
#include "gth-catalog.h"


#define GET_WIDGET(name) _gtk_builder_get_widget (data->builder, (name))


typedef struct {
	GthBrowser  *browser;
	GtkBuilder  *builder;
	GtkWidget   *dialog;
	GtkWidget   *time_selector;
	GthCatalog  *catalog;
	GthFileData *file_data;
	GFile       *original_file;
} DialogData;


static void
destroy_cb (GtkWidget  *widget,
	    DialogData *data)
{
	g_object_ref (data->file_data);
	_g_object_unref (data->catalog);
	g_object_unref (data->builder);
	g_free (data);
}


static void
catalog_saved_cb (void     *buffer,
		  gsize     count,
		  GError   *error,
		  gpointer  user_data)
{
	DialogData *data = user_data;

	if (error == NULL) {
		if (! g_file_equal (data->original_file, data->file_data->file)) {
			GFile *gio_file;

			gio_file = gth_catalog_file_to_gio_file (data->original_file);
			g_file_delete (gio_file, NULL, NULL);
			g_object_unref (gio_file);

			gth_monitor_file_renamed (gth_main_get_default_monitor (),
						  data->original_file,
						  data->file_data->file);
73

74
		}
75
76
		gth_catalog_update_metadata (data->catalog, data->file_data);
		gth_monitor_metadata_changed (gth_main_get_default_monitor (), data->file_data);
77
78

		gth_hook_invoke ("dlg-catalog-properties-saved", data->browser, data->file_data, data->catalog);
79
80
81
82
83
84
85
86
87
	}
	else
		_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->browser), _("Could not save the catalog"), &error);

	g_free (buffer);
	gtk_widget_destroy (data->dialog);
}


88
89
90
91
92
93
94
static char *
clear_name_for_file (const char *display_name)
{
	return _g_utf8_replace (display_name, "/", "_");
}


95
96
97
98
99
100
101
102
103
static void
save_button_clicked_cb (GtkButton  *button,
			DialogData *data)
{
	GthDateTime *date_time;
	GFile       *gio_file;
	char        *buffer;
	gsize        buffer_size;

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	if (strcmp (gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))), "") != 0) {
		GFile *parent;
		char  *uri;
		char  *clean_name;
		char  *display_name;
		GFile *new_file;

		parent = g_file_get_parent (data->original_file);
		uri = g_file_get_uri (data->original_file);
		clean_name = clear_name_for_file (gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))));
		display_name = g_strconcat (clean_name, _g_uri_get_file_extension (uri), NULL);
		new_file = g_file_get_child_for_display_name (parent, display_name, NULL);
		if ((new_file != NULL) && ! g_file_equal (new_file, data->original_file))
			gth_file_data_set_file (data->file_data, new_file);

		_g_object_unref (new_file);
		g_free (display_name);
		g_free (clean_name);
		g_free (uri);
		g_object_unref (parent);
	}
125

126
	gth_catalog_set_name (data->catalog, gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("name_entry"))));
127
128
129
130
131
132
133
134

	date_time = gth_datetime_new ();
	gth_time_selector_get_value (GTH_TIME_SELECTOR (data->time_selector), date_time);
	gth_catalog_set_date (data->catalog, date_time);
	gth_datetime_free (date_time);

	/* invoke the hook to save derived catalogs such as searches */

135
	gth_hook_invoke ("dlg-catalog-properties-save", data->builder, data->file_data, data->catalog);
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

	gio_file = gth_catalog_file_to_gio_file (data->file_data->file);
	buffer = gth_catalog_to_data (data->catalog, &buffer_size);
	g_write_file_async (gio_file,
			    buffer,
			    buffer_size,
			    G_PRIORITY_DEFAULT,
			    NULL,
			    catalog_saved_cb,
			    data);

	g_object_unref (gio_file);
}


static void
help_button_clicked_cb (GtkWidget  *widget,
			DialogData *data)
{
	show_help_dialog (GTK_WINDOW (data->dialog), "catalog-properties");
}


static void
catalog_ready_cb (GObject  *object,
		  GError   *error,
		  gpointer  user_data)
{
	DialogData *data = user_data;

	if (error != NULL) {
		_gtk_error_dialog_from_gerror_show (GTK_WINDOW(data->browser), _("Could not load the catalog"), &error);
		gtk_widget_destroy (data->dialog);
		return;
	}

	data->catalog = g_object_ref (object);
173

174
	if (gth_catalog_get_name (data->catalog) != NULL) {
175
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("name_entry")), gth_catalog_get_name (data->catalog));
176
	}
177
	else if (! gth_datetime_valid (gth_catalog_get_date (data->catalog))) {
178
179
180
181
182
183
184
185
186
187
188
189
190
191
		char *basename;
		char *name;
		char *utf8_name;

		basename = g_file_get_basename (data->file_data->file);
		name = _g_uri_remove_extension (basename);
		utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("name_entry")), utf8_name);

		g_free (utf8_name);
		g_free (name);
		g_free (basename);
	}

192
193
194
	gth_time_selector_set_value (GTH_TIME_SELECTOR (data->time_selector), gth_catalog_get_date (data->catalog));
	gth_hook_invoke ("dlg-catalog-properties", data->builder, data->file_data, data->catalog);
	gtk_widget_show (data->dialog);
195
196

	g_object_unref (object);
197
198
199
200
201
202
203
204
205
206
207
208
209
}


void
dlg_catalog_properties (GthBrowser  *browser,
			GthFileData *file_data)
{
	DialogData *data;

	g_return_if_fail (file_data != NULL);

	data = g_new0 (DialogData, 1);
	data->browser = browser;
210
	data->file_data = gth_file_data_dup (file_data);
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
	data->original_file = g_file_dup (data->file_data->file);
	data->builder = _gtk_builder_new_from_file ("catalog-properties.ui", "catalogs");
	data->dialog = GET_WIDGET ("properties_dialog");

	/* Set widgets data. */

	data->time_selector = gth_time_selector_new ();
	gth_time_selector_show_time (GTH_TIME_SELECTOR (data->time_selector), FALSE);
	gtk_widget_show (data->time_selector);
	gtk_box_pack_start (GTK_BOX (GET_WIDGET ("date_container_box")), data->time_selector, TRUE, TRUE, 0);

	/* Set the signals handlers. */

	g_signal_connect (G_OBJECT (data->dialog),
			  "destroy",
			  G_CALLBACK (destroy_cb),
			  data);
	g_signal_connect (G_OBJECT (GET_WIDGET ("save_button")),
			  "clicked",
			  G_CALLBACK (save_button_clicked_cb),
			  data);
	g_signal_connect_swapped (G_OBJECT (GET_WIDGET ("cancel_button")),
				  "clicked",
				  G_CALLBACK (gtk_widget_destroy),
				  data->dialog);
	g_signal_connect (G_OBJECT (GET_WIDGET ("help_button")),
			  "clicked",
			  G_CALLBACK (help_button_clicked_cb),
			  data);

	/* run dialog. */

	gtk_window_set_transient_for (GTK_WINDOW (data->dialog), GTK_WINDOW (browser));
	gtk_window_set_modal (GTK_WINDOW (data->dialog), TRUE);

	gth_catalog_load_from_file (file_data->file, NULL, catalog_ready_cb, data);
}