[vinagre] Rewrite remaining utility functions with Vala



commit 1c0c2d0f53bc3a5b7240aa3907d5d554696c7ae6
Author: David King <amigadave amigadave com>
Date:   Sun May 29 06:26:29 2011 +0200

    Rewrite remaining utility functions with Vala
    
    Move some utility functions to the single place where they are used.
    Remove vinagre-util.c and vinagre-util.h. Add VINAGRE_DATADIR to
    config.vapi.

 Makefile.am                                        |    2 -
 plugins/rdp/vinagre-rdp-connection.c               |    1 -
 plugins/rdp/vinagre-rdp-tab.c                      |    1 -
 .../vinagre-reverse-vnc-listener-dialog.c          |   18 +-
 plugins/reverse-vnc/vinagre-reverse-vnc-listener.c |    1 -
 plugins/spice/vinagre-spice-connection.c           |    1 -
 plugins/spice/vinagre-spice-tab.c                  |    3 +-
 plugins/ssh/vinagre-ssh-connection.c               |    1 -
 plugins/ssh/vinagre-ssh-tab.c                      |    1 -
 plugins/vnc/vinagre-vnc-connection.c               |    1 -
 plugins/vnc/vinagre-vnc-tab.c                      |   13 +-
 po/POTFILES.in                                     |    1 -
 vinagre/vapi/config.vapi                           |    1 +
 vinagre/vinagre-bookmarks-migration.c              |    1 -
 vinagre/vinagre-bookmarks-ui.c                     |    5 +-
 vinagre/vinagre-commands.c                         |    1 -
 vinagre/vinagre-connect.c                          |    3 +-
 vinagre/vinagre-connection.c                       |    1 -
 vinagre/vinagre-dnd.h                              |    2 +
 vinagre/vinagre-fav.c                              |    1 -
 vinagre/vinagre-main.c                             |    1 -
 vinagre/vinagre-notebook.c                         |    1 -
 vinagre/vinagre-options.c                          |    1 -
 vinagre/vinagre-prefs.c                            |    3 +-
 vinagre/vinagre-ssh.c                              |   68 ++++-
 vinagre/vinagre-tab.c                              |    1 -
 vinagre/vinagre-tube-handler.c                     |    1 -
 vinagre/vinagre-util.c                             |  334 --------------------
 vinagre/vinagre-util.h                             |   63 ----
 vinagre/vinagre-utils.vala                         |  136 ++++++++-
 vinagre/vinagre-window.c                           |   47 +++-
 31 files changed, 263 insertions(+), 452 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 0e5a7cf..c2936c7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -71,7 +71,6 @@ vinagreh_HEADERS = \
 	vinagre/vinagre-prefs.h \
 	vinagre/vinagre-tab.h \
 	vinagre/vinagre-ui.h \
-	vinagre/vinagre-util.h \
 	vinagre/vinagre-window.h \
 	vinagre/vinagre-dnd.h \
 	vinagre/vinagre-ssh.h \
@@ -108,7 +107,6 @@ handwritten_sources = \
 	vinagre/vinagre-prefs.c \
 	vinagre/vinagre-tab.c \
 	vinagre/vinagre-utils.vala \
-	vinagre/vinagre-util.c \
 	vinagre/vinagre-window.c \
 	vinagre/pty_open.c \
 	vinagre/vinagre-ssh.c \
diff --git a/plugins/rdp/vinagre-rdp-connection.c b/plugins/rdp/vinagre-rdp-connection.c
index 23ed2ca..dfa9399 100644
--- a/plugins/rdp/vinagre-rdp-connection.c
+++ b/plugins/rdp/vinagre-rdp-connection.c
@@ -20,7 +20,6 @@
  */
 
 #include <glib/gi18n.h>
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-cache-prefs.h>
 #include "vinagre-rdp-connection.h"
 
diff --git a/plugins/rdp/vinagre-rdp-tab.c b/plugins/rdp/vinagre-rdp-tab.c
index bd8b84b..d7647dd 100644
--- a/plugins/rdp/vinagre-rdp-tab.c
+++ b/plugins/rdp/vinagre-rdp-tab.c
@@ -26,7 +26,6 @@
 #include <glib/gi18n.h>
 #include <gtk/gtkx.h>
 
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-prefs.h>
 
 #include "vinagre-rdp-tab.h"
diff --git a/plugins/reverse-vnc/vinagre-reverse-vnc-listener-dialog.c b/plugins/reverse-vnc/vinagre-reverse-vnc-listener-dialog.c
index 23676f4..f501b59 100644
--- a/plugins/reverse-vnc/vinagre-reverse-vnc-listener-dialog.c
+++ b/plugins/reverse-vnc/vinagre-reverse-vnc-listener-dialog.c
@@ -39,7 +39,6 @@
 #include <string.h>
 #include <glib/gi18n.h>
 
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-prefs.h>
 #include "vinagre-reverse-vnc-listener-dialog.h"
 #include "vinagre-reverse-vnc-listener.h"
@@ -208,12 +207,27 @@ vinagre_reverse_vnc_listener_dialog_show (GtkWindow *parent)
 {
   VncListenDialog *dialog;
   GtkBuilder *xml;
+  GError *error = NULL;
   gboolean always;
   gchar *filename;
 
   filename = g_build_filename (VINAGRE_REVERSE_VNC_DATADIR, "reverse-vnc.ui", NULL);
 
-  xml = vinagre_utils_get_builder (filename);
+  xml = gtk_builder_new ();
+  if (!gtk_builder_add_from_file (xml, filename, &error))
+  {
+    GString *str = g_string_new (NULL);
+
+    g_string_append (str, _("The reverse VNC plugin failed to open a UI file, with the error message:"));
+    g_string_append_printf (str, "\n\n%s\n\n", error->message);
+    g_string_append (str, _("Please check your installation."));
+    vinagre_utils_show_error_dialog (_("Error loading UI file"), str->str, NULL);
+    g_error_free (error);
+    g_string_free (str, TRUE);
+    g_object_unref (xml);
+    xml = NULL;
+  }
+
   g_free (filename);
   if (!xml)
     return;
diff --git a/plugins/reverse-vnc/vinagre-reverse-vnc-listener.c b/plugins/reverse-vnc/vinagre-reverse-vnc-listener.c
index f7ed9cc..d8decd9 100644
--- a/plugins/reverse-vnc/vinagre-reverse-vnc-listener.c
+++ b/plugins/reverse-vnc/vinagre-reverse-vnc-listener.c
@@ -35,7 +35,6 @@
 #include <glib/gi18n.h>
 
 #include <vinagre/vinagre-commands.h>
-#include "vinagre/vinagre-util.h"
 #include "vinagre-reverse-vnc-listener.h"
 #include "../vnc/vinagre-vnc-connection.h"
 
diff --git a/plugins/spice/vinagre-spice-connection.c b/plugins/spice/vinagre-spice-connection.c
index 81d2581..25a6c0d 100644
--- a/plugins/spice/vinagre-spice-connection.c
+++ b/plugins/spice/vinagre-spice-connection.c
@@ -22,7 +22,6 @@
 
 #include <glib/gi18n.h>
 #include <stdlib.h>
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-cache-prefs.h>
 
 #include "vinagre-spice-connection.h"
diff --git a/plugins/spice/vinagre-spice-tab.c b/plugins/spice/vinagre-spice-tab.c
index bb45f33..90dc87b 100644
--- a/plugins/spice/vinagre-spice-tab.c
+++ b/plugins/spice/vinagre-spice-tab.c
@@ -27,7 +27,6 @@
 #include <spice-client-gtk-3.0/spice-widget.h>
 #include <gdk/gdkkeysyms.h>
 
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-prefs.h>
 
 #include "vinagre-spice-tab.h"
@@ -294,7 +293,7 @@ spice_main_channel_event_cb(SpiceChannel *channel, SpiceChannelEvent event,
     gboolean save_in_keyring = FALSE;
 
     vinagre_tab_remove_credentials_from_keyring (VINAGRE_TAB (tab));
-    if (!vinagre_utils_ask_credential (window,
+    if (!vinagre_utils_request_credential (window,
 				       "SPICE",
 				       name,
 				       FALSE,
diff --git a/plugins/ssh/vinagre-ssh-connection.c b/plugins/ssh/vinagre-ssh-connection.c
index f1214f5..2851db9 100644
--- a/plugins/ssh/vinagre-ssh-connection.c
+++ b/plugins/ssh/vinagre-ssh-connection.c
@@ -20,7 +20,6 @@
  */
 
 #include <glib/gi18n.h>
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-cache-prefs.h>
 #include "vinagre-ssh-connection.h"
 
diff --git a/plugins/ssh/vinagre-ssh-tab.c b/plugins/ssh/vinagre-ssh-tab.c
index 5abfdb2..67f0514 100644
--- a/plugins/ssh/vinagre-ssh-tab.c
+++ b/plugins/ssh/vinagre-ssh-tab.c
@@ -24,7 +24,6 @@
 #include <vte/vte.h>
 #include <gdk/gdkkeysyms.h>
 
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-prefs.h>
 
 #include "vinagre-ssh-tab.h"
diff --git a/plugins/vnc/vinagre-vnc-connection.c b/plugins/vnc/vinagre-vnc-connection.c
index 01f63a8..8ce2fc1 100644
--- a/plugins/vnc/vinagre-vnc-connection.c
+++ b/plugins/vnc/vinagre-vnc-connection.c
@@ -22,7 +22,6 @@
 #include <config.h>
 #include <glib/gi18n.h>
 #include <stdlib.h>
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-cache-prefs.h>
 
 #include "vinagre-vnc-connection.h"
diff --git a/plugins/vnc/vinagre-vnc-tab.c b/plugins/vnc/vinagre-vnc-tab.c
index 62ff9fb..3cb584a 100644
--- a/plugins/vnc/vinagre-vnc-tab.c
+++ b/plugins/vnc/vinagre-vnc-tab.c
@@ -24,7 +24,6 @@
 #include <vncdisplay.h>
 #include <gdk/gdkkeysyms.h>
 
-#include "vinagre/vinagre-util.h"
 #include <vinagre/vinagre-prefs.h>
 
 #include "vinagre-vnc-tab.h"
@@ -496,15 +495,9 @@ vnc_authentication_cb (VncDisplay *vnc, GValueArray *credList, VinagreVncTab *vn
       if ( (need_username && !username) || (need_password && !password) )
 	{
 	  host = vinagre_connection_get_best_name (conn);
-	  if (!vinagre_utils_ask_credential (window,
-					     "VNC",
-					     host,
-					     need_username,
-					     need_password,
-					     8,
-					     &username,
-					     &password,
-					     &save_in_keyring))
+	  if (!vinagre_utils_request_credential (window, "VNC", host,
+	     need_username, need_password, 8, &username, &password,
+	     &save_in_keyring))
 	    {
 	      vinagre_tab_remove_from_notebook (tab);
 	      goto out;
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4bd9c21..c4d3df4 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -48,6 +48,5 @@ vinagre/vinagre-ssh.c
 vinagre/vinagre-tab.c
 vinagre/vinagre-tube-handler.c
 vinagre/vinagre-ui.h
-vinagre/vinagre-util.c
 vinagre/vinagre-utils.vala
 vinagre/vinagre-window.c
diff --git a/vinagre/vapi/config.vapi b/vinagre/vapi/config.vapi
index eea4517..2e461c2 100644
--- a/vinagre/vapi/config.vapi
+++ b/vinagre/vapi/config.vapi
@@ -4,4 +4,5 @@ namespace Vinagre.Config
     public const string PACKAGE_TARNAME;
     public const string PACKAGE_URL;
     public const string PACKAGE_VERSION;
+    public const string VINAGRE_DATADIR;
 } // namespace Vinagre.Config
diff --git a/vinagre/vinagre-bookmarks-migration.c b/vinagre/vinagre-bookmarks-migration.c
index 518454c..e8c1c45 100644
--- a/vinagre/vinagre-bookmarks-migration.c
+++ b/vinagre/vinagre-bookmarks-migration.c
@@ -37,7 +37,6 @@
 #include "vinagre-bookmarks.h"
 #include "vinagre-plugins-engine.h"
 #include "vinagre-dirs.h"
-#include "vinagre-util.h"
 
 static void
 fill_xml (GSList *list, xmlTextWriter *writer)
diff --git a/vinagre/vinagre-bookmarks-ui.c b/vinagre/vinagre-bookmarks-ui.c
index 8e3fa3d..5f37aa2 100644
--- a/vinagre/vinagre-bookmarks-ui.c
+++ b/vinagre/vinagre-bookmarks-ui.c
@@ -23,7 +23,6 @@
 #include <glib/gi18n.h>
 
 #include "vinagre-bookmarks-ui.h"
-#include "vinagre-util.h"
 #include "vinagre-bookmarks-tree.h"
 #include "vinagre-plugins-engine.h"
 
@@ -44,7 +43,7 @@ show_dialog_folder (VinagreBookmarks      *book,
   GtkWidget   *dialog, *box, *tree, *name_entry, *save_button;
   const gchar *name;
 
-  xml = vinagre_utils_get_builder (NULL);
+  xml = vinagre_utils_get_builder ();
   dialog     = GTK_WIDGET (gtk_builder_get_object (xml, "bookmarks_add_edit_folder_dialog"));
   name_entry = GTK_WIDGET (gtk_builder_get_object (xml, "edit_bookmark_folder_name_entry"));
   box        = GTK_WIDGET (gtk_builder_get_object (xml, "folder_box1"));
@@ -129,7 +128,7 @@ show_dialog_conn (VinagreBookmarks      *book,
   VinagreProtocol    *ext;
   gchar              **props;
 
-  xml = vinagre_utils_get_builder (NULL);
+  xml = vinagre_utils_get_builder ();
   dialog         = GTK_WIDGET (gtk_builder_get_object (xml, "bookmarks_add_edit_conn_dialog"));
   name_entry     = GTK_WIDGET (gtk_builder_get_object (xml, "edit_bookmark_name_entry"));
   host_entry     = GTK_WIDGET (gtk_builder_get_object (xml, "edit_bookmark_host_entry"));
diff --git a/vinagre/vinagre-commands.c b/vinagre/vinagre-commands.c
index 8c4cd82..f89e90d 100644
--- a/vinagre/vinagre-commands.c
+++ b/vinagre/vinagre-commands.c
@@ -28,7 +28,6 @@
 #include <string.h>
 
 #include "vinagre-commands.h"
-#include "vinagre-util.h"
 #include "vinagre-connection.h"
 #include "vinagre-notebook.h"
 #include "vinagre-tab.h"
diff --git a/vinagre/vinagre-connect.c b/vinagre/vinagre-connect.c
index 5b1f413..a2b05ff 100644
--- a/vinagre/vinagre-connect.c
+++ b/vinagre/vinagre-connect.c
@@ -32,7 +32,6 @@
 #endif
 
 #include "vinagre-connect.h"
-#include "vinagre-util.h"
 #include "vinagre-bookmarks.h"
 #include "vinagre-prefs.h"
 #include "vinagre-cache-prefs.h"
@@ -402,7 +401,7 @@ vinagre_connect (VinagreWindow *window)
   gint                  result;
   VinagreConnectDialog  dialog;
 
-  dialog.xml = vinagre_utils_get_builder (NULL);
+  dialog.xml = vinagre_utils_get_builder ();
   if (!dialog.xml)
     return NULL;
 
diff --git a/vinagre/vinagre-connection.c b/vinagre/vinagre-connection.c
index d4b8156..ba51a83 100644
--- a/vinagre/vinagre-connection.c
+++ b/vinagre/vinagre-connection.c
@@ -27,7 +27,6 @@
 #include "vinagre-connection.h"
 #include "vinagre-bookmarks.h"
 #include "vinagre-plugins-engine.h"
-#include "vinagre-util.h"
 
 struct _VinagreConnectionPrivate
 {
diff --git a/vinagre/vinagre-dnd.h b/vinagre/vinagre-dnd.h
index 1224204..55eb57b 100644
--- a/vinagre/vinagre-dnd.h
+++ b/vinagre/vinagre-dnd.h
@@ -18,6 +18,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <gtk/gtk.h>
+
 typedef enum {
   TARGET_VINAGRE,
   TARGET_STRING
diff --git a/vinagre/vinagre-fav.c b/vinagre/vinagre-fav.c
index 06b7664..293f04c 100644
--- a/vinagre/vinagre-fav.c
+++ b/vinagre/vinagre-fav.c
@@ -24,7 +24,6 @@
 
 #include <glib/gi18n.h>
 #include "vinagre-fav.h"
-#include "vinagre-util.h"
 #include "vinagre-dnd.h"
 #include "vinagre-bookmarks.h"
 #include "vinagre-bookmarks-ui.h"
diff --git a/vinagre/vinagre-main.c b/vinagre/vinagre-main.c
index f3cdfa8..742df27 100644
--- a/vinagre/vinagre-main.c
+++ b/vinagre/vinagre-main.c
@@ -31,7 +31,6 @@
 #include <locale.h>
 
 #include "vinagre-window.h"
-#include "vinagre-util.h"
 #include "vinagre-prefs.h"
 #include "vinagre-cache-prefs.h"
 #include "vinagre-debug.h"
diff --git a/vinagre/vinagre-notebook.c b/vinagre/vinagre-notebook.c
index 00026f8..fce9fd7 100644
--- a/vinagre/vinagre-notebook.c
+++ b/vinagre/vinagre-notebook.c
@@ -24,7 +24,6 @@
 
 #include <glib/gi18n.h>
 
-#include "vinagre-util.h"
 #include "vinagre-dnd.h"
 #include "vinagre-prefs.h"
 
diff --git a/vinagre/vinagre-options.c b/vinagre/vinagre-options.c
index 165010f..25a68d5 100644
--- a/vinagre/vinagre-options.c
+++ b/vinagre/vinagre-options.c
@@ -23,7 +23,6 @@
 #include "vinagre-connection.h"
 #include "vinagre-window.h"
 #include "vinagre-commands.h"
-#include "vinagre-util.h"
 
 const GOptionEntry all_options [] =
 {
diff --git a/vinagre/vinagre-prefs.c b/vinagre/vinagre-prefs.c
index 45b8d01..f52ccce 100644
--- a/vinagre/vinagre-prefs.c
+++ b/vinagre/vinagre-prefs.c
@@ -21,7 +21,6 @@
 #include <config.h>
 #include <glib/gi18n.h>
 #include "vinagre-prefs.h"
-#include "vinagre-util.h"
 
 #define VINAGRE_SCHEMA_NAME		"org.gnome.Vinagre"
 #define VM_ALWAYS_SHOW_TABS		"always-show-tabs"
@@ -230,7 +229,7 @@ vinagre_prefs_dialog_show (VinagreWindow *window)
 
   dialog = g_new (VinagrePrefsDialog, 1);
 
-  dialog->xml = vinagre_utils_get_builder (NULL);
+  dialog->xml = vinagre_utils_get_builder ();
   dialog->dialog = GTK_WIDGET (gtk_builder_get_object (dialog->xml, "preferences_dialog"));
   dialog->parent = GTK_WINDOW (window);
   gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog), dialog->parent);
diff --git a/vinagre/vinagre-ssh.c b/vinagre/vinagre-ssh.c
index ce6d2f7..ef33526 100644
--- a/vinagre/vinagre-ssh.c
+++ b/vinagre/vinagre-ssh.c
@@ -30,7 +30,6 @@
 #include <gnome-keyring.h>
 
 #include "vinagre-ssh.h"
-#include "vinagre-util.h"
 #include "pty_open.h"
 
 #define SSH_READ_TIMEOUT 40   /* seconds */
@@ -294,6 +293,60 @@ get_hostname_and_fingerprint_from_line (const gchar *buffer,
   return TRUE;
 }
 
+/**
+ * _ask_question:
+ * @parent: transient parent, or NULL for none
+ * @message: The message to be displayed, if it contains multiple lines,
+ *  the first one is considered as the title.
+ * @choices: NULL-terminated array of button's labels of the dialog
+ * @choice: Place to store the selected button. Zero is the first.
+ *
+ * Displays a dialog with a message and some options to the user.
+ *
+ * Returns TRUE if the user has selected any option, FALSE if the dialog
+ *  was canceled.
+ */
+static gboolean
+_ask_question (GtkWindow  *parent,
+			    const char *message,
+			    char       **choices,
+			    int        *choice)
+{
+  gchar **messages;
+  GtkWidget *d;
+  int i, n_choices, result;
+
+  g_return_val_if_fail (message && choices && choice, FALSE);
+
+  messages = g_strsplit (message, "\n", 2);
+
+  d = gtk_message_dialog_new (parent,
+			      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+			      GTK_MESSAGE_QUESTION,
+			      GTK_BUTTONS_NONE,
+			      "%s",
+			      messages[0]);
+
+  if (g_strv_length (messages) > 1)
+    gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (d),
+						"%s",
+						messages[1]);
+  g_strfreev (messages);
+
+  n_choices = g_strv_length (choices);
+  for (i = 0; i < n_choices; i++)
+    gtk_dialog_add_button (GTK_DIALOG (d), choices[i], i);
+
+  result = gtk_dialog_run (GTK_DIALOG (d));
+  gtk_widget_destroy (d);
+
+  if (result == GTK_RESPONSE_NONE || result == GTK_RESPONSE_DELETE_EVENT)
+    return FALSE;
+
+  *choice = result;
+  return TRUE;
+}
+
 static gboolean
 handle_login (GtkWindow *parent,
 	      const   gchar *host,
@@ -430,15 +483,8 @@ handle_login (GtkWindow *parent,
 	      gboolean res;
 
 	      full_host = g_strjoin ("@", user, host, NULL);
-	      res = vinagre_utils_ask_credential (parent,
-						  "SSH",
-						  full_host,
-						  FALSE,
-						  TRUE,
-						  0,
-						  NULL,
-						  &password,
-						  &save_in_keyring);
+	      res = vinagre_utils_request_credential (parent, "SSH", full_host,
+		  FALSE, TRUE, 0, NULL, &password, &save_in_keyring);
 	      g_free (full_host);
               if (!res)
                 {
@@ -495,7 +541,7 @@ handle_login (GtkWindow *parent,
 	  g_free (hostname);
 	  g_free (fingerprint);
 
-	  if (!vinagre_utils_ask_question (NULL,
+	  if (!_ask_question (NULL,
 					   message,
 					   (char **)choices,
 					   &choice))
diff --git a/vinagre/vinagre-tab.c b/vinagre/vinagre-tab.c
index 3f0feb4..649aab2 100644
--- a/vinagre/vinagre-tab.c
+++ b/vinagre/vinagre-tab.c
@@ -28,7 +28,6 @@
 
 #include "vinagre-tab.h"
 #include "vinagre-notebook.h"
-#include "vinagre-util.h"
 #include "vinagre-prefs.h"
 #include "view/autoDrawer.h"
 #include "vinagre-plugins-engine.h"
diff --git a/vinagre/vinagre-tube-handler.c b/vinagre/vinagre-tube-handler.c
index ec32c93..d478113 100644
--- a/vinagre/vinagre-tube-handler.c
+++ b/vinagre/vinagre-tube-handler.c
@@ -39,7 +39,6 @@
 #include "vinagre-debug.h"
 #include "vinagre-protocol.h"
 #include "vinagre-plugins-engine.h"
-#include "vinagre-util.h"
 
 G_DEFINE_TYPE (VinagreTubeHandler, vinagre_tube_handler, G_TYPE_OBJECT);
 
diff --git a/vinagre/vinagre-utils.vala b/vinagre/vinagre-utils.vala
index 852a0f8..d601d77 100644
--- a/vinagre/vinagre-utils.vala
+++ b/vinagre/vinagre-utils.vala
@@ -18,7 +18,23 @@
 
 namespace Vinagre.Utils {
 
-static void show_error_dialog(string? title, string? message, Gtk.Window parent)
+static bool parse_boolean(string str)
+{
+    if(str == "true" || str == "1")
+        return true;
+    else
+        return false;
+}
+
+static void toggle_widget_visible(Gtk.Widget widget)
+{
+    if(widget.visible)
+        widget.hide();
+    else
+        widget.show_all();
+}
+
+static void show_error_dialog(string? title, string? message, Gtk.Window? parent)
 {
     if(title != null)
         title = _("An error occurred");
@@ -34,6 +50,13 @@ static void show_error_dialog(string? title, string? message, Gtk.Window parent)
     dialog.show_all();
 }
 
+static void show_many_errors(string? title, GLib.SList<string> items, Gtk.Window parent)
+{
+    string messages = "";
+    items.foreach((message) => messages.printf("%s\n", message));
+    show_error_dialog(title, messages, parent);
+}
+
 static bool create_dir_for_file(string filename) throws GLib.Error
 {
     var file = GLib.File.new_for_path(filename);
@@ -46,6 +69,117 @@ static bool create_dir_for_file(string filename) throws GLib.Error
         return true;
 }
 
+static Gtk.Builder get_builder()
+{
+    string filename = GLib.Path.build_filename(Vinagre.Config.VINAGRE_DATADIR,
+        "vinagre.ui");
+
+    var builder = new Gtk.Builder();
+    try
+    {
+        builder.add_from_file(filename);
+    }
+    catch(GLib.Error err)
+    {
+        string subtitle = _("Vinagre failed to open a UI file, with the error message:");
+	string closing = _("Please check your installation.");
+	string message = "%s\n\n%s\n\n%s".printf(subtitle, err.message,
+            closing);
+        show_error_dialog(_("Error loading UI file"), message, null);
+    }
+
+    return builder;
+}
+
+static bool request_credential(Gtk.Window parent, string protocol, string host,
+    bool need_username, bool need_password, int password_limit,
+    out string username, out string password, out bool save_in_keyring)
+{
+    var xml = get_builder();
+
+    var password_dialog = xml.get_object("auth_required_dialog") as Gtk.Dialog;
+    password_dialog.set_transient_for(parent);
+    string auth_label_message =
+        // Translators: %s is a protocol, like VNC or SSH.
+        _("%s authentication is required").printf(protocol);
+
+    var auth_label = xml.get_object("auth_required_label") as Gtk.Label;
+    auth_label.label = auth_label_message;
+
+    var host_label = xml.get_object("host_label") as Gtk.Label;
+    host_label.label = host;
+
+    var password_label = xml.get_object("password_label") as Gtk.Label;
+    var username_label = xml.get_object("username_label") as Gtk.Label;
+    var save_credential_check = xml.get_object("save_credential_check")
+        as Gtk.CheckButton;
+
+    var ok_button = xml.get_object("ok_button") as Gtk.Button;
+    var image = new Gtk.Image.from_stock(Gtk.Stock.DIALOG_AUTHENTICATION,
+        Gtk.IconSize.DIALOG);
+    ok_button.image = image;
+
+    var username_entry = xml.get_object("username_entry") as Gtk.Entry;
+    var password_entry = xml.get_object("password_entry") as Gtk.Entry;
+    username_entry.changed.connect(() => {
+        var enabled = true;
+
+        if(username_entry.visible)
+            enabled = enabled && username_entry.text_length > 0;
+
+        if(password_entry.visible)
+            enabled = enabled && password_entry.text_length > 0;
+
+        ok_button.sensitive = enabled;
+    });
+
+    if(need_username)
+        username_entry.text = username;
+    else
+    {
+        username_label.hide();
+	username_entry.hide();
+    }
+
+    password_entry.changed.connect(() => {
+        var enabled = true;
+
+        if(username_entry.visible)
+            enabled = enabled && username_entry.text_length > 0;
+
+        if(password_entry.visible)
+            enabled = enabled && password_entry.text_length > 0;
+
+        ok_button.sensitive = enabled;
+    });
+    if(need_password)
+    {
+        password_entry.max_length = password_limit;
+        password_entry.text = password;
+    }
+    else
+    {
+        password_label.hide();
+        password_entry.hide();
+    }
+
+    var result = password_dialog.run();
+    if(result == Gtk.ResponseType.OK)
+    {
+        if(username_entry.text_length > 0)
+            username = username_entry.text;
+
+        if(password_entry.text_length > 0)
+            password = password_entry.text;
+
+        if(save_in_keyring)
+            save_in_keyring = save_credential_check.active;
+    }
+
+    password_dialog.destroy();
+    return result == Gtk.ResponseType.OK;
+}
+
 static void show_help(Gtk.Window window, string? page)
 {
     string uri;
diff --git a/vinagre/vinagre-window.c b/vinagre/vinagre-window.c
index 0264a7a..0e9ba89 100644
--- a/vinagre/vinagre-window.c
+++ b/vinagre/vinagre-window.c
@@ -37,7 +37,6 @@
 #include "vinagre-fav.h"
 #include "vinagre-prefs.h"
 #include "vinagre-cache-prefs.h"
-#include "vinagre-util.h"
 #include "vinagre-bookmarks.h"
 #include "vinagre-ui.h"
 #include "vinagre-window-private.h"
@@ -492,6 +491,48 @@ fav_panel_size_allocate (GtkWidget     *widget,
     vinagre_cache_prefs_set_integer ("window", "side-panel-size", window->priv->side_panel_size);
 }
 
+/*
+ * Doubles underscore to avoid spurious menu accels.
+ */
+static gchar *
+_escape_underscores (const gchar* text,
+	gssize       length)
+{
+	GString *str;
+	const gchar *p;
+	const gchar *end;
+
+	g_return_val_if_fail (text != NULL, NULL);
+
+	if (length < 0)
+		length = strlen (text);
+
+	str = g_string_sized_new (length);
+
+	p = text;
+	end = text + length;
+
+	while (p != end)
+	{
+		const gchar *next;
+		next = g_utf8_next_char (p);
+
+		switch (*p)
+		{
+			case '_':
+				g_string_append (str, "__");
+				break;
+			default:
+				g_string_append_len (str, p, next - p);
+				break;
+		}
+
+		p = next;
+	}
+
+	return g_string_free (str, FALSE);
+}
+
 static void
 vinagre_window_populate_bookmarks (VinagreWindow *window,
 				   const gchar   *group,
@@ -513,7 +554,7 @@ vinagre_window_populate_bookmarks (VinagreWindow *window,
       switch (vinagre_bookmarks_entry_get_node (entry))
 	{
 	  case VINAGRE_BOOKMARKS_ENTRY_NODE_FOLDER:
-	    action_label = vinagre_utils_escape_underscores (vinagre_bookmarks_entry_get_name (entry), -1);
+	    action_label = _escape_underscores (vinagre_bookmarks_entry_get_name (entry), -1);
 	    action_name = g_strdup_printf ("BOOKMARK_FOLDER_ACTION_%d", ++i);
 	    action = gtk_action_new (action_name,
 				     action_label,
@@ -549,7 +590,7 @@ vinagre_window_populate_bookmarks (VinagreWindow *window,
 	     continue;
 
 	    action_name = vinagre_connection_get_best_name (conn);
-	    action_label = vinagre_utils_escape_underscores (action_name, -1);
+	    action_label = _escape_underscores (action_name, -1);
 	    g_free (action_name);
 
 	    action_name = g_strdup_printf ("BOOKMARK_ITEM_ACTION_%d", ++i);



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