/* * vinagre-bookmarks.c * This file is part of vinagre * * Copyright (C) 2007 Jonh Wendell * * 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 . */ #include "vinagre-bookmarks.h" #include "vinagre-utils.h" #include #include #include #include #define VINAGRE_FAVORITES_FILE "vinagre.bookmarks" GKeyFile *bookmarks = NULL; static gchar * filename (void) { return g_build_filename (g_get_home_dir (), ".gnome2", VINAGRE_FAVORITES_FILE, NULL); } static void vinagre_bookmarks_save_file (void) { gchar *file; gchar *data; gsize length; GError *error; error = NULL; data = g_key_file_to_data (bookmarks, &length, &error); if (!data) { if (error) { g_warning (_("Error while saving bookmarks: %s"), error->message); g_error_free (error); } return; } file = filename (); error = NULL; if (!g_file_set_contents (file, data, length, &error)) { if (error) { g_warning (_("Error while saving bookmarks: %s"), error->message); g_error_free (error); } } g_free (file); g_free (data); } void vinagre_bookmarks_init (void) { gchar *file; gboolean loaded; GError *error; g_return_if_fail (bookmarks == NULL); bookmarks = g_key_file_new (); loaded = FALSE; error = NULL; file = filename (); if (g_file_test (file, G_FILE_TEST_EXISTS)) loaded = g_key_file_load_from_file (bookmarks, file, G_KEY_FILE_NONE, &error); g_free (file); if (!loaded) { if (error) { g_warning (_("Error while initializing bookmarks: %s"), error->message); g_error_free (error); } } } void vinagre_bookmarks_finalize (void) { g_return_if_fail (bookmarks != NULL); g_key_file_free (bookmarks); } gboolean vinagre_bookmarks_add (VinagreConnection *conn, VinagreWindow *window) { gint result; GladeXML *xml; const gchar *glade_file; GtkWidget *dialog; const gchar *name; g_return_val_if_fail (conn != NULL, FALSE); glade_file = vinagre_utils_get_glade_filename (); xml = glade_xml_new (glade_file, "add_to_bookmarks_dialog", NULL); dialog = glade_xml_get_widget (xml, "add_to_bookmarks_dialog"); gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(window)); gtk_widget_show_all (dialog); result = gtk_dialog_run (GTK_DIALOG (dialog)); if (result == GTK_RESPONSE_OK) { name = gtk_entry_get_text (GTK_ENTRY (glade_xml_get_widget (xml, "bookmark_name_entry"))); if (strlen(name) < 1) name = conn->host; g_key_file_set_string (bookmarks, name, "host", conn->host); g_key_file_set_integer (bookmarks, name, "port", conn->port); vinagre_connection_set_name (conn, name); vinagre_bookmarks_save_file (); } gtk_widget_destroy (GTK_WIDGET (dialog)); g_object_unref (G_OBJECT (xml)); return (result == GTK_RESPONSE_OK); } gboolean vinagre_bookmarks_edit (VinagreConnection *conn, VinagreWindow *window) { gint result; GladeXML *xml; const gchar *glade_file; GtkWidget *dialog, *host_entry, *name_entry, *port_entry; const gchar *name; g_return_val_if_fail (conn != NULL, FALSE); glade_file = vinagre_utils_get_glade_filename (); xml = glade_xml_new (glade_file, "edit_bookmark_dialog", NULL); dialog = glade_xml_get_widget (xml, "edit_bookmark_dialog"); gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(window)); name_entry = glade_xml_get_widget (xml, "edit_bookmark_name_entry"); host_entry = glade_xml_get_widget (xml, "edit_bookmark_host_entry"); port_entry = glade_xml_get_widget (xml, "edit_bookmark_port_entry"); gtk_entry_set_text (GTK_ENTRY(name_entry), conn->name); gtk_entry_set_text (GTK_ENTRY(host_entry), conn->host); gtk_spin_button_set_value (GTK_SPIN_BUTTON (port_entry), conn->port); gtk_widget_show_all (dialog); result = gtk_dialog_run (GTK_DIALOG (dialog)); if (result == GTK_RESPONSE_OK) { g_key_file_remove_group (bookmarks, conn->name, NULL); name = gtk_entry_get_text (GTK_ENTRY (name_entry)); vinagre_connection_set_host (conn, gtk_entry_get_text (GTK_ENTRY (host_entry))); vinagre_connection_set_port (conn, gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (port_entry))); if (strlen(conn->name) < 1) name = conn->host; g_key_file_set_string (bookmarks, name, "host", conn->host); g_key_file_set_integer (bookmarks, name, "port", conn->port); vinagre_connection_set_name (conn, name); vinagre_bookmarks_save_file (); } gtk_widget_destroy (GTK_WIDGET (dialog)); g_object_unref (G_OBJECT (xml)); return (result == GTK_RESPONSE_OK); } GList * vinagre_bookmarks_get_all (void) { GList *list = NULL; gsize length, i; gchar **conns; VinagreConnection *conn; gchar *s_value; gint i_value; g_return_val_if_fail (bookmarks != NULL, NULL); conns = g_key_file_get_groups (bookmarks, &length); for (i=0; i%s from bookmarks?"), name); result = gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); if (result == GTK_RESPONSE_OK) { g_key_file_remove_group (bookmarks, name, &error); if (error) { g_warning (_("Error while removing %s from bookmarks: %s"), name, error->message); g_error_free (error); g_free (name); return FALSE; } g_free (name); vinagre_bookmarks_save_file (); } return (result == GTK_RESPONSE_OK); } VinagreConnection * vinagre_bookmarks_exists (const char *host, int port) { VinagreConnection *conn = NULL; gsize length, i; gchar **conns = NULL; gchar *s_host = NULL; gint i_port; g_return_val_if_fail (bookmarks != NULL, NULL); conns = g_key_file_get_groups (bookmarks, &length); for (i=0; i