/** * seahorse_util_uri_unique: * @uri: The uri to guarantee is unique * * Creates a URI based on @uri that does not exist. * A simple numbering scheme is used to create new * URIs. Not meant for temp file creation. * * Returns: Newly allocated unique URI. **/ gchar* seahorse_util_uri_unique (const gchar* uri) { gchar* suffix; gchar* prefix; gchar* uri_try; gchar* x; guint len; int i; /* Simple when doesn't exist */ if (!seahorse_util_uri_exists (uri)) return g_strdup (uri); prefix = g_strdup (uri); len = strlen (prefix); /* Always take off a slash at end */ g_return_val_if_fail (len > 1, g_strdup (uri)); if (prefix[len - 1] == '/') prefix[len - 1] = 0; /* Split into prefix and suffix */ suffix = strrchr (prefix, '.'); x = strrchr(uri, '/'); if (suffix == NULL || (x != NULL && suffix < x)) { suffix = g_strdup (""); } else { x = suffix; suffix = g_strdup (suffix); *x = 0; } for (i = 1; i < 1000; i++) { uri_try = g_strdup_printf ("%s-%d%s", prefix, i, suffix); if (!seahorse_util_uri_exists (uri_try)) break; g_free (uri_try); uri_try = NULL; } g_free (suffix); g_free (prefix); return uri_try ? uri_try : g_strdup (uri); }
/** * seahorse_util_remove_suffix: * @path: Path with a suffix * @prompt:Overwrite prompt text * * Removes a suffix from @path. Does not check if @path actually has a suffix. * * Returns: @path without a suffix. NULL if prompt cancelled **/ gchar* seahorse_util_remove_suffix (const gchar *path, const gchar *prompt) { GtkWidget *dialog; gchar *uri; gchar *t; g_return_val_if_fail (path != NULL, NULL); uri = g_strndup (path, strlen (path) - 4); if (prompt && uri && seahorse_util_uri_exists (uri)) { t = g_strdup_printf (prompt, seahorse_util_uri_get_last (uri)); dialog = seahorse_util_chooser_save_new (t, NULL); g_free (t); seahorse_util_chooser_show_key_files (dialog); gtk_file_chooser_select_uri (GTK_FILE_CHOOSER (dialog), uri); g_free (uri); uri = NULL; uri = seahorse_util_chooser_save_prompt (dialog); } return uri; }
/** * seahorse_util_add_suffix: * @ctx: Gpgme Context * @path: Path of an existing file * @suffix: Suffix type * @prompt: Overwrite prompt text * * Constructs a new path for a file based on @path plus a suffix determined by * @suffix. If ASCII Armor is enabled, the suffix will be '.asc'. Otherwise the * suffix will be '.pgp' if @suffix is %SEAHORSE_CRYPT_SUFFIX or '.sig' if * @suffix is %SEAHORSE_SIG_SUFFIX. * * Returns: A new path with the suffix appended to @path. NULL if prompt cancelled **/ gchar* seahorse_util_add_suffix (const gchar *path, SeahorseSuffix suffix, const gchar *prompt) { GtkWidget *dialog; const gchar *ext; gchar *uri; gchar *t; if (suffix == SEAHORSE_CRYPT_SUFFIX) ext = SEAHORSE_EXT_PGP; else ext = SEAHORSE_EXT_SIG; uri = g_strdup_printf ("%s%s", path, ext); if (prompt && uri && seahorse_util_uri_exists (uri)) { t = g_strdup_printf (prompt, seahorse_util_uri_get_last (uri)); dialog = seahorse_util_chooser_save_new (t, NULL); g_free (t); seahorse_util_chooser_show_key_files (dialog); gtk_file_chooser_select_uri (GTK_FILE_CHOOSER (dialog), uri); g_free (uri); uri = NULL; uri = seahorse_util_chooser_save_prompt (dialog); } return uri; }
gchar* seahorse_util_chooser_save_prompt (GtkWidget *dialog) { GtkWidget* edlg; gchar *uri = NULL; while (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER (dialog)); if (uri == NULL) continue; if (seahorse_util_uri_exists (uri)) { edlg = gtk_message_dialog_new_with_markup (GTK_WINDOW (dialog), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("<b>A file already exists with this name.</b>\n\nDo you want to replace it with a new file?")); gtk_dialog_add_buttons (GTK_DIALOG (edlg), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, _("_Replace"), GTK_RESPONSE_ACCEPT, NULL); gtk_dialog_set_default_response (GTK_DIALOG (edlg), GTK_RESPONSE_CANCEL); if (gtk_dialog_run (GTK_DIALOG (edlg)) != GTK_RESPONSE_ACCEPT) { g_free (uri); uri = NULL; } gtk_widget_destroy (edlg); } if (uri != NULL) break; } gtk_widget_destroy (dialog); return uri; }
static gboolean verify_start (SeahorseToolMode *mode, const gchar *uri, gpgme_data_t uridata, SeahorsePGPOperation *pop, GError **err) { gpgme_data_t plain; gpgme_error_t gerr; gchar *original, *unesc_uri; /* File to decrypt to */ original = seahorse_util_remove_suffix (uri, NULL); /* The original file doesn't exist, prompt for it */ if (!seahorse_util_uri_exists (original)) { GtkWidget *dialog; gchar *t; unesc_uri = g_uri_unescape_string (seahorse_util_uri_get_last (uri), NULL); t = g_strdup_printf (_("Choose Original File for '%s'"), unesc_uri); dialog = gtk_file_chooser_dialog_new (t, NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); g_free (unesc_uri); g_free (t); gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (dialog), original); gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (dialog), FALSE); g_free (original); original = NULL; seahorse_tool_progress_block (TRUE); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) original = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog)); seahorse_tool_progress_block (FALSE); gtk_widget_destroy (dialog); } if (!original) return FALSE; g_object_set_data_full (G_OBJECT (pop), "original-file", original, g_free); /* Open necessary files, release with operation */ plain = seahorse_vfs_data_create (original, SEAHORSE_VFS_READ, err); if (!plain) return FALSE; g_object_set_data_full (G_OBJECT (pop), "plain-data", plain, (GDestroyNotify)gpgme_data_release); /* Start actual verify */ gerr = gpgme_op_verify_start (pop->gctx, uridata, plain, NULL); if (gerr != 0) { seahorse_util_gpgme_to_error (gerr, err); return FALSE; } return TRUE; }