uncomment implemented functions
[melted_gui] / src / playlist.c
index a3be0e6..6d03b08 100644 (file)
 #include <gdk/gdkkeysyms.h>
 #include <pthread.h>
 
+#include <errno.h>
+
 #include "playlist.h"
 #include "ui.h"
 #include "timecode.h"
+#include "library.h"
 
 extern GtkTargetEntry drag_targets[];
 
@@ -338,6 +341,9 @@ void playlist_block(instance_t* app, int loop)
     start = list[0];
     stop = list[c - 1];
 
+    if(loop)
+        loop = PLAYLIST_BLOCK_LOOP;
+
     if(!playlist_range_cued(app, start, stop))
     {
         /* update selected item */
@@ -566,411 +572,398 @@ void playlist_insert_items(instance_t* app, int idx, playlist_item_t* items, int
     pthread_mutex_unlock(&app->playlist.lock);
 };
 
-#if 0
-
-static int load_file_ply(omnplay_instance_t* app, char* filename)
+void playlist_item_copy(instance_t* app)
 {
-    FILE* f;
-    char *ID, *CH, *B, *IN, *OUT, *DUR, *REST, *l;
-    int count = 0, i;
-    playlist_item_t* items;
+    int *list, i, c;
 
-    /* allocate space for strings and items */
-    items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
-    memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
-    ID = malloc(PATH_MAX);
-    CH = malloc(PATH_MAX);
-    B = malloc(PATH_MAX);
-    IN = malloc(PATH_MAX);
-    OUT = malloc(PATH_MAX);
-    DUR = malloc(PATH_MAX);
-    REST = malloc(PATH_MAX);
-    l = malloc(PATH_MAX);
-
-    /* open and process file */
-    f = fopen(filename, "rt");
-    if(f)
-    {
-        while( !feof(f) )
-        {
-            char* s;
+    list = playlist_get_selected_items_idx(app, &c);
+    if(!list) return;
 
-            /* load string */
-            memset(l, 0, PATH_MAX);
-            fgets(l, PATH_MAX, f);
+    for(i = 0; i < c; i++)
+        app->clipboard.item[i] = app->playlist.item[list[i]];
+    app->clipboard.count = c;
 
-            /* remove newlines */
-            if( (s = strchr(l, '\n')) ) *s = 0;
-            if( (s = strchr(l, '\r')) ) *s = 0;
-            if( (s = strchr(l, '\t')) ) *s = 0;
+    free(list);
+};
 
-            /* check for empty line */
-            if(l[0] && l[0] != '#')
-            {
-                if (6 != sscanf(l, "%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%s",
-                    ID, CH, B, IN, OUT, DUR, REST))
-                {
-                    int b = atol(B);
-                    /* setup item */
-                    tc2frames(IN, 25.0, &items[count].in);
-                    tc2frames(DUR, 25.0, &items[count].dur);
-                    strncpy(items[count].id, ID, PATH_MAX);
-                    items[count].player = atol(CH) - 1;
-                    switch(b)
-                    {
-                        case 1: items[count].type = PLAYLIST_ITEM_BLOCK_SINGLE; break;
-                        case 2: items[count].type = PLAYLIST_ITEM_LOOP_BEGIN; break;
-                        case 3: items[count].type = PLAYLIST_ITEM_LOOP_BODY; break;
-                        case 4: items[count].type = PLAYLIST_ITEM_LOOP_END; break;
-                        case 6: items[count].type = PLAYLIST_ITEM_BLOCK_END; break;
-                        case 0:
-                            if(!count)
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
-                            else if(items[count - 1].type == PLAYLIST_ITEM_BLOCK_BEGIN ||
-                                    items[count - 1].type == PLAYLIST_ITEM_BLOCK_BODY)
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BODY;
-                            else
-                                items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
-                            break;
-                        default:
-                            if(b >= 1024)
-                                items[count].type = b - 1024;
-                    };
-#if 0
-                    {
-                        char* n;
-                        switch(items[count].type)
-                        {
-                            case PLAYLIST_ITEM_BLOCK_BEGIN:       n = "BLOCK_BEGIN"; break;
-                            case PLAYLIST_ITEM_BLOCK_BODY:        n = "BLOCK_BODY"; break;
-                            case PLAYLIST_ITEM_BLOCK_END:         n = "BLOCK_END"; break;
-                            case PLAYLIST_ITEM_BLOCK_SINGLE:      n = "BLOCK_SINGLE"; break;
-                            case PLAYLIST_ITEM_LOOP_BEGIN:        n = "LOOP_BEGIN"; break;
-                            case PLAYLIST_ITEM_LOOP_BODY:         n = "LOOP_BODY"; break;
-                            case PLAYLIST_ITEM_LOOP_END:          n = "LOOP_END"; break;
-                            case PLAYLIST_ITEM_LOOP_SINGLE:       n = "LOOP_SINGLE"; break;
-                        };
-                        fprintf(stderr, "src=[%s]\ndst=[idx=%d,block=%s,block_id=%d,in=%d,out=%d]\n",
-                            l, count, n, items[count].type, items[count].in, items[count].dur);
-                    };
-#endif
+void playlist_item_paste(instance_t* app, int after)
+{
+    int idx, i;
+    playlist_item_type_t t;
 
-                    count++;
-                }
-            };
-        }
+    /* find insert position */
+    idx = playlist_get_first_selected_item_idx(app);
+    if(idx < 0)
+        idx = 0;
+    else
+        idx += (after)?1:0;
 
-        fclose(f);
-    }
+    if(!playlist_insert_check(app, idx, &t))
+        return;
 
-    /* add loaded items to playlist */
-    if(count)
+    /* clear item */
+    if(app->clipboard.count)
     {
-        pthread_mutex_lock(&app->playlist.lock);
-        for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
+        for(i = 0; i < app->clipboard.count; i++)
         {
-            omnplay_library_normalize_item(app, &items[i]);
-            app->playlist.item[app->playlist.count++] = items[i];
+            app->clipboard.item[i].type = t;
+            app->clipboard.item[i].error = 0;
         };
-        app->playlist.ver_curr++;
-        pthread_mutex_unlock(&app->playlist.lock);
-    }
-
-    /* free data */
-    free(items);
-    free(ID);
-    free(CH);
-    free(IN);
-    free(OUT);
-    free(DUR);
-    free(REST);
-    free(l);
-
-    return count;
+        playlist_insert_items(app, idx, app->clipboard.item, app->clipboard.count);
+    };
 };
 
-void omnplay_playlist_load(omnplay_instance_t* app)
+void playlist_item_swap(instance_t* app, int dir)
 {
-    int r;
-    GtkWidget *dialog;
-    GtkFileFilter *filter;
+    int sel, a, b, e = 1;
+    playlist_item_t item;
+
+    /* find insert position */
+    sel = playlist_get_first_selected_item_idx(app);
+    if(sel < 0)
+        return;
 
-    dialog = gtk_file_chooser_dialog_new("Open File",
-        GTK_WINDOW (app->window),
-        GTK_FILE_CHOOSER_ACTION_OPEN,
-        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-        NULL);
+    if(dir < 0)
+    {
+        a = sel - 1;
+        b = sel;
+        sel = a;
+    }
+    else
+    {
+        a = sel;
+        b = sel + 1;
+        sel = b;
+    };
 
-    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
-        (app->playlist.path)?app->playlist.path:getenv("HOME"));
+    /* check for playing block */
+    if(playlist_idx_cued(app, a, NULL) || playlist_idx_cued(app, b, NULL))
+        return;
 
-    filter = gtk_file_filter_new();
-    gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
-    gtk_file_filter_add_pattern(filter, "*.ply");
-    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
-    filter = gtk_file_filter_new();
-    gtk_file_filter_set_name(filter, "All types (*.*)");
-    gtk_file_filter_add_pattern(filter, "*.*");
-    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
 
-    r = gtk_dialog_run(GTK_DIALOG(dialog));
+    /* swap */
+    item = app->playlist.item[a];
+    app->playlist.item[a] = app->playlist.item[b];
+    app->playlist.item[b] = item;
 
-    if(r == GTK_RESPONSE_ACCEPT)
+    /* rewite type */
+    if(app->playlist.item[a].type != app->playlist.item[b].type)
     {
-        char *filename;
-
-        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+        e = 0;
+        app->playlist.item[a].type = PLAYLIST_ITEM_BLOCK_SINGLE;
+        app->playlist.item[b].type = PLAYLIST_ITEM_BLOCK_SINGLE;
+    };
 
-        r = load_file_ply(app, filename);
+    /* redraw main items */
+    ui_playlist_draw_item(app, a);
+    ui_playlist_draw_item(app, b);
 
-        if(r)
-            omnplay_playlist_draw(app);
+    /* fix block types */
+    if(a && !e)
+    {
+        app->playlist.item[a - 1].type = (playlist_item_type_t)(app->playlist.item[a - 1].type |
+            PLAYLIST_BLOCK_END);
+        ui_playlist_draw_item(app, a - 1);
+    };
+    if(b + 1 < app->playlist.count && !e)
+    {
+        app->playlist.item[b + 1].type = (playlist_item_type_t)(app->playlist.item[b + 1].type |
+            PLAYLIST_BLOCK_BEGIN);
+        ui_playlist_draw_item(app, b + 1);
+    };
 
-        if(app->playlist.path)
-            g_free(app->playlist.path);
-        if((app->playlist.path = filename))
-        {
-            char* e = strrchr(app->playlist.path, '/');
-            if(e) *e = 0;
-        }
-    }
+    /* select */
+    ui_playlist_select_item(app, sel);
 
-    gtk_widget_destroy (dialog);
+    pthread_mutex_unlock(&app->players.lock);
+    pthread_mutex_unlock(&app->playlist.lock);
 };
 
-static int save_file_ply(omnplay_instance_t* app, char* filename)
+static int playlist_load_plt(instance_t* app, char* filename, char* err_buf, int err_len)
 {
-    int i;
     FILE* f;
-    char tc1[12], tc2[12], tc3[12];
-    char* fname = filename;
+    char *line;
+    int count = 0, i = 0;
+    playlist_item_t* items;
 
-    filename = (char*)malloc(PATH_MAX);
-    strncpy(filename, fname, PATH_MAX);
-    i = strlen(filename);
-    if(i < 4 || strcasecmp(filename + i - 4, ".ply"))
-        strcat(filename, ".ply");
+    f = fopen(filename, "rt");
 
-    if((f = fopen(filename, "wt")))
+    if(!f)
     {
-        for(i = 0; i < app->playlist.count; i++)
-            fprintf(f, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
-                app->playlist.item[i].id,
-                app->playlist.item[i].player + 1,
-                app->playlist.item[i].type + 1024,
-                frames2tc(app->playlist.item[i].in, 25.0, tc1),
-                frames2tc(app->playlist.item[i].in + app->playlist.item[i].dur, 25.0, tc2),
-                frames2tc(app->playlist.item[i].dur, 25.0, tc3));
+        i = errno;
+        snprintf(err_buf, err_len, "Failed to open file [%s], error: %s",
+            filename, strerror(i));
+        return -i;
     };
 
-    free(filename);
-
-    return 0;
-};
-
-void omnplay_playlist_save(omnplay_instance_t* app)
-{
-    int r;
-    GtkWidget *dialog;
-    GtkFileFilter *filter;
+    /* allocate space for strings and items */
+    items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
+    memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
+    line = malloc(PATH_MAX);
 
-    dialog = gtk_file_chooser_dialog_new("Save File",
-        GTK_WINDOW (app->window),
-        GTK_FILE_CHOOSER_ACTION_SAVE,
-        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-        GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
-        NULL);
+    while(1)
+    {
+        char* s;
 
-    gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
+        if(feof(f))
+            break;
 
-    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
-        (app->playlist.path)?app->playlist.path:getenv("HOME"));
+        /* load string */
+        memset(line, 0, PATH_MAX);
+        fgets(line, PATH_MAX, f);
 
-    filter = gtk_file_filter_new();
-    gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
-    gtk_file_filter_add_pattern(filter, "*.ply");
-    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
-    g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(0));
-    filter = gtk_file_filter_new();
-    gtk_file_filter_set_name(filter, "Text (*.txt)");
-    gtk_file_filter_add_pattern(filter, "*.*");
-    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
-    g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(1));
+        /* remove newlines */
+        if( (s = strchr(line, '\n')) ) *s = 0;
+        if( (s = strchr(line, '\r')) ) *s = 0;
 
-    r = gtk_dialog_run(GTK_DIALOG(dialog));
+        /* check for empty line */
+        if(!line[0]  || line[0] == '#')
+            continue;
 
-    if(r == GTK_RESPONSE_ACCEPT)
-    {
-        char *filename;
+        /* check item contine */
+        if(line[0] == '\t')
+        {
+            /* line without header */
+            if(!i)
+                continue;
 
-        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+            /* load playlist item */
+            switch(i)
+            {
+                case 1:         // title
+                    strncpy(items[count].title, line + 1, PATH_MAX);
+                    i++;
+                    break;
+
+                case 2:         // in
+                    tc2frames(line + 1, 25.0, &items[count].in);
+                    i++;
+                    break;
+
+                case 3:         // dur
+                    tc2frames(line + 1, 25.0, &items[count].dur);
+                    i++;
+                    break;
+
+                case 4:         // player
+                    items[count].player = atol(line + 1) - 1;
+                    i++;
+                    break;
+
+                case 5:         // type
+                    items[count].type = atol(line + 1) - 1024;
+
+                    /* last item - break a chain */
+                    i = 0;
+                    count++;
+                    break;
+            };
+        }
+        else
+        {
+            strcpy(items[count].id, line);
+            i = 1;
+        };
 
-        r = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id"));
+    };
 
-        r = save_file_ply(app, filename);
+    fclose(f);
 
-        if(app->playlist.path)
-            g_free(app->playlist.path);
-        if((app->playlist.path = filename))
+    /* add loaded items to playlist */
+    if(count)
+    {
+        pthread_mutex_lock(&app->playlist.lock);
+        for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
         {
-            char* e = strrchr(app->playlist.path, '/');
-            if(e) *e = 0;
-        }
+//            omnplay_library_normalize_item(app, &items[i]);
+            app->playlist.item[app->playlist.count++] = items[i];
+        };
+        app->playlist.ver_curr++;
+        pthread_mutex_unlock(&app->playlist.lock);
     }
 
-    gtk_widget_destroy (dialog);
+    /* free items */
+    free(line);
+    free(items);
 
+    return 0;
 };
 
-void omnplay_playlist_draw(omnplay_instance_t* app)
+static int playlist_save_plt(instance_t* app, char* filename, char* err_buf, int err_len)
 {
     int i;
-    int* sels;
-    char tc1[12], tc2[12];
-    GtkListStore *list_store;
-    GtkTreeIter iter;
-
-    sels = omnplay_selected_idxs_playlist(app);
-
-    list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
-    gtk_list_store_clear(list_store);
+    FILE* f;
+    char in[12], dur[12];
 
-    pthread_mutex_lock(&app->playlist.lock);
+    f = fopen(filename, "wt");
 
-    for(i = 0;i < app->playlist.count; i++)
+    if(!f)
     {
-        char ch[3];
+        i = errno;
+        strncpy(err_buf, strerror(i), err_len);
+        return -i;
+    };
 
-        if(PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
-            snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
-        else
-            ch[0] = 0;
-
-        gtk_list_store_append(list_store, &iter);
-
-        gtk_list_store_set(list_store, &iter,
-            0, "",
-            1, app->playlist.block_icons[app->playlist.item[i].type],
-            2, ch,
-            3, app->playlist.item[i].id,
-            4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
-            5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
-            6, app->playlist.item[i].title,
-            7, i,
-            8, (app->playlist.item[i].error != 0),
-            9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
-            -1 );
-    }
+    for(i = 0; i < app->playlist.count; i++)
+        fprintf
+        (
+            f,
+            "%s\n"      // id
+            "\t%s\n"    // title
+            "\t%s\n"    // in
+            "\t%s\n"    // dur
+            "\t%d\n"    // player
+            "\t%d\n",   // type
+
+            app->playlist.item[i].id,
+            app->playlist.item[i].title,
+            frames2tc(app->playlist.item[i].in, 25.0, in),
+            frames2tc(app->playlist.item[i].dur, 25.0, dur),
+            app->playlist.item[i].player + 1,
+            app->playlist.item[i].type + 1024
+        );
 
-    app->playlist.ver_prev = app->playlist.ver_curr;
+    fclose(f);
 
-    if(sels)
-    {
-        GtkTreePath *path;
+    return 0;
+};
 
-        /* select */
-        path = gtk_tree_path_new_from_indices(sels[1], -1);
-        gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
-        gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
-        gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE, 0, 0);
-        gtk_tree_path_free(path);
+static int playlist_load_plx(instance_t* app, char* filename, char* err_buf, int err_len)
+{
+    strncpy(err_buf, "Method not implemented", err_len);
+    return -1;
+};
 
-        free(sels);
-    };
+static int playlist_save_plx(instance_t* app, char* filename, char* err_buf, int err_len)
+{
+    strncpy(err_buf, "Method not implemented", err_len);
+    return -1;
+};
 
-    pthread_mutex_unlock(&app->playlist.lock);
+static struct ui_playlist_io_funcs playlist_io[] =
+{
+    {
+        "Text formatted playlist (*.plt)",
+        "*.plt",
+        playlist_load_plt,
+        playlist_save_plt
+    },
+    {
+        "Xml formatted playlist (*.plx)",
+        "*.plx",
+        playlist_load_plx,
+        playlist_save_plx,
+    },
+    {
+        NULL,
+        NULL,
+        NULL,
+        NULL
+    }
 };
 
-typedef struct omnplay_playlist_draw_item_desc
+void playlist_load(instance_t* app)
 {
-    GtkListStore *list_store;
-    omnplay_instance_t* app;
-    int idx;
-} omnplay_playlist_draw_item_t;
+    ui_playlist_load(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
+};
 
-static gboolean omnplay_playlist_draw_item_proc(
-    GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
+void playlist_save(instance_t* app)
 {
-    int i;
-    char tc1[12], tc2[12];
-    char ch[3];
-    omnplay_playlist_draw_item_t* item = (omnplay_playlist_draw_item_t*)user_data;
-    omnplay_instance_t* app = item->app;
+    ui_playlist_save(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
+};
 
-    gtk_tree_model_get(model, iter, 7, &i, -1);
+void playlist_relink(instance_t* app)
+{
+    int i, cnt;
+    int *list;
 
-    if(i != item->idx) return FALSE;
+    pthread_mutex_lock(&app->playlist.lock);
+    list = playlist_get_selected_items_idx(app, &cnt);
+    if(list)
+    {
+        for(i = 0; i < cnt; i++)
+        {
+            /* check for playing block */
+            if(playlist_idx_cued(app, list[i], NULL))
+                continue;
 
-    if(PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
-        snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
-    else
-        ch[0] = 0;
+            /* relink item */
+            library_relink_item(app, &app->playlist.item[list[i]]);
+        };
 
-    gtk_list_store_set(item->list_store, iter,
-        0, "",
-        1, app->playlist.block_icons[app->playlist.item[i].type],
-        2, ch,
-        3, app->playlist.item[i].id,
-        4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
-        5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
-        6, app->playlist.item[i].title,
-        7, i,
-        8, (app->playlist.item[i].error != 0),
-        9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
-        -1 );
+        free(list);
+    };
+    pthread_mutex_unlock(&app->playlist.lock);
 
-    return TRUE;
+    /* redraw playlist */
+    ui_playlist_draw(app);
 };
 
-void omnplay_playlist_draw_item(omnplay_instance_t* app, int idx)
+void playlist_item_add(instance_t* app, int after)
 {
-    GtkListStore *list_store;
-    omnplay_playlist_draw_item_t item;
+    int idx;
+    playlist_item_t item;
+    playlist_item_type_t t;
 
-    list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
+    /* find insert position */
+    idx = playlist_get_first_selected_item_idx(app);
+    if(idx < 0)
+        idx = 0;
+    else
+        idx += (after)?1:0;
 
-    pthread_mutex_lock(&app->playlist.lock);
+    if(!playlist_insert_check(app, idx, &t))
+        return;
 
-    item.idx = idx;
-    item.app = app;
-    item.list_store = list_store;
-    gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_proc, &item);
+    g_warning("allowed insert into idx=%d\n", idx);
 
-    pthread_mutex_unlock(&app->playlist.lock);
+    /* clear item */
+    memset(&item, 0, sizeof(playlist_item_t));
+    if(ui_playlist_item_dialog(app, &item))
+    {
+        library_normalize_item(app, &item);
+        item.type = t;
+        playlist_insert_items(app, idx, &item, 1);
+    };
 };
 
-static gboolean omnplay_playlist_draw_item_rem_proc(
-    GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
+void playlist_item_edit(instance_t* app)
 {
-    int i;
-    void** args                 = (void**)user_data;
-    GtkListStore *list_store    = (GtkListStore *)args[1];
-    int idx                     = (int)args[2];
-    char* rem                   = (char*)args[3];
+    int idx;
+    playlist_item_t item;
 
-    gtk_tree_model_get(model, iter, 7, &i, -1);
+    /* find insert position */
+    idx = playlist_get_first_selected_item_idx(app);
 
-    if(i != idx) return FALSE;
+    if(idx < 0)
+        return;
+
+    /* check for playing block */
+    if(playlist_idx_cued(app, idx, NULL))
+        return;
 
-    gtk_list_store_set(list_store, iter, 0, rem, -1);
+    item = app->playlist.item[idx];
 
-    return TRUE;
+    if(ui_playlist_item_dialog(app, &item))
+    {
+        library_normalize_item(app, &item);
+        app->playlist.item[idx] = item;
+        ui_playlist_draw_item(app, idx);
+    };
 };
 
-void omnplay_playlist_draw_item_rem(omnplay_instance_t* app, int idx, char* rem)
+void playlist_item_add_from_library(instance_t* app, int after)
 {
-    void* item[4];
-    GtkListStore *list_store;
-
-    list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
+};
 
-    item[0] = (void*)app;
-    item[1] = (void*)list_store;
-    item[2] = (void*)idx;
-    item[3] = (void*)rem;
+void playlist_normalize(instance_t* app)
+{
+    int i;
 
-    gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_rem_proc, item);
+    /* normalize playlist */
+    for(i = 0; i < app->playlist.count; i++)
+        if(library_normalize_item(app, &app->playlist.item[i]))
+            ui_playlist_draw_item(app, i);
 };
-
-#endif