probe clips on server
[melted_gui] / src / library.c
1 /*
2 * library.c -- GTK+ 2 melted gui
3 * Copyright (C) 2012 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 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <pthread.h>
34 #include <string.h>
35
36 #include <mvcp/mvcp.h>
37 #include <mvcp/mvcp_remote.h>
38
39 #include "instance.h"
40 #include "ui.h"
41 #include "timecode.h"
42 #include "support.h"
43
44 void library_release(instance_t* app)
45 {
46 mvcp_close(app->library.handle[0]);
47 mvcp_parser_close(app->library.handle[1]);
48 };
49
50 static void library_add_fake(instance_t* app, GtkTreeStore *tree_store, GtkTreeIter* parent)
51 {
52 GtkTreeIter iter;
53 gtk_tree_store_append(tree_store, &iter, parent);
54 gtk_tree_store_set(tree_store, &iter, -1);
55 };
56
57 static int library_init_load(instance_t* app)
58 {
59 GtkTreeIter iter;
60 GtkTreeStore *tree_store;
61
62 tree_store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_tree)));
63 gtk_tree_store_clear(tree_store);
64
65 gtk_tree_store_append(tree_store, &iter, NULL);
66 gtk_tree_store_set(tree_store, &iter,
67 0, app->library.icons[0],
68 1, "<dir>>",
69 2, "LIBRARY",
70 3, NULL,
71 4, NULL,
72 5, FALSE,
73 6, "red",
74 -1 );
75 library_add_fake(app, tree_store, &iter);
76
77 gtk_tree_view_collapse_all(GTK_TREE_VIEW(app->library_tree));
78
79 return 0;
80 };
81
82 static void library_add_item(instance_t* app, GtkTreeStore *treestore, GtkTreeIter *iter,
83 mvcp_dir_entry e, mvcp_list_entry p)
84 {
85 GtkTreeIter this, child;
86
87 if(e->dir)
88 {
89 gtk_tree_store_prepend(treestore, &this, iter);
90
91 gtk_tree_store_set(treestore, &this,
92 0, app->library.icons[0],
93 1, "<dir>",
94 2, e->name,
95 3, e,
96 4, NULL,
97 5, FALSE,
98 6, "red",
99 -1 );
100
101 gtk_tree_store_append(treestore, &child, &this);
102 gtk_tree_store_set(treestore, &child, -1);
103 }
104 else
105 {
106 char dur[32];
107
108 if(p)
109 frames2tc(p->size, p->fps, dur);
110 else
111 strcpy(dur, "<file>");
112
113 gtk_tree_store_append(treestore, &this, iter);
114
115 gtk_tree_store_set(treestore, &this,
116 0, app->library.icons[1],
117 1, dur,
118 2, e->name,
119 3, e,
120 4, p,
121 5, FALSE,
122 6, "red",
123 -1 );
124 };
125 };
126
127 static void on_library_row_expanded
128 (
129 GtkTreeView *treeview,
130 GtkTreeIter *iter,
131 GtkTreePath *path,
132 gpointer user_data
133 )
134 {
135 int i;
136 char* p;
137 GtkTreeIter fake;
138 GtkTreeModel *model;
139 GdkCursor* cursor;
140 mvcp_dir dir;
141 mvcp_dir_entry_t *e;
142 instance_t* app = (instance_t*)user_data;
143
144 // g_warning("on_library_row_expanded: HERE");
145
146 /* Set busy cursor */
147 cursor = gdk_cursor_new(GDK_WATCH);
148 gdk_window_set_cursor(gtk_widget_get_toplevel(GTK_WIDGET(treeview))->window, cursor);
149 gdk_cursor_unref(cursor);
150 gdk_flush();
151
152 model = gtk_tree_view_get_model(treeview);
153
154 /* save fake item */
155 gtk_tree_model_iter_children(GTK_TREE_MODEL(model), &fake, iter);
156
157 /* request mvcp entry */
158 gtk_tree_model_get(GTK_TREE_MODEL(model), iter,
159 3, &e,
160 -1);
161
162 /* setup root path */
163 if(!e)
164 p = "/";
165 else
166 p = e->full;
167
168 /* read dir */
169 dir = mvcp_dir_init(app->library.handle[0], p);
170 for (i = 0; i < mvcp_dir_count(dir); i++)
171 {
172 mvcp_dir_entry_t dir_entry;
173 mvcp_list_entry_t *list_e = NULL, list_entry;
174
175 if(mvcp_ok != mvcp_dir_get(dir, i, &dir_entry))
176 continue;
177
178 // g_warning("on_library_row_expanded: path=[%s], entry.dur=[%d], entry.full=[%s], entry.name[%s]",
179 // p, entry.dir, entry.full, entry.name);
180
181 e = (mvcp_dir_entry_t*)malloc(sizeof(mvcp_dir_entry_t));
182 *e = dir_entry;
183
184 if(!e->dir && mvcp_ok == mvcp_probe_clip( app->library.handle[0], e->full, &list_entry))
185 {
186 list_e = (mvcp_list_entry_t*)malloc(sizeof(mvcp_list_entry_t));
187 *list_e = list_entry;
188 };
189
190 library_add_item(app, GTK_TREE_STORE(model), iter, e, list_e);
191 };
192
193 /* restore cursor */
194 gdk_window_set_cursor(gtk_widget_get_toplevel(GTK_WIDGET(treeview))->window, NULL);
195
196 /* delete fake item */
197 gtk_tree_store_remove(GTK_TREE_STORE(model), &fake);
198 };
199
200 static void on_library_row_collapsed
201 (
202 GtkTreeView *treeview,
203 GtkTreeIter *iter,
204 GtkTreePath *path,
205 gpointer user_data
206 )
207 {
208 GtkTreeModel *model;
209 GtkTreeIter child;
210 // g_warning("on_library_row_collapsed: HERE");
211
212 /* delete all items */
213 model = gtk_tree_view_get_model(treeview);
214 while (gtk_tree_model_iter_children(GTK_TREE_MODEL(model), &child, iter))
215 {
216 mvcp_dir_entry_t *e;
217 mvcp_list_entry_t *l;
218
219 /* request mvcp entry */
220 gtk_tree_model_get(GTK_TREE_MODEL(model), &child,
221 3, &e,
222 4, &l,
223 -1);
224
225 /* free entry */
226 if(e) free(e);
227 if(l) free(l);
228
229 gtk_tree_store_remove(GTK_TREE_STORE(model), &child);
230 };
231
232 /* add a fake element */
233 library_add_fake(user_data, GTK_TREE_STORE(model), iter);
234 };
235
236 void library_init(instance_t* app)
237 {
238 /* connect to library */
239 app->library.handle[1] = mvcp_parser_init_remote(app->players.host, 5250);
240 app->library.handle[0] = mvcp_init(app->library.handle[1]);
241 if(mvcp_connect(app->library.handle[0]) != mvcp_ok)
242 {
243 g_warning("library_init: failed to connect to server %s", app->players.host);
244 return;
245 };
246
247 /* setup icons */
248 app->library.icons[0] = create_pixbuf("Axialis_Team_playlist_open_16x16.png");
249 app->library.icons[1] = create_pixbuf("Axialis_Team_playlist_save_16x16.png");
250
251 /* load lib */
252 library_init_load(app);
253
254 /* set handlers */
255 gtk_signal_connect(GTK_OBJECT(app->library_tree), "row-expanded",
256 GTK_SIGNAL_FUNC(on_library_row_expanded), app);
257 gtk_signal_connect(GTK_OBJECT(app->library_tree), "row-collapsed",
258 GTK_SIGNAL_FUNC(on_library_row_collapsed), app);
259
260 #if 0
261 pthread_mutex_lock(&app->library.lock);
262
263 if(app->library.filename[0])
264 {
265 app->library.count = MAX_LIBRARY_ITEMS;
266 omnplay_library_load_file(app->library.item, &app->library.count, app->library.filename);
267 };
268
269 omnplay_library_sort(app);
270
271 pthread_mutex_unlock(&app->library.lock);
272
273 omnplay_library_draw(app);
274 #endif
275 };
276
277 #if 0
278 playlist_item_t* omnplay_library_find(omnplay_instance_t* app, char* id)
279 {
280 int i;
281 playlist_item_t* item = NULL;
282
283 pthread_mutex_lock(&app->library.lock);
284
285 for(i = 0; i < app->library.count && !item; i++)
286 if(!strcasecmp(id, app->library.item[i].id))
287 item = &app->library.item[i];
288
289 pthread_mutex_unlock(&app->library.lock);
290
291 return item;
292 };
293
294 int omnplay_library_normalize_item(omnplay_instance_t* app, playlist_item_t* item)
295 {
296 int r = 0;
297 playlist_item_t* lib;
298 playlist_item_t prev;
299
300 pthread_mutex_lock(&app->library.lock);
301
302 prev = *item;
303
304 lib = omnplay_library_find(app, item->id);
305
306 item->error = 0;
307
308 if(lib)
309 {
310 if(!item->title[0])
311 {
312 strcpy(item->title, lib->title);
313 r++;
314 };
315
316 if(item->in < lib->in || item->in >= (lib->in + lib->dur))
317 {
318 item->in = lib->in;
319 r++;
320 };
321
322 if(!item->dur || (item->in + item->dur) > (lib->in + lib->dur))
323 {
324 item->dur = lib->in + lib->dur - item->in;
325 r++;
326 };
327
328 if(r)
329 g_warning("omnplay_library_normalize_item: [%s,%d,%d]->[%s,%d,%d]\n",
330 prev.title, prev.in, prev.dur, item->title, item->in, item->dur);
331 }
332 else
333 {
334 r = 1;
335 item->error = PLAYLIST_ITEM_ERROR_LIB;
336 };
337
338 pthread_mutex_unlock(&app->library.lock);
339
340 return r;
341 };
342
343 int omnplay_library_relink_item(omnplay_instance_t* app, playlist_item_t* item)
344 {
345 int r = 0;
346 playlist_item_t* lib;
347
348 pthread_mutex_lock(&app->library.lock);
349
350 lib = omnplay_library_find(app, item->id);
351
352 item->error = 0;
353
354 if(lib)
355 {
356 r = 1;
357 strcpy(item->title, lib->title);
358 item->dur = lib->dur;
359 item->in = lib->in;
360 }
361 else
362 {
363 r = 1;
364 item->error = PLAYLIST_ITEM_ERROR_LIB;
365 };
366
367 pthread_mutex_unlock(&app->library.lock);
368
369 return r;
370 };
371
372 void omnplay_library_sort(omnplay_instance_t* app)
373 {
374 int i, j, m;
375 playlist_item_t item;
376
377 for(i = 0; i < app->library.count; i++)
378 {
379 /* find max */
380 for(j = i + 1, m = i; j < app->library.count; j++)
381 if(strcasecmp(app->library.item[j].id, app->library.item[m].id) < 0)
382 m = j;
383
384 if(m != i)
385 {
386 item = app->library.item[i];
387 app->library.item[i] = app->library.item[m];
388 app->library.item[m] = item;
389 };
390 };
391 };
392
393 int omnplay_library_load_file(playlist_item_t* items, int *pcount, char* filename)
394 {
395 int i, c = 0, r = 0;
396 FILE* f;
397 char *l;
398 int limit = *pcount;
399 playlist_item_t item;
400
401 /* allocate space for strings and items */
402 l = malloc(PATH_MAX);
403
404 *pcount = 0;
405
406 /* open and process file */
407 if((f = fopen(filename, "rt")))
408 {
409 while(!feof(f) && c < limit)
410 {
411 char *s, *sp_r, *sp_b;
412
413 /* load string */
414 memset(l, 0, PATH_MAX);
415 fgets(l, PATH_MAX, f);
416
417 /* remove newlines */
418 if( (s = strchr(l, '\n')) ) *s = 0;
419 if( (s = strchr(l, '\r')) ) *s = 0;
420
421 /* check for empty line */
422 if(l[0] && l[0] != '#' && l[0] != '|')
423 {
424 memset(&item, 0, sizeof(playlist_item_t));
425
426 for(i = 0, sp_b = l; (NULL != (sp_r = strtok(sp_b, "\t"))); i++, sp_b = NULL)
427 {
428 switch(i)
429 {
430 case 0: strncpy(item.id, sp_r, PATH_MAX); break;
431 case 1: tc2frames(sp_r, 25.0, &item.in); break;
432 case 2: tc2frames(sp_r, 25.0, &item.dur); break;
433 case 3: strncpy(item.title, sp_r, PATH_MAX); break;
434 };
435 };
436
437 /* insert item */
438 items[c++] = item;
439 }
440 else
441 g_warning("omnplay_library_load_file: ignored line [%s]\n", l);
442 }
443
444 fclose(f);
445 }
446 else
447 r = -1;
448
449 /* free data */
450 free(l);
451
452 *pcount = c;
453
454 g_warning("omnplay_library_load_file: loaded [%d] items from [%s] file, limit [%d]\n", c, filename, limit);
455
456 return r;
457 };
458
459
460 static void omnplay_library_save_file(playlist_item_t* item, int count, char* filename)
461 {
462 int i;
463 FILE* f;
464
465 if((f = fopen(filename, "wt")))
466 {
467 char tc_in[32], tc_dur[32];
468
469 for(i = 0; i < count; i++)
470 fprintf(f, "%s\t%s\t%s\t%s\n",
471 item[i].id,
472 frames2tc(item[i].in, 25.0, tc_in),
473 frames2tc(item[i].dur, 25.0, tc_dur),
474 item[i].title);
475 fclose(f);
476 g_warning("omnplay_library_save_file: written [%d] lines to file [%s]\n", count, filename);
477 };
478 };
479
480 void omnplay_library_save(omnplay_instance_t* app)
481 {
482 pthread_mutex_lock(&app->library.lock);
483
484 if(app->library.filename[0])
485 omnplay_library_save_file(app->library.item, app->library.count,
486 app->library.filename);
487
488 pthread_mutex_unlock(&app->library.lock);
489 };
490
491 static void omnplay_get_content_cb(omnplay_instance_t* app, playlist_item_t* item, void* data)
492 {
493 if(!(app->library.id_display_idx % app->library.id_display_rate))
494 omnplay_set_status(app, item->id);
495 app->library.id_display_idx++;
496 };
497
498 static void* omnplay_library_refresh_proc(void* data)
499 {
500 omnplay_instance_t* app = (omnplay_instance_t*)data;
501 int count, i;
502 playlist_item_t* items;
503
504 gdk_threads_enter();
505 gtk_widget_set_sensitive(app->window, FALSE);
506 gdk_flush();
507 gdk_threads_leave();
508
509 omnplay_set_status(app, "Updating library...");
510
511 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * MAX_LIBRARY_ITEMS);
512
513 count = omnplay_get_content(app, items, MAX_LIBRARY_ITEMS, omnplay_get_content_cb, NULL);
514
515 if(count > 0)
516 {
517 omnplay_set_status(app, "Quering whois...");
518
519 if(app->library.whois[0])
520 omnplay_whois_list(app, items, &count);
521
522 omnplay_set_status(app, "Setting library...");
523
524 pthread_mutex_lock(&app->library.lock);
525
526 for(i = 0; i < count; i++)
527 app->library.item[i] = items[i];
528
529 app->library.count = count;
530
531 omnplay_library_sort(app);
532
533 pthread_mutex_unlock(&app->library.lock);
534
535 gdk_threads_enter();
536 omnplay_library_draw(app);
537 gdk_flush();
538 gdk_threads_leave();
539 };
540
541 omnplay_set_status(app, "Normalizing playlist...");
542
543 free(items);
544
545 gdk_threads_enter();
546 omnplay_playlist_normalize(app);
547 gdk_flush();
548 gdk_threads_leave();
549
550 omnplay_set_status(app, "");
551
552 gdk_threads_enter();
553 gtk_widget_set_sensitive(app->window, TRUE);
554 gdk_flush();
555 gdk_threads_leave();
556
557
558 return NULL;
559 };
560
561 void omnplay_library_refresh(omnplay_instance_t* app)
562 {
563 if(app->library.refresh_thread)
564 g_thread_join(app->library.refresh_thread);
565
566 app->library.refresh_thread = g_thread_create(
567 omnplay_library_refresh_proc, app, TRUE, NULL);
568 };
569
570 void omnplay_library_draw(omnplay_instance_t* app)
571 {
572 int i;
573 char tc[12];
574 GtkListStore *list_store;
575 GtkTreeIter iter;
576
577 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_grid)));
578 gtk_list_store_clear(list_store);
579
580 pthread_mutex_lock(&app->library.lock);
581
582 for(i = 0;i < app->library.count; i++)
583 {
584 gtk_list_store_append(list_store, &iter);
585
586 gtk_list_store_set(list_store, &iter,
587 0, app->library.item[i].id,
588 1, frames2tc(app->library.item[i].dur, 25.0, tc),
589 2, app->library.item[i].title,
590 3, i,
591 4, FALSE,
592 5, "red",
593 -1 );
594 }
595
596 pthread_mutex_unlock(&app->library.lock);
597 };
598
599 static void get_selected_idx_library_proc(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
600 {
601 int idx, *list = (int*)data;
602 gtk_tree_model_get(model, iter, 3, &idx, -1);
603 list[list[0] + 1] = idx;
604 list[0] = list[0] + 1;
605 };
606
607 static int* get_selected_idx_library(omnplay_instance_t* app)
608 {
609 int* list = NULL;
610 GtkTreeSelection *selection;
611
612 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->library_grid));
613 if(selection)
614 {
615 list = (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS + 1));
616 memset(list, 0, sizeof(int) * (MAX_LIBRARY_ITEMS + 1));
617
618 gtk_tree_selection_selected_foreach(
619 selection,
620 get_selected_idx_library_proc,
621 list);
622
623 if(!list[0])
624 {
625 free(list);
626 list = NULL;
627 };
628 };
629
630 return list;
631 };
632
633
634 playlist_item_t* omnplay_library_get_selected(omnplay_instance_t* app, int *count)
635 {
636 int* idxs;
637 playlist_item_t* items = NULL;
638
639 pthread_mutex_lock(&app->library.lock);
640
641 *count = 0;
642
643 idxs = get_selected_idx_library(app);
644
645 if(idxs)
646 {
647 int i;
648
649 /* alloc items */
650 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * (idxs[0] + 1));
651
652 /* clear last item */
653 memset(&items[idxs[0]], 0, sizeof(playlist_item_t));
654
655 /* copy items */
656 for(i = 0; i < idxs[0]; i++)
657 items[i] = app->library.item[idxs[i + 1]];
658
659 *count = idxs[0];
660 free(idxs);
661 };
662
663 pthread_mutex_unlock(&app->library.lock);
664
665 return items;
666 };
667
668 void omnplay_library_search(omnplay_instance_t* app, int next)
669 {
670 int idx = 0, i;
671 int* idxs;
672 const char *search;
673 GtkTreePath* path;
674
675 pthread_mutex_lock(&app->library.lock);
676
677 idxs = get_selected_idx_library(app);
678 if(idxs) idx = idxs[1];
679 free(idxs);
680
681 if(!next) idx = 0;
682 else idx++;
683
684 search = gtk_entry_get_text(GTK_ENTRY(app->library.search));
685
686 if(search[0])
687 {
688 for(i = idx; i < app->library.count; i++)
689 if( strcasestr(app->library.item[i].id, search) ||
690 strcasestr(app->library.item[i].title, search))
691 break;
692
693 if(i < app->library.count)
694 {
695 g_warning("found at pos=%d\n", i);
696
697 /* select */
698 path = gtk_tree_path_new_from_indices(i, -1);
699 gtk_tree_selection_select_path(gtk_tree_view_get_selection(
700 GTK_TREE_VIEW(app->library_grid)), path);
701 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->library_grid), path, NULL, FALSE);
702 gtk_tree_path_free(path);
703 };
704 };
705
706 pthread_mutex_unlock(&app->library.lock);
707 };
708
709 #endif