Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 77c579d | 2018-01-30 15:54:02 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Savoir-faire Linux Inc. |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 3 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 4 | * Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> |
| 5 | * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "ringwelcomeview.h" |
| 23 | |
| 24 | #include <gtk/gtk.h> |
| 25 | #include <glib/gi18n.h> |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 26 | #include <qrencode.h> |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 27 | |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 28 | // Qt |
| 29 | #include <QObject> |
| 30 | #include <QItemSelectionModel> |
| 31 | |
Nicolas Jager | f71f8d8 | 2017-04-17 10:22:06 -0400 | [diff] [blame] | 32 | // LRC |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 33 | #include "utils/accounts.h" |
Nicolas Jager | f71f8d8 | 2017-04-17 10:22:06 -0400 | [diff] [blame] | 34 | |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 35 | struct _RingWelcomeView |
| 36 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 37 | GtkScrolledWindow parent; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct _RingWelcomeViewClass |
| 41 | { |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 42 | GtkScrolledWindowClass parent_class; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | typedef struct _RingWelcomeViewPrivate RingWelcomeViewPrivate; |
| 46 | |
| 47 | struct _RingWelcomeViewPrivate |
| 48 | { |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 49 | GtkWidget *box_overlay; |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 50 | GtkWidget *label_ringid; |
| 51 | GtkWidget *label_explanation; |
| 52 | GtkWidget *button_qrcode; |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 53 | GtkWidget *revealer_qrcode; |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 54 | |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 55 | AccountInfoPointer const *accountInfo_; |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 56 | }; |
| 57 | |
Stepan Salenikovich | ab0f5be | 2016-02-05 15:16:35 -0500 | [diff] [blame] | 58 | 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] | 59 | |
| 60 | #define RING_WELCOME_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_WELCOME_VIEW_TYPE, RingWelcomeViewPrivate)) |
| 61 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 62 | static gboolean draw_qrcode(GtkWidget*,cairo_t*,RingWelcomeView*); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 63 | static void switch_qrcode(RingWelcomeView* self); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 64 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 65 | void |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 66 | ring_welcome_update_view(RingWelcomeView* self) { |
Stepan Salenikovich | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 67 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 68 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 69 | // Only draw a basic view for SIP accounts |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 70 | if (not *priv->accountInfo_ || (*priv->accountInfo_)->profileInfo.type == lrc::api::profile::Type::SIP) { |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 71 | gtk_widget_hide(priv->button_qrcode); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 72 | gtk_widget_hide(priv->label_ringid); |
| 73 | gtk_widget_hide(priv->label_explanation); |
Nicolas Jager | 15a8b90 | 2017-03-21 07:53:06 -0400 | [diff] [blame] | 74 | gtk_widget_hide(priv->revealer_qrcode); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 75 | gtk_widget_set_opacity(priv->box_overlay, 1.0); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 76 | gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer_qrcode), FALSE); |
| 77 | return; |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 78 | } |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 79 | |
| 80 | // Get registeredName, else the Ring Id |
| 81 | gchar *ring_id = nullptr; |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 82 | if(! (*priv->accountInfo_)->registeredName.empty()){ |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 83 | gtk_label_set_text( |
| 84 | GTK_LABEL(priv->label_explanation), |
| 85 | _("This is your Ring username.\nCopy and share it with your friends!") |
| 86 | ); |
| 87 | ring_id = g_markup_printf_escaped("<span fgcolor=\"black\">ring:%s</span>", |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 88 | (*priv->accountInfo_)->registeredName.c_str()); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 89 | } |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 90 | else if (!(*priv->accountInfo_)->profileInfo.uri.empty()) { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 91 | gtk_label_set_text( |
| 92 | GTK_LABEL(priv->label_explanation), |
| 93 | C_("Do not translate \"RingID\"", "This is your RingID.\nCopy and share it with your friends!") |
| 94 | ); |
| 95 | ring_id = g_markup_printf_escaped("<span fgcolor=\"black\">%s</span>", |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 96 | (*priv->accountInfo_)->profileInfo.uri.c_str()); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 97 | } else { |
| 98 | gtk_label_set_text(GTK_LABEL(priv->label_explanation), NULL); |
Sébastien Blin | 1140f87 | 2018-02-26 10:12:16 -0500 | [diff] [blame] | 99 | ring_id = {}; |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | gtk_label_set_markup(GTK_LABEL(priv->label_ringid), ring_id); |
| 103 | |
| 104 | gtk_widget_show(priv->label_explanation); |
| 105 | gtk_widget_show(priv->label_ringid); |
| 106 | gtk_widget_show(priv->button_qrcode); |
| 107 | gtk_widget_show(priv->revealer_qrcode); |
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); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 188 | g_signal_connect(drawingarea_qrcode, "draw", G_CALLBACK(draw_qrcode), self); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 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 | 64d446c | 2016-09-15 10:56:55 -0400 | [diff] [blame] | 209 | gtk_widget_show_all(GTK_WIDGET(self)); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void |
| 213 | ring_welcome_view_dispose(GObject *object) |
| 214 | { |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 215 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->dispose(object); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | ring_welcome_view_finalize(GObject *object) |
| 220 | { |
| 221 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->finalize(object); |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | ring_welcome_view_class_init(RingWelcomeViewClass *klass) |
| 226 | { |
| 227 | G_OBJECT_CLASS(klass)->finalize = ring_welcome_view_finalize; |
| 228 | G_OBJECT_CLASS(klass)->dispose = ring_welcome_view_dispose; |
| 229 | } |
| 230 | |
| 231 | GtkWidget * |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 232 | ring_welcome_view_new(AccountInfoPointer const & accountInfo) |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 233 | { |
| 234 | gpointer self = g_object_new(RING_WELCOME_VIEW_TYPE, NULL); |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 235 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 236 | priv->accountInfo_ = &accountInfo; |
| 237 | ring_welcome_update_view(RING_WELCOME_VIEW(self)); |
Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame] | 238 | |
| 239 | return (GtkWidget *)self; |
| 240 | } |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 241 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 242 | |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 243 | static gboolean |
Stepan Salenikovich | 635258c | 2016-05-17 14:41:20 -0400 | [diff] [blame] | 244 | draw_qrcode(G_GNUC_UNUSED GtkWidget* diese, |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 245 | cairo_t* cr, |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 246 | RingWelcomeView* self) |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 247 | { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 248 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 249 | |
Hugo Lefeuvre | 6f2ceb1 | 2018-04-18 15:08:01 -0400 | [diff] [blame] | 250 | auto rcode = QRcode_encodeString((*priv->accountInfo_)->profileInfo.uri.c_str(), |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 251 | 0, //Let the version be decided by libqrencode |
| 252 | QR_ECLEVEL_L, // Lowest level of error correction |
| 253 | QR_MODE_8, // 8-bit data mode |
| 254 | 1); |
| 255 | |
| 256 | if (!rcode) { // no rcode, no draw |
| 257 | g_warning("Failed to generate QR code"); |
| 258 | return TRUE; |
| 259 | } |
| 260 | |
| 261 | auto margin = 5; |
| 262 | auto qrsize = 200; |
| 263 | int qrwidth = rcode->width + margin * 2; |
| 264 | |
| 265 | /* scaling */ |
| 266 | auto scale = qrsize/qrwidth; |
| 267 | cairo_scale(cr, scale, scale); |
| 268 | |
| 269 | /* fill the background in white */ |
| 270 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 271 | cairo_rectangle(cr, 0, 0, qrwidth, qrwidth); |
| 272 | cairo_fill (cr); |
| 273 | |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 274 | unsigned char *p; |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 275 | p = rcode->data; |
| 276 | cairo_set_source_rgb(cr, 0, 0, 0); // back in black |
| 277 | for(int y = 0; y < rcode->width; y++) { |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 278 | unsigned char* row = (p + (y * rcode->width)); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 279 | for(int x = 0; x < rcode->width; x++) { |
| 280 | if(*(row + x) & 0x1) { |
| 281 | cairo_rectangle(cr, margin + x, margin + y, 1, 1); |
| 282 | cairo_fill(cr); |
| 283 | } |
| 284 | } |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | QRcode_free(rcode); |
| 288 | return TRUE; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | static void |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 293 | switch_qrcode(RingWelcomeView* self) |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 294 | { |
Stepan Salenikovich | 3e2851a | 2016-09-15 12:08:13 -0400 | [diff] [blame] | 295 | auto priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 296 | |
| 297 | auto to_reveal = !gtk_revealer_get_reveal_child(GTK_REVEALER(priv->revealer_qrcode)); |
| 298 | gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer_qrcode), to_reveal); |
| 299 | gtk_widget_set_opacity(priv->box_overlay, to_reveal ? 0.25 : 1.0); |
aviau | dd355ba | 2016-04-08 14:23:09 -0400 | [diff] [blame] | 300 | } |