blob: 5094ae7a4557130ffa0263becf207076380bec16 [file] [log] [blame]
Nicolas Jager8a85e1f2016-08-15 15:11:06 -04001/**************************************************************************
2* Copyright (C) 2016 by Savoir-faire Linux *
3* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 3 of the License, or *
8* (at your option) any later version. *
9* *
10* This program is distributed in the hope that it will be useful, *
11* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13* GNU General Public License for more details. *
14* *
15* You should have received a copy of the GNU General Public License *
16* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17**************************************************************************/
18#include "pch.h"
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040019#include "ContactsViewModel.h"
Nicolas Jagerc551c362016-10-01 19:24:50 -040020
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040021#include "MainPage.xaml.h"
22
23#include "MessageTextPage.xaml.h"
24
25using namespace RingClientUWP::Views;
26using namespace RingClientUWP::ViewModel;
27
28using namespace Platform;
29using namespace Windows::Foundation;
30using namespace Windows::Foundation::Collections;
31using namespace Windows::UI::Xaml;
32using namespace Windows::UI::Xaml::Controls;
33using namespace Windows::UI::Xaml::Controls::Primitives;
34using namespace Windows::UI::Xaml::Data;
35using namespace Windows::UI::Xaml::Documents;
36using namespace Windows::UI::Xaml::Input;
37using namespace Windows::UI::Xaml::Media;
38using namespace Windows::UI::Xaml::Navigation;
Nicolas Jager7bef1492016-09-14 11:23:29 -040039using namespace Windows::ApplicationModel::Core;
40using namespace Platform;
41using namespace Windows::UI::Core;
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040042
atraczykaeb4a5c2016-12-01 19:17:18 -050043using namespace Windows::UI::Popups;
44
Nicolas Jager7f34b772016-12-09 12:25:25 -050045// refacto : the message text page should be
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040046MessageTextPage::MessageTextPage()
47{
48 InitializeComponent();
Nicolas Jager93abfea2016-08-30 12:33:07 -040049
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040050 /* bind the source to account only able to be used to contact the contact */
51 _associableAccountsList_->ItemsSource = AccountsViewModel::instance->accountsList;
52 _associableAccountsList_->SelectionChanged += ref new Windows::UI::Xaml::Controls::SelectionChangedEventHandler(this, &RingClientUWP::Views::MessageTextPage::OnSelectionChanged);
53
Nicolas Jagerc551c362016-10-01 19:24:50 -040054 /* connect to delegates */
Nicolas Jager93abfea2016-08-30 12:33:07 -040055 RingD::instance->incomingAccountMessage += ref new IncomingAccountMessage([&](String^ accountId,
Nicolas Jagerc551c362016-10-01 19:24:50 -040056 String^ fromRingId, String^ payload) {
57 scrollDown();
Nicolas Jager7bef1492016-09-14 11:23:29 -040058 });
Nicolas Jager8f805cd2016-10-05 11:54:40 -040059 RingD::instance->incomingMessage += ref new RingClientUWP::IncomingMessage(this, &RingClientUWP::Views::MessageTextPage::OnincomingMessage);
60
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040061}
62
63void
64RingClientUWP::Views::MessageTextPage::updatePageContent()
65{
Nicolas Jagerc551c362016-10-01 19:24:50 -040066 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
67 auto contact = item->_contact;
68
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040069 if (!contact) /* should never happen */
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040070 return;
71
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040072 /* show the name of contact on the page */
Nicolas Jager7f34b772016-12-09 12:25:25 -050073 _title_->Text = contact->_name;
74 _profilName_->Text = contact->_displayName;
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040075
atraczykaeb4a5c2016-12-01 19:17:18 -050076 String^ image_path = Utils::toPlatformString(RingD::instance->getLocalFolder()) + ".vcards\\" + contact->_vcardUID + ".png";
atraczyk890921f2016-12-02 16:26:31 -050077 if (Utils::fileExists(Utils::toString(image_path))) {
78 auto uri = ref new Windows::Foundation::Uri(image_path);
79 _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
80 }
Nicolas Jager7f34b772016-12-09 12:25:25 -050081 else {
82 auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/TESTS/contactAvatar.png");
83 _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
84 }
atraczykaeb4a5c2016-12-01 19:17:18 -050085
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040086 /* show messages */
Nicolas Jager93abfea2016-08-30 12:33:07 -040087 _messagesList_->ItemsSource = contact->_conversation->_messages;
88
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040089 /* select the associated accountId stored with the contact */
90 auto accountIdAssociated = contact->_accountIdAssociated;
91 auto list = AccountsViewModel::instance->accountsList;
92 unsigned int index = 0;
93 bool found = true;
94
95 for (auto item : list)
96 if (item->accountID_ == accountIdAssociated) {
97 found = list->IndexOf(item, &index);
98 break;
99 }
100
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400101 if (found)
102 _associableAccountsList_->SelectedIndex = index;
103 else
104 ERR_("mismatch between accountIdAssociated and associable accounts!");
105
106 /* scroll to the last message on the page*/
Nicolas Jager7bef1492016-09-14 11:23:29 -0400107 scrollDown();
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400108
Nicolas Jager7bef1492016-09-14 11:23:29 -0400109}
110
111void RingClientUWP::Views::MessageTextPage::scrollDown()
112{
113 _scrollView_->UpdateLayout();
114 _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400115}
116
117void
118RingClientUWP::Views::MessageTextPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
119{
120 sendMessage();
121}
122
123void
124RingClientUWP::Views::MessageTextPage::_messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
125{
126 if (e->Key == Windows::System::VirtualKey::Enter) {
127 sendMessage();
128 }
129}
130
131void
132RingClientUWP::Views::MessageTextPage::sendMessage()
133{
Nicolas Jagerc551c362016-10-01 19:24:50 -0400134 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
135 auto contact = item->_contact;
136
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400137 auto txt = _messageTextBox_->Text;
138
139 /* empty the textbox */
140 _messageTextBox_->Text = "";
141
142 if (!contact || txt->IsEmpty())
143 return;
144
Nicolas Jager655df542016-08-31 10:24:47 -0400145 RingD::instance->sendAccountTextMessage(txt);
Nicolas Jager7bef1492016-09-14 11:23:29 -0400146 scrollDown();
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400147}
Nicolas Jager7bef1492016-09-14 11:23:29 -0400148
149Object ^ RingClientUWP::Views::BubbleBackground::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
150{
atraczykaeb4a5c2016-12-01 19:17:18 -0500151 auto color1 = Windows::UI::ColorHelper::FromArgb(255, 0, 76, 96);
152 auto color2 = Windows::UI::ColorHelper::FromArgb(255, 58, 192, 210);
153 return ((bool)value) ? ref new SolidColorBrush(color1) : ref new SolidColorBrush(color2);
Nicolas Jager7bef1492016-09-14 11:23:29 -0400154}
155
156// we only do OneWay so the next function is not used
157Object ^ RingClientUWP::Views::BubbleBackground::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
158{
159 throw ref new Platform::NotImplementedException();
160}
161
162RingClientUWP::Views::BubbleBackground::BubbleBackground()
163{}
164
165Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
166{
167 return ((bool)value) ? Windows::UI::Xaml::HorizontalAlignment::Left : Windows::UI::Xaml::HorizontalAlignment::Right;
168}
169
170// we only do OneWay so the next function is not used
171Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
172{
173 throw ref new Platform::NotImplementedException();
174}
175
176RingClientUWP::Views::BubbleHorizontalAlignement::BubbleHorizontalAlignement()
177{}
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400178
179
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400180void RingClientUWP::Views::MessageTextPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400181{
182 scrollDown();
183}
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400184
185
186void RingClientUWP::Views::MessageTextPage::OnSelectionChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^e)
187{
188 auto account = dynamic_cast<Account^>(_associableAccountsList_->SelectedItem);
189 SmartPanelItemsViewModel::instance->_selectedItem->_contact->_accountIdAssociated = account->accountID_;
Nicolas Jagere72856b2016-10-30 15:06:17 -0400190}
191
192void RingClientUWP::Views::MessageTextPage::_deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
193{
194 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
195 auto contact = item->_contact;
196
atraczykaeb4a5c2016-12-01 19:17:18 -0500197 auto messageDialog = ref new MessageDialog("Are you sure you want to remove this contact?", "Remove Contact");
Nicolas Jagere72856b2016-10-30 15:06:17 -0400198
atraczykaeb4a5c2016-12-01 19:17:18 -0500199 messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
200 {})));
201 messageDialog->Commands->Append(ref new UICommand("Remove", ref new UICommandInvokedHandler([=](IUICommand^ command)
202 {
203 closeMessageTextPage();
204 ContactsViewModel::instance->deleteContact(contact);
205 SmartPanelItemsViewModel::instance->removeItem(item);
206 })));
207
208 messageDialog->DefaultCommandIndex = 1;
209
210 messageDialog->ShowAsync();
Nicolas Jagere72856b2016-10-30 15:06:17 -0400211}
Nicolas Jager1d4ab4a2016-10-30 15:18:26 -0400212
213
214void RingClientUWP::Views::MessageTextPage::_clearConversation__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
215{
216 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
217 auto contact = item->_contact;
atraczykaeb4a5c2016-12-01 19:17:18 -0500218
219 auto messageDialog = ref new MessageDialog("Are you sure you want to clear the conversation?", "Clear conversation");
220
221 messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
222 {})));
223 messageDialog->Commands->Append(ref new UICommand("Clear", ref new UICommandInvokedHandler([=](IUICommand^ command)
224 {
225 contact->_conversation->_messages->Clear();
226 contact->saveConversationToFile();
227 })));
228
229 messageDialog->DefaultCommandIndex = 1;
230
231 messageDialog->ShowAsync();
Nicolas Jager1d4ab4a2016-10-30 15:18:26 -0400232}
Nicolas Jager61e6f0e2016-11-04 15:12:08 -0400233
234
235void RingClientUWP::Views::MessageTextPage::_audioCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
236{
237 auto button = dynamic_cast<Button^>(e->OriginalSource);
238 if (button) {
239 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
240 if (item) {
241 auto contact = item->_contact;
242 if (contact)
243 RingD::instance->placeCall(contact);
244 }
245 }
246}
247
248
249void RingClientUWP::Views::MessageTextPage::_videoCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
250{
251 auto button = dynamic_cast<Button^>(e->OriginalSource);
252 if (button) {
253 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
254 if (item) {
255 auto contact = item->_contact;
256 if (contact)
257 RingD::instance->placeCall(contact);
258 }
259 }
260}