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