Stepan Salenikovich | 2cde761 | 2015-09-25 10:44:01 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 Savoir-faire Linux Inc. |
| 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> |
| 27 | |
| 28 | struct _RingWelcomeView |
| 29 | { |
| 30 | GtkBox parent; |
| 31 | }; |
| 32 | |
| 33 | struct _RingWelcomeViewClass |
| 34 | { |
| 35 | GtkBoxClass parent_class; |
| 36 | }; |
| 37 | |
| 38 | typedef struct _RingWelcomeViewPrivate RingWelcomeViewPrivate; |
| 39 | |
| 40 | struct _RingWelcomeViewPrivate |
| 41 | { |
| 42 | QMetaObject::Connection ringaccount_updated; |
| 43 | }; |
| 44 | |
| 45 | G_DEFINE_TYPE_WITH_PRIVATE(RingWelcomeView, ring_welcome_view, GTK_TYPE_BOX); |
| 46 | |
| 47 | #define RING_WELCOME_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_WELCOME_VIEW_TYPE, RingWelcomeViewPrivate)) |
| 48 | |
| 49 | static void |
| 50 | show_ring_id(GtkLabel *label, Account *account) { |
| 51 | g_return_if_fail(label); |
| 52 | |
| 53 | if (account) { |
| 54 | if (!account->username().isEmpty()) { |
| 55 | QString hash = "<span fgcolor=\"black\">" + account->username() + "</span>"; |
| 56 | gtk_label_set_markup(label, hash.toUtf8().constData()); |
| 57 | } else { |
| 58 | g_warning("got ring account, but Ring id is empty"); |
| 59 | gtk_label_set_markup(label, |
| 60 | g_strdup_printf("<span fgcolor=\"gray\">%s</span>", |
| 61 | _("fetching RingID..."))); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | ring_welcome_view_init(RingWelcomeView *self) |
| 68 | { |
| 69 | RingWelcomeViewPrivate *priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 70 | |
| 71 | gtk_orientable_set_orientation(GTK_ORIENTABLE(self), GTK_ORIENTATION_VERTICAL); |
| 72 | gtk_box_set_spacing(GTK_BOX(self), 15); |
| 73 | gtk_box_set_baseline_position(GTK_BOX(self), GTK_BASELINE_POSITION_CENTER); |
| 74 | gtk_widget_set_vexpand(GTK_WIDGET(self), TRUE); |
| 75 | gtk_widget_set_hexpand(GTK_WIDGET(self), FALSE); |
| 76 | gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_CENTER); |
| 77 | gtk_widget_set_halign(GTK_WIDGET(self), GTK_ALIGN_CENTER); |
| 78 | |
| 79 | /* get logo */ |
| 80 | GError *error = NULL; |
| 81 | GdkPixbuf* logo = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-logo-blue", |
| 82 | 350, -1, TRUE, &error); |
| 83 | if (logo == NULL) { |
| 84 | g_debug("Could not load logo: %s", error->message); |
| 85 | g_clear_error(&error); |
| 86 | } else { |
| 87 | auto image_ring_logo = gtk_image_new_from_pixbuf(logo); |
| 88 | gtk_box_pack_start(GTK_BOX(self), image_ring_logo, FALSE, TRUE, 0); |
| 89 | } |
| 90 | |
| 91 | /* welcome text */ |
| 92 | auto label_welcome_text = gtk_label_new(_("Ring is a secure and distributed voice, video, and chat communication platform that requires no centralized server and leaves the power of privacy in the hands of the user.")); |
| 93 | gtk_label_set_justify(GTK_LABEL(label_welcome_text), GTK_JUSTIFY_CENTER); |
| 94 | gtk_widget_override_font(label_welcome_text, pango_font_description_from_string("12")); |
| 95 | gtk_label_set_line_wrap(GTK_LABEL(label_welcome_text), TRUE); |
| 96 | /* the max width chars is to limit how much the text expands */ |
| 97 | gtk_label_set_max_width_chars(GTK_LABEL(label_welcome_text), 50); |
| 98 | gtk_label_set_selectable(GTK_LABEL(label_welcome_text), TRUE); |
| 99 | gtk_box_pack_start(GTK_BOX(self), label_welcome_text, FALSE, TRUE, 0); |
| 100 | |
| 101 | /* RingID explanation */ |
| 102 | auto explanation = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", |
| 103 | C_("Do not translate \"RingID\"", "This is your RingID.\nCopy and share it with your friends!")); |
| 104 | auto label_explanation = gtk_label_new(NULL); |
| 105 | gtk_label_set_markup(GTK_LABEL(label_explanation), explanation); |
| 106 | gtk_label_set_justify(GTK_LABEL(label_explanation), GTK_JUSTIFY_CENTER); |
| 107 | gtk_label_set_selectable(GTK_LABEL(label_explanation), TRUE); |
| 108 | gtk_widget_set_margin_top(label_explanation, 20); |
| 109 | g_free(explanation); |
| 110 | /* we migth need to hide the label if a RING account doesn't exist */ |
| 111 | gtk_widget_set_no_show_all(label_explanation, TRUE); |
| 112 | gtk_box_pack_start(GTK_BOX(self), label_explanation, FALSE, TRUE, 0); |
| 113 | |
| 114 | /* RingID label */ |
| 115 | auto label_ringid = gtk_label_new(NULL); |
| 116 | gtk_label_set_selectable(GTK_LABEL(label_ringid), TRUE); |
| 117 | gtk_widget_override_font(label_ringid, pango_font_description_from_string("monospace 12")); |
| 118 | show_ring_id(GTK_LABEL(label_ringid), get_active_ring_account()); |
| 119 | gtk_widget_set_no_show_all(label_ringid, TRUE); |
| 120 | gtk_box_pack_start(GTK_BOX(self), label_ringid, FALSE, TRUE, 0); |
| 121 | |
| 122 | if (get_active_ring_account()) { |
| 123 | gtk_widget_show(label_explanation); |
| 124 | gtk_widget_show(label_ringid); |
| 125 | } |
| 126 | |
| 127 | priv-> ringaccount_updated = QObject::connect( |
| 128 | AccountModel::instance(), |
| 129 | &AccountModel::dataChanged, |
| 130 | [label_explanation, label_ringid] () { |
| 131 | /* check if the active ring account has changed, |
| 132 | * eg: if it was deleted */ |
| 133 | auto account = get_active_ring_account(); |
| 134 | show_ring_id(GTK_LABEL(label_ringid), get_active_ring_account()); |
| 135 | if (account) { |
| 136 | gtk_widget_show(label_explanation); |
| 137 | gtk_widget_show(label_ringid); |
| 138 | } else { |
| 139 | gtk_widget_hide(label_explanation); |
| 140 | gtk_widget_hide(label_ringid); |
| 141 | } |
| 142 | } |
| 143 | ); |
| 144 | |
| 145 | gtk_widget_show_all(GTK_WIDGET(self)); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | ring_welcome_view_dispose(GObject *object) |
| 150 | { |
| 151 | RingWelcomeView *self = RING_WELCOME_VIEW(object); |
| 152 | RingWelcomeViewPrivate *priv = RING_WELCOME_VIEW_GET_PRIVATE(self); |
| 153 | |
| 154 | QObject::disconnect(priv->ringaccount_updated); |
| 155 | |
| 156 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->dispose(object); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | ring_welcome_view_finalize(GObject *object) |
| 161 | { |
| 162 | G_OBJECT_CLASS(ring_welcome_view_parent_class)->finalize(object); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | ring_welcome_view_class_init(RingWelcomeViewClass *klass) |
| 167 | { |
| 168 | G_OBJECT_CLASS(klass)->finalize = ring_welcome_view_finalize; |
| 169 | G_OBJECT_CLASS(klass)->dispose = ring_welcome_view_dispose; |
| 170 | } |
| 171 | |
| 172 | GtkWidget * |
| 173 | ring_welcome_view_new() |
| 174 | { |
| 175 | gpointer self = g_object_new(RING_WELCOME_VIEW_TYPE, NULL); |
| 176 | |
| 177 | return (GtkWidget *)self; |
| 178 | } |