Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -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. |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 3 | * Author: Nicolas Jager <nicolas.jager@savoirfairelinux.com> |
| 4 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> |
| 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 | #include "avatarmanipulation.h" |
| 22 | |
| 23 | /* LRC */ |
| 24 | #include <globalinstances.h> |
| 25 | #include <person.h> |
| 26 | #include <profile.h> |
| 27 | #include <profilemodel.h> |
| 28 | #include <video/configurationproxy.h> |
| 29 | #include <video/previewmanager.h> |
| 30 | #include <video/devicemodel.h> |
| 31 | |
| 32 | /* client */ |
| 33 | #include "native/pixbufmanipulator.h" |
| 34 | #include "video/video_widget.h" |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 35 | #include "cc-crop-area.h" |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 36 | |
| 37 | /* system */ |
| 38 | #include <glib/gi18n.h> |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 39 | |
| 40 | /* size of avatar */ |
| 41 | static constexpr int AVATAR_WIDTH = 100; /* px */ |
| 42 | static constexpr int AVATAR_HEIGHT = 100; /* px */ |
| 43 | |
| 44 | /* size of video widget */ |
| 45 | static constexpr int VIDEO_WIDTH = 300; /* px */ |
| 46 | static constexpr int VIDEO_HEIGHT = 200; /* px */ |
| 47 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 48 | struct _AvatarManipulation |
| 49 | { |
| 50 | GtkBox parent; |
| 51 | }; |
| 52 | |
| 53 | struct _AvatarManipulationClass |
| 54 | { |
| 55 | GtkBoxClass parent_class; |
| 56 | }; |
| 57 | |
| 58 | typedef struct _AvatarManipulationPrivate AvatarManipulationPrivate; |
| 59 | |
| 60 | struct _AvatarManipulationPrivate |
| 61 | { |
| 62 | GtkWidget *stack_avatar_manipulation; |
| 63 | GtkWidget *video_widget; |
| 64 | GtkWidget *box_views_and_controls; |
| 65 | GtkWidget *box_controls; |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 66 | |
| 67 | GtkWidget *button_box_current; |
| 68 | GtkWidget *button_box_photo; |
| 69 | GtkWidget *button_box_edit; |
| 70 | |
| 71 | GtkWidget *button_start_camera; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 72 | GtkWidget *button_choose_picture; |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 73 | GtkWidget *button_take_photo; |
| 74 | GtkWidget *button_return_photo; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 75 | GtkWidget *button_set_avatar; |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 76 | GtkWidget *button_return_edit; |
| 77 | |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 78 | // GtkWidget *selector_widget; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 79 | GtkWidget *stack_views; |
| 80 | GtkWidget *image_avatar; |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 81 | GtkWidget *vbox_crop_area; |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 82 | GtkWidget *frame_video; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 83 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 84 | AvatarManipulationState state; |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 85 | AvatarManipulationState last_state; |
| 86 | |
| 87 | /* this is used to keep track of the state of the preview when the camera is used to take a |
| 88 | * photo; if a call is in progress, then the preview should already be started and we don't want |
| 89 | * to stop it when the settings are closed, in this case |
| 90 | */ |
| 91 | gboolean video_started_by_avatar_manipulation; |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 92 | |
| 93 | GtkWidget *crop_area; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | G_DEFINE_TYPE_WITH_PRIVATE(AvatarManipulation, avatar_manipulation, GTK_TYPE_BOX); |
| 97 | |
| 98 | #define AVATAR_MANIPULATION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), AVATAR_MANIPULATION_TYPE, \ |
| 99 | AvatarManipulationPrivate)) |
| 100 | |
| 101 | static void set_state(AvatarManipulation *self, AvatarManipulationState state); |
| 102 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 103 | static void start_camera(AvatarManipulation *self); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 104 | static void take_a_photo(AvatarManipulation *self); |
| 105 | static void choose_picture(AvatarManipulation *self); |
Stepan Salenikovich | d9a3830 | 2016-09-14 15:08:11 -0400 | [diff] [blame] | 106 | static void return_to_previous(AvatarManipulation *self); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 107 | static void update_preview_cb(GtkFileChooser *file_chooser, GtkWidget *preview); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 108 | static void set_avatar(AvatarManipulation *self); |
| 109 | static void got_snapshot(AvatarManipulation *parent); |
| 110 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 111 | static void |
| 112 | avatar_manipulation_dispose(GObject *object) |
| 113 | { |
| 114 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(object); |
| 115 | |
| 116 | /* make sure we stop the preview and the video widget */ |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 117 | if (priv->video_started_by_avatar_manipulation) |
| 118 | Video::PreviewManager::instance().stopPreview(); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 119 | if (priv->video_widget) { |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 120 | gtk_container_remove(GTK_CONTAINER(priv->frame_video), priv->video_widget); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 121 | priv->video_widget = NULL; |
| 122 | } |
| 123 | |
| 124 | G_OBJECT_CLASS(avatar_manipulation_parent_class)->dispose(object); |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | avatar_manipulation_finalize(GObject *object) |
| 129 | { |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 130 | G_OBJECT_CLASS(avatar_manipulation_parent_class)->finalize(object); |
| 131 | } |
| 132 | |
| 133 | GtkWidget* |
| 134 | avatar_manipulation_new(void) |
| 135 | { |
| 136 | // a profile must exist |
| 137 | g_return_val_if_fail(ProfileModel::instance().selectedProfile(), NULL); |
| 138 | |
| 139 | return (GtkWidget *)g_object_new(AVATAR_MANIPULATION_TYPE, NULL); |
| 140 | } |
| 141 | |
| 142 | GtkWidget* |
| 143 | avatar_manipulation_new_from_wizard(void) |
| 144 | { |
| 145 | auto self = avatar_manipulation_new(); |
| 146 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 147 | /* in this mode, we want to automatically go to the PHOTO avatar state, unless one already exists */ |
| 148 | if (!ProfileModel::instance().selectedProfile()->person()->photo().isValid()) { |
| 149 | // check if there is a camera |
| 150 | if (Video::DeviceModel::instance().rowCount() > 0) |
| 151 | set_state(AVATAR_MANIPULATION(self), AVATAR_MANIPULATION_STATE_PHOTO); |
| 152 | } |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 153 | |
| 154 | return self; |
| 155 | } |
| 156 | |
| 157 | static void |
| 158 | avatar_manipulation_class_init(AvatarManipulationClass *klass) |
| 159 | { |
| 160 | G_OBJECT_CLASS(klass)->finalize = avatar_manipulation_finalize; |
| 161 | G_OBJECT_CLASS(klass)->dispose = avatar_manipulation_dispose; |
| 162 | |
| 163 | gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass), "/cx/ring/RingGnome/avatarmanipulation.ui"); |
| 164 | |
| 165 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, box_views_and_controls); |
| 166 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, box_controls); |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 167 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_start_camera); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 168 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_choose_picture); |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 169 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_take_photo); |
| 170 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_return_photo); |
| 171 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_set_avatar); |
| 172 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_return_edit); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 173 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, stack_views); |
| 174 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, image_avatar); |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 175 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, frame_video); |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 176 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, vbox_crop_area); |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 177 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_box_current); |
| 178 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_box_photo); |
| 179 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), AvatarManipulation, button_box_edit); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void |
| 183 | avatar_manipulation_init(AvatarManipulation *self) |
| 184 | { |
| 185 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 186 | gtk_widget_init_template(GTK_WIDGET(self)); |
| 187 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 188 | /* our desired size for the image area */ |
| 189 | gtk_widget_set_size_request(priv->stack_views, VIDEO_WIDTH, VIDEO_HEIGHT); |
| 190 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 191 | /* signals */ |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 192 | g_signal_connect_swapped(priv->button_start_camera, "clicked", G_CALLBACK(start_camera), self); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 193 | g_signal_connect_swapped(priv->button_choose_picture, "clicked", G_CALLBACK(choose_picture), self); |
| 194 | g_signal_connect_swapped(priv->button_take_photo, "clicked", G_CALLBACK(take_a_photo), self); |
Stepan Salenikovich | d9a3830 | 2016-09-14 15:08:11 -0400 | [diff] [blame] | 195 | g_signal_connect_swapped(priv->button_return_photo, "clicked", G_CALLBACK(return_to_previous), self); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 196 | g_signal_connect_swapped(priv->button_set_avatar, "clicked", G_CALLBACK(set_avatar), self); |
Stepan Salenikovich | d9a3830 | 2016-09-14 15:08:11 -0400 | [diff] [blame] | 197 | g_signal_connect_swapped(priv->button_return_edit, "clicked", G_CALLBACK(return_to_previous), self); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 198 | |
| 199 | set_state(self, AVATAR_MANIPULATION_STATE_CURRENT); |
| 200 | |
| 201 | gtk_widget_show_all(priv->stack_views); |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | set_state(AvatarManipulation *self, AvatarManipulationState state) |
| 206 | { |
| 207 | // note: this funciton does not check if the state transition is valid, this is assumed to have |
| 208 | // been done by the caller |
| 209 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 210 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 211 | // save prev state |
| 212 | priv->last_state = priv->state; |
| 213 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 214 | switch (state) { |
| 215 | case AVATAR_MANIPULATION_STATE_CURRENT: |
| 216 | { |
| 217 | /* get the current or default profile avatar */ |
| 218 | auto photo = GlobalInstances::pixmapManipulator().contactPhoto( |
| 219 | ProfileModel::instance().selectedProfile()->person(), |
| 220 | QSize(AVATAR_WIDTH, AVATAR_HEIGHT), |
| 221 | false); |
| 222 | std::shared_ptr<GdkPixbuf> pixbuf_photo = photo.value<std::shared_ptr<GdkPixbuf>>(); |
| 223 | |
| 224 | if (photo.isValid()) { |
| 225 | gtk_image_set_from_pixbuf (GTK_IMAGE(priv->image_avatar), pixbuf_photo.get()); |
| 226 | } else { |
| 227 | g_warning("invlid pixbuf"); |
| 228 | } |
| 229 | |
| 230 | gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_views), "page_avatar"); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 231 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 232 | /* available actions: start camera (if available) or choose image */ |
| 233 | if (Video::DeviceModel::instance().rowCount() > 0) { |
| 234 | // TODO: update if a video device gets inserted while in this state |
| 235 | gtk_widget_set_visible(priv->button_start_camera, true); |
| 236 | } |
| 237 | gtk_widget_set_visible(priv->button_box_current, true); |
| 238 | gtk_widget_set_visible(priv->button_box_photo, false); |
| 239 | gtk_widget_set_visible(priv->button_box_edit, false); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 240 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 241 | /* make sure video widget and camera is not running */ |
| 242 | if (priv->video_started_by_avatar_manipulation) |
| 243 | Video::PreviewManager::instance().stopPreview(); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 244 | if (priv->video_widget) { |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 245 | gtk_container_remove(GTK_CONTAINER(priv->frame_video), priv->video_widget); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 246 | priv->video_widget = NULL; |
| 247 | } |
| 248 | } |
| 249 | break; |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 250 | case AVATAR_MANIPULATION_STATE_PHOTO: |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 251 | { |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 252 | // start the video; if its not available we should not be in this state |
| 253 | priv->video_widget = video_widget_new(); |
| 254 | g_signal_connect_swapped(priv->video_widget, "snapshot-taken", G_CALLBACK (got_snapshot), self); |
| 255 | gtk_widget_set_vexpand_set(priv->video_widget, FALSE); |
| 256 | gtk_widget_set_hexpand_set(priv->video_widget, FALSE); |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 257 | gtk_container_add(GTK_CONTAINER(priv->frame_video), priv->video_widget); |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 258 | gtk_widget_set_visible(priv->video_widget, true); |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 259 | gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_views), "page_photobooth"); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 260 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 261 | |
| 262 | /* local renderer, but set as "remote" so that it takes up the whole screen */ |
| 263 | video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget), |
| 264 | Video::PreviewManager::instance().previewRenderer(), |
| 265 | VIDEO_RENDERER_REMOTE); |
| 266 | |
| 267 | if (!Video::PreviewManager::instance().isPreviewing()) { |
| 268 | priv->video_started_by_avatar_manipulation = TRUE; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 269 | Video::PreviewManager::instance().startPreview(); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 270 | } else { |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 271 | priv->video_started_by_avatar_manipulation = FALSE; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 274 | /* available actions: take snapshot, return*/ |
| 275 | gtk_widget_set_visible(priv->button_box_current, false); |
| 276 | gtk_widget_set_visible(priv->button_box_photo, true); |
| 277 | gtk_widget_set_visible(priv->button_box_edit, false); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 278 | } |
| 279 | break; |
| 280 | case AVATAR_MANIPULATION_STATE_EDIT: |
| 281 | { |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 282 | /* make sure video widget and camera is not running */ |
| 283 | if (priv->video_started_by_avatar_manipulation) |
| 284 | Video::PreviewManager::instance().stopPreview(); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 285 | if (priv->video_widget) { |
Stepan Salenikovich | 1c1b0d8 | 2016-07-29 08:13:59 -0400 | [diff] [blame] | 286 | gtk_container_remove(GTK_CONTAINER(priv->frame_video), priv->video_widget); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 287 | priv->video_widget = NULL; |
| 288 | } |
| 289 | |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 290 | /* available actions: set avatar, return */ |
| 291 | gtk_widget_set_visible(priv->button_box_current, false); |
| 292 | gtk_widget_set_visible(priv->button_box_photo, false); |
| 293 | gtk_widget_set_visible(priv->button_box_edit, true); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 294 | |
| 295 | gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_views), "page_edit_view"); |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | priv->state = state; |
| 301 | } |
| 302 | |
| 303 | static void |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 304 | start_camera(AvatarManipulation *self) |
| 305 | { |
| 306 | set_state(self, AVATAR_MANIPULATION_STATE_PHOTO); |
| 307 | } |
| 308 | |
| 309 | static void |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 310 | take_a_photo(AvatarManipulation *self) |
| 311 | { |
| 312 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 313 | video_widget_take_snapshot(VIDEO_WIDGET(priv->video_widget)); |
| 314 | } |
| 315 | |
| 316 | static void |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 317 | set_avatar(AvatarManipulation *self) |
| 318 | { |
| 319 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 320 | |
| 321 | gchar* png_buffer_signed = nullptr; |
| 322 | gsize png_buffer_size; |
| 323 | GError* error = nullptr; |
| 324 | |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 325 | /* get the cropped area */ |
| 326 | GdkPixbuf *selector_pixbuf = cc_crop_area_get_picture(CC_CROP_AREA(priv->crop_area)); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 327 | |
| 328 | /* scale it */ |
| 329 | GdkPixbuf* pixbuf_frame_resized = gdk_pixbuf_scale_simple(selector_pixbuf, AVATAR_WIDTH, AVATAR_HEIGHT, |
| 330 | GDK_INTERP_HYPER); |
| 331 | |
| 332 | /* save the png in memory */ |
| 333 | gdk_pixbuf_save_to_buffer(pixbuf_frame_resized, &png_buffer_signed, &png_buffer_size, "png", &error, NULL); |
| 334 | if (!png_buffer_signed) { |
| 335 | g_warning("(set_avatar) failed to save pixbuffer to png: %s\n", error->message); |
| 336 | g_error_free(error); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | /* convert buffer to QByteArray in base 64*/ |
| 341 | QByteArray png_q_byte_array = QByteArray::fromRawData(png_buffer_signed, png_buffer_size).toBase64(); |
| 342 | |
| 343 | /* save in profile */ |
| 344 | QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(png_q_byte_array); |
| 345 | ProfileModel::instance().selectedProfile()->person()->setPhoto(photo); |
| 346 | ProfileModel::instance().selectedProfile()->save(); |
| 347 | |
| 348 | g_free(png_buffer_signed); |
| 349 | g_object_unref(selector_pixbuf); |
| 350 | g_object_unref(pixbuf_frame_resized); |
| 351 | |
| 352 | set_state(self, AVATAR_MANIPULATION_STATE_CURRENT); |
| 353 | } |
| 354 | |
| 355 | static void |
Stepan Salenikovich | d9a3830 | 2016-09-14 15:08:11 -0400 | [diff] [blame] | 356 | return_to_previous(AvatarManipulation *self) |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 357 | { |
| 358 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
Stepan Salenikovich | d9a3830 | 2016-09-14 15:08:11 -0400 | [diff] [blame] | 359 | |
| 360 | if (priv->state == AVATAR_MANIPULATION_STATE_PHOTO) { |
| 361 | // from photo we alway go back to current |
| 362 | set_state(self, AVATAR_MANIPULATION_STATE_CURRENT); |
| 363 | } else { |
| 364 | // otherwise, if we were in edit state, we may have come from photo or current state |
| 365 | set_state(self, priv->last_state); |
| 366 | } |
Stepan Salenikovich | ae92efd | 2016-07-28 18:51:27 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 370 | choose_picture(AvatarManipulation *self) |
| 371 | { |
| 372 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 373 | GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 374 | gint res; |
| 375 | |
| 376 | auto preview = gtk_image_new(); |
| 377 | |
| 378 | GtkWidget *ring_main_window = gtk_widget_get_toplevel(GTK_WIDGET(self)); |
| 379 | |
| 380 | auto dialog = gtk_file_chooser_dialog_new (_("Open Avatar Image"), |
| 381 | GTK_WINDOW(ring_main_window), |
| 382 | action, |
| 383 | _("_Cancel"), |
| 384 | GTK_RESPONSE_CANCEL, |
| 385 | _("_Open"), |
| 386 | GTK_RESPONSE_ACCEPT, |
| 387 | NULL); |
| 388 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 389 | /* add an image preview inside the file choose */ |
| 390 | gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(dialog), preview); |
| 391 | g_signal_connect (GTK_FILE_CHOOSER(dialog), "update-preview", G_CALLBACK (update_preview_cb), preview); |
| 392 | |
| 393 | /* start the file chooser */ |
| 394 | res = gtk_dialog_run (GTK_DIALOG(dialog)); /* blocks until the dialog is closed */ |
| 395 | |
| 396 | if (res == GTK_RESPONSE_ACCEPT) { |
| 397 | if(auto filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog))) { |
| 398 | GError* error = nullptr; /* initialising to null avoid trouble... */ |
| 399 | |
Stepan Salenikovich | d8a9ce2 | 2016-09-15 13:36:05 -0400 | [diff] [blame] | 400 | auto picture = gdk_pixbuf_new_from_file_at_size (filename, VIDEO_WIDTH, VIDEO_HEIGHT, &error); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 401 | |
| 402 | if (!error) { |
Stepan Salenikovich | d8a9ce2 | 2016-09-15 13:36:05 -0400 | [diff] [blame] | 403 | /* reset crop area */ |
| 404 | if (priv->crop_area) |
| 405 | gtk_container_remove(GTK_CONTAINER(priv->vbox_crop_area), priv->crop_area); |
| 406 | priv->crop_area = cc_crop_area_new(); |
| 407 | gtk_widget_show(priv->crop_area); |
| 408 | gtk_box_pack_start(GTK_BOX(priv->vbox_crop_area), priv->crop_area, TRUE, TRUE, 0); |
| 409 | cc_crop_area_set_picture(CC_CROP_AREA(priv->crop_area), picture); |
| 410 | g_object_unref(picture); |
| 411 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 412 | set_state(self, AVATAR_MANIPULATION_STATE_EDIT); |
| 413 | } else { |
| 414 | g_warning("(choose_picture) failed to load pixbuf from file: %s", error->message); |
| 415 | g_error_free(error); |
| 416 | } |
| 417 | |
| 418 | g_free(filename); |
| 419 | } else { |
| 420 | g_warning("(choose_picture) filename empty"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | gtk_widget_destroy(dialog); |
| 425 | } |
| 426 | |
| 427 | static void |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 428 | update_preview_cb(GtkFileChooser *file_chooser, GtkWidget *preview) |
| 429 | { |
| 430 | gboolean have_preview = FALSE; |
| 431 | if (auto filename = gtk_file_chooser_get_preview_filename(file_chooser)) { |
| 432 | GError* error = nullptr; |
| 433 | auto pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, &error); |
| 434 | if (!error) { |
| 435 | gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); |
| 436 | g_object_unref(pixbuf); |
| 437 | have_preview = TRUE; |
| 438 | } else { |
| 439 | // nothing to do, the file is probably not a picture |
| 440 | } |
| 441 | g_free (filename); |
| 442 | } |
| 443 | gtk_file_chooser_set_preview_widget_active(file_chooser, have_preview); |
| 444 | } |
| 445 | |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 446 | static void |
| 447 | got_snapshot(AvatarManipulation *self) |
| 448 | { |
| 449 | AvatarManipulationPrivate *priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 450 | GdkPixbuf* pix = video_widget_get_snapshot(VIDEO_WIDGET(priv->video_widget)); |
| 451 | |
Stepan Salenikovich | d8a9ce2 | 2016-09-15 13:36:05 -0400 | [diff] [blame] | 452 | if (priv->crop_area) |
| 453 | gtk_container_remove(GTK_CONTAINER(priv->vbox_crop_area), priv->crop_area); |
| 454 | priv->crop_area = cc_crop_area_new(); |
| 455 | gtk_widget_show(priv->crop_area); |
| 456 | gtk_box_pack_start(GTK_BOX(priv->vbox_crop_area), priv->crop_area, TRUE, TRUE, 0); |
Stepan Salenikovich | 5c54b35 | 2016-09-14 14:28:19 -0400 | [diff] [blame] | 457 | cc_crop_area_set_picture(CC_CROP_AREA(priv->crop_area), pix); |
Nicolas Jager | b413b30 | 2016-05-06 11:41:32 -0400 | [diff] [blame] | 458 | |
| 459 | set_state(self, AVATAR_MANIPULATION_STATE_EDIT); |
| 460 | } |
Stepan Salenikovich | ec16deb | 2017-04-14 14:18:37 -0400 | [diff] [blame] | 461 | |
| 462 | void |
| 463 | avatar_manipulation_wizard_completed(AvatarManipulation *self) |
| 464 | { |
| 465 | auto priv = AVATAR_MANIPULATION_GET_PRIVATE(self); |
| 466 | |
| 467 | /* Tuleap: #1441 |
| 468 | * if the user did not validate the avatar area selection, we still take that as the image |
| 469 | * for their avatar; otherwise many users end up with no avatar by default |
| 470 | * TODO: improve avatar creation process to not need this fix |
| 471 | */ |
| 472 | if (priv->state == AVATAR_MANIPULATION_STATE_EDIT) |
| 473 | set_avatar(self); |
| 474 | } |