char *list_get_selected_digest(const enum hash_func_e id) { GList *rows = gtk_tree_selection_get_selected_rows(gui.treeselection, &gui.treemodel); // Should only have one row selected g_assert(!rows->next); GtkTreePath *path = rows->data; unsigned int row = *gtk_tree_path_get_indices(path); gtk_tree_path_free(path); g_list_free(rows); return list_get_digest(row, id); }
void check_file_save(const char * const filename) { g_assert(filename); GString *string = g_string_sized_new(1024); for (int i = 0; i < HASH_FUNCS_N; i++) { if (!hash.funcs[i].enabled) continue; switch (gui.view) { case GUI_VIEW_FILE: { const bool hmac_active = gtk_toggle_button_get_active( gui.togglebutton_hmac_file); const char *digest = gtk_entry_get_text( gui.hash_widgets[i].entry_file); if (digest && *digest) { g_string_append_printf(string, (hmac_active && hash.funcs[i].hmac_supported) ? "# HMAC-%s\n" : "# %s\n", hash.funcs[i].name); } else continue; char *path = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(gui.filechooserbutton)); char *basename = g_path_get_basename(path); g_free(path); g_string_append_printf(string, "%s %s\n", gtk_entry_get_text(gui.hash_widgets[i].entry_file), basename); g_free(basename); break; } case GUI_VIEW_TEXT: { const bool hmac_active = gtk_toggle_button_get_active( gui.togglebutton_hmac_text); g_string_append_printf(string, (hmac_active && hash.funcs[i].hmac_supported) ? "# HMAC-%s\n" : "# %s\n", hash.funcs[i].name); g_string_append_printf(string, "%s \"%s\"\n", gtk_entry_get_text(gui.hash_widgets[i].entry_text), gtk_entry_get_text(gui.entry_text)); break; } case GUI_VIEW_FILE_LIST: { int prev = -1; for (unsigned int row = 0; row < list.rows; row++) { char *digest = list_get_digest(row, i); if (digest && *digest) { if (i != prev) g_string_append_printf(string, "# %s\n", hash.funcs[i].name); prev = i; } else { if (digest) g_free(digest); prev = i; continue; } char *uri = list_get_uri(row); char *basename = g_filename_display_basename(uri); g_string_append_printf(string, "%s %s\n", digest, basename); g_free(basename); g_free(uri); g_free(digest); } break; } default: g_assert_not_reached(); } } char *data = g_string_free(string, false); g_file_set_contents(filename, data, -1, NULL); g_free(data); }
static void on_menuitem_save_as_activate(void) { GtkFileChooser *chooser = GTK_FILE_CHOOSER( gtk_file_chooser_dialog_new(_("Save Digests"), gui.window, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL)); gtk_file_chooser_set_do_overwrite_confirmation(chooser, true); if (gtk_dialog_run(GTK_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT) { char *filename = gtk_file_chooser_get_filename(chooser); GString *string = g_string_sized_new(1024); for (int i = 0; i < HASH_FUNCS_N; i++) { if (!hash.funcs[i].enabled) continue; switch (gui_get_view()) { case GUI_VIEW_FILE: { const bool hmac_active = gtk_toggle_button_get_active( gui.togglebutton_hmac_file); const char *digest = gtk_entry_get_text( gui.hash_widgets[i].entry_file); if (digest && *digest) { g_string_append_printf(string, (hmac_active && (hash.funcs[i].block_size > 0)) ? "# HMAC-%s\n" : "# %s\n", hash.funcs[i].name); } else continue; char *path = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(gui.filechooserbutton)); char *basename = g_path_get_basename(path); g_free(path); g_string_append_printf(string, "%s %s\n", gtk_entry_get_text(gui.hash_widgets[i].entry_file), basename); g_free(basename); break; } case GUI_VIEW_TEXT: { const bool hmac_active = gtk_toggle_button_get_active( gui.togglebutton_hmac_text); g_string_append_printf(string, (hmac_active && (hash.funcs[i].block_size > 0)) ? "# HMAC-%s\n" : "# %s\n", hash.funcs[i].name); g_string_append_printf(string, "%s \"%s\"\n", gtk_entry_get_text(gui.hash_widgets[i].entry_text), gtk_entry_get_text(gui.entry_text)); break; } case GUI_VIEW_FILE_LIST: { int prev = -1; for (unsigned int row = 0; row < list_count_rows(); row++) { char *digest = list_get_digest(row, i); if (digest && *digest) { if (i != prev) g_string_append_printf(string, "# %s\n", hash.funcs[i].name); prev = i; } else { if (digest) g_free(digest); prev = i; continue; } char *uri = list_get_uri(row); char *basename = g_filename_display_basename(uri); g_string_append_printf(string, "%s %s\n", digest, basename); g_free(basename); g_free(uri); g_free(digest); } break; } default: g_assert_not_reached(); } } char *data = g_string_free(string, false); g_file_set_contents(filename, data, -1, NULL); g_free(data); g_free(filename); } gtk_widget_destroy(GTK_WIDGET(chooser)); }