tries to unlock ui during library reading, still no luck
[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 char msg[PATH_MAX];
146
147 // g_warning("on_library_row_expanded: HERE");
148
149 /* Set busy cursor */
150 cursor = gdk_cursor_new(GDK_WATCH);
151 gdk_window_set_cursor(gtk_widget_get_toplevel(GTK_WIDGET(treeview))->window, cursor);
152 gdk_cursor_unref(cursor);
153 gdk_flush();
154
155 model = gtk_tree_view_get_model(treeview);
156
157 /* save fake item */
158 gtk_tree_model_iter_children(GTK_TREE_MODEL(model), &fake, iter);
159
160 /* request mvcp entry */
161 gtk_tree_model_get(GTK_TREE_MODEL(model), iter,
162 3, &e,
163 -1);
164
165 /* setup root path */
166 if(!e)
167 p = "/";
168 else
169 p = e->full;
170
171 /* read dir */
172 dir = mvcp_dir_init(app->library.handle[0], p);
173 for (i = 0; i < mvcp_dir_count(dir); i++)
174 {
175 mvcp_dir_entry_t dir_entry;
176 mvcp_list_entry_t *list_e = NULL, list_entry;
177
178 if(mvcp_ok != mvcp_dir_get(dir, i, &dir_entry))
179 continue;
180
181 // g_warning("on_library_row_expanded: path=[%s], entry.dur=[%d], entry.full=[%s], entry.name[%s]",
182 // p, entry.dir, entry.full, entry.name);
183
184 e = (mvcp_dir_entry_t*)malloc(sizeof(mvcp_dir_entry_t));
185 *e = dir_entry;
186
187 snprintf(msg, sizeof(msg), "probing [%s]", e->full);
188 ui_set_status(app, msg, 0);
189
190 gdk_threads_leave();
191
192 if(!e->dir && mvcp_ok == mvcp_probe_clip( app->library.handle[0], e->full, &list_entry))
193 {
194 list_e = (mvcp_list_entry_t*)malloc(sizeof(mvcp_list_entry_t));
195 *list_e = list_entry;
196 };
197
198 gdk_threads_enter();
199
200 library_add_item(app, GTK_TREE_STORE(model), iter, e, list_e);
201 };
202
203 snprintf(msg, sizeof(msg), "read [%s] done", p);
204 ui_set_status(app, msg, 0);
205
206 /* restore cursor */
207 gdk_window_set_cursor(gtk_widget_get_toplevel(GTK_WIDGET(treeview))->window, NULL);
208
209 /* delete fake item */
210 gtk_tree_store_remove(GTK_TREE_STORE(model), &fake);
211 };
212
213 static void on_library_row_collapsed
214 (
215 GtkTreeView *treeview,
216 GtkTreeIter *iter,
217 GtkTreePath *path,
218 gpointer user_data
219 )
220 {
221 GtkTreeModel *model;
222 GtkTreeIter child;
223 // g_warning("on_library_row_collapsed: HERE");
224
225 /* delete all items */
226 model = gtk_tree_view_get_model(treeview);
227 while (gtk_tree_model_iter_children(GTK_TREE_MODEL(model), &child, iter))
228 {
229 mvcp_dir_entry_t *e;
230 mvcp_list_entry_t *l;
231
232 /* request mvcp entry */
233 gtk_tree_model_get(GTK_TREE_MODEL(model), &child,
234 3, &e,
235 4, &l,
236 -1);
237
238 /* free entry */
239 if(e) free(e);
240 if(l) free(l);
241
242 gtk_tree_store_remove(GTK_TREE_STORE(model), &child);
243 };
244
245 /* add a fake element */
246 library_add_fake(user_data, GTK_TREE_STORE(model), iter);
247 };
248
249 static void library_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
250 GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
251 {
252 int c;
253 playlist_item_t* items;
254 instance_t* app = (instance_t*)userdata;
255
256 g_warning("library_drag_data_get_cb");
257
258 items = library_get_selected_items(app, &c);
259
260 /* clear item */
261 if(items)
262 {
263 gtk_selection_data_set(selection_data, selection_data->target, 8,
264 (const guchar *)items, sizeof(playlist_item_t) * c);
265 free(items);
266 };
267 };
268
269 static void library_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
270 {
271 g_warning("library_drag_begin_cb");
272 gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
273 };
274
275
276 void library_init(instance_t* app)
277 {
278 /* connect to library */
279 app->library.handle[1] = mvcp_parser_init_remote(app->players.host, app->library.port);
280 app->library.handle[0] = mvcp_init(app->library.handle[1]);
281 if(mvcp_connect(app->library.handle[0]) != mvcp_ok)
282 {
283 g_warning("library_init: failed to connect to server %s:%d",
284 app->players.host, app->library.port);
285 return;
286 };
287
288 /* setup icons */
289 app->library.icons[0] = create_pixbuf("Axialis_Team_playlist_open_16x16.png");
290 app->library.icons[1] = create_pixbuf("Axialis_Team_playlist_save_16x16.png");
291
292 /* load lib */
293 library_init_load(app);
294
295 /* allow drag source */
296 gtk_drag_source_set(app->library_tree, GDK_BUTTON1_MASK,
297 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY));
298
299 /* set handlers */
300 gtk_signal_connect(GTK_OBJECT(app->library_tree), "row-expanded",
301 GTK_SIGNAL_FUNC(on_library_row_expanded), app);
302 gtk_signal_connect(GTK_OBJECT(app->library_tree), "row-collapsed",
303 GTK_SIGNAL_FUNC(on_library_row_collapsed), app);
304 g_signal_connect(GTK_OBJECT(app->library_tree), "drag_data_get",
305 G_CALLBACK(library_drag_data_get_cb), app);
306 g_signal_connect(GTK_OBJECT(app->library_tree), "drag_begin",
307 G_CALLBACK(library_drag_begin_cb), app);
308
309 };
310
311 static void library_get_selected_items_iter
312 (
313 GtkTreeModel *model,
314 GtkTreePath *path,
315 GtkTreeIter *iter,
316 gpointer data
317 )
318 {
319 int l;
320 mvcp_dir_entry_t *dir;
321 mvcp_list_entry_t *list;
322 playlist_item_t** pitems = (playlist_item_t**)data;
323 playlist_item_t* items = *pitems;
324
325 /* request pointers to list and dir entries of library items */
326 gtk_tree_model_get(model, iter,
327 3, &dir,
328 4, &list,
329 -1);
330
331 /* check if defined */
332 if(dir && list)
333 {
334 /* allocate items */
335 if(!items)
336 {
337 items = (playlist_item_t*)malloc(sizeof(playlist_item_t));
338 memset(items, 0, sizeof(playlist_item_t));
339 };
340
341 /* find numbers of items in list */
342 for(l = 0; items[l].id[0]; l++);
343 g_warning("library_get_selected_items_iter: l=%d", l);
344
345 /* realloc items */
346 items = (playlist_item_t*)realloc(items, (l + 2) * sizeof(playlist_item_t));
347
348 /* clean last item */
349 memset(&items[l + 1], 0, sizeof(playlist_item_t));
350
351 /* setup items */
352 memset(&items[l + 0], 0, sizeof(playlist_item_t));
353 strncpy(items[l].title, dir->name, PATH_MAX);
354 strncpy(items[l].id, dir->full, PATH_MAX);
355 items[l].dur = list->size;
356 };
357
358 *pitems = items;
359 };
360
361 playlist_item_t* library_get_selected_items(instance_t* app, int *count)
362 {
363 int l = 0;
364 playlist_item_t* items = NULL;
365
366 GtkTreeSelection *selection;
367
368 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->library_tree));
369 if(selection)
370 {
371 gtk_tree_selection_selected_foreach(
372 selection,
373 library_get_selected_items_iter,
374 &items);
375
376 if(items)
377 for(; items[l].id[0]; l++);
378 };
379
380 *count = l;
381 return items;
382 };
383
384 int library_normalize_item(instance_t* app, playlist_item_t* item)
385 {
386 #if 0
387 int r = 0;
388 playlist_item_t* lib;
389 playlist_item_t prev;
390
391 pthread_mutex_lock(&app->library.lock);
392
393 prev = *item;
394
395 lib = omnplay_library_find(app, item->id);
396
397 item->error = 0;
398
399 if(lib)
400 {
401 if(!item->title[0])
402 {
403 strcpy(item->title, lib->title);
404 r++;
405 };
406
407 if(item->in < lib->in || item->in >= (lib->in + lib->dur))
408 {
409 item->in = lib->in;
410 r++;
411 };
412
413 if(!item->dur || (item->in + item->dur) > (lib->in + lib->dur))
414 {
415 item->dur = lib->in + lib->dur - item->in;
416 r++;
417 };
418
419 if(r)
420 g_warning("omnplay_library_normalize_item: [%s,%d,%d]->[%s,%d,%d]\n",
421 prev.title, prev.in, prev.dur, item->title, item->in, item->dur);
422 }
423 else
424 {
425 r = 1;
426 item->error = PLAYLIST_ITEM_ERROR_LIB;
427 };
428
429 pthread_mutex_unlock(&app->library.lock);
430
431 return r;
432 #else
433 return 0;
434 #endif
435 };
436
437 int library_relink_item(instance_t* app, playlist_item_t* item)
438 {
439 #if 0
440 int r = 0;
441 playlist_item_t* lib;
442
443 pthread_mutex_lock(&app->library.lock);
444
445 lib = omnplay_library_find(app, item->id);
446
447 item->error = 0;
448
449 if(lib)
450 {
451 r = 1;
452 strcpy(item->title, lib->title);
453 item->dur = lib->dur;
454 item->in = lib->in;
455 }
456 else
457 {
458 r = 1;
459 item->error = PLAYLIST_ITEM_ERROR_LIB;
460 };
461
462 pthread_mutex_unlock(&app->library.lock);
463
464 return r;
465 #else
466 return 0;
467 #endif
468 };