[PATCH 12/15] gnome-control-center: Revert Dialog



gnome-control-center:  110_cc-randr12-revert-dialog.patch (LP: #197673)

Adds a revert dialog, shamelessly stolen and adapted from the current
Screen Resolution code.  Also adds some i18n support.

Pops up a dialog asking for confirmation of the settings, which if not
answered, prevents the settings from being written permanently.

This could probably be improved by using glade instead of building the
dialog in gtk directly, but otherwise seems to work ok.


diff -Nur -x '*.orig' -x '*~' gnome-control-center-2.22.1/capplets/display/xrandr-capplet.c gnome-control-center-2.22.1.new/capplets/display/xrandr-capplet.c
--- gnome-control-center-2.22.1/capplets/display/xrandr-capplet.c	2008-04-09 11:11:09.000000000 +0100
+++ gnome-control-center-2.22.1.new/capplets/display/xrandr-capplet.c	2008-04-09 11:12:24.000000000 +0100
@@ -19,6 +19,8 @@
  * Author: Soren Sandmann <sandmann redhat com>
  */
 
+#include <config.h>
+
 #include <gtk/gtk.h>
 #include <glade/glade.h>
 #include <string.h>
@@ -29,6 +31,10 @@
 #include <libgnomeui/monitor-db.h>
 #include <gdk/gdkx.h>
 #include <X11/Xlib.h>
+#include <X11/extensions/Xrandr.h>
+#include <libintl.h>
+
+#define REVERT_COUNT 20
 
 #include "capplet-util.h"
 #include <config.h>
@@ -1475,6 +1481,127 @@
     return atom;
 }
 
+struct TimeoutData {
+    int time;
+    GtkLabel *label;
+    GtkDialog *dialog;
+    gboolean timed_out;
+};
+
+static char *
+timeout_string (int time)
+{
+    return g_strdup_printf (ngettext ("Testing the new settings. If you don't respond in %d second the previous settings will be restored.", "Testing the new settings. If you don't respond in %d seconds the previous settings will be restored.", time), time);
+}
+
+static gboolean
+save_timeout_callback (gpointer _data)
+{
+    struct TimeoutData *data = _data;
+    char *str;
+
+    data->time--;
+
+    if (data->time == 0)
+    {
+        gtk_dialog_response (data->dialog, GTK_RESPONSE_NO);
+        data->timed_out = TRUE;
+        return FALSE;
+    }
+
+    str = timeout_string (data->time);
+    gtk_label_set_text (data->label, str);
+    g_free (str);
+
+    return TRUE;
+}
+
+static int
+run_revert_dialog ()
+{
+    GtkWidget *dialog;
+    GtkWidget *hbox;
+    GtkWidget *vbox;
+    GtkWidget *label;
+    GtkWidget *label_sec;
+    GtkWidget *image;
+    int res;
+    struct TimeoutData timeout_data;
+    guint timeout;
+    char *str;
+
+    g_warning("Creating revert dialog");
+
+    dialog = gtk_dialog_new ();
+    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
+    gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
+    gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
+    gtk_window_set_title (GTK_WINDOW (dialog), gettext("Keep Settings"));
+    gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER_ALWAYS);
+
+    label = gtk_label_new (NULL);
+    str = g_strdup_printf ("<b>%s</b>", gettext("Do you want to keep these screen settings?"));
+    gtk_label_set_markup (GTK_LABEL (label), str);
+    g_free (str);
+    image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
+    gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
+
+    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+    gtk_label_set_selectable (GTK_LABEL (label), TRUE);
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+
+    str = timeout_string (REVERT_COUNT);
+    label_sec = gtk_label_new (str);
+    g_free (str);
+    gtk_label_set_line_wrap (GTK_LABEL (label_sec), TRUE);
+    gtk_label_set_selectable (GTK_LABEL (label_sec), TRUE);
+    gtk_misc_set_alignment (GTK_MISC (label_sec), 0.0, 0.5);
+
+    hbox = gtk_hbox_new (FALSE, 6);
+    vbox = gtk_vbox_new (FALSE, 6);
+
+    gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX (vbox), label_sec, TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
+    gtk_dialog_add_buttons (GTK_DIALOG (dialog),gettext("Use _previous settings"), GTK_RESPONSE_NO, gettext("_Keep settings"), GTK_RESPONSE_YES, NULL);
+
+    gtk_widget_show_all (hbox);
+
+    timeout_data.time = REVERT_COUNT;
+    timeout_data.label = GTK_LABEL (label_sec);
+    timeout_data.dialog = GTK_DIALOG (dialog);
+    timeout_data.timed_out = FALSE;
+
+    timeout = g_timeout_add (1000, save_timeout_callback, &timeout_data);
+    res = gtk_dialog_run (GTK_DIALOG (dialog));
+
+    if (!timeout_data.timed_out)
+        g_source_remove (timeout);
+
+    gtk_widget_destroy (dialog);
+
+    return res == GTK_RESPONSE_YES;
+}
+
+static void
+send_xrandr_update_message ()
+{
+    XEvent message;
+
+    message.xclient.type = ClientMessage;
+    message.xclient.message_type = gnome_randr_atom();
+    message.xclient.format = 8;
+
+    g_print ("Sending client message\n");
+
+    XSendEvent (gdk_x11_get_default_xdisplay(),
+                gdk_x11_get_default_root_xwindow(),
+                FALSE,
+                StructureNotifyMask, &message);
+}
+
 static void
 apply (App *app)
 {
@@ -1484,20 +1611,26 @@
 
     foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
     
-    if (configuration_save (app->current_configuration, &err))
-    {
-	XEvent message;
+    g_print("Applying changes for onetime test\n");
 
-	message.xclient.type = ClientMessage;
-	message.xclient.message_type = gnome_randr_atom();
-	message.xclient.format = 8;
-
-	g_print ("Sending client message\n");
+    /* Only allow the configuration to be used once */
+    if (configuration_save_onetime (app->current_configuration, &err))
+    {
+        g_print("Sending xrandr update message\n");
+        send_xrandr_update_message ();
 
-	XSendEvent (gdk_x11_get_default_xdisplay(),
-		    gdk_x11_get_default_root_xwindow(),
-		    FALSE,
-		    StructureNotifyMask, &message);
+        if ( ! run_revert_dialog () ) {
+            g_print("Reverting to original configuration\n");
+            send_xrandr_update_message ();
+            return;
+        } else {
+            g_print("Keeping configuration permanently\n");
+        }
+
+        /* Save the configuration permanently */
+        configuration_save (app->current_configuration, &err);
+    } else {
+        g_print ("Could not apply temporary configuration\n");
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]