/* * Write current entry */ void write_current_entry() { section_data *curr_section = NULL; entry_data *curr_entry = NULL; book_data *curr_book = NULL; // Assert master exists g_assert_nonnull(master); // If no current selection then // Return without warning curr_book = master->curr_book; if(curr_book == NULL) return; curr_section = curr_book->curr_section; if(curr_section == NULL) return; curr_entry = curr_section->curr_entry; if(curr_entry == NULL) return; sn_trace("< Writing current entry [%s/%s].", curr_section->name, curr_entry->name); write_text(curr_book, curr_entry); if(curr_entry->need_rename == TRUE) { curr_entry->need_rename = FALSE; if(options.auto_name_entry == TRUE) rename_entry(); } return; } // Write current entry
/* Randomly select something to do with a directory entry */ static void operate_on_entry(struct dir_entry_info *entry) { /* 1 time in 1000 rename */ if (tests_random_no(1000) == 0) { rename_entry(entry); return; } if (entry->type == 's') { symlink_check(entry->entry.symlink); /* If shrinking, 1 time in 50, remove a symlink */ if (shrink && tests_random_no(50) == 0) symlink_remove(entry->entry.symlink); return; } if (entry->type == 'd') { /* If shrinking, 1 time in 50, remove a directory */ if (shrink && tests_random_no(50) == 0) { dir_remove(entry->entry.dir); return; } operate_on_dir(entry->entry.dir); } if (entry->type == 'f') { /* If shrinking, 1 time in 10, remove a file */ if (shrink && tests_random_no(10) == 0) { file_delete(entry->entry.file); return; } /* If not growing, 1 time in 10, unlink a file with links > 1 */ if (!grow && entry->entry.file->link_count > 1 && tests_random_no(10) == 0) { file_unlink_file(entry->entry.file); return; } operate_on_file(entry->entry.file); } }
/* * 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