Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 2a6150d | 2017-07-19 18:24:47 -0400 | [diff] [blame^] | 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include "ringwelcomeview.h" |
| 21 | |
| 22 | #include <gtk/gtk.h> |
| 23 | #include <glib/gi18n.h> |
| 24 | #include "utils/accounts.h" |
| 25 | #include <account.h> |
| 26 | #include <accountmodel.h> |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 27 | #include <qrencode.h> |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 28 | |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 29 | // Qt |
| 30 | #include <QObject> |
| 31 | #include <QItemSelectionModel> |
| 32 | |
Nicolas Jager | f71f8d8 | 2017-04-17 10:22:06 -0400 | [diff] [blame] | 33 | // LRC |
| 34 | #include <availableaccountmodel.h> |
| 35 | |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 36 | struct _RingWelcomeView |
| 37 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 38 | GtkScrolledWindow parent; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct _RingWelcomeViewClass |
| 42 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 43 | GtkScrolledWindowClass parent_class; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | typedef struct _RingWelcomeViewPrivate RingWelcomeViewPrivate; |
| 47 | |
| 48 | struct _RingWelcomeViewPrivate |
| 49 | { |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 50 | GtkWidget *box_overlay; |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 51 | GtkWidget *label_ringid; |
| 52 | GtkWidget *label_explanation; |
| 53 | GtkWidget *button_qrcode; |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 54 | GtkWidget *revealer_qrcode; |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 55 | |
aviau | df1f97b | 2016-10-24 12:34:14 -0400 | [diff] [blame] | 56 | QMetaObject::Connection account_model_data_changed; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 59 | G_DEFINE_TYPE_WITH_PRIVATE(RingWelcomeView, ring_welcome_view, GTK_TYPE_SCROLLED_WINDOW); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 60 | |
| 61 | #define RING_WELCOME_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_WELCOME_VIEW_TYPE, RingWelcomeViewPrivate)) |
| 62 | |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 63 | static gboolean draw_qrcode(GtkWidget*,cairo_t*,gpointer); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 64 | static void switch_qrcode(RingWelcomeView* self); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 65 | |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 66 | static void |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 67 | update_view(RingWelcomeView *self) { |
| 68 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 69 | |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 70 | if (auto account = get_active_ring_account()) { |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 71 | gchar *ring_id = nullptr; |
aviau | f74c7b5 | 2016-12-01 13:57:00 -0500 | [diff] [blame] | 72 | if(!account->registeredName().isEmpty()){ |
| 73 | gtk_label_set_text( |
| 74 | GTK_LABEL(priv->label_explanation), |
| 75 | _("This is your Ring username.\nCopy and share it with your friends!") |
| 76 | ); |
| 77 | ring_id = g_markup_printf_escaped("<span fgcolor=\"black\">ring:%s</span>", |
| 78 | account->registeredName().toUtf8().constData()); |
| 79 | } |
| 80 | else if (!account->username().isEmpty()) { |
| 81 | gtk_label_set_text( |
| 82 | GTK_LABEL(priv->label_explanation), |
| 83 | C_("Do not translate \"RingID\"", "This is your RingID.\nCopy and share it with your friends!") |
| 84 | ); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 85 | ring_id = g_markup_printf_escaped("<span fgcolor=\"black\">%s</span>", |
| 86 | account->username().toUtf8().constData()); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 87 | } else { |
aviau | f74c7b5 | 2016-12-01 13:57:00 -0500 | [diff] [blame] | 88 | gtk_label_set_text(GTK_LABEL(priv->label_explanation), NULL); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 89 | ring_id = g_markup_printf_escaped("<span fgcolor=\"gray\">%s</span>", |
| 90 | _("fetching RingID...")); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 91 | } |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 92 | gtk_label_set_markup(GTK_LABEL(priv->label_ringid), ring_id); |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 93 | gtk_widget_show(priv->label_explanation); |
| 94 | gtk_widget_show(priv->label_ringid); |
| 95 | gtk_widget_show(priv->button_qrcode); |
| 96 | gtk_widget_show(priv->revealer_qrcode); |
| 97 | |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 98 | g_free(ring_id); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 99 | |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 100 | } else { // any other protocols |
| 101 | gtk_widget_hide(priv->label_explanation); |
| 102 | gtk_widget_hide(priv->label_ringid); |
| 103 | gtk_widget_hide(priv->button_qrcode); |
| 104 | gtk_widget_hide(priv->revealer_qrcode); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 105 | gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer_qrcode), FALSE); |
| 106 | gtk_widget_set_opacity(priv->box_overlay, 1.0); |
| 107 | } |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void |
| 111 | ring_welcome_view_init(RingWelcomeView *self) |
| 112 | { |
| 113 | RingWelcomeViewPrivate *priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 114 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 115 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
| 116 | |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 117 | auto box_main = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15); |
| 118 | gtk_container_add(GTK_CONTAINER(self), box_main); |
| 119 | gtk_box_set_baseline_position(GTK_BOX(box_main), GTK_BASELINE_POSITION_CENTER); |
| 120 | gtk_widget_set_vexpand(GTK_WIDGET(box_main), TRUE); |
| 121 | gtk_widget_set_hexpand(GTK_WIDGET(box_main), FALSE); |
| 122 | gtk_widget_set_valign(GTK_WIDGET(box_main), GTK_ALIGN_CENTER); |
| 123 | gtk_widget_set_halign(GTK_WIDGET(box_main), GTK_ALIGN_CENTER); |
| 124 | gtk_widget_set_visible(GTK_WIDGET(box_main), TRUE); |
| 125 | |
| 126 | /* overlay to show hide the qr code over the stuff in it */ |
| 127 | auto overlay_qrcode = gtk_overlay_new(); |
| 128 | gtk_box_pack_start(GTK_BOX(box_main), overlay_qrcode, FALSE, TRUE, 0); |
| 129 | |
| 130 | /* box which will be in the overaly so that we can display the QR code over the stuff in this box */ |
| 131 | priv->box_overlay = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15); |
| 132 | gtk_container_add(GTK_CONTAINER(overlay_qrcode), priv->box_overlay); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 133 | |
| 134 | /* get logo */ |
| 135 | GError *error = NULL; |
| 136 | GdkPixbuf* logo = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-logo-blue", |
| 137 | 350, -1, TRUE, &error); |
| 138 | if (logo == NULL) { |
| 139 | g_debug("Could not load logo: %s", error->message); |
| 140 | g_clear_error(&error); |
| 141 | } else { |
| 142 | auto image_ring_logo = gtk_image_new_from_pixbuf(logo); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 143 | gtk_box_pack_start(GTK_BOX(priv->box_overlay), image_ring_logo, FALSE, TRUE, 0); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 144 | gtk_widget_set_visible(GTK_WIDGET(image_ring_logo), TRUE); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* welcome text */ |
Stepan Salenikovich | d4d19d6 | 2016-11-03 11:12:22 -0400 | [diff] [blame] | 148 | auto label_welcome_text = gtk_label_new(_("Ring is free software for universal communication which respects the freedoms and privacy of its users.")); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 149 | gtk_label_set_justify(GTK_LABEL(label_welcome_text), GTK_JUSTIFY_CENTER); |
Houmin | 6829382 | 2017-05-16 12:13:55 +0800 | [diff] [blame] | 150 | PangoAttrList *attrs_welcome_text = pango_attr_list_new(); |
| 151 | PangoAttribute *font_desc_welcome_text = pango_attr_font_desc_new(pango_font_description_from_string("12")); |
| 152 | pango_attr_list_insert(attrs_welcome_text, font_desc_welcome_text); |
| 153 | gtk_label_set_attributes(GTK_LABEL(label_welcome_text), attrs_welcome_text); |
| 154 | pango_attr_list_unref(attrs_welcome_text); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 155 | gtk_label_set_line_wrap(GTK_LABEL(label_welcome_text), TRUE); |
| 156 | /* the max width chars is to limit how much the text expands */ |
| 157 | gtk_label_set_max_width_chars(GTK_LABEL(label_welcome_text), 50); |
| 158 | gtk_label_set_selectable(GTK_LABEL(label_welcome_text), TRUE); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 159 | gtk_box_pack_start(GTK_BOX(priv->box_overlay), label_welcome_text, FALSE, TRUE, 0); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 160 | gtk_widget_set_visible(GTK_WIDGET(label_welcome_text), TRUE); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 161 | |
| 162 | /* RingID explanation */ |
aviau | f74c7b5 | 2016-12-01 13:57:00 -0500 | [diff] [blame] | 163 | priv->label_explanation = gtk_label_new(NULL); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 164 | auto context = gtk_widget_get_style_context(priv->label_explanation); |
Stepan Salenikovich | cebc592 | 2015-10-22 15:43:09 -0400 | [diff] [blame] | 165 | gtk_style_context_add_class(context, GTK_STYLE_CLASS_DIM_LABEL); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 166 | gtk_label_set_justify(GTK_LABEL(priv->label_explanation), GTK_JUSTIFY_CENTER); |
| 167 | gtk_label_set_selectable(GTK_LABEL(priv->label_explanation), TRUE); |
| 168 | gtk_widget_set_margin_top(priv->label_explanation, 20); |
| 169 | gtk_widget_set_no_show_all(priv->label_explanation, TRUE); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 170 | gtk_box_pack_start(GTK_BOX(priv->box_overlay), priv->label_explanation, FALSE, TRUE, 0); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 171 | |
| 172 | /* RingID label */ |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 173 | priv->label_ringid = gtk_label_new(NULL); |
| 174 | gtk_label_set_selectable(GTK_LABEL(priv->label_ringid), TRUE); |
Houmin | 6829382 | 2017-05-16 12:13:55 +0800 | [diff] [blame] | 175 | PangoAttrList *attrs_ringid = pango_attr_list_new(); |
| 176 | PangoAttribute *font_desc_ringid = pango_attr_font_desc_new(pango_font_description_from_string("monospace 12")); |
| 177 | pango_attr_list_insert(attrs_ringid, font_desc_ringid); |
| 178 | gtk_label_set_attributes(GTK_LABEL(priv->label_ringid), attrs_ringid); |
| 179 | pango_attr_list_unref(attrs_ringid); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 180 | gtk_widget_set_no_show_all(priv->label_ringid, TRUE); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 181 | gtk_box_pack_start(GTK_BOX(box_main), priv->label_ringid, FALSE, TRUE, 0); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 182 | gtk_label_set_ellipsize(GTK_LABEL(priv->label_ringid), PANGO_ELLIPSIZE_END); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 183 | |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 184 | /* QR drawing area */ |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 185 | auto drawingarea_qrcode = gtk_drawing_area_new(); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 186 | auto qrsize = 200; |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 187 | gtk_widget_set_size_request(drawingarea_qrcode, qrsize, qrsize); |
| 188 | g_signal_connect(drawingarea_qrcode, "draw", G_CALLBACK(draw_qrcode), NULL); |
| 189 | gtk_widget_set_visible(drawingarea_qrcode, TRUE); |
| 190 | |
| 191 | /* revealer which will show the qr code */ |
| 192 | priv->revealer_qrcode = gtk_revealer_new(); |
| 193 | gtk_container_add(GTK_CONTAINER(priv->revealer_qrcode), drawingarea_qrcode); |
| 194 | gtk_revealer_set_transition_type(GTK_REVEALER(priv->revealer_qrcode), GTK_REVEALER_TRANSITION_TYPE_CROSSFADE); //GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP); |
| 195 | gtk_widget_set_halign(priv->revealer_qrcode, GTK_ALIGN_CENTER); |
| 196 | gtk_widget_set_valign(priv->revealer_qrcode, GTK_ALIGN_CENTER); |
| 197 | gtk_overlay_add_overlay(GTK_OVERLAY(overlay_qrcode), priv->revealer_qrcode); |
| 198 | gtk_widget_set_no_show_all(priv->revealer_qrcode, TRUE); |
| 199 | gtk_widget_set_visible(priv->revealer_qrcode, FALSE); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 200 | |
| 201 | /* QR code button */ |
aviau | f74c7b5 | 2016-12-01 13:57:00 -0500 | [diff] [blame] | 202 | priv->button_qrcode = gtk_button_new_with_label(C_("Do not translate \"Ring ID\"", "Ring ID QR code")); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 203 | gtk_widget_set_hexpand(priv->button_qrcode, FALSE); |
| 204 | gtk_widget_set_size_request(priv->button_qrcode,10,10); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 205 | g_signal_connect_swapped(priv->button_qrcode, "clicked", G_CALLBACK(switch_qrcode), self); |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 206 | gtk_widget_set_no_show_all(priv->button_qrcode, TRUE); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 207 | gtk_box_pack_start(GTK_BOX(box_main), priv->button_qrcode, TRUE, TRUE, 0); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 208 | |
Stepan Salenikovich | 11d88d8 | 2017-04-26 13:53:11 -0400 | [diff] [blame] | 209 | update_view(self); |
aviau | df1f97b | 2016-10-24 12:34:14 -0400 | [diff] [blame] | 210 | priv->account_model_data_changed = QObject::connect( |
Nicolas Jager | e23e494 | 2017-04-18 13:48:22 -0400 | [diff] [blame] | 211 | AvailableAccountModel::instance().selectionModel(), |
| 212 | &QItemSelectionModel::currentChanged, |
| 213 | [self] () |
Nicolas Jager | f71f8d8 | 2017-04-17 10:22:06 -0400 | [diff] [blame] | 214 | { |
| 215 | update_view(self); |
| 216 | }); |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 217 | |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 218 | gtk_widget_show_all(GTK_WIDGET(self)); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void |
| 222 | ring_welcome_view_dispose(GObject *object) |
| 223 | { |
| 224 | RingWelcomeView *self = RING_WELCOME_VIEW(object); |
| 225 | RingWelcomeViewPrivate *priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 226 | |
aviau | df1f97b | 2016-10-24 12:34:14 -0400 | [diff] [blame] | 227 | QObject::disconnect(priv->account_model_data_changed); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 228 | |
| 229 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->dispose(object); |
| 230 | } |
| 231 | |
| 232 | static void |
| 233 | ring_welcome_view_finalize(GObject *object) |
| 234 | { |
| 235 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->finalize(object); |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | ring_welcome_view_class_init(RingWelcomeViewClass *klass) |
| 240 | { |
| 241 | G_OBJECT_CLASS(klass)->finalize = ring_welcome_view_finalize; |
| 242 | G_OBJECT_CLASS(klass)->dispose = ring_welcome_view_dispose; |
| 243 | } |
| 244 | |
| 245 | GtkWidget * |
| 246 | ring_welcome_view_new() |
| 247 | { |
| 248 | gpointer self = g_object_new(RING_WELCOME_VIEW_TYPE, NULL); |
| 249 | |
| 250 | return (GtkWidget *)self; |
| 251 | } |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 252 | |
| 253 | static gboolean |
Stepan Salenikovich | 635258c | 2016-05-17 14:41:20 -0400 | [diff] [blame] | 254 | draw_qrcode(G_GNUC_UNUSED GtkWidget* diese, |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 255 | cairo_t* cr, |
Stepan Salenikovich | 635258c | 2016-05-17 14:41:20 -0400 | [diff] [blame] | 256 | G_GNUC_UNUSED gpointer data) |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 257 | { |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 258 | auto account = get_active_ring_account(); |
| 259 | if (!account) |
| 260 | return TRUE; |
| 261 | |
| 262 | auto rcode = QRcode_encodeString(account->username().toStdString().c_str(), |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 263 | 0, //Let the version be decided by libqrencode |
| 264 | QR_ECLEVEL_L, // Lowest level of error correction |
| 265 | QR_MODE_8, // 8-bit data mode |
| 266 | 1); |
| 267 | |
| 268 | if (!rcode) { // no rcode, no draw |
| 269 | g_warning("Failed to generate QR code"); |
| 270 | return TRUE; |
| 271 | } |
| 272 | |
| 273 | auto margin = 5; |
| 274 | auto qrsize = 200; |
| 275 | int qrwidth = rcode->width + margin * 2; |
| 276 | |
| 277 | /* scaling */ |
| 278 | auto scale = qrsize/qrwidth; |
| 279 | cairo_scale(cr, scale, scale); |
| 280 | |
| 281 | /* fill the background in white */ |
| 282 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 283 | cairo_rectangle(cr, 0, 0, qrwidth, qrwidth); |
| 284 | cairo_fill (cr); |
| 285 | |
| 286 | unsigned char *row, *p; |
| 287 | p = rcode->data; |
| 288 | cairo_set_source_rgb(cr, 0, 0, 0); // back in black |
| 289 | for(int y = 0; y < rcode->width; y++) { |
| 290 | row = (p + (y * rcode->width)); |
| 291 | for(int x = 0; x < rcode->width; x++) { |
| 292 | if(*(row + x) & 0x1) { |
| 293 | cairo_rectangle(cr, margin + x, margin + y, 1, 1); |
| 294 | cairo_fill(cr); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | QRcode_free(rcode); |
| 301 | return TRUE; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | static void |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 306 | switch_qrcode(RingWelcomeView* self) |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 307 | { |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 308 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 309 | |
| 310 | auto to_reveal = !gtk_revealer_get_reveal_child(GTK_REVEALER(priv->revealer_qrcode)); |
| 311 | gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer_qrcode), to_reveal); |
| 312 | gtk_widget_set_opacity(priv->box_overlay, to_reveal ? 0.25 : 1.0); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 313 | } |