From 47ab19b3a06b6f96965482e4a33d443c2276c1d8 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 11 Mar 2017 12:13:06 +0100 Subject: UI to choose PGP key/disabled in account settings --- gpgme-vala/CMakeLists.txt | 4 +- gpgme-vala/src/fix.c | 3 - gpgme-vala/src/fix.h | 8 --- gpgme-vala/src/gpgme-helper.vala | 91 ------------------------------ gpgme-vala/src/gpgme_fix.c | 12 ++++ gpgme-vala/src/gpgme_fix.h | 12 ++++ gpgme-vala/src/gpgme_helper.vala | 90 +++++++++++++++++++++++++++++ gpgme-vala/vapi/gpgme.vapi | 2 +- gpgme-vala/vapi/gpgme_public.vapi | 9 +-- libdino/CMakeLists.txt | 11 ++-- libdino/data/manage_accounts/dialog.ui | 2 +- libdino/src/ui/manage_accounts/dialog.vala | 39 ++++++++++++- 12 files changed, 167 insertions(+), 116 deletions(-) delete mode 100644 gpgme-vala/src/fix.c delete mode 100644 gpgme-vala/src/fix.h delete mode 100644 gpgme-vala/src/gpgme-helper.vala create mode 100644 gpgme-vala/src/gpgme_fix.c create mode 100644 gpgme-vala/src/gpgme_fix.h create mode 100644 gpgme-vala/src/gpgme_helper.vala diff --git a/gpgme-vala/CMakeLists.txt b/gpgme-vala/CMakeLists.txt index 519b4ce7..a510d293 100644 --- a/gpgme-vala/CMakeLists.txt +++ b/gpgme-vala/CMakeLists.txt @@ -12,7 +12,7 @@ pkg_check_modules(GPGME_VALA REQUIRED ${GPGME_VALA_PACKAGES}) vala_precompile(GPGME_VALA_C SOURCES - "src/gpgme-helper.vala" + "src/gpgme_helper.vala" CUSTOM_VAPIS "${CMAKE_CURRENT_SOURCE_DIR}/vapi/gpgme.vapi" "${CMAKE_CURRENT_SOURCE_DIR}/vapi/gpgme_public.vapi" @@ -30,7 +30,7 @@ OPTIONS set(CFLAGS ${VALA_CFLAGS} ${GPGME_VALA_CFLAGS} ${GPGME_CFLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/src) add_definitions(${CFLAGS}) -add_library(gpgme-vala SHARED ${GPGME_VALA_C} src/fix.c) +add_library(gpgme-vala SHARED ${GPGME_VALA_C} src/gpgme_fix.c) target_link_libraries(gpgme-vala ${GPGME_VALA_LIBRARIES} ${GPGME_LIBRARIES}) add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/gpgme-vala/gpgme.vapi diff --git a/gpgme-vala/src/fix.c b/gpgme-vala/src/fix.c deleted file mode 100644 index 76aff720..00000000 --- a/gpgme-vala/src/fix.c +++ /dev/null @@ -1,3 +0,0 @@ -#include - -static GRecMutex gpgme_global_mutex = {0}; diff --git a/gpgme-vala/src/fix.h b/gpgme-vala/src/fix.h deleted file mode 100644 index 15ecd3ac..00000000 --- a/gpgme-vala/src/fix.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef GPGME_FIX -#define GPGME_FIX 1 - -#include - -static GRecMutex gpgme_global_mutex; - -#endif \ No newline at end of file diff --git a/gpgme-vala/src/gpgme-helper.vala b/gpgme-vala/src/gpgme-helper.vala deleted file mode 100644 index d2f3f7e6..00000000 --- a/gpgme-vala/src/gpgme-helper.vala +++ /dev/null @@ -1,91 +0,0 @@ -using Gee; -using GPG; - -namespace GPGHelper { - -public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error { - global_mutex.lock(); - Data plain_data = Data.create_from_memory(plain.data, false); - Context context = Context.create(); - context.set_armor(true); - Data enc_data = context.op_encrypt(keys, flags, plain_data); - global_mutex.unlock(); - return get_string_from_data(enc_data); -} - -public static string decrypt(string encr) throws GLib.Error { - global_mutex.lock(); - Data enc_data = Data.create_from_memory(encr.data, false); - Context context = Context.create(); - Data dec_data = context.op_decrypt(enc_data); - global_mutex.unlock(); - return get_string_from_data(dec_data); -} - -public static string sign(string plain, SigMode mode) throws GLib.Error { - global_mutex.lock(); - Data plain_data = Data.create_from_memory(plain.data, false); - Context context = Context.create(); - Data signed_data = context.op_sign(plain_data, mode); - global_mutex.unlock(); - return get_string_from_data(signed_data); -} - -public static string? get_sign_key(string signature, string? text) throws GLib.Error { - global_mutex.lock(); - Data sig_data = Data.create_from_memory(signature.data, false); - Data text_data; - if (text != null) { - text_data = Data.create_from_memory(text.data, false); - } else { - text_data = Data.create(); - } - Context context = Context.create(); - context.op_verify(sig_data, text_data); - VerifyResult* verify_res = context.op_verify_result(); - if (verify_res == null || verify_res.signatures == null) return null; - global_mutex.unlock(); - return verify_res.signatures.fpr; -} - -public static Gee.List get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error { - Gee.List keys = new ArrayList(); - Context context = Context.create(); - context.op_keylist_start(pattern, secret_only ? 1 : 0); - try { - while (true) { - Key key = context.op_keylist_next(); - keys.add(key); - } - } catch (Error e) { -// if (e.message != GPGError.ErrorCode.EOF.to_string()) throw e; - if (e.message != "EOF") throw e; - } - return keys; -} - -public static Key? get_public_key(string sig) throws GLib.Error { - global_mutex.lock(); - Context context = Context.create(); - Key key = context.get_key(sig, false); - global_mutex.unlock(); - return key; -} - -private static string get_string_from_data(Data data) { - data.seek(0); - uint8[] buf = new uint8[256]; - ssize_t? len = null; - string res = ""; - do { - len = data.read(buf); - if (len > 0) { - string part = (string) buf; - part = part.substring(0, (long) len); - res += part; - } - } while (len > 0); - return res; -} - -} \ No newline at end of file diff --git a/gpgme-vala/src/gpgme_fix.c b/gpgme-vala/src/gpgme_fix.c new file mode 100644 index 00000000..2bc139e9 --- /dev/null +++ b/gpgme-vala/src/gpgme_fix.c @@ -0,0 +1,12 @@ +#include + +static GRecMutex gpgme_global_mutex = {0}; + +gpgme_key_t gpgme_key_ref_vapi (gpgme_key_t key) { + gpgme_key_ref(key); + return key; +} +gpgme_key_t gpgme_key_unref_vapi (gpgme_key_t key) { + gpgme_key_unref(key); + return key; +} \ No newline at end of file diff --git a/gpgme-vala/src/gpgme_fix.h b/gpgme-vala/src/gpgme_fix.h new file mode 100644 index 00000000..3daa7db0 --- /dev/null +++ b/gpgme-vala/src/gpgme_fix.h @@ -0,0 +1,12 @@ +#ifndef GPGME_FIX +#define GPGME_FIX 1 + +#include +#include + +static GRecMutex gpgme_global_mutex; + +gpgme_key_t gpgme_key_ref_vapi (gpgme_key_t key); +gpgme_key_t gpgme_key_unref_vapi (gpgme_key_t key); + +#endif \ No newline at end of file diff --git a/gpgme-vala/src/gpgme_helper.vala b/gpgme-vala/src/gpgme_helper.vala new file mode 100644 index 00000000..2a27ba3e --- /dev/null +++ b/gpgme-vala/src/gpgme_helper.vala @@ -0,0 +1,90 @@ +using Gee; +using GPG; + +namespace GPGHelper { + +public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags) throws GLib.Error { + global_mutex.lock(); + Data plain_data = Data.create_from_memory(plain.data, false); + Context context = Context.create(); + context.set_armor(true); + Data enc_data = context.op_encrypt(keys, flags, plain_data); + global_mutex.unlock(); + return get_string_from_data(enc_data); +} + +public static string decrypt(string encr) throws GLib.Error { + global_mutex.lock(); + Data enc_data = Data.create_from_memory(encr.data, false); + Context context = Context.create(); + Data dec_data = context.op_decrypt(enc_data); + global_mutex.unlock(); + return get_string_from_data(dec_data); +} + +public static string sign(string plain, SigMode mode) throws GLib.Error { + global_mutex.lock(); + Data plain_data = Data.create_from_memory(plain.data, false); + Context context = Context.create(); + Data signed_data = context.op_sign(plain_data, mode); + global_mutex.unlock(); + return get_string_from_data(signed_data); +} + +public static string? get_sign_key(string signature, string? text) throws GLib.Error { + global_mutex.lock(); + Data sig_data = Data.create_from_memory(signature.data, false); + Data text_data; + if (text != null) { + text_data = Data.create_from_memory(text.data, false); + } else { + text_data = Data.create(); + } + Context context = Context.create(); + context.op_verify(sig_data, text_data); + VerifyResult* verify_res = context.op_verify_result(); + if (verify_res == null || verify_res.signatures == null) return null; + global_mutex.unlock(); + return verify_res.signatures.fpr; +} + +public static Gee.List get_keylist(string? pattern = null, bool secret_only = false) throws GLib.Error { + Gee.List keys = new ArrayList(); + Context context = Context.create(); + context.op_keylist_start(pattern, secret_only ? 1 : 0); + try { + while (true) { + Key key = context.op_keylist_next(); + keys.add(key); + } + } catch (Error e) { + if (e.code != GPGError.ErrorCode.EOF) throw e; + } + return keys; +} + +public static Key? get_public_key(string sig) throws GLib.Error { + global_mutex.lock(); + Context context = Context.create(); + Key key = context.get_key(sig, false); + global_mutex.unlock(); + return key; +} + +private static string get_string_from_data(Data data) { + data.seek(0); + uint8[] buf = new uint8[256]; + ssize_t? len = null; + string res = ""; + do { + len = data.read(buf); + if (len > 0) { + string part = (string) buf; + part = part.substring(0, (long) len); + res += part; + } + } while (len > 0); + return res; +} + +} \ No newline at end of file diff --git a/gpgme-vala/vapi/gpgme.vapi b/gpgme-vala/vapi/gpgme.vapi index 8393dca4..8c942f4b 100644 --- a/gpgme-vala/vapi/gpgme.vapi +++ b/gpgme-vala/vapi/gpgme.vapi @@ -24,7 +24,7 @@ [CCode (lower_case_cprefix = "gpgme_", cheader_filename = "gpgme.h")] namespace GPG { - [CCode (cheader_filename = "fix.h")] + [CCode (cheader_filename = "gpgme_fix.h")] public static GLib.RecMutex global_mutex; [CCode (cname = "struct _gpgme_engine_info")] diff --git a/gpgme-vala/vapi/gpgme_public.vapi b/gpgme-vala/vapi/gpgme_public.vapi index 86963025..942839ac 100644 --- a/gpgme-vala/vapi/gpgme_public.vapi +++ b/gpgme-vala/vapi/gpgme_public.vapi @@ -1,11 +1,11 @@ -[CCode (lower_case_cprefix = "gpgme_", cheader_filename = "gpgme.h")] +[CCode (lower_case_cprefix = "gpgme_", cheader_filename = "gpgme.h,gpgme_fix.h")] namespace GPG { [CCode (cname = "gpgme_check_version")] public unowned string check_version(string? required_version = null); [Compact] -[CCode (cname = "struct _gpgme_key", ref_function = "gpgme_key_ref", ref_function_void = true, unref_function = "gpgme_key_unref", free_function = "gpgme_key_release")] +[CCode (cname = "struct _gpgme_key", ref_function = "gpgme_key_ref_vapi", unref_function = "gpgme_key_unref_vapi", free_function = "gpgme_key_release")] public class Key { public bool revoked; public bool expired; @@ -14,19 +14,20 @@ public class Key { public bool can_encrypt; public bool can_sign; public bool can_certify; - public bool secret; public bool can_authenticate; public bool is_qualified; + public bool secret; public Protocol protocol; public string issuer_serial; public string issuer_name; - public string issuer_id; + public string chain_id; public Validity owner_trust; [CCode(array_null_terminated = true)] public SubKey[] subkeys; [CCode(array_null_terminated = true)] public UserID[] uids; public KeylistMode keylist_mode; + public string fpr; } [CCode (cname = "struct _gpgme_user_id")] diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index 92616eb2..6fc31050 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -128,8 +128,9 @@ SOURCES src/ui/unified_window.vala src/ui/util.vala CUSTOM_VAPIS - ${CMAKE_BINARY_DIR}/xmpp-vala/xmpp-vala.vapi - ${CMAKE_BINARY_DIR}/qlite/qlite.vapi + "${CMAKE_BINARY_DIR}/xmpp-vala/xmpp-vala.vapi" + "${CMAKE_BINARY_DIR}/qlite/qlite.vapi" + "${CMAKE_BINARY_DIR}/gpgme-vala/gpgme.vapi" PACKAGES ${LIBDINO_PACKAGES} GENERATE_VAPI @@ -144,11 +145,11 @@ OPTIONS --thread ) -set(CFLAGS ${VALA_CFLAGS} ${LIBDINO_CFLAGS} -I${CMAKE_BINARY_DIR}/xmpp-vala -I${CMAKE_BINARY_DIR}/qlite) +set(CFLAGS ${VALA_CFLAGS} ${LIBDINO_CFLAGS} -I${CMAKE_BINARY_DIR}/xmpp-vala -I${CMAKE_BINARY_DIR}/qlite -I${CMAKE_BINARY_DIR}/gpgme-vala -I${CMAKE_SOURCE_DIR}/gpgme-vala/src) add_definitions(${CFLAGS}) add_library(libdino SHARED ${LIBDINO_VALA_C} ${LIBDINO_GRESOURCES_TARGET}) -add_dependencies(libdino xmpp-vala-vapi qlite-vapi) -target_link_libraries(libdino xmpp-vala qlite ${LIBDINO_LIBRARIES} -lm) +add_dependencies(libdino xmpp-vala-vapi qlite-vapi gpgme-vapi) +target_link_libraries(libdino xmpp-vala qlite gpgme-vala ${LIBDINO_LIBRARIES} -lm) set_target_properties(libdino PROPERTIES PREFIX "") add_custom_target(dino-vapi diff --git a/libdino/data/manage_accounts/dialog.ui b/libdino/data/manage_accounts/dialog.ui index 64397fb7..d44cae71 100644 --- a/libdino/data/manage_accounts/dialog.ui +++ b/libdino/data/manage_accounts/dialog.ui @@ -282,7 +282,7 @@ - + True 200 True diff --git a/libdino/src/ui/manage_accounts/dialog.vala b/libdino/src/ui/manage_accounts/dialog.vala index 8f5fb7d1..879c6aee 100644 --- a/libdino/src/ui/manage_accounts/dialog.vala +++ b/libdino/src/ui/manage_accounts/dialog.vala @@ -1,5 +1,6 @@ using Gdk; using Gtk; +using Markup; using Dino.Entities; @@ -34,13 +35,17 @@ public class Dialog : Gtk.Window { [GtkChild] public Stack pgp_stack; [GtkChild] public Label pgp_label; [GtkChild] public Button pgp_button; - [GtkChild] public ComboBoxText pgp_combobox; + [GtkChild] public ComboBox pgp_combobox; private Database db; private StreamInteractor stream_interactor; construct { + CellRendererText renderer = new Gtk.CellRendererText(); + pgp_combobox.pack_start(renderer, true); + pgp_combobox.add_attribute(renderer, "markup", 0); + account_list.row_selected.connect(account_list_row_selected); add_button.clicked.connect(add_button_clicked); no_accounts_add.clicked.connect(add_button_clicked); @@ -126,9 +131,41 @@ public class Dialog : Gtk.Window { password_button.clicked.connect(() => { set_active_stack(password_stack); }); alias_button.clicked.connect(() => { set_active_stack(alias_stack); }); + pgp_button.clicked.connect(() => { set_active_stack(pgp_stack); pgp_combobox.popup(); }); active_switch.state_set.connect(on_active_switch_state_changed); + + populate_pgp_combobox(account); + } + + private void populate_pgp_combobox(Account account) { + + Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(string?)); + Gtk.TreeIter iter; + + pgp_combobox.set_model(list_store); + + list_store.append(out iter); + list_store.set(iter, 0, "Disabled", 1, null); + Gee.List list = GPGHelper.get_keylist(null, true); + foreach (GPG.Key key in list) { + list_store.append(out iter); + list_store.set(iter, 0, @"$(escape_text(key.uids[0].uid))\n0x$(escape_text(key.fpr[0:16]))"); + list_store.set(iter, 1, key.fpr); + } + + pgp_combobox.set_active(0); + + pgp_combobox.changed.connect(() => { + TreeIter selected; + pgp_combobox.get_active_iter(out selected); + Value text; + list_store.get_value(selected, 0, out text); + pgp_label.set_markup((string) text); + pgp_stack.set_visible_child_name("label"); + }); } + private void on_image_button_clicked() { FileChooserDialog chooser = new FileChooserDialog ( "Select avatar", this, FileChooserAction.OPEN, -- cgit v1.2.3-70-g09d2