/** * gdict_source_loader_update: * @loader: a #GdictSourceLoader * * Queue an update of the sources inside @loader. * * Since: 1.0 */ void gdict_source_loader_update (GdictSourceLoader *loader) { g_return_if_fail (GDICT_IS_SOURCE_LOADER (loader)); loader->priv->paths_dirty = TRUE; }
/** * gdict_source_loader_get_paths: * @loader: a #GdictSourceLoader * * Gets the list of paths used by @loader to search for dictionary source * files. * * Return value: a list containing the paths. The returned list is owned * by the #GdictSourceLoader object and should never be free or modified. * * Since: 1.0 */ const GSList * gdict_source_loader_get_paths (GdictSourceLoader *loader) { g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL); return loader->priv->paths; }
/** * gdict_source_chooser_set_loader: * @chooser: a #GdictSourceChooser * @loader: a #GdictSourceLoader or %NULL to unset it * * Sets the #GdictSourceLoader to be used by the source chooser * widget. * * Since: 0.12 */ void gdict_source_chooser_set_loader (GdictSourceChooser *chooser, GdictSourceLoader *loader) { GdictSourceChooserPrivate *priv; g_return_if_fail (GDICT_IS_SOURCE_CHOOSER (chooser)); g_return_if_fail (loader == NULL || GDICT_IS_SOURCE_LOADER (loader)); priv = chooser->priv; if (priv->loader != loader) { if (priv->loader) g_object_unref (priv->loader); if (loader) { priv->loader = g_object_ref (loader); gdict_source_chooser_refresh (chooser); } g_object_notify (G_OBJECT (chooser), "loader"); } }
/** * gdict_source_chooser_new_with_loader: * @loader: a #GdictSourceLoader * * Creates a new #GdictSourceChooser widget and sets @loader as the * #GdictSourceLoader object to be used to retrieve the list of * available dictionary sources. * * Return value: the newly created #GdictSourceChooser widget. * * Since: 0.12 */ GtkWidget * gdict_source_chooser_new_with_loader (GdictSourceLoader *loader) { g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL); return g_object_new (GDICT_TYPE_SOURCE_CHOOSER, "loader", loader, NULL); }
GtkWidget * gdict_source_dialog_new (GtkWindow *parent, const gchar *title, GdictSourceDialogAction action, GdictSourceLoader *loader, const gchar *source_name) { GtkWidget *retval; g_return_val_if_fail ((parent == NULL || GTK_IS_WINDOW (parent)), NULL); g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL); retval = g_object_new (GDICT_TYPE_SOURCE_DIALOG, "source-loader", loader, "source-name", source_name, "action", action, "title", title, NULL); if (parent) { gtk_window_set_transient_for (GTK_WINDOW (retval), parent); gtk_window_set_destroy_with_parent (GTK_WINDOW (retval), TRUE); gtk_window_set_screen (GTK_WINDOW (retval), gtk_widget_get_screen (GTK_WIDGET (parent))); } return retval; }
/** * gdict_source_loader_get_sources: * @loader: a #GdictSourceLoader * * Retrieves the list of dictionary sources available into the search * paths of @loader, in form of #GdictSource objects. * * Return value: a list of #GdictSource objects. The returned list * is owned by the #GdictSourceLoader object, and should never be * freed or modified. * * Since: 1.0 */ const GSList * gdict_source_loader_get_sources (GdictSourceLoader *loader) { g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL); if (loader->priv->paths_dirty) gdict_source_loader_update_sources (loader); return loader->priv->sources; }
/** * gdict_source_loader_has_source: * @loader: a #GdictSourceLoader * @source_name: the name of a dictionary source * * Checks whether @loader has a dictionary source with name @source_name. * * Return value: %TRUE if the dictionary source is known * * Since: 0.12 */ gboolean gdict_source_loader_has_source (GdictSourceLoader *loader, const gchar *source_name) { g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), FALSE); g_return_val_if_fail (source_name != NULL, FALSE); if (loader->priv->paths_dirty) gdict_source_loader_update_sources (loader); return (g_hash_table_lookup (loader->priv->sources_by_name, source_name) != NULL); }
static void gdict_source_loader_update_sources (GdictSourceLoader *loader) { GSList *filenames, *f; g_assert (GDICT_IS_SOURCE_LOADER (loader)); g_slist_foreach (loader->priv->sources, (GFunc) g_object_unref, NULL); g_slist_free (loader->priv->sources); loader->priv->sources = NULL; filenames = build_source_filenames (loader); for (f = filenames; f != NULL; f = f->next) { GdictSource *source; GError *load_err; gchar *path = (gchar *) f->data; g_assert (path != NULL); source = gdict_source_new (); load_err = NULL; gdict_source_load_from_file (source, path, &load_err); if (load_err) { g_warning ("Unable to load dictionary source at '%s': %s\n", path, load_err->message); g_error_free (load_err); continue; } loader->priv->sources = g_slist_append (loader->priv->sources, source); g_hash_table_replace (loader->priv->sources_by_name, g_strdup (gdict_source_get_name (source)), source); g_signal_emit (loader, loader_signals[SOURCE_LOADED], 0, source); } g_slist_foreach (filenames, (GFunc) g_free, NULL); g_slist_free (filenames); loader->priv->paths_dirty = FALSE; }
/** * gdict_source_loader_remove_source: * @loader: a #GdictSourceLoader * @name: name of a dictionary source * * Removes the dictionary source @name from @loader. This function will * also remove the dictionary source definition file bound to it. * * Return value: %TRUE if the dictionary source was successfully removed * * Since: 1.0 */ gboolean gdict_source_loader_remove_source (GdictSourceLoader *loader, const gchar *name) { GdictSourceLoaderPrivate *priv; GSList *l; g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), FALSE); g_return_val_if_fail (name != NULL, FALSE); priv = loader->priv; if (priv->paths_dirty) gdict_source_loader_update_sources (loader); for (l = priv->sources; l != NULL; l = l->next) { GdictSource *s = GDICT_SOURCE (l->data); if (strcmp (gdict_source_get_name (s), name) == 0) { gchar *filename; g_object_get (G_OBJECT (s), "filename", &filename, NULL); if (g_unlink (filename) == -1) { g_warning ("Unable to remove filename '%s' for the " "dictionary source '%s'\n", filename, name); return FALSE; } g_hash_table_remove (priv->sources_by_name, name); priv->sources = g_slist_remove_link (priv->sources, l); g_object_unref (s); g_slist_free (l); return TRUE; } } return FALSE; }
/** * gdict_source_loader_add_search_path: * @loader: a #GdictSourceLoader * @path: a path to be added to the search path list * * Adds @path to the search paths list of @loader. * * Since: 1.0 */ void gdict_source_loader_add_search_path (GdictSourceLoader *loader, const gchar *path) { GSList *l; g_return_if_fail (GDICT_IS_SOURCE_LOADER (loader)); g_return_if_fail (path != NULL); /* avoid duplications */ for (l = loader->priv->paths; l != NULL; l = l->next) if (strcmp (path, (gchar *) l->data) == 0) return; loader->priv->paths = g_slist_append (loader->priv->paths, g_strdup (path)); loader->priv->paths_dirty = TRUE; }
/* create the list of dictionary source files, by scanning the search path * directories for .desktop files; we disavow symlinks and sub-directories * for the time being. */ static GSList * build_source_filenames (GdictSourceLoader *loader) { GSList *retval, *d; g_assert (GDICT_IS_SOURCE_LOADER (loader)); if (!loader->priv->paths) return NULL; retval = NULL; for (d = loader->priv->paths; d != NULL; d = d->next) { gchar *path = (gchar *) d->data; const gchar *filename; GDir *dir; dir = g_dir_open (path, 0, NULL); if (!dir) continue; do { filename = g_dir_read_name (dir); if (filename) { gchar *full_path; if (!g_str_has_suffix (filename, GDICT_SOURCE_FILE_SUFFIX)) break; full_path = g_build_filename (path, filename, NULL); if (g_file_test (full_path, G_FILE_TEST_IS_REGULAR)) { retval = g_slist_prepend (retval, full_path); } } } while (filename != NULL); g_dir_close (dir); } return g_slist_reverse (retval); }
/** * gdict_source_loader_get_source: * @loader: a #GdictSourceLoader * @name: a name of a dictionary source * * Retrieves a dictionary source using @name. You can use the returned * #GdictSource object to create the right #GdictContext for that * dictionary source. * * Return value: a referenced #GdictSource object. You should de-reference * it using g_object_unref() when you finished using it. * * Since: 1.0 */ GdictSource * gdict_source_loader_get_source (GdictSourceLoader *loader, const gchar *name) { GdictSource *retval; g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL); g_return_val_if_fail (name != NULL, NULL); if (loader->priv->paths_dirty) gdict_source_loader_update_sources (loader); retval = g_hash_table_lookup (loader->priv->sources_by_name, name); if (retval) return g_object_ref (retval); return NULL; }
void gdict_show_pref_dialog (GtkWidget *parent, const gchar *title, GdictSourceLoader *loader) { GtkWidget *dialog; g_return_if_fail (GTK_IS_WIDGET (parent)); g_return_if_fail (GDICT_IS_SOURCE_LOADER (loader)); if (parent) dialog = g_object_get_data (G_OBJECT (parent), "gdict-pref-dialog"); else dialog = global_dialog; if (!dialog) { dialog = g_object_new (GDICT_TYPE_PREF_DIALOG, "source-loader", loader, "title", title, NULL); g_object_ref_sink (dialog); g_signal_connect (dialog, "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL); if (parent && GTK_IS_WINDOW (parent)) { gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent)); gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE); g_object_set_data_full (G_OBJECT (parent), "gdict-pref-dialog", dialog, g_object_unref); } else global_dialog = dialog; } gtk_window_set_screen (GTK_WINDOW (dialog), gtk_widget_get_screen (parent)); gtk_window_present (GTK_WINDOW (dialog)); }