bbda4b815d2e6230a26955453cff55ce59dffe20
[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
43 void library_release(instance_t* app)
44 {
45 mvcp_close(app->library.handle[0]);
46 mvcp_parser_close(app->library.handle[1]);
47 };
48
49
50 static int library_load_node(instance_t* app, GtkTreeStore *tree_store, GtkTreeIter* parent, char* path)
51 {
52 int i;
53 GtkTreeIter iter;
54 mvcp_dir_entry_t entry;
55 mvcp_dir dir = mvcp_dir_init(app->library.handle[0], path);
56
57 for (i = 0; i < mvcp_dir_count(dir); i++)
58 {
59 if(mvcp_ok != mvcp_dir_get(dir, i, &entry))
60 continue;
61
62 g_warning("library_load_node: path=[%s], entry.dur=[%d], entry.full=[%s], entry.name[%s]",
63 path, entry.dir, entry.full, entry.name);
64
65 if(entry.dir)
66 {
67 gtk_tree_store_prepend(tree_store, &iter, parent);
68
69 gtk_tree_store_set(tree_store, &iter,
70 0, app->library.icons[0],
71 1, "<dir>",
72 2, entry.name,
73 3, NULL,
74 4, NULL,
75 5, FALSE,
76 6, "red",
77 -1 );
78
79 library_load_node(app, tree_store, &iter, entry.full);
80 }
81 else
82 {
83 gtk_tree_store_append(tree_store, &iter, parent);
84
85 gtk_tree_store_set(tree_store, &iter,
86 0, app->library.icons[1],
87 1, "<file>",
88 2, entry.name,
89 3, NULL,
90 4, NULL,
91 5, FALSE,
92 6, "red",
93 -1 );
94 };
95 };
96
97 return 0;
98 };
99
100 static int library_init_load(instance_t* app)
101 {
102 int i;
103 char tc[12];
104 GtkTreeStore *tree_store;
105 GtkTreeIter iter;
106
107 tree_store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_tree)));
108 gtk_tree_store_clear(tree_store);
109
110 /*
111 gtk_tree_store_append(tree_store, &iter, NULL);
112
113 gtk_tree_store_set(tree_store, &iter,
114 0, NULL,
115 1, "/",
116 2, "<dir>",
117 3, NULL,
118 4, NULL,
119 5, FALSE,
120 6, "red",
121 -1 );
122 */
123
124 library_load_node(app, tree_store, NULL, "/home/studio/Videos");
125
126 gtk_tree_view_collapse_all(GTK_TREE_VIEW(app->library_tree));
127
128 return 0;
129 };
130
131
132 void library_init(instance_t* app)
133 {
134 /* connect to library */
135 app->library.handle[1] = mvcp_parser_init_remote(app->players.host, 5250);
136 app->library.handle[0] = mvcp_init(app->library.handle[1]);
137 if(mvcp_connect(app->library.handle[0]) != mvcp_ok)
138 {
139 g_warning("library_init: failed to connect to server %s", app->players.host);
140 return;
141 };
142
143 app->library.icons[0] = create_pixbuf("Axialis_Team_playlist_open_16x16.png");
144 app->library.icons[1] = create_pixbuf("Axialis_Team_playlist_save_16x16.png");
145
146 library_init_load(app);
147
148 #if 0
149 pthread_mutex_lock(&app->library.lock);
150
151 if(app->library.filename[0])
152 {
153 app->library.count = MAX_LIBRARY_ITEMS;
154 omnplay_library_load_file(app->library.item, &app->library.count, app->library.filename);
155 };
156
157 omnplay_library_sort(app);
158
159 pthread_mutex_unlock(&app->library.lock);
160
161 omnplay_library_draw(app);
162 #endif
163 };
164
165 #if 0
166 playlist_item_t* omnplay_library_find(omnplay_instance_t* app, char* id)
167 {
168 int i;
169 playlist_item_t* item = NULL;
170
171 pthread_mutex_lock(&app->library.lock);
172
173 for(i = 0; i < app->library.count && !item; i++)
174 if(!strcasecmp(id, app->library.item[i].id))
175 item = &app->library.item[i];
176
177 pthread_mutex_unlock(&app->library.lock);
178
179 return item;
180 };
181
182 int omnplay_library_normalize_item(omnplay_instance_t* app, playlist_item_t* item)
183 {
184 int r = 0;
185 playlist_item_t* lib;
186 playlist_item_t prev;
187
188 pthread_mutex_lock(&app->library.lock);
189
190 prev = *item;
191
192 lib = omnplay_library_find(app, item->id);
193
194 item->error = 0;
195
196 if(lib)
197 {
198 if(!item->title[0])
199 {
200 strcpy(item->title, lib->title);
201 r++;
202 };
203
204 if(item->in < lib->in || item->in >= (lib->in + lib->dur))
205 {
206 item->in = lib->in;
207 r++;
208 };
209
210 if(!item->dur || (item->in + item->dur) > (lib->in + lib->dur))
211 {
212 item->dur = lib->in + lib->dur - item->in;
213 r++;
214 };
215
216 if(r)
217 g_warning("omnplay_library_normalize_item: [%s,%d,%d]->[%s,%d,%d]\n",
218 prev.title, prev.in, prev.dur, item->title, item->in, item->dur);
219 }
220 else
221 {
222 r = 1;
223 item->error = PLAYLIST_ITEM_ERROR_LIB;
224 };
225
226 pthread_mutex_unlock(&app->library.lock);
227
228 return r;
229 };
230
231 int omnplay_library_relink_item(omnplay_instance_t* app, playlist_item_t* item)
232 {
233 int r = 0;
234 playlist_item_t* lib;
235
236 pthread_mutex_lock(&app->library.lock);
237
238 lib = omnplay_library_find(app, item->id);
239
240 item->error = 0;
241
242 if(lib)
243 {
244 r = 1;
245 strcpy(item->title, lib->title);
246 item->dur = lib->dur;
247 item->in = lib->in;
248 }
249 else
250 {
251 r = 1;
252 item->error = PLAYLIST_ITEM_ERROR_LIB;
253 };
254
255 pthread_mutex_unlock(&app->library.lock);
256
257 return r;
258 };
259
260 void omnplay_library_sort(omnplay_instance_t* app)
261 {
262 int i, j, m;
263 playlist_item_t item;
264
265 for(i = 0; i < app->library.count; i++)
266 {
267 /* find max */
268 for(j = i + 1, m = i; j < app->library.count; j++)
269 if(strcasecmp(app->library.item[j].id, app->library.item[m].id) < 0)
270 m = j;
271
272 if(m != i)
273 {
274 item = app->library.item[i];
275 app->library.item[i] = app->library.item[m];
276 app->library.item[m] = item;
277 };
278 };
279 };
280
281 int omnplay_library_load_file(playlist_item_t* items, int *pcount, char* filename)
282 {
283 int i, c = 0, r = 0;
284 FILE* f;
285 char *l;
286 int limit = *pcount;
287 playlist_item_t item;
288
289 /* allocate space for strings and items */
290 l = malloc(PATH_MAX);
291
292 *pcount = 0;
293
294 /* open and process file */
295 if((f = fopen(filename, "rt")))
296 {
297 while(!feof(f) && c < limit)
298 {
299 char *s, *sp_r, *sp_b;
300
301 /* load string */
302 memset(l, 0, PATH_MAX);
303 fgets(l, PATH_MAX, f);
304
305 /* remove newlines */
306 if( (s = strchr(l, '\n')) ) *s = 0;
307 if( (s = strchr(l, '\r')) ) *s = 0;
308
309 /* check for empty line */
310 if(l[0] && l[0] != '#' && l[0] != '|')
311 {
312 memset(&item, 0, sizeof(playlist_item_t));
313
314 for(i = 0, sp_b = l; (NULL != (sp_r = strtok(sp_b, "\t"))); i++, sp_b = NULL)
315 {
316 switch(i)
317 {
318 case 0: strncpy(item.id, sp_r, PATH_MAX); break;
319 case 1: tc2frames(sp_r, 25.0, &item.in); break;
320 case 2: tc2frames(sp_r, 25.0, &item.dur); break;
321 case 3: strncpy(item.title, sp_r, PATH_MAX); break;
322 };
323 };
324
325 /* insert item */
326 items[c++] = item;
327 }
328 else
329 g_warning("omnplay_library_load_file: ignored line [%s]\n", l);
330 }
331
332 fclose(f);
333 }
334 else
335 r = -1;
336
337 /* free data */
338 free(l);
339
340 *pcount = c;
341
342 g_warning("omnplay_library_load_file: loaded [%d] items from [%s] file, limit [%d]\n", c, filename, limit);
343
344 return r;
345 };
346
347
348 static void omnplay_library_save_file(playlist_item_t* item, int count, char* filename)
349 {
350 int i;
351 FILE* f;
352
353 if((f = fopen(filename, "wt")))
354 {
355 char tc_in[32], tc_dur[32];
356
357 for(i = 0; i < count; i++)
358 fprintf(f, "%s\t%s\t%s\t%s\n",
359 item[i].id,
360 frames2tc(item[i].in, 25.0, tc_in),
361 frames2tc(item[i].dur, 25.0, tc_dur),
362 item[i].title);
363 fclose(f);
364 g_warning("omnplay_library_save_file: written [%d] lines to file [%s]\n", count, filename);
365 };
366 };
367
368 void omnplay_library_save(omnplay_instance_t* app)
369 {
370 pthread_mutex_lock(&app->library.lock);
371
372 if(app->library.filename[0])
373 omnplay_library_save_file(app->library.item, app->library.count,
374 app->library.filename);
375
376 pthread_mutex_unlock(&app->library.lock);
377 };
378
379 static void omnplay_get_content_cb(omnplay_instance_t* app, playlist_item_t* item, void* data)
380 {
381 if(!(app->library.id_display_idx % app->library.id_display_rate))
382 omnplay_set_status(app, item->id);
383 app->library.id_display_idx++;
384 };
385
386 static void* omnplay_library_refresh_proc(void* data)
387 {
388 omnplay_instance_t* app = (omnplay_instance_t*)data;
389 int count, i;
390 playlist_item_t* items;
391
392 gdk_threads_enter();
393 gtk_widget_set_sensitive(app->window, FALSE);
394 gdk_flush();
395 gdk_threads_leave();
396
397 omnplay_set_status(app, "Updating library...");
398
399 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * MAX_LIBRARY_ITEMS);
400
401 count = omnplay_get_content(app, items, MAX_LIBRARY_ITEMS, omnplay_get_content_cb, NULL);
402
403 if(count > 0)
404 {
405 omnplay_set_status(app, "Quering whois...");
406
407 if(app->library.whois[0])
408 omnplay_whois_list(app, items, &count);
409
410 omnplay_set_status(app, "Setting library...");
411
412 pthread_mutex_lock(&app->library.lock);
413
414 for(i = 0; i < count; i++)
415 app->library.item[i] = items[i];
416
417 app->library.count = count;
418
419 omnplay_library_sort(app);
420
421 pthread_mutex_unlock(&app->library.lock);
422
423 gdk_threads_enter();
424 omnplay_library_draw(app);
425 gdk_flush();
426 gdk_threads_leave();
427 };
428
429 omnplay_set_status(app, "Normalizing playlist...");
430
431 free(items);
432
433 gdk_threads_enter();
434 omnplay_playlist_normalize(app);
435 gdk_flush();
436 gdk_threads_leave();
437
438 omnplay_set_status(app, "");
439
440 gdk_threads_enter();
441 gtk_widget_set_sensitive(app->window, TRUE);
442 gdk_flush();
443 gdk_threads_leave();
444
445
446 return NULL;
447 };
448
449 void omnplay_library_refresh(omnplay_instance_t* app)
450 {
451 if(app->library.refresh_thread)
452 g_thread_join(app->library.refresh_thread);
453
454 app->library.refresh_thread = g_thread_create(
455 omnplay_library_refresh_proc, app, TRUE, NULL);
456 };
457
458 void omnplay_library_draw(omnplay_instance_t* app)
459 {
460 int i;
461 char tc[12];
462 GtkListStore *list_store;
463 GtkTreeIter iter;
464
465 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_grid)));
466 gtk_list_store_clear(list_store);
467
468 pthread_mutex_lock(&app->library.lock);
469
470 for(i = 0;i < app->library.count; i++)
471 {
472 gtk_list_store_append(list_store, &iter);
473
474 gtk_list_store_set(list_store, &iter,
475 0, app->library.item[i].id,
476 1, frames2tc(app->library.item[i].dur, 25.0, tc),
477 2, app->library.item[i].title,
478 3, i,
479 4, FALSE,
480 5, "red",
481 -1 );
482 }
483
484 pthread_mutex_unlock(&app->library.lock);
485 };
486
487 static void get_selected_idx_library_proc(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
488 {
489 int idx, *list = (int*)data;
490 gtk_tree_model_get(model, iter, 3, &idx, -1);
491 list[list[0] + 1] = idx;
492 list[0] = list[0] + 1;
493 };
494
495 static int* get_selected_idx_library(omnplay_instance_t* app)
496 {
497 int* list = NULL;
498 GtkTreeSelection *selection;
499
500 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->library_grid));
501 if(selection)
502 {
503 list = (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS + 1));
504 memset(list, 0, sizeof(int) * (MAX_LIBRARY_ITEMS + 1));
505
506 gtk_tree_selection_selected_foreach(
507 selection,
508 get_selected_idx_library_proc,
509 list);
510
511 if(!list[0])
512 {
513 free(list);
514 list = NULL;
515 };
516 };
517
518 return list;
519 };
520
521
522 playlist_item_t* omnplay_library_get_selected(omnplay_instance_t* app, int *count)
523 {
524 int* idxs;
525 playlist_item_t* items = NULL;
526
527 pthread_mutex_lock(&app->library.lock);
528
529 *count = 0;
530
531 idxs = get_selected_idx_library(app);
532
533 if(idxs)
534 {
535 int i;
536
537 /* alloc items */
538 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * (idxs[0] + 1));
539
540 /* clear last item */
541 memset(&items[idxs[0]], 0, sizeof(playlist_item_t));
542
543 /* copy items */
544 for(i = 0; i < idxs[0]; i++)
545 items[i] = app->library.item[idxs[i + 1]];
546
547 *count = idxs[0];
548 free(idxs);
549 };
550
551 pthread_mutex_unlock(&app->library.lock);
552
553 return items;
554 };
555
556 void omnplay_library_search(omnplay_instance_t* app, int next)
557 {
558 int idx = 0, i;
559 int* idxs;
560 const char *search;
561 GtkTreePath* path;
562
563 pthread_mutex_lock(&app->library.lock);
564
565 idxs = get_selected_idx_library(app);
566 if(idxs) idx = idxs[1];
567 free(idxs);
568
569 if(!next) idx = 0;
570 else idx++;
571
572 search = gtk_entry_get_text(GTK_ENTRY(app->library.search));
573
574 if(search[0])
575 {
576 for(i = idx; i < app->library.count; i++)
577 if( strcasestr(app->library.item[i].id, search) ||
578 strcasestr(app->library.item[i].title, search))
579 break;
580
581 if(i < app->library.count)
582 {
583 g_warning("found at pos=%d\n", i);
584
585 /* select */
586 path = gtk_tree_path_new_from_indices(i, -1);
587 gtk_tree_selection_select_path(gtk_tree_view_get_selection(
588 GTK_TREE_VIEW(app->library_grid)), path);
589 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->library_grid), path, NULL, FALSE);
590 gtk_tree_path_free(path);
591 };
592 };
593
594 pthread_mutex_unlock(&app->library.lock);
595 };
596
597 #endif