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.
28 #include <gdk/gdkkeysyms.h>
35 playlist_item_t
* omnplay_library_find(omnplay_instance_t
* app
, char* id
)
38 playlist_item_t
* item
= NULL
;
40 pthread_mutex_lock(&app
->library
.lock
);
42 for(i
= 0; i
< app
->library
.count
&& !item
; i
++)
43 if(!strcasecmp(id
, app
->library
.item
[i
].id
))
44 item
= &app
->library
.item
[i
];
46 pthread_mutex_unlock(&app
->library
.lock
);
51 int omnplay_library_normalize_item(omnplay_instance_t
* app
, playlist_item_t
* item
)
56 pthread_mutex_lock(&app
->library
.lock
);
58 lib
= omnplay_library_find(app
, item
->id
);
67 strcpy(item
->title
, lib
->title
);
71 if(!item
->dur
|| item
->in
< lib
->in
|| (item
->in
+ item
->dur
) > (lib
->in
+ lib
->dur
))
81 item
->error
= PLAYLIST_ITEM_ERROR_LIB
;
84 pthread_mutex_unlock(&app
->library
.lock
);
89 void omnplay_library_sort(omnplay_instance_t
* app
)
94 for(i
= 0; i
< app
->library
.count
; i
++)
97 for(j
= i
+ 1, m
= i
; j
< app
->library
.count
; j
++)
98 if(strcasecmp(app
->library
.item
[j
].id
, app
->library
.item
[m
].id
) < 0)
103 item
= app
->library
.item
[i
];
104 app
->library
.item
[i
] = app
->library
.item
[m
];
105 app
->library
.item
[m
] = item
;
110 int omnplay_library_load_file(playlist_item_t
* items
, int *pcount
, char* filename
)
116 playlist_item_t item
;
118 /* allocate space for strings and items */
119 l
= malloc(PATH_MAX
);
123 /* open and process file */
124 if((f
= fopen(filename
, "rt")))
126 while( !feof(f
) && c
< (limit
-1))
128 char *s
, *sp_r
, *sp_b
;
131 memset(l
, 0, PATH_MAX
);
132 fgets(l
, PATH_MAX
, f
);
134 /* remove newlines */
135 if( (s
= strchr(l
, '\n')) ) *s
= 0;
136 if( (s
= strchr(l
, '\r')) ) *s
= 0;
138 /* check for empty line */
139 if(l
[0] && l
[0] != '#' && l
[0] != '|')
141 memset(&item
, 0, sizeof(playlist_item_t
));
143 for(i
= 0, sp_b
= l
; (NULL
!= (sp_r
= strtok(sp_b
, "\t"))); i
++, sp_b
= NULL
)
147 case 0: strncpy(item
.id
, sp_r
, PATH_MAX
); break;
148 case 1: tc2frames(sp_r
, 25.0, &item
.in
); break;
149 case 2: tc2frames(sp_r
, 25.0, &item
.dur
); break;
150 case 3: strncpy(item
.title
, sp_r
, PATH_MAX
); break;
172 void omnplay_library_load(omnplay_instance_t
* app
)
174 pthread_mutex_lock(&app
->library
.lock
);
176 if(app
->library
.filename
[0])
178 app
->library
.count
= MAX_LIBRARY_ITEMS
;
179 omnplay_library_load_file(app
->library
.item
, &app
->library
.count
, app
->library
.filename
);
182 omnplay_library_sort(app
);
184 pthread_mutex_unlock(&app
->library
.lock
);
186 omnplay_library_draw(app
);
189 static void omnplay_library_save_file(playlist_item_t
* item
, int count
, char* filename
)
194 if((f
= fopen(filename
, "wt")))
196 char tc_in
[32], tc_dur
[32];
198 for(i
= 0; i
< count
; i
++)
199 fprintf(f
, "%s\t%s\t%s\t%s\n",
201 frames2tc(item
[i
].in
, 25.0, tc_in
),
202 frames2tc(item
[i
].dur
, 25.0, tc_dur
),
209 void omnplay_library_save(omnplay_instance_t
* app
)
211 pthread_mutex_lock(&app
->library
.lock
);
213 if(app
->library
.filename
[0])
214 omnplay_library_save_file(app
->library
.item
, app
->library
.count
,
215 app
->library
.filename
);
217 pthread_mutex_unlock(&app
->library
.lock
);
220 static void omnplay_get_content_cb(omnplay_instance_t
* app
, playlist_item_t
* item
, void* data
)
222 fprintf(stderr
, "requested: id=[%s]\n", item
->id
);
225 void omnplay_library_refresh(omnplay_instance_t
* app
)
228 playlist_item_t
* items
;
230 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * MAX_LIBRARY_ITEMS
);
232 count
= omnplay_get_content(app
, items
, MAX_LIBRARY_ITEMS
, omnplay_get_content_cb
, NULL
);
236 if(app
->library
.whois
[0])
237 omnplay_whois_list(app
, items
, &count
);
239 pthread_mutex_lock(&app
->library
.lock
);
241 for(i
= 0; i
< count
; i
++)
242 app
->library
.item
[i
] = items
[i
];
244 app
->library
.count
= count
;
246 omnplay_library_sort(app
);
248 pthread_mutex_unlock(&app
->library
.lock
);
250 omnplay_library_draw(app
);
255 omnplay_playlist_normalize(app
);
258 void omnplay_library_draw(omnplay_instance_t
* app
)
262 GtkListStore
*list_store
;
265 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->library_grid
)));
266 gtk_list_store_clear(list_store
);
268 pthread_mutex_lock(&app
->library
.lock
);
270 for(i
= 0;i
< app
->library
.count
; i
++)
272 gtk_list_store_append(list_store
, &iter
);
274 gtk_list_store_set(list_store
, &iter
,
275 0, app
->library
.item
[i
].id
,
276 1, frames2tc(app
->library
.item
[i
].dur
, 25.0, tc
),
277 2, app
->library
.item
[i
].title
,
284 pthread_mutex_unlock(&app
->library
.lock
);
287 static void get_selected_idx_library_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
289 int idx
, *list
= (int*)data
;
290 gtk_tree_model_get(model
, iter
, 3, &idx
, -1);
291 list
[list
[0] + 1] = idx
;
292 list
[0] = list
[0] + 1;
295 static int* get_selected_idx_library(omnplay_instance_t
* app
)
298 GtkTreeSelection
*selection
;
300 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->library_grid
));
303 list
= (int*)malloc(sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
304 memset(list
, 0, sizeof(int) * (MAX_LIBRARY_ITEMS
+ 1));
306 gtk_tree_selection_selected_foreach(
308 get_selected_idx_library_proc
,
322 playlist_item_t
* omnplay_library_get_selected(omnplay_instance_t
* app
, int *count
)
325 playlist_item_t
* items
= NULL
;
327 pthread_mutex_lock(&app
->library
.lock
);
331 idxs
= get_selected_idx_library(app
);
338 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * (idxs
[0] + 1));
340 /* clear last item */
341 memset(&items
[idxs
[0]], 0, sizeof(playlist_item_t
));
344 for(i
= 0; i
< idxs
[0]; i
++)
345 items
[i
] = app
->library
.item
[idxs
[i
+ 1]];
351 pthread_mutex_unlock(&app
->library
.lock
);