2 * library.c -- GTK+ 2 melted gui
3 * Copyright (C) 2012 Maksym Veremeyenko <verem@m1stereo.tv>
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.
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.
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.
32 #include <gdk/gdkkeysyms.h>
36 #include <mvcp/mvcp.h>
37 #include <mvcp/mvcp_remote.h>
43 void library_release(instance_t
* app
)
45 mvcp_close(app
->library
.handle
[0]);
46 mvcp_parser_close(app
->library
.handle
[1]);
50 static int library_load_node(instance_t
* app
, GtkTreeStore
*tree_store
, GtkTreeIter
* parent
, char* path
)
54 mvcp_dir_entry_t entry
;
55 mvcp_dir dir
= mvcp_dir_init(app
->library
.handle
[0], path
);
57 for (i
= 0; i
< mvcp_dir_count(dir
); i
++)
59 if(mvcp_ok
!= mvcp_dir_get(dir
, i
, &entry
))
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
);
67 gtk_tree_store_prepend(tree_store
, &iter
, parent
);
69 gtk_tree_store_set(tree_store
, &iter
,
70 0, app
->library
.icons
[0],
79 library_load_node(app
, tree_store
, &iter
, entry
.full
);
83 gtk_tree_store_append(tree_store
, &iter
, parent
);
85 gtk_tree_store_set(tree_store
, &iter
,
86 0, app
->library
.icons
[1],
100 static int library_init_load(instance_t
* app
)
104 GtkTreeStore
*tree_store
;
107 tree_store
= GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->library_tree
)));
108 gtk_tree_store_clear(tree_store
);
111 gtk_tree_store_append(tree_store, &iter, NULL);
113 gtk_tree_store_set(tree_store, &iter,
124 library_load_node(app
, tree_store
, NULL
, "/home/studio/Videos");
126 gtk_tree_view_collapse_all(GTK_TREE_VIEW(app
->library_tree
));
132 void library_init(instance_t
* app
)
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
)
139 g_warning("library_init: failed to connect to server %s", app
->players
.host
);
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");
146 library_init_load(app
);
149 pthread_mutex_lock(&app
->library
.lock
);
151 if(app
->library
.filename
[0])
153 app
->library
.count
= MAX_LIBRARY_ITEMS
;
154 omnplay_library_load_file(app
->library
.item
, &app
->library
.count
, app
->library
.filename
);
157 omnplay_library_sort(app
);
159 pthread_mutex_unlock(&app
->library
.lock
);
161 omnplay_library_draw(app
);
166 playlist_item_t
* omnplay_library_find(omnplay_instance_t
* app
, char* id
)
169 playlist_item_t
* item
= NULL
;
171 pthread_mutex_lock(&app
->library
.lock
);
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
];
177 pthread_mutex_unlock(&app
->library
.lock
);
182 int omnplay_library_normalize_item(omnplay_instance_t
* app
, playlist_item_t
* item
)
185 playlist_item_t
* lib
;
186 playlist_item_t prev
;
188 pthread_mutex_lock(&app
->library
.lock
);
192 lib
= omnplay_library_find(app
, item
->id
);
200 strcpy(item
->title
, lib
->title
);
204 if(item
->in
< lib
->in
|| item
->in
>= (lib
->in
+ lib
->dur
))
210 if(!item
->dur
|| (item
->in
+ item
->dur
) > (lib
->in
+ lib
->dur
))
212 item
->dur
= lib
->in
+ lib
->dur
- item
->in
;
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
);
223 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
226 pthread_mutex_unlock(&app
->library
.lock
);
231 int omnplay_library_relink_item(omnplay_instance_t
* app
, playlist_item_t
* item
)
234 playlist_item_t
* lib
;
236 pthread_mutex_lock(&app
->library
.lock
);
238 lib
= omnplay_library_find(app
, item
->id
);
245 strcpy(item
->title
, lib
->title
);
246 item
->dur
= lib
->dur
;
252 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
255 pthread_mutex_unlock(&app
->library
.lock
);
260 void omnplay_library_sort(omnplay_instance_t
* app
)
263 playlist_item_t item
;
265 for(i
= 0; i
< app
->library
.count
; i
++)
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)
274 item
= app
->library
.item
[i
];
275 app
->library
.item
[i
] = app
->library
.item
[m
];
276 app
->library
.item
[m
] = item
;
281 int omnplay_library_load_file(playlist_item_t
* items
, int *pcount
, char* filename
)
287 playlist_item_t item
;
289 /* allocate space for strings and items */
290 l
= malloc(PATH_MAX
);
294 /* open and process file */
295 if((f
= fopen(filename
, "rt")))
297 while(!feof(f
) && c
< limit
)
299 char *s
, *sp_r
, *sp_b
;
302 memset(l
, 0, PATH_MAX
);
303 fgets(l
, PATH_MAX
, f
);
305 /* remove newlines */
306 if( (s
= strchr(l
, '\n')) ) *s
= 0;
307 if( (s
= strchr(l
, '\r')) ) *s
= 0;
309 /* check for empty line */
310 if(l
[0] && l
[0] != '#' && l
[0] != '|')
312 memset(&item
, 0, sizeof(playlist_item_t
));
314 for(i
= 0, sp_b
= l
; (NULL
!= (sp_r
= strtok(sp_b
, "\t"))); i
++, sp_b
= NULL
)
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;
329 g_warning("omnplay_library_load_file: ignored line [%s]\n", l
);
342 g_warning("omnplay_library_load_file: loaded [%d] items from [%s] file, limit [%d]\n", c
, filename
, limit
);
348 static void omnplay_library_save_file(playlist_item_t
* item
, int count
, char* filename
)
353 if((f
= fopen(filename
, "wt")))
355 char tc_in
[32], tc_dur
[32];
357 for(i
= 0; i
< count
; i
++)
358 fprintf(f
, "%s\t%s\t%s\t%s\n",
360 frames2tc(item
[i
].in
, 25.0, tc_in
),
361 frames2tc(item
[i
].dur
, 25.0, tc_dur
),
364 g_warning("omnplay_library_save_file: written [%d] lines to file [%s]\n", count
, filename
);
368 void omnplay_library_save(omnplay_instance_t
* app
)
370 pthread_mutex_lock(&app
->library
.lock
);
372 if(app
->library
.filename
[0])
373 omnplay_library_save_file(app
->library
.item
, app
->library
.count
,
374 app
->library
.filename
);
376 pthread_mutex_unlock(&app
->library
.lock
);
379 static void omnplay_get_content_cb(omnplay_instance_t
* app
, playlist_item_t
* item
, void* data
)
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
++;
386 static void* omnplay_library_refresh_proc(void* data
)
388 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
390 playlist_item_t
* items
;
393 gtk_widget_set_sensitive(app
->window
, FALSE
);
397 omnplay_set_status(app
, "Updating library...");
399 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * MAX_LIBRARY_ITEMS
);
401 count
= omnplay_get_content(app
, items
, MAX_LIBRARY_ITEMS
, omnplay_get_content_cb
, NULL
);
405 omnplay_set_status(app
, "Quering whois...");
407 if(app
->library
.whois
[0])
408 omnplay_whois_list(app
, items
, &count
);
410 omnplay_set_status(app
, "Setting library...");
412 pthread_mutex_lock(&app
->library
.lock
);
414 for(i
= 0; i
< count
; i
++)
415 app
->library
.item
[i
] = items
[i
];
417 app
->library
.count
= count
;
419 omnplay_library_sort(app
);
421 pthread_mutex_unlock(&app
->library
.lock
);
424 omnplay_library_draw(app
);
429 omnplay_set_status(app
, "Normalizing playlist...");
434 omnplay_playlist_normalize(app
);
438 omnplay_set_status(app
, "");
441 gtk_widget_set_sensitive(app
->window
, TRUE
);
449 void omnplay_library_refresh(omnplay_instance_t
* app
)
451 if(app
->library
.refresh_thread
)
452 g_thread_join(app
->library
.refresh_thread
);
454 app
->library
.refresh_thread
= g_thread_create(
455 omnplay_library_refresh_proc
, app
, TRUE
, NULL
);
458 void omnplay_library_draw(omnplay_instance_t
* app
)
462 GtkListStore
*list_store
;
465 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->library_grid
)));
466 gtk_list_store_clear(list_store
);
468 pthread_mutex_lock(&app
->library
.lock
);
470 for(i
= 0;i
< app
->library
.count
; i
++)
472 gtk_list_store_append(list_store
, &iter
);
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
,
484 pthread_mutex_unlock(&app
->library
.lock
);
487 static void get_selected_idx_library_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
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;
495 static int* get_selected_idx_library(omnplay_instance_t
* app
)
498 GtkTreeSelection
*selection
;
500 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->library_grid
));
503 list
= (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
504 memset(list
, 0, sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
506 gtk_tree_selection_selected_foreach(
508 get_selected_idx_library_proc
,
522 playlist_item_t
* omnplay_library_get_selected(omnplay_instance_t
* app
, int *count
)
525 playlist_item_t
* items
= NULL
;
527 pthread_mutex_lock(&app
->library
.lock
);
531 idxs
= get_selected_idx_library(app
);
538 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * (idxs
[0] + 1));
540 /* clear last item */
541 memset(&items
[idxs
[0]], 0, sizeof(playlist_item_t
));
544 for(i
= 0; i
< idxs
[0]; i
++)
545 items
[i
] = app
->library
.item
[idxs
[i
+ 1]];
551 pthread_mutex_unlock(&app
->library
.lock
);
556 void omnplay_library_search(omnplay_instance_t
* app
, int next
)
563 pthread_mutex_lock(&app
->library
.lock
);
565 idxs
= get_selected_idx_library(app
);
566 if(idxs
) idx
= idxs
[1];
572 search
= gtk_entry_get_text(GTK_ENTRY(app
->library
.search
));
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
))
581 if(i
< app
->library
.count
)
583 g_warning("found at pos=%d\n", i
);
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
);
594 pthread_mutex_unlock(&app
->library
.lock
);