blob: 1f6a6230ef99593008ed3a0f463c8f3c01620e9b [file] [log] [blame]
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05003 * 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.
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050018 */
19
20#include "ringmainwindow.h"
21
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040022// system
23#include <string.h>
24#include <memory>
25
26// GTK+ related
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040027#include <glib/gi18n.h>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040028
29// Qt
30#include <QtCore/QItemSelectionModel>
31#include <QtCore/QSortFilterProxyModel>
Stepan Salenikoviche9933242016-06-21 18:08:48 -040032#include <QtCore/QDateTime>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040033
34// LRC
Stepan Salenikovicha3557452015-02-20 14:14:12 -050035#include <callmodel.h>
Stepan Salenikovich69771842015-02-24 18:11:45 -050036#include <call.h>
Stepan Salenikovich15142182015-03-11 17:15:26 -040037#include <contactmethod.h>
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040038#include <accountmodel.h>
Edric Milaret58e71072016-03-21 12:16:37 -040039#include <codecmodel.h>
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040040#include <video/previewmanager.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040041#include <personmodel.h>
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040042#include <globalinstances.h>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040043#include <categorizedcontactmodel.h>
44#include <recentmodel.h>
45#include <profilemodel.h>
46#include <profile.h>
Stepan Salenikovichf53128f2016-10-07 10:32:16 -040047#include <phonedirectorymodel.h>
Stepan Salenikovich7b7be482016-11-03 16:57:48 -040048#include <availableaccountmodel.h>
Nicolas Jager0efc8432017-03-22 16:22:00 -040049#include <contactrequest.h>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040050
51// Ring client
52#include "models/gtkqtreemodel.h"
53#include "incomingcallview.h"
54#include "currentcallview.h"
Stepan Salenikovichf6078222016-10-03 17:31:16 -040055#include "models/gtkqtreemodel.h"
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040056#include "accountview.h"
57#include "dialogs.h"
58#include "mediasettingsview.h"
59#include "utils/drawing.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040060#include "native/pixbufmanipulator.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040061#include "models/activeitemproxymodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040062#include "utils/calling.h"
63#include "contactsview.h"
64#include "historyview.h"
65#include "utils/models.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040066#include "generalsettingsview.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040067#include "utils/accounts.h"
Stepan Salenikovich2cde7612015-09-25 10:44:01 -040068#include "ringwelcomeview.h"
aviau6aeb4852016-08-18 16:01:09 -040069#include "accountmigrationview.h"
70#include "accountcreationwizard.h"
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040071#include "recentcontactsview.h"
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050072#include "chatview.h"
Nicolas Jagerb413b302016-05-06 11:41:32 -040073#include "avatarmanipulation.h"
Stepan Salenikovichba87ae22016-09-13 14:27:20 -040074#include "utils/files.h"
Nicolas Jager0efc8432017-03-22 16:22:00 -040075#include "pendingcontactrequests.h"
Nicolas Jagerb413b302016-05-06 11:41:32 -040076
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040077static constexpr const char* CALL_VIEW_NAME = "calls";
aviau6aeb4852016-08-18 16:01:09 -040078static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard";
79static constexpr const char* ACCOUNT_MIGRATION_VIEW_NAME = "account-migration-view";
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040080static constexpr const char* GENERAL_SETTINGS_VIEW_NAME = "general";
81static constexpr const char* AUDIO_SETTINGS_VIEW_NAME = "audio";
82static constexpr const char* MEDIA_SETTINGS_VIEW_NAME = "media";
83static constexpr const char* ACCOUNT_SETTINGS_VIEW_NAME = "accounts";
Stepan Salenikovich2cde7612015-09-25 10:44:01 -040084static constexpr const char* DEFAULT_VIEW_NAME = "welcome";
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040085static constexpr const char* VIEW_CONTACTS = "contacts";
86static constexpr const char* VIEW_HISTORY = "history";
87static constexpr const char* VIEW_PRESENCE = "presence";
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050088
89struct _RingMainWindow
90{
91 GtkApplicationWindow parent;
92};
93
94struct _RingMainWindowClass
95{
96 GtkApplicationWindowClass parent_class;
97};
98
99typedef struct _RingMainWindowPrivate RingMainWindowPrivate;
100
101struct _RingMainWindowPrivate
102{
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400103 GtkWidget *ring_menu;
104 GtkWidget *image_ring;
105 GtkWidget *ring_settings;
106 GtkWidget *image_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400107 GtkWidget *hbox_settings;
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400108 GtkWidget *scrolled_window_smartview;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400109 GtkWidget *treeview_conversations;
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -0400110 GtkWidget *scrolled_window_contacts;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400111 GtkWidget *treeview_contacts;
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -0400112 GtkWidget *scrolled_window_history;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400113 GtkWidget *treeview_history;
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -0400114 GtkWidget *vbox_left_pane;
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -0500115 GtkWidget *search_entry;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500116 GtkWidget *stack_main_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400117 GtkWidget *vbox_call_view;
Stepan Salenikovich3034d922015-10-09 10:11:42 -0400118 GtkWidget *frame_call;
119 GtkWidget *welcome_view;
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400120 GtkWidget *button_new_conversation ;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400121 GtkWidget *account_settings_view;
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400122 GtkWidget *media_settings_view;
Stepan Salenikovichde896112015-05-11 16:46:33 -0400123 GtkWidget *general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400124 GtkWidget *last_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400125 GtkWidget *radiobutton_general_settings;
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400126 GtkWidget *radiobutton_media_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400127 GtkWidget *radiobutton_account_settings;
aviau69081842016-10-14 14:51:54 -0400128 GtkWidget *account_creation_wizard;
129 GtkWidget *account_migration_view;
Nicolas Jager55903142017-02-01 14:43:45 -0500130 GtkWidget *spinner_lookup;
Nicolas Jager15a8b902017-03-21 07:53:06 -0400131 GtkWidget *combobox_account_selector;
Nicolas Jager0efc8432017-03-22 16:22:00 -0400132 GtkWidget *treeview_contact_requests;
133 GtkWidget *scrolled_window_contact_requests;
134 GtkWidget *contact_request_view;
Stepan Salenikovich5eab3032015-03-28 10:50:51 -0400135
aviau8c0b2422016-10-31 16:06:44 -0400136 /* Pending ring usernames lookup for the search entry */
137 QMetaObject::Connection username_lookup;
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400138 std::string* pending_username_lookup;
aviau8c0b2422016-10-31 16:06:44 -0400139
aviau039001d2016-09-29 16:39:05 -0400140 /* The webkit_chat_container is created once, then reused for all chat views */
141 GtkWidget *webkit_chat_container;
142
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400143 QMetaObject::Connection selected_item_changed;
144 QMetaObject::Connection selected_call_over;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400145
146 gboolean show_settings;
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400147
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500148 /* fullscreen */
149 gboolean is_fullscreen;
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400150
151 GSettings *settings;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500152};
153
154G_DEFINE_TYPE_WITH_PRIVATE(RingMainWindow, ring_main_window, GTK_TYPE_APPLICATION_WINDOW);
155
156#define RING_MAIN_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_MAIN_WINDOW_TYPE, RingMainWindowPrivate))
157
aviau039001d2016-09-29 16:39:05 -0400158static gboolean selection_changed(RingMainWindow *win);
159
160static WebKitChatContainer*
161get_webkit_chat_container(RingMainWindow *win)
162{
163 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
164 if (!priv->webkit_chat_container)
165 {
166 priv->webkit_chat_container = webkit_chat_container_new();
167
168 //We don't want it to be deleted, ever.
169 g_object_ref(priv->webkit_chat_container);
170 }
171 return WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container);
172}
173
Stepan Salenikovich69771842015-02-24 18:11:45 -0500174static void
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500175enter_full_screen(RingMainWindow *self)
176{
177 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
178 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
179
180 if (!priv->is_fullscreen) {
181 gtk_widget_hide(priv->vbox_left_pane);
182 gtk_window_fullscreen(GTK_WINDOW(self));
183 priv->is_fullscreen = TRUE;
184 }
185}
186
187static void
188leave_full_screen(RingMainWindow *self)
189{
190 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
191 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
192
193 if (priv->is_fullscreen) {
194 gtk_widget_show(priv->vbox_left_pane);
195 gtk_window_unfullscreen(GTK_WINDOW(self));
196 priv->is_fullscreen = FALSE;
197 }
198}
199
200static void
Stepan Salenikovich5e431cf2015-12-24 14:09:01 -0500201video_double_clicked(G_GNUC_UNUSED CurrentCallView *view, RingMainWindow *self)
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500202{
203 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
204 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
205
206 if (priv->is_fullscreen) {
207 leave_full_screen(self);
208 } else {
209 enter_full_screen(self);
210 }
211}
212
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400213static void
214hide_view_clicked(G_GNUC_UNUSED GtkWidget *view, RingMainWindow *self)
215{
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400216 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400217
218 /* clear selection */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400219 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
220 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
221 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
222 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
223 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
224 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
Nicolas Jager0efc8432017-03-22 16:22:00 -0400225 auto selection_contact_request = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contact_requests));
226 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contact_request));
227
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400228}
229
aviau039001d2016-09-29 16:39:05 -0400230static void
231change_view(RingMainWindow *self, GtkWidget* old, QObject *object, GType type)
232{
233 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
234 leave_full_screen(self);
235 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old);
236
237 GtkWidget *new_view = nullptr;
238
239 QObject::disconnect(priv->selected_item_changed);
240 QObject::disconnect(priv->selected_call_over);
241
242 if (g_type_is_a(INCOMING_CALL_VIEW_TYPE, type)) {
243 if (auto call = qobject_cast<Call *>(object)) {
244 new_view = incoming_call_view_new(call, get_webkit_chat_container(self));
245 priv->selected_item_changed = QObject::connect(
246 call,
247 &Call::lifeCycleStateChanged,
248 [self] (Call::LifeCycleState, Call::LifeCycleState)
249 {g_idle_add((GSourceFunc)selection_changed, self);}
250 );
251 priv->selected_call_over = QObject::connect(
252 call,
253 &Call::isOver,
254 [self] ()
255 {g_idle_add((GSourceFunc)selection_changed, self);}
256 );
257 } else
258 g_warning("Trying to display a view of type IncomingCallView, but the object is not of type Call");
259 } else if (g_type_is_a(CURRENT_CALL_VIEW_TYPE, type)) {
260 if (auto call = qobject_cast<Call *>(object)) {
261 new_view = current_call_view_new(call, get_webkit_chat_container(self));
262 g_signal_connect(new_view, "video-double-clicked", G_CALLBACK(video_double_clicked), self);
263 priv->selected_item_changed = QObject::connect(
264 call,
265 &Call::lifeCycleStateChanged,
266 [self] (Call::LifeCycleState, Call::LifeCycleState)
267 {g_idle_add((GSourceFunc)selection_changed, self);}
268 );
269 priv->selected_call_over = QObject::connect(
270 call,
271 &Call::isOver,
272 [self] ()
273 {g_idle_add((GSourceFunc)selection_changed, self);}
274 );
275 } else
276 g_warning("Trying to display a view of type CurrentCallView, but the object is not of type Call");
277 } else if (g_type_is_a(CHAT_VIEW_TYPE, type)) {
278 if (auto person = qobject_cast<Person *>(object)) {
279 new_view = chat_view_new_person(get_webkit_chat_container(self), person);
280 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
281
282 /* connect to the Person's callAdded signal, because we want to switch to the call view
283 * in this case */
284 priv->selected_item_changed = QObject::connect(
285 person,
286 &Person::callAdded,
287 [self] (Call*)
288 {g_idle_add((GSourceFunc)selection_changed, self);}
289 );
290 } else if (auto cm = qobject_cast<ContactMethod *>(object)) {
291 new_view = chat_view_new_cm(get_webkit_chat_container(self), cm);
292 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
293
294 /* connect to the ContactMethod's callAdded signal, because we want to switch to the
295 * call view in this case */
296 priv->selected_item_changed = QObject::connect(
297 cm,
298 &ContactMethod::callAdded,
299 [self] (Call*)
300 {g_idle_add((GSourceFunc)selection_changed, self);}
301 );
302 } else {
303 g_warning("Trying to display a veiw of type ChatView, but the object is neither a Person nor a ContactMethod");
304 }
305 } else {
306 // display the welcome view
307 new_view = priv->welcome_view;
308 }
309
310 gtk_container_add(GTK_CONTAINER(priv->frame_call), new_view);
311 gtk_widget_show(new_view);
312}
313
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500314/**
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400315 * This function determines which view to display in the right panel of the main window based on
316 * which item is selected in one of the 3 contact list views (Conversations, Contacts, History). The
317 * possible views ares:
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500318 * - incoming call view
319 * - current call view
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500320 * - chat view
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400321 * - welcome view (if no valid item is selected)
322 *
323 * There should never be a conflict of which item should be displayed (ie: which item is selected),
324 * as there should only ever be one selection at a time in the 3 views except in the case that the
325 * same item is selected in more than one view (see the compare_treeview_selection() function).
326 *
327 * This function could be called from a g_idle source, so it returns a boolean to remove itself from
328 * being called again. The boolean doesn't reflect success nor failure.
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500329 */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400330static gboolean
331selection_changed(RingMainWindow *win)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500332{
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400333
334 g_return_val_if_fail(IS_RING_MAIN_WINDOW(win), G_SOURCE_REMOVE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500335 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(win));
336
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400337 auto old_view = gtk_bin_get_child(GTK_BIN(priv->frame_call));
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500338
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400339 /* if we're showing the settings, then we need to make sure to remove any possible ongoing call
340 * view so that we don't have more than one VideoWidget at a time */
341 if (priv->show_settings) {
342 if (old_view != priv->welcome_view) {
343 leave_full_screen(win);
344 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old_view);
345 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
346 gtk_widget_show(priv->welcome_view);
347 }
348 return G_SOURCE_REMOVE;
349 }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500350
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400351 /* there should only be one item selected at a time, get which one is selected */
352 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
353 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
354 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
Nicolas Jager0efc8432017-03-22 16:22:00 -0400355 auto selection_contact_request = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contact_requests));
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400356
357 GtkTreeModel *model = nullptr;
358 GtkTreeIter iter;
359 QModelIndex idx;
360
361 if (gtk_tree_selection_get_selected(selection_conversations, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400362 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400363 } else if (gtk_tree_selection_get_selected(selection_contacts, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400364 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400365 } else if (gtk_tree_selection_get_selected(selection_history, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400366 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Nicolas Jager0efc8432017-03-22 16:22:00 -0400367 } else if (gtk_tree_selection_get_selected(selection_contact_request, &model, &iter)) {
368 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400369 }
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500370
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500371 /* check which object type is selected */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400372 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
373 auto object = idx.data(static_cast<int>(Ring::Role::Object));
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500374
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400375 if (idx.isValid() && type.isValid() && object.isValid()) {
Stepan Salenikovich69771842015-02-24 18:11:45 -0500376
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400377 /* we prioritize the views in the following order:
378 * - call view (if there is an ongoing call associated with the item)
379 * - chat view built from Person
380 * - chat view built from ContactMethod
381 */
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500382
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400383 Person *person = nullptr;
384 ContactMethod *cm = nullptr;
385 Call *call = nullptr;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500386
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400387 /* use the RecentModel to see if there are any ongoing calls associated with the selected item */
388 switch(type.value<Ring::ObjectType>()) {
389 case Ring::ObjectType::Person:
390 {
391 person = object.value<Person *>();
392 auto recent_idx = RecentModel::instance().getIndex(person);
393 if (recent_idx.isValid()) {
394 call = RecentModel::instance().getActiveCall(recent_idx);
395 }
396 }
397 break;
398 case Ring::ObjectType::ContactMethod:
399 {
400 cm = object.value<ContactMethod *>();
401 auto recent_idx = RecentModel::instance().getIndex(cm);
402 if (recent_idx.isValid()) {
403 call = RecentModel::instance().getActiveCall(recent_idx);
404 }
405 }
406 break;
407 case Ring::ObjectType::Call:
408 {
409 /* if the call is over (eg: if it comes from the history model) then we first check
410 * if there is an ongoing call with the same ContactMethod, otherwise we need to get
411 * the associated Person or ContactMethod */
412 call = object.value<Call *>();
413 if (call->isHistory()) {
414 cm = call->peerContactMethod();
415 call = nullptr;
416 if (cm) {
417 auto recent_idx = RecentModel::instance().getIndex(cm);
418 if (recent_idx.isValid()) {
419 call = RecentModel::instance().getActiveCall(recent_idx);
420 }
421 person = cm->contact();
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500422 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500423 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400424 }
425 break;
426 case Ring::ObjectType::Media:
427 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400428 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400429 case Ring::ObjectType::COUNT__:
430 // nothing to do
431 break;
432 }
433
434 /* we prioritize showing the call view */
435 if (call) {
436 auto state = call->lifeCycleState();
437
438 Call *current_call = nullptr;
439
440 switch(state) {
441 case Call::LifeCycleState::CREATION:
442 case Call::LifeCycleState::INITIALIZATION:
443 case Call::LifeCycleState::FINISHED:
444 // check if we're already displaying this call
445 if (IS_INCOMING_CALL_VIEW(old_view))
446 current_call = incoming_call_view_get_call(INCOMING_CALL_VIEW(old_view));
447 if (current_call != call)
aviau039001d2016-09-29 16:39:05 -0400448 change_view(win, old_view, call, INCOMING_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400449 break;
450 case Call::LifeCycleState::PROGRESS:
451 // check if we're already displaying this call
452 if (IS_CURRENT_CALL_VIEW(old_view))
453 current_call = current_call_view_get_call(CURRENT_CALL_VIEW(old_view));
aviau039001d2016-09-29 16:39:05 -0400454 if (current_call != call)
455 change_view(win, old_view, call, CURRENT_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400456 break;
457 case Call::LifeCycleState::COUNT__:
458 g_warning("LifeCycleState should never be COUNT");
459 break;
460 }
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500461
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400462 } else if (person) {
463 /* show chat view constructed from Person object */
464
465 // check if we're already displaying the chat view for this person
466 Person *current_person = nullptr;
467 if (IS_CHAT_VIEW(old_view))
468 current_person = chat_view_get_person(CHAT_VIEW(old_view));
469
aviau039001d2016-09-29 16:39:05 -0400470 if (current_person != person)
471 change_view(win, old_view, person, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400472 } else if (cm) {
473 /* show chat view constructed from CM */
474
475 // check if we're already displaying the chat view for this cm
476 ContactMethod *current_cm = nullptr;
477 if (IS_CHAT_VIEW(old_view))
478 current_cm = chat_view_get_cm(CHAT_VIEW(old_view));
479
aviau039001d2016-09-29 16:39:05 -0400480 if (current_cm != cm)
481 change_view(win, old_view, cm, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400482 } else {
483 /* not a supported object type, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400484 if (old_view != priv->welcome_view)
485 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500486 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400487 } else {
488 /* selection isn't valid, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400489 if (old_view != priv->welcome_view)
490 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500491 }
Stepan Salenikovich504f7722016-01-20 17:10:45 -0500492
493 return G_SOURCE_REMOVE;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500494}
495
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500496static void
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400497process_search_entry_contact_method(RingMainWindow *self, const URI& uri, Account* accountOrNullptr)
aviau8c0b2422016-10-31 16:06:44 -0400498{
499 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
500
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400501 auto cm = PhoneDirectoryModel::instance().getNumber(uri, accountOrNullptr);
aviau8c0b2422016-10-31 16:06:44 -0400502
503 if (g_settings_get_boolean(priv->settings, "search-entry-places-call")) {
504 place_new_call(cm);
505
506 /* move focus away from entry so that DTMF tones can be entered via the keyboard */
507 gtk_widget_child_focus(GTK_WIDGET(self), GTK_DIR_TAB_FORWARD);
508 } else {
509 // if its a new CM, bring it to the top
510 if (cm->lastUsed() == 0)
511 cm->setLastUsed(QDateTime::currentDateTime().toTime_t());
512
513 // select cm
514 RecentModel::instance().selectionModel()->setCurrentIndex(RecentModel::instance().getIndex(cm), QItemSelectionModel::ClearAndSelect);
515 }
516 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
517}
518
519static void
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400520search_entry_activated(RingMainWindow *self)
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500521{
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400522 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500523
Nicolas Jager55903142017-02-01 14:43:45 -0500524 const auto *querry = gtk_entry_get_text(GTK_ENTRY(priv->search_entry));
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500525
Nicolas Jager55903142017-02-01 14:43:45 -0500526 URI uri = URI(querry);
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400527
Nicolas Jager40916b42017-03-18 14:59:20 -0400528 /* we don't want to lookup a ring id and we require to have a ring account chosen */
529 if (uri.protocolHint() != URI::ProtocolHint::RING && get_active_ring_account()) {
530 *priv->pending_username_lookup = querry;
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400531
Nicolas Jager40916b42017-03-18 14:59:20 -0400532 gtk_spinner_start(GTK_SPINNER(priv->spinner_lookup));
533 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400534
Nicolas Jager40916b42017-03-18 14:59:20 -0400535 QString username_to_lookup = uri.format(URI::Section::USER_INFO|URI::Section::HOSTNAME|URI::Section::PORT);
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400536
Nicolas Jager40916b42017-03-18 14:59:20 -0400537 /* disconect previous lookup */
538 QObject::disconnect(priv->username_lookup);
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500539
Nicolas Jager40916b42017-03-18 14:59:20 -0400540 /* connect new lookup */
541 priv->username_lookup = QObject::connect(
542 &NameDirectory::instance(),
543 &NameDirectory::registeredNameFound,
544 [self, priv, username_to_lookup] (Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) {
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400545
Nicolas Jager40916b42017-03-18 14:59:20 -0400546 auto name_qbarray = name.toLatin1();
547 if ( strcmp(priv->pending_username_lookup->data(), name_qbarray.data()) != 0 )
548 return;
Stepan Salenikovich895a70b2015-03-27 16:02:27 -0400549
Nicolas Jager40916b42017-03-18 14:59:20 -0400550 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
Stepan Salenikovich895a70b2015-03-27 16:02:27 -0400551
Nicolas Jager40916b42017-03-18 14:59:20 -0400552 switch(status)
553 {
554 case NameDirectory::LookupStatus::SUCCESS:
aviau8c0b2422016-10-31 16:06:44 -0400555 {
Nicolas Jager40916b42017-03-18 14:59:20 -0400556 URI uri = URI("ring:" + address);
557 process_search_entry_contact_method(self, uri, account);
558 break;
aviau8c0b2422016-10-31 16:06:44 -0400559 }
Nicolas Jager40916b42017-03-18 14:59:20 -0400560 case NameDirectory::LookupStatus::INVALID_NAME:
561 {
562 //Can't be a ring username, could be something else.
563 auto dialog = gtk_message_dialog_new(
564 GTK_WINDOW(self),
565 GTK_DIALOG_DESTROY_WITH_PARENT,
566 GTK_MESSAGE_ERROR,
567 GTK_BUTTONS_CLOSE,
568 _("Invalid Ring username: %s"),
569 name.toUtf8().constData()
570 );
aviau8c0b2422016-10-31 16:06:44 -0400571
Nicolas Jager40916b42017-03-18 14:59:20 -0400572 gtk_dialog_run (GTK_DIALOG (dialog));
573 gtk_widget_destroy (dialog);
574 break;
575 }
576 case NameDirectory::LookupStatus::ERROR:
577 {
578 auto dialog = gtk_message_dialog_new(
579 GTK_WINDOW(self),
580 GTK_DIALOG_DESTROY_WITH_PARENT,
581 GTK_MESSAGE_ERROR,
582 GTK_BUTTONS_CLOSE,
583 _("Error resolving username, nameserver is possibly unreachable")
584 );
585
586 gtk_dialog_run(GTK_DIALOG (dialog));
587 gtk_widget_destroy(dialog);
588 break;
589 }
590 case NameDirectory::LookupStatus::NOT_FOUND:
591 {
592 auto dialog = gtk_message_dialog_new(
593 GTK_WINDOW(self),
594 GTK_DIALOG_DESTROY_WITH_PARENT,
595 GTK_MESSAGE_ERROR,
596 GTK_BUTTONS_CLOSE,
597 _("Could not resolve Ring username: %s"),
598 name.toUtf8().constData()
599 );
600
601 gtk_dialog_run(GTK_DIALOG (dialog));
602 gtk_widget_destroy(dialog);
603 break;
604 }
605 }
606 *priv->pending_username_lookup = "";
607 gtk_spinner_stop(GTK_SPINNER(priv->spinner_lookup));
608 }
609 );
610
611 /* lookup */
612 NameDirectory::instance().lookupName(get_active_ring_account(), QString(), username_to_lookup);
613 }else // no lookup
614 process_search_entry_contact_method(self, uri, nullptr);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500615}
616
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400617static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400618save_accounts(GtkWidget *working_dialog)
619{
620 /* save changes to accounts */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400621 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400622
623 if (working_dialog)
624 gtk_widget_destroy(working_dialog);
625
626 return G_SOURCE_REMOVE;
627}
628
Stepan Salenikovich15142182015-03-11 17:15:26 -0400629static void
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400630settings_clicked(G_GNUC_UNUSED GtkButton *button, RingMainWindow *win)
631{
632 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
633 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
634
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400635 /* check which view to show */
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500636 if (!priv->show_settings) {
637 /* show the settings */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400638 priv->show_settings = TRUE;
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500639
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500640 /* make sure we are not showing a call view so we don't have more than one clutter stage at a time */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400641 selection_changed(win);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500642
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400643 /* show settings */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400644 gtk_image_set_from_icon_name(GTK_IMAGE(priv->image_settings), "emblem-ok-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400645
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400646 gtk_widget_show(priv->hbox_settings);
647
Stepan Salenikovich41118912015-05-01 11:25:46 -0400648 /* make sure to start preview if we're showing the video settings */
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400649 if (priv->last_settings_view == priv->media_settings_view)
650 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400651
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400652 /* make sure to show the profile if we're showing the general settings */
653 if (priv->last_settings_view == priv->general_settings_view)
654 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
655
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400656 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->last_settings_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400657 } else {
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500658 /* hide the settings */
659 priv->show_settings = FALSE;
660
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400661 /* show working dialog in case save operation takes time */
662 GtkWidget *working = ring_dialog_working(GTK_WIDGET(win), NULL);
663 gtk_window_present(GTK_WINDOW(working));
664
665 /* now save after the time it takes to transition back to the call view (400ms)
666 * the save doesn't happen before the "working" dialog is presented
667 * the timeout function should destroy the "working" dialog when done saving
668 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400669 g_timeout_add_full(G_PRIORITY_DEFAULT, 400, (GSourceFunc)save_accounts, working, NULL);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400670
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400671 /* show calls */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400672 gtk_image_set_from_icon_name(GTK_IMAGE(priv->image_settings), "emblem-system-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400673
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400674 gtk_widget_hide(priv->hbox_settings);
675
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500676 /* make sure video preview is stopped, in case it was started */
677 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400678 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500679
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400680 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400681
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500682 /* show the view which was selected previously */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400683 selection_changed(win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400684 }
685}
686
687static void
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400688show_media_settings(GtkToggleButton *navbutton, RingMainWindow *win)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400689{
690 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
691 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
692
693 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400694 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400695 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400696 priv->last_settings_view = priv->media_settings_view;
Stepan Salenikovich41118912015-05-01 11:25:46 -0400697 } else {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400698 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400699 }
700}
701
702static void
703show_account_settings(GtkToggleButton *navbutton, RingMainWindow *win)
704{
705 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
706 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
707
708 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400709 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), ACCOUNT_SETTINGS_VIEW_NAME);
710 priv->last_settings_view = priv->account_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400711 }
712}
713
Stepan Salenikovichde896112015-05-11 16:46:33 -0400714static void
715show_general_settings(GtkToggleButton *navbutton, RingMainWindow *win)
716{
717 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
718 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
719
720 if (gtk_toggle_button_get_active(navbutton)) {
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400721 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400722 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
723 priv->last_settings_view = priv->general_settings_view;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400724 } else {
725 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400726 }
727}
728
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400729static void
aviau6aeb4852016-08-18 16:01:09 -0400730on_account_creation_completed(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400731{
Stepan Salenikovichbabe26f2016-02-12 16:47:07 -0500732 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400733 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
734
aviau6aeb4852016-08-18 16:01:09 -0400735 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
736
737 /* destroy the wizard */
aviau69081842016-10-14 14:51:54 -0400738 if (priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400739 {
aviau69081842016-10-14 14:51:54 -0400740 gtk_container_remove(GTK_CONTAINER(priv->stack_main_view), priv->account_creation_wizard);
741 gtk_widget_destroy(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400742 }
aviau6aeb4852016-08-18 16:01:09 -0400743
744 /* show the settings button*/
745 gtk_widget_show(priv->ring_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -0400746
747 /* show the account selector */
748 gtk_widget_show(priv->combobox_account_selector);
749
750 /* init the selection for the account selector */
751 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400752}
753
754static void
aviau6aeb4852016-08-18 16:01:09 -0400755show_account_creation_wizard(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400756{
757 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
758
aviau61d05b32016-10-18 16:47:53 -0400759 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400760 {
aviau69081842016-10-14 14:51:54 -0400761 priv->account_creation_wizard = account_creation_wizard_new(false);
762 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
763 g_signal_connect_swapped(priv->account_creation_wizard, "account-creation-completed", G_CALLBACK(on_account_creation_completed), win);
Stepan Salenikovich419414c2016-06-07 10:35:07 -0400764
aviau6aeb4852016-08-18 16:01:09 -0400765 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400766 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400767 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
768 }
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400769
Stepan Salenikovich64505672015-09-24 10:46:07 -0400770 /* hide settings button until account creation is complete */
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400771 gtk_widget_hide(priv->ring_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -0400772 gtk_widget_hide(priv->combobox_account_selector);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400773
aviau69081842016-10-14 14:51:54 -0400774 gtk_widget_show(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400775
aviau69081842016-10-14 14:51:54 -0400776 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400777}
778
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400779static void
Stepan Salenikovicha06ce192016-11-01 14:26:43 -0400780search_entry_text_changed(GtkSearchEntry *search_entry, RingMainWindow *self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400781{
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400782 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
783
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400784 /* get the text from the entry */
785 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
786
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400787 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400788 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), text);
789 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), text);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400790}
791
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400792static gboolean
aviau4c87b832016-12-13 14:18:53 -0500793search_entry_key_released(G_GNUC_UNUSED GtkEntry *search_entry, GdkEventKey *key, RingMainWindow *self)
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400794{
795 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
796
797 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
798 if (key->keyval == GDK_KEY_Escape) {
799 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
800 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), "");
801 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
802 return GDK_EVENT_STOP;
803 }
804
805 return GDK_EVENT_PROPAGATE;
806}
807
808static gboolean
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400809dtmf_pressed(RingMainWindow *win,
810 GdkEventKey *event,
811 G_GNUC_UNUSED gpointer user_data)
812{
813 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
814
815 /* we want to react to digit key presses, as long as a GtkEntry is not the
816 * input focus
817 */
818 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(win));
819 if (GTK_IS_ENTRY(focus))
820 return GDK_EVENT_PROPAGATE;
821
822 /* make sure that a call is selected*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400823 QItemSelectionModel *selection = CallModel::instance().selectionModel();
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400824 QModelIndex idx = selection->currentIndex();
825 if (!idx.isValid())
826 return GDK_EVENT_PROPAGATE;
827
828 /* make sure that the selected call is in progress */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400829 Call *call = CallModel::instance().getCall(idx);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400830 Call::LifeCycleState state = call->lifeCycleState();
831 if (state != Call::LifeCycleState::PROGRESS)
832 return GDK_EVENT_PROPAGATE;
833
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400834 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
835 * does not result in a 'c' being played.
836 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
837 if ( event->state
838 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
839 return GDK_EVENT_PROPAGATE;
840
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400841 /* pass the character that was entered to be played by the daemon;
842 * the daemon will filter out invalid DTMF characters */
843 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
844 QString val = QString::fromUcs4(&unicode_val, 1);
845 call->playDTMF(val);
846 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400847
848 /* always propogate the key, so we don't steal accelerators/shortcuts */
849 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400850}
851
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400852/**
853 * This function determines if two different contact list views (Conversations, Contacts, History)
854 * have the same item selected. Note that the same item does not necessarily mean the same object.
855 * For example, if the history view has a Call selected and the Conversations view has a Person
856 * selected which is associated with the ContactMethod to which that Call was made, then this will
857 * evaluate to TRUE.
858 */
859static gboolean
860compare_treeview_selection(GtkTreeSelection *selection1, GtkTreeSelection *selection2)
861{
862 g_return_val_if_fail(selection1 && selection2, FALSE);
863
864 if (selection1 == selection2) return TRUE;
865
866 auto idx1 = get_index_from_selection(selection1);
867 auto type1 = idx1.data(static_cast<int>(Ring::Role::ObjectType));
868 auto object1 = idx1.data(static_cast<int>(Ring::Role::Object));
869
870 auto idx2 = get_index_from_selection(selection2);
871 auto type2 = idx2.data(static_cast<int>(Ring::Role::ObjectType));
872 auto object2 = idx2.data(static_cast<int>(Ring::Role::Object));
873
874 if (idx1.isValid() && type1.isValid() && object1.isValid() && idx2.isValid() && type2.isValid() && object2.isValid()) {
875 Call *call1 = nullptr;
876 ContactMethod *cm1 = nullptr;
877 Person *person1 = nullptr;
878
879 Call *call2 = nullptr;
880 ContactMethod *cm2 = nullptr;
881 Person *person2 = nullptr;
882
883 switch(type1.value<Ring::ObjectType>()) {
884 case Ring::ObjectType::Person:
885 person1 = object1.value<Person *>();
886 break;
887 case Ring::ObjectType::ContactMethod:
888 cm1 = object1.value<ContactMethod *>();
889 person1 = cm1->contact();
890 break;
891 case Ring::ObjectType::Call:
892 call1 = object1.value<Call *>();
893 cm1 = call1->peerContactMethod();
894 person1 = cm1->contact();
895 break;
896 case Ring::ObjectType::Media:
897 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400898 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400899 case Ring::ObjectType::COUNT__:
900 // nothing to do
901 break;
902 }
903
904 switch(type2.value<Ring::ObjectType>()) {
905 case Ring::ObjectType::Person:
906 person2 = object2.value<Person *>();
907 break;
908 case Ring::ObjectType::ContactMethod:
909 cm2 = object2.value<ContactMethod *>();
910 person2 = cm2->contact();
911 break;
912 case Ring::ObjectType::Call:
913 call2 = object2.value<Call *>();
914 cm2 = call2->peerContactMethod();
915 person2 = cm2->contact();
916 break;
917 case Ring::ObjectType::Media:
918 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400919 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400920 case Ring::ObjectType::COUNT__:
921 // nothing to do
922 break;
923 }
924
925 if (person1 != nullptr && person1 == person2)
926 return TRUE;
927 if (cm1 != nullptr && cm1 == cm2)
928 return TRUE;
929 if (call1 != nullptr && call1 == call2)
930 return TRUE;
931 }
932
933 return FALSE;
934}
935
936static void
937conversation_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
938{
939 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
940 GtkTreeIter iter;
941 GtkTreeModel *model = nullptr;
942 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
943 /* something is selected in the conversations view, clear the selection in the other views;
944 * unless the current selection is of the same item; we don't try to match the selection in
945 * all views since not all items exist in all 3 contact list views
946 */
947
948 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
949 if (!compare_treeview_selection(selection, selection_contacts))
950 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
951
952 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
953 if (!compare_treeview_selection(selection, selection_history))
954 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
955 }
956
957 selection_changed(self);
958}
959
960static void
961contact_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
962{
963 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
964 GtkTreeIter iter;
965 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
966 /* something is selected in the contacts view, clear the selection in the other views;
967 * unless the current selection is of the same item; we don't try to match the selection in
968 * all views since not all items exist in all 3 contact list views
969 */
970 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
971 if (!compare_treeview_selection(selection, selection_conversations))
972 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
973
974 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
975 if (!compare_treeview_selection(selection, selection_history))
976 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
977 }
978
979 selection_changed(self);
980}
981
982static void
983history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
984{
985 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
986 GtkTreeIter iter;
987 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
988 /* something is selected in the history view, clear the selection in the other views;
989 * unless the current selection is of the same item; we don't try to match the selection in
990 * all views since not all items exist in all 3 contact list views
991 */
992
993 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
994 if (!compare_treeview_selection(selection, selection_conversations))
995 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
996
997 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
998 if (!compare_treeview_selection(selection, selection_contacts))
999 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1000 }
1001
1002 selection_changed(self);
1003}
1004
Nicolas Jager0efc8432017-03-22 16:22:00 -04001005
1006/**
1007 * gtk callback function called when the selection in the contact request list changed
1008 */
1009static void
1010contact_request_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
1011{
1012 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1013 GtkTreeIter iter;
1014
1015 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
1016 /* unselect previous selection in conversations list */
1017 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1018 if (!compare_treeview_selection(selection, selection_conversations))
1019 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
1020
1021 /* unselect previous selection in contacts list */
1022 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1023 if (!compare_treeview_selection(selection, selection_contacts))
1024 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1025 }
1026
1027 selection_changed(self);
1028}
1029
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001030static gboolean
1031window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
1032{
1033 auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1034
1035 g_settings_set_int(priv->settings, "window-width", event->width);
1036 g_settings_set_int(priv->settings, "window-height", event->height);
1037
1038 return GDK_EVENT_PROPAGATE;
1039}
1040
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -04001041static void
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001042search_entry_places_call_changed(GSettings *settings, const gchar *key, RingMainWindow *self)
1043{
1044 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1045
1046 if (g_settings_get_boolean(settings, key)) {
1047 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will place a new call", "place call"));
1048 } else {
1049 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will open chat", "open chat"));
1050 }
1051}
1052
1053static void
aviau6aeb4852016-08-18 16:01:09 -04001054handle_account_migrations(RingMainWindow *win)
1055{
1056 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1057
1058 /* If there is an existing migration view, remove it */
aviau69081842016-10-14 14:51:54 -04001059 if (priv->account_migration_view)
aviau6aeb4852016-08-18 16:01:09 -04001060 {
Stepan Salenikovich56530b12016-10-20 17:51:34 -04001061 gtk_widget_destroy(priv->account_migration_view);
1062 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -04001063 }
1064
1065 QList<Account*> accounts = AccountModel::instance().accountsToMigrate();
1066 if (!accounts.isEmpty())
1067 {
1068 Account* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -04001069
aviau69081842016-10-14 14:51:54 -04001070 priv->account_migration_view = account_migration_view_new(account);
aviau69081842016-10-14 14:51:54 -04001071 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed", G_CALLBACK(handle_account_migrations), win);
1072 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
aviau6aeb4852016-08-18 16:01:09 -04001073
1074 gtk_widget_hide(priv->ring_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001075 gtk_widget_hide(priv->combobox_account_selector);
aviau69081842016-10-14 14:51:54 -04001076 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -04001077 gtk_stack_add_named(
1078 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -04001079 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -04001080 ACCOUNT_MIGRATION_VIEW_NAME
1081 );
1082 gtk_stack_set_visible_child_name(
1083 GTK_STACK(priv->stack_main_view),
1084 ACCOUNT_MIGRATION_VIEW_NAME
1085 );
1086 }
1087 else
1088 {
1089 gtk_widget_show(priv->ring_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001090 gtk_widget_show(priv->combobox_account_selector);
1091
1092 /* init the selection for the account selector */
1093 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
aviau6aeb4852016-08-18 16:01:09 -04001094 }
1095}
1096
1097static void
Nicolas Jager15a8b902017-03-21 07:53:06 -04001098selected_account_changed(GtkComboBox *gtk_combo_box, RingMainWindow *self)
1099{
1100 int nbr = gtk_combo_box_get_active(gtk_combo_box);
1101
1102 QModelIndex idx = AccountModel::instance().userSelectionModel()->model()->index(nbr, 0);
1103 auto account = AccountModel::instance().getAccountByModelIndex(idx);
1104
1105 AccountModel::instance().setUserChosenAccount(account);
1106
1107 // we closing any view opened to avoid confusion (especially between SIP and Ring protocols).
1108 hide_view_clicked(nullptr, self);
1109}
1110
Nicolas Jager5e2be732017-03-18 21:35:50 -04001111/**
1112 * set the column value by printing the alias and the state of an account in combobox_account_selector.
1113 */
1114static void
1115print_account_and_state(GtkCellLayout* cell_layout,
1116 GtkCellRenderer* cell_renderer,
1117 GtkTreeModel* tree_model,
1118 GtkTreeIter* iter,
1119 gpointer* data)
1120{
1121 (void) cell_layout; // UNUSED
1122 (void) data; // UNUSED
1123
1124 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001125 gchar* display_alias = nullptr;
1126
1127 if (idx.isValid()) {
Nicolas Jagera169c712017-04-14 12:02:50 -04001128 auto state = idx.data(static_cast<int>(Account::Role::RegistrationState));
1129 auto name = idx.data(static_cast<int>(Account::Role::Alias));
1130 auto account_alias = name.value<QString>().toStdString();
Nicolas Jager5e2be732017-03-18 21:35:50 -04001131
Nicolas Jagera169c712017-04-14 12:02:50 -04001132 switch (state.value<Account::RegistrationState>()) {
Nicolas Jager5e2be732017-03-18 21:35:50 -04001133 case Account::RegistrationState::READY:
1134 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001135 display_alias = g_strdup_printf("%s",account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001136 }
1137 break;
1138 case Account::RegistrationState::UNREGISTERED:
1139 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001140 display_alias = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001141 }
1142 break;
1143 case Account::RegistrationState::TRYING:
1144 case Account::RegistrationState::INITIALIZING:
1145 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001146 auto account_alias_i18n = g_markup_printf_escaped (C_("string format will be replaced by the account alias", "%s..."), account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001147 display_alias = g_strdup_printf("<span fgcolor=\"gray\" font_style=\"italic\">%s</span>", account_alias_i18n);
1148 }
1149 break;
1150 case Account::RegistrationState::ERROR:
1151 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001152 auto error_string = g_markup_printf_escaped(C_("string format will be replaced by the account alias", "%s (error)"), account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001153 display_alias = g_strdup_printf("<span fgcolor=\"red\">%s</span>", error_string);
1154 }
1155 case Account::RegistrationState::COUNT__:
1156 break;
1157 }
1158 }
1159
1160 g_object_set(G_OBJECT(cell_renderer), "markup", display_alias, nullptr);
1161 g_free(display_alias);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001162
1163}
1164
Nicolas Jager15a8b902017-03-21 07:53:06 -04001165static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001166ring_main_window_init(RingMainWindow *win)
1167{
1168 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1169 gtk_widget_init_template(GTK_WIDGET(win));
1170
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001171 priv->pending_username_lookup = new std::string; // see object finilize for delete
1172
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001173 /* bind to window size settings */
1174 priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
1175 auto width = g_settings_get_int(priv->settings, "window-width");
1176 auto height = g_settings_get_int(priv->settings, "window-height");
1177 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1178 g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
1179
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001180 /* search-entry-places-call setting */
1181 search_entry_places_call_changed(priv->settings, "search-entry-places-call", win);
1182 g_signal_connect(priv->settings, "changed::search-entry-places-call", G_CALLBACK(search_entry_places_call_changed), win);
1183
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001184 /* set window icon */
1185 GError *error = NULL;
1186 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
1187 if (icon == NULL) {
1188 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001189 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001190 } else
1191 gtk_window_set_icon(GTK_WINDOW(win), icon);
1192
1193 /* set menu icon */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001194 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001195 -1, 16, TRUE, &error);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001196 if (image_ring == NULL) {
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001197 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001198 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001199 } else
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001200 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_ring), image_ring);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001201
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001202 /* ring menu */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001203 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
1204 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001205 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->ring_menu), menu);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001206 g_object_unref(builder);
Stepan Salenikovicha3557452015-02-20 14:14:12 -05001207
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001208 /* settings icon */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001209 gtk_image_set_from_icon_name(GTK_IMAGE(priv->image_settings), "emblem-system-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001210
1211 /* connect settings button signal */
1212 g_signal_connect(priv->ring_settings, "clicked", G_CALLBACK(settings_clicked), win);
1213
1214 /* add the call view to the main stack */
1215 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
1216 priv->vbox_call_view,
1217 CALL_VIEW_NAME);
1218
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001219 /* init the settings views */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001220 priv->account_settings_view = account_view_new();
1221 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
1222
Stepan Salenikovich0bd53492015-05-11 14:28:52 -04001223 priv->media_settings_view = media_settings_view_new();
1224 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->media_settings_view, MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001225
Stepan Salenikovichde896112015-05-11 16:46:33 -04001226 priv->general_settings_view = general_settings_view_new();
1227 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
1228
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001229 /* make the setting we will show first the active one */
Stepan Salenikovichde896112015-05-11 16:46:33 -04001230 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->radiobutton_general_settings), TRUE);
1231 priv->last_settings_view = priv->general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001232
1233 /* connect the settings button signals to switch settings views */
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001234 g_signal_connect(priv->radiobutton_media_settings, "toggled", G_CALLBACK(show_media_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001235 g_signal_connect(priv->radiobutton_account_settings, "toggled", G_CALLBACK(show_account_settings), win);
Stepan Salenikovichde896112015-05-11 16:46:33 -04001236 g_signal_connect(priv->radiobutton_general_settings, "toggled", G_CALLBACK(show_general_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001237
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001238 /* populate the notebook */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001239 priv->treeview_conversations = recent_contacts_view_new();
1240 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_smartview), priv->treeview_conversations);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -04001241
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001242 priv->treeview_contacts = contacts_view_new();
1243 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contacts), priv->treeview_contacts);
Stepan Salenikovich15142182015-03-11 17:15:26 -04001244
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001245 priv->treeview_history = history_view_new();
1246 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001247
Nicolas Jager15a8b902017-03-21 07:53:06 -04001248 /* use this event to refresh the treeview_conversations when account selection changed */
1249 QObject::connect(AccountModel::instance().userSelectionModel(), &QItemSelectionModel::currentChanged, [priv](const QModelIndex& idx){
1250 // next line will refresh the recentmodel so the treeview will do
1251 RecentModel::instance().peopleProxy()->setFilterRegExp("");
1252
1253 // set the good index to the combox. Required when the selected account changed by different way than selecting
1254 // from the combo box.
1255 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), idx.row());
1256
1257 });
1258
Nicolas Jagera169c712017-04-14 12:02:50 -04001259 QObject::connect(&AccountModel::instance(), &AccountModel::accountEnabledChanged, [win, priv](Account* a){
1260 Q_UNUSED(a)
1261 gtk_widget_set_visible(priv->combobox_account_selector,
1262 (AvailableAccountModel::instance().rowCount() > 1)? TRUE : FALSE);
1263 });
1264
Nicolas Jager0efc8432017-03-22 16:22:00 -04001265 priv->treeview_contact_requests = pending_contact_requests_view_new();
1266 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contact_requests), priv->treeview_contact_requests);
1267
Stepan Salenikovich2cde7612015-09-25 10:44:01 -04001268 /* welcome/default view */
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001269 priv->welcome_view = ring_welcome_view_new();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001270 g_object_ref(priv->welcome_view); // increase ref because don't want it to be destroyed when not displayed
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001271 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
1272 gtk_widget_show(priv->welcome_view);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001273
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001274 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1275 g_signal_connect(selection_conversations, "changed", G_CALLBACK(conversation_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001276
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001277 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1278 g_signal_connect(selection_contacts, "changed", G_CALLBACK(contact_selection_changed), win);
1279
1280 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
1281 g_signal_connect(selection_history, "changed", G_CALLBACK(history_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001282
Nicolas Jager0efc8432017-03-22 16:22:00 -04001283 auto selection_contact_request = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contact_requests));
1284 g_signal_connect(selection_contact_request, "changed", G_CALLBACK(contact_request_selection_changed), win);
1285
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -04001286 g_signal_connect_swapped(priv->button_new_conversation, "clicked", G_CALLBACK(search_entry_activated), win);
1287 g_signal_connect_swapped(priv->search_entry, "activate", G_CALLBACK(search_entry_activated), win);
Stepan Salenikovicha06ce192016-11-01 14:26:43 -04001288 g_signal_connect(priv->search_entry, "search-changed", G_CALLBACK(search_entry_text_changed), win);
Stepan Salenikovichcac35042016-10-28 12:07:38 -04001289 g_signal_connect(priv->search_entry, "key-release-event", G_CALLBACK(search_entry_key_released), win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001290
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001291 /* make sure the incoming call is the selected call in the CallModel */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001292 QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -04001293 &CallModel::instance(),
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001294 &CallModel::incomingCall,
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001295 [priv](Call* call) {
Nicolas Jager85e992a2017-03-22 04:11:47 -04001296 // select the revelant account
1297 AccountModel::instance().setUserChosenAccount(call->account());
1298
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001299 // clear the regex to make sure the call is shown
1300 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001301 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts),"");
1302 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001303
1304 // now make sure its selected
1305 RecentModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001306 CallModel::instance().selectCall(call);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001307 }
1308 );
Stepan Salenikovich5eab3032015-03-28 10:50:51 -04001309
Stepan Salenikovichb01d7362015-04-27 23:02:00 -04001310 /* react to digit key press events */
1311 g_signal_connect(win, "key-press-event", G_CALLBACK(dtmf_pressed), NULL);
Stepan Salenikovich48b45c12015-09-25 15:49:51 -04001312
1313 /* set the search entry placeholder text */
1314 gtk_entry_set_placeholder_text(GTK_ENTRY(priv->search_entry),
1315 C_("Please try to make the translation 50 chars or less so that it fits into the layout", "Search contacts or enter number"));
aviau6aeb4852016-08-18 16:01:09 -04001316
aviau039001d2016-09-29 16:39:05 -04001317 /* init chat webkit container so that it starts loading before the first time we need it*/
1318 get_webkit_chat_container(win);
1319
Stepan Salenikovichcbc8dcc2017-04-05 11:27:10 -04001320 if (has_ring_account()) {
1321 /* user has ring account, so show the call view right away */
1322 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->vbox_call_view);
1323
1324 handle_account_migrations(win);
1325 } else {
1326 /* user has to create the ring account */
1327 show_account_creation_wizard(win);
1328 }
Nicolas Jager15a8b902017-03-21 07:53:06 -04001329
1330 /* model for the combobox for Account chooser */
Nicolas Jagera169c712017-04-14 12:02:50 -04001331 auto account_model = gtk_q_tree_model_new(&AvailableAccountModel::instance(), 1,
Nicolas Jager15a8b902017-03-21 07:53:06 -04001332 0, Account::Role::Alias, G_TYPE_STRING);
1333
1334 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_selector), GTK_TREE_MODEL(account_model));
1335
1336 auto *renderer = gtk_cell_renderer_text_new();
1337
Nicolas Jagera169c712017-04-14 12:02:50 -04001338 /* set visibility */
1339 gtk_widget_set_visible(priv->combobox_account_selector,
1340 (AvailableAccountModel::instance().rowCount() > 1)? TRUE : FALSE);
1341
Nicolas Jager15a8b902017-03-21 07:53:06 -04001342 /* layout */
1343 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, FALSE);
1344 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, "text", 0, NULL);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001345 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(priv->combobox_account_selector),
1346 renderer,
1347 (GtkCellLayoutDataFunc)print_account_and_state,
1348 nullptr, nullptr);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001349
1350 g_signal_connect(priv->combobox_account_selector, "changed", G_CALLBACK(selected_account_changed), win);
1351
1352 /* init the selection for the account selector */
1353 selected_account_changed(GTK_COMBO_BOX(priv->combobox_account_selector), win);
1354
1355 g_object_unref(account_model);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001356}
1357
1358static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001359ring_main_window_dispose(GObject *object)
1360{
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001361 RingMainWindow *self = RING_MAIN_WINDOW(object);
1362 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1363
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001364 QObject::disconnect(priv->selected_item_changed);
1365 QObject::disconnect(priv->selected_call_over);
aviau8c0b2422016-10-31 16:06:44 -04001366 QObject::disconnect(priv->username_lookup);
aviau039001d2016-09-29 16:39:05 -04001367
1368 g_clear_object(&priv->welcome_view);
1369 g_clear_object(&priv->webkit_chat_container);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001370
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001371 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1372}
1373
1374static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001375ring_main_window_finalize(GObject *object)
1376{
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001377 RingMainWindow *self = RING_MAIN_WINDOW(object);
1378 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1379
1380 g_clear_object(&priv->settings);
1381
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001382 delete priv->pending_username_lookup;
1383 priv->pending_username_lookup = nullptr;
1384
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001385 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001386}
1387
1388static void
1389ring_main_window_class_init(RingMainWindowClass *klass)
1390{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001391 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001392 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001393
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001394 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1395 "/cx/ring/RingGnome/ringmainwindow.ui");
1396
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001397 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001398 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001399 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contacts);
1400 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_history);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001401 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1402 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1403 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1404 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001405 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001406 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001407 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001408 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001409 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001410 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001411 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001412 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001413 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Nicolas Jager55903142017-02-01 14:43:45 -05001414 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, spinner_lookup);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001415 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, combobox_account_selector);
Nicolas Jager0efc8432017-03-22 16:22:00 -04001416 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contact_requests);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001417}
1418
1419GtkWidget *
1420ring_main_window_new (GtkApplication *app)
1421{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001422 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
1423
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001424 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001425}