/* * Shift entry up */ gboolean shift_entry_up() { GtkTreePath *entry_path = NULL; GtkTreeView *entry_view = NULL; GtkTreeModel *entry_model = NULL; GtkTreeSelection *entry_selection = NULL; GtkTreeViewColumn *entry_column = NULL; GtkTreeIter entry_iter; book_data *book = NULL; section_data *section = NULL; entry_data *entry = NULL; guint entry_index = 0; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); // Get entry model and view entry_view = get_entry_view(book); entry_model = gtk_tree_view_get_model(entry_view); sn_trace0("Shifting entry up."); // Make sure we are not at the top gtk_tree_view_get_cursor (entry_view, &entry_path, &entry_column); if(entry_path != NULL) { if(gtk_tree_path_prev(entry_path)) { // Get the entry to move up entry_selection = gtk_tree_view_get_selection(entry_view); if(gtk_tree_selection_get_selected (entry_selection, &entry_model, &entry_iter)) { gtk_tree_model_get(entry_model, &entry_iter, ENTRY_ITEM, &entry, END_OF_LIST); // Move entry up if(entry != NULL) { entry_index = g_list_index(section->entry_list, entry); section->entry_list = g_list_remove(section->entry_list, entry); section->entry_list = g_list_insert (section->entry_list, entry, --entry_index); // Write book write_book(book, note_dir); } } // Populate entries populate_entries(book, section); gtk_tree_view_set_cursor (entry_view, entry_path, entry_column, FALSE); } gtk_tree_path_free(entry_path); } return TRUE; } // Shift entry up
void setup(void) { WT_CONNECTION *conn; WT_SESSION *session; int ret; char config[512]; if ((ret = system("rm -f WiredTiger* *.bf")) != 0) testutil_die(ret, "system cleanup call failed"); /* * This test doesn't test public Wired Tiger functionality, it still * needs connection and session handles. */ /* * Open configuration -- put command line configuration options at the * end so they can override "standard" configuration. */ snprintf(config, sizeof(config), "create,error_prefix=\"%s\",cache_size=%" PRIu32 "MB,%s", g.progname, g.c_cache, g.config_open == NULL ? "" : g.config_open); testutil_check(wiredtiger_open(NULL, NULL, config, &conn)); testutil_check(conn->open_session(conn, NULL, NULL, &session)); g.wt_conn = conn; g.wt_session = session; populate_entries(); }
/** * Attempt to access PCI subsystem using Linux's sysfs interface. */ _pci_hidden int pci_system_linux_sysfs_create( void ) { int err = 0; struct stat st; /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem * can be accessed using this interface. */ if ( stat( SYS_BUS_PCI, & st ) == 0 ) { pci_sys = calloc( 1, sizeof( struct pci_system ) ); if ( pci_sys != NULL ) { pci_sys->methods = & linux_sysfs_methods; #ifdef HAVE_MTRR pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY | O_CLOEXEC); #endif err = populate_entries(pci_sys); } else { err = ENOMEM; } } else { err = errno; } return err; }
void on_action_entryDelete_activate(GtkAction *action, gpointer user_data) { struct s_gui_data *gui_data = (struct s_gui_data *)user_data; GtkTreeSelection *selection; GtkTreeModel *model; GtkTreeIter iter; selection = gtk_tree_view_get_selection(gui_data->treeviewEntries); if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gchar *path; gtk_tree_model_get (model, &iter, COL_ENTRY_PATH, &path, -1); monconf_entry *entry = monconf_entry_get_by_path(gui_data->conf, path); GtkWidget *dialog; dialog = gtk_message_dialog_new(GTK_WINDOW(gui_data->windowMain), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Remove the entry '%s'?",path); gtk_window_set_title(GTK_WINDOW(dialog), "Remove Entry"); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_YES) { monconf_remove_entry(gui_data->conf, entry); populate_entries(gui_data); } gtk_widget_destroy(dialog); g_free(path); } }
static void test_slice_hash_table() { const test_entry test_entries[] = { {"key_0", "value_0"}, {"key_1", "value_1"}, {"key_2", "value_2"}, {"key_3", "value_3"}, {"key_4", "value_4"}, {"key_5", "value_5"}, {"key_6", "value_6"}, {"key_7", "value_7"}, {"key_8", "value_8"}, {"key_9", "value_9"}, {"key_10", "value_10"}, {"key_11", "value_11"}, {"key_12", "value_12"}, {"key_13", "value_13"}, {"key_14", "value_14"}, {"key_15", "value_15"}, {"key_16", "value_16"}, {"key_17", "value_17"}, {"key_18", "value_18"}, {"key_19", "value_19"}, {"key_20", "value_20"}, {"key_21", "value_21"}, {"key_22", "value_22"}, {"key_23", "value_23"}, {"key_24", "value_24"}, {"key_25", "value_25"}, {"key_26", "value_26"}, {"key_27", "value_27"}, {"key_28", "value_28"}, {"key_29", "value_29"}, {"key_30", "value_30"}, {"key_31", "value_31"}, {"key_32", "value_32"}, {"key_33", "value_33"}, {"key_34", "value_34"}, {"key_35", "value_35"}, {"key_36", "value_36"}, {"key_37", "value_37"}, {"key_38", "value_38"}, {"key_39", "value_39"}, {"key_40", "value_40"}, {"key_41", "value_41"}, {"key_42", "value_42"}, {"key_43", "value_43"}, {"key_44", "value_44"}, {"key_45", "value_45"}, {"key_46", "value_46"}, {"key_47", "value_47"}, {"key_48", "value_48"}, {"key_49", "value_49"}, {"key_50", "value_50"}, {"key_51", "value_51"}, {"key_52", "value_52"}, {"key_53", "value_53"}, {"key_54", "value_54"}, {"key_55", "value_55"}, {"key_56", "value_56"}, {"key_57", "value_57"}, {"key_58", "value_58"}, {"key_59", "value_59"}, {"key_60", "value_60"}, {"key_61", "value_61"}, {"key_62", "value_62"}, {"key_63", "value_63"}, {"key_64", "value_64"}, {"key_65", "value_65"}, {"key_66", "value_66"}, {"key_67", "value_67"}, {"key_68", "value_68"}, {"key_69", "value_69"}, {"key_70", "value_70"}, {"key_71", "value_71"}, {"key_72", "value_72"}, {"key_73", "value_73"}, {"key_74", "value_74"}, {"key_75", "value_75"}, {"key_76", "value_76"}, {"key_77", "value_77"}, {"key_78", "value_78"}, {"key_79", "value_79"}, {"key_80", "value_80"}, {"key_81", "value_81"}, {"key_82", "value_82"}, {"key_83", "value_83"}, {"key_84", "value_84"}, {"key_85", "value_85"}, {"key_86", "value_86"}, {"key_87", "value_87"}, {"key_88", "value_88"}, {"key_89", "value_89"}, {"key_90", "value_90"}, {"key_91", "value_91"}, {"key_92", "value_92"}, {"key_93", "value_93"}, {"key_94", "value_94"}, {"key_95", "value_95"}, {"key_96", "value_96"}, {"key_97", "value_97"}, {"key_98", "value_98"}, {"key_99", "value_99"}, }; const size_t num_entries = GPR_ARRAY_SIZE(test_entries); // Construct table. grpc_slice_hash_table_entry* entries = gpr_zalloc(sizeof(*entries) * num_entries); populate_entries(test_entries, num_entries, entries); grpc_slice_hash_table* table = grpc_slice_hash_table_create(num_entries, entries, destroy_string); gpr_free(entries); // Check contents of table. check_values(test_entries, num_entries, table); check_non_existent_value("XX", table); // Clean up. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_slice_hash_table_unref(&exec_ctx, table); grpc_exec_ctx_finish(&exec_ctx); }
void on_action_entryDuplicate_activate(GtkAction *action, gpointer user_data) { GtkTreeSelection *selection; GtkTreeModel *model; GtkTreeIter iter; monconf_entry *dupl_entry; struct s_gui_data *gui_data = (struct s_gui_data *)user_data; selection = gtk_tree_view_get_selection(gui_data->treeviewEntries); if (gtk_tree_selection_get_selected(selection, &model, &iter)) { gchar *path; gtk_tree_model_get (model, &iter, COL_ENTRY_PATH, &path, -1); monconf_entry *entry = monconf_entry_get_by_path(gui_data->conf, path); dupl_entry = monconf_entry_duplicate(gui_data->conf, entry); populate_entries(gui_data); } }
int main (int argc, char *argv[]) { char *cwd; char *glade_path; struct s_gui_data data; config_args args; args.config_path = NULL; monconf_parse_cli_args(&args, argc, argv); monconf_prepare_config_directory(); monconf *conf = monconf_create(); monconf_read_config(conf, args.config_path); monconf_load_available_actions(conf); data.rstart.active = 0; data.lstart.active = 0; gtk_init (&argc, &argv); data.builder = gtk_builder_new (); glade_path = monconf_resolve_path("ui-glade/monarqui_gui.glade"); gtk_builder_add_from_file (data.builder, glade_path, NULL); g_free(glade_path); data.builder = GTK_BUILDER(data.builder); data.windowMain = GTK_WIDGET(gtk_builder_get_object (data.builder, "windowMain")); data.windowEntry = GTK_WIDGET(gtk_builder_get_object (data.builder, "windowEntry")); data.windowConfig = GTK_WIDGET(gtk_builder_get_object (data.builder, "windowConfig")); data.windowAction = GTK_WIDGET(gtk_builder_get_object (data.builder, "windowAction")); data.action_mainExit = GTK_ACTION(gtk_builder_get_object(data.builder, "action_mainExit")); data.action_configOpen = GTK_ACTION(gtk_builder_get_object(data.builder, "action_configOpen")); data.action_configClose = GTK_ACTION(gtk_builder_get_object(data.builder, "action_configClose")); data.action_startPause = GTK_ACTION(gtk_builder_get_object(data.builder, "action_startPause")); data.action_entryAdd = GTK_ACTION(gtk_builder_get_object(data.builder, "action_entryAdd")); data.action_entryModify = GTK_ACTION(gtk_builder_get_object(data.builder, "action_entryModify")); data.action_entryDuplicate = GTK_ACTION(gtk_builder_get_object(data.builder, "action_entryDuplicate")); data.action_entryDelete = GTK_ACTION(gtk_builder_get_object(data.builder, "action_entryDelete")); data.action_actionAdd = GTK_ACTION(gtk_builder_get_object(data.builder, "action_actionAdd")); data.action_actionModify = GTK_ACTION(gtk_builder_get_object(data.builder, "action_actionModify")); data.action_actionDelete = GTK_ACTION(gtk_builder_get_object(data.builder, "action_actionDelete")); data.action_saveConfig = GTK_ACTION(gtk_builder_get_object(data.builder, "action_saveConfig")); data.action_toggleApplication = GTK_ACTION(gtk_builder_get_object(data.builder, "action_toggleApplication")); data.action_systrayPopup = GTK_ACTION(gtk_builder_get_object(data.builder, "action_systrayPopup")); data.image_startStop = GTK_IMAGE(gtk_builder_get_object(data.builder,"image_startStop")); data.systrayIcon = GTK_STATUS_ICON(gtk_builder_get_object(data.builder,"systrayIcon")); data.args = &args; data.conf = conf; data.treeviewActions = GTK_TREE_VIEW(gtk_builder_get_object(data.builder,"treeviewActions")); data.treeviewEntries = GTK_TREE_VIEW(gtk_builder_get_object(data.builder,"treeviewEntries")); data.treeviewEntryActions = GTK_TREE_VIEW(gtk_builder_get_object(data.builder,"treeviewEntryActions")); data.listStoreActions = GTK_LIST_STORE(gtk_builder_get_object(data.builder,"listStoreActions")); data.listStoreEntries = GTK_LIST_STORE(gtk_builder_get_object(data.builder,"listStoreEntries")); data.listStoreEntryActions = GTK_LIST_STORE(gtk_builder_get_object(data.builder,"listStoreEntryActions")); data.image_started = GTK_IMAGE(gtk_builder_get_object(data.builder,"image_started")); data.image_started_event = GTK_IMAGE(gtk_builder_get_object(data.builder,"image_started_event")); data.image_stopped = GTK_IMAGE(gtk_builder_get_object(data.builder,"image_stopped")); data.logged_event = FALSE; data.textbufferLog = GTK_TEXT_BUFFER(gtk_builder_get_object(data.builder, "textbufferLog")); data.systrayMenu = GTK_MENU(gtk_menu_new()); data.systrayMenuItem_startStop = GTK_MENU_ITEM(gtk_menu_item_new_with_label("Start")); data.systrayMenuItem_quit = GTK_MENU_ITEM(gtk_menu_item_new_with_label("Quit")); gtk_menu_shell_append(GTK_MENU_SHELL(data.systrayMenu), GTK_WIDGET(data.systrayMenuItem_startStop)); gtk_menu_shell_append(GTK_MENU_SHELL(data.systrayMenu), GTK_WIDGET(data.systrayMenuItem_quit)); gtk_action_set_label(data.action_startPause, "Start"); gtk_builder_connect_signals (data.builder, (gpointer)&data); gtk_image_set_from_icon_name(data.image_startStop, ICON_NAME_START, GTK_ICON_SIZE_BUTTON); gtk_status_icon_set_from_pixbuf(data.systrayIcon, gtk_image_get_pixbuf(data.image_stopped)); populate_entries(&data); populate_actions(&data); gtk_widget_show (data.windowMain); gtk_main (); g_object_unref (G_OBJECT (data.builder)); free(args.config_path); return 0; }
void on_action_entrySave_activate(GtkAction *action, gpointer user_data) { struct s_gui_data *gui_data = (struct s_gui_data *)user_data; GList *list_item; monconf *conf = gui_data->conf; monconf_action_entry *action_entry; GtkTreeModel *model; GtkTreeIter iter; if(gui_data->curr_entry != NULL) { g_free(gui_data->curr_entry->file_name); } else { gui_data->curr_entry = monconf_new_entry(gui_data->conf); } monconf_entry_add_ignores_from_csv(gui_data->curr_entry, (char *)gtk_entry_get_text(GTK_ENTRY(gtk_builder_get_object(gui_data->builder,"entryIgnores")))); gui_data->curr_entry->file_name = g_strdup(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(gtk_builder_get_object(gui_data->builder,"filechooserPath")))); gui_data->curr_entry->recursive = (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxRecursive"))->toggle_button)) ? TRUE : FALSE); gui_data->curr_entry->events = (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnCreate"))->toggle_button)) ? MON_CREATE : 0) | (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnModify"))->toggle_button)) ? MON_MODIFY: 0) | (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnDelete"))->toggle_button)) ? MON_DELETE: 0) | (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnAttrib"))->toggle_button)) ? MON_ATTRIB: 0) | (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnMovedFrom"))->toggle_button)) ? MON_MOVED_FROM : 0) | (gtk_toggle_button_get_active(&(GTK_CHECK_BUTTON(gtk_builder_get_object(gui_data->builder,"checkboxOnMovedTo"))->toggle_button)) ? MON_MOVED_TO: 0); short int valid; gboolean enabled, event_create, event_modify, event_delete, event_attribs, event_move_from, event_move_to; gchar *action_name, *globs; model = gtk_tree_view_get_model(gui_data->treeviewEntryActions); valid = gtk_tree_model_get_iter_first(model, &iter); while(valid) { gboolean move_next = TRUE; gtk_tree_model_get(model, &iter, COL_ACTION_ENTRY_ENABLED, &enabled, COL_ACTION_ENTRY_NAME, &action_name, COL_ACTION_ENTRY_GLOBS, &globs, COL_ACTION_ENTRY_CREATE, &event_create, COL_ACTION_ENTRY_MODIFY, &event_modify, COL_ACTION_ENTRY_DELETE, &event_delete, COL_ACTION_ENTRY_ATTRIBS, &event_attribs, COL_ACTION_ENTRY_MOVED_FROM, &event_move_from, COL_ACTION_ENTRY_MOVED_TO, &event_move_to, -1); action_entry = monconf_action_entry_get_by_name(gui_data->curr_entry, action_name); if(!enabled) { if(action_entry) { monconf_entry_remove_action_entry(gui_data->curr_entry, action_entry); move_next=0; } } else { if(!action_entry) { action_entry = monconf_entry_new_action(gui_data->curr_entry); action_entry->action = monconf_action_get_by_name(conf, action_name); } monconf_action_entry_add_globs_from_csv(action_entry, globs); action_entry->events = (event_create ? MON_CREATE : 0) | (event_modify ? MON_MODIFY: 0) | (event_delete ? MON_DELETE : 0) | (event_attribs ? MON_ATTRIB : 0) | (event_move_from ? MON_MOVED_FROM : 0) | (event_move_to ? MON_MOVED_TO : 0); } g_free(action_name); g_free(globs); valid = gtk_tree_model_iter_next(model, &iter); } populate_entries(gui_data); }
/* * Move entry * Signal handler for "activate" move entry */ gboolean move_entry() { FILE *fp = NULL; GtkWidget *msg_dialog = NULL; GtkWidget *section_dialog = NULL; GtkWidget *section_view = NULL; GtkTreeModel *section_model = NULL; GtkTreeSelection *section_selection = NULL; GtkTreeIter section_iter; gchar entry_display_name[MAX_NAME_LEN]; gchar curr_entry_path[MAX_PATH_LEN]; gchar move_entry_path[MAX_PATH_LEN]; GList *entry_item = NULL; book_data *book = NULL; section_data *section = NULL; entry_data *entry = NULL; book_data *book_copy = NULL; section_data *section_copy = NULL; gint result; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); entry = get_current_entry_or_return_with_warning(); // Create section view section_view = gtk_tree_view_new(); // Create section dialog section_dialog = create_book_section_dialog(section_view, "Select the section to move the entry to..."); // Init section view init_book_section_view(GTK_TREE_VIEW(section_view)); // Create section entry model / store section_model = create_book_section_model(master); // Set section model on section view gtk_tree_view_set_model(GTK_TREE_VIEW(section_view), GTK_TREE_MODEL(section_model)); if(section_model != NULL) g_object_unref(section_model); // Show and run view section dialog gtk_widget_show_all(section_dialog); result = gtk_dialog_run(GTK_DIALOG(section_dialog)); switch (result) { case GTK_RESPONSE_ACCEPT: section_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(section_view)); if(gtk_tree_selection_get_selected(section_selection, §ion_model, §ion_iter)) { gtk_tree_model_get(section_model, §ion_iter, SECTION_ITEM, §ion_copy, END_OF_LIST); if(section == NULL) { gtk_widget_destroy(section_dialog); return FALSE; } } else { gtk_widget_destroy(section_dialog); return FALSE; } book_copy = section_copy->parent_book; // Curr entry file path g_snprintf(curr_entry_path, sizeof(curr_entry_path), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry->name); // Move entry file path g_snprintf(move_entry_path, sizeof(move_entry_path), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book_copy->name, G_DIR_SEPARATOR_S, section_copy->name, G_DIR_SEPARATOR_S, entry->name); // Set display name strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5); if(strlen(entry_display_name) > 25) strcpy(entry_display_name+25, "...\0"); // Check that new entry path is valid fp = fopen(move_entry_path, "wx"); if (fp == NULL) { sn_warning("Unable to move entry [%s].", move_entry_path); msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Unable to move entry \"%s\".", entry_display_name); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog), "Please ensure the entry name doesn't aleady exist."); gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name); gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE); result = gtk_dialog_run(GTK_DIALOG(msg_dialog)); gtk_widget_destroy(msg_dialog); gtk_widget_destroy(section_dialog); return FALSE; } fclose(fp); // Remove file created by previous open result = remove(move_entry_path); sn_trace("Moving entry [%s] to [%s].", curr_entry_path, move_entry_path); // Move the entry file to the new path result = rename(curr_entry_path, move_entry_path); if(result == 0) { // Remove history remove_history(); // Select next entry entry_item = g_list_find(section->entry_list, entry); entry_item = entry_item->next; if(entry_item != NULL) section->curr_entry = entry_item->data; else section->curr_entry = NULL; // Remove entry section->entry_list = g_list_remove(section->entry_list, entry); // Append entry section_copy->entry_list = g_list_append(section_copy->entry_list, entry); // Update entry entry->parent_section = section_copy; // Write book write_book(book, note_dir); // Update view populate_entries(book, section); gtk_widget_destroy(section_dialog); return TRUE; } else { gtk_widget_destroy(section_dialog); sn_warning("Unable to move entry [%s].", curr_entry_path); return FALSE; } gtk_widget_destroy(section_dialog); return FALSE; default: gtk_widget_destroy(section_dialog); return FALSE; } // End switch return FALSE; } // Move entry
/* * Rename entry * Signal handler for "activate" rename entry */ gboolean rename_entry() { FILE *fp = NULL; GtkWidget *name_dialog = NULL; GtkWidget *msg_dialog = NULL; GtkWidget *vbox = NULL; GtkWidget *hbox = NULL; GtkWidget *label = NULL; GtkWidget *name_entry = NULL; const gchar *entry_text = NULL; gchar old_entry_display_name[MAX_NAME_LEN]; gchar new_entry_display_name[MAX_NAME_LEN]; gchar new_entry_name[MAX_PATH_LEN]; gchar old_entry_name[MAX_PATH_LEN]; book_data *book = NULL; section_data *section = NULL; entry_data *entry = NULL; gchar *temp_string; gint result; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); entry = get_current_entry_or_return_with_warning(); // Write current entry write_current_entry(); // Create name dialog name_dialog = gtk_dialog_new_with_buttons(app_name, GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL); gtk_widget_set_size_request(name_dialog, SM_DLG_WIDTH, SM_DLG_HEIGHT); gtk_window_set_type_hint(GTK_WINDOW(name_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(name_dialog), FALSE); label = gtk_label_new("Enter new entry name..."); name_entry = gtk_entry_new(); gtk_entry_set_max_length(GTK_ENTRY(name_entry), MAX_NAME_LEN-5); // Get auto name if(options.auto_name_entry == TRUE) { temp_string = g_malloc0(MAX_NAME_LEN); strdelchr(temp_string, get_auto_entry_name(get_text_view(book)), ILLEGAL_CHARS); temp_string[MAX_NAME_LEN-5] = 0; if(strcmp(temp_string, "") == 0) gtk_entry_set_text(GTK_ENTRY(name_entry), entry->name); else gtk_entry_set_text(GTK_ENTRY(name_entry), temp_string); g_free(temp_string); } else { gtk_entry_set_text(GTK_ENTRY(name_entry), entry->name); } vbox = gtk_dialog_get_content_area(GTK_DIALOG(name_dialog)); hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5); gtk_entry_set_activates_default(GTK_ENTRY(name_entry), TRUE); gtk_dialog_set_default_response (GTK_DIALOG(name_dialog),GTK_RESPONSE_ACCEPT); gtk_widget_show_all(name_dialog); // Run dialog result = gtk_dialog_run(GTK_DIALOG(name_dialog)); switch (result) { case GTK_RESPONSE_ACCEPT: entry_text = gtk_entry_get_text(GTK_ENTRY(name_entry)); // Scan name for illegal characters temp_string = g_malloc0(MAX_NAME_LEN); strdelchr(temp_string, entry_text, ILLEGAL_CHARS); gtk_entry_set_text(GTK_ENTRY(name_entry), temp_string); g_free(temp_string); entry_text = gtk_entry_get_text(GTK_ENTRY(name_entry)); // Old entry file g_snprintf(old_entry_name, sizeof(old_entry_name), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry->name); // New entry file g_snprintf(new_entry_name, sizeof(new_entry_name), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry_text); // Ignore rename if names match if(strcmp(entry->name, entry_text) == 0) { gtk_widget_destroy(name_dialog); return TRUE; } // Set display name strncpy(old_entry_display_name, entry->name, MAX_NAME_LEN-5); if(strlen(old_entry_display_name) > 25) strcpy(old_entry_display_name+25, "...\0"); strncpy(new_entry_display_name, entry_text, MAX_NAME_LEN-5); if(strlen(new_entry_display_name) > 25) strcpy(new_entry_display_name+25, "...\0"); // Check that new entry name is valid if(strlen(entry_text) < 1) { sn_warning("Unable to rename entry [%s] to [%s].", entry->name, entry_text); msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Unable to rename entry \"%s\" to \"%s\".", old_entry_display_name, new_entry_display_name); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog), "Please ensure the entry name contains a valid character."); gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name); gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE); result = gtk_dialog_run(GTK_DIALOG(msg_dialog)); gtk_widget_destroy(name_dialog); gtk_widget_destroy(msg_dialog); return FALSE; } // Check that new entry name is valid fp = fopen(new_entry_name, "wx"); if (fp == NULL) { sn_warning("Unable to rename entry [%s] to [%s].", entry->name, entry_text); msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Unable to rename entry \"%s\" to \"%s\".", old_entry_display_name, new_entry_display_name); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog), "Please ensure the entry name doesn't aleady exist."); gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name); gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE); result = gtk_dialog_run(GTK_DIALOG(msg_dialog)); gtk_widget_destroy(name_dialog); gtk_widget_destroy(msg_dialog); return FALSE; } fclose(fp); sn_trace("Renaming entry [%s] to [%s].", entry->name, entry_text); // Rename entry file result = rename(old_entry_name, new_entry_name); if(result == 0) { // Remove history remove_history(); // Update entry strcpy(entry->name, entry_text); // Update book write_book(book, note_dir); // Update view populate_entries(book, section); on_entry_change(get_entry_view(book), book); gtk_widget_destroy(name_dialog); return TRUE; } else { sn_warning("Unable to rename entry [%s].", entry->name); gtk_widget_destroy(name_dialog); return FALSE; } default: gtk_widget_destroy(name_dialog); return FALSE; } // End switch return FALSE; } // Rename entry
/* * Create entry * Signal handler for "activate" create entry */ gboolean create_entry() { FILE *fp = NULL; gchar entry_name[MAX_NAME_LEN]; gchar filename[MAX_PATH_LEN]; GtkTreeModel *entry_model = NULL; GtkTreeView *entry_view = NULL; GtkTreeSelection *selection = NULL; GtkTreeIter tree_iter; GList *entry_item = NULL; GtkTreePath *tree_path = NULL; entry_data *curr_entry = NULL; book_data *book = NULL; section_data *section = NULL; entry_data *entry = NULL; gint cindex = 0; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); // Create new entry filename while (++cindex < MAX_TRIES) { // Create entry name g_snprintf(entry_name, sizeof(entry_name), "%s%d", default_entry_name, cindex); // Create entry text file g_snprintf(filename, sizeof(filename), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry_name); fp = fopen(filename, "wx"); if (fp == NULL) { continue; } fclose(fp); break; } sn_trace("Creating entry text file [%s].", filename); // Create entry entry = g_new0(entry_data, NEW_INSTANCE); strcpy(entry->name, entry_name); entry->parent_section = section; // Get selected entry entry_view = get_entry_view(book); entry_model = gtk_tree_view_get_model(entry_view); selection = gtk_tree_view_get_selection(entry_view); // Update model if(gtk_tree_selection_get_selected(selection, &entry_model, &tree_iter)) { gtk_tree_model_get(entry_model, &tree_iter, ENTRY_ITEM, &curr_entry, END_OF_LIST); tree_path = gtk_tree_model_get_path(entry_model, &tree_iter); entry_item = g_list_find(section->entry_list, curr_entry); section->entry_list = g_list_insert_before(section->entry_list, entry_item, entry); } else { section->entry_list = g_list_append(section->entry_list, entry); } // Write book write_book(book, note_dir); // Update view populate_entries(book, section); if(tree_path != NULL) gtk_tree_selection_select_path(selection, tree_path); on_entry_change(entry_view, book); // Set text focus set_text_view_focus(get_text_view(book)); // Name entry if(options.auto_name_entry == TRUE) { entry->need_rename = TRUE; } else { rename_entry(); } return TRUE; } // Create entry
/* * Delete entry * Signal handler for "activate" delete entry */ gboolean delete_entry() { GtkWidget *msg_dialog; gchar filename[MAX_PATH_LEN]; gchar entry_display_name[MAX_NAME_LEN]; GList *entry_item = NULL; book_data *book = NULL; section_data *section = NULL; entry_data *entry = NULL; gint result; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); entry = get_current_entry_or_return_with_warning(); // Set display name strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5); if(strlen(entry_display_name) > 25) strcpy(entry_display_name+25, "...\0"); // Confirm delete action msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Are you sure you want to delete\n\"%s\"?", entry_display_name); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog), "If you delete an entry, it is permanently lost."); gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name); gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE); result = gtk_dialog_run(GTK_DIALOG(msg_dialog)); switch (result) { case GTK_RESPONSE_YES: // Delete entry text file g_snprintf(filename, sizeof(filename), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry->name); sn_trace("Deleting entry [%s] in delete_entry().", filename); result = remove(filename); if (result == 0) { // Remove history remove_history(); // Select next entry entry_item = g_list_find(section->entry_list, entry); entry_item = entry_item->next; if(entry_item != NULL) section->curr_entry = entry_item->data; else section->curr_entry = NULL; // Remove entry section->entry_list = g_list_remove(section->entry_list, entry); free_entry(entry); // Write book write_book(book, note_dir); // Update view populate_entries(book, section); gtk_widget_grab_focus(GTK_WIDGET(get_entry_view(book))); gtk_widget_destroy(msg_dialog); return TRUE; } else { sn_warning("Unable to delete entry [%s].", entry->name); gtk_widget_destroy(msg_dialog); return FALSE; } default: gtk_widget_destroy(msg_dialog); return FALSE; } return FALSE; } // Delete entry
/* * Trash entry * Signal handler for "activate" trash entry */ gboolean trash_entry() { FILE *fp = NULL; GtkWidget *msg_dialog = NULL; gchar entry_display_name[MAX_NAME_LEN]; gchar curr_entry_path[MAX_PATH_LEN]; gchar trash_entry_path[MAX_PATH_LEN]; GList *entry_item = NULL; book_data *book = NULL; section_data *section = NULL; section_data *trash_section = NULL; entry_data *entry = NULL; gint result; // Assert master exists g_assert_nonnull(master); // Get currently selected book = get_current_book_or_return_with_warning(); section = get_current_section_or_return_with_warning(); entry = get_current_entry_or_return_with_warning(); // Get trash section trash_section = book->trash_section; // Curr entry file path g_snprintf(curr_entry_path, sizeof(curr_entry_path), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, section->name, G_DIR_SEPARATOR_S, entry->name); // Trash entry file path g_snprintf(trash_entry_path, sizeof(trash_entry_path), "%s%s%s%s%s%s%s.txt", note_dir, G_DIR_SEPARATOR_S, book->name, G_DIR_SEPARATOR_S, trash_section_name, G_DIR_SEPARATOR_S, entry->name); // Set display name strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5); if(strlen(entry_display_name) > 25) strcpy(entry_display_name+25, "...\0"); // Check that new entry path is valid fp = fopen(trash_entry_path, "wx"); if (fp == NULL) { sn_warning("Unable to trash entry [%s].", trash_entry_path); msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Unable to trash entry \"%s\".", entry_display_name); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog), "Please ensure the entry name doesn't aleady exist."); gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU); gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE); gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name); result = gtk_dialog_run(GTK_DIALOG(msg_dialog)); gtk_widget_destroy(msg_dialog); return FALSE; } fclose(fp); // Remove file created by previous open result = remove(trash_entry_path); sn_trace("Trashing entry [%s] to [%s].", curr_entry_path, trash_entry_path); // Move the entry file to the new path result = rename(curr_entry_path, trash_entry_path); if(result == 0) { // Remove history remove_history(); // Select next entry entry_item = g_list_find(section->entry_list, entry); entry_item = entry_item->next; if(entry_item != NULL) section->curr_entry = entry_item->data; else section->curr_entry = NULL; // Remove entry section->entry_list = g_list_remove(section->entry_list, entry); // Append entry trash_section->entry_list = g_list_append(trash_section->entry_list, entry); // Update entry entry->parent_section = trash_section; // Write book write_book(book, note_dir); // Update view populate_entries(book, section); return TRUE; } else { sn_warning("Unable to trash entry [%s].", curr_entry_path); return FALSE; } return FALSE; } // Trash entry