static void save_source (GdictSourceDialog *dialog) { GdictSource *source; GdictDatabaseChooser *db_chooser; GdictStrategyChooser *strat_chooser; gchar *name, *text; GdictSourceTransport transport; gchar *host, *port; gchar *data; gsize length; GError *error; gchar *filename; source = gdict_source_loader_get_source (dialog->loader, dialog->source_name); if (!source) { g_warning ("Attempting to save source `%s', but no " "source for that name was found.", dialog->source_name); return; } text = get_text_from_entry (dialog, "description_entry"); gdict_source_set_description (source, text); g_free (text); db_chooser = GDICT_DATABASE_CHOOSER (dialog->db_chooser); text = gdict_database_chooser_get_current_database (db_chooser); gdict_source_set_database (source, text); g_free (text); strat_chooser = GDICT_STRATEGY_CHOOSER (dialog->strat_chooser); text = gdict_strategy_chooser_get_current_strategy (strat_chooser); gdict_source_set_strategy (source, text); g_free (text); /* get the selected transport id */ transport = dialog->transport; switch (transport) { case GDICT_SOURCE_TRANSPORT_DICTD: host = get_text_from_entry (dialog, "hostname_entry"); port = get_text_from_entry (dialog, "port_entry"); gdict_source_set_transport (source, GDICT_SOURCE_TRANSPORT_DICTD, "hostname", host, "port", atoi (port), NULL); g_free (host); g_free (port); break; case GDICT_SOURCE_TRANSPORT_INVALID: default: g_warning ("Invalid transport"); return; } error = NULL; data = gdict_source_to_data (source, &length, &error); if (error) { gdict_show_gerror_dialog (GTK_WINDOW (dialog), _("Unable to create a source file"), error); g_object_unref (source); return; } name = g_strdup_printf ("%s.desktop", gdict_source_get_name (source)); filename = g_build_filename (g_get_home_dir (), ".gnome2", "gnome-dictionary", name, NULL); g_free (name); g_file_set_contents (filename, data, length, &error); if (error) gdict_show_gerror_dialog (GTK_WINDOW (dialog), _("Unable to save source file"), error); g_free (filename); g_free (data); g_object_unref (source); }
static void build_new_source (GdictSourceDialog *dialog) { GdictSource *source; gchar *name, *text; GdictSourceTransport transport; gchar *host, *port; gchar *data; gsize length; GError *error; gchar *filename; GdictDatabaseChooser *db_chooser; GdictStrategyChooser *strat_chooser; source = gdict_source_new (); /* use the timestamp and the pid to get a unique name */ name = g_strdup_printf ("source-%lu-%u", (gulong) time (NULL), (guint) getpid ()); gdict_source_set_name (source, name); g_free (name); text = get_text_from_entry (dialog, "description_entry"); gdict_source_set_description (source, text); g_free (text); db_chooser = GDICT_DATABASE_CHOOSER (dialog->db_chooser); text = gdict_database_chooser_get_current_database (db_chooser); gdict_source_set_database (source, text); g_free (text); strat_chooser = GDICT_STRATEGY_CHOOSER (dialog->strat_chooser); text = gdict_strategy_chooser_get_current_strategy (strat_chooser); gdict_source_set_strategy (source, text); g_free (text); /* get the selected transport id */ transport = dialog->transport; switch (transport) { case GDICT_SOURCE_TRANSPORT_DICTD: host = get_text_from_entry (dialog, "hostname_entry"); port = get_text_from_entry (dialog, "port_entry"); gdict_source_set_transport (source, GDICT_SOURCE_TRANSPORT_DICTD, "hostname", host, "port", atoi (port), NULL); g_free (host); g_free (port); break; case GDICT_SOURCE_TRANSPORT_INVALID: default: g_warning ("Invalid transport"); return; } error = NULL; data = gdict_source_to_data (source, &length, &error); if (error) { gdict_show_gerror_dialog (GTK_WINDOW (dialog), _("Unable to create a source file"), error); g_object_unref (source); return; } name = g_strdup_printf ("%s.desktop", gdict_source_get_name (source)); filename = g_build_filename (g_get_user_config_dir (), "mate", "mate-dictionary", name, NULL); g_free (name); g_file_set_contents (filename, data, length, &error); if (error) gdict_show_gerror_dialog (GTK_WINDOW (dialog), _("Unable to save source file"), error); g_free (filename); g_free (data); g_object_unref (source); }