aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 1 | /* |
Guillaume Roguez | 2a6150d | 2017-07-19 18:24:47 -0400 | [diff] [blame] | 2 | * Copyright (C) 2016-2017 Savoir-faire Linux Inc. |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 3 | * Author: Alexandre Viau <alexandre.viau@savoirfairelinux.com> |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 4 | * Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | // GTK+ related |
| 23 | #include <gtk/gtk.h> |
| 24 | #include <glib/gi18n.h> |
| 25 | |
| 26 | // LRC |
| 27 | #include <codecmodel.h> |
| 28 | #include <account.h> |
| 29 | #include <accountmodel.h> |
| 30 | #include <profilemodel.h> |
| 31 | #include <profile.h> |
| 32 | #include <personmodel.h> |
| 33 | #include <globalinstances.h> |
| 34 | |
| 35 | // Ring Client |
| 36 | #include "avatarmanipulation.h" |
| 37 | #include "accountmigrationview.h" |
aviau | 2da3d9c | 2016-09-06 11:28:36 -0400 | [diff] [blame] | 38 | #include "usernameregistrationbox.h" |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 39 | #include "utils/models.h" |
| 40 | #include "native/pixbufmanipulator.h" |
| 41 | #include "video/video_widget.h" |
| 42 | |
| 43 | /* size of avatar */ |
| 44 | static constexpr int AVATAR_WIDTH = 100; /* px */ |
| 45 | static constexpr int AVATAR_HEIGHT = 100; /* px */ |
| 46 | |
| 47 | struct _AccountMigrationView |
| 48 | { |
| 49 | GtkBox parent; |
| 50 | }; |
| 51 | |
| 52 | struct _AccountMigrationViewClass |
| 53 | { |
| 54 | GtkBoxClass parent_class; |
| 55 | }; |
| 56 | |
| 57 | typedef struct _AccountMigrationViewPrivate AccountMigrationViewPrivate; |
| 58 | |
| 59 | struct _AccountMigrationViewPrivate |
| 60 | { |
| 61 | Account *account; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 62 | QMetaObject::Connection state_changed; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 63 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 64 | /* main_view */ |
| 65 | GtkWidget *main_view; |
| 66 | GtkWidget *label_account_alias; |
Stepan Salenikovich | a603542 | 2016-11-04 16:04:53 -0400 | [diff] [blame] | 67 | GtkWidget *label_account_ringid; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 68 | GtkWidget *image_avatar; |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 69 | GtkWidget *label_migration_error; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 70 | GtkWidget *entry_password; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 71 | GtkWidget *button_migrate_account; |
| 72 | }; |
| 73 | |
| 74 | G_DEFINE_TYPE_WITH_PRIVATE(AccountMigrationView, account_migration_view, GTK_TYPE_BOX); |
| 75 | |
| 76 | #define ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ACCOUNT_MIGRATION_VIEW_TYPE, AccountMigrationViewPrivate)) |
| 77 | |
| 78 | /* signals */ |
| 79 | enum { |
| 80 | ACCOUNT_MIGRATION_COMPLETED, |
| 81 | ACCOUNT_MIGRATION_FAILED, |
| 82 | LAST_SIGNAL |
| 83 | }; |
| 84 | |
| 85 | static guint account_migration_view_signals[LAST_SIGNAL] = { 0 }; |
| 86 | |
| 87 | static void |
| 88 | account_migration_view_dispose(GObject *object) |
| 89 | { |
Stepan Salenikovich | 56530b1 | 2016-10-20 17:51:34 -0400 | [diff] [blame] | 90 | auto priv = ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(object); |
| 91 | |
| 92 | // make sure to disconnect from all signals when disposing of view |
| 93 | QObject::disconnect(priv->state_changed); |
| 94 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 95 | G_OBJECT_CLASS(account_migration_view_parent_class)->dispose(object); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | account_migration_view_init(AccountMigrationView *view) |
| 100 | { |
| 101 | gtk_widget_init_template(GTK_WIDGET(view)); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | account_migration_view_class_init(AccountMigrationViewClass *klass) |
| 106 | { |
| 107 | G_OBJECT_CLASS(klass)->dispose = account_migration_view_dispose; |
| 108 | |
| 109 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), |
| 110 | "/cx/ring/RingGnome/accountmigrationview.ui"); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 111 | /* main_view */ |
| 112 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, main_view); |
| 113 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, label_account_alias); |
Stepan Salenikovich | a603542 | 2016-11-04 16:04:53 -0400 | [diff] [blame] | 114 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, label_account_ringid); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 115 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, image_avatar); |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 116 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, label_migration_error); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 117 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, entry_password); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 118 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AccountMigrationView, button_migrate_account); |
| 119 | |
| 120 | /* add signals */ |
| 121 | account_migration_view_signals[ACCOUNT_MIGRATION_COMPLETED] = g_signal_new("account-migration-completed", |
| 122 | G_TYPE_FROM_CLASS(klass), |
| 123 | (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 124 | 0, |
| 125 | nullptr, |
| 126 | nullptr, |
| 127 | g_cclosure_marshal_VOID__VOID, |
| 128 | G_TYPE_NONE, 0); |
| 129 | |
| 130 | account_migration_view_signals[ACCOUNT_MIGRATION_FAILED] = g_signal_new("account-migration-failed", |
| 131 | G_TYPE_FROM_CLASS(klass), |
| 132 | (GSignalFlags) (G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION), |
| 133 | 0, |
| 134 | nullptr, |
| 135 | nullptr, |
| 136 | g_cclosure_marshal_VOID__VOID, |
| 137 | G_TYPE_NONE, 0); |
| 138 | } |
| 139 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 140 | static void |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 141 | migrate(AccountMigrationView *view) |
aviau | 2da3d9c | 2016-09-06 11:28:36 -0400 | [diff] [blame] | 142 | { |
| 143 | AccountMigrationViewPrivate *priv = ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(view); |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 144 | gtk_widget_set_sensitive(priv->button_migrate_account, FALSE); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 145 | const gchar *password = gtk_entry_get_text(GTK_ENTRY(priv->entry_password)); |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 146 | priv->state_changed = QObject::connect(priv->account, &Account::migrationEnded, |
| 147 | [=] (const Account::MigrationEndedStatus state) |
| 148 | { |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 149 | priv->account << Account::EditAction::RELOAD; |
Stepan Salenikovich | 56530b1 | 2016-10-20 17:51:34 -0400 | [diff] [blame] | 150 | |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 151 | switch(state) |
| 152 | { |
| 153 | case Account::MigrationEndedStatus::SUCCESS: |
| 154 | g_signal_emit(G_OBJECT(view), account_migration_view_signals[ACCOUNT_MIGRATION_COMPLETED], 0); |
| 155 | break; |
| 156 | case Account::MigrationEndedStatus::INVALID: |
| 157 | case Account::MigrationEndedStatus::UNDEFINED_STATUS: |
| 158 | gtk_widget_show(priv->label_migration_error); |
| 159 | break; |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 160 | } |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 161 | }); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 162 | |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 163 | priv->account->setArchivePassword(password); |
| 164 | gtk_entry_set_text(GTK_ENTRY(priv->entry_password), ""); |
| 165 | priv->account->performAction(Account::EditAction::SAVE); |
Stepan Salenikovich | 155c4b8 | 2016-10-14 11:46:21 -0400 | [diff] [blame] | 166 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void |
| 170 | password_entry_changed(G_GNUC_UNUSED GtkEntry* entry, AccountMigrationView *view) |
| 171 | { |
| 172 | AccountMigrationViewPrivate *priv = ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(view); |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 173 | gtk_widget_hide(priv->label_migration_error); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 174 | |
AmarOk | a85c1f8 | 2017-09-15 13:31:09 -0400 | [diff] [blame] | 175 | gtk_widget_set_sensitive(priv->button_migrate_account, TRUE); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void |
| 179 | build_migration_view(AccountMigrationView *view) |
| 180 | { |
| 181 | g_return_if_fail(IS_ACCOUNT_MIGRATION_VIEW(view)); |
| 182 | AccountMigrationViewPrivate *priv = ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(view); |
| 183 | |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 184 | g_signal_connect(priv->entry_password, "changed", G_CALLBACK(password_entry_changed), view); |
Nicolas Jager | 84fe45e | 2017-03-01 09:14:17 -0500 | [diff] [blame] | 185 | g_signal_connect_swapped(priv->button_migrate_account, "clicked", G_CALLBACK(migrate), view); |
| 186 | g_signal_connect_swapped(priv->entry_password, "activate", G_CALLBACK(migrate), view); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 187 | |
| 188 | gtk_label_set_text(GTK_LABEL(priv->label_account_alias), priv->account->alias().toUtf8().constData()); |
Stepan Salenikovich | a603542 | 2016-11-04 16:04:53 -0400 | [diff] [blame] | 189 | // display the ringID (without "ring:") |
| 190 | gtk_label_set_text(GTK_LABEL(priv->label_account_ringid), URI(priv->account->username()).userinfo().toUtf8().constData()); |
aviau | 6aeb485 | 2016-08-18 16:01:09 -0400 | [diff] [blame] | 191 | |
| 192 | /* set the avatar picture */ |
| 193 | auto photo = GlobalInstances::pixmapManipulator().contactPhoto( |
| 194 | ProfileModel::instance().selectedProfile()->person(), |
| 195 | QSize(AVATAR_WIDTH, AVATAR_HEIGHT), |
| 196 | false); |
| 197 | std::shared_ptr<GdkPixbuf> pixbuf_photo = photo.value<std::shared_ptr<GdkPixbuf>>(); |
| 198 | |
| 199 | if (photo.isValid()) { |
| 200 | gtk_image_set_from_pixbuf (GTK_IMAGE(priv->image_avatar), pixbuf_photo.get()); |
| 201 | } else { |
| 202 | g_warning("invalid pixbuf"); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | GtkWidget * |
| 207 | account_migration_view_new(Account* account) |
| 208 | { |
| 209 | gpointer view = g_object_new(ACCOUNT_MIGRATION_VIEW_TYPE, NULL); |
| 210 | AccountMigrationViewPrivate *priv = ACCOUNT_MIGRATION_VIEW_GET_PRIVATE(view); |
| 211 | priv->account = account; |
| 212 | build_migration_view(ACCOUNT_MIGRATION_VIEW(view)); |
| 213 | return (GtkWidget *)view; |
| 214 | } |