blob: 051e3974a01aec5559dd28938709a00476fa4ce6 [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>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040049
50// Ring client
51#include "models/gtkqtreemodel.h"
52#include "incomingcallview.h"
53#include "currentcallview.h"
Stepan Salenikovichf6078222016-10-03 17:31:16 -040054#include "models/gtkqtreemodel.h"
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040055#include "accountview.h"
56#include "dialogs.h"
57#include "mediasettingsview.h"
58#include "utils/drawing.h"
Stepan Salenikovichbbd6c132015-08-20 15:21:48 -040059#include "native/pixbufmanipulator.h"
Stepan Salenikovich6f687072015-03-26 10:43:37 -040060#include "models/activeitemproxymodel.h"
Stepan Salenikovich9816a942015-04-22 17:49:16 -040061#include "utils/calling.h"
62#include "contactsview.h"
63#include "historyview.h"
64#include "utils/models.h"
Stepan Salenikovichde896112015-05-11 16:46:33 -040065#include "generalsettingsview.h"
Stepan Salenikovich75a39172015-07-10 13:21:08 -040066#include "utils/accounts.h"
Stepan Salenikovich2cde7612015-09-25 10:44:01 -040067#include "ringwelcomeview.h"
aviau6aeb4852016-08-18 16:01:09 -040068#include "accountmigrationview.h"
69#include "accountcreationwizard.h"
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040070#include "recentcontactsview.h"
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050071#include "chatview.h"
Nicolas Jagerb413b302016-05-06 11:41:32 -040072#include "avatarmanipulation.h"
Stepan Salenikovichba87ae22016-09-13 14:27:20 -040073#include "utils/files.h"
Nicolas Jagerb413b302016-05-06 11:41:32 -040074
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040075static constexpr const char* CALL_VIEW_NAME = "calls";
aviau6aeb4852016-08-18 16:01:09 -040076static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard";
77static constexpr const char* ACCOUNT_MIGRATION_VIEW_NAME = "account-migration-view";
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040078static constexpr const char* GENERAL_SETTINGS_VIEW_NAME = "general";
79static constexpr const char* AUDIO_SETTINGS_VIEW_NAME = "audio";
80static constexpr const char* MEDIA_SETTINGS_VIEW_NAME = "media";
81static constexpr const char* ACCOUNT_SETTINGS_VIEW_NAME = "accounts";
Stepan Salenikovich2cde7612015-09-25 10:44:01 -040082static constexpr const char* DEFAULT_VIEW_NAME = "welcome";
Stepan Salenikovich9ffad5e2015-09-25 13:16:50 -040083static constexpr const char* VIEW_CONTACTS = "contacts";
84static constexpr const char* VIEW_HISTORY = "history";
85static constexpr const char* VIEW_PRESENCE = "presence";
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050086
87struct _RingMainWindow
88{
89 GtkApplicationWindow parent;
90};
91
92struct _RingMainWindowClass
93{
94 GtkApplicationWindowClass parent_class;
95};
96
97typedef struct _RingMainWindowPrivate RingMainWindowPrivate;
98
99struct _RingMainWindowPrivate
100{
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400101 GtkWidget *ring_menu;
102 GtkWidget *image_ring;
103 GtkWidget *ring_settings;
104 GtkWidget *image_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400105 GtkWidget *hbox_settings;
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -0400106 GtkWidget *scrolled_window_smartview;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400107 GtkWidget *treeview_conversations;
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -0400108 GtkWidget *scrolled_window_contacts;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400109 GtkWidget *treeview_contacts;
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -0400110 GtkWidget *scrolled_window_history;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400111 GtkWidget *treeview_history;
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -0400112 GtkWidget *vbox_left_pane;
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -0500113 GtkWidget *search_entry;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500114 GtkWidget *stack_main_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400115 GtkWidget *vbox_call_view;
Stepan Salenikovich3034d922015-10-09 10:11:42 -0400116 GtkWidget *frame_call;
117 GtkWidget *welcome_view;
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400118 GtkWidget *button_new_conversation ;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400119 GtkWidget *account_settings_view;
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400120 GtkWidget *media_settings_view;
Stepan Salenikovichde896112015-05-11 16:46:33 -0400121 GtkWidget *general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400122 GtkWidget *last_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400123 GtkWidget *radiobutton_general_settings;
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400124 GtkWidget *radiobutton_media_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400125 GtkWidget *radiobutton_account_settings;
aviau69081842016-10-14 14:51:54 -0400126 GtkWidget *account_creation_wizard;
127 GtkWidget *account_migration_view;
Nicolas Jager55903142017-02-01 14:43:45 -0500128 GtkWidget *spinner_lookup;
Stepan Salenikovich5eab3032015-03-28 10:50:51 -0400129
aviau8c0b2422016-10-31 16:06:44 -0400130 /* Pending ring usernames lookup for the search entry */
131 QMetaObject::Connection username_lookup;
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400132 std::string* pending_username_lookup;
aviau8c0b2422016-10-31 16:06:44 -0400133
aviau039001d2016-09-29 16:39:05 -0400134 /* The webkit_chat_container is created once, then reused for all chat views */
135 GtkWidget *webkit_chat_container;
136
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400137 QMetaObject::Connection selected_item_changed;
138 QMetaObject::Connection selected_call_over;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400139
140 gboolean show_settings;
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400141
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500142 /* fullscreen */
143 gboolean is_fullscreen;
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400144
145 GSettings *settings;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500146};
147
148G_DEFINE_TYPE_WITH_PRIVATE(RingMainWindow, ring_main_window, GTK_TYPE_APPLICATION_WINDOW);
149
150#define RING_MAIN_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_MAIN_WINDOW_TYPE, RingMainWindowPrivate))
151
aviau039001d2016-09-29 16:39:05 -0400152static gboolean selection_changed(RingMainWindow *win);
153
154static WebKitChatContainer*
155get_webkit_chat_container(RingMainWindow *win)
156{
157 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
158 if (!priv->webkit_chat_container)
159 {
160 priv->webkit_chat_container = webkit_chat_container_new();
161
162 //We don't want it to be deleted, ever.
163 g_object_ref(priv->webkit_chat_container);
164 }
165 return WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container);
166}
167
Stepan Salenikovich69771842015-02-24 18:11:45 -0500168static void
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500169enter_full_screen(RingMainWindow *self)
170{
171 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
172 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
173
174 if (!priv->is_fullscreen) {
175 gtk_widget_hide(priv->vbox_left_pane);
176 gtk_window_fullscreen(GTK_WINDOW(self));
177 priv->is_fullscreen = TRUE;
178 }
179}
180
181static void
182leave_full_screen(RingMainWindow *self)
183{
184 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
185 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
186
187 if (priv->is_fullscreen) {
188 gtk_widget_show(priv->vbox_left_pane);
189 gtk_window_unfullscreen(GTK_WINDOW(self));
190 priv->is_fullscreen = FALSE;
191 }
192}
193
194static void
Stepan Salenikovich5e431cf2015-12-24 14:09:01 -0500195video_double_clicked(G_GNUC_UNUSED CurrentCallView *view, RingMainWindow *self)
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500196{
197 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
198 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
199
200 if (priv->is_fullscreen) {
201 leave_full_screen(self);
202 } else {
203 enter_full_screen(self);
204 }
205}
206
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400207static void
208hide_view_clicked(G_GNUC_UNUSED GtkWidget *view, RingMainWindow *self)
209{
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400210 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400211
212 /* clear selection */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400213 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
214 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
215 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
216 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
217 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
218 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400219}
220
aviau039001d2016-09-29 16:39:05 -0400221static void
222change_view(RingMainWindow *self, GtkWidget* old, QObject *object, GType type)
223{
224 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
225 leave_full_screen(self);
226 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old);
227
228 GtkWidget *new_view = nullptr;
229
230 QObject::disconnect(priv->selected_item_changed);
231 QObject::disconnect(priv->selected_call_over);
232
233 if (g_type_is_a(INCOMING_CALL_VIEW_TYPE, type)) {
234 if (auto call = qobject_cast<Call *>(object)) {
235 new_view = incoming_call_view_new(call, get_webkit_chat_container(self));
236 priv->selected_item_changed = QObject::connect(
237 call,
238 &Call::lifeCycleStateChanged,
239 [self] (Call::LifeCycleState, Call::LifeCycleState)
240 {g_idle_add((GSourceFunc)selection_changed, self);}
241 );
242 priv->selected_call_over = QObject::connect(
243 call,
244 &Call::isOver,
245 [self] ()
246 {g_idle_add((GSourceFunc)selection_changed, self);}
247 );
248 } else
249 g_warning("Trying to display a view of type IncomingCallView, but the object is not of type Call");
250 } else if (g_type_is_a(CURRENT_CALL_VIEW_TYPE, type)) {
251 if (auto call = qobject_cast<Call *>(object)) {
252 new_view = current_call_view_new(call, get_webkit_chat_container(self));
253 g_signal_connect(new_view, "video-double-clicked", G_CALLBACK(video_double_clicked), self);
254 priv->selected_item_changed = QObject::connect(
255 call,
256 &Call::lifeCycleStateChanged,
257 [self] (Call::LifeCycleState, Call::LifeCycleState)
258 {g_idle_add((GSourceFunc)selection_changed, self);}
259 );
260 priv->selected_call_over = QObject::connect(
261 call,
262 &Call::isOver,
263 [self] ()
264 {g_idle_add((GSourceFunc)selection_changed, self);}
265 );
266 } else
267 g_warning("Trying to display a view of type CurrentCallView, but the object is not of type Call");
268 } else if (g_type_is_a(CHAT_VIEW_TYPE, type)) {
269 if (auto person = qobject_cast<Person *>(object)) {
270 new_view = chat_view_new_person(get_webkit_chat_container(self), person);
271 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
272
273 /* connect to the Person's callAdded signal, because we want to switch to the call view
274 * in this case */
275 priv->selected_item_changed = QObject::connect(
276 person,
277 &Person::callAdded,
278 [self] (Call*)
279 {g_idle_add((GSourceFunc)selection_changed, self);}
280 );
281 } else if (auto cm = qobject_cast<ContactMethod *>(object)) {
282 new_view = chat_view_new_cm(get_webkit_chat_container(self), cm);
283 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
284
285 /* connect to the ContactMethod's callAdded signal, because we want to switch to the
286 * call view in this case */
287 priv->selected_item_changed = QObject::connect(
288 cm,
289 &ContactMethod::callAdded,
290 [self] (Call*)
291 {g_idle_add((GSourceFunc)selection_changed, self);}
292 );
293 } else {
294 g_warning("Trying to display a veiw of type ChatView, but the object is neither a Person nor a ContactMethod");
295 }
296 } else {
297 // display the welcome view
298 new_view = priv->welcome_view;
299 }
300
301 gtk_container_add(GTK_CONTAINER(priv->frame_call), new_view);
302 gtk_widget_show(new_view);
303}
304
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500305/**
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400306 * This function determines which view to display in the right panel of the main window based on
307 * which item is selected in one of the 3 contact list views (Conversations, Contacts, History). The
308 * possible views ares:
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500309 * - incoming call view
310 * - current call view
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500311 * - chat view
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400312 * - welcome view (if no valid item is selected)
313 *
314 * There should never be a conflict of which item should be displayed (ie: which item is selected),
315 * as there should only ever be one selection at a time in the 3 views except in the case that the
316 * same item is selected in more than one view (see the compare_treeview_selection() function).
317 *
318 * This function could be called from a g_idle source, so it returns a boolean to remove itself from
319 * being called again. The boolean doesn't reflect success nor failure.
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500320 */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400321static gboolean
322selection_changed(RingMainWindow *win)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500323{
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500324 // g_debug("selection changed");
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400325
326 g_return_val_if_fail(IS_RING_MAIN_WINDOW(win), G_SOURCE_REMOVE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500327 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(win));
328
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400329 auto old_view = gtk_bin_get_child(GTK_BIN(priv->frame_call));
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500330
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400331 /* if we're showing the settings, then we need to make sure to remove any possible ongoing call
332 * view so that we don't have more than one VideoWidget at a time */
333 if (priv->show_settings) {
334 if (old_view != priv->welcome_view) {
335 leave_full_screen(win);
336 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old_view);
337 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
338 gtk_widget_show(priv->welcome_view);
339 }
340 return G_SOURCE_REMOVE;
341 }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500342
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400343 /* there should only be one item selected at a time, get which one is selected */
344 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
345 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
346 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
347
348 GtkTreeModel *model = nullptr;
349 GtkTreeIter iter;
350 QModelIndex idx;
351
352 if (gtk_tree_selection_get_selected(selection_conversations, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400353 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400354 } else if (gtk_tree_selection_get_selected(selection_contacts, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400355 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400356 } else if (gtk_tree_selection_get_selected(selection_history, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400357 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400358 }
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500359
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500360 /* check which object type is selected */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400361 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
362 auto object = idx.data(static_cast<int>(Ring::Role::Object));
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500363
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400364 if (idx.isValid() && type.isValid() && object.isValid()) {
Stepan Salenikovich69771842015-02-24 18:11:45 -0500365
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400366 /* we prioritize the views in the following order:
367 * - call view (if there is an ongoing call associated with the item)
368 * - chat view built from Person
369 * - chat view built from ContactMethod
370 */
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500371
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400372 Person *person = nullptr;
373 ContactMethod *cm = nullptr;
374 Call *call = nullptr;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500375
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400376 /* use the RecentModel to see if there are any ongoing calls associated with the selected item */
377 switch(type.value<Ring::ObjectType>()) {
378 case Ring::ObjectType::Person:
379 {
380 person = object.value<Person *>();
381 auto recent_idx = RecentModel::instance().getIndex(person);
382 if (recent_idx.isValid()) {
383 call = RecentModel::instance().getActiveCall(recent_idx);
384 }
385 }
386 break;
387 case Ring::ObjectType::ContactMethod:
388 {
389 cm = object.value<ContactMethod *>();
390 auto recent_idx = RecentModel::instance().getIndex(cm);
391 if (recent_idx.isValid()) {
392 call = RecentModel::instance().getActiveCall(recent_idx);
393 }
394 }
395 break;
396 case Ring::ObjectType::Call:
397 {
398 /* if the call is over (eg: if it comes from the history model) then we first check
399 * if there is an ongoing call with the same ContactMethod, otherwise we need to get
400 * the associated Person or ContactMethod */
401 call = object.value<Call *>();
402 if (call->isHistory()) {
403 cm = call->peerContactMethod();
404 call = nullptr;
405 if (cm) {
406 auto recent_idx = RecentModel::instance().getIndex(cm);
407 if (recent_idx.isValid()) {
408 call = RecentModel::instance().getActiveCall(recent_idx);
409 }
410 person = cm->contact();
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500411 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500412 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400413 }
414 break;
415 case Ring::ObjectType::Media:
416 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400417 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400418 case Ring::ObjectType::COUNT__:
419 // nothing to do
420 break;
421 }
422
423 /* we prioritize showing the call view */
424 if (call) {
425 auto state = call->lifeCycleState();
426
427 Call *current_call = nullptr;
428
429 switch(state) {
430 case Call::LifeCycleState::CREATION:
431 case Call::LifeCycleState::INITIALIZATION:
432 case Call::LifeCycleState::FINISHED:
433 // check if we're already displaying this call
434 if (IS_INCOMING_CALL_VIEW(old_view))
435 current_call = incoming_call_view_get_call(INCOMING_CALL_VIEW(old_view));
436 if (current_call != call)
aviau039001d2016-09-29 16:39:05 -0400437 change_view(win, old_view, call, INCOMING_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400438 break;
439 case Call::LifeCycleState::PROGRESS:
440 // check if we're already displaying this call
441 if (IS_CURRENT_CALL_VIEW(old_view))
442 current_call = current_call_view_get_call(CURRENT_CALL_VIEW(old_view));
aviau039001d2016-09-29 16:39:05 -0400443 if (current_call != call)
444 change_view(win, old_view, call, CURRENT_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400445 break;
446 case Call::LifeCycleState::COUNT__:
447 g_warning("LifeCycleState should never be COUNT");
448 break;
449 }
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500450
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400451 } else if (person) {
452 /* show chat view constructed from Person object */
453
454 // check if we're already displaying the chat view for this person
455 Person *current_person = nullptr;
456 if (IS_CHAT_VIEW(old_view))
457 current_person = chat_view_get_person(CHAT_VIEW(old_view));
458
aviau039001d2016-09-29 16:39:05 -0400459 if (current_person != person)
460 change_view(win, old_view, person, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400461 } else if (cm) {
462 /* show chat view constructed from CM */
463
464 // check if we're already displaying the chat view for this cm
465 ContactMethod *current_cm = nullptr;
466 if (IS_CHAT_VIEW(old_view))
467 current_cm = chat_view_get_cm(CHAT_VIEW(old_view));
468
aviau039001d2016-09-29 16:39:05 -0400469 if (current_cm != cm)
470 change_view(win, old_view, cm, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400471 } else {
472 /* not a supported object type, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400473 if (old_view != priv->welcome_view)
474 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500475 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400476 } else {
477 /* selection isn't valid, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400478 if (old_view != priv->welcome_view)
479 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500480 }
Stepan Salenikovich504f7722016-01-20 17:10:45 -0500481
482 return G_SOURCE_REMOVE;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500483}
484
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500485static void
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400486process_search_entry_contact_method(RingMainWindow *self, const URI& uri, Account* accountOrNullptr)
aviau8c0b2422016-10-31 16:06:44 -0400487{
488 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
489
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400490 auto cm = PhoneDirectoryModel::instance().getNumber(uri, accountOrNullptr);
aviau8c0b2422016-10-31 16:06:44 -0400491
492 if (g_settings_get_boolean(priv->settings, "search-entry-places-call")) {
493 place_new_call(cm);
494
495 /* move focus away from entry so that DTMF tones can be entered via the keyboard */
496 gtk_widget_child_focus(GTK_WIDGET(self), GTK_DIR_TAB_FORWARD);
497 } else {
498 // if its a new CM, bring it to the top
499 if (cm->lastUsed() == 0)
500 cm->setLastUsed(QDateTime::currentDateTime().toTime_t());
501
502 // select cm
503 RecentModel::instance().selectionModel()->setCurrentIndex(RecentModel::instance().getIndex(cm), QItemSelectionModel::ClearAndSelect);
504 }
505 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
506}
507
508static void
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400509search_entry_activated(RingMainWindow *self)
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500510{
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400511 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500512
Nicolas Jager55903142017-02-01 14:43:45 -0500513 const auto *querry = gtk_entry_get_text(GTK_ENTRY(priv->search_entry));
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500514
Nicolas Jager55903142017-02-01 14:43:45 -0500515 URI uri = URI(querry);
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400516
517
518 // make sure the userinfo part isn't empty, only specifying a protocol isn't enough
519 if (!uri.userinfo().isEmpty()) {
520
521 gboolean lookup_username = FALSE;
522
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500523 /* get the account with which we will do the lookup */
524 auto ring_account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::RING);
525
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400526 if (uri.protocolHint() == URI::ProtocolHint::RING_USERNAME ) {
527 lookup_username = TRUE;
528 } else if (
529 uri.protocolHint() != URI::ProtocolHint::RING && // not a RingID
530 uri.schemeType() == URI::SchemeType::NONE // scheme type not specified
531 ) {
532 // if no scheme type has been specified, determine ring vs sip by the first available account
533 auto idx = AvailableAccountModel::instance().index(0, 0);
534 if (idx.isValid()) {
535 auto account = idx.data((int)Ring::Role::Object).value<Account *>();
536 if (account && account->protocol() == Account::Protocol::RING)
537 lookup_username = TRUE;
538 }
539 }
540
541 if (lookup_username)
aviau8c0b2422016-10-31 16:06:44 -0400542 {
Nicolas Jager55903142017-02-01 14:43:45 -0500543 gtk_spinner_start(GTK_SPINNER(priv->spinner_lookup));
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400544 *priv->pending_username_lookup = querry;
Nicolas Jager55903142017-02-01 14:43:45 -0500545 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
Stepan Salenikovich895a70b2015-03-27 16:02:27 -0400546
aviau8f436a32016-11-05 01:49:55 -0400547 QString username_to_lookup = uri.format(
548 URI::Section::USER_INFO |
549 URI::Section::HOSTNAME |
550 URI::Section::PORT
551 );
Stepan Salenikovich895a70b2015-03-27 16:02:27 -0400552
aviau8c0b2422016-10-31 16:06:44 -0400553 QObject::disconnect(priv->username_lookup);
554 priv->username_lookup = QObject::connect(
555 &NameDirectory::instance(),
556 &NameDirectory::registeredNameFound,
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400557 [self, priv, username_to_lookup] (Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) {
Stepan Salenikovich1564bba2015-04-29 11:28:43 -0400558
Nicolas Jager55903142017-02-01 14:43:45 -0500559 auto name_qbarray = name.toLatin1();
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400560 if ( strcmp(priv->pending_username_lookup->data(), name_qbarray.data()) != 0 )
Nicolas Jager55903142017-02-01 14:43:45 -0500561 return;
562
aviau8c0b2422016-10-31 16:06:44 -0400563 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
564
565 switch(status)
566 {
567 case NameDirectory::LookupStatus::SUCCESS:
568 {
569 URI uri = URI("ring:" + address);
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400570 process_search_entry_contact_method(self, uri, account);
aviau8c0b2422016-10-31 16:06:44 -0400571 break;
572 }
573 case NameDirectory::LookupStatus::INVALID_NAME:
574 {
575 //Can't be a ring username, could be something else.
576 auto dialog = gtk_message_dialog_new(
577 GTK_WINDOW(self),
578 GTK_DIALOG_DESTROY_WITH_PARENT,
579 GTK_MESSAGE_ERROR,
580 GTK_BUTTONS_CLOSE,
581 _("Invalid Ring username: %s"),
582 name.toUtf8().constData()
583 );
584
585 gtk_dialog_run (GTK_DIALOG (dialog));
586 gtk_widget_destroy (dialog);
587 break;
588 }
589 case NameDirectory::LookupStatus::ERROR:
Stepan Salenikovichfa24b932016-11-21 15:19:57 -0500590 {
591 auto dialog = gtk_message_dialog_new(
592 GTK_WINDOW(self),
593 GTK_DIALOG_DESTROY_WITH_PARENT,
594 GTK_MESSAGE_ERROR,
595 GTK_BUTTONS_CLOSE,
596 _("Error resolving username, nameserver is possibly unreachable")
597 );
598
599 gtk_dialog_run(GTK_DIALOG (dialog));
600 gtk_widget_destroy(dialog);
601 break;
602 }
aviau8c0b2422016-10-31 16:06:44 -0400603 case NameDirectory::LookupStatus::NOT_FOUND:
604 {
605 auto dialog = gtk_message_dialog_new(
606 GTK_WINDOW(self),
607 GTK_DIALOG_DESTROY_WITH_PARENT,
608 GTK_MESSAGE_ERROR,
609 GTK_BUTTONS_CLOSE,
610 _("Could not resolve Ring username: %s"),
611 name.toUtf8().constData()
612 );
613
614 gtk_dialog_run(GTK_DIALOG (dialog));
615 gtk_widget_destroy(dialog);
616 break;
617 }
618 }
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400619 *priv->pending_username_lookup = "";
Nicolas Jager55903142017-02-01 14:43:45 -0500620 gtk_spinner_stop(GTK_SPINNER(priv->spinner_lookup));
aviau8c0b2422016-10-31 16:06:44 -0400621 }
622 );
623
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500624 NameDirectory::instance().lookupName(ring_account, QString(), username_to_lookup);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400625 }
aviau8c0b2422016-10-31 16:06:44 -0400626 else
627 {
Guillaume Rogueze7221ed2017-03-17 20:09:56 -0400628 *priv->pending_username_lookup = "";
Nicolas Jager55903142017-02-01 14:43:45 -0500629 gtk_spinner_stop(GTK_SPINNER(priv->spinner_lookup));
Nicolas Jager65d6e1f2017-03-27 16:57:55 -0400630 process_search_entry_contact_method(self, uri, nullptr);
aviau8c0b2422016-10-31 16:06:44 -0400631 }
Stepan Salenikovich2e6ab562015-05-07 12:33:07 -0400632 }
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500633}
634
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400635static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400636save_accounts(GtkWidget *working_dialog)
637{
638 /* save changes to accounts */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400639 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400640
641 if (working_dialog)
642 gtk_widget_destroy(working_dialog);
643
644 return G_SOURCE_REMOVE;
645}
646
Stepan Salenikovich15142182015-03-11 17:15:26 -0400647static void
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400648settings_clicked(G_GNUC_UNUSED GtkButton *button, RingMainWindow *win)
649{
650 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
651 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
652
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400653 /* check which view to show */
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500654 if (!priv->show_settings) {
655 /* show the settings */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400656 priv->show_settings = TRUE;
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500657
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500658 /* 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 -0400659 selection_changed(win);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500660
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400661 /* show settings */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400662 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 -0400663
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400664 gtk_widget_show(priv->hbox_settings);
665
Stepan Salenikovich41118912015-05-01 11:25:46 -0400666 /* make sure to start preview if we're showing the video settings */
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400667 if (priv->last_settings_view == priv->media_settings_view)
668 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400669
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400670 /* make sure to show the profile if we're showing the general settings */
671 if (priv->last_settings_view == priv->general_settings_view)
672 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
673
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400674 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->last_settings_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400675 } else {
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500676 /* hide the settings */
677 priv->show_settings = FALSE;
678
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400679 /* show working dialog in case save operation takes time */
680 GtkWidget *working = ring_dialog_working(GTK_WIDGET(win), NULL);
681 gtk_window_present(GTK_WINDOW(working));
682
683 /* now save after the time it takes to transition back to the call view (400ms)
684 * the save doesn't happen before the "working" dialog is presented
685 * the timeout function should destroy the "working" dialog when done saving
686 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400687 g_timeout_add_full(G_PRIORITY_DEFAULT, 400, (GSourceFunc)save_accounts, working, NULL);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400688
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400689 /* show calls */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400690 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 -0400691
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400692 gtk_widget_hide(priv->hbox_settings);
693
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500694 /* make sure video preview is stopped, in case it was started */
695 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400696 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500697
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400698 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400699
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500700 /* show the view which was selected previously */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400701 selection_changed(win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400702 }
703}
704
705static void
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400706show_media_settings(GtkToggleButton *navbutton, RingMainWindow *win)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400707{
708 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
709 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
710
711 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400712 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400713 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400714 priv->last_settings_view = priv->media_settings_view;
Stepan Salenikovich41118912015-05-01 11:25:46 -0400715 } else {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400716 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400717 }
718}
719
720static void
721show_account_settings(GtkToggleButton *navbutton, RingMainWindow *win)
722{
723 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
724 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
725
726 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400727 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), ACCOUNT_SETTINGS_VIEW_NAME);
728 priv->last_settings_view = priv->account_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400729 }
730}
731
Stepan Salenikovichde896112015-05-11 16:46:33 -0400732static void
733show_general_settings(GtkToggleButton *navbutton, RingMainWindow *win)
734{
735 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
736 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
737
738 if (gtk_toggle_button_get_active(navbutton)) {
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400739 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400740 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
741 priv->last_settings_view = priv->general_settings_view;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400742 } else {
743 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400744 }
745}
746
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400747static void
aviau6aeb4852016-08-18 16:01:09 -0400748on_account_creation_completed(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400749{
Stepan Salenikovichbabe26f2016-02-12 16:47:07 -0500750 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400751 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
752
aviau6aeb4852016-08-18 16:01:09 -0400753 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
754
755 /* destroy the wizard */
aviau69081842016-10-14 14:51:54 -0400756 if (priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400757 {
aviau69081842016-10-14 14:51:54 -0400758 gtk_container_remove(GTK_CONTAINER(priv->stack_main_view), priv->account_creation_wizard);
759 gtk_widget_destroy(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400760 }
aviau6aeb4852016-08-18 16:01:09 -0400761
762 /* show the settings button*/
763 gtk_widget_show(priv->ring_settings);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400764}
765
766static void
aviau6aeb4852016-08-18 16:01:09 -0400767show_account_creation_wizard(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400768{
769 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
770
aviau61d05b32016-10-18 16:47:53 -0400771 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400772 {
aviau69081842016-10-14 14:51:54 -0400773 priv->account_creation_wizard = account_creation_wizard_new(false);
774 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
775 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 -0400776
aviau6aeb4852016-08-18 16:01:09 -0400777 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400778 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400779 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
780 }
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400781
Stepan Salenikovich64505672015-09-24 10:46:07 -0400782 /* hide settings button until account creation is complete */
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400783 gtk_widget_hide(priv->ring_settings);
784
aviau69081842016-10-14 14:51:54 -0400785 gtk_widget_show(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400786
aviau69081842016-10-14 14:51:54 -0400787 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400788}
789
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400790static void
Stepan Salenikovicha06ce192016-11-01 14:26:43 -0400791search_entry_text_changed(GtkSearchEntry *search_entry, RingMainWindow *self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400792{
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400793 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
794
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400795 /* get the text from the entry */
796 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
797
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400798 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400799 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), text);
800 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), text);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400801}
802
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400803static gboolean
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400804search_entry_key_released(GtkEntry *search_entry, GdkEventKey *key, RingMainWindow *self)
805{
806 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
807
808 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
809 if (key->keyval == GDK_KEY_Escape) {
810 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
811 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), "");
812 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
813 return GDK_EVENT_STOP;
814 }
815
816 return GDK_EVENT_PROPAGATE;
817}
818
819static gboolean
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400820dtmf_pressed(RingMainWindow *win,
821 GdkEventKey *event,
822 G_GNUC_UNUSED gpointer user_data)
823{
824 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
825
826 /* we want to react to digit key presses, as long as a GtkEntry is not the
827 * input focus
828 */
829 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(win));
830 if (GTK_IS_ENTRY(focus))
831 return GDK_EVENT_PROPAGATE;
832
833 /* make sure that a call is selected*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400834 QItemSelectionModel *selection = CallModel::instance().selectionModel();
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400835 QModelIndex idx = selection->currentIndex();
836 if (!idx.isValid())
837 return GDK_EVENT_PROPAGATE;
838
839 /* make sure that the selected call is in progress */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400840 Call *call = CallModel::instance().getCall(idx);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400841 Call::LifeCycleState state = call->lifeCycleState();
842 if (state != Call::LifeCycleState::PROGRESS)
843 return GDK_EVENT_PROPAGATE;
844
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400845 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
846 * does not result in a 'c' being played.
847 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
848 if ( event->state
849 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
850 return GDK_EVENT_PROPAGATE;
851
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400852 /* pass the character that was entered to be played by the daemon;
853 * the daemon will filter out invalid DTMF characters */
854 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
855 QString val = QString::fromUcs4(&unicode_val, 1);
856 call->playDTMF(val);
857 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400858
859 /* always propogate the key, so we don't steal accelerators/shortcuts */
860 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400861}
862
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400863/**
864 * This function determines if two different contact list views (Conversations, Contacts, History)
865 * have the same item selected. Note that the same item does not necessarily mean the same object.
866 * For example, if the history view has a Call selected and the Conversations view has a Person
867 * selected which is associated with the ContactMethod to which that Call was made, then this will
868 * evaluate to TRUE.
869 */
870static gboolean
871compare_treeview_selection(GtkTreeSelection *selection1, GtkTreeSelection *selection2)
872{
873 g_return_val_if_fail(selection1 && selection2, FALSE);
874
875 if (selection1 == selection2) return TRUE;
876
877 auto idx1 = get_index_from_selection(selection1);
878 auto type1 = idx1.data(static_cast<int>(Ring::Role::ObjectType));
879 auto object1 = idx1.data(static_cast<int>(Ring::Role::Object));
880
881 auto idx2 = get_index_from_selection(selection2);
882 auto type2 = idx2.data(static_cast<int>(Ring::Role::ObjectType));
883 auto object2 = idx2.data(static_cast<int>(Ring::Role::Object));
884
885 if (idx1.isValid() && type1.isValid() && object1.isValid() && idx2.isValid() && type2.isValid() && object2.isValid()) {
886 Call *call1 = nullptr;
887 ContactMethod *cm1 = nullptr;
888 Person *person1 = nullptr;
889
890 Call *call2 = nullptr;
891 ContactMethod *cm2 = nullptr;
892 Person *person2 = nullptr;
893
894 switch(type1.value<Ring::ObjectType>()) {
895 case Ring::ObjectType::Person:
896 person1 = object1.value<Person *>();
897 break;
898 case Ring::ObjectType::ContactMethod:
899 cm1 = object1.value<ContactMethod *>();
900 person1 = cm1->contact();
901 break;
902 case Ring::ObjectType::Call:
903 call1 = object1.value<Call *>();
904 cm1 = call1->peerContactMethod();
905 person1 = cm1->contact();
906 break;
907 case Ring::ObjectType::Media:
908 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400909 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400910 case Ring::ObjectType::COUNT__:
911 // nothing to do
912 break;
913 }
914
915 switch(type2.value<Ring::ObjectType>()) {
916 case Ring::ObjectType::Person:
917 person2 = object2.value<Person *>();
918 break;
919 case Ring::ObjectType::ContactMethod:
920 cm2 = object2.value<ContactMethod *>();
921 person2 = cm2->contact();
922 break;
923 case Ring::ObjectType::Call:
924 call2 = object2.value<Call *>();
925 cm2 = call2->peerContactMethod();
926 person2 = cm2->contact();
927 break;
928 case Ring::ObjectType::Media:
929 case Ring::ObjectType::Certificate:
Nicolas Jagerc74f7612017-03-28 09:48:18 -0400930 case Ring::ObjectType::ContactRequest:
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400931 case Ring::ObjectType::COUNT__:
932 // nothing to do
933 break;
934 }
935
936 if (person1 != nullptr && person1 == person2)
937 return TRUE;
938 if (cm1 != nullptr && cm1 == cm2)
939 return TRUE;
940 if (call1 != nullptr && call1 == call2)
941 return TRUE;
942 }
943
944 return FALSE;
945}
946
947static void
948conversation_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
949{
950 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
951 GtkTreeIter iter;
952 GtkTreeModel *model = nullptr;
953 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
954 /* something is selected in the conversations view, clear the selection in the other views;
955 * unless the current selection is of the same item; we don't try to match the selection in
956 * all views since not all items exist in all 3 contact list views
957 */
958
959 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
960 if (!compare_treeview_selection(selection, selection_contacts))
961 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
962
963 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
964 if (!compare_treeview_selection(selection, selection_history))
965 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
966 }
967
968 selection_changed(self);
969}
970
971static void
972contact_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
973{
974 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
975 GtkTreeIter iter;
976 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
977 /* something is selected in the contacts view, clear the selection in the other views;
978 * unless the current selection is of the same item; we don't try to match the selection in
979 * all views since not all items exist in all 3 contact list views
980 */
981 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
982 if (!compare_treeview_selection(selection, selection_conversations))
983 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
984
985 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
986 if (!compare_treeview_selection(selection, selection_history))
987 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
988 }
989
990 selection_changed(self);
991}
992
993static void
994history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
995{
996 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
997 GtkTreeIter iter;
998 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
999 /* something is selected in the history view, clear the selection in the other views;
1000 * unless the current selection is of the same item; we don't try to match the selection in
1001 * all views since not all items exist in all 3 contact list views
1002 */
1003
1004 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1005 if (!compare_treeview_selection(selection, selection_conversations))
1006 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
1007
1008 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1009 if (!compare_treeview_selection(selection, selection_contacts))
1010 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1011 }
1012
1013 selection_changed(self);
1014}
1015
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001016static gboolean
1017window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
1018{
1019 auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1020
1021 g_settings_set_int(priv->settings, "window-width", event->width);
1022 g_settings_set_int(priv->settings, "window-height", event->height);
1023
1024 return GDK_EVENT_PROPAGATE;
1025}
1026
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -04001027static void
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001028search_entry_places_call_changed(GSettings *settings, const gchar *key, RingMainWindow *self)
1029{
1030 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1031
1032 if (g_settings_get_boolean(settings, key)) {
1033 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will place a new call", "place call"));
1034 } else {
1035 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will open chat", "open chat"));
1036 }
1037}
1038
1039static void
aviau6aeb4852016-08-18 16:01:09 -04001040handle_account_migrations(RingMainWindow *win)
1041{
1042 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1043
1044 /* If there is an existing migration view, remove it */
aviau69081842016-10-14 14:51:54 -04001045 if (priv->account_migration_view)
aviau6aeb4852016-08-18 16:01:09 -04001046 {
Stepan Salenikovich56530b12016-10-20 17:51:34 -04001047 gtk_widget_destroy(priv->account_migration_view);
1048 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -04001049 }
1050
1051 QList<Account*> accounts = AccountModel::instance().accountsToMigrate();
1052 if (!accounts.isEmpty())
1053 {
1054 Account* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -04001055
aviau69081842016-10-14 14:51:54 -04001056 priv->account_migration_view = account_migration_view_new(account);
aviau69081842016-10-14 14:51:54 -04001057 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed", G_CALLBACK(handle_account_migrations), win);
1058 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
aviau6aeb4852016-08-18 16:01:09 -04001059
1060 gtk_widget_hide(priv->ring_settings);
aviau69081842016-10-14 14:51:54 -04001061 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -04001062 gtk_stack_add_named(
1063 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -04001064 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -04001065 ACCOUNT_MIGRATION_VIEW_NAME
1066 );
1067 gtk_stack_set_visible_child_name(
1068 GTK_STACK(priv->stack_main_view),
1069 ACCOUNT_MIGRATION_VIEW_NAME
1070 );
1071 }
1072 else
1073 {
1074 gtk_widget_show(priv->ring_settings);
1075 }
1076}
1077
1078static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001079ring_main_window_init(RingMainWindow *win)
1080{
1081 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1082 gtk_widget_init_template(GTK_WIDGET(win));
1083
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001084 priv->pending_username_lookup = new std::string; // see object finilize for delete
1085
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001086 /* bind to window size settings */
1087 priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
1088 auto width = g_settings_get_int(priv->settings, "window-width");
1089 auto height = g_settings_get_int(priv->settings, "window-height");
1090 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1091 g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
1092
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001093 /* search-entry-places-call setting */
1094 search_entry_places_call_changed(priv->settings, "search-entry-places-call", win);
1095 g_signal_connect(priv->settings, "changed::search-entry-places-call", G_CALLBACK(search_entry_places_call_changed), win);
1096
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001097 /* set window icon */
1098 GError *error = NULL;
1099 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
1100 if (icon == NULL) {
1101 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001102 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001103 } else
1104 gtk_window_set_icon(GTK_WINDOW(win), icon);
1105
1106 /* set menu icon */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001107 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001108 -1, 16, TRUE, &error);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001109 if (image_ring == NULL) {
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001110 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001111 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001112 } else
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001113 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_ring), image_ring);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001114
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001115 /* ring menu */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001116 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
1117 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001118 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->ring_menu), menu);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001119 g_object_unref(builder);
Stepan Salenikovicha3557452015-02-20 14:14:12 -05001120
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001121 /* settings icon */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001122 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 -04001123
1124 /* connect settings button signal */
1125 g_signal_connect(priv->ring_settings, "clicked", G_CALLBACK(settings_clicked), win);
1126
1127 /* add the call view to the main stack */
1128 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
1129 priv->vbox_call_view,
1130 CALL_VIEW_NAME);
1131
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001132 /* init the settings views */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001133 priv->account_settings_view = account_view_new();
1134 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
1135
Stepan Salenikovich0bd53492015-05-11 14:28:52 -04001136 priv->media_settings_view = media_settings_view_new();
1137 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 -04001138
Stepan Salenikovichde896112015-05-11 16:46:33 -04001139 priv->general_settings_view = general_settings_view_new();
1140 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
1141
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001142 /* make the setting we will show first the active one */
Stepan Salenikovichde896112015-05-11 16:46:33 -04001143 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->radiobutton_general_settings), TRUE);
1144 priv->last_settings_view = priv->general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001145
1146 /* connect the settings button signals to switch settings views */
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001147 g_signal_connect(priv->radiobutton_media_settings, "toggled", G_CALLBACK(show_media_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001148 g_signal_connect(priv->radiobutton_account_settings, "toggled", G_CALLBACK(show_account_settings), win);
Stepan Salenikovichde896112015-05-11 16:46:33 -04001149 g_signal_connect(priv->radiobutton_general_settings, "toggled", G_CALLBACK(show_general_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001150
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001151 /* populate the notebook */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001152 priv->treeview_conversations = recent_contacts_view_new();
1153 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_smartview), priv->treeview_conversations);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -04001154
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001155 priv->treeview_contacts = contacts_view_new();
1156 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contacts), priv->treeview_contacts);
Stepan Salenikovich15142182015-03-11 17:15:26 -04001157
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001158 priv->treeview_history = history_view_new();
1159 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001160
Stepan Salenikovich2cde7612015-09-25 10:44:01 -04001161 /* welcome/default view */
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001162 priv->welcome_view = ring_welcome_view_new();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001163 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 -04001164 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
1165 gtk_widget_show(priv->welcome_view);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001166
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001167 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1168 g_signal_connect(selection_conversations, "changed", G_CALLBACK(conversation_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001169
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001170 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1171 g_signal_connect(selection_contacts, "changed", G_CALLBACK(contact_selection_changed), win);
1172
1173 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
1174 g_signal_connect(selection_history, "changed", G_CALLBACK(history_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001175
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -04001176 g_signal_connect_swapped(priv->button_new_conversation, "clicked", G_CALLBACK(search_entry_activated), win);
1177 g_signal_connect_swapped(priv->search_entry, "activate", G_CALLBACK(search_entry_activated), win);
Stepan Salenikovicha06ce192016-11-01 14:26:43 -04001178 g_signal_connect(priv->search_entry, "search-changed", G_CALLBACK(search_entry_text_changed), win);
Stepan Salenikovichcac35042016-10-28 12:07:38 -04001179 g_signal_connect(priv->search_entry, "key-release-event", G_CALLBACK(search_entry_key_released), win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001180
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001181 /* make sure the incoming call is the selected call in the CallModel */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001182 QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -04001183 &CallModel::instance(),
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001184 &CallModel::incomingCall,
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001185 [priv](Call* call) {
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001186 // clear the regex to make sure the call is shown
1187 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001188 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts),"");
1189 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001190
1191 // now make sure its selected
1192 RecentModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001193 CallModel::instance().selectCall(call);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001194 }
1195 );
Stepan Salenikovich5eab3032015-03-28 10:50:51 -04001196
Stepan Salenikovichb01d7362015-04-27 23:02:00 -04001197 /* react to digit key press events */
1198 g_signal_connect(win, "key-press-event", G_CALLBACK(dtmf_pressed), NULL);
Stepan Salenikovich48b45c12015-09-25 15:49:51 -04001199
1200 /* set the search entry placeholder text */
1201 gtk_entry_set_placeholder_text(GTK_ENTRY(priv->search_entry),
1202 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 -04001203
aviau039001d2016-09-29 16:39:05 -04001204 /* init chat webkit container so that it starts loading before the first time we need it*/
1205 get_webkit_chat_container(win);
1206
Stepan Salenikovichcbc8dcc2017-04-05 11:27:10 -04001207 if (has_ring_account()) {
1208 /* user has ring account, so show the call view right away */
1209 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->vbox_call_view);
1210
1211 handle_account_migrations(win);
1212 } else {
1213 /* user has to create the ring account */
1214 show_account_creation_wizard(win);
1215 }
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001216}
1217
1218static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001219ring_main_window_dispose(GObject *object)
1220{
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001221 RingMainWindow *self = RING_MAIN_WINDOW(object);
1222 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1223
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001224 QObject::disconnect(priv->selected_item_changed);
1225 QObject::disconnect(priv->selected_call_over);
aviau8c0b2422016-10-31 16:06:44 -04001226 QObject::disconnect(priv->username_lookup);
aviau039001d2016-09-29 16:39:05 -04001227
1228 g_clear_object(&priv->welcome_view);
1229 g_clear_object(&priv->webkit_chat_container);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001230
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001231 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1232}
1233
1234static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001235ring_main_window_finalize(GObject *object)
1236{
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001237 RingMainWindow *self = RING_MAIN_WINDOW(object);
1238 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1239
1240 g_clear_object(&priv->settings);
1241
Guillaume Rogueze7221ed2017-03-17 20:09:56 -04001242 delete priv->pending_username_lookup;
1243 priv->pending_username_lookup = nullptr;
1244
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001245 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001246}
1247
1248static void
1249ring_main_window_class_init(RingMainWindowClass *klass)
1250{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001251 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001252 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001253
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001254 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1255 "/cx/ring/RingGnome/ringmainwindow.ui");
1256
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001257 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001258 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001259 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contacts);
1260 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_history);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001261 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1262 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1263 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1264 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001265 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001266 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001267 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001268 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001269 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001270 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001271 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001272 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001273 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Nicolas Jager55903142017-02-01 14:43:45 -05001274 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, spinner_lookup);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001275}
1276
1277GtkWidget *
1278ring_main_window_new (GtkApplication *app)
1279{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001280 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
1281
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001282 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001283}