blob: 2bdf87eadfd5c303fc280dfd14af0b513f098134 [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;
Stepan Salenikovich5eab3032015-03-28 10:50:51 -0400128
aviau8c0b2422016-10-31 16:06:44 -0400129 /* Pending ring usernames lookup for the search entry */
130 QMetaObject::Connection username_lookup;
131
aviau039001d2016-09-29 16:39:05 -0400132 /* The webkit_chat_container is created once, then reused for all chat views */
133 GtkWidget *webkit_chat_container;
134
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400135 QMetaObject::Connection selected_item_changed;
136 QMetaObject::Connection selected_call_over;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400137
138 gboolean show_settings;
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400139
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500140 /* fullscreen */
141 gboolean is_fullscreen;
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400142
143 GSettings *settings;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500144};
145
146G_DEFINE_TYPE_WITH_PRIVATE(RingMainWindow, ring_main_window, GTK_TYPE_APPLICATION_WINDOW);
147
148#define RING_MAIN_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_MAIN_WINDOW_TYPE, RingMainWindowPrivate))
149
aviau039001d2016-09-29 16:39:05 -0400150static gboolean selection_changed(RingMainWindow *win);
151
152static WebKitChatContainer*
153get_webkit_chat_container(RingMainWindow *win)
154{
155 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
156 if (!priv->webkit_chat_container)
157 {
158 priv->webkit_chat_container = webkit_chat_container_new();
159
160 //We don't want it to be deleted, ever.
161 g_object_ref(priv->webkit_chat_container);
162 }
163 return WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container);
164}
165
Stepan Salenikovich69771842015-02-24 18:11:45 -0500166static void
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500167enter_full_screen(RingMainWindow *self)
168{
169 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
170 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
171
172 if (!priv->is_fullscreen) {
173 gtk_widget_hide(priv->vbox_left_pane);
174 gtk_window_fullscreen(GTK_WINDOW(self));
175 priv->is_fullscreen = TRUE;
176 }
177}
178
179static void
180leave_full_screen(RingMainWindow *self)
181{
182 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
183 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
184
185 if (priv->is_fullscreen) {
186 gtk_widget_show(priv->vbox_left_pane);
187 gtk_window_unfullscreen(GTK_WINDOW(self));
188 priv->is_fullscreen = FALSE;
189 }
190}
191
192static void
Stepan Salenikovich5e431cf2015-12-24 14:09:01 -0500193video_double_clicked(G_GNUC_UNUSED CurrentCallView *view, RingMainWindow *self)
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500194{
195 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
196 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
197
198 if (priv->is_fullscreen) {
199 leave_full_screen(self);
200 } else {
201 enter_full_screen(self);
202 }
203}
204
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400205static void
206hide_view_clicked(G_GNUC_UNUSED GtkWidget *view, RingMainWindow *self)
207{
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400208 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400209
210 /* clear selection */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400211 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
212 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
213 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
214 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
215 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
216 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
Stepan Salenikovich8043a562016-03-18 13:56:40 -0400217}
218
aviau039001d2016-09-29 16:39:05 -0400219static void
220change_view(RingMainWindow *self, GtkWidget* old, QObject *object, GType type)
221{
222 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
223 leave_full_screen(self);
224 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old);
225
226 GtkWidget *new_view = nullptr;
227
228 QObject::disconnect(priv->selected_item_changed);
229 QObject::disconnect(priv->selected_call_over);
230
231 if (g_type_is_a(INCOMING_CALL_VIEW_TYPE, type)) {
232 if (auto call = qobject_cast<Call *>(object)) {
233 new_view = incoming_call_view_new(call, get_webkit_chat_container(self));
234 priv->selected_item_changed = QObject::connect(
235 call,
236 &Call::lifeCycleStateChanged,
237 [self] (Call::LifeCycleState, Call::LifeCycleState)
238 {g_idle_add((GSourceFunc)selection_changed, self);}
239 );
240 priv->selected_call_over = QObject::connect(
241 call,
242 &Call::isOver,
243 [self] ()
244 {g_idle_add((GSourceFunc)selection_changed, self);}
245 );
246 } else
247 g_warning("Trying to display a view of type IncomingCallView, but the object is not of type Call");
248 } else if (g_type_is_a(CURRENT_CALL_VIEW_TYPE, type)) {
249 if (auto call = qobject_cast<Call *>(object)) {
250 new_view = current_call_view_new(call, get_webkit_chat_container(self));
251 g_signal_connect(new_view, "video-double-clicked", G_CALLBACK(video_double_clicked), self);
252 priv->selected_item_changed = QObject::connect(
253 call,
254 &Call::lifeCycleStateChanged,
255 [self] (Call::LifeCycleState, Call::LifeCycleState)
256 {g_idle_add((GSourceFunc)selection_changed, self);}
257 );
258 priv->selected_call_over = QObject::connect(
259 call,
260 &Call::isOver,
261 [self] ()
262 {g_idle_add((GSourceFunc)selection_changed, self);}
263 );
264 } else
265 g_warning("Trying to display a view of type CurrentCallView, but the object is not of type Call");
266 } else if (g_type_is_a(CHAT_VIEW_TYPE, type)) {
267 if (auto person = qobject_cast<Person *>(object)) {
268 new_view = chat_view_new_person(get_webkit_chat_container(self), person);
269 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
270
271 /* connect to the Person's callAdded signal, because we want to switch to the call view
272 * in this case */
273 priv->selected_item_changed = QObject::connect(
274 person,
275 &Person::callAdded,
276 [self] (Call*)
277 {g_idle_add((GSourceFunc)selection_changed, self);}
278 );
279 } else if (auto cm = qobject_cast<ContactMethod *>(object)) {
280 new_view = chat_view_new_cm(get_webkit_chat_container(self), cm);
281 g_signal_connect(new_view, "hide-view-clicked", G_CALLBACK(hide_view_clicked), self);
282
283 /* connect to the ContactMethod's callAdded signal, because we want to switch to the
284 * call view in this case */
285 priv->selected_item_changed = QObject::connect(
286 cm,
287 &ContactMethod::callAdded,
288 [self] (Call*)
289 {g_idle_add((GSourceFunc)selection_changed, self);}
290 );
291 } else {
292 g_warning("Trying to display a veiw of type ChatView, but the object is neither a Person nor a ContactMethod");
293 }
294 } else {
295 // display the welcome view
296 new_view = priv->welcome_view;
297 }
298
299 gtk_container_add(GTK_CONTAINER(priv->frame_call), new_view);
300 gtk_widget_show(new_view);
301}
302
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500303/**
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400304 * This function determines which view to display in the right panel of the main window based on
305 * which item is selected in one of the 3 contact list views (Conversations, Contacts, History). The
306 * possible views ares:
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500307 * - incoming call view
308 * - current call view
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500309 * - chat view
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400310 * - welcome view (if no valid item is selected)
311 *
312 * There should never be a conflict of which item should be displayed (ie: which item is selected),
313 * as there should only ever be one selection at a time in the 3 views except in the case that the
314 * same item is selected in more than one view (see the compare_treeview_selection() function).
315 *
316 * This function could be called from a g_idle source, so it returns a boolean to remove itself from
317 * being called again. The boolean doesn't reflect success nor failure.
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500318 */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400319static gboolean
320selection_changed(RingMainWindow *win)
Stepan Salenikovich69771842015-02-24 18:11:45 -0500321{
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500322 // g_debug("selection changed");
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400323
324 g_return_val_if_fail(IS_RING_MAIN_WINDOW(win), G_SOURCE_REMOVE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500325 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(win));
326
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400327 auto old_view = gtk_bin_get_child(GTK_BIN(priv->frame_call));
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500328
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400329 /* if we're showing the settings, then we need to make sure to remove any possible ongoing call
330 * view so that we don't have more than one VideoWidget at a time */
331 if (priv->show_settings) {
332 if (old_view != priv->welcome_view) {
333 leave_full_screen(win);
334 gtk_container_remove(GTK_CONTAINER(priv->frame_call), old_view);
335 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
336 gtk_widget_show(priv->welcome_view);
337 }
338 return G_SOURCE_REMOVE;
339 }
Stepan Salenikovich69771842015-02-24 18:11:45 -0500340
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400341 /* there should only be one item selected at a time, get which one is selected */
342 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
343 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
344 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
345
346 GtkTreeModel *model = nullptr;
347 GtkTreeIter iter;
348 QModelIndex idx;
349
350 if (gtk_tree_selection_get_selected(selection_conversations, &model, &iter)) {
Stepan Salenikovichf6078222016-10-03 17:31:16 -0400351 idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(model), &iter);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400352 } else if (gtk_tree_selection_get_selected(selection_contacts, &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_history, &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 }
Stepan Salenikoviche1b54892015-12-13 22:18:44 -0500357
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500358 /* check which object type is selected */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400359 auto type = idx.data(static_cast<int>(Ring::Role::ObjectType));
360 auto object = idx.data(static_cast<int>(Ring::Role::Object));
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500361
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400362 if (idx.isValid() && type.isValid() && object.isValid()) {
Stepan Salenikovich69771842015-02-24 18:11:45 -0500363
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400364 /* we prioritize the views in the following order:
365 * - call view (if there is an ongoing call associated with the item)
366 * - chat view built from Person
367 * - chat view built from ContactMethod
368 */
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500369
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400370 Person *person = nullptr;
371 ContactMethod *cm = nullptr;
372 Call *call = nullptr;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500373
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400374 /* use the RecentModel to see if there are any ongoing calls associated with the selected item */
375 switch(type.value<Ring::ObjectType>()) {
376 case Ring::ObjectType::Person:
377 {
378 person = object.value<Person *>();
379 auto recent_idx = RecentModel::instance().getIndex(person);
380 if (recent_idx.isValid()) {
381 call = RecentModel::instance().getActiveCall(recent_idx);
382 }
383 }
384 break;
385 case Ring::ObjectType::ContactMethod:
386 {
387 cm = object.value<ContactMethod *>();
388 auto recent_idx = RecentModel::instance().getIndex(cm);
389 if (recent_idx.isValid()) {
390 call = RecentModel::instance().getActiveCall(recent_idx);
391 }
392 }
393 break;
394 case Ring::ObjectType::Call:
395 {
396 /* if the call is over (eg: if it comes from the history model) then we first check
397 * if there is an ongoing call with the same ContactMethod, otherwise we need to get
398 * the associated Person or ContactMethod */
399 call = object.value<Call *>();
400 if (call->isHistory()) {
401 cm = call->peerContactMethod();
402 call = nullptr;
403 if (cm) {
404 auto recent_idx = RecentModel::instance().getIndex(cm);
405 if (recent_idx.isValid()) {
406 call = RecentModel::instance().getActiveCall(recent_idx);
407 }
408 person = cm->contact();
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500409 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500410 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400411 }
412 break;
413 case Ring::ObjectType::Media:
414 case Ring::ObjectType::Certificate:
415 case Ring::ObjectType::COUNT__:
416 // nothing to do
417 break;
418 }
419
420 /* we prioritize showing the call view */
421 if (call) {
422 auto state = call->lifeCycleState();
423
424 Call *current_call = nullptr;
425
426 switch(state) {
427 case Call::LifeCycleState::CREATION:
428 case Call::LifeCycleState::INITIALIZATION:
429 case Call::LifeCycleState::FINISHED:
430 // check if we're already displaying this call
431 if (IS_INCOMING_CALL_VIEW(old_view))
432 current_call = incoming_call_view_get_call(INCOMING_CALL_VIEW(old_view));
433 if (current_call != call)
aviau039001d2016-09-29 16:39:05 -0400434 change_view(win, old_view, call, INCOMING_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400435 break;
436 case Call::LifeCycleState::PROGRESS:
437 // check if we're already displaying this call
438 if (IS_CURRENT_CALL_VIEW(old_view))
439 current_call = current_call_view_get_call(CURRENT_CALL_VIEW(old_view));
aviau039001d2016-09-29 16:39:05 -0400440 if (current_call != call)
441 change_view(win, old_view, call, CURRENT_CALL_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400442 break;
443 case Call::LifeCycleState::COUNT__:
444 g_warning("LifeCycleState should never be COUNT");
445 break;
446 }
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500447
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400448 } else if (person) {
449 /* show chat view constructed from Person object */
450
451 // check if we're already displaying the chat view for this person
452 Person *current_person = nullptr;
453 if (IS_CHAT_VIEW(old_view))
454 current_person = chat_view_get_person(CHAT_VIEW(old_view));
455
aviau039001d2016-09-29 16:39:05 -0400456 if (current_person != person)
457 change_view(win, old_view, person, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400458 } else if (cm) {
459 /* show chat view constructed from CM */
460
461 // check if we're already displaying the chat view for this cm
462 ContactMethod *current_cm = nullptr;
463 if (IS_CHAT_VIEW(old_view))
464 current_cm = chat_view_get_cm(CHAT_VIEW(old_view));
465
aviau039001d2016-09-29 16:39:05 -0400466 if (current_cm != cm)
467 change_view(win, old_view, cm, CHAT_VIEW_TYPE);
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400468 } else {
469 /* not a supported object type, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400470 if (old_view != priv->welcome_view)
471 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -0500472 }
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400473 } else {
474 /* selection isn't valid, display the welcome view */
aviau039001d2016-09-29 16:39:05 -0400475 if (old_view != priv->welcome_view)
476 change_view(win, old_view, nullptr, RING_WELCOME_VIEW_TYPE);
Stepan Salenikovich69771842015-02-24 18:11:45 -0500477 }
Stepan Salenikovich504f7722016-01-20 17:10:45 -0500478
479 return G_SOURCE_REMOVE;
Stepan Salenikovich69771842015-02-24 18:11:45 -0500480}
481
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500482static void
aviau8c0b2422016-10-31 16:06:44 -0400483process_search_entry_contact_method(RingMainWindow *self, const URI& uri)
484{
485 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
486
487 auto cm = PhoneDirectoryModel::instance().getNumber(uri);
488
489 if (g_settings_get_boolean(priv->settings, "search-entry-places-call")) {
490 place_new_call(cm);
491
492 /* move focus away from entry so that DTMF tones can be entered via the keyboard */
493 gtk_widget_child_focus(GTK_WIDGET(self), GTK_DIR_TAB_FORWARD);
494 } else {
495 // if its a new CM, bring it to the top
496 if (cm->lastUsed() == 0)
497 cm->setLastUsed(QDateTime::currentDateTime().toTime_t());
498
499 // select cm
500 RecentModel::instance().selectionModel()->setCurrentIndex(RecentModel::instance().getIndex(cm), QItemSelectionModel::ClearAndSelect);
501 }
502 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
503}
504
505static void
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400506search_entry_activated(RingMainWindow *self)
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500507{
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400508 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500509
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -0400510 const auto *number_entered = gtk_entry_get_text(GTK_ENTRY(priv->search_entry));
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500511
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400512 URI uri = URI(number_entered);
513
514
515 // make sure the userinfo part isn't empty, only specifying a protocol isn't enough
516 if (!uri.userinfo().isEmpty()) {
517
518 gboolean lookup_username = FALSE;
519
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500520 /* get the account with which we will do the lookup */
521 auto ring_account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::RING);
522
Stepan Salenikovich7b7be482016-11-03 16:57:48 -0400523 if (uri.protocolHint() == URI::ProtocolHint::RING_USERNAME ) {
524 lookup_username = TRUE;
525 } else if (
526 uri.protocolHint() != URI::ProtocolHint::RING && // not a RingID
527 uri.schemeType() == URI::SchemeType::NONE // scheme type not specified
528 ) {
529 // if no scheme type has been specified, determine ring vs sip by the first available account
530 auto idx = AvailableAccountModel::instance().index(0, 0);
531 if (idx.isValid()) {
532 auto account = idx.data((int)Ring::Role::Object).value<Account *>();
533 if (account && account->protocol() == Account::Protocol::RING)
534 lookup_username = TRUE;
535 }
536 }
537
538 if (lookup_username)
aviau8c0b2422016-10-31 16:06:44 -0400539 {
540 gtk_widget_set_sensitive(priv->search_entry, FALSE);
541 gtk_widget_set_sensitive(priv->button_new_conversation, FALSE);
542 gtk_entry_set_text(
543 GTK_ENTRY(priv->search_entry),
544 _("looking up RingID")
545 );
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,
557 [self, priv, username_to_lookup] (G_GNUC_UNUSED const Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) {
558 if (username_to_lookup.compare(name) != 0)
559 {
560 //That is not our lookup.
561 return;
562 }
Stepan Salenikovich1564bba2015-04-29 11:28:43 -0400563
aviau8c0b2422016-10-31 16:06:44 -0400564 gtk_widget_set_sensitive(priv->search_entry, TRUE);
565 gtk_widget_set_sensitive(priv->button_new_conversation, TRUE);
566 gtk_entry_set_text(GTK_ENTRY(priv->search_entry), "");
567
568 switch(status)
569 {
570 case NameDirectory::LookupStatus::SUCCESS:
571 {
572 URI uri = URI("ring:" + address);
573 process_search_entry_contact_method(self, uri);
574 break;
575 }
576 case NameDirectory::LookupStatus::INVALID_NAME:
577 {
578 //Can't be a ring username, could be something else.
579 auto dialog = gtk_message_dialog_new(
580 GTK_WINDOW(self),
581 GTK_DIALOG_DESTROY_WITH_PARENT,
582 GTK_MESSAGE_ERROR,
583 GTK_BUTTONS_CLOSE,
584 _("Invalid Ring username: %s"),
585 name.toUtf8().constData()
586 );
587
588 gtk_dialog_run (GTK_DIALOG (dialog));
589 gtk_widget_destroy (dialog);
590 break;
591 }
592 case NameDirectory::LookupStatus::ERROR:
Stepan Salenikovichfa24b932016-11-21 15:19:57 -0500593 {
594 auto dialog = gtk_message_dialog_new(
595 GTK_WINDOW(self),
596 GTK_DIALOG_DESTROY_WITH_PARENT,
597 GTK_MESSAGE_ERROR,
598 GTK_BUTTONS_CLOSE,
599 _("Error resolving username, nameserver is possibly unreachable")
600 );
601
602 gtk_dialog_run(GTK_DIALOG (dialog));
603 gtk_widget_destroy(dialog);
604 break;
605 }
aviau8c0b2422016-10-31 16:06:44 -0400606 case NameDirectory::LookupStatus::NOT_FOUND:
607 {
608 auto dialog = gtk_message_dialog_new(
609 GTK_WINDOW(self),
610 GTK_DIALOG_DESTROY_WITH_PARENT,
611 GTK_MESSAGE_ERROR,
612 GTK_BUTTONS_CLOSE,
613 _("Could not resolve Ring username: %s"),
614 name.toUtf8().constData()
615 );
616
617 gtk_dialog_run(GTK_DIALOG (dialog));
618 gtk_widget_destroy(dialog);
619 break;
620 }
621 }
622 }
623 );
624
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500625 NameDirectory::instance().lookupName(ring_account, QString(), username_to_lookup);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400626 }
aviau8c0b2422016-10-31 16:06:44 -0400627 else
628 {
629 process_search_entry_contact_method(self, uri);
630 }
Stepan Salenikovich2e6ab562015-05-07 12:33:07 -0400631 }
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500632}
633
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400634static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400635save_accounts(GtkWidget *working_dialog)
636{
637 /* save changes to accounts */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400638 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400639
640 if (working_dialog)
641 gtk_widget_destroy(working_dialog);
642
643 return G_SOURCE_REMOVE;
644}
645
Stepan Salenikovich15142182015-03-11 17:15:26 -0400646static void
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400647settings_clicked(G_GNUC_UNUSED GtkButton *button, RingMainWindow *win)
648{
649 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
650 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
651
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400652 /* check which view to show */
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500653 if (!priv->show_settings) {
654 /* show the settings */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400655 priv->show_settings = TRUE;
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500656
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500657 /* 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 -0400658 selection_changed(win);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500659
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400660 /* show settings */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400661 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 -0400662
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400663 gtk_widget_show(priv->hbox_settings);
664
Stepan Salenikovich41118912015-05-01 11:25:46 -0400665 /* make sure to start preview if we're showing the video settings */
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400666 if (priv->last_settings_view == priv->media_settings_view)
667 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400668
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400669 /* make sure to show the profile if we're showing the general settings */
670 if (priv->last_settings_view == priv->general_settings_view)
671 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
672
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400673 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_UP);
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_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_DOWN);
699 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400700
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500701 /* show the view which was selected previously */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400702 selection_changed(win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400703 }
704}
705
706static void
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400707show_media_settings(GtkToggleButton *navbutton, RingMainWindow *win)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400708{
709 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
710 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
711
712 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400713 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400714 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400715 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400716 priv->last_settings_view = priv->media_settings_view;
Stepan Salenikovich41118912015-05-01 11:25:46 -0400717 } else {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400718 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400719 }
720}
721
722static void
723show_account_settings(GtkToggleButton *navbutton, RingMainWindow *win)
724{
725 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
726 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
727
728 if (gtk_toggle_button_get_active(navbutton)) {
729 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
730 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), ACCOUNT_SETTINGS_VIEW_NAME);
731 priv->last_settings_view = priv->account_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400732 }
733}
734
Stepan Salenikovichde896112015-05-11 16:46:33 -0400735static void
736show_general_settings(GtkToggleButton *navbutton, RingMainWindow *win)
737{
738 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
739 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
740
741 if (gtk_toggle_button_get_active(navbutton)) {
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400742 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400743 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
744 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
745 priv->last_settings_view = priv->general_settings_view;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400746 } else {
747 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400748 }
749}
750
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400751static void
aviau6aeb4852016-08-18 16:01:09 -0400752on_account_creation_completed(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400753{
Stepan Salenikovichbabe26f2016-02-12 16:47:07 -0500754 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400755 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
756
aviau6aeb4852016-08-18 16:01:09 -0400757 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT);
758 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
759
760 /* destroy the wizard */
aviau69081842016-10-14 14:51:54 -0400761 if (priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400762 {
aviau69081842016-10-14 14:51:54 -0400763 gtk_container_remove(GTK_CONTAINER(priv->stack_main_view), priv->account_creation_wizard);
764 gtk_widget_destroy(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400765 }
aviau6aeb4852016-08-18 16:01:09 -0400766
767 /* show the settings button*/
768 gtk_widget_show(priv->ring_settings);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400769}
770
771static void
aviau6aeb4852016-08-18 16:01:09 -0400772show_account_creation_wizard(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400773{
774 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
775
aviau61d05b32016-10-18 16:47:53 -0400776 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400777 {
aviau69081842016-10-14 14:51:54 -0400778 priv->account_creation_wizard = account_creation_wizard_new(false);
779 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
780 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 -0400781
aviau6aeb4852016-08-18 16:01:09 -0400782 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400783 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400784 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
785 }
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400786
Stepan Salenikovich64505672015-09-24 10:46:07 -0400787 /* hide settings button until account creation is complete */
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400788 gtk_widget_hide(priv->ring_settings);
789
aviau69081842016-10-14 14:51:54 -0400790 gtk_widget_show(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400791
aviau69081842016-10-14 14:51:54 -0400792 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400793}
794
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400795static void
Stepan Salenikovicha06ce192016-11-01 14:26:43 -0400796search_entry_text_changed(GtkSearchEntry *search_entry, RingMainWindow *self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400797{
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400798 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
799
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400800 /* get the text from the entry */
801 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
802
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400803 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400804 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), text);
805 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), text);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400806}
807
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400808static gboolean
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400809search_entry_key_released(GtkEntry *search_entry, GdkEventKey *key, RingMainWindow *self)
810{
811 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
812
813 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
814 if (key->keyval == GDK_KEY_Escape) {
815 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
816 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), "");
817 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
818 return GDK_EVENT_STOP;
819 }
820
821 return GDK_EVENT_PROPAGATE;
822}
823
824static gboolean
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400825dtmf_pressed(RingMainWindow *win,
826 GdkEventKey *event,
827 G_GNUC_UNUSED gpointer user_data)
828{
829 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
830
831 /* we want to react to digit key presses, as long as a GtkEntry is not the
832 * input focus
833 */
834 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(win));
835 if (GTK_IS_ENTRY(focus))
836 return GDK_EVENT_PROPAGATE;
837
838 /* make sure that a call is selected*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400839 QItemSelectionModel *selection = CallModel::instance().selectionModel();
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400840 QModelIndex idx = selection->currentIndex();
841 if (!idx.isValid())
842 return GDK_EVENT_PROPAGATE;
843
844 /* make sure that the selected call is in progress */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400845 Call *call = CallModel::instance().getCall(idx);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400846 Call::LifeCycleState state = call->lifeCycleState();
847 if (state != Call::LifeCycleState::PROGRESS)
848 return GDK_EVENT_PROPAGATE;
849
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400850 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
851 * does not result in a 'c' being played.
852 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
853 if ( event->state
854 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
855 return GDK_EVENT_PROPAGATE;
856
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400857 /* pass the character that was entered to be played by the daemon;
858 * the daemon will filter out invalid DTMF characters */
859 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
860 QString val = QString::fromUcs4(&unicode_val, 1);
861 call->playDTMF(val);
862 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400863
864 /* always propogate the key, so we don't steal accelerators/shortcuts */
865 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400866}
867
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400868/**
869 * This function determines if two different contact list views (Conversations, Contacts, History)
870 * have the same item selected. Note that the same item does not necessarily mean the same object.
871 * For example, if the history view has a Call selected and the Conversations view has a Person
872 * selected which is associated with the ContactMethod to which that Call was made, then this will
873 * evaluate to TRUE.
874 */
875static gboolean
876compare_treeview_selection(GtkTreeSelection *selection1, GtkTreeSelection *selection2)
877{
878 g_return_val_if_fail(selection1 && selection2, FALSE);
879
880 if (selection1 == selection2) return TRUE;
881
882 auto idx1 = get_index_from_selection(selection1);
883 auto type1 = idx1.data(static_cast<int>(Ring::Role::ObjectType));
884 auto object1 = idx1.data(static_cast<int>(Ring::Role::Object));
885
886 auto idx2 = get_index_from_selection(selection2);
887 auto type2 = idx2.data(static_cast<int>(Ring::Role::ObjectType));
888 auto object2 = idx2.data(static_cast<int>(Ring::Role::Object));
889
890 if (idx1.isValid() && type1.isValid() && object1.isValid() && idx2.isValid() && type2.isValid() && object2.isValid()) {
891 Call *call1 = nullptr;
892 ContactMethod *cm1 = nullptr;
893 Person *person1 = nullptr;
894
895 Call *call2 = nullptr;
896 ContactMethod *cm2 = nullptr;
897 Person *person2 = nullptr;
898
899 switch(type1.value<Ring::ObjectType>()) {
900 case Ring::ObjectType::Person:
901 person1 = object1.value<Person *>();
902 break;
903 case Ring::ObjectType::ContactMethod:
904 cm1 = object1.value<ContactMethod *>();
905 person1 = cm1->contact();
906 break;
907 case Ring::ObjectType::Call:
908 call1 = object1.value<Call *>();
909 cm1 = call1->peerContactMethod();
910 person1 = cm1->contact();
911 break;
912 case Ring::ObjectType::Media:
913 case Ring::ObjectType::Certificate:
914 case Ring::ObjectType::COUNT__:
915 // nothing to do
916 break;
917 }
918
919 switch(type2.value<Ring::ObjectType>()) {
920 case Ring::ObjectType::Person:
921 person2 = object2.value<Person *>();
922 break;
923 case Ring::ObjectType::ContactMethod:
924 cm2 = object2.value<ContactMethod *>();
925 person2 = cm2->contact();
926 break;
927 case Ring::ObjectType::Call:
928 call2 = object2.value<Call *>();
929 cm2 = call2->peerContactMethod();
930 person2 = cm2->contact();
931 break;
932 case Ring::ObjectType::Media:
933 case Ring::ObjectType::Certificate:
934 case Ring::ObjectType::COUNT__:
935 // nothing to do
936 break;
937 }
938
939 if (person1 != nullptr && person1 == person2)
940 return TRUE;
941 if (cm1 != nullptr && cm1 == cm2)
942 return TRUE;
943 if (call1 != nullptr && call1 == call2)
944 return TRUE;
945 }
946
947 return FALSE;
948}
949
950static void
951conversation_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
952{
953 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
954 GtkTreeIter iter;
955 GtkTreeModel *model = nullptr;
956 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
957 /* something is selected in the conversations view, clear the selection in the other views;
958 * unless the current selection is of the same item; we don't try to match the selection in
959 * all views since not all items exist in all 3 contact list views
960 */
961
962 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
963 if (!compare_treeview_selection(selection, selection_contacts))
964 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
965
966 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
967 if (!compare_treeview_selection(selection, selection_history))
968 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
969 }
970
971 selection_changed(self);
972}
973
974static void
975contact_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
976{
977 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
978 GtkTreeIter iter;
979 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
980 /* something is selected in the contacts view, clear the selection in the other views;
981 * unless the current selection is of the same item; we don't try to match the selection in
982 * all views since not all items exist in all 3 contact list views
983 */
984 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
985 if (!compare_treeview_selection(selection, selection_conversations))
986 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
987
988 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
989 if (!compare_treeview_selection(selection, selection_history))
990 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
991 }
992
993 selection_changed(self);
994}
995
996static void
997history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
998{
999 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1000 GtkTreeIter iter;
1001 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
1002 /* something is selected in the history view, clear the selection in the other views;
1003 * unless the current selection is of the same item; we don't try to match the selection in
1004 * all views since not all items exist in all 3 contact list views
1005 */
1006
1007 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1008 if (!compare_treeview_selection(selection, selection_conversations))
1009 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
1010
1011 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1012 if (!compare_treeview_selection(selection, selection_contacts))
1013 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1014 }
1015
1016 selection_changed(self);
1017}
1018
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001019static gboolean
1020window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
1021{
1022 auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1023
1024 g_settings_set_int(priv->settings, "window-width", event->width);
1025 g_settings_set_int(priv->settings, "window-height", event->height);
1026
1027 return GDK_EVENT_PROPAGATE;
1028}
1029
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -04001030static void
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001031search_entry_places_call_changed(GSettings *settings, const gchar *key, RingMainWindow *self)
1032{
1033 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1034
1035 if (g_settings_get_boolean(settings, key)) {
1036 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will place a new call", "place call"));
1037 } else {
1038 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will open chat", "open chat"));
1039 }
1040}
1041
1042static void
aviau6aeb4852016-08-18 16:01:09 -04001043handle_account_migrations(RingMainWindow *win)
1044{
1045 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1046
1047 /* If there is an existing migration view, remove it */
aviau69081842016-10-14 14:51:54 -04001048 if (priv->account_migration_view)
aviau6aeb4852016-08-18 16:01:09 -04001049 {
Stepan Salenikovich56530b12016-10-20 17:51:34 -04001050 gtk_widget_destroy(priv->account_migration_view);
1051 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -04001052 }
1053
1054 QList<Account*> accounts = AccountModel::instance().accountsToMigrate();
1055 if (!accounts.isEmpty())
1056 {
1057 Account* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -04001058
aviau69081842016-10-14 14:51:54 -04001059 priv->account_migration_view = account_migration_view_new(account);
aviau69081842016-10-14 14:51:54 -04001060 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed", G_CALLBACK(handle_account_migrations), win);
1061 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
aviau6aeb4852016-08-18 16:01:09 -04001062
1063 gtk_widget_hide(priv->ring_settings);
aviau69081842016-10-14 14:51:54 -04001064 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -04001065 gtk_stack_add_named(
1066 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -04001067 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -04001068 ACCOUNT_MIGRATION_VIEW_NAME
1069 );
1070 gtk_stack_set_visible_child_name(
1071 GTK_STACK(priv->stack_main_view),
1072 ACCOUNT_MIGRATION_VIEW_NAME
1073 );
1074 }
1075 else
1076 {
1077 gtk_widget_show(priv->ring_settings);
1078 }
1079}
1080
1081static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001082ring_main_window_init(RingMainWindow *win)
1083{
1084 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1085 gtk_widget_init_template(GTK_WIDGET(win));
1086
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001087 /* bind to window size settings */
1088 priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
1089 auto width = g_settings_get_int(priv->settings, "window-width");
1090 auto height = g_settings_get_int(priv->settings, "window-height");
1091 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1092 g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
1093
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001094 /* search-entry-places-call setting */
1095 search_entry_places_call_changed(priv->settings, "search-entry-places-call", win);
1096 g_signal_connect(priv->settings, "changed::search-entry-places-call", G_CALLBACK(search_entry_places_call_changed), win);
1097
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001098 /* set window icon */
1099 GError *error = NULL;
1100 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
1101 if (icon == NULL) {
1102 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001103 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001104 } else
1105 gtk_window_set_icon(GTK_WINDOW(win), icon);
1106
1107 /* set menu icon */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001108 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001109 -1, 16, TRUE, &error);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001110 if (image_ring == NULL) {
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001111 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001112 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001113 } else
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001114 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_ring), image_ring);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001115
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001116 /* ring menu */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001117 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
1118 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001119 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->ring_menu), menu);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001120 g_object_unref(builder);
Stepan Salenikovicha3557452015-02-20 14:14:12 -05001121
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001122 /* settings icon */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001123 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 -04001124
1125 /* connect settings button signal */
1126 g_signal_connect(priv->ring_settings, "clicked", G_CALLBACK(settings_clicked), win);
1127
1128 /* add the call view to the main stack */
1129 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
1130 priv->vbox_call_view,
1131 CALL_VIEW_NAME);
1132
Stepan Salenikovichb8e41272015-03-27 14:31:54 -04001133 if (has_ring_account()) {
1134 /* user has ring account, so show the call view right away */
1135 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->vbox_call_view);
1136 } else {
1137 /* user has to create the ring account */
aviau6aeb4852016-08-18 16:01:09 -04001138 show_account_creation_wizard(win);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -04001139 }
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001140
1141 /* init the settings views */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001142 priv->account_settings_view = account_view_new();
1143 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
1144
Stepan Salenikovich0bd53492015-05-11 14:28:52 -04001145 priv->media_settings_view = media_settings_view_new();
1146 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 -04001147
Stepan Salenikovichde896112015-05-11 16:46:33 -04001148 priv->general_settings_view = general_settings_view_new();
1149 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
1150
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001151 /* make the setting we will show first the active one */
Stepan Salenikovichde896112015-05-11 16:46:33 -04001152 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->radiobutton_general_settings), TRUE);
1153 priv->last_settings_view = priv->general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001154
1155 /* connect the settings button signals to switch settings views */
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001156 g_signal_connect(priv->radiobutton_media_settings, "toggled", G_CALLBACK(show_media_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001157 g_signal_connect(priv->radiobutton_account_settings, "toggled", G_CALLBACK(show_account_settings), win);
Stepan Salenikovichde896112015-05-11 16:46:33 -04001158 g_signal_connect(priv->radiobutton_general_settings, "toggled", G_CALLBACK(show_general_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001159
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001160 /* populate the notebook */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001161 priv->treeview_conversations = recent_contacts_view_new();
1162 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_smartview), priv->treeview_conversations);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -04001163
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001164 priv->treeview_contacts = contacts_view_new();
1165 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contacts), priv->treeview_contacts);
Stepan Salenikovich15142182015-03-11 17:15:26 -04001166
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001167 priv->treeview_history = history_view_new();
1168 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001169
Stepan Salenikovich2cde7612015-09-25 10:44:01 -04001170 /* welcome/default view */
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001171 priv->welcome_view = ring_welcome_view_new();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001172 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 -04001173 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
1174 gtk_widget_show(priv->welcome_view);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001175
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001176 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1177 g_signal_connect(selection_conversations, "changed", G_CALLBACK(conversation_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001178
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001179 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1180 g_signal_connect(selection_contacts, "changed", G_CALLBACK(contact_selection_changed), win);
1181
1182 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
1183 g_signal_connect(selection_history, "changed", G_CALLBACK(history_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001184
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -04001185 g_signal_connect_swapped(priv->button_new_conversation, "clicked", G_CALLBACK(search_entry_activated), win);
1186 g_signal_connect_swapped(priv->search_entry, "activate", G_CALLBACK(search_entry_activated), win);
Stepan Salenikovicha06ce192016-11-01 14:26:43 -04001187 g_signal_connect(priv->search_entry, "search-changed", G_CALLBACK(search_entry_text_changed), win);
Stepan Salenikovichcac35042016-10-28 12:07:38 -04001188 g_signal_connect(priv->search_entry, "key-release-event", G_CALLBACK(search_entry_key_released), win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001189
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001190 /* make sure the incoming call is the selected call in the CallModel */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001191 QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -04001192 &CallModel::instance(),
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001193 &CallModel::incomingCall,
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001194 [priv](Call* call) {
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001195 // clear the regex to make sure the call is shown
1196 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001197 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts),"");
1198 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001199
1200 // now make sure its selected
1201 RecentModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001202 CallModel::instance().selectCall(call);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001203 }
1204 );
Stepan Salenikovich5eab3032015-03-28 10:50:51 -04001205
Stepan Salenikovichb01d7362015-04-27 23:02:00 -04001206 /* react to digit key press events */
1207 g_signal_connect(win, "key-press-event", G_CALLBACK(dtmf_pressed), NULL);
Stepan Salenikovich48b45c12015-09-25 15:49:51 -04001208
1209 /* set the search entry placeholder text */
1210 gtk_entry_set_placeholder_text(GTK_ENTRY(priv->search_entry),
1211 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 -04001212
aviau039001d2016-09-29 16:39:05 -04001213 /* init chat webkit container so that it starts loading before the first time we need it*/
1214 get_webkit_chat_container(win);
1215
aviau6aeb4852016-08-18 16:01:09 -04001216 handle_account_migrations(win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001217}
1218
1219static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001220ring_main_window_dispose(GObject *object)
1221{
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001222 RingMainWindow *self = RING_MAIN_WINDOW(object);
1223 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1224
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001225 QObject::disconnect(priv->selected_item_changed);
1226 QObject::disconnect(priv->selected_call_over);
aviau8c0b2422016-10-31 16:06:44 -04001227 QObject::disconnect(priv->username_lookup);
aviau039001d2016-09-29 16:39:05 -04001228
1229 g_clear_object(&priv->welcome_view);
1230 g_clear_object(&priv->webkit_chat_container);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001231
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001232 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1233}
1234
1235static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001236ring_main_window_finalize(GObject *object)
1237{
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001238 RingMainWindow *self = RING_MAIN_WINDOW(object);
1239 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1240
1241 g_clear_object(&priv->settings);
1242
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001243 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001244}
1245
1246static void
1247ring_main_window_class_init(RingMainWindowClass *klass)
1248{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001249 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001250 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001251
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001252 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1253 "/cx/ring/RingGnome/ringmainwindow.ui");
1254
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001255 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001256 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001257 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contacts);
1258 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_history);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001259 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1260 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1261 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1262 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001263 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001264 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001265 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001266 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001267 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001268 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001269 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001270 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001271 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001272}
1273
1274GtkWidget *
1275ring_main_window_new (GtkApplication *app)
1276{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001277 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
1278
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001279 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001280}