fix playlist closing
[omnplay] / src / playlist.c
1 /*
2 * playlist.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <pthread.h>
30
31 #include "omnplay.h"
32 #include "ui.h"
33 #include "timecode.h"
34
35 #if !HAVE_STRSEP
36 /*
37 https://gitorious.org/mingw-unofficial/msys/source/956185bef3c2dafb0e6f5f9eadd925ec55139f2e:rt/src/winsup/cygwin/strsep.cc
38 */
39 static char * strsep (char **stringp, const char *delim)
40 {
41 register char *s;
42 register const char *spanp;
43 register int c, sc;
44 char *tok;
45 if((s = *stringp) == NULL)
46 return (NULL);
47
48 for(tok = s;;)
49 {
50 c = *s++;
51 spanp = delim;
52 do
53 {
54 if((sc = *spanp++) == c)
55 {
56 if(c == 0)
57 s = NULL;
58 else
59 s[-1] = 0;
60 *stringp = s;
61
62 return (tok);
63 }
64 } while (sc != 0);
65 }
66
67 /* NOTREACHED */
68 };
69
70 #endif
71
72 static int load_file_ply(omnplay_instance_t* app, char* filename)
73 {
74 FILE* f;
75 char *l;
76 int count = 0, i;
77 playlist_item_t* items;
78
79 g_warning("%s:%d filename = [%s]", __FUNCTION__, __LINE__, filename);
80
81 /* open and process file */
82 f = fopen(filename, "rt");
83 if(!f)
84 {
85 g_warning("%s:%d failed to open filename = [%s]", __FUNCTION__, __LINE__, filename);
86 return 0;
87 };
88
89 /* allocate space for strings and items */
90 items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
91 memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
92 l = malloc(PATH_MAX);
93
94 while(!feof(f))
95 {
96 char* s;
97 char *save, *str, *tok;
98 playlist_item_t item;
99
100 /* load string */
101 l[0] = 0;
102 fgets(l, PATH_MAX, f);
103
104 /* remove newlines */
105 while((s = strchr(l, '\n'))) *s = 0;
106 while((s = strchr(l, '\r'))) *s = 0;
107 while((s = strchr(l, '\t'))) *s = 0;
108
109 /* check for empty line */
110 if(!l[0] || l[0] == '#')
111 continue;
112
113 /* process split tokens */
114 memset(&item, 0, sizeof(item));
115 i = 0;
116 str = save = strdup(l);
117 while((tok = strsep(&str, ",")) != NULL)
118 {
119 g_warning("%s:%d tok[%d]=[%s]", __FUNCTION__, __LINE__, i, tok);
120
121 if(0 == i)
122 strncpy(item.id, tok, PATH_MAX);
123 else if(1 == i)
124 item.player = atol(tok) - 1;
125 else if(2 == i)
126 {
127 int b = atol(tok);
128 switch(b)
129 {
130 case 1: item.type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE; break;
131 case 2: item.type = OMNPLAY_PLAYLIST_ITEM_LOOP_BEGIN; break;
132 case 3: item.type = OMNPLAY_PLAYLIST_ITEM_LOOP_BODY; break;
133 case 4: item.type = OMNPLAY_PLAYLIST_ITEM_LOOP_END; break;
134 case 6: item.type = OMNPLAY_PLAYLIST_ITEM_BLOCK_END; break;
135 case 0:
136 if(!count)
137 item.type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
138 else if
139 (
140 items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN
141 ||
142 items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY
143 )
144 item.type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
145 else
146 item.type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
147 break;
148 default:
149 if(b >= 1024)
150 item.type = b - 1024;
151 }
152 }
153 else if(3 == i)
154 tc2frames(tok, 25.0, &item.in);
155 else if(5 == i)
156 tc2frames(tok, 25.0, &item.dur);
157
158 i++;
159 }
160
161 if(i > 5)
162 {
163 g_warning("%s:%d count=[%d] id={%s} in={%d} dur={%d}", __FUNCTION__, __LINE__, count, item.id, item.in, item.dur);
164 items[count] = item;
165 count++;
166 }
167 else
168 g_warning("%s:%d i = %d", __FUNCTION__, __LINE__, i);
169 }
170
171 fclose(f);
172
173 /* add loaded items to playlist */
174 if(count)
175 {
176 pthread_mutex_lock(&app->playlist.lock);
177 for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
178 {
179 omnplay_library_normalize_item(app, &items[i]);
180 app->playlist.item[app->playlist.count++] = items[i];
181 };
182 app->playlist.ver_curr++;
183 pthread_mutex_unlock(&app->playlist.lock);
184 }
185
186 /* free data */
187 free(items);
188 free(l);
189
190 return count;
191 };
192
193 void omnplay_playlist_load(omnplay_instance_t* app)
194 {
195 int r;
196 GtkWidget *dialog;
197 GtkFileFilter *filter;
198
199 dialog = gtk_file_chooser_dialog_new("Open File",
200 GTK_WINDOW (app->window),
201 GTK_FILE_CHOOSER_ACTION_OPEN,
202 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
203 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
204 NULL);
205
206 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
207 (app->playlist.path)?app->playlist.path:getenv("HOME"));
208
209 filter = gtk_file_filter_new();
210 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
211 gtk_file_filter_add_pattern(filter, "*.ply");
212 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
213 filter = gtk_file_filter_new();
214 gtk_file_filter_set_name(filter, "All types (*.*)");
215 gtk_file_filter_add_pattern(filter, "*.*");
216 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
217
218 r = gtk_dialog_run(GTK_DIALOG(dialog));
219
220 if(r == GTK_RESPONSE_ACCEPT)
221 {
222 char *filename;
223
224 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
225
226 r = load_file_ply(app, filename);
227
228 if(r)
229 omnplay_playlist_draw(app);
230
231 if(app->playlist.path)
232 g_free(app->playlist.path);
233 if((app->playlist.path = filename))
234 {
235 char* e = strrchr(app->playlist.path, '/');
236 if(e) *e = 0;
237 }
238 }
239
240 gtk_widget_destroy (dialog);
241 };
242
243 static int save_file_ply(omnplay_instance_t* app, char* filename)
244 {
245 int i;
246 FILE* f;
247 char tc1[12], tc2[12], tc3[12];
248 char* fname = filename;
249
250 filename = (char*)malloc(PATH_MAX);
251 strncpy(filename, fname, PATH_MAX);
252 i = strlen(filename);
253 if(i < 4 || strcasecmp(filename + i - 4, ".ply"))
254 strcat(filename, ".ply");
255
256 if((f = fopen(filename, "wt")))
257 {
258 for(i = 0; i < app->playlist.count; i++)
259 fprintf(f, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
260 app->playlist.item[i].id,
261 app->playlist.item[i].player + 1,
262 app->playlist.item[i].type + 1024,
263 frames2tc(app->playlist.item[i].in, 25.0, tc1),
264 frames2tc(app->playlist.item[i].in + app->playlist.item[i].dur, 25.0, tc2),
265 frames2tc(app->playlist.item[i].dur, 25.0, tc3));
266
267 close(f);
268 };
269
270 free(filename);
271
272 return 0;
273 };
274
275 void omnplay_playlist_save(omnplay_instance_t* app)
276 {
277 int r;
278 GtkWidget *dialog;
279 GtkFileFilter *filter;
280
281 dialog = gtk_file_chooser_dialog_new("Save File",
282 GTK_WINDOW (app->window),
283 GTK_FILE_CHOOSER_ACTION_SAVE,
284 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
285 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
286 NULL);
287
288 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
289
290 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
291 (app->playlist.path)?app->playlist.path:getenv("HOME"));
292
293 filter = gtk_file_filter_new();
294 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
295 gtk_file_filter_add_pattern(filter, "*.ply");
296 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
297 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(0));
298 filter = gtk_file_filter_new();
299 gtk_file_filter_set_name(filter, "Text (*.txt)");
300 gtk_file_filter_add_pattern(filter, "*.*");
301 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
302 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(1));
303
304 r = gtk_dialog_run(GTK_DIALOG(dialog));
305
306 if(r == GTK_RESPONSE_ACCEPT)
307 {
308 char *filename;
309
310 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
311
312 r = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id"));
313
314 r = save_file_ply(app, filename);
315
316 if(app->playlist.path)
317 g_free(app->playlist.path);
318 if((app->playlist.path = filename))
319 {
320 char* e = strrchr(app->playlist.path, '/');
321 if(e) *e = 0;
322 }
323 }
324
325 gtk_widget_destroy (dialog);
326
327 };
328
329 void omnplay_playlist_draw(omnplay_instance_t* app)
330 {
331 int i;
332 int* sels;
333 char tc1[12], tc2[12];
334 GtkListStore *list_store;
335 GtkTreeIter iter;
336
337 sels = omnplay_selected_idxs_playlist(app);
338
339 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
340 gtk_list_store_clear(list_store);
341
342 pthread_mutex_lock(&app->playlist.lock);
343
344 for(i = 0;i < app->playlist.count; i++)
345 {
346 char ch[3];
347
348 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
349 snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
350 else
351 ch[0] = 0;
352
353 gtk_list_store_append(list_store, &iter);
354
355 gtk_list_store_set(list_store, &iter,
356 0, "",
357 1, app->playlist.block_icons[app->playlist.item[i].type],
358 2, ch,
359 3, app->playlist.item[i].id,
360 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
361 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
362 6, app->playlist.item[i].title,
363 7, i,
364 8, (app->playlist.item[i].error != 0),
365 9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
366 -1 );
367 }
368
369 app->playlist.ver_prev = app->playlist.ver_curr;
370
371 if(sels)
372 {
373 GtkTreePath *path;
374
375 /* select */
376 path = gtk_tree_path_new_from_indices(sels[1], -1);
377 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
378 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
379 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE, 0, 0);
380 gtk_tree_path_free(path);
381
382 free(sels);
383 };
384
385 pthread_mutex_unlock(&app->playlist.lock);
386 };
387
388 typedef struct omnplay_playlist_draw_item_desc
389 {
390 GtkListStore *list_store;
391 omnplay_instance_t* app;
392 int idx;
393 } omnplay_playlist_draw_item_t;
394
395 static gboolean omnplay_playlist_draw_item_proc(
396 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
397 {
398 int i;
399 char tc1[12], tc2[12];
400 char ch[3];
401 omnplay_playlist_draw_item_t* item = (omnplay_playlist_draw_item_t*)user_data;
402 omnplay_instance_t* app = item->app;
403
404 gtk_tree_model_get(model, iter, 7, &i, -1);
405
406 if(i != item->idx) return FALSE;
407
408 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
409 snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
410 else
411 ch[0] = 0;
412
413 gtk_list_store_set(item->list_store, iter,
414 0, "",
415 1, app->playlist.block_icons[app->playlist.item[i].type],
416 2, ch,
417 3, app->playlist.item[i].id,
418 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
419 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
420 6, app->playlist.item[i].title,
421 7, i,
422 8, (app->playlist.item[i].error != 0),
423 9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
424 -1 );
425
426 return TRUE;
427 };
428
429 void omnplay_playlist_draw_item(omnplay_instance_t* app, int idx)
430 {
431 GtkListStore *list_store;
432 omnplay_playlist_draw_item_t item;
433
434 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
435
436 pthread_mutex_lock(&app->playlist.lock);
437
438 item.idx = idx;
439 item.app = app;
440 item.list_store = list_store;
441 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_proc, &item);
442
443 pthread_mutex_unlock(&app->playlist.lock);
444 };
445
446 static gboolean omnplay_playlist_draw_item_rem_proc(
447 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
448 {
449 int i;
450 void** args = (void**)user_data;
451 GtkListStore *list_store = (GtkListStore *)args[1];
452 int idx = (int)args[2];
453 char* rem = (char*)args[3];
454
455 gtk_tree_model_get(model, iter, 7, &i, -1);
456
457 if(i != idx) return FALSE;
458
459 gtk_list_store_set(list_store, iter, 0, rem, -1);
460
461 return TRUE;
462 };
463
464 void omnplay_playlist_draw_item_rem(omnplay_instance_t* app, int idx, char* rem)
465 {
466 void* item[4];
467 GtkListStore *list_store;
468
469 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
470
471 item[0] = (void*)app;
472 item[1] = (void*)list_store;
473 item[2] = (void*)idx;
474 item[3] = (void*)rem;
475
476 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_rem_proc, item);
477 };