SmartPanel.xaml.cpp 11.7 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/***************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com>          *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/
#include "pch.h"
20
#include <string> // move it
Nicolas Jager's avatar
Nicolas Jager committed
21
22
23
24
25
#include "SmartPanel.xaml.h"

using namespace Platform;

using namespace RingClientUWP;
26
using namespace RingClientUWP::Controls;
Nicolas Jager's avatar
Nicolas Jager committed
27
28
29
30
31
32
33
34
35
36
37
using namespace RingClientUWP::Views;
using namespace RingClientUWP::ViewModel;
using namespace Windows::Media::Capture;
using namespace Windows::UI::Xaml;
using namespace Windows::Storage;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::UI::Xaml::Shapes;
using namespace Windows::UI::Xaml::Media;
using namespace Concurrency;
using namespace Windows::Foundation;

38
39
40
41
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Storage;
using namespace Windows::UI::Core;

Nicolas Jager's avatar
Nicolas Jager committed
42
43
44
45
SmartPanel::SmartPanel()
{
    InitializeComponent();

46
47
48
49
50
51
    _accountsList_->ItemsSource = AccountsViewModel::instance->accountsList;

    /* populate the smartlist */
    smartPanelItemsList_ = ref new Vector<SmartPanelItem^>();
    _smartList_->ItemsSource = smartPanelItemsList_;

52
    /* connect delegates */
Nicolas Jager's avatar
Nicolas Jager committed
53
54
55
    Configuration::UserPreferences::instance->selectIndex += ref new SelectIndex([this](int index) {
        _accountsList_->SelectedIndex = index;
    });
56
57
58
59
60
61
    Configuration::UserPreferences::instance->loadProfileImage += ref new LoadProfileImage([this]() {
        StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
        String^ image_path = localfolder->Path + "\\.profile\\profile_image.png";
        auto uri = ref new Windows::Foundation::Uri(image_path);
        _selectedAccountAvatar_->ImageSource = ref new BitmapImage(uri);
    });
atraczyk's avatar
atraczyk committed
62
63
64
65
    AccountsViewModel::instance->updateScrollView += ref new UpdateScrollView([this]() {
        _accountsListScrollView_->UpdateLayout();
        _accountsListScrollView_->ScrollToVerticalOffset(_accountsListScrollView_->ScrollableHeight);
    });
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    CallsViewModel::instance->callRecieved += ref new RingClientUWP::CallRecieved([&](
    Call^ call) {
        auto from = call->from;
        auto contact = ContactsViewModel::instance->findContactByName(from);

        if (contact == nullptr)
            contact = ContactsViewModel::instance->addNewContact(from, from); // contact checked inside addNewContact.

        if (contact == nullptr) {
            ERR_("contact not handled!");
            return;
        }

        auto item = findItem(contact);
        item->_call = call;
    });
    RingD::instance->stateChange += ref new StateChange([this](String^ callId, String^ state, int code) {
        auto call = CallsViewModel::instance->findCall(callId);

        if (call == nullptr)
            return;

        auto item = findItem(call);

        if (!item) {
            WNG_("item not found");
            return;
        }

        if (call->state == "incoming call")
            item->_callBar = Windows::UI::Xaml::Visibility::Visible;

        if (call->state == "CURRENT")
            item->_callBar = Windows::UI::Xaml::Visibility::Collapsed;

        if (call->state == "")
            item->_callBar = Windows::UI::Xaml::Visibility::Collapsed;
    });


    ContactsViewModel::instance->contactAdded += ref new ContactAdded([this](Contact^ contact) {
        auto smartPanelItem = ref new SmartPanelItem();
        smartPanelItem->_contact = contact;
        smartPanelItemsList_->Append(smartPanelItem);
    });
Nicolas Jager's avatar
Nicolas Jager committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

}

void
RingClientUWP::Views::SmartPanel::updatePageContent()
{
    auto account = AccountsViewModel::instance->selectedAccount;
    if (!account)
        return;

    Configuration::UserPreferences::instance->PREF_ACCOUNT_INDEX = _accountsList_->SelectedIndex;
    Configuration::UserPreferences::instance->save();
    _selectedAccountName_->Text = account->name_;
}

void RingClientUWP::Views::SmartPanel::_accountsMenuButton__Checked(Object^ sender, RoutedEventArgs^ e)
{
    _shareMenuButton_->IsChecked = false;
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

void RingClientUWP::Views::SmartPanel::_accountsMenuButton__Unchecked(Object^ sender, RoutedEventArgs^ e)
{
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

void RingClientUWP::Views::SmartPanel::_settings__Checked(Object^ sender, RoutedEventArgs^ e)
{
    _smartGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _settings_->Visibility = Windows::UI::Xaml::Visibility::Visible;
}

void RingClientUWP::Views::SmartPanel::_settings__Unchecked(Object^ sender, RoutedEventArgs^ e)
{
    _settings_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _smartGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
}

void RingClientUWP::Views::SmartPanel::setMode(RingClientUWP::Views::SmartPanel::Mode mode)
{
    if (mode == RingClientUWP::Views::SmartPanel::Mode::Normal) {
        _rowRingTxtBx_->Height = 40;
155
        _selectedAccountAvatarContainer_->Height = 80;
Nicolas Jager's avatar
Nicolas Jager committed
156
157
158
159
160
        _selectedAccountAvatarColumn_->Width = 90;
        _selectedAccountRow_->Height = 90;
    }
    else {
        _rowRingTxtBx_->Height = 0;
161
        _selectedAccountAvatarContainer_->Height = 50;
Nicolas Jager's avatar
Nicolas Jager committed
162
163
164
165
        _selectedAccountAvatarColumn_->Width = 60;
        _selectedAccountRow_->Height = 60;
    }

166
    _selectedAccountAvatarContainer_->Width = _selectedAccountAvatarContainer_->Height;
Nicolas Jager's avatar
Nicolas Jager committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    _settingsTBtn_->IsChecked = false;
    _accountsMenuButton_->IsChecked = false;
    _shareMenuButton_->IsChecked = false;
}

void RingClientUWP::Views::SmartPanel::_shareMenuButton__Checked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _shareMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _accountsMenuButton_->IsChecked = false;
}

void RingClientUWP::Views::SmartPanel::_shareMenuButton__Unchecked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _shareMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

void RingClientUWP::Views::SmartPanel::_addAccountBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
}

void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
atraczyk's avatar
atraczyk committed
191
192
193
    switch (_accountTypeComboBox_->SelectedIndex)
    {
    case 0:
194
    {
195
196
197
198
199
        CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
        ref new DispatchedHandler([=]() {
            auto frame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content);
            dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(true, true);
        }));
200
201
202
        RingD::instance->createRINGAccount(_aliasTextBox_->Text);
        _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _accountsMenuButton__Checked(nullptr, nullptr);
atraczyk's avatar
atraczyk committed
203
        break;
204
205
    }
    break;
atraczyk's avatar
atraczyk committed
206
    case 1:
207
208
209
210
211
212
213
214
    {
        RingD::instance->createSIPAccount(_aliasTextBox_->Text);
        _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _accountsMenuButton__Checked(nullptr, nullptr);
        break;
    }
    default:
        break;
atraczyk's avatar
atraczyk committed
215
    }
216
    _aliasTextBox_->Text = "";
Nicolas Jager's avatar
Nicolas Jager committed
217
218
219
220
221
}


void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
atraczyk's avatar
atraczyk committed
222
223
    _accountsMenuButton_->IsChecked = false;
    _accountsMenuButton__Unchecked(nullptr,nullptr);
Nicolas Jager's avatar
Nicolas Jager committed
224
225
226
227
228
229
}

void
SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto listbox = safe_cast<ListBox^>(sender);
230
    auto item = safe_cast<SmartPanelItem^>(listbox->SelectedItem);
231
232
233
234
235
236
237
    if (item != nullptr) {
        auto contact = safe_cast<Contact^>(item->_contact);
        ContactsViewModel::instance->selectedContact = contact;
    }
    else {
        ContactsViewModel::instance->selectedContact = nullptr;
    }
Nicolas Jager's avatar
Nicolas Jager committed
238
239
240
241
242
243
}

void
SmartPanel::_accountList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto listbox = safe_cast<ListBox^>(sender);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    // disable deselction from listbox
    if (listbox->SelectedItem == nullptr)
    {
        if (e->RemovedItems->Size > 0)
        {
            Object^ itemToReselect = e->RemovedItems->GetAt(0);
            for each (auto item in listbox->Items) {
                if (item == itemToReselect) {
                    listbox->SelectedItem = itemToReselect;
                    continue;
                }
            }
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
258
259
260
261
262
263
264
265
266
267
268
269
270
    auto account = safe_cast<Account^>(listbox->SelectedItem);
    AccountsViewModel::instance->selectedAccount = account;
    updatePageContent();
}

void RingClientUWP::Views::SmartPanel::_ringTxtBx__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    /* add contact, test purpose but will be reused later in some way */
    if (e->Key == Windows::System::VirtualKey::Enter && _ringTxtBx_->Text != "") {
        ContactsViewModel::instance->addNewContact(_ringTxtBx_->Text, _ringTxtBx_->Text);
        _ringTxtBx_->Text = "";
    }
}
271
272
273
274
275


void RingClientUWP::Views::SmartPanel::_rejectIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
276
    auto call = dynamic_cast<Call^>(button->DataContext);
277
278
279
280
281
282
283
284

    call->refuse();
}


void RingClientUWP::Views::SmartPanel::_acceptIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
285
    auto call = dynamic_cast<Call^>(button->DataContext);
286
287
288

    call->accept();
}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

SmartPanelItem^
SmartPanel::findItem(Contact^ contact)
{
    for each (SmartPanelItem^ item in smartPanelItemsList_)
        if (item->_contact == contact)
            return item;

    return nullptr;
}


SmartPanelItem^
SmartPanel::findItem(Call^ call)
{
    for each (SmartPanelItem^ item in smartPanelItemsList_)
        if (item->_call == call)
            return item;

    return nullptr;
}