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