blob: a4570c907d5d7e063a2684a69f3e17cf168d5ab9 [file] [log] [blame]
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001/*
Guillaume Roguez2a6150d2017-07-19 18:24:47 -04002 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05003 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
Guillaume Roguez6d4734e2017-05-26 16:44:45 -04004 * Author: Guillaume Roguew <guillaume.roguez@savoirfairelinux.com>
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050019 */
20
21#include "ringmainwindow.h"
22
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040023// GTK+ related
Stepan Salenikovicha1b8cb32015-09-11 14:58:35 -040024#include <glib/gi18n.h>
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040025
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040026// LRC
Sébastien Blin55bff9d2017-10-03 15:15:23 -040027#include <accountmodel.h> // Old lrc but still used
28#include <api/account.h>
29#include <api/contact.h>
30#include <api/profile.h>
31#include <api/contactmodel.h>
32#include <api/conversation.h>
33#include <api/conversationmodel.h>
34#include <api/lrc.h>
35#include <api/newaccountmodel.h>
36#include <api/newcallmodel.h>
37#include <api/behaviorcontroller.h>
38#include "accountcontainer.h"
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040039
40// Ring client
Stepan Salenikovich5fd97bc2016-08-30 09:40:38 -040041#include "accountview.h"
aviau6aeb4852016-08-18 16:01:09 -040042#include "accountmigrationview.h"
43#include "accountcreationwizard.h"
Stepan Salenikovichc6a3b982016-01-11 18:11:39 -050044#include "chatview.h"
Sébastien Blin55bff9d2017-10-03 15:15:23 -040045#include "conversationsview.h"
46#include "currentcallview.h"
47#include "dialogs.h"
48#include "generalsettingsview.h"
49#include "incomingcallview.h"
50#include "mediasettingsview.h"
51#include "models/gtkqtreemodel.h"
52#include "ringwelcomeview.h"
53#include "utils/accounts.h"
Stepan Salenikovichba87ae22016-09-13 14:27:20 -040054#include "utils/files.h"
Nicolas Jagerb413b302016-05-06 11:41:32 -040055
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -050056//==============================================================================
57
58namespace details
59{
60class CppImpl;
61}
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050062
63struct _RingMainWindow
64{
65 GtkApplicationWindow parent;
66};
67
68struct _RingMainWindowClass
69{
70 GtkApplicationWindowClass parent_class;
71};
72
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -050073struct RingMainWindowPrivate
Stepan Salenikovich434b88f2015-02-19 17:49:08 -050074{
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040075 GtkWidget *ring_menu;
76 GtkWidget *image_ring;
77 GtkWidget *ring_settings;
78 GtkWidget *image_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040079 GtkWidget *hbox_settings;
Sébastien Blin55bff9d2017-10-03 15:15:23 -040080 GtkWidget *notebook_contacts;
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -040081 GtkWidget *scrolled_window_smartview;
Stepan Salenikovich09e0b782016-09-07 16:28:50 -040082 GtkWidget *treeview_conversations;
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -040083 GtkWidget *vbox_left_pane;
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -050084 GtkWidget *search_entry;
Stepan Salenikovich69771842015-02-24 18:11:45 -050085 GtkWidget *stack_main_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040086 GtkWidget *vbox_call_view;
Stepan Salenikovich3034d922015-10-09 10:11:42 -040087 GtkWidget *frame_call;
88 GtkWidget *welcome_view;
Sébastien Blin55bff9d2017-10-03 15:15:23 -040089 GtkWidget *button_new_conversation;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040090 GtkWidget *account_settings_view;
Stepan Salenikovich0bd53492015-05-11 14:28:52 -040091 GtkWidget *media_settings_view;
Stepan Salenikovichde896112015-05-11 16:46:33 -040092 GtkWidget *general_settings_view;
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -040093 GtkWidget *last_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040094 GtkWidget *radiobutton_general_settings;
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -040095 GtkWidget *radiobutton_media_settings;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -040096 GtkWidget *radiobutton_account_settings;
aviau69081842016-10-14 14:51:54 -040097 GtkWidget *account_creation_wizard;
98 GtkWidget *account_migration_view;
Nicolas Jager15a8b902017-03-21 07:53:06 -040099 GtkWidget *combobox_account_selector;
Nicolas Jager0efc8432017-03-22 16:22:00 -0400100 GtkWidget *treeview_contact_requests;
101 GtkWidget *scrolled_window_contact_requests;
Guillaume Roguez0a411202017-05-25 15:27:26 -0400102 GtkWidget *image_contact_requests_list;
Guillaume Roguez6f431372017-12-12 21:15:39 -0500103 GtkWidget *webkit_chat_container; ///< The webkit_chat_container is created once, then reused for all chat views
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400104
105 GSettings *settings;
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400106
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500107 details::CppImpl* cpp; ///< Non-UI and C++ only code
Stepan Salenikovich434b88f2015-02-19 17:49:08 -0500108};
109
110G_DEFINE_TYPE_WITH_PRIVATE(RingMainWindow, ring_main_window, GTK_TYPE_APPLICATION_WINDOW);
111
112#define RING_MAIN_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_MAIN_WINDOW_TYPE, RingMainWindowPrivate))
113
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500114//==============================================================================
115
116inline namespace details
117{
118
119static constexpr const char* CALL_VIEW_NAME = "calls";
120static constexpr const char* ACCOUNT_CREATION_WIZARD_VIEW_NAME = "account-creation-wizard";
121static constexpr const char* ACCOUNT_MIGRATION_VIEW_NAME = "account-migration-view";
122static constexpr const char* GENERAL_SETTINGS_VIEW_NAME = "general";
123static constexpr const char* MEDIA_SETTINGS_VIEW_NAME = "media";
124static constexpr const char* ACCOUNT_SETTINGS_VIEW_NAME = "accounts";
125
126class CppImpl
127{
128public:
129 explicit CppImpl(RingMainWindow& widget);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500130 ~CppImpl();
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500131
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500132 void init();
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500133 void updateLrc(const std::string& accountId);
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500134 void changeView(GType type, lrc::api::conversation::Info conversation = {});
Guillaume Roguez11339f82017-12-12 23:15:34 -0500135 WebKitChatContainer* webkitChatContainer() const;
Guillaume Roguez1d963272017-12-12 23:32:13 -0500136 void enterFullScreen();
137 void leaveFullScreen();
138 void toggleFullScreen();
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500139 void resetToWelcome();
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500140 void setPendingContactRequestTabIcon();
141 void showAccountSelectorWidget(bool show = true);
142 void useAccount(const std::string& accountId);
143 void onAccountChanged(const std::string& accountId);
144 void enterAccountCreationWizard();
145 void leaveAccountCreationWizard();
146 void enterSettingsView();
147 void leaveSettingsView();
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500148
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500149 RingMainWindow* self = nullptr; // The GTK widget itself
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500150 RingMainWindowPrivate* widgets = nullptr;
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500151
Guillaume Roguez6f431372017-12-12 21:15:39 -0500152 std::unique_ptr<lrc::api::Lrc> lrc_;
153 AccountContainer* accountContainer_ = nullptr;
154 lrc::api::conversation::Info* chatViewConversation_ = nullptr;
155 lrc::api::profile::Type currentTypeFilter_;
156 bool show_settings = false;
157 bool is_fullscreen = false;
158
159 QMetaObject::Connection selected_item_changed;
160 QMetaObject::Connection selected_call_over;
161 QMetaObject::Connection showChatViewConnection_;
162 QMetaObject::Connection showCallViewConnection_;
163 QMetaObject::Connection showIncomingViewConnection_;
164 QMetaObject::Connection changeAccountConnection_;
165 QMetaObject::Connection newAccountConnection_;
166 QMetaObject::Connection rmAccountConnection_;
167 QMetaObject::Connection historyClearedConnection_;
168 QMetaObject::Connection modelSortedConnection_;
169 QMetaObject::Connection filterChangedConnection_;
170 QMetaObject::Connection newConversationConnection_;
171 QMetaObject::Connection conversationRemovedConnection_;
172 QMetaObject::Connection accountStatusChangedConnection_;
173
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500174private:
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500175 GtkWidget* displayIncomingView(lrc::api::conversation::Info);
176 GtkWidget* displayCurrentCallView(lrc::api::conversation::Info);
177 GtkWidget* displayChatView(lrc::api::conversation::Info);
178
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500179 CppImpl() = delete;
180 CppImpl(const CppImpl&) = delete;
181 CppImpl& operator=(const CppImpl&) = delete;
182};
183
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500184inline namespace gtk_callbacks
185{
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500186
Stepan Salenikovich7c71bfe2015-05-13 18:08:09 -0400187static gboolean
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500188on_save_accounts_timeout(GtkWidget* working_dialog)
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400189{
190 /* save changes to accounts */
Guillaume Roguez5d1514b2015-10-22 15:55:31 -0400191 AccountModel::instance().save();
Stepan Salenikovichbd029582015-03-24 11:00:56 -0400192
193 if (working_dialog)
194 gtk_widget_destroy(working_dialog);
195
196 return G_SOURCE_REMOVE;
197}
198
Stepan Salenikovich15142182015-03-11 17:15:26 -0400199static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500200on_video_double_clicked(RingMainWindow* self)
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400201{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500202 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
203 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
204 priv->cpp->toggleFullScreen();
205}
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400206
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500207static void
208on_hide_view_clicked(RingMainWindow* self)
209{
210 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
211 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
212 priv->cpp->resetToWelcome();
213}
Stepan Salenikovichbd4b3772015-12-13 23:28:06 -0500214
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500215static void
216on_account_creation_completed(RingMainWindow* self)
217{
218 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
219 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
220 priv->cpp->leaveAccountCreationWizard();
221}
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400222
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500223static void
224on_account_changed(RingMainWindow* self)
225{
226 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
227 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400228
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500229 auto accountComboBox = GTK_COMBO_BOX(priv->combobox_account_selector);
230 auto model = gtk_combo_box_get_model(accountComboBox);
Stepan Salenikovich41118912015-05-01 11:25:46 -0400231
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500232 GtkTreeIter iter;
233 if (gtk_combo_box_get_active_iter(accountComboBox, &iter)) {
234 gchar* accountId;
235 gtk_tree_model_get(model, &iter, 0 /* col# */, &accountId /* data */, -1);
236 priv->cpp->onAccountChanged(accountId);
237 g_free(accountId);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400238 }
239}
240
241static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500242on_settings_clicked(RingMainWindow* self)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400243{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500244 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
245 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
246
247 if (!priv->cpp->show_settings)
248 priv->cpp->enterSettingsView();
249 else
250 priv->cpp->leaveSettingsView();
251}
252
253static void
254on_show_media_settings(GtkToggleButton* navbutton, RingMainWindow* self)
255{
256 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
257 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400258
259 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400260 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), TRUE);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -0400261 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), MEDIA_SETTINGS_VIEW_NAME);
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400262 priv->last_settings_view = priv->media_settings_view;
Stepan Salenikovich41118912015-05-01 11:25:46 -0400263 } else {
Stepan Salenikovich0bd53492015-05-11 14:28:52 -0400264 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(priv->media_settings_view), FALSE);
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400265 }
266}
267
268static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500269on_show_account_settings(GtkToggleButton* navbutton, RingMainWindow* self)
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400270{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500271 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
272 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400273
274 if (gtk_toggle_button_get_active(navbutton)) {
Stepan Salenikovichf903d1b2015-03-25 14:51:45 -0400275 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), ACCOUNT_SETTINGS_VIEW_NAME);
276 priv->last_settings_view = priv->account_settings_view;
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -0400277 }
278}
279
Stepan Salenikovichde896112015-05-11 16:46:33 -0400280static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500281on_show_general_settings(GtkToggleButton* navbutton, RingMainWindow* self)
Stepan Salenikovichde896112015-05-11 16:46:33 -0400282{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500283 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
284 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovichde896112015-05-11 16:46:33 -0400285
286 if (gtk_toggle_button_get_active(navbutton)) {
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400287 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), TRUE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400288 gtk_stack_set_visible_child_name(GTK_STACK(priv->stack_main_view), GENERAL_SETTINGS_VIEW_NAME);
289 priv->last_settings_view = priv->general_settings_view;
Nicolas Jager6f5e04a2016-05-18 15:04:59 -0400290 } else {
291 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(priv->general_settings_view), FALSE);
Stepan Salenikovichde896112015-05-11 16:46:33 -0400292 }
293}
294
Stepan Salenikovichb8e41272015-03-27 14:31:54 -0400295static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500296on_search_entry_text_changed(GtkSearchEntry* search_entry, RingMainWindow* self)
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400297{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500298 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
299 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400300
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400301 // Filter model
302 const gchar *text = gtk_entry_get_text(GTK_ENTRY(search_entry));
Guillaume Roguez6f431372017-12-12 21:15:39 -0500303 priv->cpp->accountContainer_->info.conversationModel->setFilter(text);
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400304}
305
306static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500307on_search_entry_activated(RingMainWindow* self)
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400308{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500309 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
310 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
311
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400312 // Select the first conversation of the list
Guillaume Roguez6f431372017-12-12 21:15:39 -0500313 auto& conversationModel = priv->cpp->accountContainer_->info.conversationModel;
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400314 auto conversations = conversationModel->allFilteredConversations();
315 if (!conversations.empty())
316 conversationModel->selectConversation(conversations[0].uid);
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400317}
318
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400319static gboolean
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500320on_search_entry_key_released(G_GNUC_UNUSED GtkEntry* search_entry, GdkEventKey* key, RingMainWindow* self)
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400321{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500322 g_return_val_if_fail(IS_RING_MAIN_WINDOW(self), GDK_EVENT_PROPAGATE);
323 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400324
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400325 // if esc key pressed, clear the regex (keep the text, the user might not want to actually delete it)
326 if (key->keyval == GDK_KEY_Escape) {
Guillaume Roguez6f431372017-12-12 21:15:39 -0500327 priv->cpp->accountContainer_->info.conversationModel->setFilter("");
Stepan Salenikovichcac35042016-10-28 12:07:38 -0400328 return GDK_EVENT_STOP;
329 }
330
331 return GDK_EVENT_PROPAGATE;
332}
333
334static gboolean
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500335on_dtmf_pressed(RingMainWindow* self, GdkEventKey* event, gpointer user_data)
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400336{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500337 (void)user_data;
338
339 g_return_val_if_fail(IS_RING_MAIN_WINDOW(self), GDK_EVENT_PROPAGATE);
340 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
341
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400342 g_return_val_if_fail(event->type == GDK_KEY_PRESS, GDK_EVENT_PROPAGATE);
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400343 g_return_val_if_fail(priv, GDK_EVENT_PROPAGATE);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400344
345 /* we want to react to digit key presses, as long as a GtkEntry is not the
346 * input focus
347 */
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500348 GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(self));
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400349 if (GTK_IS_ENTRY(focus))
350 return GDK_EVENT_PROPAGATE;
351
Guillaume Roguez6f431372017-12-12 21:15:39 -0500352 if (priv->cpp->accountContainer_ &&
353 priv->cpp->accountContainer_->info.profileInfo.type != lrc::api::profile::Type::SIP)
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400354 return GDK_EVENT_PROPAGATE;
355
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400356 /* filter out cretain MOD masked key presses so that, for example, 'Ctrl+c'
357 * does not result in a 'c' being played.
358 * we filter Ctrl, Alt, and SUPER/HYPER/META keys */
359 if ( event->state
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400360 & ( GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ))
361 return GDK_EVENT_PROPAGATE;
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400362
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400363 // Get current conversation
364 auto current_view = gtk_bin_get_child(GTK_BIN(priv->frame_call));
365 lrc::api::conversation::Info current_item;
366 if (IS_CURRENT_CALL_VIEW(current_view))
367 current_item = current_call_view_get_conversation(CURRENT_CALL_VIEW(current_view));
368 else
369 return GDK_EVENT_PROPAGATE;
370
371 if (current_item.callId.empty())
372 return GDK_EVENT_PROPAGATE;
373
374 // pass the character that was entered to be played by the daemon;
375 // the daemon will filter out invalid DTMF characters
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400376 guint32 unicode_val = gdk_keyval_to_unicode(event->keyval);
377 QString val = QString::fromUcs4(&unicode_val, 1);
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400378 g_debug("attemptingto play DTMF tone during ongoing call: %s", val.toUtf8().constData());
Guillaume Roguez6f431372017-12-12 21:15:39 -0500379 priv->cpp->accountContainer_->info.callModel->playDTMF(current_item.callId, val.toStdString());
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400380 // always propogate the key, so we don't steal accelerators/shortcuts
Stepan Salenikovich447f4a72015-04-29 11:09:28 -0400381 return GDK_EVENT_PROPAGATE;
Stepan Salenikovichb01d7362015-04-27 23:02:00 -0400382}
383
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400384static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500385on_tab_changed(GtkNotebook* notebook, GtkWidget* page, guint page_num, RingMainWindow* self)
Stepan Salenikovich09e0b782016-09-07 16:28:50 -0400386{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500387 (void)notebook;
388 (void)page;
389
390 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
391 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
392
Guillaume Roguez6f431372017-12-12 21:15:39 -0500393 auto newType = page_num == 0 ? priv->cpp->accountContainer_->info.profileInfo.type : lrc::api::profile::Type::PENDING;
394 if (priv->cpp->currentTypeFilter_ != newType) {
395 priv->cpp->currentTypeFilter_ = newType;
396 priv->cpp->accountContainer_->info.conversationModel->setFilter(priv->cpp->currentTypeFilter_);
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400397 }
Nicolas Jager0efc8432017-03-22 16:22:00 -0400398}
399
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400400static gboolean
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500401on_window_size_changed(GtkWidget* self, GdkEventConfigure* event, gpointer user_data)
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400402{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500403 (void)user_data;
404
405 g_return_val_if_fail(IS_RING_MAIN_WINDOW(self), GDK_EVENT_PROPAGATE);
406 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikovichba87ae22016-09-13 14:27:20 -0400407
408 g_settings_set_int(priv->settings, "window-width", event->width);
409 g_settings_set_int(priv->settings, "window-height", event->height);
410
411 return GDK_EVENT_PROPAGATE;
412}
413
Stepan Salenikovicheafc3dd2015-04-16 18:38:01 -0400414static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500415on_search_entry_places_call_changed(GSettings* settings, const gchar* key, RingMainWindow* self)
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400416{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500417 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
418 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400419
420 if (g_settings_get_boolean(settings, key)) {
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500421 gtk_widget_set_tooltip_text(priv->button_new_conversation,
422 C_("button next to search entry will place a new call",
423 "place call"));
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400424 } else {
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500425 gtk_widget_set_tooltip_text(priv->button_new_conversation,
426 C_("button next to search entry will open chat", "open chat"));
Stepan Salenikoviche9933242016-06-21 18:08:48 -0400427 }
428}
429
430static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500431on_handle_account_migrations(RingMainWindow* self)
aviau6aeb4852016-08-18 16:01:09 -0400432{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500433 g_return_if_fail(IS_RING_MAIN_WINDOW(self));
434 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(RING_MAIN_WINDOW(self));
aviau6aeb4852016-08-18 16:01:09 -0400435
436 /* If there is an existing migration view, remove it */
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500437 if (priv->account_migration_view) {
Stepan Salenikovich56530b12016-10-20 17:51:34 -0400438 gtk_widget_destroy(priv->account_migration_view);
439 priv->account_migration_view = nullptr;
aviau6aeb4852016-08-18 16:01:09 -0400440 }
441
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500442 auto accounts = AccountModel::instance().accountsToMigrate();
443 if (!accounts.isEmpty()) {
444 auto* account = accounts.first();
aviau6aeb4852016-08-18 16:01:09 -0400445
aviau69081842016-10-14 14:51:54 -0400446 priv->account_migration_view = account_migration_view_new(account);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500447 g_signal_connect_swapped(priv->account_migration_view, "account-migration-completed",
448 G_CALLBACK(on_handle_account_migrations), self);
449 g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed",
450 G_CALLBACK(on_handle_account_migrations), self);
aviau6aeb4852016-08-18 16:01:09 -0400451
452 gtk_widget_hide(priv->ring_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500453 priv->cpp->showAccountSelectorWidget(false);
aviau69081842016-10-14 14:51:54 -0400454 gtk_widget_show(priv->account_migration_view);
aviau6aeb4852016-08-18 16:01:09 -0400455 gtk_stack_add_named(
456 GTK_STACK(priv->stack_main_view),
aviau69081842016-10-14 14:51:54 -0400457 priv->account_migration_view,
aviau6aeb4852016-08-18 16:01:09 -0400458 ACCOUNT_MIGRATION_VIEW_NAME
459 );
460 gtk_stack_set_visible_child_name(
461 GTK_STACK(priv->stack_main_view),
462 ACCOUNT_MIGRATION_VIEW_NAME
463 );
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500464 } else {
aviau6aeb4852016-08-18 16:01:09 -0400465 gtk_widget_show(priv->ring_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500466 priv->cpp->showAccountSelectorWidget();
aviau6aeb4852016-08-18 16:01:09 -0400467 }
468}
469
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500470} // namespace gtk_callbacks
471
Nicolas Jager5e2be732017-03-18 21:35:50 -0400472/**
473 * set the column value by printing the alias and the state of an account in combobox_account_selector.
474 */
475static void
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500476print_account_and_state(GtkCellLayout* cell_layout,
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400477 GtkCellRenderer* cell,
478 GtkTreeModel* model,
Nicolas Jager5e2be732017-03-18 21:35:50 -0400479 GtkTreeIter* iter,
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500480 gpointer* data)
Nicolas Jager5e2be732017-03-18 21:35:50 -0400481{
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500482 (void)cell_layout;
483 (void)data;
484
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400485 gchar *alias;
486 gchar *text;
Nicolas Jager5e2be732017-03-18 21:35:50 -0400487
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400488 gtk_tree_model_get (model, iter,
489 1 /* col# */, &alias /* data */,
490 -1);
Nicolas Jager5e2be732017-03-18 21:35:50 -0400491
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400492 text = g_markup_printf_escaped(
493 "<span fgcolor=\"gray\">%s</span>",
494 alias
495 );
Nicolas Jager5e2be732017-03-18 21:35:50 -0400496
Sébastien Blin55bff9d2017-10-03 15:15:23 -0400497 g_object_set(G_OBJECT(cell), "markup", text, NULL);
498 g_free(alias);
Nicolas Jager5e2be732017-03-18 21:35:50 -0400499}
500
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500501CppImpl::CppImpl(RingMainWindow& widget)
502 : self {&widget}
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500503 , widgets {RING_MAIN_WINDOW_GET_PRIVATE(&widget)}
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500504 , lrc_ {std::make_unique<lrc::api::Lrc>()}
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -0500505{}
506
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500507void
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500508CppImpl::init()
509{
510 // NOTE: When new models will be fully implemented, we need to move this
511 // in rign_client.cpp->
512 // Init LRC and the vew
513 const auto accountIds = lrc_->getAccountModel().getAccountList();
514 auto isInitialized = false;
515 if (not accountIds.empty()) {
516 for (const auto& accountId : accountIds) {
517 const auto& accountInfo = lrc_->getAccountModel().getAccountInfo(accountId);
518 if (accountInfo.enabled) {
519 updateLrc(accountId);
520 isInitialized = true;
521 break;
522 }
523 }
524 if (!isInitialized) {
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500525 widgets->treeview_conversations = conversations_view_new(accountContainer_);
526 gtk_container_add(GTK_CONTAINER(widgets->scrolled_window_smartview), widgets->treeview_conversations);
527 widgets->treeview_contact_requests = conversations_view_new(accountContainer_);
528 gtk_container_add(GTK_CONTAINER(widgets->scrolled_window_contact_requests), widgets->treeview_contact_requests);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500529 }
530 }
531
532 // Account status changed
533 auto slotAccountStatusChanged = [this] (const std::string& accountId) {
534 if (not accountContainer_ ) {
535 updateLrc(accountId);
536
537 if (accountContainer_)
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500538 ring_welcome_update_view(RING_WELCOME_VIEW(widgets->welcome_view),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500539 accountContainer_);
540 } else {
541 auto accounts = lrc_->getAccountModel().getAccountList();
542 auto store = gtk_list_store_new (2 /* # of cols */ ,
543 G_TYPE_STRING,
544 G_TYPE_STRING,
545 G_TYPE_UINT);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500546 auto currentIdx = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->combobox_account_selector));
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500547 GtkTreeIter iter;
548 auto enabledAccounts = 0;
549 for (const auto& account : accounts) {
550 const auto& accountInfo = lrc_->getAccountModel().getAccountInfo(account);
551 showAccountSelectorWidget();
552 if (accountId == account && !accountInfo.enabled) currentIdx = 0;
553 if (accountInfo.enabled) {
554 ++enabledAccounts;
555 gtk_list_store_append (store, &iter);
556 gtk_list_store_set (store, &iter,
557 0 /* col # */ , account.c_str() /* celldata */,
558 1 /* col # */ , accountInfo.profileInfo.alias.c_str() /* celldata */,
559 -1 /* end */);
560 }
561 }
562 // Redraw combobox
563 gtk_combo_box_set_model(
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500564 GTK_COMBO_BOX(widgets->combobox_account_selector),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500565 GTK_TREE_MODEL (store)
566 );
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500567 gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_account_selector), currentIdx);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500568 // if no account. reset the view.
569 if (enabledAccounts == 0) {
570 updateLrc("");
571 changeView(RING_WELCOME_VIEW_TYPE);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500572 ring_welcome_update_view(RING_WELCOME_VIEW(widgets->welcome_view), nullptr);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500573 }
574 }
575 };
576
577 accountStatusChangedConnection_ = QObject::connect(&lrc_->getAccountModel(),
578 &lrc::api::NewAccountModel::accountStatusChanged,
579 slotAccountStatusChanged);
580
581 // This signal must be connected if no account
582 auto slotAccountAdded = [this] (const std::string& idAdded) {
583 Q_UNUSED(idAdded)
584 // New account added. go to this account
585 auto accounts = lrc_->getAccountModel().getAccountList();
586 auto store = gtk_list_store_new (2 /* # of cols */ ,
587 G_TYPE_STRING,
588 G_TYPE_STRING,
589 G_TYPE_UINT);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500590 auto currentIdx = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->combobox_account_selector));
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500591 GtkTreeIter iter;
592 for (const auto& accountId : accounts) {
593 const auto& account = lrc_->getAccountModel().getAccountInfo(accountId);
594 if (account.enabled) {
595 gtk_list_store_append (store, &iter);
596 gtk_list_store_set (store, &iter,
597 0 /* col # */ , accountId.c_str() /* celldata */,
598 1 /* col # */ , account.profileInfo.alias.c_str() /* celldata */,
599 -1 /* end */);
600 }
601 }
602 // Redraw combobox
603 gtk_combo_box_set_model(
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500604 GTK_COMBO_BOX(widgets->combobox_account_selector),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500605 GTK_TREE_MODEL (store)
606 );
607 // If no account selected, select the new account
608 if (currentIdx == -1) currentIdx = 0;
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500609 gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_account_selector), currentIdx);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500610 showAccountSelectorWidget();
611 };
612
613 newAccountConnection_ = QObject::connect(&lrc_->getAccountModel(),
614 &lrc::api::NewAccountModel::accountAdded,
615 slotAccountAdded);
616
617 auto slotAccountRemoved = [this] (G_GNUC_UNUSED const std::string& idRemoved) {
618 // Account removed, change account if it's the current account selected.
619 auto accounts = lrc_->getAccountModel().getAccountList();
620 if (accounts.empty()) {
621 enterAccountCreationWizard();
622 } else {
623 auto store = gtk_list_store_new (2 /* # of cols */ ,
624 G_TYPE_STRING,
625 G_TYPE_STRING,
626 G_TYPE_UINT);
627 GtkTreeIter iter;
628 for (const auto& accountId : accounts) {
629 const auto& account = lrc_->getAccountModel().getAccountInfo(accountId);
630 if (account.enabled) {
631 gtk_list_store_append (store, &iter);
632 gtk_list_store_set (store, &iter,
633 0 /* col # */ , accountId.c_str() /* celldata */,
634 1 /* col # */ , account.profileInfo.alias.c_str() /* celldata */,
635 -1 /* end */);
636 }
637 }
638
639 gtk_combo_box_set_model(
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500640 GTK_COMBO_BOX(widgets->combobox_account_selector),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500641 GTK_TREE_MODEL (store)
642 );
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500643 gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_account_selector), 0);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500644 showAccountSelectorWidget();
645 // Show conversation panel
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500646 gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->notebook_contacts), 0);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500647 // Reinit LRC
648 updateLrc(std::string(accounts.at(0)));
649 // Update the welcome view
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500650 ring_welcome_update_view(RING_WELCOME_VIEW(widgets->welcome_view), accountContainer_);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500651 }
652 };
653
654 rmAccountConnection_ = QObject::connect(&lrc_->getAccountModel(),
655 &lrc::api::NewAccountModel::accountRemoved,
656 slotAccountRemoved);
657
658 /* bind to window size settings */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500659 widgets->settings = g_settings_new_full(get_ring_schema(), nullptr, nullptr);
660 auto width = g_settings_get_int(widgets->settings, "window-width");
661 auto height = g_settings_get_int(widgets->settings, "window-height");
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500662 gtk_window_set_default_size(GTK_WINDOW(self), width, height);
663 g_signal_connect(self, "configure-event", G_CALLBACK(on_window_size_changed), nullptr);
664
665 /* search-entry-places-call setting */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500666 on_search_entry_places_call_changed(widgets->settings, "search-entry-places-call", self);
667 g_signal_connect(widgets->settings, "changed::search-entry-places-call", G_CALLBACK(on_search_entry_places_call_changed), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500668
669 /* set window icon */
670 GError *error = NULL;
671 GdkPixbuf* icon = gdk_pixbuf_new_from_resource("/cx/ring/RingGnome/ring-symbol-blue", &error);
672 if (icon == NULL) {
673 g_debug("Could not load icon: %s", error->message);
674 g_clear_error(&error);
675 } else
676 gtk_window_set_icon(GTK_WINDOW(self), icon);
677
678 /* set menu icon */
679 GdkPixbuf* image_ring = gdk_pixbuf_new_from_resource_at_scale("/cx/ring/RingGnome/ring-symbol-blue",
680 -1, 16, TRUE, &error);
681 if (image_ring == NULL) {
682 g_debug("Could not load icon: %s", error->message);
683 g_clear_error(&error);
684 } else
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500685 gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->image_ring), image_ring);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500686
687 /* ring menu */
688 GtkBuilder *builder = gtk_builder_new_from_resource("/cx/ring/RingGnome/ringgearsmenu.ui");
689 GMenuModel *menu = G_MENU_MODEL(gtk_builder_get_object(builder, "menu"));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500690 gtk_menu_button_set_menu_model(GTK_MENU_BUTTON(widgets->ring_menu), menu);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500691 g_object_unref(builder);
692
693 /* settings icon */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500694 gtk_image_set_from_icon_name(GTK_IMAGE(widgets->image_settings), "emblem-system-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500695
696 /* connect settings button signal */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500697 g_signal_connect_swapped(widgets->ring_settings, "clicked", G_CALLBACK(on_settings_clicked), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500698
699 /* add the call view to the main stack */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500700 gtk_stack_add_named(GTK_STACK(widgets->stack_main_view),
701 widgets->vbox_call_view,
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500702 CALL_VIEW_NAME);
703
704 /* init the settings views */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500705 widgets->account_settings_view = account_view_new();
706 gtk_stack_add_named(GTK_STACK(widgets->stack_main_view), widgets->account_settings_view, ACCOUNT_SETTINGS_VIEW_NAME);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500707
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500708 widgets->media_settings_view = media_settings_view_new();
709 gtk_stack_add_named(GTK_STACK(widgets->stack_main_view), widgets->media_settings_view, MEDIA_SETTINGS_VIEW_NAME);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500710
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500711 widgets->general_settings_view = general_settings_view_new();
712 gtk_stack_add_named(GTK_STACK(widgets->stack_main_view), widgets->general_settings_view, GENERAL_SETTINGS_VIEW_NAME);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500713
714 /* make the setting we will show first the active one */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500715 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->radiobutton_general_settings), TRUE);
716 widgets->last_settings_view = widgets->general_settings_view;
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500717
718 /* connect the settings button signals to switch settings views */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500719 g_signal_connect(widgets->radiobutton_media_settings, "toggled", G_CALLBACK(on_show_media_settings), self);
720 g_signal_connect(widgets->radiobutton_account_settings, "toggled", G_CALLBACK(on_show_account_settings), self);
721 g_signal_connect(widgets->radiobutton_general_settings, "toggled", G_CALLBACK(on_show_general_settings), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500722
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500723 g_signal_connect(widgets->notebook_contacts, "switch-page", G_CALLBACK(on_tab_changed), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500724
725 /* welcome/default view */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500726 widgets->welcome_view = ring_welcome_view_new(accountContainer_);
727 g_object_ref(widgets->welcome_view); // increase ref because don't want it to be destroyed when not displayed
728 gtk_container_add(GTK_CONTAINER(widgets->frame_call), widgets->welcome_view);
729 gtk_widget_show(widgets->welcome_view);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500730
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500731 g_signal_connect_swapped(widgets->search_entry, "activate", G_CALLBACK(on_search_entry_activated), self);
732 g_signal_connect_swapped(widgets->button_new_conversation, "clicked", G_CALLBACK(on_search_entry_activated), self);
733 g_signal_connect(widgets->search_entry, "search-changed", G_CALLBACK(on_search_entry_text_changed), self);
734 g_signal_connect(widgets->search_entry, "key-release-event", G_CALLBACK(on_search_entry_key_released), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500735
736 auto provider = gtk_css_provider_new();
737 gtk_css_provider_load_from_data(provider,
738 ".search-entry-style { border: 0; border-radius: 0; } \
739 .spinner-style { border: 0; background: white; } \
740 .new-conversation-style { border: 0; background: white; transition: all 0.3s ease; border-radius: 0; } \
741 .new-conversation-style:hover { background: #bae5f0; }",
742 -1, nullptr
743 );
744 gtk_style_context_add_provider_for_screen(gdk_display_get_default_screen(gdk_display_get_default()),
745 GTK_STYLE_PROVIDER(provider),
746 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
747
748
749 /* react to digit key press events */
750 g_signal_connect(self, "key-press-event", G_CALLBACK(on_dtmf_pressed), nullptr);
751
752 /* set the search entry placeholder text */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500753 gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->search_entry),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500754 C_("Please try to make the translation 50 chars or less so that it fits into the layout", "Search contacts or enter number"));
755
756 /* init chat webkit container so that it starts loading before the first time we need it*/
757 (void)webkitChatContainer();
758
759 if (has_ring_account()) {
760 /* user has ring account, so show the call view right away */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500761 gtk_stack_set_visible_child(GTK_STACK(widgets->stack_main_view), widgets->vbox_call_view);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500762
763 on_handle_account_migrations(self);
764 } else {
765 /* user has to create the ring account */
766 enterAccountCreationWizard();
767 }
768
769 // setup account selector and select the first account
770 auto store = gtk_list_store_new (2 /* # of cols */ ,
771 G_TYPE_STRING,
772 G_TYPE_STRING,
773 G_TYPE_UINT);
774 GtkTreeIter iter;
775 for (const auto& accountId : accountIds) {
776 const auto& accountInfo = lrc_->getAccountModel().getAccountInfo(accountId);
777 if (accountInfo.enabled) {
778 gtk_list_store_append (store, &iter);
779 gtk_list_store_set (store, &iter,
780 0 /* col # */ , accountId.c_str() /* celldata */,
781 1 /* col # */ , accountInfo.profileInfo.alias.c_str() /* celldata */,
782 -1 /* end */);
783 }
784 }
785
786 gtk_combo_box_set_model(
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500787 GTK_COMBO_BOX(widgets->combobox_account_selector),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500788 GTK_TREE_MODEL (store)
789 );
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500790 gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_account_selector), 0);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500791
792 /* set visibility */
793 showAccountSelectorWidget();
794
795 /* layout */
796 auto *renderer = gtk_cell_renderer_text_new();
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500797 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widgets->combobox_account_selector), renderer, FALSE);
798 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(widgets->combobox_account_selector), renderer, "text", 0, NULL);
799 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(widgets->combobox_account_selector),
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500800 renderer,
801 (GtkCellLayoutDataFunc)print_account_and_state,
802 nullptr, nullptr);
803
804 // we closing any view opened to avoid confusion (especially between SIP and Ring protocols).
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500805 g_signal_connect_swapped(widgets->combobox_account_selector, "changed", G_CALLBACK(on_account_changed), self);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500806
807 // initialize the pending contact request icon.
808 setPendingContactRequestTabIcon();
809
810 if (isInitialized) {
811 auto& conversationModel = accountContainer_->info.conversationModel;
812 auto conversations = conversationModel->allFilteredConversations();
813 for (const auto& conversation: conversations) {
814 if (!conversation.callId.empty()) {
815 accountContainer_->info.conversationModel->selectConversation(conversation.uid);
816 }
817 }
818 }
819}
820
821CppImpl::~CppImpl()
822{
823 delete accountContainer_;
824 delete chatViewConversation_;
825
826 QObject::disconnect(selected_item_changed);
827 QObject::disconnect(selected_call_over);
828 QObject::disconnect(showChatViewConnection_);
829 QObject::disconnect(showIncomingViewConnection_);
830 QObject::disconnect(historyClearedConnection_);
831 QObject::disconnect(modelSortedConnection_);
832 QObject::disconnect(filterChangedConnection_);
833 QObject::disconnect(newConversationConnection_);
834 QObject::disconnect(conversationRemovedConnection_);
835 QObject::disconnect(changeAccountConnection_);
836 QObject::disconnect(newAccountConnection_);
837 QObject::disconnect(rmAccountConnection_);
838 QObject::disconnect(showCallViewConnection_);
839 QObject::disconnect(modelSortedConnection_);
840 QObject::disconnect(accountStatusChangedConnection_);
841
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500842 g_clear_object(&widgets->welcome_view);
843 g_clear_object(&widgets->webkit_chat_container);
Guillaume Roguez47cd8b52017-12-13 14:06:01 -0500844}
845
846void
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500847CppImpl::changeView(GType type, lrc::api::conversation::Info conversation)
848{
Guillaume Roguez1d963272017-12-12 23:32:13 -0500849 leaveFullScreen();
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500850 gtk_container_remove(GTK_CONTAINER(widgets->frame_call),
851 gtk_bin_get_child(GTK_BIN(widgets->frame_call)));
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500852
853 QObject::disconnect(selected_item_changed);
854 QObject::disconnect(selected_call_over);
855
856 GtkWidget* new_view;
857 if (g_type_is_a(INCOMING_CALL_VIEW_TYPE, type)) {
858 new_view = displayIncomingView(conversation);
859 } else if (g_type_is_a(CURRENT_CALL_VIEW_TYPE, type)) {
860 new_view = displayCurrentCallView(conversation);
861 } else if (g_type_is_a(CHAT_VIEW_TYPE, type)) {
862 new_view = displayChatView(conversation);
863 } else {
864 // TODO select first conversation?
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500865 new_view = widgets->welcome_view;
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500866 }
867
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500868 gtk_container_add(GTK_CONTAINER(widgets->frame_call), new_view);
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500869 gtk_widget_show(new_view);
870}
871
872GtkWidget*
873CppImpl::displayIncomingView(lrc::api::conversation::Info conversation)
874{
875 delete chatViewConversation_;
876 chatViewConversation_ = new lrc::api::conversation::Info(conversation);
Guillaume Roguez11339f82017-12-12 23:15:34 -0500877 return incoming_call_view_new(webkitChatContainer(), accountContainer_, chatViewConversation_);
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500878}
879
880GtkWidget*
881CppImpl::displayCurrentCallView(lrc::api::conversation::Info conversation)
882{
883 delete chatViewConversation_;
884 chatViewConversation_ = new lrc::api::conversation::Info(conversation);
Guillaume Roguez11339f82017-12-12 23:15:34 -0500885 auto* new_view = current_call_view_new(webkitChatContainer(),
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500886 accountContainer_, chatViewConversation_);
887
888 try {
889 auto contactUri = chatViewConversation_->participants.front();
890 auto contactInfo = accountContainer_->info.contactModel->getContact(contactUri);
891 if (auto chat_view = current_call_view_get_chat_view(CURRENT_CALL_VIEW(new_view))) {
892 chat_view_update_temporary(CHAT_VIEW(chat_view),
893 contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING
894 || contactInfo.profileInfo.type == lrc::api::profile::Type::TEMPORARY);
895 }
896 } catch(...) { }
897
Guillaume Roguez1d963272017-12-12 23:32:13 -0500898 g_signal_connect_swapped(new_view, "video-double-clicked",
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500899 G_CALLBACK(on_video_double_clicked), self);
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500900 return new_view;
901}
902
903GtkWidget*
904CppImpl::displayChatView(lrc::api::conversation::Info conversation)
905{
906 delete chatViewConversation_;
907 chatViewConversation_ = new lrc::api::conversation::Info(conversation);
Guillaume Roguez11339f82017-12-12 23:15:34 -0500908 auto* new_view = chat_view_new(webkitChatContainer(), accountContainer_, chatViewConversation_);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500909 g_signal_connect_swapped(new_view, "hide-view-clicked", G_CALLBACK(on_hide_view_clicked), self);
Guillaume Roguez527dc6b2017-12-12 22:58:51 -0500910 return new_view;
911}
912
Guillaume Roguez11339f82017-12-12 23:15:34 -0500913WebKitChatContainer*
914CppImpl::webkitChatContainer() const
915{
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500916 if (!widgets->webkit_chat_container) {
917 widgets->webkit_chat_container = webkit_chat_container_new();
Guillaume Roguez11339f82017-12-12 23:15:34 -0500918
919 // We don't want it to be deleted, ever.
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500920 g_object_ref(widgets->webkit_chat_container);
Guillaume Roguez11339f82017-12-12 23:15:34 -0500921 }
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500922 return WEBKIT_CHAT_CONTAINER(widgets->webkit_chat_container);
Guillaume Roguez11339f82017-12-12 23:15:34 -0500923}
924
Guillaume Roguez1d963272017-12-12 23:32:13 -0500925void
926CppImpl::enterFullScreen()
927{
928 if (!is_fullscreen) {
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500929 gtk_widget_hide(widgets->vbox_left_pane);
Guillaume Roguez1d963272017-12-12 23:32:13 -0500930 gtk_window_fullscreen(GTK_WINDOW(self));
931 is_fullscreen = true;
932 }
933}
934
935void
936CppImpl::leaveFullScreen()
937{
938 if (is_fullscreen) {
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500939 gtk_widget_show(widgets->vbox_left_pane);
Guillaume Roguez1d963272017-12-12 23:32:13 -0500940 gtk_window_unfullscreen(GTK_WINDOW(self));
941 is_fullscreen = false;
942 }
943}
944
945void
946CppImpl::toggleFullScreen()
947{
948 if (is_fullscreen)
949 leaveFullScreen();
950 else
951 enterFullScreen();
952}
953
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500954/// Clear selection and go to welcome page.
955void
956CppImpl::resetToWelcome()
957{
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500958 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->treeview_conversations));
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500959 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500960 auto selection_contact_request = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->treeview_contact_requests));
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500961 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_contact_request));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500962 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguez6ee8df62017-12-12 23:54:18 -0500963 lrc::api::conversation::Info current_item;
964 if (IS_CHAT_VIEW(old_view))
965 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
966 changeView(RING_WELCOME_VIEW_TYPE, current_item);
967}
968
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500969void
970CppImpl::setPendingContactRequestTabIcon()
971{
972 if (not accountContainer_)
973 return;
974
975 auto isRingAccount = accountContainer_->info.profileInfo.type == lrc::api::profile::Type::RING;
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500976 gtk_widget_set_visible(widgets->scrolled_window_contact_requests, isRingAccount);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500977
978 if (not isRingAccount)
979 return;
980
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500981 gtk_image_set_from_resource(GTK_IMAGE(widgets->image_contact_requests_list),
Guillaume Roguezdbefafd2017-12-12 23:54:49 -0500982 (accountContainer_->info.contactModel->hasPendingRequests())
983 ? "/cx/ring/RingGnome/contact_requests_list_with_notification"
984 : "/cx/ring/RingGnome/contact_requests_list");
985}
986
987void
988CppImpl::showAccountSelectorWidget(bool show)
989{
990 // we only want to show the account selector when there is more than 1 enabled
991 // account; so every time we want to show it, we should preform this check
992 auto activatedAccount = 0;
993 auto accounts = lrc_->getAccountModel().getAccountList();
994 for (const auto& account : accounts) {
995 const auto& accountInfo = lrc_->getAccountModel().getAccountInfo(account);
996 if (accountInfo.enabled)
997 ++activatedAccount;
998 }
Guillaume Roguez3ea37de2017-12-13 14:13:54 -0500999 gtk_widget_set_visible(widgets->combobox_account_selector, show && activatedAccount > 1);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001000}
1001
1002void
1003CppImpl::enterAccountCreationWizard()
1004{
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001005 if (!widgets->account_creation_wizard) {
1006 widgets->account_creation_wizard = account_creation_wizard_new(false);
1007 g_object_add_weak_pointer(G_OBJECT(widgets->account_creation_wizard),
1008 reinterpret_cast<gpointer*>(&widgets->account_creation_wizard));
1009 g_signal_connect_swapped(widgets->account_creation_wizard, "account-creation-completed",
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001010 G_CALLBACK(on_account_creation_completed), self);
1011
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001012 gtk_stack_add_named(GTK_STACK(widgets->stack_main_view),
1013 widgets->account_creation_wizard,
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001014 ACCOUNT_CREATION_WIZARD_VIEW_NAME);
1015 }
1016
1017 /* hide settings button until account creation is complete */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001018 gtk_widget_hide(widgets->ring_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001019 showAccountSelectorWidget(false);
1020
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001021 gtk_widget_show(widgets->account_creation_wizard);
1022 gtk_stack_set_visible_child(GTK_STACK(widgets->stack_main_view), widgets->account_creation_wizard);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001023}
1024
1025void
1026CppImpl::leaveAccountCreationWizard()
1027{
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001028 gtk_stack_set_visible_child_name(GTK_STACK(widgets->stack_main_view), CALL_VIEW_NAME);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001029
1030 /* destroy the wizard */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001031 if (widgets->account_creation_wizard) {
1032 gtk_container_remove(GTK_CONTAINER(widgets->stack_main_view), widgets->account_creation_wizard);
1033 gtk_widget_destroy(widgets->account_creation_wizard);
1034 widgets->account_creation_wizard = nullptr;
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001035 }
1036
1037 /* show the settings button */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001038 gtk_widget_show(widgets->ring_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001039
1040 /* show the account selector */
1041 showAccountSelectorWidget();
1042}
1043
1044void
1045CppImpl::useAccount(const std::string& accountId)
1046{
1047 // Go to welcome view
1048 changeView(RING_WELCOME_VIEW_TYPE);
1049
1050 // Change account selection widget
1051 auto accounts = lrc_->getAccountModel().getAccountList();
1052 auto it = std::find(accounts.begin(), accounts.end(), accountId );
1053 if (it == accounts.end()) {
1054 qDebug() << "useAccount, cannot find account " << accountId.c_str();
1055 return;
1056 }
1057
1058 int row = it - accounts.begin();
1059
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001060 gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->combobox_account_selector), row);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001061 // Show conversation panel
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001062 gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->notebook_contacts), 0);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001063 // Reinit LRC
1064 updateLrc(std::string(accountId));
1065 // Update the welcome view
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001066 ring_welcome_update_view(RING_WELCOME_VIEW(widgets->welcome_view), accountContainer_);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001067}
1068
1069void
1070CppImpl::onAccountChanged(const std::string& accountId)
1071{
1072 // Reinit view
1073 changeView(RING_WELCOME_VIEW_TYPE);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001074 gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->notebook_contacts), 0);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001075
1076 // Change account
1077 updateLrc(accountId);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001078 ring_welcome_update_view(RING_WELCOME_VIEW(widgets->welcome_view), accountContainer_);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001079}
1080
1081void
1082CppImpl::enterSettingsView()
1083{
1084 /* show the settings */
1085 show_settings = true;
1086
1087 /* show settings */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001088 gtk_image_set_from_icon_name(GTK_IMAGE(widgets->image_settings), "emblem-ok-symbolic",
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001089 GTK_ICON_SIZE_SMALL_TOOLBAR);
1090
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001091 gtk_widget_show(widgets->hbox_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001092
1093 /* make sure to start preview if we're showing the video settings */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001094 if (widgets->last_settings_view == widgets->media_settings_view)
1095 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(widgets->media_settings_view), TRUE);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001096
1097 /* make sure to show the profile if we're showing the general settings */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001098 if (widgets->last_settings_view == widgets->general_settings_view)
1099 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(widgets->general_settings_view),
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001100 TRUE);
1101
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001102 gtk_stack_set_visible_child(GTK_STACK(widgets->stack_main_view), widgets->last_settings_view);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001103}
1104
1105void
1106CppImpl::leaveSettingsView()
1107{
1108 /* hide the settings */
1109 show_settings = false;
1110
1111 /* show working dialog in case save operation takes time */
1112 auto working = ring_dialog_working(GTK_WIDGET(self), nullptr);
1113 gtk_window_present(GTK_WINDOW(working));
1114
1115 /* now save after the time it takes to transition back to the call view (400ms)
1116 * the save doesn't happen before the "working" dialog is presented
1117 * the timeout function should destroy the "working" dialog when done saving
1118 */
1119 g_timeout_add_full(G_PRIORITY_DEFAULT, 400, GSourceFunc(on_save_accounts_timeout), working,
1120 nullptr);
1121
1122 /* show calls */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001123 gtk_image_set_from_icon_name(GTK_IMAGE(widgets->image_settings), "emblem-system-symbolic",
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001124 GTK_ICON_SIZE_SMALL_TOOLBAR);
1125
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001126 gtk_widget_hide(widgets->hbox_settings);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001127
1128 /* make sure video preview is stopped, in case it was started */
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001129 media_settings_view_show_preview(MEDIA_SETTINGS_VIEW(widgets->media_settings_view), FALSE);
1130 general_settings_view_show_profile(GENERAL_SETTINGS_VIEW(widgets->general_settings_view),
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001131 FALSE);
1132
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001133 gtk_stack_set_visible_child_name(GTK_STACK(widgets->stack_main_view), CALL_VIEW_NAME);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001134
1135 /* show the view which was selected previously */
1136}
1137
1138void
1139CppImpl::updateLrc(const std::string& accountId)
1140{
1141 // Disconnect old signals.
1142 QObject::disconnect(showChatViewConnection_);
1143 QObject::disconnect(showIncomingViewConnection_);
1144 QObject::disconnect(changeAccountConnection_);
1145 QObject::disconnect(showCallViewConnection_);
1146 QObject::disconnect(modelSortedConnection_);
1147 QObject::disconnect(historyClearedConnection_);
1148 QObject::disconnect(filterChangedConnection_);
1149 QObject::disconnect(newConversationConnection_);
1150 QObject::disconnect(conversationRemovedConnection_);
1151
1152 // Get the account selected
1153 if (accountContainer_) delete accountContainer_;
1154 if (!accountId.empty())
1155 accountContainer_ = new AccountContainer(lrc_->getAccountModel().getAccountInfo(accountId));
1156 else
1157 accountContainer_ = nullptr;
1158
1159 // Reinit tree views
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001160 if (widgets->treeview_conversations) {
1161 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->treeview_conversations));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001162 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001163 gtk_widget_destroy(widgets->treeview_conversations);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001164 }
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001165 widgets->treeview_conversations = conversations_view_new(accountContainer_);
1166 gtk_container_add(GTK_CONTAINER(widgets->scrolled_window_smartview), widgets->treeview_conversations);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001167
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001168 if (widgets->treeview_contact_requests) {
1169 auto selection_conversations = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->treeview_contact_requests));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001170 gtk_tree_selection_unselect_all(GTK_TREE_SELECTION(selection_conversations));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001171 gtk_widget_destroy(widgets->treeview_contact_requests);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001172 }
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001173 widgets->treeview_contact_requests = conversations_view_new(accountContainer_);
1174 gtk_container_add(GTK_CONTAINER(widgets->scrolled_window_contact_requests), widgets->treeview_contact_requests);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001175
1176 if (!accountContainer_) return;
1177
1178 // define slots (we are using this way because gtk is not a c++ lib)
1179 auto slotConversationCleared = [this] (const std::string& uid) {
1180 // Change the view when the history is cleared.
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001181 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001182 lrc::api::conversation::Info current_item;
1183 current_item.uid = "-1"; // Because the Searching item has an empty uid
1184 if (IS_CHAT_VIEW(old_view))
1185 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
1186 else
1187 // if incoming or call view we don't want to change the view
1188 return;
1189 if (current_item.uid == uid) {
1190 // We are on the conversation cleared.
1191 // Go to welcome view because user doesn't want this conversation
1192 // TODO go to first conversation?
1193 resetToWelcome();
1194 }
1195 };
1196
1197 auto slotModelSorted = [this] () {
1198 // Synchronize selection when sorted and update pending icon
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001199 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001200 lrc::api::conversation::Info current_item;
1201 current_item.uid = "-1";
1202 if (IS_CHAT_VIEW(old_view))
1203 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
1204 else if (IS_CURRENT_CALL_VIEW(old_view))
1205 current_item = current_call_view_get_conversation(CURRENT_CALL_VIEW(old_view));
1206 else if (IS_INCOMING_CALL_VIEW(old_view))
1207 current_item = incoming_call_view_get_conversation(INCOMING_CALL_VIEW(old_view));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001208 conversations_view_select_conversation(CONVERSATIONS_VIEW(widgets->treeview_conversations), current_item.uid);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001209 setPendingContactRequestTabIcon();
1210 };
1211
1212 auto slotFilterChanged = [this] () {
1213 // Synchronize selection when filter changes
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001214 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001215 lrc::api::conversation::Info current_item;
1216 current_item.uid = "-1";
1217 if (IS_CHAT_VIEW(old_view))
1218 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
1219 else if (IS_CURRENT_CALL_VIEW(old_view))
1220 current_item = current_call_view_get_conversation(CURRENT_CALL_VIEW(old_view));
1221 else if (IS_INCOMING_CALL_VIEW(old_view))
1222 current_item = incoming_call_view_get_conversation(INCOMING_CALL_VIEW(old_view));
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001223 conversations_view_select_conversation(CONVERSATIONS_VIEW(widgets->treeview_conversations), current_item.uid);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001224 // Get if conversation still exists.
1225 auto& conversationModel = accountContainer_->info.conversationModel;
1226 auto conversations = conversationModel->allFilteredConversations();
1227 auto isInConv = std::find_if(
1228 conversations.begin(), conversations.end(),
1229 [current_item](const lrc::api::conversation::Info& conversation) {
1230 return current_item.uid == conversation.uid;
1231 });
1232 if (IS_CHAT_VIEW(old_view) && isInConv == conversations.end()) {
1233 changeView(RING_WELCOME_VIEW_TYPE);
1234 }
1235 };
1236
1237 auto slotNewConversation = [this] (const std::string& uid) {
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001238 gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->notebook_contacts), 0);
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001239 accountContainer_->info.conversationModel->setFilter(lrc::api::profile::Type::RING);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001240 gtk_entry_set_text(GTK_ENTRY(widgets->search_entry), "");
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001241 accountContainer_->info.conversationModel->setFilter("");
1242 // Select new conversation if contact added
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001243 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001244 if (IS_RING_WELCOME_VIEW(old_view) || (IS_CHAT_VIEW(old_view) && chat_view_get_temporary(CHAT_VIEW(old_view)))) {
1245 accountContainer_->info.conversationModel->selectConversation(uid);
1246 try {
1247 auto contactUri = chatViewConversation_->participants.front();
1248 auto contactInfo = accountContainer_->info.contactModel->getContact(contactUri);
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001249 chat_view_update_temporary(CHAT_VIEW(gtk_bin_get_child(GTK_BIN(widgets->frame_call))),
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001250 contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING
1251 || contactInfo.profileInfo.type == lrc::api::profile::Type::TEMPORARY);
1252 } catch(...) { }
1253 }
1254 };
1255
1256 auto slotConversationRemoved = [this] (const std::string& uid) {
1257 // If contact is removed, go to welcome view
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001258 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001259 lrc::api::conversation::Info current_item;
1260 if (IS_CHAT_VIEW(old_view))
1261 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
1262 else if (IS_CURRENT_CALL_VIEW(old_view))
1263 current_item = current_call_view_get_conversation(CURRENT_CALL_VIEW(old_view));
1264 else if (IS_INCOMING_CALL_VIEW(old_view))
1265 current_item = incoming_call_view_get_conversation(INCOMING_CALL_VIEW(old_view));
1266 if (current_item.uid == uid)
1267 changeView(RING_WELCOME_VIEW_TYPE);
1268 };
1269
1270 auto slotShowChatView = [this] (const std::string& accountId, lrc::api::conversation::Info origin) {
1271 if (accountId != accountContainer_->info.id)
1272 useAccount(accountId);
1273 // Show chat view if not in call (unless if it's the same conversation)
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001274 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001275 lrc::api::conversation::Info current_item;
1276 current_item.uid = "-1";
1277 if (IS_CHAT_VIEW(old_view))
1278 current_item = chat_view_get_conversation(CHAT_VIEW(old_view));
1279 if (current_item.uid != origin.uid) {
1280 if (origin.participants.empty()) return;
1281 auto firstContactUri = origin.participants.front();
1282 auto contactInfo = accountContainer_->info.contactModel->getContact(firstContactUri);
1283 if (contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING && current_item.uid != "-1") return;
1284 changeView(CHAT_VIEW_TYPE, origin);
1285 }
1286 };
1287
1288 auto slotShowCallView = [this] (const std::string& accountId, lrc::api::conversation::Info origin) {
1289 if (accountId != accountContainer_->info.id)
1290 useAccount(accountId);
1291 // Change the view if we want a different view.
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001292 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001293
1294 lrc::api::conversation::Info current_item;
1295 if (IS_CURRENT_CALL_VIEW(old_view))
1296 current_item = current_call_view_get_conversation(CURRENT_CALL_VIEW(old_view));
1297
1298 if (current_item.uid != origin.uid)
1299 changeView(CURRENT_CALL_VIEW_TYPE, origin);
1300 };
1301
1302 auto slotShowIncomingCallView = [this] (const std::string& accountId, lrc::api::conversation::Info origin) {
1303 if (accountId != accountContainer_->info.id)
1304 useAccount(accountId);
1305 // Change the view if we want a different view.
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001306 auto* old_view = gtk_bin_get_child(GTK_BIN(widgets->frame_call));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001307
1308 lrc::api::conversation::Info current_item;
1309 if (IS_INCOMING_CALL_VIEW(old_view))
1310 current_item = incoming_call_view_get_conversation(INCOMING_CALL_VIEW(old_view));
1311
1312 if (current_item.uid != origin.uid)
1313 changeView(INCOMING_CALL_VIEW_TYPE, origin);
1314 };
1315
1316 // Connect to signals from LRC
1317 historyClearedConnection_ = QObject::connect(&*accountContainer_->info.conversationModel,
1318 &lrc::api::ConversationModel::conversationCleared,
1319 slotConversationCleared);
1320
1321 modelSortedConnection_ = QObject::connect(&*accountContainer_->info.conversationModel,
1322 &lrc::api::ConversationModel::modelSorted,
1323 slotModelSorted);
1324
1325 filterChangedConnection_ = QObject::connect(&*accountContainer_->info.conversationModel,
1326 &lrc::api::ConversationModel::filterChanged,
1327 slotFilterChanged);
1328
1329 newConversationConnection_ = QObject::connect(&*accountContainer_->info.conversationModel,
1330 &lrc::api::ConversationModel::newConversation,
1331 slotNewConversation);
1332
1333 conversationRemovedConnection_ = QObject::connect(&*accountContainer_->info.conversationModel,
1334 &lrc::api::ConversationModel::conversationRemoved,
1335 slotConversationRemoved);
1336
1337 showChatViewConnection_ = QObject::connect(&lrc_->getBehaviorController(),
1338 &lrc::api::BehaviorController::showChatView,
1339 slotShowChatView);
1340
1341 showCallViewConnection_ = QObject::connect(&lrc_->getBehaviorController(),
1342 &lrc::api::BehaviorController::showCallView,
1343 slotShowCallView);
1344
1345 showIncomingViewConnection_ = QObject::connect(&lrc_->getBehaviorController(),
1346 &lrc::api::BehaviorController::showIncomingCallView,
1347 slotShowIncomingCallView);
1348
Guillaume Roguez3ea37de2017-12-13 14:13:54 -05001349 const gchar *text = gtk_entry_get_text(GTK_ENTRY(widgets->search_entry));
Guillaume Roguezdbefafd2017-12-12 23:54:49 -05001350 currentTypeFilter_ = accountContainer_->info.profileInfo.type;
1351 accountContainer_->info.conversationModel->setFilter(text);
1352 accountContainer_->info.conversationModel->setFilter(currentTypeFilter_);
1353}
1354
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -05001355} // namespace details
1356
1357//==============================================================================
Guillaume Roguez0a411202017-05-25 15:27:26 -04001358
1359static void
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001360ring_main_window_init(RingMainWindow *win)
1361{
Guillaume Roguez6f431372017-12-12 21:15:39 -05001362 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001363 gtk_widget_init_template(GTK_WIDGET(win));
1364
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -05001365 // CppImpl ctor
1366 priv->cpp = new details::CppImpl {*win};
Guillaume Roguez47cd8b52017-12-13 14:06:01 -05001367 priv->cpp->init();
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001368}
1369
1370static void
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001371ring_main_window_dispose(GObject *object)
1372{
Guillaume Roguez6f431372017-12-12 21:15:39 -05001373 auto* self = RING_MAIN_WINDOW(object);
1374 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001375
Guillaume Roguez1e50f0b2017-12-12 20:58:30 -05001376 delete priv->cpp;
1377 priv->cpp = nullptr;
1378
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001379 G_OBJECT_CLASS(ring_main_window_parent_class)->dispose(object);
1380}
1381
1382static void
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001383ring_main_window_finalize(GObject *object)
1384{
Guillaume Roguez6f431372017-12-12 21:15:39 -05001385 auto* self = RING_MAIN_WINDOW(object);
1386 auto* priv = RING_MAIN_WINDOW_GET_PRIVATE(self);
Stepan Salenikovichba87ae22016-09-13 14:27:20 -04001387
1388 g_clear_object(&priv->settings);
1389
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001390 G_OBJECT_CLASS(ring_main_window_parent_class)->finalize(object);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001391}
1392
1393static void
1394ring_main_window_class_init(RingMainWindowClass *klass)
1395{
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001396 G_OBJECT_CLASS(klass)->finalize = ring_main_window_finalize;
Stepan Salenikovich6f687072015-03-26 10:43:37 -04001397 G_OBJECT_CLASS(klass)->dispose = ring_main_window_dispose;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -05001398
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001399 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
1400 "/cx/ring/RingGnome/ringmainwindow.ui");
1401
Stepan Salenikovichbbb10d82015-05-13 12:26:44 -04001402 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_left_pane);
Sébastien Blin55bff9d2017-10-03 15:15:23 -04001403 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, notebook_contacts);
Stepan Salenikovich2f8b4492015-09-21 17:10:36 -04001404 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_smartview);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001405 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_menu);
1406 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_ring);
1407 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, ring_settings);
1408 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001409 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, hbox_settings);
Stepan Salenikovich45d25aa2015-02-20 17:39:09 -05001410 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, search_entry);
Stepan Salenikovich69771842015-02-24 18:11:45 -05001411 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, stack_main_view);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001412 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, vbox_call_view);
Stepan Salenikovich3034d922015-10-09 10:11:42 -04001413 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, frame_call);
Stepan Salenikoviche9933242016-06-21 18:08:48 -04001414 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, button_new_conversation );
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001415 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_general_settings);
Stepan Salenikovich5d3506e2015-03-30 11:01:29 -04001416 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
Stepan Salenikovich2d63d5e2015-03-22 23:23:54 -04001417 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
Nicolas Jager15a8b902017-03-21 07:53:06 -04001418 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, combobox_account_selector);
Nicolas Jager0efc8432017-03-22 16:22:00 -04001419 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, scrolled_window_contact_requests);
Guillaume Roguez0a411202017-05-25 15:27:26 -04001420 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, image_contact_requests_list);
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001421}
1422
1423GtkWidget *
Guillaume Roguez47cd8b52017-12-13 14:06:01 -05001424ring_main_window_new(GtkApplication *app)
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001425{
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001426 gpointer win = g_object_new(RING_MAIN_WINDOW_TYPE, "application", app, NULL);
Stepan Salenikovich61cbab02015-03-16 18:35:10 -04001427 return (GtkWidget *)win;
Stepan Salenikovich434b88f2015-02-19 17:49:08 -05001428}