nautilus-compress-dialog-controller.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* nautilus-compress-dialog-controller.h
 *
 * Copyright (C) 2016 the Nautilus developers
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

20 21 22 23 24 25 26 27 28
#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"

29 30 31
struct _NautilusCompressDialogController
{
    NautilusFileNameWidgetController parent_instance;
32

33
    GtkWidget *compress_dialog;
34
    GtkWidget *description_stack;
35 36 37 38
    GtkWidget *name_entry;
    GtkWidget *zip_radio_button;
    GtkWidget *tar_xz_radio_button;
    GtkWidget *seven_zip_radio_button;
39

40
    const char *extension;
41

42
    gint response_handler_id;
43 44 45 46 47 48 49 50 51
};

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)
{
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    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;
71 72 73 74 75
}

static gchar *
nautilus_compress_dialog_controller_get_new_name (NautilusFileNameWidgetController *controller)
{
76
    NautilusCompressDialogController *self;
77
    g_autofree gchar *basename = NULL;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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);
100 101 102 103 104 105 106
}

static void
compress_dialog_controller_on_response (GtkDialog *dialog,
                                        gint       response_id,
                                        gpointer   user_data)
{
107
    NautilusCompressDialogController *controller;
108

109
    controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
110

111 112 113 114
    if (response_id != GTK_RESPONSE_OK)
    {
        g_signal_emit_by_name (controller, "cancelled");
    }
115 116 117 118 119 120
}

static void
update_selected_format (NautilusCompressDialogController *self,
                        NautilusCompressionFormat         format)
{
121
    const char *extension;
122
    const char *description_label_name;
123 124 125 126 127
    GtkWidget *active_button;

    switch (format)
    {
        case NAUTILUS_COMPRESSION_ZIP:
128
        {
129
            extension = ".zip";
130
            description_label_name = "zip-description-label";
131
            active_button = self->zip_radio_button;
132 133
        }
        break;
134 135

        case NAUTILUS_COMPRESSION_TAR_XZ:
136
        {
137
            extension = ".tar.xz";
138
            description_label_name = "tar-xz-description-label";
139
            active_button = self->tar_xz_radio_button;
140 141
        }
        break;
142 143

        case NAUTILUS_COMPRESSION_7ZIP:
144
        {
145
            extension = ".7z";
146
            description_label_name = "seven-zip-description-label";
147
            active_button = self->seven_zip_radio_button;
148 149
        }
        break;
150 151

        default:
152
        {
153
            g_assert_not_reached ();
154 155
        }
        break;
156 157 158 159
    }

    self->extension = extension;

160 161
    gtk_stack_set_visible_child_name (GTK_STACK (self->description_stack),
                                      description_label_name);
162 163 164 165 166 167 168 169 170 171 172

    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");
173 174 175 176 177 178
}

static void
zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
                             gpointer         user_data)
{
179
    NautilusCompressDialogController *controller;
180

181
    controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
182

183 184 185 186
    if (!gtk_toggle_button_get_active (toggle_button))
    {
        return;
    }
187

188 189
    update_selected_format (controller,
                            NAUTILUS_COMPRESSION_ZIP);
190 191 192 193 194 195
}

static void
tar_xz_radio_button_on_toggled (GtkToggleButton *toggle_button,
                                gpointer         user_data)
{
196
    NautilusCompressDialogController *controller;
197

198
    controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
199

200 201 202 203
    if (!gtk_toggle_button_get_active (toggle_button))
    {
        return;
    }
204

205 206
    update_selected_format (controller,
                            NAUTILUS_COMPRESSION_TAR_XZ);
207 208 209 210 211 212
}

static void
seven_zip_radio_button_on_toggled (GtkToggleButton *toggle_button,
                                   gpointer         user_data)
{
213
    NautilusCompressDialogController *controller;
214

215
    controller = NAUTILUS_COMPRESS_DIALOG_CONTROLLER (user_data);
216

217 218 219 220
    if (!gtk_toggle_button_get_active (toggle_button))
    {
        return;
    }
221

222 223
    update_selected_format (controller,
                            NAUTILUS_COMPRESSION_7ZIP);
224 225 226 227 228 229 230
}

NautilusCompressDialogController *
nautilus_compress_dialog_controller_new (GtkWindow         *parent_window,
                                         NautilusDirectory *destination_directory,
                                         gchar             *initial_name)
{
231
    NautilusCompressDialogController *self;
232
    g_autoptr (GtkBuilder) builder = NULL;
233 234 235 236 237
    GtkWidget *compress_dialog;
    GtkWidget *error_revealer;
    GtkWidget *error_label;
    GtkWidget *name_entry;
    GtkWidget *activate_button;
238
    GtkWidget *description_stack;
239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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"));
253
    description_stack = GTK_WIDGET (gtk_builder_get_object (builder, "description_stack"));
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

    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;
269
    self->description_stack = description_stack;
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
    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;
300 301 302 303 304 305 306 307 308 309
}

static void
nautilus_compress_dialog_controller_init (NautilusCompressDialogController *self)
{
}

static void
nautilus_compress_dialog_controller_finalize (GObject *object)
{
310 311 312 313 314 315 316 317 318 319 320
    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;
321
        }
322 323 324
        gtk_widget_destroy (self->compress_dialog);
        self->compress_dialog = NULL;
    }
325

326
    G_OBJECT_CLASS (nautilus_compress_dialog_controller_parent_class)->finalize (object);
327 328 329 330 331
}

static void
nautilus_compress_dialog_controller_class_init (NautilusCompressDialogControllerClass *klass)
{
332 333
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    NautilusFileNameWidgetControllerClass *parent_class = NAUTILUS_FILE_NAME_WIDGET_CONTROLLER_CLASS (klass);
334

335
    object_class->finalize = nautilus_compress_dialog_controller_finalize;
336

337 338
    parent_class->get_new_name = nautilus_compress_dialog_controller_get_new_name;
    parent_class->name_is_valid = nautilus_compress_dialog_controller_name_is_valid;
339
}