blob: c529f989b531eaeb89b5f9a3f0699d2e199d6c8f [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
Stepan Salenikovich37194a62017-04-14 16:43:27 -0400730show_combobox_account_selector(RingMainWindow *self, gboolean show)
731{
732 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
733 /* we only want to show the account selector when there is more than 1 eneabled account; so
734 * every time we want to show it, we should preform this check */
735 gtk_widget_set_visible(priv->combobox_account_selector,
736 show && AvailableAccountModel::instance().rowCount() > 1);
737}
738
739static void
aviau6aeb4852016-08-18 16:01:09 -0400740on_account_creation_completed(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400741{
Stepan Salenikovichbabe26f2016-02-12 16:47:07 -0500742 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400743 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
744
aviau6aeb4852016-08-18 16:01:09 -0400745 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
746
747 /* destroy the wizard */
aviau69081842016-10-14 14:51:54 -0400748 if (priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400749 {
aviau69081842016-10-14 14:51:54 -0400750 gtk_container_remove(GTK_CONTAINER(priv->stack_main_view), priv->account_creation_wizard);
751 gtk_widget_destroy(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400752 }
aviau6aeb4852016-08-18 16:01:09 -0400753
754 /* show the settings button*/
755 gtk_widget_show(priv->ring_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -0400756
757 /* show the account selector */
Stepan Salenikovich37194a62017-04-14 16:43:27 -0400758 show_combobox_account_selector(win, TRUE);
Nicolas Jager15a8b902017-03-21 07:53:06 -0400759
760 /* init the selection for the account selector */
761 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400762}
763
764static void
aviau6aeb4852016-08-18 16:01:09 -0400765show_account_creation_wizard(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400766{
767 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
768
aviau61d05b32016-10-18 16:47:53 -0400769 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400770 {
aviau69081842016-10-14 14:51:54 -0400771 priv->account_creation_wizard = account_creation_wizard_new(false);
772 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
773 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 -0400774
aviau6aeb4852016-08-18 16:01:09 -0400775 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400776 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400777 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
778 }
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400779
Stepan Salenikovich64505672015-09-24 10:46:07 -0400780 /* hide settings button until account creation is complete */
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400781 gtk_widget_hide(priv->ring_settings);
Stepan Salenikovich37194a62017-04-14 16:43:27 -0400782 show_combobox_account_selector(win, FALSE);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400783
aviau69081842016-10-14 14:51:54 -0400784 gtk_widget_show(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400785
aviau69081842016-10-14 14:51:54 -0400786 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400787}
788
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400789static void
Stepan Salenikovicha06ce192016-11-01 14:26:43 -0400790search_entry_text_changed(GtkSearchEntry *search_entry, RingMainWindow *self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400791{
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400792 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
793
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400794 /* get the text from the entry */
795 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
796
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400797 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400798 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), text);
799 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), text);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400800}
801
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400802static gboolean
aviau4c87b832016-12-13 14:18:53 -0500803search_entry_key_released(G_GNUC_UNUSED GtkEntry *search_entry, GdkEventKey *key, RingMainWindow *self)
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400804{
805 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
806
807 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
808 if (key->keyval == GDK_KEY_Escape) {
809 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
810 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), "");
811 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
812 return GDK_EVENT_STOP;
813 }
814
815 return GDK_EVENT_PROPAGATE;
816}
817
818static gboolean
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400819dtmf_pressed(RingMainWindow *win,
820 GdkEventKey *event,
821 G_GNUC_UNUSED gpointer user_data)
822{
823 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
824
825 /* we want to react to digit key presses, as long as a GtkEntry is not the
826 * input focus
827 */
828 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(win));
829 if (GTK_IS_ENTRY(focus))
830 return GDK_EVENT_PROPAGATE;
831
832 /* make sure that a call is selected*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400833 QItemSelectionModel *selection = CallModel::instance().selectionModel();
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400834 QModelIndex idx = selection->currentIndex();
835 if (!idx.isValid())
836 return GDK_EVENT_PROPAGATE;
837
838 /* make sure that the selected call is in progress */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400839 Call *call = CallModel::instance().getCall(idx);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400840 Call::LifeCycleState state = call->lifeCycleState();
841 if (state != Call::LifeCycleState::PROGRESS)
842 return GDK_EVENT_PROPAGATE;
843
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400844 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
845 * does not result in a 'c' being played.
846 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
847 if ( event->state
848 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
849 return GDK_EVENT_PROPAGATE;
850
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400851 /* pass the character that was entered to be played by the daemon;
852 * the daemon will filter out invalid DTMF characters */
853 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
854 QString val = QString::fromUcs4(&unicode_val, 1);
855 call->playDTMF(val);
856 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400857
858 /* always propogate the key, so we don't steal accelerators/shortcuts */
859 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400860}
861
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400862/**
863 * This function determines if two different contact list views (Conversations, Contacts, History)
864 * have the same item selected. Note that the same item does not necessarily mean the same object.
865 * For example, if the history view has a Call selected and the Conversations view has a Person
866 * selected which is associated with the ContactMethod to which that Call was made, then this will
867 * evaluate to TRUE.
868 */
869static gboolean
870compare_treeview_selection(GtkTreeSelection *selection1, GtkTreeSelection *selection2)
871{
872 g_return_val_if_fail(selection1 && selection2, FALSE);
873
874 if (selection1 == selection2) return TRUE;
875
876 auto idx1 = get_index_from_selection(selection1);
877 auto type1 = idx1.data(static_cast<int>(Ring::Role::ObjectType));
878 auto object1 = idx1.data(static_cast<int>(Ring::Role::Object));
879
880 auto idx2 = get_index_from_selection(selection2);
881 auto type2 = idx2.data(static_cast<int>(Ring::Role::ObjectType));
882 auto object2 = idx2.data(static_cast<int>(Ring::Role::Object));
883
884 if (idx1.isValid() && type1.isValid() && object1.isValid() && idx2.isValid() && type2.isValid() && object2.isValid()) {
885 Call *call1 = nullptr;
886 ContactMethod *cm1 = nullptr;
887 Person *person1 = nullptr;
888
889 Call *call2 = nullptr;
890 ContactMethod *cm2 = nullptr;
891 Person *person2 = nullptr;
892
893 switch(type1.value<Ring::ObjectType>()) {
894 case Ring::ObjectType::Person:
895 person1 = object1.value<Person *>();
896 break;
897 case Ring::ObjectType::ContactMethod:
898 cm1 = object1.value<ContactMethod *>();
899 person1 = cm1->contact();
900 break;
901 case Ring::ObjectType::Call:
902 call1 = object1.value<Call *>();
903 cm1 = call1->peerContactMethod();
904 person1 = cm1->contact();
905 break;
906 case Ring::ObjectType::Media:
907 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400908 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400909 case Ring::ObjectType::COUNT__:
910 // nothing to do
911 break;
912 }
913
914 switch(type2.value<Ring::ObjectType>()) {
915 case Ring::ObjectType::Person:
916 person2 = object2.value<Person *>();
917 break;
918 case Ring::ObjectType::ContactMethod:
919 cm2 = object2.value<ContactMethod *>();
920 person2 = cm2->contact();
921 break;
922 case Ring::ObjectType::Call:
923 call2 = object2.value<Call *>();
924 cm2 = call2->peerContactMethod();
925 person2 = cm2->contact();
926 break;
927 case Ring::ObjectType::Media:
928 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400929 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400930 case Ring::ObjectType::COUNT__:
931 // nothing to do
932 break;
933 }
934
935 if (person1 != nullptr && person1 == person2)
936 return TRUE;
937 if (cm1 != nullptr && cm1 == cm2)
938 return TRUE;
939 if (call1 != nullptr && call1 == call2)
940 return TRUE;
941 }
942
943 return FALSE;
944}
945
946static void
947conversation_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
948{
949 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
950 GtkTreeIter iter;
951 GtkTreeModel *model = nullptr;
952 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
953 /* something is selected in the conversations view, clear the selection in the other views;
954 * unless the current selection is of the same item; we don't try to match the selection in
955 * all views since not all items exist in all 3 contact list views
956 */
957
958 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
959 if (!compare_treeview_selection(selection, selection_contacts))
960 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
961
962 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
963 if (!compare_treeview_selection(selection, selection_history))
964 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
965 }
966
967 selection_changed(self);
968}
969
970static void
971contact_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
972{
973 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
974 GtkTreeIter iter;
975 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
976 /* something is selected in the contacts view, clear the selection in the other views;
977 * unless the current selection is of the same item; we don't try to match the selection in
978 * all views since not all items exist in all 3 contact list views
979 */
980 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
981 if (!compare_treeview_selection(selection, selection_conversations))
982 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
983
984 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
985 if (!compare_treeview_selection(selection, selection_history))
986 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
987 }
988
989 selection_changed(self);
990}
991
992static void
993history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
994{
995 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
996 GtkTreeIter iter;
997 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
998 /* something is selected in the history view, clear the selection in the other views;
999 * unless the current selection is of the same item; we don't try to match the selection in
1000 * all views since not all items exist in all 3 contact list views
1001 */
1002
1003 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1004 if (!compare_treeview_selection(selection, selection_conversations))
1005 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
1006
1007 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1008 if (!compare_treeview_selection(selection, selection_contacts))
1009 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1010 }
1011
1012 selection_changed(self);
1013}
1014
Nicolas Jager0efc8432017-03-22 16:22:00 -04001015
1016/**
1017 * gtk callback function called when the selection in the contact request list changed
1018 */
1019static void
1020contact_request_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
1021{
1022 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1023 GtkTreeIter iter;
1024
1025 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
1026 /* unselect previous selection in conversations list */
1027 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1028 if (!compare_treeview_selection(selection, selection_conversations))
1029 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
1030
1031 /* unselect previous selection in contacts list */
1032 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1033 if (!compare_treeview_selection(selection, selection_contacts))
1034 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1035 }
1036
1037 selection_changed(self);
1038}
1039
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001040static gboolean
1041window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
1042{
1043 auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1044
1045 g_settings_set_int(priv->settings, "window-width", event->width);
1046 g_settings_set_int(priv->settings, "window-height", event->height);
1047
1048 return GDK_EVENT_PROPAGATE;
1049}
1050
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -04001051static void
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001052search_entry_places_call_changed(GSettings *settings, const gchar *key, RingMainWindow *self)
1053{
1054 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1055
1056 if (g_settings_get_boolean(settings, key)) {
1057 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will place a new call", "place call"));
1058 } else {
1059 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will open chat", "open chat"));
1060 }
1061}
1062
1063static void
aviau6aeb4852016-08-18 16:01:09 -04001064handle_account_migrations(RingMainWindow *win)
1065{
1066 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1067
1068 /* If there is an existing migration view, remove it */
aviau69081842016-10-14 14:51:54 -04001069 if (priv->account_migration_view)
aviau6aeb4852016-08-18 16:01:09 -04001070 {
Stepan Salenikovich56530b12016-10-20 17:51:34 -04001071 gtk_widget_destroy(priv->account_migration_view);
1072 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -04001073 }
1074
1075 QList<Account*> accounts = AccountModel::instance().accountsToMigrate();
1076 if (!accounts.isEmpty())
1077 {
1078 Account* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -04001079
aviau69081842016-10-14 14:51:54 -04001080 priv->account_migration_view = account_migration_view_new(account);
aviau69081842016-10-14 14:51:54 -04001081 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed", G_CALLBACK(handle_account_migrations), win);
1082 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
aviau6aeb4852016-08-18 16:01:09 -04001083
1084 gtk_widget_hide(priv->ring_settings);
Stepan Salenikovich37194a62017-04-14 16:43:27 -04001085 show_combobox_account_selector(win, FALSE);
aviau69081842016-10-14 14:51:54 -04001086 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -04001087 gtk_stack_add_named(
1088 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -04001089 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -04001090 ACCOUNT_MIGRATION_VIEW_NAME
1091 );
1092 gtk_stack_set_visible_child_name(
1093 GTK_STACK(priv->stack_main_view),
1094 ACCOUNT_MIGRATION_VIEW_NAME
1095 );
1096 }
1097 else
1098 {
1099 gtk_widget_show(priv->ring_settings);
Stepan Salenikovich37194a62017-04-14 16:43:27 -04001100 show_combobox_account_selector(win, TRUE);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001101
1102 /* init the selection for the account selector */
1103 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
aviau6aeb4852016-08-18 16:01:09 -04001104 }
1105}
1106
1107static void
Nicolas Jager15a8b902017-03-21 07:53:06 -04001108selected_account_changed(GtkComboBox *gtk_combo_box, RingMainWindow *self)
1109{
1110 int nbr = gtk_combo_box_get_active(gtk_combo_box);
1111
Nicolas Jager08389d22017-04-17 11:58:40 -04001112 QModelIndex idx = AccountModel::instance().userSelectionModel()->model()->index(nbr, 0);
1113 auto account = AccountModel::instance().getAccountByModelIndex(idx);
1114
1115 AccountModel::instance().setUserChosenAccount(account);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001116
1117 // we closing any view opened to avoid confusion (especially between SIP and Ring protocols).
1118 hide_view_clicked(nullptr, self);
1119}
1120
Nicolas Jager5e2be732017-03-18 21:35:50 -04001121/**
1122 * set the column value by printing the alias and the state of an account in combobox_account_selector.
1123 */
1124static void
1125print_account_and_state(GtkCellLayout* cell_layout,
1126 GtkCellRenderer* cell_renderer,
1127 GtkTreeModel* tree_model,
1128 GtkTreeIter* iter,
1129 gpointer* data)
1130{
1131 (void) cell_layout; // UNUSED
1132 (void) data; // UNUSED
1133
1134 QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001135 gchar* display_alias = nullptr;
1136
1137 if (idx.isValid()) {
Nicolas Jagera169c712017-04-14 12:02:50 -04001138 auto state = idx.data(static_cast<int>(Account::Role::RegistrationState));
1139 auto name = idx.data(static_cast<int>(Account::Role::Alias));
1140 auto account_alias = name.value<QString>().toStdString();
Nicolas Jager5e2be732017-03-18 21:35:50 -04001141
Nicolas Jagera169c712017-04-14 12:02:50 -04001142 switch (state.value<Account::RegistrationState>()) {
Nicolas Jager5e2be732017-03-18 21:35:50 -04001143 case Account::RegistrationState::READY:
1144 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001145 display_alias = g_strdup_printf("%s",account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001146 }
1147 break;
1148 case Account::RegistrationState::UNREGISTERED:
1149 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001150 display_alias = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", account_alias.c_str());
Nicolas Jager5e2be732017-03-18 21:35:50 -04001151 }
1152 break;
1153 case Account::RegistrationState::TRYING:
1154 case Account::RegistrationState::INITIALIZING:
1155 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001156 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 -04001157 display_alias = g_strdup_printf("<span fgcolor=\"gray\" font_style=\"italic\">%s</span>", account_alias_i18n);
1158 }
1159 break;
1160 case Account::RegistrationState::ERROR:
1161 {
Nicolas Jagera169c712017-04-14 12:02:50 -04001162 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 -04001163 display_alias = g_strdup_printf("<span fgcolor=\"red\">%s</span>", error_string);
1164 }
1165 case Account::RegistrationState::COUNT__:
1166 break;
1167 }
1168 }
1169
1170 g_object_set(G_OBJECT(cell_renderer), "markup", display_alias, nullptr);
1171 g_free(display_alias);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001172
1173}
1174
Nicolas Jager15a8b902017-03-21 07:53:06 -04001175static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001176ring_main_window_init(RingMainWindow *win)
1177{
1178 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1179 gtk_widget_init_template(GTK_WIDGET(win));
1180
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001181 priv->pending_username_lookup = new std::string; // see object finilize for delete
1182
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001183 /* bind to window size settings */
1184 priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
1185 auto width = g_settings_get_int(priv->settings, "window-width");
1186 auto height = g_settings_get_int(priv->settings, "window-height");
1187 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1188 g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
1189
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001190 /* search-entry-places-call setting */
1191 search_entry_places_call_changed(priv->settings, "search-entry-places-call", win);
1192 g_signal_connect(priv->settings, "changed::search-entry-places-call", G_CALLBACK(search_entry_places_call_changed), win);
1193
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001194 /* set window icon */
1195 GError *error = NULL;
1196 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
1197 if (icon == NULL) {
1198 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001199 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001200 } else
1201 gtk_window_set_icon(GTK_WINDOW(win), icon);
1202
1203 /* set menu icon */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001204 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001205 -1, 16, TRUE, &error);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001206 if (image_ring == NULL) {
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001207 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001208 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001209 } else
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001210 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_ring), image_ring);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001211
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001212 /* ring menu */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001213 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
1214 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001215 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->ring_menu), menu);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001216 g_object_unref(builder);
Stepan Salenikovicha3557452015-02-20 14:14:12 -05001217
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001218 /* settings icon */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001219 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 -04001220
1221 /* connect settings button signal */
1222 g_signal_connect(priv->ring_settings, "clicked", G_CALLBACK(settings_clicked), win);
1223
1224 /* add the call view to the main stack */
1225 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
1226 priv->vbox_call_view,
1227 CALL_VIEW_NAME);
1228
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001229 /* init the settings views */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001230 priv->account_settings_view = account_view_new();
1231 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
1232
Stepan Salenikovich0bd53492015-05-11 14:28:52 -04001233 priv->media_settings_view = media_settings_view_new();
1234 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 -04001235
Stepan Salenikovichde896112015-05-11 16:46:33 -04001236 priv->general_settings_view = general_settings_view_new();
1237 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
1238
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001239 /* make the setting we will show first the active one */
Stepan Salenikovichde896112015-05-11 16:46:33 -04001240 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->radiobutton_general_settings), TRUE);
1241 priv->last_settings_view = priv->general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001242
1243 /* connect the settings button signals to switch settings views */
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001244 g_signal_connect(priv->radiobutton_media_settings, "toggled", G_CALLBACK(show_media_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001245 g_signal_connect(priv->radiobutton_account_settings, "toggled", G_CALLBACK(show_account_settings), win);
Stepan Salenikovichde896112015-05-11 16:46:33 -04001246 g_signal_connect(priv->radiobutton_general_settings, "toggled", G_CALLBACK(show_general_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001247
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001248 /* populate the notebook */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001249 priv->treeview_conversations = recent_contacts_view_new();
1250 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_smartview), priv->treeview_conversations);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -04001251
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001252 priv->treeview_contacts = contacts_view_new();
1253 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contacts), priv->treeview_contacts);
Stepan Salenikovich15142182015-03-11 17:15:26 -04001254
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001255 priv->treeview_history = history_view_new();
1256 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001257
Nicolas Jager15a8b902017-03-21 07:53:06 -04001258 /* use this event to refresh the treeview_conversations when account selection changed */
Nicolas Jager08389d22017-04-17 11:58:40 -04001259 QObject::connect(AccountModel::instance().userSelectionModel(), &QItemSelectionModel::currentChanged, [priv](const QModelIndex& idx){
Nicolas Jager15a8b902017-03-21 07:53:06 -04001260 // next line will refresh the recentmodel so the treeview will do
1261 RecentModel::instance().peopleProxy()->setFilterRegExp("");
1262
1263 // set the good index to the combox. Required when the selected account changed by different way than selecting
1264 // from the combo box.
1265 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), idx.row());
1266
1267 });
1268
Stepan Salenikovich37194a62017-04-14 16:43:27 -04001269 auto available_accounts_changed = [win, priv] {
1270 /* if we're hiding the settings it means we're in the migration or wizard view and we don't
1271 * want to show the combo box */
1272 if (gtk_widget_get_visible(priv->ring_settings))
1273 show_combobox_account_selector(win, TRUE);
1274 };
1275 QObject::connect(&AvailableAccountModel::instance(), &QAbstractItemModel::rowsRemoved, available_accounts_changed);
1276 QObject::connect(&AvailableAccountModel::instance(), &QAbstractItemModel::rowsInserted, available_accounts_changed);
Nicolas Jagera169c712017-04-14 12:02:50 -04001277
Nicolas Jager0efc8432017-03-22 16:22:00 -04001278 priv->treeview_contact_requests = pending_contact_requests_view_new();
1279 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contact_requests), priv->treeview_contact_requests);
1280
Stepan Salenikovich2cde7612015-09-25 10:44:01 -04001281 /* welcome/default view */
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001282 priv->welcome_view = ring_welcome_view_new();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001283 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 -04001284 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
1285 gtk_widget_show(priv->welcome_view);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001286
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001287 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1288 g_signal_connect(selection_conversations, "changed", G_CALLBACK(conversation_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001289
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001290 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1291 g_signal_connect(selection_contacts, "changed", G_CALLBACK(contact_selection_changed), win);
1292
1293 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
1294 g_signal_connect(selection_history, "changed", G_CALLBACK(history_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001295
Nicolas Jager0efc8432017-03-22 16:22:00 -04001296 auto selection_contact_request = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contact_requests));
1297 g_signal_connect(selection_contact_request, "changed", G_CALLBACK(contact_request_selection_changed), win);
1298
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -04001299 g_signal_connect_swapped(priv->button_new_conversation, "clicked", G_CALLBACK(search_entry_activated), win);
1300 g_signal_connect_swapped(priv->search_entry, "activate", G_CALLBACK(search_entry_activated), win);
Stepan Salenikovicha06ce192016-11-01 14:26:43 -04001301 g_signal_connect(priv->search_entry, "search-changed", G_CALLBACK(search_entry_text_changed), win);
Stepan Salenikovichcac35042016-10-28 12:07:38 -04001302 g_signal_connect(priv->search_entry, "key-release-event", G_CALLBACK(search_entry_key_released), win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001303
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001304 /* make sure the incoming call is the selected call in the CallModel */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001305 QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -04001306 &CallModel::instance(),
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001307 &CallModel::incomingCall,
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001308 [priv](Call* call) {
Nicolas Jager85e992a2017-03-22 04:11:47 -04001309 // select the revelant account
Nicolas Jager08389d22017-04-17 11:58:40 -04001310 AccountModel::instance().setUserChosenAccount(call->account());
Nicolas Jager85e992a2017-03-22 04:11:47 -04001311
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001312 // clear the regex to make sure the call is shown
1313 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001314 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts),"");
1315 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001316
1317 // now make sure its selected
1318 RecentModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001319 CallModel::instance().selectCall(call);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001320 }
1321 );
Stepan Salenikovich5eab3032015-03-28 10:50:51 -04001322
Stepan Salenikovichb01d7362015-04-27 23:02:00 -04001323 /* react to digit key press events */
1324 g_signal_connect(win, "key-press-event", G_CALLBACK(dtmf_pressed), NULL);
Stepan Salenikovich48b45c12015-09-25 15:49:51 -04001325
1326 /* set the search entry placeholder text */
1327 gtk_entry_set_placeholder_text(GTK_ENTRY(priv->search_entry),
1328 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 -04001329
aviau039001d2016-09-29 16:39:05 -04001330 /* init chat webkit container so that it starts loading before the first time we need it*/
1331 get_webkit_chat_container(win);
1332
Stepan Salenikovichcbc8dcc2017-04-05 11:27:10 -04001333 if (has_ring_account()) {
1334 /* user has ring account, so show the call view right away */
1335 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->vbox_call_view);
1336
1337 handle_account_migrations(win);
1338 } else {
1339 /* user has to create the ring account */
1340 show_account_creation_wizard(win);
1341 }
Nicolas Jager15a8b902017-03-21 07:53:06 -04001342
1343 /* model for the combobox for Account chooser */
Nicolas Jagera169c712017-04-14 12:02:50 -04001344 auto account_model = gtk_q_tree_model_new(&AvailableAccountModel::instance(), 1,
Nicolas Jager15a8b902017-03-21 07:53:06 -04001345 0, Account::Role::Alias, G_TYPE_STRING);
1346
1347 gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_selector), GTK_TREE_MODEL(account_model));
1348
1349 auto *renderer = gtk_cell_renderer_text_new();
1350
Nicolas Jagera169c712017-04-14 12:02:50 -04001351 /* set visibility */
Stepan Salenikovich37194a62017-04-14 16:43:27 -04001352 show_combobox_account_selector(win, TRUE);
Nicolas Jagera169c712017-04-14 12:02:50 -04001353
Nicolas Jager15a8b902017-03-21 07:53:06 -04001354 /* layout */
1355 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, FALSE);
1356 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, "text", 0, NULL);
Nicolas Jager5e2be732017-03-18 21:35:50 -04001357 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(priv->combobox_account_selector),
1358 renderer,
1359 (GtkCellLayoutDataFunc)print_account_and_state,
1360 nullptr, nullptr);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001361
1362 g_signal_connect(priv->combobox_account_selector, "changed", G_CALLBACK(selected_account_changed), win);
1363
1364 /* init the selection for the account selector */
1365 selected_account_changed(GTK_COMBO_BOX(priv->combobox_account_selector), win);
1366
1367 g_object_unref(account_model);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001368}
1369
1370static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001371ring_main_window_dispose(GObject *object)
1372{
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001373 RingMainWindow *self = RING_MAIN_WINDOW(object);
1374 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1375
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001376 QObject::disconnect(priv->selected_item_changed);
1377 QObject::disconnect(priv->selected_call_over);
aviau8c0b2422016-10-31 16:06:44 -04001378 QObject::disconnect(priv->username_lookup);
aviau039001d2016-09-29 16:39:05 -04001379
1380 g_clear_object(&priv->welcome_view);
1381 g_clear_object(&priv->webkit_chat_container);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001382
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001383 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1384}
1385
1386static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001387ring_main_window_finalize(GObject *object)
1388{
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001389 RingMainWindow *self = RING_MAIN_WINDOW(object);
1390 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1391
1392 g_clear_object(&priv->settings);
1393
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001394 delete priv->pending_username_lookup;
1395 priv->pending_username_lookup = nullptr;
1396
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001397 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001398}
1399
1400static void
1401ring_main_window_class_init(RingMainWindowClass *klass)
1402{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001403 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001404 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001405
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001406 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1407 "/cx/ring/RingGnome/ringmainwindow.ui");
1408
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001409 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001410 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001411 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contacts);
1412 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_history);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001413 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1414 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1415 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1416 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001417 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001418 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001419 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001420 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001421 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001422 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001423 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001424 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001425 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Nicolas Jager55903142017-02-01 14:43:45 -05001426 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, spinner_lookup);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001427 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, combobox_account_selector);
Nicolas Jager0efc8432017-03-22 16:22:00 -04001428 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contact_requests);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001429}
1430
1431GtkWidget *
1432ring_main_window_new (GtkApplication *app)
1433{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001434 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
1435
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001436 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001437}