Modification log added
[mediadatabase.git] / gtk / medias.c
blobb35ec0d84bbd1ec12aa76d9f469dd618ec8e3a08
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: medias.c,v 1.6 2005/02/19 21:02:34 nedko Exp $
6 * DESCRIPTION:
7 *
9 * NOTES:
12 *****************************************************************************/
14 #include <time.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <gtk/gtk.h>
20 #include "../result.h"
21 #include "../libdb/libdb.h"
22 #include "../libfrontend/db.h"
23 #include "common.h"
24 #include "helper.h"
25 #include "media.h"
26 #include "menu.h"
27 #include "path.h"
28 #include "glade.h"
30 enum
32 COL_MEDIAID = 0,
33 COL_MEDIAICON,
34 COL_MEDIANAME,
35 COL_MEDIAFILES,
36 COL_MEDIASIZE,
37 COL_MEDIADATE,
38 COL_MEDIALOCATION,
39 COL_MEDIACOMMENT,
40 NUM_COLS
43 void
44 media_callback(
45 void *pUserContext,
46 mediadb_uint nMediaID,
47 mediadb_mediatype nType,
48 const char *pszName,
49 const char *pszComment,
50 mediadb_uint nTimeAdded,
51 mediadb_uint nTotalFiles,
52 mediadb_uint nTotalSize,
53 mediadb_uint nLocationID,
54 const char *pszLocation
57 GtkTreeIter iter;
58 char strTime[26];
59 time_t time = nTimeAdded;
60 char strTotalSize[MAX_SIZE_DESCRIPTION];
61 struct tm tm;
62 GdkPixbuf * pIcon;
63 GError * error = NULL;
64 char * pszIcon;
65 gchar * pszIconFullname;
67 /* ctime_r(&time, strTime); */
68 /* strTime[strlen(strTime) - 1] = 0; */
70 localtime_r(&time, &tm);
71 sprintf(strTime,
72 "%u-%02u-%02u %02u:%02u:%02u",
73 1900+tm.tm_year,
74 1+tm.tm_mon,
75 tm.tm_mday,
76 tm.tm_hour,
77 tm.tm_min,
78 tm.tm_sec);
80 get_size_description(nTotalSize, strTotalSize);
82 pIcon = NULL;
84 if (nType == MEDIADB_MT_AUDIO)
86 pszIcon = "icon_acd.png";
88 else if (nType == MEDIADB_MT_DATA)
90 pszIcon = "icon_cd.png";
92 else
94 pszIcon = NULL;
97 if (pszIcon != NULL)
99 pszIconFullname = path_get_data_filename(pszIcon);
100 if (pszIconFullname != NULL)
102 pIcon = gdk_pixbuf_new_from_file(pszIconFullname, &error);
103 if (error)
105 g_warning("Could not load icon: %s\n", error->message);
106 g_error_free(error);
107 error = NULL;
110 g_free(pszIconFullname);
112 else
114 g_warning("Could not find icon %s\n", pszIcon);
118 gtk_list_store_prepend(
119 (GtkListStore *)pUserContext,
120 &iter);
121 gtk_list_store_set(
122 (GtkListStore *)pUserContext,
123 &iter,
124 COL_MEDIAID, (guint)nMediaID,
125 COL_MEDIAICON, pIcon,
126 COL_MEDIANAME, pszName,
127 COL_MEDIAFILES, (guint)nTotalFiles,
128 COL_MEDIASIZE, strTotalSize,
129 COL_MEDIADATE, strTime,
130 COL_MEDIALOCATION, pszLocation,
131 COL_MEDIACOMMENT, pszComment,
132 -1);
135 static GtkTreeModel *
136 create_and_fill_model(void)
138 mediadb_result r;
139 GtkListStore *pStore;
140 GtkWidget *pDialog;
142 pStore = gtk_list_store_new(
143 NUM_COLS,
144 G_TYPE_UINT, /* COL_MEDIAID */
145 GDK_TYPE_PIXBUF, /* COL_MEDIAICON */
146 G_TYPE_STRING, /* COL_MEDIANAME */
147 G_TYPE_UINT, /* COL_MEDIAFILES */
148 G_TYPE_STRING, /* COL_MEDIASIZE */
149 G_TYPE_STRING, /* COL_MEDIADATE */
150 G_TYPE_STRING, /* COL_MEDIALOCATION */
151 G_TYPE_STRING); /* COL_MEDIACOMMENT */
153 if (g_hDB != NULL)
155 r = mediadb_media_get_all(
156 g_hDB,
157 media_callback,
158 pStore);
159 if (MEDIADB_IS_ERROR(r))
161 g_warning("%s\n", mediadb_get_error_message(g_hDB));
163 pDialog = gtk_message_dialog_new(
164 GTK_WINDOW(pMainWindow),
165 GTK_DIALOG_DESTROY_WITH_PARENT,
166 GTK_MESSAGE_ERROR,
167 GTK_BUTTONS_CLOSE,
168 "%s",
169 mediadb_get_error_message(g_hDB));
171 gtk_dialog_run(GTK_DIALOG(pDialog));
172 gtk_widget_destroy(pDialog);
176 return GTK_TREE_MODEL(pStore);
179 void
180 view_onRowActivated(
181 GtkTreeView *pTreeView,
182 GtkTreePath *pPath,
183 GtkTreeViewColumn *pColumn,
184 gpointer userdata)
186 GtkTreeModel *pModel;
187 GtkTreeIter iter;
188 unsigned int nMediaID;
190 pModel = gtk_tree_view_get_model(pTreeView);
192 if (gtk_tree_model_get_iter(pModel, &iter, pPath))
194 gtk_tree_model_get(pModel, &iter, COL_MEDIAID, &nMediaID, -1);
196 CreateMediaWindow(nMediaID);
200 static void
201 setup_view_and_model(GtkWidget *pTreeView)
203 GtkTreeViewColumn *col;
204 GtkCellRenderer *renderer;
205 GtkTreeModel *model;
206 GtkCellRenderer * pRendererPixbuf;
208 renderer = gtk_cell_renderer_text_new();
209 pRendererPixbuf = gtk_cell_renderer_pixbuf_new();
211 /* --- Column #1 --- */
213 col = gtk_tree_view_column_new_with_attributes(
214 "ID",
215 renderer,
216 "text", COL_MEDIAID,
217 NULL);
219 gtk_tree_view_append_column(
220 GTK_TREE_VIEW(pTreeView),
221 col);
223 /* --- Column #2 --- */
225 col = gtk_tree_view_column_new();
227 gtk_tree_view_column_set_title(col, "Name");
229 gtk_tree_view_column_pack_start(col, pRendererPixbuf, FALSE);
230 gtk_tree_view_column_set_attributes(col, pRendererPixbuf,
231 "pixbuf", COL_MEDIAICON,
232 NULL);
234 gtk_tree_view_column_pack_end(col, renderer, TRUE);
235 gtk_tree_view_column_set_attributes(col,
236 renderer,
237 "text", COL_MEDIANAME,
238 NULL);
240 gtk_tree_view_append_column(
241 GTK_TREE_VIEW(pTreeView),
242 col);
244 gtk_tree_view_column_set_resizable(col, TRUE);
245 // gtk_tree_view_column_set_max_width(col, 200);
246 // gtk_tree_view_column_set_min_width(col, 200);
248 /* --- Column #3 --- */
250 col = gtk_tree_view_column_new_with_attributes(
251 "Location",
252 renderer,
253 "text", COL_MEDIALOCATION,
254 NULL);
256 gtk_tree_view_append_column(
257 GTK_TREE_VIEW(pTreeView),
258 col);
260 /* --- Column #4 --- */
262 col = gtk_tree_view_column_new_with_attributes(
263 "Files Size",
264 renderer,
265 "text", COL_MEDIASIZE,
266 NULL);
268 gtk_tree_view_append_column(
269 GTK_TREE_VIEW(pTreeView),
270 col);
272 /* --- Column #5 --- */
274 col = gtk_tree_view_column_new_with_attributes(
275 "Files Count",
276 renderer,
277 "text", COL_MEDIAFILES,
278 NULL);
280 gtk_tree_view_append_column(
281 GTK_TREE_VIEW(pTreeView),
282 col);
284 /* --- Column #6 --- */
286 col = gtk_tree_view_column_new_with_attributes(
287 "Date added",
288 renderer,
289 "text", COL_MEDIADATE,
290 NULL);
292 gtk_tree_view_append_column(
293 GTK_TREE_VIEW(pTreeView),
294 col);
296 /* --- Column #7 --- */
298 col = gtk_tree_view_column_new_with_attributes(
299 "Comment",
300 renderer,
301 "text", COL_MEDIACOMMENT,
302 NULL);
304 gtk_tree_view_append_column(
305 GTK_TREE_VIEW(pTreeView),
306 col);
308 model = create_and_fill_model();
310 gtk_tree_view_set_model(GTK_TREE_VIEW(pTreeView), model);
312 g_object_unref(model); /* destroy model automatically with view */
314 g_signal_connect(
315 pTreeView,
316 "row-activated",
317 (GCallback)view_onRowActivated,
318 NULL);
321 GtkWidget *
322 CreateMediasWindow()
324 GtkWidget *pMediasWindow;
326 pMediasWindow = construct_glade_widget("medias_window");
328 setup_view_and_model(get_glade_widget_child(pMediasWindow, "medias_treeview"));
330 gtk_widget_show_all(pMediasWindow);
332 return pMediasWindow;
335 /*****************************************************************************
337 * Modifications log:
339 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
341 * $Log: medias.c,v $
342 * Revision 1.6 2005/02/19 21:02:34 nedko
343 * Don't try to access database when it is not opened.
345 * Revision 1.5 2005/02/18 22:20:05 nedko
346 * Cleanup things and make them better way.
348 * Revision 1.4 2005/02/18 02:12:47 nedko
349 * Use path module to access project specific data files.
350 * Use libglade.
352 * Revision 1.3 2004/08/31 22:40:15 nedko
353 * Partitally implemented search feature.
355 * Revision 1.2 2004/05/22 00:36:24 nedko
356 * set WM_WINDOWS_ROLE
358 * Revision 1.1 2004/05/21 23:43:38 nedko
359 * Implement media window.
361 *****************************************************************************/