1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: search.c,v 1.6 2005/02/19 13:39:20 nedko Exp $
12 *****************************************************************************/
21 /* ULLONG_MAX should come from limits.h but doesn't */
22 #if !defined(ULLONG_MAX) && defined(ULONG_LONG_MAX)
23 # define ULLONG_MAX ULONG_LONG_MAX
26 #include "../result.h"
27 #include "../libdb/libdb.h"
28 #include "../libfrontend/db.h"
48 #define KEY_PREFIX "mediadatabase_file_search_"
49 #define KEY_FILENAME KEY_PREFIX "filename"
50 #define KEY_FILENAME_PMM KEY_PREFIX "filename_pmm"
51 #define KEY_PATH KEY_PREFIX "path"
52 #define KEY_PATH_PMM KEY_PREFIX "path_pmm"
53 #define KEY_MIN_SIZE KEY_PREFIX "min_size"
54 #define KEY_MIN_SIZE_UNITS KEY_PREFIX "min_size_units"
55 #define KEY_MAX_SIZE KEY_PREFIX "max_size"
56 #define KEY_MAX_SIZE_UNITS KEY_PREFIX "max_size_units"
57 #define KEY_PMM KEY_PREFIX "pmm"
58 #define KEY_STORE KEY_PREFIX "store"
63 mediadb_uint nMediaID
,
64 mediadb_mediatype nMediaType
,
65 const char *pszMediaName
,
66 const char *pszMediaLocation
,
69 mediadb_filetype Filetype
,
75 GdkPixbuf
* pMediaIcon
;
79 GError
* error
= NULL
;
81 char strSize
[MAX_SIZE_DESCRIPTION
];
83 gchar
* pszIconFullname
;
85 pszFullPath
= g_malloc(strlen(pszPath
)+strlen(pszName
)+1);
86 sprintf(pszFullPath
, "%s%s", pszPath
, pszName
);
88 localtime_r(&time
, &tm
);
90 "%u-%02u-%02u %02u:%02u:%02u",
98 get_size_description(nSize
, strSize
);
102 if (Filetype
== MEDIADB_FILETYPE_DIR
)
104 pszIcon
= "icon_folder.png";
108 pszIcon
= "icon_unknown.png";
111 pszIconFullname
= path_get_data_filename(pszIcon
);
112 if (pszIconFullname
!= NULL
)
114 pIcon
= gdk_pixbuf_new_from_file(pszIconFullname
, &error
);
117 g_warning("Could not load icon: %s\n", error
->message
);
122 g_free(pszIconFullname
);
126 g_warning("Could not find icon %s\n", pszIcon
);
129 pszIcon
= "icon_cd.png";
131 pszIconFullname
= path_get_data_filename(pszIcon
);
132 if (pszIconFullname
!= NULL
)
134 pMediaIcon
= gdk_pixbuf_new_from_file(pszIconFullname
, &error
);
137 g_warning("Could not load icon: %s\n", error
->message
);
142 g_free(pszIconFullname
);
146 g_warning("Could not find icon %s\n", pszIcon
);
149 gtk_list_store_append(
152 G_OBJECT(pUserContext
),
159 G_OBJECT(pUserContext
),
163 COL_NAME
, pszFullPath
,
164 COL_SIZE
, (Filetype
== MEDIADB_FILETYPE_DIR
)?NULL
:strSize
,
166 COL_MEDIAID
, (unsigned int)nMediaID
,
167 COL_MEDIAICON
, pMediaIcon
,
168 COL_MEDIANAME
, pszMediaName
,
169 COL_MEDIALOCATION
, pszMediaLocation
,
181 const char *pszFilename
;
182 gint nFilenamePMMIndex
;
185 const char *pszMinSize
;
187 const char *pszMaxSize
;
189 const struct mediadb_pattern_match_method
*pPMM
;
190 mediadb_uint nMinSize
;
191 mediadb_uint nMaxSize
;
195 pPMM
= (const struct mediadb_pattern_match_method
*)g_object_get_data(
199 pszFilename
= gtk_entry_get_text(
205 nFilenamePMMIndex
= gtk_combo_box_get_active(
211 pszPath
= gtk_entry_get_text(
217 nPathPMMIndex
= gtk_combo_box_get_active(
223 pszMinSize
= gtk_entry_get_text(
229 nMinSizeUnits
= gtk_combo_box_get_active(
233 KEY_MIN_SIZE_UNITS
)));
235 pszMaxSize
= gtk_entry_get_text(
241 nMaxSizeUnits
= gtk_combo_box_get_active(
245 KEY_MAX_SIZE_UNITS
)));
247 /* We better handle such thing in backend
248 if (*pszFilename == 0 &&
253 pDialog = gtk_message_dialog_new(
254 GTK_WINDOW(pUserData),
255 GTK_DIALOG_DESTROY_WITH_PARENT,
258 "No search criteria ...\n");
260 gtk_dialog_run(GTK_DIALOG(pDialog));
261 gtk_widget_destroy(pDialog);
269 nMinSizeUnits
> nMaxSizeUnits
)
271 pDialog
= gtk_message_dialog_new(
272 GTK_WINDOW(pUserData
),
273 GTK_DIALOG_DESTROY_WITH_PARENT
,
276 "Min size units (%s) are bigger than max size units (%s).",
277 g_arrSizeUnits
[nMinSizeUnits
].pszUnitsName
,
278 g_arrSizeUnits
[nMaxSizeUnits
].pszUnitsName
);
280 gtk_dialog_run(GTK_DIALOG(pDialog
));
281 gtk_widget_destroy(pDialog
);
288 nMinSize
= strtoull(pszMinSize
, &pchEnd
, 10);
289 if (nMinSize
== 0 || nMinSize
== ULLONG_MAX
|| *pchEnd
)
291 pDialog
= gtk_message_dialog_new(
292 GTK_WINDOW(pUserData
),
293 GTK_DIALOG_DESTROY_WITH_PARENT
,
296 "Cannot convert \"%s\" to decimal integer number.",
299 gtk_dialog_run(GTK_DIALOG(pDialog
));
300 gtk_widget_destroy(pDialog
);
305 if (ULLONG_MAX
/ g_arrSizeUnits
[nMinSizeUnits
].nUnitSize
< nMinSize
)
307 pDialog
= gtk_message_dialog_new(
308 GTK_WINDOW(pUserData
),
309 GTK_DIALOG_DESTROY_WITH_PARENT
,
312 "%llu %s is too large number.",
314 g_arrSizeUnits
[nMinSizeUnits
].pszUnitsName
);
316 gtk_dialog_run(GTK_DIALOG(pDialog
));
317 gtk_widget_destroy(pDialog
);
322 nMinSize
= nMinSize
* g_arrSizeUnits
[nMinSizeUnits
].nUnitSize
;
327 nMaxSize
= strtoull(pszMaxSize
, &pchEnd
, 10);
328 if (nMaxSize
== 0 || nMaxSize
== ULLONG_MAX
|| *pchEnd
)
330 pDialog
= gtk_message_dialog_new(
331 GTK_WINDOW(pUserData
),
332 GTK_DIALOG_DESTROY_WITH_PARENT
,
335 "Cannot convert \"%s\" to decimal integer number.",
338 gtk_dialog_run(GTK_DIALOG(pDialog
));
339 gtk_widget_destroy(pDialog
);
344 if (ULLONG_MAX
/ g_arrSizeUnits
[nMaxSizeUnits
].nUnitSize
< nMaxSize
)
346 pDialog
= gtk_message_dialog_new(
347 GTK_WINDOW(pUserData
),
348 GTK_DIALOG_DESTROY_WITH_PARENT
,
351 "%llu %s is too large number.",
353 g_arrSizeUnits
[nMaxSizeUnits
].pszUnitsName
);
355 gtk_dialog_run(GTK_DIALOG(pDialog
));
356 gtk_widget_destroy(pDialog
);
361 nMaxSize
= nMaxSize
* g_arrSizeUnits
[nMaxSizeUnits
].nUnitSize
;
364 pDialog
= gtk_message_dialog_new(
365 GTK_WINDOW(pUserData
),
366 GTK_DIALOG_DESTROY_WITH_PARENT
,
374 *pszFilename
?"Filename \"":"",
375 *pszFilename
?pszFilename
:"",
376 *pszFilename
?"\" - ":"",
377 *pszFilename
?pPMM
[nFilenamePMMIndex
].pszDescription
:"",
378 *pszFilename
?"\n":"",
379 *pszPath
?"Path \"":"",
382 *pszPath
?pPMM
[nPathPMMIndex
].pszDescription
:"",
384 *pszMinSize
?"Min size ":"",
385 *pszMinSize
?pszMinSize
:"",
387 *pszMinSize
?g_arrSizeUnits
[nMinSizeUnits
].pszUnitsName
:"",
389 *pszMaxSize
?"Max size ":"",
390 *pszMaxSize
?pszMaxSize
:"",
392 *pszMaxSize
?g_arrSizeUnits
[nMaxSizeUnits
].pszUnitsName
:"",
393 *pszMaxSize
?"\n":"");
395 gtk_dialog_run(GTK_DIALOG(pDialog
));
396 gtk_widget_destroy(pDialog
);
398 gtk_list_store_clear(
404 r
= mediadb_files_search(
406 *pszFilename
?pPMM
[nFilenamePMMIndex
].nID
:MEDIADB_PMM_NULL
,
407 *pszFilename
?pszFilename
:NULL
,
408 *pszPath
?pPMM
[nPathPMMIndex
].nID
:MEDIADB_PMM_NULL
,
409 *pszPath
?pszPath
:NULL
,
410 *pszMinSize
?&nMinSize
:NULL
,
411 *pszMaxSize
?&nMaxSize
:NULL
,
414 if (MEDIADB_IS_ERROR(r
))
416 display_error_message_mediadb(GTK_WINDOW(pUserData
));
424 GtkWidget
*pSearchWindow
;
426 GtkCellRenderer
* pRendererText
;
427 GtkCellRenderer
* pRendererPixbuf
;
428 GtkTreeViewColumn
* pColumn
;
429 GtkListStore
*pListStore
;
430 const struct mediadb_pattern_match_method
*pPMM
;
434 r
= mediadb_get_pattern_match_methods(
437 if (MEDIADB_IS_ERROR(r
))
439 display_error_message_mediadb(GTK_WINDOW(pMainWindow
));
443 pSearchWindow
= construct_glade_widget("search_window");
445 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_PMM
, (gpointer
)pPMM
);
448 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_filename_entry");
456 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_FILENAME
, pChild
);
460 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_filename_pmm");
461 setup_text_combo(pChild
);
465 while (pPMM
[i
].nID
!= MEDIADB_PMM_NULL
)
467 gtk_combo_box_append_text(GTK_COMBO_BOX(pChild
), pPMM
[i
].pszName
);
471 gtk_combo_box_set_active(GTK_COMBO_BOX(pChild
), 0);
473 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_FILENAME_PMM
, pChild
);
477 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_path_entry");
485 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_PATH
, pChild
);
489 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_path_pmm");
490 setup_text_combo(pChild
);
494 while (pPMM
[i
].nID
!= MEDIADB_PMM_NULL
)
496 gtk_combo_box_append_text(GTK_COMBO_BOX(pChild
), pPMM
[i
].pszName
);
500 gtk_combo_box_set_active(GTK_COMBO_BOX(pChild
), 0);
502 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_PATH_PMM
, pChild
);
506 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_min_size_entry");
514 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_MIN_SIZE
, pChild
);
518 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_min_size_units");
519 setup_text_combo(pChild
);
523 while (g_arrSizeUnits
[i
].nUnitSize
!= 0)
527 /* Second entry should be kilobytes */
531 gtk_combo_box_append_text(GTK_COMBO_BOX(pChild
), g_arrSizeUnits
[i
].pszUnitsName
);
536 gtk_combo_box_set_active(GTK_COMBO_BOX(pChild
), j
);
538 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_MIN_SIZE_UNITS
, pChild
);
542 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_max_size_entry");
550 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_MAX_SIZE
, pChild
);
554 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_max_size_units");
555 setup_text_combo(pChild
);
559 while (g_arrSizeUnits
[i
].nUnitSize
!= 0)
563 /* Second entry should be kilobytes */
567 gtk_combo_box_append_text(GTK_COMBO_BOX(pChild
), g_arrSizeUnits
[i
].pszUnitsName
);
572 gtk_combo_box_set_active(GTK_COMBO_BOX(pChild
), j
);
574 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_MAX_SIZE_UNITS
, pChild
);
578 pChild
= get_glade_widget_child(pSearchWindow
, "search_window_button_find");
587 /* Tree view setup */
588 pChild
= get_glade_widget_child(pSearchWindow
, "treeview");
590 pRendererText
= gtk_cell_renderer_text_new();
591 pRendererPixbuf
= gtk_cell_renderer_pixbuf_new();
593 /* --- Column #1 --- */
595 pColumn
= gtk_tree_view_column_new();
596 gtk_tree_view_column_set_title(pColumn
, "Name");
598 gtk_tree_view_column_pack_start(pColumn
, pRendererPixbuf
, FALSE
);
599 gtk_tree_view_column_set_attributes(pColumn
, pRendererPixbuf
,
603 gtk_tree_view_column_pack_start(pColumn
, pRendererText
, TRUE
);
604 gtk_tree_view_column_set_attributes(pColumn
,
609 gtk_tree_view_append_column(
610 GTK_TREE_VIEW(pChild
),
613 /* --- Column #2 --- */
615 pColumn
= gtk_tree_view_column_new_with_attributes(
621 gtk_tree_view_append_column(
622 GTK_TREE_VIEW(pChild
),
625 /* --- Column #3 --- */
627 pColumn
= gtk_tree_view_column_new_with_attributes(
633 gtk_tree_view_append_column(
634 GTK_TREE_VIEW(pChild
),
637 /* --- Column #4 --- */
639 pColumn
= gtk_tree_view_column_new_with_attributes(
645 gtk_tree_view_append_column(
646 GTK_TREE_VIEW(pChild
),
649 /* --- Column #5 --- */
651 pColumn
= gtk_tree_view_column_new();
653 gtk_tree_view_column_set_title(
657 gtk_tree_view_column_pack_start(
662 gtk_tree_view_column_set_attributes(
669 gtk_tree_view_column_pack_end(
674 gtk_tree_view_column_set_attributes(
677 "text", COL_MEDIANAME
,
680 gtk_tree_view_append_column(
681 GTK_TREE_VIEW(pChild
),
684 /* --- Column #6 --- */
686 pColumn
= gtk_tree_view_column_new_with_attributes(
689 "text", COL_MEDIALOCATION
,
692 gtk_tree_view_append_column(
693 GTK_TREE_VIEW(pChild
),
696 /* Create the store */
697 pListStore
= gtk_list_store_new(
699 GDK_TYPE_PIXBUF
, /* COL_ICON */
700 G_TYPE_STRING
, /* COL_NAME */
701 G_TYPE_STRING
, /* COL_SIZE */
702 G_TYPE_STRING
, /* COL_TIME */
703 G_TYPE_UINT
, /* COL_MEDIAID */
704 GDK_TYPE_PIXBUF
, /* COL_MEDIAICON */
705 G_TYPE_STRING
, /* COL_MEDIANAME */
706 G_TYPE_STRING
); /* COL_MEDIALOCATION */
709 gtk_tree_view_set_model(GTK_TREE_VIEW(pChild
), GTK_TREE_MODEL(pListStore
));
711 g_object_set_data(G_OBJECT(pSearchWindow
), KEY_STORE
, pListStore
);
713 gtk_widget_show_all(pSearchWindow
);
716 /*****************************************************************************
720 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
723 * Revision 1.6 2005/02/19 13:39:20 nedko
724 * Switch search window to glade.
726 * Revision 1.5 2005/02/18 02:12:08 nedko
727 * Use path module to access project specific data files.
729 * Revision 1.4 2004/09/18 21:34:59 nedko
730 * Return media name, media type and media location when searching files.
732 * Revision 1.3 2004/09/01 05:05:43 nedko
733 * Search partitially works.
735 * Revision 1.2 2004/09/01 00:56:43 nedko
737 * Attach callback for search action.
739 * Revision 1.1 2004/08/31 22:40:15 nedko
740 * Partitally implemented search feature.
742 *****************************************************************************/