tries to unlock ui during library reading, still no luck
[melted_gui] / src / playlist.c
index 287d964..da7ce7e 100644 (file)
@@ -28,6 +28,8 @@
 #include <gdk/gdkkeysyms.h>
 #include <pthread.h>
 
+#include <errno.h>
+
 #include "playlist.h"
 #include "ui.h"
 #include "timecode.h"
@@ -679,101 +681,95 @@ void playlist_item_swap(instance_t* app, int dir)
     pthread_mutex_unlock(&app->playlist.lock);
 };
 
-#if 0
-static int playlist_load_file_ply(instance_t* app, char* filename)
+static int playlist_load_plt(instance_t* app, char* filename, char* err_buf, int err_len)
 {
     FILE* f;
-    char *ID, *CH, *B, *IN, *OUT, *DUR, *REST, *l;
-    int count = 0, i;
+    char *line;
+    int count = 0, i = 0;
     playlist_item_t* items;
 
+    f = fopen(filename, "rt");
+
+    if(!f)
+    {
+        i = errno;
+        snprintf(err_buf, err_len, "Failed to open file [%s], error: %s",
+            filename, strerror(i));
+        return -i;
+    };
+
     /* 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)
+    line = malloc(PATH_MAX);
+
+    while(1)
     {
-        while( !feof(f) )
-        {
-            char* s;
+        char* s;
 
-            /* load string */
-            memset(l, 0, PATH_MAX);
-            fgets(l, PATH_MAX, f);
+        if(feof(f))
+            break;
 
-            /* remove newlines */
-            if( (s = strchr(l, '\n')) ) *s = 0;
-            if( (s = strchr(l, '\r')) ) *s = 0;
-            if( (s = strchr(l, '\t')) ) *s = 0;
+        /* load string */
+        memset(line, 0, PATH_MAX);
+        fgets(line, PATH_MAX, f);
 
-            /* 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
+        /* remove newlines */
+        if( (s = strchr(line, '\n')) ) *s = 0;
+        if( (s = strchr(line, '\r')) ) *s = 0;
+
+        /* check for empty line */
+        if(!line[0]  || line[0] == '#')
+            continue;
 
+        /* check item contine */
+        if(line[0] == '\t')
+        {
+            /* line without header */
+            if(!i)
+                continue;
+
+            /* 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;
+        };
 
-        fclose(f);
-    }
+    };
+
+    fclose(f);
 
     /* add loaded items to playlist */
     if(count)
@@ -788,69 +784,61 @@ static int playlist_load_file_ply(instance_t* app, char* filename)
         pthread_mutex_unlock(&app->playlist.lock);
     }
 
-    /* free data */
+    /* free items */
+    free(line);
     free(items);
-    free(ID);
-    free(CH);
-    free(IN);
-    free(OUT);
-    free(DUR);
-    free(REST);
-    free(l);
 
-    return count;
+    return 0;
 };
 
-static int playlist_save_file_ply(instance_t* app, char* filename)
+static int playlist_save_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 in[12], dur[12];
 
-    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, "wt");
 
-    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));
-
-        fclose(f);
+        i = errno;
+        strncpy(err_buf, strerror(i), err_len);
+        return -i;
     };
 
-    free(filename);
-
-    return 0;
-};
-#endif
+    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
+        );
 
-static int playlist_load_plm(instance_t* app, char* filename)
-{
-    return 0;
-};
+    fclose(f);
 
-static int playlist_save_plm(instance_t* app, char* filename)
-{
     return 0;
 };
 
-static int playlist_load_plx(instance_t* app, char* filename)
+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;
 };
 
-static int playlist_save_plx(instance_t* app, char* filename)
+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;
 };
 
@@ -858,9 +846,9 @@ static struct ui_playlist_io_funcs playlist_io[] =
 {
     {
         "Text formatted playlist (*.plt)",
-        "*.plm",
-        playlist_load_plm,
-        playlist_save_plm
+        "*.plt",
+        playlist_load_plt,
+        playlist_save_plt
     },
     {
         "Xml formatted playlist (*.plx)",
@@ -886,7 +874,6 @@ void playlist_save(instance_t* app)
     ui_playlist_save(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
 };
 
-
 void playlist_relink(instance_t* app)
 {
     int i, cnt;
@@ -969,6 +956,34 @@ void playlist_item_edit(instance_t* app)
 
 void playlist_item_add_from_library(instance_t* app, int after)
 {
+    int i, idx, cnt;
+    playlist_item_t *items;
+    playlist_item_type_t t;
+
+    /* find insert position */
+    idx = playlist_get_first_selected_item_idx(app);
+    if(idx < 0)
+        idx = 0;
+    else
+        idx += (after)?1:0;
+
+    if(!playlist_insert_check(app, idx, &t))
+        return;
+
+
+    items = library_get_selected_items(app, &cnt);
+    if(items)
+    {
+        for(i = 0; i < cnt; i++)
+        {
+            items[i].type = t;
+            items[i].error = 0;
+        };
+
+        playlist_insert_items(app, idx, items, cnt);
+
+        free(items);
+    };
 };
 
 void playlist_normalize(instance_t* app)