Browse Source

Allow to choose the output filename.

Add a button to fire up a file chooser dialog to select the name
of the encrypted or signed file to write.
master
parent
commit
b1376efc61
  1. 69
      src/encrypt.c
  2. 63
      src/yki-encrypt.ui

69
src/encrypt.c

@ -32,7 +32,7 @@
/* GUI stuff. */
/* Context structure and associated functions. */
typedef struct {
gpgme_ctx_t gpgme;
@ -45,10 +45,34 @@ typedef struct {
gboolean detached;
gboolean armored;
const char *original_filename;
char *output_filename;
gboolean output_filename_userset;
GtkWidget *ok_button;
GtkWidget *detached_button;
GtkWidget *output_button;
} yki_encrypt_data_t;
static void
set_default_output_filename(yki_encrypt_data_t *yki, const char *original)
{
if ( yki->output_filename_userset )
return;
if ( original )
yki->original_filename = original;
g_free(yki->output_filename);
yki->output_filename = g_strdup_printf("%s.%s", yki->original_filename, yki->armored ? "asc" : "gpg");
gtk_button_set_label(GTK_BUTTON(yki->output_button), yki->output_filename);
}
/* GUI stuff. */
static void
update_buttons(yki_encrypt_data_t *yki)
{
@ -86,6 +110,7 @@ static void
armored_toggled(GtkWidget *widget, gpointer data)
{
((yki_encrypt_data_t *)data)->armored = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
set_default_output_filename((yki_encrypt_data_t *)data, NULL);
}
static void
@ -108,6 +133,34 @@ public_key_toggled(GtkCellRendererToggle *renderer,
update_buttons(yki);
}
static void
output_clicked(GtkButton *button, gpointer data)
{
yki_encrypt_data_t *yki = (yki_encrypt_data_t *)data;
GtkWidget *dlg;
char *filename;
dlg = gtk_file_chooser_dialog_new("Save as",
NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
"_Cancel", GTK_RESPONSE_CANCEL,
"_OK", GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dlg), yki->output_filename);
filename = g_path_get_basename(yki->output_filename);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dlg), filename);
if ( gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT ) {
g_free(yki->output_filename);
yki->output_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
yki->output_filename_userset = TRUE;
gtk_button_set_label(GTK_BUTTON(yki->output_button), yki->output_filename);
}
gtk_widget_destroy(dlg);
g_free(filename);
}
static gboolean
public_key_search(GtkTreeModel *model,
gint column,
@ -139,6 +192,8 @@ create_encrypt_dialog(yki_encrypt_data_t *yki)
"clicked", G_CALLBACK(detach_sign_toggled), yki);
g_signal_connect(gtk_builder_get_object(builder, "chkArmor"),
"clicked", G_CALLBACK(armored_toggled), yki);
g_signal_connect(gtk_builder_get_object(builder, "btnOutput"),
"clicked", G_CALLBACK(output_clicked), yki);
widget = gtk_builder_get_object(builder, "trvPublicKeys");
yki->public_keys = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));
@ -156,6 +211,9 @@ create_encrypt_dialog(yki_encrypt_data_t *yki)
yki->ok_button = GTK_WIDGET(gtk_builder_get_object(builder, "btnOK"));
yki->detached_button = GTK_WIDGET(gtk_builder_get_object(builder, "chkDetachSign"));
yki->output_button = GTK_WIDGET(gtk_builder_get_object(builder, "btnOutput"));
gtk_button_set_always_show_image(GTK_BUTTON(yki->output_button), TRUE);
update_buttons(yki);
@ -299,11 +357,8 @@ yki_encrypt(gpgme_ctx_t gpgme, const char *file, GError **error)
ret = gpgutil_open_file(file, "r", &in, error);
if ( ret ) {
outname = g_strdup_printf("%s.%s", file, yki.armored ? "asc" : "gpg");
ret = gpgutil_open_file(outname, "w", &out, error);
g_free(outname);
}
if ( ret )
ret = gpgutil_open_file(yki.output_filename, "w", &out, error);
if ( ret )
ret = do_encrypt(&yki, in.buffer, out.buffer, error);
@ -315,5 +370,7 @@ yki_encrypt(gpgme_ctx_t gpgme, const char *file, GError **error)
gtk_tree_model_foreach(GTK_TREE_MODEL(yki.public_keys), free_key_in_list, NULL);
gtk_widget_destroy(GTK_WIDGET(dlg));
g_free(yki.output_filename);
return ret;
}

63
src/yki-encrypt.ui

@ -2,6 +2,11 @@
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-save-as</property>
</object>
<object class="GtkListStore" id="lstPublicKeys">
<columns>
<!-- column-name Selected -->
@ -225,12 +230,60 @@
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkCheckButton" id="chkArmor">
<property name="label" translatable="yes">Armored output</property>
<object class="GtkVBox" id="vbox4">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkCheckButton" id="chkArmor">
<property name="label" translatable="yes">Armored output</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Output file: </property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnOutput">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">image1</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>

Loading…
Cancel
Save