a54bfaf77109796b10faa3891bdcd48c6c853bea
[melted_gui] / src / library.c
1 /*
2 * playlist.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 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 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <pthread.h>
30
31 #include "omnplay.h"
32 #include "ui.h"
33 #include "timecode.h"
34
35 playlist_item_t* omnplay_library_find(omnplay_instance_t* app, char* id)
36 {
37 int i;
38 playlist_item_t* item = NULL;
39
40 pthread_mutex_lock(&app->library.lock);
41
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];
45
46 pthread_mutex_unlock(&app->library.lock);
47
48 return item;
49 };
50
51 void omnplay_library_normalize_item(omnplay_instance_t* app, playlist_item_t* item)
52 {
53 playlist_item_t* lib;
54
55 pthread_mutex_lock(&app->library.lock);
56
57 lib = omnplay_library_find(app, item->id);
58
59 if(lib)
60 {
61
62 if(!item->title[0])
63 strcpy(item->title, lib->title);
64
65 if(!item->dur)
66 {
67 item->dur = lib->dur;
68 item->in = lib->in;
69 };
70 }
71
72 pthread_mutex_unlock(&app->library.lock);
73 };
74
75 void omnplay_library_sort(omnplay_instance_t* app)
76 {
77 int i, j, m;
78 playlist_item_t item;
79
80 for(i = 0; i < app->library.count; i++)
81 {
82 /* find max */
83 for(j = i + 1, m = i; j < app->library.count; j++)
84 if(strcasecmp(app->library.item[j].id, app->library.item[m].id) < 0)
85 m = j;
86
87 if(m != i)
88 {
89 item = app->library.item[i];
90 app->library.item[i] = app->library.item[m];
91 app->library.item[m] = item;
92 };
93 };
94 };
95
96 int omnplay_library_load_file(playlist_item_t* items, int *pcount, char* filename)
97 {
98 int i, c = 0, r = 0;
99 FILE* f;
100 char *l;
101 int limit = *pcount;
102 playlist_item_t item;
103
104 /* allocate space for strings and items */
105 l = malloc(PATH_MAX);
106
107 *pcount = 0;
108
109 /* open and process file */
110 if((f = fopen(filename, "rt")))
111 {
112 while( !feof(f) && c < (limit -1))
113 {
114 char *s, *sp_r, *sp_b;
115
116 /* load string */
117 memset(l, 0, PATH_MAX);
118 fgets(l, PATH_MAX, f);
119
120 /* remove newlines */
121 if( (s = strchr(l, '\n')) ) *s = 0;
122 if( (s = strchr(l, '\r')) ) *s = 0;
123
124 /* check for empty line */
125 if(l[0] && l[0] != '#' && l[0] != '|')
126 {
127 memset(&item, 0, sizeof(playlist_item_t));
128
129 for(i = 0, sp_b = l; (NULL != (sp_r = strtok(sp_b, "\t"))); i++, sp_b = NULL)
130 {
131 switch(i)
132 {
133 case 0: strncpy(item.id, sp_r, PATH_MAX); break;
134 case 1: tc2frames(sp_r, 25.0, &item.in); break;
135 case 2: tc2frames(sp_r, 25.0, &item.dur); break;
136 case 3: strncpy(item.title, sp_r, PATH_MAX); break;
137 };
138 };
139
140 /* insert item */
141 items[c++] = item;
142 };
143 }
144
145 fclose(f);
146 }
147 else
148 r = -1;
149
150 /* free data */
151 free(l);
152
153 *pcount = c;
154
155 return r;
156 };
157
158 void omnplay_library_load(omnplay_instance_t* app)
159 {
160 pthread_mutex_lock(&app->library.lock);
161
162 if(app->library.filename[0])
163 {
164 app->library.count = MAX_LIBRARY_ITEMS;
165 omnplay_library_load_file(app->library.item, &app->library.count, app->library.filename);
166 };
167
168 omnplay_library_sort(app);
169
170 pthread_mutex_unlock(&app->library.lock);
171
172 omnplay_library_draw(app);
173 };
174
175 static void omnplay_library_save_file(playlist_item_t* item, int count, char* filename)
176 {
177 int i;
178 FILE* f;
179
180 if((f = fopen(filename, "wt")))
181 {
182 char tc_in[32], tc_dur[32];
183
184 for(i = 0; i < count; i++)
185 fprintf(f, "%s\t%s\t%s\t%s\n",
186 item[i].id,
187 frames2tc(item[i].in, 25.0, tc_in),
188 frames2tc(item[i].dur, 25.0, tc_dur),
189 item[i].title);
190
191 fclose(f);
192 };
193 };
194
195 void omnplay_library_save(omnplay_instance_t* app)
196 {
197 pthread_mutex_lock(&app->library.lock);
198
199 if(app->library.filename[0])
200 omnplay_library_save_file(app->library.item, app->library.count,
201 app->library.filename);
202
203 pthread_mutex_unlock(&app->library.lock);
204 };
205
206 static void omnplay_get_content_cb(omnplay_instance_t* app, playlist_item_t* item, void* data)
207 {
208 fprintf(stderr, "requested: id=[%s]\n", item->id);
209 };
210
211 void omnplay_library_refresh(omnplay_instance_t* app)
212 {
213 int count, i;
214 playlist_item_t* items;
215
216
217 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * MAX_LIBRARY_ITEMS);
218
219 count = omnplay_get_content(app, items, MAX_LIBRARY_ITEMS, omnplay_get_content_cb, NULL);
220
221 if(count > 0)
222 {
223 if(app->library.whois[0])
224 omnplay_whois_list(app, items, &count);
225
226 pthread_mutex_lock(&app->library.lock);
227
228 for(i = 0; i < count; i++)
229 app->library.item[i] = items[i];
230
231 app->library.count = count;
232
233 omnplay_library_sort(app);
234
235 pthread_mutex_unlock(&app->library.lock);
236
237 omnplay_library_draw(app);
238 };
239
240 free(items);
241 };
242
243 void omnplay_library_draw(omnplay_instance_t* app)
244 {
245 int i;
246 char tc[12];
247 GtkListStore *list_store;
248 GtkTreeIter iter;
249
250 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_grid)));
251 gtk_list_store_clear(list_store);
252
253 pthread_mutex_lock(&app->library.lock);
254
255 for(i = 0;i < app->library.count; i++)
256 {
257 gtk_list_store_append(list_store, &iter);
258
259 gtk_list_store_set(list_store, &iter,
260 0, app->library.item[i].id,
261 1, frames2tc(app->library.item[i].dur, 25.0, tc),
262 2, app->library.item[i].title,
263 3, i,
264 -1 );
265 }
266
267 pthread_mutex_unlock(&app->library.lock);
268 };
269