2 * playlist.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 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>
40 playlist_item_t
* omnplay_library_find(omnplay_instance_t
* app
, char* id
)
43 playlist_item_t
* item
= NULL
;
45 pthread_mutex_lock(&app
->library
.lock
);
47 for(i
= 0; i
< app
->library
.count
&& !item
; i
++)
48 if(!strcasecmp(id
, app
->library
.item
[i
].id
))
49 item
= &app
->library
.item
[i
];
51 pthread_mutex_unlock(&app
->library
.lock
);
56 int omnplay_library_normalize_item(omnplay_instance_t
* app
, playlist_item_t
* item
)
62 pthread_mutex_lock(&app
->library
.lock
);
66 lib
= omnplay_library_find(app
, item
->id
);
74 strcpy(item
->title
, lib
->title
);
78 if(item
->in
< lib
->in
|| item
->in
>= (lib
->in
+ lib
->dur
))
84 if(!item
->dur
|| (item
->in
+ item
->dur
) > (lib
->in
+ lib
->dur
))
91 g_warning("omnplay_library_normalize_item: [%s,%d,%d]->[%s,%d,%d]\n",
92 prev
.title
, prev
.in
, prev
.dur
, item
->title
, item
->in
, item
->dur
);
97 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
100 pthread_mutex_unlock(&app
->library
.lock
);
105 int omnplay_library_relink_item(omnplay_instance_t
* app
, playlist_item_t
* item
)
108 playlist_item_t
* lib
;
110 pthread_mutex_lock(&app
->library
.lock
);
112 lib
= omnplay_library_find(app
, item
->id
);
119 strcpy(item
->title
, lib
->title
);
120 item
->dur
= lib
->dur
;
126 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
129 pthread_mutex_unlock(&app
->library
.lock
);
134 void omnplay_library_sort(omnplay_instance_t
* app
)
137 playlist_item_t item
;
139 for(i
= 0; i
< app
->library
.count
; i
++)
142 for(j
= i
+ 1, m
= i
; j
< app
->library
.count
; j
++)
143 if(strcasecmp(app
->library
.item
[j
].id
, app
->library
.item
[m
].id
) < 0)
148 item
= app
->library
.item
[i
];
149 app
->library
.item
[i
] = app
->library
.item
[m
];
150 app
->library
.item
[m
] = item
;
155 int omnplay_library_load_file(playlist_item_t
* items
, int *pcount
, char* filename
)
161 playlist_item_t item
;
163 /* allocate space for strings and items */
164 l
= malloc(PATH_MAX
);
168 /* open and process file */
169 if((f
= fopen(filename
, "rt")))
171 while(!feof(f
) && c
< limit
)
173 char *s
, *sp_r
, *sp_b
;
176 memset(l
, 0, PATH_MAX
);
177 fgets(l
, PATH_MAX
, f
);
179 /* remove newlines */
180 if( (s
= strchr(l
, '\n')) ) *s
= 0;
181 if( (s
= strchr(l
, '\r')) ) *s
= 0;
183 /* check for empty line */
184 if(l
[0] && l
[0] != '#' && l
[0] != '|')
186 memset(&item
, 0, sizeof(playlist_item_t
));
188 for(i
= 0, sp_b
= l
; (NULL
!= (sp_r
= strtok(sp_b
, "\t"))); i
++, sp_b
= NULL
)
192 case 0: strncpy(item
.id
, sp_r
, PATH_MAX
); break;
193 case 1: tc2frames(sp_r
, 25.0, &item
.in
); break;
194 case 2: tc2frames(sp_r
, 25.0, &item
.dur
); break;
195 case 3: strncpy(item
.title
, sp_r
, PATH_MAX
); break;
203 g_warning("omnplay_library_load_file: ignored line [%s]\n", l
);
216 g_warning("omnplay_library_load_file: loaded [%d] items from [%s] file, limit [%d]\n", c
, filename
, limit
);
221 void omnplay_library_load(omnplay_instance_t
* app
)
223 pthread_mutex_lock(&app
->library
.lock
);
225 if(app
->library
.filename
[0])
227 app
->library
.count
= MAX_LIBRARY_ITEMS
;
228 omnplay_library_load_file(app
->library
.item
, &app
->library
.count
, app
->library
.filename
);
231 omnplay_library_sort(app
);
233 pthread_mutex_unlock(&app
->library
.lock
);
235 omnplay_library_draw(app
);
238 static void omnplay_library_save_file(playlist_item_t
* item
, int count
, char* filename
)
243 if((f
= fopen(filename
, "wt")))
245 char tc_in
[32], tc_dur
[32];
247 for(i
= 0; i
< count
; i
++)
248 fprintf(f
, "%s\t%s\t%s\t%s\n",
250 frames2tc(item
[i
].in
, 25.0, tc_in
),
251 frames2tc(item
[i
].dur
, 25.0, tc_dur
),
254 g_warning("omnplay_library_save_file: written [%d] lines to file [%s]\n", count
, filename
);
258 void omnplay_library_save(omnplay_instance_t
* app
)
260 pthread_mutex_lock(&app
->library
.lock
);
262 if(app
->library
.filename
[0])
263 omnplay_library_save_file(app
->library
.item
, app
->library
.count
,
264 app
->library
.filename
);
266 pthread_mutex_unlock(&app
->library
.lock
);
269 static void omnplay_get_content_cb(omnplay_instance_t
* app
, playlist_item_t
* item
, void* data
)
271 if(!(app
->library
.id_display_idx
% app
->library
.id_display_rate
))
272 omnplay_set_status(app
, item
->id
);
273 app
->library
.id_display_idx
++;
276 static void* omnplay_library_refresh_proc(void* data
)
278 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
280 playlist_item_t
* items
;
283 gtk_widget_set_sensitive(app
->window
, FALSE
);
287 omnplay_set_status(app
, "Updating library...");
289 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * MAX_LIBRARY_ITEMS
);
291 count
= omnplay_get_content(app
, items
, MAX_LIBRARY_ITEMS
, omnplay_get_content_cb
, NULL
);
295 omnplay_set_status(app
, "Quering whois...");
297 if(app
->library
.whois
[0])
298 omnplay_whois_list(app
, items
, &count
);
300 omnplay_set_status(app
, "Setting library...");
302 pthread_mutex_lock(&app
->library
.lock
);
304 for(i
= 0; i
< count
; i
++)
305 app
->library
.item
[i
] = items
[i
];
307 app
->library
.count
= count
;
309 omnplay_library_sort(app
);
311 pthread_mutex_unlock(&app
->library
.lock
);
314 omnplay_library_draw(app
);
319 omnplay_set_status(app
, "Normalizing playlist...");
324 omnplay_playlist_normalize(app
);
328 omnplay_set_status(app
, "");
331 gtk_widget_set_sensitive(app
->window
, TRUE
);
339 void omnplay_library_refresh(omnplay_instance_t
* app
)
341 if(app
->library
.refresh_thread
)
342 g_thread_join(app
->library
.refresh_thread
);
344 app
->library
.refresh_thread
= g_thread_create(
345 omnplay_library_refresh_proc
, app
, TRUE
, NULL
);
348 void omnplay_library_draw(omnplay_instance_t
* app
)
352 GtkListStore
*list_store
;
355 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->library_grid
)));
356 gtk_list_store_clear(list_store
);
358 pthread_mutex_lock(&app
->library
.lock
);
360 for(i
= 0;i
< app
->library
.count
; i
++)
362 gtk_list_store_append(list_store
, &iter
);
364 gtk_list_store_set(list_store
, &iter
,
365 0, app
->library
.item
[i
].id
,
366 1, frames2tc(app
->library
.item
[i
].dur
, 25.0, tc
),
367 2, app
->library
.item
[i
].title
,
374 pthread_mutex_unlock(&app
->library
.lock
);
377 static void get_selected_idx_library_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
379 int idx
, *list
= (int*)data
;
380 gtk_tree_model_get(model
, iter
, 3, &idx
, -1);
381 list
[list
[0] + 1] = idx
;
382 list
[0] = list
[0] + 1;
385 static int* get_selected_idx_library(omnplay_instance_t
* app
)
388 GtkTreeSelection
*selection
;
390 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->library_grid
));
393 list
= (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
394 memset(list
, 0, sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
396 gtk_tree_selection_selected_foreach(
398 get_selected_idx_library_proc
,
412 playlist_item_t
* omnplay_library_get_selected(omnplay_instance_t
* app
, int *count
)
415 playlist_item_t
* items
= NULL
;
417 pthread_mutex_lock(&app
->library
.lock
);
421 idxs
= get_selected_idx_library(app
);
428 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * (idxs
[0] + 1));
430 /* clear last item */
431 memset(&items
[idxs
[0]], 0, sizeof(playlist_item_t
));
434 for(i
= 0; i
< idxs
[0]; i
++)
435 items
[i
] = app
->library
.item
[idxs
[i
+ 1]];
441 pthread_mutex_unlock(&app
->library
.lock
);
446 void omnplay_library_search(omnplay_instance_t
* app
, int next
)
453 pthread_mutex_lock(&app
->library
.lock
);
455 idxs
= get_selected_idx_library(app
);
456 if(idxs
) idx
= idxs
[1];
462 search
= gtk_entry_get_text(GTK_ENTRY(app
->library
.search
));
466 for(i
= idx
; i
< app
->library
.count
; i
++)
467 if( strcasestr(app
->library
.item
[i
].id
, search
) ||
468 strcasestr(app
->library
.item
[i
].title
, search
))
471 if(i
< app
->library
.count
)
473 g_warning("found at pos=%d\n", i
);
476 path
= gtk_tree_path_new_from_indices(i
, -1);
477 gtk_tree_selection_select_path(gtk_tree_view_get_selection(
478 GTK_TREE_VIEW(app
->library_grid
)), path
);
479 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->library_grid
), path
, NULL
, FALSE
);
480 gtk_tree_path_free(path
);
484 pthread_mutex_unlock(&app
->library
.lock
);