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