#include <gdk/gdkkeysyms.h>
#include <pthread.h>
+#include <errno.h>
+
#include "playlist.h"
#include "ui.h"
#include "timecode.h"
pthread_mutex_unlock(&app->playlist.lock);
};
-
-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)
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;
-};
-
-void playlist_load(instance_t* app)
-{
- int r;
- GtkWidget *dialog;
- GtkFileFilter *filter;
-
- 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);
-
- gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
- (app->playlist.path)?app->playlist.path:getenv("HOME"));
-
- 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);
-
- r = gtk_dialog_run(GTK_DIALOG(dialog));
-
- if(r == GTK_RESPONSE_ACCEPT)
- {
- char *filename;
-
- filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
- r = playlist_load_file_ply(app, filename);
-
- if(r)
- ui_playlist_draw(app);
-
- if(app->playlist.path)
- g_free(app->playlist.path);
- if((app->playlist.path = filename))
- {
- char* e = strrchr(app->playlist.path, '/');
- if(e) *e = 0;
- }
- }
-
- gtk_widget_destroy (dialog);
+ 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);
+ 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
+ );
+
+ fclose(f);
return 0;
};
-void playlist_save(instance_t* app)
+static int playlist_load_plx(instance_t* app, char* filename, char* err_buf, int err_len)
{
- int r;
- GtkWidget *dialog;
- GtkFileFilter *filter;
-
- 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);
-
- gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
-
- gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
- (app->playlist.path)?app->playlist.path:getenv("HOME"));
-
- 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));
-
- r = gtk_dialog_run(GTK_DIALOG(dialog));
-
- if(r == GTK_RESPONSE_ACCEPT)
- {
- char *filename;
-
- filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-
- r = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id"));
+ strncpy(err_buf, "Method not implemented", err_len);
+ return -1;
+};
- r = playlist_save_file_ply(app, 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;
+};
- if(app->playlist.path)
- g_free(app->playlist.path);
- if((app->playlist.path = filename))
- {
- char* e = strrchr(app->playlist.path, '/');
- if(e) *e = 0;
- }
+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
}
+};
- gtk_widget_destroy (dialog);
+void playlist_load(instance_t* app)
+{
+ ui_playlist_load(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
};
+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)
{
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)