Changeset 13808


Ignore:
Timestamp:
Jan 19, 2013, 8:44:23 AM (8 years ago)
Author:
jordan
Message:

(trunk) #1220 'change top folder names' -- add file-renaming to the GTK+ GUI

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/file-list.c

    r13760 r13808  
    778778}
    779779
     780struct rename_data
     781{
     782  int error;
     783  char * newname;
     784  char * path_string;
     785  FileData * file_data;
     786};
     787
     788static int
     789on_rename_done_idle (struct rename_data * data)
     790{
     791  if (data->error == 0)
     792    {
     793      GtkTreeIter iter;
     794
     795      if (gtk_tree_model_get_iter_from_string (data->file_data->model, &iter, data->path_string))
     796        gtk_tree_store_set (data->file_data->store, &iter, FC_LABEL, data->newname, -1);
     797    }
     798  else
     799    {
     800      GtkWidget * w = gtk_message_dialog_new (
     801        GTK_WINDOW (gtk_widget_get_toplevel(data->file_data->top)),
     802        GTK_DIALOG_MODAL,
     803        GTK_MESSAGE_ERROR,
     804        GTK_BUTTONS_CLOSE,
     805        _("Unable to rename file as \"%s\": %s"),
     806        data->newname,
     807        tr_strerror(data->error));
     808      gtk_message_dialog_format_secondary_text (
     809        GTK_MESSAGE_DIALOG (w), "%s",
     810        _("Please correct the errors and try again."));
     811      gtk_dialog_run (GTK_DIALOG (w));
     812      gtk_widget_destroy (w);
     813    }
     814
     815  /* cleanup */
     816  g_free (data->path_string);
     817  g_free (data->newname);
     818  g_free (data);
     819  return G_SOURCE_REMOVE;
     820}
     821
     822static void
     823on_rename_done (tr_torrent          * tor          G_GNUC_UNUSED,
     824                const char          * oldpath      G_GNUC_UNUSED,
     825                const char          * newname      G_GNUC_UNUSED,
     826                int                   error,
     827                struct rename_data  * rename_data)
     828{
     829  rename_data->error = error;
     830  gdk_threads_add_idle ((GSourceFunc)on_rename_done_idle, rename_data);
     831}
     832
     833static void
     834cell_edited_callback (GtkCellRendererText * cell G_GNUC_UNUSED,
     835                      gchar               * path_string,
     836                      gchar               * newname,
     837                      FileData            * data)
     838{
     839  tr_torrent * tor;
     840  GString * oldpath;
     841  GtkTreeIter iter;
     842  struct rename_data * rename_data;
     843
     844  tor = gtr_core_find_torrent (data->core, data->torrentId);
     845  if (tor == NULL)
     846    return;
     847  if (!gtk_tree_model_get_iter_from_string (data->model, &iter, path_string))
     848    return;
     849
     850  /* build oldpath */
     851  oldpath = g_string_new (NULL);
     852  for (;;)
     853    {
     854      char * token = NULL;
     855      GtkTreeIter child;
     856      gtk_tree_model_get (data->model, &iter, FC_LABEL, &token, -1);
     857      g_string_prepend (oldpath, token);
     858      g_free (token);
     859
     860      child = iter;
     861      if (!gtk_tree_model_iter_parent (data->model, &iter, &child))
     862        break;
     863
     864      g_string_prepend_c (oldpath, G_DIR_SEPARATOR);
     865    }
     866
     867  /* do the renaming */
     868  rename_data = g_new0 (struct rename_data, 1);
     869  rename_data->newname = g_strdup (newname);
     870  rename_data->file_data = data;
     871  rename_data->path_string = g_strdup (path_string);
     872  tr_torrentRenamePath (tor, oldpath->str, newname, (tr_torrent_rename_done_func*)on_rename_done, rename_data);
     873
     874  /* cleanup */
     875  g_string_free (oldpath, TRUE);
     876}
     877
     878
    780879GtkWidget *
    781880gtr_file_list_new (TrCore * core, int torrentId)
     
    834933    /* add text renderer */
    835934    rend = gtk_cell_renderer_text_new ();
     935    g_object_set (rend, "editable", TRUE, NULL);
    836936    g_object_set (rend, "ellipsize", PANGO_ELLIPSIZE_END, "font-desc", pango_font_description, NULL);
     937    g_signal_connect (rend, "edited", (GCallback)cell_edited_callback, data);
    837938    gtk_tree_view_column_pack_start (col, rend, TRUE);
    838939    gtk_tree_view_column_set_attributes (col, rend, "text", FC_LABEL, NULL);
Note: See TracChangeset for help on using the changeset viewer.