nautilus-burn-bar.c 15.2 KB
Newer Older
1 2 3
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2005 William Jon McCann <mccann@jhu.edu>
4
 * Copyright (C) 2008 Philippe Rouquier <bonfire-app@wanadoo.fr>
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 20 21 22 23
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

24 25 26
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
27 28 29 30 31 32

#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>

#include "nautilus-burn-bar.h"

33
static void nautilus_disc_burn_bar_finalize   (GObject *object);
34

35
#define NAUTILUS_DISC_BURN_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NAUTILUS_TYPE_DISC_BURN_BAR, NautilusDiscBurnBarPrivate))
36

37
struct NautilusDiscBurnBarPrivate
38
{
39 40 41
        GtkWidget  *button;
        GtkWidget  *title;
        gchar      *icon_path;
42 43 44
};

enum {
45 46 47 48
        TITLE_CHANGED,
        ICON_CHANGED,
        ACTIVATE,
        LAST_SIGNAL
49 50 51 52
};

static guint           signals [LAST_SIGNAL] = { 0, };

53
G_DEFINE_TYPE (NautilusDiscBurnBar, nautilus_disc_burn_bar, GTK_TYPE_BOX)
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
const gchar *
nautilus_disc_burn_bar_get_icon (NautilusDiscBurnBar *bar)
{
        g_return_val_if_fail (bar != NULL, NULL);
        return bar->priv->icon_path;
}

void
nautilus_disc_burn_bar_set_icon (NautilusDiscBurnBar *bar,
                                 const gchar *icon_path)
{
        g_return_if_fail (bar != NULL);

        if (bar->priv->icon_path)
                g_free (bar->priv->icon_path);

        bar->priv->icon_path = g_strdup (icon_path);

        if (bar->priv->icon_path) {
                GIcon *icon;
                GFile *file;

                file = g_file_new_for_path (bar->priv->icon_path);
		icon = g_file_icon_new (file);
                g_object_unref (file);
                gtk_entry_set_icon_from_gicon (GTK_ENTRY (bar->priv->title),
                                               GTK_ENTRY_ICON_PRIMARY,
                                               icon);
                g_object_unref (icon);
        }
	else
                gtk_entry_set_icon_from_icon_name (GTK_ENTRY (bar->priv->title),
						   GTK_ENTRY_ICON_PRIMARY,
						   "media-optical");
}

const gchar *
nautilus_disc_burn_bar_get_title (NautilusDiscBurnBar *bar)
{
        g_return_val_if_fail (bar != NULL, NULL);
        return gtk_entry_get_text (GTK_ENTRY (bar->priv->title));
}

void
nautilus_disc_burn_bar_set_title (NautilusDiscBurnBar *bar,
                                  const gchar *title)
{
        g_return_if_fail (bar != NULL);

        if (!title) {
                time_t  t;
                gchar  *title_str;
                gchar   buffer [128];

                t = time (NULL);
                strftime (buffer, sizeof (buffer), "%d %b %y", localtime (&t));

	        /* NOTE to translators: the final string must not be over
		 * 32 _bytes_ otherwise it gets truncated.
		 * The %s is the date */
		title_str = g_strdup_printf (_("Data disc (%s)"), buffer);

		if (strlen (title_str) > 32) {
			g_free (title_str);
			strftime (buffer, sizeof (buffer), "%F", localtime (&t));
			title_str = g_strdup_printf ("Data disc %s", buffer);
		}

                gtk_entry_set_text (GTK_ENTRY (bar->priv->title), title_str);
        }
        else
                gtk_entry_set_text (GTK_ENTRY (bar->priv->title), title);
}

129
GtkWidget *
130
nautilus_disc_burn_bar_get_button (NautilusDiscBurnBar *bar)
131 132 133 134 135 136 137 138 139 140 141
{
        GtkWidget *button;

        g_return_val_if_fail (bar != NULL, NULL);

        button = bar->priv->button;

        return button;
}

static void
142
nautilus_disc_burn_bar_set_property (GObject            *object,
143 144 145 146 147 148 149 150 151 152 153 154
                                guint               prop_id,
                                const GValue       *value,
                                GParamSpec         *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
155
nautilus_disc_burn_bar_get_property (GObject    *object,
156 157 158 159 160 161 162 163 164 165 166 167
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
168
nautilus_disc_burn_bar_class_init (NautilusDiscBurnBarClass *klass)
169 170 171
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

172 173 174
        object_class->finalize     = nautilus_disc_burn_bar_finalize;
        object_class->get_property = nautilus_disc_burn_bar_get_property;
        object_class->set_property = nautilus_disc_burn_bar_set_property;
175

176
        g_type_class_add_private (klass, sizeof (NautilusDiscBurnBarPrivate));
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191
        signals [TITLE_CHANGED] = g_signal_new ("title_changed",
                                                G_TYPE_FROM_CLASS (klass),
                                                G_SIGNAL_RUN_LAST,
                                                G_STRUCT_OFFSET (NautilusDiscBurnBarClass, title_changed),
                                                NULL, NULL,
                                                g_cclosure_marshal_VOID__VOID,
                                                G_TYPE_NONE, 0);
        signals [ICON_CHANGED] = g_signal_new ("icon_changed",
                                               G_TYPE_FROM_CLASS (klass),
                                               G_SIGNAL_RUN_LAST,
                                               G_STRUCT_OFFSET (NautilusDiscBurnBarClass, icon_changed),
                                               NULL, NULL,
                                               g_cclosure_marshal_VOID__VOID,
                                               G_TYPE_NONE, 0);
192 193 194
        signals [ACTIVATE] = g_signal_new ("activate",
                                           G_TYPE_FROM_CLASS (klass),
                                           G_SIGNAL_RUN_LAST,
195
                                           G_STRUCT_OFFSET (NautilusDiscBurnBarClass, activate),
196 197 198 199 200 201 202
                                           NULL, NULL,
                                           g_cclosure_marshal_VOID__VOID,
                                           G_TYPE_NONE, 0);
}

static void
button_clicked_cb (GtkWidget       *button,
203
                   NautilusDiscBurnBar *bar)
204 205 206 207
{
        g_signal_emit (bar, signals [ACTIVATE], 0);
}

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
static void
nautilus_disc_burn_bar_title_changed (GtkEditable *editable,
                                      NautilusDiscBurnBar *bar)
{
	g_signal_emit (bar,
		       signals [TITLE_CHANGED],
		       0);
}

static void
nautilus_disc_burn_bar_icon_button_clicked (GtkEntry *entry,
                                            GtkEntryIconPosition position,
                                            GdkEvent *event,
                                            NautilusDiscBurnBar *bar)
{
        GtkFileFilter *filter;
	GtkWidget *chooser;
	gchar *path;
	gint res;

	chooser = gtk_file_chooser_dialog_new (_("Medium Icon"),
					       GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (bar))),
					       GTK_FILE_CHOOSER_ACTION_OPEN,
					       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					       GTK_STOCK_OK, GTK_RESPONSE_OK,
					       NULL);

	filter = gtk_file_filter_new ();
	gtk_file_filter_set_name (filter, _("All files"));
	gtk_file_filter_add_pattern (filter, "*");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

	filter = gtk_file_filter_new ();
	/* Translators: this is an image, a picture, not a "Disc Image" */
	gtk_file_filter_set_name (filter, C_("picture", "Image files"));
	gtk_file_filter_add_mime_type (filter, "image/*");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), filter);

        if (bar->priv->icon_path)
		gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (chooser), bar->priv->icon_path);

	gtk_widget_show (chooser);
	res = gtk_dialog_run (GTK_DIALOG (chooser));
	if (res != GTK_RESPONSE_OK) {
		gtk_widget_destroy (chooser);
		return;
	}

	path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
	gtk_widget_destroy (chooser);

        nautilus_disc_burn_bar_set_icon (bar, path);
        g_free (path);

        g_signal_emit (bar,
                       signals [ICON_CHANGED],
                       0);
}

static void
nautilus_disc_burn_bar_title_insert_text (GtkEditable *editable,
                                          const gchar *text,
                                          gint length,
                                          gint *position,
                                          NautilusDiscBurnBar *bar)
{
	const gchar *label;
	gchar *new_text;
	gint new_length;
	gchar *current;
	gint max_len;
	gchar *prev;
	gchar *next;

	/* check if this new text will fit in 32 _bytes_ long buffer */
	label = gtk_entry_get_text (GTK_ENTRY (editable));
	max_len = 32 - strlen (label) - length;
	if (max_len >= 0)
		return;

	gdk_beep ();

	/* get the last character '\0' of the text to be inserted */
	new_length = length;
	new_text = g_strdup (text);
	current = g_utf8_offset_to_pointer (new_text, g_utf8_strlen (new_text, -1));

	/* don't just remove one character in case there was many more
	 * that were inserted at the same time through DND, paste, ... */
	prev = g_utf8_find_prev_char (new_text, current);
	if (!prev) {
		/* no more characters so no insertion */
		g_signal_stop_emission_by_name (editable, "insert_text"); 
		g_free (new_text);
		return;
	}

	do {
		next = current;
		current = prev;

		prev = g_utf8_find_prev_char (new_text, current);
		if (!prev) {
			/* no more characters so no insertion */
			g_signal_stop_emission_by_name (editable, "insert_text"); 
			g_free (new_text);
			return;
		}

		new_length -= next - current;
		max_len += next - current;
	} while (max_len < 0 && new_length > 0);

	*current = '\0';
	g_signal_handlers_block_by_func (editable,
					 (gpointer) nautilus_disc_burn_bar_title_insert_text,
					 bar);
	gtk_editable_insert_text (editable, new_text, new_length, position);
	g_signal_handlers_unblock_by_func (editable,
					   (gpointer) nautilus_disc_burn_bar_title_insert_text,
					   bar);

	g_signal_stop_emission_by_name (editable, "insert_text");
	g_free (new_text);
}

336
static void
337
nautilus_disc_burn_bar_init (NautilusDiscBurnBar *bar)
338
{
339
        GtkWidget   *table;
340 341
        GtkWidget   *label;
        GtkWidget   *hbox;
342
        GtkWidget   *image;
343
        GtkWidget   *entry;
344
        gchar       *string;
345

346
        bar->priv = NAUTILUS_DISC_BURN_BAR_GET_PRIVATE (bar);
347 348

        hbox = GTK_WIDGET (bar);
349
        table = gtk_table_new (3, 2, FALSE);       
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        gtk_table_set_col_spacings (GTK_TABLE (table), 6);
        gtk_table_set_row_spacings (GTK_TABLE (table), 6);
        gtk_widget_show (table);
        gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, TRUE, 0);

        label = gtk_label_new (_("CD/DVD Creator Folder"));
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
        gtk_widget_show (label);
        gtk_table_attach (GTK_TABLE (table),
                          label,
                          0, 2,
                          0, 1,
                          GTK_FILL,
                          GTK_FILL,
                          0,
                          0);

        label = gtk_label_new (_("Disc Name:"));
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
370
        gtk_widget_show (label);
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
        gtk_table_attach (GTK_TABLE (table),
                          label,
                          0, 1,
                          1, 2,
                          GTK_FILL,
                          GTK_FILL,
                          0,
                          0);

        entry = gtk_entry_new ();
        bar->priv->title = entry;
        gtk_widget_show (entry);
        gtk_table_attach (GTK_TABLE (table),
                          entry,
                          1, 2,
                          1, 2,
                          GTK_FILL|GTK_EXPAND,
                          GTK_FILL|GTK_EXPAND,
                          0,
                          0);

        g_signal_connect (entry,
			  "icon-release",
			  G_CALLBACK (nautilus_disc_burn_bar_icon_button_clicked),
			  bar);
	g_signal_connect (entry,
			  "insert_text",
			  G_CALLBACK (nautilus_disc_burn_bar_title_insert_text),
			  bar);
	g_signal_connect (entry,
			  "changed",
			  G_CALLBACK (nautilus_disc_burn_bar_title_changed),
			  bar);
404

405 406
        /* Translators: be careful, anything longer than the English will likely
         * not fit on small Nautilus windows */
407 408 409
        string = g_strdup_printf ("<i>%s</i>", _("Drag or copy files below to write them to disc"));
        label = gtk_label_new (string);
        g_free (string);
410
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
411 412 413
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
        gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
        gtk_widget_show (label);
414 415 416 417 418 419 420 421
        gtk_table_attach (GTK_TABLE (table),
                          label,
                          0, 2,
                          2, 3,
                          GTK_FILL|GTK_EXPAND,
                          GTK_FILL|GTK_EXPAND,
                          0,
                          0);
422

423 424
        bar->priv->button = gtk_button_new_with_label (_("Write to Disc"));
        gtk_widget_show (bar->priv->button);
425 426 427 428 429 430 431 432
        gtk_table_attach (GTK_TABLE (table),
                          bar->priv->button,
                          2, 3,
                          1, 2,
                          GTK_FILL,
                          GTK_FILL,
                          0,
                          0);
433

434 435 436 437
        image = gtk_image_new_from_icon_name ("media-optical-burn", GTK_ICON_SIZE_BUTTON);
        gtk_widget_show (image);
        gtk_button_set_image (GTK_BUTTON (bar->priv->button), image);

438 439 440 441
        g_signal_connect (bar->priv->button, "clicked",
                          G_CALLBACK (button_clicked_cb),
                          bar);

442
        gtk_widget_set_tooltip_text (bar->priv->button, _("Write contents to a CD or DVD"));
443 444 445
}

static void
446
nautilus_disc_burn_bar_finalize (GObject *object)
447
{
448
        NautilusDiscBurnBar *bar;
449 450

        g_return_if_fail (object != NULL);
451
        g_return_if_fail (NAUTILUS_IS_DISC_BURN_BAR (object));
452

453
        bar = NAUTILUS_DISC_BURN_BAR (object);
454 455 456

        g_return_if_fail (bar->priv != NULL);

457 458 459 460 461
        if (bar->priv->icon_path) {
                g_free (bar->priv->icon_path);
                bar->priv->icon_path = NULL;
        }

462
        G_OBJECT_CLASS (nautilus_disc_burn_bar_parent_class)->finalize (object);
463 464 465
}

GtkWidget *
466
nautilus_disc_burn_bar_new (void)
467 468 469
{
        GObject *result;

470
        result = g_object_new (NAUTILUS_TYPE_DISC_BURN_BAR,
471
                               "spacing", 6,
472 473 474 475
                               NULL);

        return GTK_WIDGET (result);
}