library load/save support added
[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 void omnplay_library_load(omnplay_instance_t* app)
36 {
37 int i;
38 FILE* f;
39 char *l;
40 playlist_item_t item;
41
42 /* allocate space for strings and items */
43 l = malloc(PATH_MAX);
44
45 pthread_mutex_lock(&app->library.lock);
46
47 app->library.count = 0;
48
49 /* open and process file */
50 if(app->library.filename[0] && (f = fopen(app->library.filename, "rt")))
51 {
52 while( !feof(f) )
53 {
54 char *s, *sp_r, *sp_b;
55
56 /* load string */
57 memset(l, 0, PATH_MAX);
58 fgets(l, PATH_MAX, f);
59
60 /* remove newlines */
61 if( (s = strchr(l, '\n')) ) *s = 0;
62 if( (s = strchr(l, '\r')) ) *s = 0;
63
64 /* check for empty line */
65 if(l[0] && l[0] != '#')
66 {
67 memset(&item, 0, sizeof(playlist_item_t));
68
69 for(i = 0, sp_b = l; (NULL != (sp_r = strtok(sp_b, "\t"))); i++, sp_b = NULL)
70 {
71 switch(i)
72 {
73 case 0: strncpy(item.id, sp_r, PATH_MAX); break;
74 case 1: tc2frames(sp_r, 25.0, &item.in); break;
75 case 2: tc2frames(sp_r, 25.0, &item.dur); break;
76 case 3: strncpy(item.title, sp_r, PATH_MAX); break;
77 };
78 };
79
80 /* insert item */
81 app->library.item[app->library.count++] = item;
82 };
83 }
84
85 fclose(f);
86 }
87
88 pthread_mutex_unlock(&app->library.lock);
89
90 /* free data */
91 free(l);
92
93 omnplay_library_draw(app);
94 };
95
96 void omnplay_library_save(omnplay_instance_t* app)
97 {
98 int i;
99 FILE* f;
100
101 pthread_mutex_lock(&app->library.lock);
102
103 if(app->library.filename[0] && (f = fopen(app->library.filename, "wt")))
104 {
105 char tc_in[32], tc_dur[32];
106
107 for(i = 0; i < app->library.count; i++)
108 fprintf(f, "%s\t%s\t%s\t%s\n",
109 app->library.item[i].id,
110 frames2tc(app->library.item[i].in, 25.0, tc_in),
111 frames2tc(app->library.item[i].dur, 25.0, tc_dur),
112 app->library.item[i].title);
113
114 fclose(f);
115 };
116
117 pthread_mutex_unlock(&app->library.lock);
118 };
119
120 void omnplay_library_refresh(omnplay_instance_t* app)
121 {
122 };
123
124 void omnplay_library_draw(omnplay_instance_t* app)
125 {
126 int i;
127 char tc[12];
128 GtkListStore *list_store;
129 GtkTreeIter iter;
130
131 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->library_grid)));
132 gtk_list_store_clear(list_store);
133
134 pthread_mutex_lock(&app->library.lock);
135
136 for(i = 0;i < app->library.count; i++)
137 {
138 gtk_list_store_append(list_store, &iter);
139
140 gtk_list_store_set(list_store, &iter,
141 0, app->library.item[i].id,
142 1, frames2tc(app->playlist.item[i].dur, 25.0, tc),
143 2, app->library.item[i].title,
144 3, i,
145 -1 );
146 }
147
148 pthread_mutex_unlock(&app->library.lock);
149 };