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
|
#include <glib/gi18n.h>
#include <gnome-autoar/gnome-autoar.h>
#include <eel/eel-vfs-extensions.h>
#include "nautilus-compress-dialog-controller.h"
#include "nautilus-global-preferences.h"
#define ZIP_DESCRIPTION _("Compatible with all operating systems.")
#define TAR_XZ_DESCRIPTION _("Smaller archives but Linux and Mac only.")
#define SEVEN_ZIP_DESCRIPTION _("Smaller archives but must be installed on Windows and Mac.")
struct _NautilusCompressDialogController
{
NautilusFileNameWidgetController parent_instance;
GtkWidget *compress_dialog;
GtkWidget *description_label;
GtkWidget *name_entry;
GtkWidget *zip_radio_button;
GtkWidget *tar_xz_radio_button;
GtkWidget *seven_zip_radio_button;
const char *extension;
gint response_handler_id;
};
G_DEFINE_TYPE (NautilusCompressDialogController, nautilus_compress_dialog_controller, NAUTILUS_TYPE_FILE_NAME_WIDGET_CONTROLLER);
static gboolean
nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetController *self,
gchar *name,
gchar **error_message)
{
if (strlen (name) == 0)
{
return FALSE;
}
if (strstr (name, "/") != NULL)
{
*error_message = _("Archive names cannot contain “/”.");
}
else if (strcmp (name, ".") == 0)
{
*error_message = _("An archive cannot be called “.”.");
}
else if (strcmp (name, "..") == 0)
{
*error_message = _("An archive cannot be called “..”.");
}
return *error_message == NULL;
}
static gchar *
nautilus_compress_dialog_controller_get_new_name (NautilusFileNameWidgetController *controller)
{
NautilusCompressDialogController *self;
g_autofree gchar *basename;
gchar *error_message = NULL;
gboolean valid_name;
self = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (controller);
basename = NAUTILUS_FILE_NAME_WIDGET_CONTROLLER_CLASS (nautilus_compress_dialog_controller_parent_class)->get_new_name (controller);
/* Do not check or add the extension if the name is invalid */
valid_name = nautilus_compress_dialog_controller_name_is_valid (controller,
basename,
&error_message);
if (!valid_name)
{
return g_strdup (basename);
}
if (g_str_has_suffix (basename, self->extension))
{
return g_strdup (basename);
}
return g_strconcat (basename, self->extension, NULL);
}
static void
compress_dialog_controller_on_response (GtkDialog *dialog,
gint response_id,
gpointer user_data)
{
NautilusCompressDialogController *controller;
controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
if (response_id != GTK_RESPONSE_OK)
{
g_signal_emit_by_name (controller, "cancelled");
}
}
static void
update_selected_format (NautilusCompressDialogController *self,
NautilusCompressionFormat format)
{
const char *extension;
const char *description;
GtkWidget *active_button;
switch (format)
{
case NAUTILUS_COMPRESSION_ZIP:
extension = ".zip";
description = ZIP_DESCRIPTION;
active_button = self->zip_radio_button;
break;
case NAUTILUS_COMPRESSION_TAR_XZ:
extension = ".tar.xz";
description = TAR_XZ_DESCRIPTION;
active_button = self->tar_xz_radio_button;
break;
case NAUTILUS_COMPRESSION_7ZIP:
extension = ".7z";
description = SEVEN_ZIP_DESCRIPTION;
active_button = self->seven_zip_radio_button;
break;
default:
g_assert_not_reached ();
break;
}
self->extension = extension;
gtk_label_set_text (GTK_LABEL (self->description_label),
description);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (active_button),
TRUE);
g_settings_set_enum (nautilus_compression_preferences,
NAUTILUS_PREFERENCES_DEFAULT_COMPRESSION_FORMAT,
format);
/* Since the extension changes when the button is toggled, force a
* verification of the new file name by simulating an entry change
*/
g_signal_emit_by_name (self->name_entry, "changed");
}
static void
zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
gpointer user_data)
{
NautilusCompressDialogController *controller;
controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
if (!gtk_toggle_button_get_active (toggle_button))
{
return;
}
update_selected_format (controller,
NAUTILUS_COMPRESSION_ZIP);
}
static void
tar_xz_radio_button_on_toggled (GtkToggleButton *toggle_button,
gpointer user_data)
{
NautilusCompressDialogController *controller;
controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
if (!gtk_toggle_button_get_active (toggle_button))
{
return;
}
update_selected_format (controller,
NAUTILUS_COMPRESSION_TAR_XZ);
}
static void
seven_zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
gpointer user_data)
{
NautilusCompressDialogController *controller;
controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
if (!gtk_toggle_button_get_active (toggle_button))
{
return;
}
update_selected_format (controller,
NAUTILUS_COMPRESSION_7ZIP);
}
NautilusCompressDialogController *
nautilus_compress_dialog_controller_new (GtkWindow *parent_window,
NautilusDirectory *destination_directory,
gchar *initial_name)
{
NautilusCompressDialogController *self;
g_autoptr (GtkBuilder) builder;
GtkWidget *compress_dialog;
GtkWidget *error_revealer;
GtkWidget *error_label;
GtkWidget *name_entry;
GtkWidget *activate_button;
GtkWidget *description_label;
GtkWidget *zip_radio_button;
GtkWidget *tar_xz_radio_button;
GtkWidget *seven_zip_radio_button;
NautilusCompressionFormat format;
builder = gtk_builder_new_from_resource ("/org/gnome/nautilus/ui/nautilus-compress-dialog.ui");
compress_dialog = GTK_WIDGET (gtk_builder_get_object (builder, "compress_dialog"));
error_revealer = GTK_WIDGET (gtk_builder_get_object (builder, "error_revealer"));
error_label = GTK_WIDGET (gtk_builder_get_object (builder, "error_label"));
name_entry = GTK_WIDGET (gtk_builder_get_object (builder, "name_entry"));
activate_button = GTK_WIDGET (gtk_builder_get_object (builder, "activate_button"));
zip_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "zip_radio_button"));
tar_xz_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "tar_xz_radio_button"));
seven_zip_radio_button = GTK_WIDGET (gtk_builder_get_object (builder, "seven_zip_radio_button"));
description_label = GTK_WIDGET (gtk_builder_get_object (builder, "description_label"));
gtk_window_set_transient_for (GTK_WINDOW (compress_dialog),
parent_window);
self = g_object_new (NAUTILUS_TYPE_COMPRESS_DIALOG_CONTROLLER,
"error-revealer", error_revealer,
"error-label", error_label,
"name-entry", name_entry,
"activate-button", activate_button,
"containing-directory", destination_directory, NULL);
self->compress_dialog = compress_dialog;
self->zip_radio_button = zip_radio_button;
self->tar_xz_radio_button = tar_xz_radio_button;
self->seven_zip_radio_button = seven_zip_radio_button;
self->description_label = description_label;
self->name_entry = name_entry;
self->response_handler_id = g_signal_connect (compress_dialog,
"response",
(GCallback) compress_dialog_controller_on_response,
self);
gtk_builder_add_callback_symbols (builder,
"zip_radio_button_on_toggled",
G_CALLBACK (zip_radio_button_on_toggled),
"tar_xz_radio_button_on_toggled",
G_CALLBACK (tar_xz_radio_button_on_toggled),
"seven_zip_radio_button_on_toggled",
G_CALLBACK (seven_zip_radio_button_on_toggled),
NULL);
gtk_builder_connect_signals (builder, self);
format = g_settings_get_enum (nautilus_compression_preferences,
NAUTILUS_PREFERENCES_DEFAULT_COMPRESSION_FORMAT);
update_selected_format (self, format);
if (initial_name != NULL)
{
gtk_entry_set_text (GTK_ENTRY (name_entry), initial_name);
}
gtk_widget_show_all (compress_dialog);
return self;
}
static void
nautilus_compress_dialog_controller_init (NautilusCompressDialogController *self)
{
}
static void
nautilus_compress_dialog_controller_finalize (GObject *object)
{
NautilusCompressDialogController *self;
self = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (object);
if (self->compress_dialog != NULL)
{
if (self->response_handler_id > 0)
{
g_signal_handler_disconnect (self->compress_dialog,
self->response_handler_id);
self->response_handler_id = 0;
}
gtk_widget_destroy (self->compress_dialog);
self->compress_dialog = NULL;
}
G_OBJECT_CLASS (nautilus_compress_dialog_controller_parent_class)->finalize (object);
}
static void
nautilus_compress_dialog_controller_class_init (NautilusCompressDialogControllerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NautilusFileNameWidgetControllerClass *parent_class = NAUTILUS_FILE_NAME_WIDGET_CONTROLLER_CLASS (klass);
object_class->finalize = nautilus_compress_dialog_controller_finalize;
parent_class->get_new_name = nautilus_compress_dialog_controller_get_new_name;
parent_class->name_is_valid = nautilus_compress_dialog_controller_name_is_valid;
}
|