コード例 #1
0
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;
}
コード例 #2
0
/**
 * 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;
}
コード例 #3
0
/**
 * gdict_source_chooser_refresh:
 * @chooser: a #GdictSourceChooser
 *
 * Forces a refresh on the contents of the source chooser widget
 *
 * Since: 0.12
 */
void
gdict_source_chooser_refresh (GdictSourceChooser *chooser)
{
  GdictSourceChooserPrivate *priv;

  g_return_if_fail (GDICT_IS_SOURCE_CHOOSER (chooser));

  priv = chooser->priv;

  if (priv->loader)
    {
      const GSList *sources, *l;

      if (priv->treeview)
        gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview), NULL);

      gtk_list_store_clear (priv->store);

      sources = gdict_source_loader_get_sources (priv->loader);
      for (l = sources; l != NULL; l = l->next)
        {
          GdictSource *source = l->data;
          const gchar *name, *description;
          GdictSourceTransport transport;
          gint weight;

          transport = gdict_source_get_transport (source);
          name = gdict_source_get_name (source);
          description = gdict_source_get_description (source);
          weight = PANGO_WEIGHT_NORMAL;

          if (priv->current_source && !strcmp (priv->current_source, name))
            weight = PANGO_WEIGHT_BOLD;

          gtk_list_store_insert_with_values (priv->store, NULL, -1,
                                             SOURCE_TRANSPORT, transport,
                                             SOURCE_NAME, name,
                                             SOURCE_DESCRIPTION, description,
                                             SOURCE_CURRENT, weight,
                                             -1);
        }

      if (priv->treeview)
        gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
                                 GTK_TREE_MODEL (priv->store));
    }
}
コード例 #4
0
static void
update_sources_view (GdictPrefDialog *dialog)
{
  const GSList *sources, *l;
  
  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->sources_view), NULL);
  
  gtk_list_store_clear (dialog->sources_list);
  
  /* force update of the sources list */
  gdict_source_loader_update (dialog->loader);
  
  sources = gdict_source_loader_get_sources (dialog->loader);
  for (l = sources; l != NULL; l = l->next)
    {
      GdictSource *source = GDICT_SOURCE (l->data);
      GtkTreeIter iter;
      const gchar *name, *description;
      gboolean is_active = FALSE;
      
      name = gdict_source_get_name (source);
      description = gdict_source_get_description (source);
      if (!description)
	description = name;

      if (strcmp (name, dialog->active_source) == 0)
        is_active = TRUE;

      gtk_list_store_append (dialog->sources_list, &iter);
      gtk_list_store_set (dialog->sources_list, &iter,
      			  SOURCES_ACTIVE_COLUMN, is_active,
      			  SOURCES_NAME_COLUMN, name,
      			  SOURCES_DESCRIPTION_COLUMN, description,
      			  -1);
    }

  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->sources_view),
  			   GTK_TREE_MODEL (dialog->sources_list));
  
  /* select the currently active source name */
  gtk_tree_model_foreach (GTK_TREE_MODEL (dialog->sources_list),
  			  select_active_source_name,
  			  dialog);
}
コード例 #5
0
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);
}
コード例 #6
0
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);
}