From cd24eeef6c4d1ad70ff4882ba9566e27d4dc8e0f Mon Sep 17 00:00:00 2001 From: Maksym Veremeyenko Date: Mon, 9 Jul 2012 07:07:44 +0300 Subject: [PATCH] implement saving --- src/playlist.c | 79 +++++++++++++++++++++++++++++++------------------------ src/ui.c | 53 ++++++++++++++++++++++++++++++++++--- src/ui.h | 4 +- 3 files changed, 94 insertions(+), 42 deletions(-) diff --git a/src/playlist.c b/src/playlist.c index 287d964..7abcc47 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -28,6 +28,8 @@ #include #include +#include + #include "playlist.h" #include "ui.h" #include "timecode.h" @@ -803,54 +805,62 @@ static int playlist_load_file_ply(instance_t* app, char* filename) static int playlist_save_file_ply(instance_t* app, char* filename) { +}; +#endif + +static int playlist_load_plt(instance_t* app, char* filename, char* err_buf, int err_len) +{ + return 0; +}; + +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 +868,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 +896,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; diff --git a/src/ui.c b/src/ui.c index cf5168f..2d434d3 100644 --- a/src/ui.c +++ b/src/ui.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -993,12 +994,29 @@ void ui_set_status(instance_t* app, char* str) gdk_threads_leave(); }; +void ui_show_error(GtkWidget *window, char* title, char* message) +{ + GtkWidget *dialog; + dialog = gtk_message_dialog_new + ( + GTK_WINDOW(window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, + message + ); + + gtk_window_set_title(GTK_WINDOW(dialog), title); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); +}; + int ui_playlist_load(instance_t* app, char* path, struct ui_playlist_io_funcs* procs) { int r, i; - GtkWidget *dialog; GtkFileFilter *filter; + char error_message[1024]; dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW (app->window), @@ -1022,13 +1040,25 @@ int ui_playlist_load(instance_t* app, char* path, struct ui_playlist_io_funcs* p if(r == GTK_RESPONSE_ACCEPT) { - char *filename; + int l; + char *filename, *filename2; filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); i = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id")); - r = procs[i].load(app, filename); + filename2 = (char*)malloc(PATH_MAX); + strncpy(filename2, filename, PATH_MAX); + l = strlen(filename2); + if(l < 4 || strcasecmp(filename2 + i - 4, procs[i].ext + 1)) + strcat(filename2, procs[i].ext + 1); + + r = procs[i].load(app, filename2, error_message, sizeof(error_message)); + + if(r) + ui_show_error(dialog, "Playlist loading error", error_message); + + free(filename2); ui_playlist_draw(app); @@ -1053,6 +1083,7 @@ int ui_playlist_save(instance_t* app, char* path, struct ui_playlist_io_funcs* p int r, i; GtkWidget *dialog; GtkFileFilter *filter; + char error_message[1024]; dialog = gtk_file_chooser_dialog_new("Save File", GTK_WINDOW (app->window), @@ -1078,13 +1109,25 @@ int ui_playlist_save(instance_t* app, char* path, struct ui_playlist_io_funcs* p if(r == GTK_RESPONSE_ACCEPT) { - char *filename; + int l; + char *filename, *filename2; filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); i = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id")); - r = procs[i].save(app, filename); + filename2 = (char*)malloc(PATH_MAX); + strncpy(filename2, filename, PATH_MAX); + l = strlen(filename2); + if(l < 4 || strcasecmp(filename2 + i - 4, procs[i].ext + 1)) + strcat(filename2, procs[i].ext + 1); + + r = procs[i].save(app, filename2, error_message, sizeof(error_message)); + + if(r) + ui_show_error(dialog, "Playlist saving error", error_message); + + free(filename2); if(app->playlist.path) g_free(app->playlist.path); diff --git a/src/ui.h b/src/ui.h index 8e7353a..d6952c8 100644 --- a/src/ui.h +++ b/src/ui.h @@ -40,8 +40,8 @@ struct ui_playlist_io_funcs { char* title; char* ext; - int (*load)(instance_t* app, char* filename); - int (*save)(instance_t* app, char* filename); + int (*load)(instance_t* app, char* filename, char* err_buf, int err_len); + int (*save)(instance_t* app, char* filename, char* err_buf, int err_len); }; int ui_playlist_load(instance_t* app, char* path, struct ui_playlist_io_funcs* procs); -- 1.7.4.4