aboutsummaryrefslogtreecommitdiffstats
path: root/misc/gtk/gtk_ui.cpp
blob: 515fbc519042bdf2fc6e81725db9111a50abbfc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*************************************************
* GTK+ User Interface Source File                *
*************************************************/

#include "gtk_ui.h"

/*************************************************
* GTK+ Callback                                  *
*************************************************/
void GTK_UI::callback(GtkWidget* entry, gpointer passphrase_ptr)
   {
   const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
   char* passphrase = (char*)passphrase_ptr;
   strcpy(passphrase, entry_text);
   }

/*************************************************
* Get a passphrase from the user                 *
*************************************************/
std::string GTK_UI::get_passphrase(const std::string& what,
                                   const std::string& source,
                                   UI_Result& result) const
   {
   std::string msg = "A passphrase is needed to access the " + what;
   if(source != "") msg += "\nin " + source;
   return get_passphrase(msg, result);
   }

/*************************************************
* Get a passphrase from the user                 *
*************************************************/
std::string GTK_UI::get_passphrase(const std::string& label_text,
                                   UI_Result& result) const
   {
   const int MAX_PASSPHRASE = 64;

   GtkDialogFlags flags =
      (GtkDialogFlags)(GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL);

   GtkWidget* dialog = gtk_dialog_new_with_buttons(
      "Enter Passphrase",
      NULL, flags,
      GTK_STOCK_OK, GTK_RESPONSE_OK,
      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
      NULL);

   gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);

   GtkWidget* label = gtk_label_new(label_text.c_str());

   GtkWidget* entry = gtk_entry_new();
   gtk_entry_set_visibility(GTK_ENTRY(entry), 0);
   gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
   gtk_entry_set_max_length(GTK_ENTRY(entry), MAX_PASSPHRASE);

   char passphrase_buf[MAX_PASSPHRASE + 1] = { 0 };

   gtk_signal_connect(GTK_OBJECT(entry), "activate",
                      GTK_SIGNAL_FUNC(callback), passphrase_buf);

   GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);

   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox, TRUE, TRUE, 0);
   gtk_widget_show_all(vbox);

   /* Block until we get something back */
   result = CANCEL_ACTION;
   if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
      result = OK;

   gtk_widget_destroy(dialog);

   if(result == OK)
      return std::string(passphrase_buf);
   return "";
   }