blob: ab4d23956d3abdeedd9a13c2e2f0c0341f9dcc67 [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:
593 case NameDirectory::LookupStatus::NOT_FOUND:
594 {
595 auto dialog = gtk_message_dialog_new(
596 GTK_WINDOW(self),
597 GTK_DIALOG_DESTROY_WITH_PARENT,
598 GTK_MESSAGE_ERROR,
599 GTK_BUTTONS_CLOSE,
600 _("Could not resolve Ring username: %s"),
601 name.toUtf8().constData()
602 );
603
604 gtk_dialog_run(GTK_DIALOG (dialog));
605 gtk_widget_destroy(dialog);
606 break;
607 }
608 }
609 }
610 );
611
Stepan Salenikovichefd8cae2016-11-10 13:41:30 -0500612 NameDirectory::instance().lookupName(ring_account, QString(), username_to_lookup);
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400613 }
aviau8c0b2422016-10-31 16:06:44 -0400614 else
615 {
616 process_search_entry_contact_method(self, uri);
617 }
Stepan Salenikovich2e6ab562015-05-07 12:33:07 -0400618 }
Stepan Salenikovich297b5d12015-02-26 17:51:13 -0500619}
620
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400621static gboolean
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400622save_accounts(GtkWidget *working_dialog)
623{
624 /* save changes to accounts */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400625 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400626
627 if (working_dialog)
628 gtk_widget_destroy(working_dialog);
629
630 return G_SOURCE_REMOVE;
631}
632
Stepan Salenikovich15142182015-03-11 17:15:26 -0400633static void
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400634settings_clicked(G_GNUC_UNUSED GtkButton *button, RingMainWindow *win)
635{
636 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
637 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
638
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400639 /* check which view to show */
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500640 if (!priv->show_settings) {
641 /* show the settings */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400642 priv->show_settings = TRUE;
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500643
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500644 /* 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 -0400645 selection_changed(win);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500646
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400647 /* show settings */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400648 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 -0400649
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400650 gtk_widget_show(priv->hbox_settings);
651
Stepan Salenikovich41118912015-05-01 11:25:46 -0400652 /* make sure to start preview if we're showing the video settings */
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400653 if (priv->last_settings_view == priv->media_settings_view)
654 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400655
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400656 /* make sure to show the profile if we're showing the general settings */
657 if (priv->last_settings_view == priv->general_settings_view)
658 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
659
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400660 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_UP);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400661 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->last_settings_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400662 } else {
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500663 /* hide the settings */
664 priv->show_settings = FALSE;
665
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400666 /* show working dialog in case save operation takes time */
667 GtkWidget *working = ring_dialog_working(GTK_WIDGET(win), NULL);
668 gtk_window_present(GTK_WINDOW(working));
669
670 /* now save after the time it takes to transition back to the call view (400ms)
671 * the save doesn't happen before the "working" dialog is presented
672 * the timeout function should destroy the "working" dialog when done saving
673 */
Stepan Salenikovich12fee942015-03-25 18:38:47 -0400674 g_timeout_add_full(G_PRIORITY_DEFAULT, 400, (GSourceFunc)save_accounts, working, NULL);
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400675
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400676 /* show calls */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -0400677 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 -0400678
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400679 gtk_widget_hide(priv->hbox_settings);
680
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500681 /* make sure video preview is stopped, in case it was started */
682 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400683 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500684
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400685 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_DOWN);
686 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400687
Stepan Salenikovichc1323422016-01-06 10:54:44 -0500688 /* show the view which was selected previously */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400689 selection_changed(win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400690 }
691}
692
693static void
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400694show_media_settings(GtkToggleButton *navbutton, RingMainWindow *win)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400695{
696 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
697 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
698
699 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400700 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400701 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400702 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400703 priv->last_settings_view = priv->media_settings_view;
Stepan Salenikovich41118912015-05-01 11:25:46 -0400704 } else {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400705 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400706 }
707}
708
709static void
710show_account_settings(GtkToggleButton *navbutton, RingMainWindow *win)
711{
712 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
713 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
714
715 if (gtk_toggle_button_get_active(navbutton)) {
716 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
717 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), ACCOUNT_SETTINGS_VIEW_NAME);
718 priv->last_settings_view = priv->account_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400719 }
720}
721
Stepan Salenikovichde896112015-05-11 16:46:33 -0400722static void
723show_general_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)) {
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400729 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400730 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_RIGHT);
731 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
732 priv->last_settings_view = priv->general_settings_view;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400733 } else {
734 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400735 }
736}
737
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400738static void
aviau6aeb4852016-08-18 16:01:09 -0400739on_account_creation_completed(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400740{
Stepan Salenikovichbabe26f2016-02-12 16:47:07 -0500741 g_return_if_fail(IS_RING_MAIN_WINDOW(win));
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400742 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
743
aviau6aeb4852016-08-18 16:01:09 -0400744 gtk_stack_set_transition_type(GTK_STACK(priv->stack_main_view), GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT);
745 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), CALL_VIEW_NAME);
746
747 /* destroy the wizard */
aviau69081842016-10-14 14:51:54 -0400748 if (priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400749 {
aviau69081842016-10-14 14:51:54 -0400750 gtk_container_remove(GTK_CONTAINER(priv->stack_main_view), priv->account_creation_wizard);
751 gtk_widget_destroy(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400752 }
aviau6aeb4852016-08-18 16:01:09 -0400753
754 /* show the settings button*/
755 gtk_widget_show(priv->ring_settings);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400756}
757
758static void
aviau6aeb4852016-08-18 16:01:09 -0400759show_account_creation_wizard(RingMainWindow *win)
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400760{
761 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
762
aviau61d05b32016-10-18 16:47:53 -0400763 if (!priv->account_creation_wizard)
aviau6aeb4852016-08-18 16:01:09 -0400764 {
aviau69081842016-10-14 14:51:54 -0400765 priv->account_creation_wizard = account_creation_wizard_new(false);
766 g_object_add_weak_pointer(G_OBJECT(priv->account_creation_wizard), (gpointer *)&priv->account_creation_wizard);
767 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 -0400768
aviau6aeb4852016-08-18 16:01:09 -0400769 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400770 priv->account_creation_wizard,
aviau6aeb4852016-08-18 16:01:09 -0400771 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
772 }
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400773
Stepan Salenikovich64505672015-09-24 10:46:07 -0400774 /* hide settings button until account creation is complete */
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400775 gtk_widget_hide(priv->ring_settings);
776
aviau69081842016-10-14 14:51:54 -0400777 gtk_widget_show(priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400778
aviau69081842016-10-14 14:51:54 -0400779 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->account_creation_wizard);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400780}
781
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400782static void
Stepan Salenikovicha06ce192016-11-01 14:26:43 -0400783search_entry_text_changed(GtkSearchEntry *search_entry, RingMainWindow *self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400784{
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400785 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
786
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400787 /* get the text from the entry */
788 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
789
Stepan Salenikovichf53128f2016-10-07 10:32:16 -0400790 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -0400791 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), text);
792 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), text);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400793}
794
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400795static gboolean
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400796search_entry_key_released(GtkEntry *search_entry, GdkEventKey *key, RingMainWindow *self)
797{
798 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
799
800 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
801 if (key->keyval == GDK_KEY_Escape) {
802 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
803 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts), "");
804 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
805 return GDK_EVENT_STOP;
806 }
807
808 return GDK_EVENT_PROPAGATE;
809}
810
811static gboolean
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400812dtmf_pressed(RingMainWindow *win,
813 GdkEventKey *event,
814 G_GNUC_UNUSED gpointer user_data)
815{
816 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
817
818 /* we want to react to digit key presses, as long as a GtkEntry is not the
819 * input focus
820 */
821 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(win));
822 if (GTK_IS_ENTRY(focus))
823 return GDK_EVENT_PROPAGATE;
824
825 /* make sure that a call is selected*/
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400826 QItemSelectionModel *selection = CallModel::instance().selectionModel();
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400827 QModelIndex idx = selection->currentIndex();
828 if (!idx.isValid())
829 return GDK_EVENT_PROPAGATE;
830
831 /* make sure that the selected call is in progress */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400832 Call *call = CallModel::instance().getCall(idx);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400833 Call::LifeCycleState state = call->lifeCycleState();
834 if (state != Call::LifeCycleState::PROGRESS)
835 return GDK_EVENT_PROPAGATE;
836
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400837 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
838 * does not result in a 'c' being played.
839 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
840 if ( event->state
841 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
842 return GDK_EVENT_PROPAGATE;
843
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400844 /* pass the character that was entered to be played by the daemon;
845 * the daemon will filter out invalid DTMF characters */
846 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
847 QString val = QString::fromUcs4(&unicode_val, 1);
848 call->playDTMF(val);
849 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400850
851 /* always propogate the key, so we don't steal accelerators/shortcuts */
852 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400853}
854
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400855/**
856 * This function determines if two different contact list views (Conversations, Contacts, History)
857 * have the same item selected. Note that the same item does not necessarily mean the same object.
858 * For example, if the history view has a Call selected and the Conversations view has a Person
859 * selected which is associated with the ContactMethod to which that Call was made, then this will
860 * evaluate to TRUE.
861 */
862static gboolean
863compare_treeview_selection(GtkTreeSelection *selection1, GtkTreeSelection *selection2)
864{
865 g_return_val_if_fail(selection1 && selection2, FALSE);
866
867 if (selection1 == selection2) return TRUE;
868
869 auto idx1 = get_index_from_selection(selection1);
870 auto type1 = idx1.data(static_cast<int>(Ring::Role::ObjectType));
871 auto object1 = idx1.data(static_cast<int>(Ring::Role::Object));
872
873 auto idx2 = get_index_from_selection(selection2);
874 auto type2 = idx2.data(static_cast<int>(Ring::Role::ObjectType));
875 auto object2 = idx2.data(static_cast<int>(Ring::Role::Object));
876
877 if (idx1.isValid() && type1.isValid() && object1.isValid() && idx2.isValid() && type2.isValid() && object2.isValid()) {
878 Call *call1 = nullptr;
879 ContactMethod *cm1 = nullptr;
880 Person *person1 = nullptr;
881
882 Call *call2 = nullptr;
883 ContactMethod *cm2 = nullptr;
884 Person *person2 = nullptr;
885
886 switch(type1.value<Ring::ObjectType>()) {
887 case Ring::ObjectType::Person:
888 person1 = object1.value<Person *>();
889 break;
890 case Ring::ObjectType::ContactMethod:
891 cm1 = object1.value<ContactMethod *>();
892 person1 = cm1->contact();
893 break;
894 case Ring::ObjectType::Call:
895 call1 = object1.value<Call *>();
896 cm1 = call1->peerContactMethod();
897 person1 = cm1->contact();
898 break;
899 case Ring::ObjectType::Media:
900 case Ring::ObjectType::Certificate:
901 case Ring::ObjectType::COUNT__:
902 // nothing to do
903 break;
904 }
905
906 switch(type2.value<Ring::ObjectType>()) {
907 case Ring::ObjectType::Person:
908 person2 = object2.value<Person *>();
909 break;
910 case Ring::ObjectType::ContactMethod:
911 cm2 = object2.value<ContactMethod *>();
912 person2 = cm2->contact();
913 break;
914 case Ring::ObjectType::Call:
915 call2 = object2.value<Call *>();
916 cm2 = call2->peerContactMethod();
917 person2 = cm2->contact();
918 break;
919 case Ring::ObjectType::Media:
920 case Ring::ObjectType::Certificate:
921 case Ring::ObjectType::COUNT__:
922 // nothing to do
923 break;
924 }
925
926 if (person1 != nullptr && person1 == person2)
927 return TRUE;
928 if (cm1 != nullptr && cm1 == cm2)
929 return TRUE;
930 if (call1 != nullptr && call1 == call2)
931 return TRUE;
932 }
933
934 return FALSE;
935}
936
937static void
938conversation_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
939{
940 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
941 GtkTreeIter iter;
942 GtkTreeModel *model = nullptr;
943 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
944 /* something is selected in the conversations view, clear the selection in the other views;
945 * unless the current selection is of the same item; we don't try to match the selection in
946 * all views since not all items exist in all 3 contact list views
947 */
948
949 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
950 if (!compare_treeview_selection(selection, selection_contacts))
951 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
952
953 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
954 if (!compare_treeview_selection(selection, selection_history))
955 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
956 }
957
958 selection_changed(self);
959}
960
961static void
962contact_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
963{
964 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
965 GtkTreeIter iter;
966 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
967 /* something is selected in the contacts view, clear the selection in the other views;
968 * unless the current selection is of the same item; we don't try to match the selection in
969 * all views since not all items exist in all 3 contact list views
970 */
971 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
972 if (!compare_treeview_selection(selection, selection_conversations))
973 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
974
975 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
976 if (!compare_treeview_selection(selection, selection_history))
977 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_history));
978 }
979
980 selection_changed(self);
981}
982
983static void
984history_selection_changed(GtkTreeSelection *selection, RingMainWindow *self)
985{
986 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
987 GtkTreeIter iter;
988 if (gtk_tree_selection_get_selected(selection, nullptr, &iter)) {
989 /* something is selected in the history view, clear the selection in the other views;
990 * unless the current selection is of the same item; we don't try to match the selection in
991 * all views since not all items exist in all 3 contact list views
992 */
993
994 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
995 if (!compare_treeview_selection(selection, selection_conversations))
996 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
997
998 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
999 if (!compare_treeview_selection(selection, selection_contacts))
1000 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contacts));
1001 }
1002
1003 selection_changed(self);
1004}
1005
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001006static gboolean
1007window_size_changed(GtkWidget *win, GdkEventConfigure *event, gpointer)
1008{
1009 auto *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1010
1011 g_settings_set_int(priv->settings, "window-width", event->width);
1012 g_settings_set_int(priv->settings, "window-height", event->height);
1013
1014 return GDK_EVENT_PROPAGATE;
1015}
1016
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -04001017static void
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001018search_entry_places_call_changed(GSettings *settings, const gchar *key, RingMainWindow *self)
1019{
1020 auto priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1021
1022 if (g_settings_get_boolean(settings, key)) {
1023 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will place a new call", "place call"));
1024 } else {
1025 gtk_widget_set_tooltip_text(priv->button_new_conversation, C_("button next to search entry will open chat", "open chat"));
1026 }
1027}
1028
1029static void
aviau6aeb4852016-08-18 16:01:09 -04001030handle_account_migrations(RingMainWindow *win)
1031{
1032 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1033
1034 /* If there is an existing migration view, remove it */
aviau69081842016-10-14 14:51:54 -04001035 if (priv->account_migration_view)
aviau6aeb4852016-08-18 16:01:09 -04001036 {
Stepan Salenikovich56530b12016-10-20 17:51:34 -04001037 gtk_widget_destroy(priv->account_migration_view);
1038 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -04001039 }
1040
1041 QList<Account*> accounts = AccountModel::instance().accountsToMigrate();
1042 if (!accounts.isEmpty())
1043 {
1044 Account* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -04001045
aviau69081842016-10-14 14:51:54 -04001046 priv->account_migration_view = account_migration_view_new(account);
aviau69081842016-10-14 14:51:54 -04001047 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed", G_CALLBACK(handle_account_migrations), win);
1048 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
aviau6aeb4852016-08-18 16:01:09 -04001049
1050 gtk_widget_hide(priv->ring_settings);
aviau69081842016-10-14 14:51:54 -04001051 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -04001052 gtk_stack_add_named(
1053 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -04001054 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -04001055 ACCOUNT_MIGRATION_VIEW_NAME
1056 );
1057 gtk_stack_set_visible_child_name(
1058 GTK_STACK(priv->stack_main_view),
1059 ACCOUNT_MIGRATION_VIEW_NAME
1060 );
1061 }
1062 else
1063 {
1064 gtk_widget_show(priv->ring_settings);
1065 }
1066}
1067
1068static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001069ring_main_window_init(RingMainWindow *win)
1070{
1071 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
1072 gtk_widget_init_template(GTK_WIDGET(win));
1073
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001074 /* bind to window size settings */
1075 priv->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
1076 auto width = g_settings_get_int(priv->settings, "window-width");
1077 auto height = g_settings_get_int(priv->settings, "window-height");
1078 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1079 g_signal_connect(win, "configure-event", G_CALLBACK(window_size_changed), nullptr);
1080
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001081 /* search-entry-places-call setting */
1082 search_entry_places_call_changed(priv->settings, "search-entry-places-call", win);
1083 g_signal_connect(priv->settings, "changed::search-entry-places-call", G_CALLBACK(search_entry_places_call_changed), win);
1084
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001085 /* set window icon */
1086 GError *error = NULL;
1087 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
1088 if (icon == NULL) {
1089 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001090 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001091 } else
1092 gtk_window_set_icon(GTK_WINDOW(win), icon);
1093
1094 /* set menu icon */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001095 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001096 -1, 16, TRUE, &error);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001097 if (image_ring == NULL) {
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001098 g_debug("Could not load icon: %s", error->message);
Stepan Salenikovich8a287fc2015-05-01 16:53:20 -04001099 g_clear_error(&error);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001100 } else
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001101 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_ring), image_ring);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001102
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001103 /* ring menu */
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001104 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
1105 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001106 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(priv->ring_menu), menu);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001107 g_object_unref(builder);
Stepan Salenikovicha3557452015-02-20 14:14:12 -05001108
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001109 /* settings icon */
Stepan Salenikovichd61974b2016-03-18 16:36:27 -04001110 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 -04001111
1112 /* connect settings button signal */
1113 g_signal_connect(priv->ring_settings, "clicked", G_CALLBACK(settings_clicked), win);
1114
1115 /* add the call view to the main stack */
1116 gtk_stack_add_named(GTK_STACK(priv->stack_main_view),
1117 priv->vbox_call_view,
1118 CALL_VIEW_NAME);
1119
Stepan Salenikovichb8e41272015-03-27 14:31:54 -04001120 if (has_ring_account()) {
1121 /* user has ring account, so show the call view right away */
1122 gtk_stack_set_visible_child(GTK_STACK(priv->stack_main_view), priv->vbox_call_view);
1123 } else {
1124 /* user has to create the ring account */
aviau6aeb4852016-08-18 16:01:09 -04001125 show_account_creation_wizard(win);
Stepan Salenikovichb8e41272015-03-27 14:31:54 -04001126 }
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001127
1128 /* init the settings views */
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001129 priv->account_settings_view = account_view_new();
1130 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
1131
Stepan Salenikovich0bd53492015-05-11 14:28:52 -04001132 priv->media_settings_view = media_settings_view_new();
1133 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 -04001134
Stepan Salenikovichde896112015-05-11 16:46:33 -04001135 priv->general_settings_view = general_settings_view_new();
1136 gtk_stack_add_named(GTK_STACK(priv->stack_main_view), priv->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
1137
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001138 /* make the setting we will show first the active one */
Stepan Salenikovichde896112015-05-11 16:46:33 -04001139 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->radiobutton_general_settings), TRUE);
1140 priv->last_settings_view = priv->general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001141
1142 /* connect the settings button signals to switch settings views */
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001143 g_signal_connect(priv->radiobutton_media_settings, "toggled", G_CALLBACK(show_media_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001144 g_signal_connect(priv->radiobutton_account_settings, "toggled", G_CALLBACK(show_account_settings), win);
Stepan Salenikovichde896112015-05-11 16:46:33 -04001145 g_signal_connect(priv->radiobutton_general_settings, "toggled", G_CALLBACK(show_general_settings), win);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -04001146
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001147 /* populate the notebook */
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001148 priv->treeview_conversations = recent_contacts_view_new();
1149 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_smartview), priv->treeview_conversations);
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -04001150
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001151 priv->treeview_contacts = contacts_view_new();
1152 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contacts), priv->treeview_contacts);
Stepan Salenikovich15142182015-03-11 17:15:26 -04001153
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001154 priv->treeview_history = history_view_new();
1155 gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001156
Stepan Salenikovich2cde7612015-09-25 10:44:01 -04001157 /* welcome/default view */
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001158 priv->welcome_view = ring_welcome_view_new();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001159 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 -04001160 gtk_container_add(GTK_CONTAINER(priv->frame_call), priv->welcome_view);
1161 gtk_widget_show(priv->welcome_view);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001162
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001163 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_conversations));
1164 g_signal_connect(selection_conversations, "changed", G_CALLBACK(conversation_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001165
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001166 auto selection_contacts = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_contacts));
1167 g_signal_connect(selection_contacts, "changed", G_CALLBACK(contact_selection_changed), win);
1168
1169 auto selection_history = gtk_tree_view_get_selection(GTK_TREE_VIEW(priv->treeview_history));
1170 g_signal_connect(selection_history, "changed", G_CALLBACK(history_selection_changed), win);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001171
Stepan Salenikovichb0ffc342016-10-13 13:10:55 -04001172 g_signal_connect_swapped(priv->button_new_conversation, "clicked", G_CALLBACK(search_entry_activated), win);
1173 g_signal_connect_swapped(priv->search_entry, "activate", G_CALLBACK(search_entry_activated), win);
Stepan Salenikovicha06ce192016-11-01 14:26:43 -04001174 g_signal_connect(priv->search_entry, "search-changed", G_CALLBACK(search_entry_text_changed), win);
Stepan Salenikovichcac35042016-10-28 12:07:38 -04001175 g_signal_connect(priv->search_entry, "key-release-event", G_CALLBACK(search_entry_key_released), win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001176
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001177 /* make sure the incoming call is the selected call in the CallModel */
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001178 QObject::connect(
Guillaume Roguez5d1514b2015-10-22 15:55:31 -04001179 &CallModel::instance(),
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001180 &CallModel::incomingCall,
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001181 [priv](Call* call) {
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001182 // clear the regex to make sure the call is shown
1183 RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp());
Stepan Salenikovichdcfb5032016-10-26 18:57:56 -04001184 contacts_view_set_filter_string(CONTACTS_VIEW(priv->treeview_contacts),"");
1185 history_view_set_filter_string(HISTORY_VIEW(priv->treeview_history), "");
Stepan Salenikovichf53128f2016-10-07 10:32:16 -04001186
1187 // now make sure its selected
1188 RecentModel::instance().selectionModel()->clearCurrentIndex();
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001189 CallModel::instance().selectCall(call);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001190 }
1191 );
Stepan Salenikovich5eab3032015-03-28 10:50:51 -04001192
Stepan Salenikovichb01d7362015-04-27 23:02:00 -04001193 /* react to digit key press events */
1194 g_signal_connect(win, "key-press-event", G_CALLBACK(dtmf_pressed), NULL);
Stepan Salenikovich48b45c12015-09-25 15:49:51 -04001195
1196 /* set the search entry placeholder text */
1197 gtk_entry_set_placeholder_text(GTK_ENTRY(priv->search_entry),
1198 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 -04001199
aviau039001d2016-09-29 16:39:05 -04001200 /* init chat webkit container so that it starts loading before the first time we need it*/
1201 get_webkit_chat_container(win);
1202
aviau6aeb4852016-08-18 16:01:09 -04001203 handle_account_migrations(win);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001204}
1205
1206static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001207ring_main_window_dispose(GObject *object)
1208{
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001209 RingMainWindow *self = RING_MAIN_WINDOW(object);
1210 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1211
Stepan Salenikovich09e0b782016-09-07 16:28:50 -04001212 QObject::disconnect(priv->selected_item_changed);
1213 QObject::disconnect(priv->selected_call_over);
aviau8c0b2422016-10-31 16:06:44 -04001214 QObject::disconnect(priv->username_lookup);
aviau039001d2016-09-29 16:39:05 -04001215
1216 g_clear_object(&priv->welcome_view);
1217 g_clear_object(&priv->webkit_chat_container);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001218
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001219 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1220}
1221
1222static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001223ring_main_window_finalize(GObject *object)
1224{
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001225 RingMainWindow *self = RING_MAIN_WINDOW(object);
1226 RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
1227
1228 g_clear_object(&priv->settings);
1229
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001230 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001231}
1232
1233static void
1234ring_main_window_class_init(RingMainWindowClass *klass)
1235{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001236 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001237 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001238
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001239 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1240 "/cx/ring/RingGnome/ringmainwindow.ui");
1241
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001242 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001243 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovicha7a19bc2015-09-24 16:16:28 -04001244 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contacts);
1245 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_history);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001246 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1247 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1248 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1249 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001250 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001251 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001252 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001253 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001254 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001255 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001256 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001257 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001258 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001259}
1260
1261GtkWidget *
1262ring_main_window_new (GtkApplication *app)
1263{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001264 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
1265
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001266 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001267}