changeset 4020:66529edae49d

added tag editing capabilities, maybe raw yet
author Eugene Zagidullin <e.asphyx@gmail.com>
date Tue, 27 Nov 2007 17:42:52 +0300
parents 29650db2d5f2
children d79fd54de4fa 17bf734f8187
files src/audacious/playlist.c src/audacious/ui_fileinfo.c src/audacious/ui_fileinfo.h
diffstat 3 files changed, 202 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/playlist.c	Tue Nov 27 01:58:26 2007 +0300
+++ b/src/audacious/playlist.c	Tue Nov 27 17:42:52 2007 +0300
@@ -2453,44 +2453,55 @@
 
     PLAYLIST_UNLOCK(playlist);
 
-    if (entry->tuple)
-        mtime = tuple_get_int(entry->tuple, FIELD_MTIME, NULL);
-    else
-        mtime = 0;
-
-    /* No tuple? Try to set this entry up properly. --nenolod */
-    if (entry->tuple == NULL || mtime == -1 ||
-        mtime == 0 || mtime != playlist_get_mtime(entry->filename))
+    if (entry->decoder == NULL)
     {
-        playlist_entry_get_info(entry);
-        tuple = entry->tuple;
+        pr = input_check_file(entry->filename, FALSE); /* try to find a decoder */
+        entry->decoder = pr ? pr->ip : NULL;
+
+        g_free(pr);
     }
 
-    if (tuple != NULL)
-    {
-        if (entry->decoder == NULL)
+    /* plugin is capable to update tags. we need to bypass tuple cache. --eugene */
+    /* maybe code cleanup required... */
+    if (entry->decoder != NULL && entry->decoder->update_song_tuple != NULL &&
+        entry->decoder->file_info_box == NULL && path != NULL) {
+
+        fileinfo_show_editor_for_path(path, entry->decoder);
+        g_free(path);
+
+    } else {
+
+        if (entry->tuple)
+            mtime = tuple_get_int(entry->tuple, FIELD_MTIME, NULL);
+        else
+            mtime = 0;
+
+        /* No tuple? Try to set this entry up properly. --nenolod */
+        if (entry->tuple == NULL || mtime == -1 ||
+            mtime == 0 || mtime != playlist_get_mtime(entry->filename))
         {
-            pr = input_check_file(entry->filename, FALSE); /* try to find a decoder */
-            entry->decoder = pr ? pr->ip : NULL;
-
-            g_free(pr);
+            playlist_entry_get_info(entry);
+            tuple = entry->tuple;
         }
 
-        if (entry->decoder != NULL && entry->decoder->file_info_box == NULL)
-            fileinfo_show_for_tuple(tuple);
-        else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL)
-            entry->decoder->file_info_box(path);
-        else
-            fileinfo_show_for_path(path);
-        g_free(path);
-    }
-    else if (path != NULL)
-    {
-        if (entry != NULL && entry->decoder != NULL && entry->decoder->file_info_box != NULL)
-            entry->decoder->file_info_box(path);
-        else
-            fileinfo_show_for_path(path);
-        g_free(path);
+        if (tuple != NULL)
+        {
+            if (entry->decoder != NULL && entry->decoder->file_info_box == NULL)
+                fileinfo_show_for_tuple(tuple, FALSE);
+            else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL)
+                entry->decoder->file_info_box(path);
+            else
+                fileinfo_show_for_path(path);
+            g_free(path);
+        }
+        else if (path != NULL)
+        {
+            if (entry != NULL && entry->decoder != NULL && entry->decoder->file_info_box != NULL)
+                entry->decoder->file_info_box(path);
+            else
+                fileinfo_show_for_path(path);
+            g_free(path);
+        }
     }
 }
 
@@ -2512,23 +2523,32 @@
 
     PLAYLIST_UNLOCK(playlist);
 
-    if (tuple != NULL)
-    {
-        if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL)
-            fileinfo_show_for_tuple(tuple);
-        else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL)
-            playlist->position->decoder->file_info_box(path);
-        else
-            fileinfo_show_for_path(path);
+    if (playlist->position->decoder != NULL && playlist->position->decoder->update_song_tuple != NULL &&
+        playlist->position->decoder->file_info_box == NULL && path != NULL) {
+
+        fileinfo_show_editor_for_path(path, playlist->position->decoder);
         g_free(path);
-    }
-    else if (path != NULL)
-    {
-        if (playlist->position != NULL && playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL)
-            playlist->position->decoder->file_info_box(path);
-        else
-            fileinfo_show_for_path(path);
-        g_free(path);
+
+    } else {
+
+        if (tuple != NULL)
+        {
+            if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL)
+                fileinfo_show_for_tuple(tuple, FALSE);
+            else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL)
+                playlist->position->decoder->file_info_box(path);
+            else
+                fileinfo_show_for_path(path);
+            g_free(path);
+        }
+        else if (path != NULL)
+        {
+            if (playlist->position != NULL && playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL)
+                playlist->position->decoder->file_info_box(path);
+            else
+                fileinfo_show_for_path(path);
+            g_free(path);
+        }
     }
 }
 
--- a/src/audacious/ui_fileinfo.c	Tue Nov 27 01:58:26 2007 +0300
+++ b/src/audacious/ui_fileinfo.c	Tue Nov 27 17:42:52 2007 +0300
@@ -60,6 +60,8 @@
 #include "ui_fileinfo.h"
 #include "ui_playlist.h"
 
+#define G_FREE_CLEAR(a) { if(a != NULL) { g_free(a); a = NULL; } }
+
 GtkWidget *fileinfo_win;
 
 GtkWidget *entry_location;
@@ -76,9 +78,10 @@
 GtkWidget *image_fileicon;
 GtkWidget *label_format_name;
 GtkWidget *label_quality;
+GtkWidget *btn_apply;
 
-//static gchar *current_file = NULL;
-//static InputPlugin *current_ip = NULL;
+static gchar *current_file = NULL;
+static InputPlugin *current_ip = NULL;
 
 static void
 fileinfo_entry_set_text(GtkWidget *widget, const char *text)
@@ -86,7 +89,48 @@
     if (widget == NULL)
         return;
 
-    gtk_entry_set_text(GTK_ENTRY(widget), text);
+    gtk_entry_set_text(GTK_ENTRY(widget), text != NULL ? text : "");
+}
+
+static void
+set_entry_str_from_field(GtkWidget *widget, Tuple *tuple, gint fieldn)
+{
+    gchar *text;
+
+    if(widget != NULL) {
+        text = (gchar*)tuple_get_string(tuple, fieldn, NULL);
+        gtk_entry_set_text(GTK_ENTRY(widget), text != NULL ? text : "");
+    }
+}
+
+static void
+set_entry_int_from_field(GtkWidget *widget, Tuple *tuple, gint fieldn)
+{
+    gchar *text;
+
+    if(widget == NULL) return;
+
+    if(tuple_get_value_type(tuple, fieldn, NULL) == TUPLE_INT) {
+        text = g_strdup_printf("%d", tuple_get_int(tuple, fieldn, NULL));
+        gtk_entry_set_text(GTK_ENTRY(widget), text);
+        g_free(text);
+    } else {
+        gtk_entry_set_text(GTK_ENTRY(widget), "");
+    }
+}
+
+static void
+set_field_str_from_entry(Tuple *tuple, gint fieldn, GtkWidget *widget)
+{
+    if(widget == NULL) return;
+    tuple_associate_string(tuple, fieldn, NULL, gtk_entry_get_text(GTK_ENTRY(widget)));
+}
+
+static void
+set_field_int_from_entry(Tuple *tuple, gint fieldn, GtkWidget *widget)
+{
+    if(widget == NULL) return;
+    tuple_associate_int(tuple, fieldn, NULL, atoi(gtk_entry_get_text(GTK_ENTRY(widget))));
 }
 
 static void
@@ -109,17 +153,6 @@
 }
 
 static void
-fileinfo_entry_set_text_free(GtkWidget *widget, char *text)
-{
-    if (widget == NULL)
-        return;
-
-    gtk_entry_set_text(GTK_ENTRY(widget), text);
-
-    g_free(text);
-}
-
-static void
 fileinfo_entry_set_image(GtkWidget *widget, const char *text)
 {
         GdkPixbuf *pixbuf;
@@ -173,12 +206,51 @@
     fileinfo_entry_set_text(entry_track, "");
     fileinfo_entry_set_text(entry_location, "");
 
+    fileinfo_label_set_text(label_format_name, NULL);
+    fileinfo_label_set_text(label_quality, NULL);
+
+    current_ip = NULL;
+    G_FREE_CLEAR(current_file);
+
     fileinfo_entry_set_image(image_artwork, DATA_DIR "/images/audio.png");
 }
 
-void fileinfo_update_tuple(gpointer data)
+static void fileinfo_update_tuple(gpointer data)
 {
-  /* TODO */
+        Tuple *tuple;
+        VFSFile *fd;
+        if(current_file != NULL && current_ip != NULL && current_ip->update_song_tuple != NULL) { /* TODO only if changed*/
+
+            tuple = tuple_new();
+            fd = vfs_fopen(current_file, "r+b");
+
+            if (fd != NULL) {
+                set_field_str_from_entry(tuple, FIELD_TITLE, entry_title);
+                set_field_str_from_entry(tuple, FIELD_ARTIST, entry_artist);
+                set_field_str_from_entry(tuple, FIELD_ALBUM, entry_album);
+                set_field_str_from_entry(tuple, FIELD_COMMENT, entry_comment);
+                set_field_str_from_entry(tuple, FIELD_GENRE, entry_genre);
+
+                set_field_int_from_entry(tuple, FIELD_YEAR, entry_year);
+                set_field_int_from_entry(tuple, FIELD_TRACK_NUMBER, entry_track);
+                
+                if(current_ip->update_song_tuple(tuple, fd)) {
+                    /* TODO display ok */
+                    fprintf(stderr, "fileinfo_update_tuple(): updated\n");
+                } else {
+                    /* TODO error message */
+                    fprintf(stderr, "fileinfo_update_tuple(): failed\n");
+                }
+
+                vfs_fclose(fd);
+
+            } else {
+                /* TODO: error message */
+                fprintf(stderr, "fileinfo_update_tuple(): can't open file %s\n", current_file);
+            }
+
+            mowgli_object_unref(tuple);
+        }
 }
 
 
@@ -265,7 +337,6 @@
     GtkWidget *table1;
     GtkWidget *bbox_close;
     GtkWidget *btn_close;
-    GtkWidget *btn_apply;
     GtkWidget *alignment;
     GtkWidget *separator;
 
@@ -458,39 +529,42 @@
     gtk_widget_show_all (hbox);
 }
 
-void
-fileinfo_show_for_tuple(Tuple *tuple)
+void 
+fileinfo_show_for_tuple(Tuple *tuple, gboolean updating_enabled)
 {
-        gchar *tmp = NULL;
+        gchar *tmp, *tmp_utf = NULL;
         GdkPixbuf *icon = NULL;
 
         if (tuple == NULL)
                 return;
+        
+        if(!updating_enabled) {
+            current_ip = NULL;
+            G_FREE_CLEAR(current_file);
+        }
 
         if(!GTK_WIDGET_REALIZED(fileinfo_win)) gtk_widget_realize(fileinfo_win);
 
-        fileinfo_entry_set_text(entry_title, tuple_get_string(tuple, FIELD_TITLE, NULL));
-        fileinfo_entry_set_text(entry_artist, tuple_get_string(tuple, FIELD_ARTIST, NULL));
-        fileinfo_entry_set_text(entry_album, tuple_get_string(tuple, FIELD_ALBUM, NULL));
-        fileinfo_entry_set_text(entry_comment, tuple_get_string(tuple, FIELD_COMMENT, NULL));
-        fileinfo_entry_set_text(entry_genre, tuple_get_string(tuple, FIELD_GENRE, NULL));
+        set_entry_str_from_field(entry_title, tuple, FIELD_TITLE);
+        set_entry_str_from_field(entry_artist, tuple, FIELD_ARTIST);
+        set_entry_str_from_field(entry_album, tuple, FIELD_ALBUM);
+        set_entry_str_from_field(entry_comment, tuple, FIELD_COMMENT);
+        set_entry_str_from_field(entry_genre, tuple, FIELD_GENRE);
 
         tmp = g_strdup_printf("%s/%s",
                 tuple_get_string(tuple, FIELD_FILE_PATH, NULL),
                 tuple_get_string(tuple, FIELD_FILE_NAME, NULL));
         if(tmp){
-                fileinfo_entry_set_text_free(entry_location, str_to_utf8(tmp));
+                tmp_utf = str_to_utf8(tmp);
+                fileinfo_entry_set_text(entry_location, tmp_utf);
+                g_free(tmp_utf);
                 g_free(tmp);
                 tmp = NULL;
         }
-
-        if (tuple_get_int(tuple, FIELD_YEAR, NULL))
-                fileinfo_entry_set_text_free(entry_year,
-                        g_strdup_printf("%d", tuple_get_int(tuple, FIELD_YEAR, NULL)));
-
-        if (tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL))
-                fileinfo_entry_set_text_free(entry_track,
-                        g_strdup_printf("%d", tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL)));
+        
+        /* set empty string if field not availaible. --eugene */
+        set_entry_int_from_field(entry_year, tuple, FIELD_YEAR);
+        set_entry_int_from_field(entry_track, tuple, FIELD_TRACK_NUMBER);
 
         fileinfo_label_set_text(label_format_name, tuple_get_string(tuple, FIELD_CODEC, NULL));
         fileinfo_label_set_text(label_quality, tuple_get_string(tuple, FIELD_QUALITY, NULL));
@@ -511,6 +585,11 @@
                 fileinfo_entry_set_image(image_artwork, tmp);
                 g_free(tmp);
         }
+
+        if(updating_enabled && current_file != NULL && current_ip != NULL && current_ip->update_song_tuple != NULL)
+                gtk_widget_set_sensitive(btn_apply, TRUE);
+        else
+                gtk_widget_set_sensitive(btn_apply, FALSE);
         
         if(! GTK_WIDGET_VISIBLE(fileinfo_win)) gtk_widget_show(fileinfo_win);
 }
@@ -523,7 +602,24 @@
         if (tuple == NULL)
                 return input_file_info_box(path);
 
-        fileinfo_show_for_tuple(tuple);
+        fileinfo_show_for_tuple(tuple, FALSE);
 
         mowgli_object_unref(tuple);
 }
+
+void
+fileinfo_show_editor_for_path(gchar *path, InputPlugin *ip)
+{
+        G_FREE_CLEAR(current_file);
+        current_file = g_strdup(path);
+        current_ip = ip;
+
+        Tuple *tuple = input_get_song_tuple(path);
+
+        if (tuple == NULL)
+                return input_file_info_box(path);
+
+        fileinfo_show_for_tuple(tuple, TRUE);
+
+        mowgli_object_unref(tuple);
+}
--- a/src/audacious/ui_fileinfo.h	Tue Nov 27 01:58:26 2007 +0300
+++ b/src/audacious/ui_fileinfo.h	Tue Nov 27 17:42:52 2007 +0300
@@ -20,11 +20,13 @@
 #define _UI_FILEINFO_H_
 
 #include "tuple.h"
+#include "plugin.h"
 #include <glib.h>
 
 void create_fileinfo_window(void);
-void fileinfo_show_for_tuple(Tuple *tuple);
+void fileinfo_show_for_tuple(Tuple *tuple, gboolean updating_enabled);
 gchar* fileinfo_recursive_get_image(const gchar* path, const gchar* file_name, gint depth);
 void fileinfo_show_for_path(gchar *path);
+void fileinfo_show_editor_for_path(gchar *path, InputPlugin *ip);
 
 #endif