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
)
61 pthread_mutex_lock(&app
->library
.lock
);
63 lib
= omnplay_library_find(app
, item
->id
);
72 strcpy(item
->title
, lib
->title
);
76 if(!item
->dur
|| item
->in
< lib
->in
|| (item
->in
+ item
->dur
) > (lib
->in
+ lib
->dur
))
86 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
89 pthread_mutex_unlock(&app
->library
.lock
);
94 void omnplay_library_sort(omnplay_instance_t
* app
)
99 for(i
= 0; i
< app
->library
.count
; i
++)
102 for(j
= i
+ 1, m
= i
; j
< app
->library
.count
; j
++)
103 if(strcasecmp(app
->library
.item
[j
].id
, app
->library
.item
[m
].id
) < 0)
108 item
= app
->library
.item
[i
];
109 app
->library
.item
[i
] = app
->library
.item
[m
];
110 app
->library
.item
[m
] = item
;
115 int omnplay_library_load_file(playlist_item_t
* items
, int *pcount
, char* filename
)
121 playlist_item_t item
;
123 /* allocate space for strings and items */
124 l
= malloc(PATH_MAX
);
128 /* open and process file */
129 if((f
= fopen(filename
, "rt")))
131 while( !feof(f
) && c
< (limit
-1))
133 char *s
, *sp_r
, *sp_b
;
136 memset(l
, 0, PATH_MAX
);
137 fgets(l
, PATH_MAX
, f
);
139 /* remove newlines */
140 if( (s
= strchr(l
, '\n')) ) *s
= 0;
141 if( (s
= strchr(l
, '\r')) ) *s
= 0;
143 /* check for empty line */
144 if(l
[0] && l
[0] != '#' && l
[0] != '|')
146 memset(&item
, 0, sizeof(playlist_item_t
));
148 for(i
= 0, sp_b
= l
; (NULL
!= (sp_r
= strtok(sp_b
, "\t"))); i
++, sp_b
= NULL
)
152 case 0: strncpy(item
.id
, sp_r
, PATH_MAX
); break;
153 case 1: tc2frames(sp_r
, 25.0, &item
.in
); break;
154 case 2: tc2frames(sp_r
, 25.0, &item
.dur
); break;
155 case 3: strncpy(item
.title
, sp_r
, PATH_MAX
); break;
177 void omnplay_library_load(omnplay_instance_t
* app
)
179 pthread_mutex_lock(&app
->library
.lock
);
181 if(app
->library
.filename
[0])
183 app
->library
.count
= MAX_LIBRARY_ITEMS
;
184 omnplay_library_load_file(app
->library
.item
, &app
->library
.count
, app
->library
.filename
);
187 omnplay_library_sort(app
);
189 pthread_mutex_unlock(&app
->library
.lock
);
191 omnplay_library_draw(app
);
194 static void omnplay_library_save_file(playlist_item_t
* item
, int count
, char* filename
)
199 if((f
= fopen(filename
, "wt")))
201 char tc_in
[32], tc_dur
[32];
203 for(i
= 0; i
< count
; i
++)
204 fprintf(f
, "%s\t%s\t%s\t%s\n",
206 frames2tc(item
[i
].in
, 25.0, tc_in
),
207 frames2tc(item
[i
].dur
, 25.0, tc_dur
),
214 void omnplay_library_save(omnplay_instance_t
* app
)
216 pthread_mutex_lock(&app
->library
.lock
);
218 if(app
->library
.filename
[0])
219 omnplay_library_save_file(app
->library
.item
, app
->library
.count
,
220 app
->library
.filename
);
222 pthread_mutex_unlock(&app
->library
.lock
);
225 static void omnplay_get_content_cb(omnplay_instance_t
* app
, playlist_item_t
* item
, void* data
)
228 gtk_label_set_text(GTK_LABEL(data
), item
->id
);
233 static void* omnplay_library_refresh_proc(void* data
)
235 GtkWidget
*refresh_ui
[2];
236 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
238 playlist_item_t
* items
;
242 /* create UI for monitoring update */
243 ui_library_refresh(app
, &refresh_ui
[0], &refresh_ui
[1]);
244 gtk_widget_show_all(refresh_ui
[0]);
245 gtk_window_present(GTK_WINDOW(refresh_ui
[0]));
250 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * MAX_LIBRARY_ITEMS
);
252 count
= omnplay_get_content(app
, items
, MAX_LIBRARY_ITEMS
, omnplay_get_content_cb
, refresh_ui
[1]);
257 gtk_label_set_text(GTK_LABEL(refresh_ui
[1]), "Quering whois...");
261 if(app
->library
.whois
[0])
262 omnplay_whois_list(app
, items
, &count
);
265 gtk_label_set_text(GTK_LABEL(refresh_ui
[1]), "Setting library...");
269 pthread_mutex_lock(&app
->library
.lock
);
271 for(i
= 0; i
< count
; i
++)
272 app
->library
.item
[i
] = items
[i
];
274 app
->library
.count
= count
;
276 omnplay_library_sort(app
);
278 pthread_mutex_unlock(&app
->library
.lock
);
281 omnplay_library_draw(app
);
289 omnplay_playlist_normalize(app
);
294 gtk_widget_destroy(refresh_ui
[0]);
301 void omnplay_library_refresh(omnplay_instance_t
* app
)
303 if(app
->library
.refresh_thread
)
304 g_thread_join(app
->library
.refresh_thread
);
306 app
->library
.refresh_thread
= g_thread_create(
307 omnplay_library_refresh_proc
, app
, TRUE
, NULL
);
310 void omnplay_library_draw(omnplay_instance_t
* app
)
314 GtkListStore
*list_store
;
317 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->library_grid
)));
318 gtk_list_store_clear(list_store
);
320 pthread_mutex_lock(&app
->library
.lock
);
322 for(i
= 0;i
< app
->library
.count
; i
++)
324 gtk_list_store_append(list_store
, &iter
);
326 gtk_list_store_set(list_store
, &iter
,
327 0, app
->library
.item
[i
].id
,
328 1, frames2tc(app
->library
.item
[i
].dur
, 25.0, tc
),
329 2, app
->library
.item
[i
].title
,
336 pthread_mutex_unlock(&app
->library
.lock
);
339 static void get_selected_idx_library_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
341 int idx
, *list
= (int*)data
;
342 gtk_tree_model_get(model
, iter
, 3, &idx
, -1);
343 list
[list
[0] + 1] = idx
;
344 list
[0] = list
[0] + 1;
347 static int* get_selected_idx_library(omnplay_instance_t
* app
)
350 GtkTreeSelection
*selection
;
352 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->library_grid
));
355 list
= (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
356 memset(list
, 0, sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
358 gtk_tree_selection_selected_foreach(
360 get_selected_idx_library_proc
,
374 playlist_item_t
* omnplay_library_get_selected(omnplay_instance_t
* app
, int *count
)
377 playlist_item_t
* items
= NULL
;
379 pthread_mutex_lock(&app
->library
.lock
);
383 idxs
= get_selected_idx_library(app
);
390 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * (idxs
[0] + 1));
392 /* clear last item */
393 memset(&items
[idxs
[0]], 0, sizeof(playlist_item_t
));
396 for(i
= 0; i
< idxs
[0]; i
++)
397 items
[i
] = app
->library
.item
[idxs
[i
+ 1]];
403 pthread_mutex_unlock(&app
->library
.lock
);
408 void omnplay_library_search(omnplay_instance_t
* app
, int next
)
415 pthread_mutex_lock(&app
->library
.lock
);
417 idxs
= get_selected_idx_library(app
);
418 if(idxs
) idx
= idxs
[1];
424 search
= gtk_entry_get_text(GTK_ENTRY(app
->library
.search
));
428 for(i
= idx
; i
< app
->library
.count
; i
++)
429 if( strcasestr(app
->library
.item
[i
].id
, search
) ||
430 strcasestr(app
->library
.item
[i
].title
, search
))
433 if(i
< app
->library
.count
)
435 g_warning("found at pos=%d\n", i
);
438 path
= gtk_tree_path_new_from_indices(i
, -1);
439 gtk_tree_selection_select_path(gtk_tree_view_get_selection(
440 GTK_TREE_VIEW(app
->library_grid
)), path
);
441 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->library_grid
), path
, NULL
, FALSE
);
442 gtk_tree_path_free(path
);
446 pthread_mutex_unlock(&app
->library
.lock
);