/* * Copyright 2015 (c) Anna Schumaker. */ #include #include #include #include #include enum collection_sidebar_columns { C_SB_IMAGE, C_SB_IMAGE_SZ, C_SB_ENABLED, C_SB_SHOW_ENABLED, C_SB_PATH, C_SB_LIBRARY, }; static GtkTreeModel *c_model; static void __collection_set_header(GtkTreeIter *iter, const gchar *image, GtkIconSize size, const gchar *text) { gtk_tree_store_set(GTK_TREE_STORE(c_model), iter, C_SB_IMAGE, image, C_SB_IMAGE_SZ, size, C_SB_PATH, text, -1); } static void __collection_set_library(GtkTreeIter *iter, struct library *library) { gtk_tree_store_set(GTK_TREE_STORE(c_model), iter, C_SB_ENABLED, library->li_enabled, C_SB_SHOW_ENABLED, true, C_SB_PATH, library->li_path, C_SB_LIBRARY, library, -1); } static struct library *__collection_get_library(GtkTreeIter *iter) { struct library *library; gtk_tree_model_get(c_model, iter, C_SB_LIBRARY, &library, -1); return library; } void __collection_add(GtkButton *button, GtkFileChooser *chooser) { gchar *filename = gtk_file_chooser_get_filename(chooser); GtkTreeIter iter, last; gtk_tree_model_get_iter_first(c_model, &iter); gtk_tree_model_iter_nth_child(c_model, &last, &iter, 0); while (__collection_get_library(&last) != NULL) gtk_tree_model_iter_next(c_model, &last); gtk_tree_store_insert_before(GTK_TREE_STORE(c_model), &iter, NULL, &last); __collection_set_library(&iter, collection_add(filename)); gui_collection_idle_enable(); g_free(filename); } void __collection_selection_changed(GtkTreeSelection *selection, gpointer data) { GtkNotebook *notebook = GTK_NOTEBOOK(gui_builder_widget("o_notebook")); GtkTreeIter iter; if (gtk_tree_selection_get_selected(selection, &c_model, &iter)) { if (!__collection_get_library(&iter)) { gtk_notebook_set_current_page(notebook, tempq_count() + 3); } } } static gboolean __collection_select(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean selected, gpointer data) { return gtk_tree_path_get_depth(path) != 1; } static gboolean __collection_on_idle(gpointer data) { GtkProgressBar *progress = GTK_PROGRESS_BAR(data); if (idle_run_task()) { gtk_progress_bar_set_fraction(progress, idle_progress()); return G_SOURCE_CONTINUE; } else { gtk_widget_hide(GTK_WIDGET(progress)); return G_SOURCE_REMOVE; } } void gui_collection_init() { GtkTreeIter parent, iter; GtkTreeView *treeview; c_model = GTK_TREE_MODEL(gui_builder_object("o_collection_store")); /* Add "Collection" header. */ gtk_tree_store_insert(GTK_TREE_STORE(c_model), &parent, NULL, -1); __collection_set_header(&parent, "system-file-manager", GTK_ICON_SIZE_LARGE_TOOLBAR, "Collection"); /* Add "Add new Library" entry. */ gtk_tree_store_insert(GTK_TREE_STORE(c_model), &iter, &parent, -1); __collection_set_header(&iter, "folder-new", GTK_ICON_SIZE_MENU, "<Add new path>"); treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view")); gtk_tree_view_expand_all(treeview); gtk_tree_selection_set_select_function( gtk_tree_view_get_selection(treeview), __collection_select, NULL, NULL); } void gui_collection_idle_enable() { GtkWidget *progress = gui_builder_widget("o_idle_progress"); gtk_widget_show(progress); g_idle_add(__collection_on_idle, progress); }