profile : allows to edit profile in the settings

allows to edit the avatar and the name in settings.

Tuleap: #717
Change-Id: I9fcb3606087f2e5a6680a2ea5ba9c7c5cc23d1b1
diff --git a/src/generalsettingsview.cpp b/src/generalsettingsview.cpp
index 82b01f2..c362bc4 100644
--- a/src/generalsettingsview.cpp
+++ b/src/generalsettingsview.cpp
@@ -23,6 +23,12 @@
 #include <glib/gi18n.h>
 #include <categorizedhistorymodel.h>
 #include "utils/files.h"
+#include "avatarmanipulation.h"
+
+/* lrc */
+#include <person.h>
+#include <profile.h>
+#include <profilemodel.h>
 
 struct _GeneralSettingsView
 {
@@ -46,6 +52,9 @@
     GtkWidget *checkbutton_bringtofront;
     GtkWidget *radiobutton_chatright;
     GtkWidget *radiobutton_chatbottom;
+    GtkWidget *box_profil_settings;
+    GtkWidget *avatarmanipulation;
+    GtkWidget *profile_name;
 
     /* history settings */
     GtkWidget *adjustment_history_duration;
@@ -63,6 +72,9 @@
 
     g_clear_object(&priv->settings);
 
+    //make sure the VideoWidget is destroyed
+    general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(object), FALSE);
+
     G_OBJECT_CLASS(general_settings_view_parent_class)->dispose(object);
 }
 
@@ -165,6 +177,7 @@
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, radiobutton_chatbottom);
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, adjustment_history_duration);
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, button_clear_history);
+    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), GeneralSettingsView, box_profil_settings);
 }
 
 GtkWidget *
@@ -174,3 +187,48 @@
 
     return (GtkWidget *)view;
 }
+
+static void
+change_profile_name(GtkEntry *entry)
+{
+    auto profile = ProfileModel::instance().selectedProfile();
+    profile->person()->setFormattedName(gtk_entry_get_text(entry));
+    profile->save();
+}
+
+void
+general_settings_view_show_profile(GeneralSettingsView *self, gboolean show_profile)
+{
+    g_return_if_fail(GENERAL_SETTINGS_VIEW(self));
+    GeneralSettingsViewPrivate *priv = GENERAL_SETTINGS_VIEW_GET_PRIVATE(self);
+
+    /* We will construct and destroy the profile (AvatarManipulation widget) each time the profile
+     * should be visible and hidden, respectively. It is not the "prettiest" way of doing things,
+     * but this way we ensure 1. that the profile is updated correctly when it is shown and 2. that
+     * the VideoWidget inside is destroyed when it is not being shown.
+     */
+    if (show_profile) {
+        /* avatar manipulation widget */
+        priv->avatarmanipulation = avatar_manipulation_new();
+        gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->avatarmanipulation, true, true, 0);
+        gtk_widget_set_visible(priv->avatarmanipulation, true);
+
+        /* print the profile name. as long as we have only one profil, profil name = person name (a.k.a formatedName) */
+        priv->profile_name = gtk_entry_new();
+        gtk_entry_set_text (GTK_ENTRY(priv->profile_name),
+                            ProfileModel::instance().selectedProfile()->person()->formattedName().toUtf8().constData());
+        gtk_widget_set_visible(priv->profile_name, true);
+        gtk_entry_set_alignment(GTK_ENTRY(priv->profile_name), 0.5);
+        gtk_box_pack_start(GTK_BOX(priv->box_profil_settings), priv->profile_name, true, true, 0);
+        g_signal_connect(priv->profile_name, "changed", G_CALLBACK(change_profile_name), NULL);
+    } else {
+        if (priv->avatarmanipulation) {
+            gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->avatarmanipulation);
+            priv->avatarmanipulation = nullptr;
+        }
+        if (priv->profile_name) {
+            gtk_container_remove(GTK_CONTAINER(priv->box_profil_settings), priv->profile_name);
+            priv->profile_name = nullptr;
+        }
+    }
+}
diff --git a/src/generalsettingsview.h b/src/generalsettingsview.h
index 8557eb7..2a24e88 100644
--- a/src/generalsettingsview.h
+++ b/src/generalsettingsview.h
@@ -35,6 +35,7 @@
 
 GType      general_settings_view_get_type      (void) G_GNUC_CONST;
 GtkWidget *general_settings_view_new           (void);
+void       general_settings_view_show_profile  (GeneralSettingsView *self, gboolean show_profile);
 
 G_END_DECLS
 
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index d796116..f574c18 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -423,6 +423,10 @@
         if (priv->last_settings_view == priv->media_settings_view)
             media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
 
+        /* make sure to show the profile if we're showing the general settings */
+        if (priv->last_settings_view == priv->general_settings_view)
+            general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
+
         gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_UP);
         gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->last_settings_view);
 
@@ -448,6 +452,7 @@
 
         /* make sure video preview is stopped, in case it was started */
         media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
+        general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
 
         gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_DOWN);
         gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
@@ -493,9 +498,12 @@
     RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
 
     if (gtk_toggle_button_get_active(navbutton)) {
+        general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
         gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
         gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
         priv->last_settings_view = priv->general_settings_view;
+    } else {
+        general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
     }
 }