blob: 9c6be50edabb0f379825d09b70c86265dadc8aba [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 Jager8e8787f2016-12-11 17:25:17 -050075 contact->_unreadMessages = 0;
Nicolas Jager8a85e1f2016-08-15 15:11:06 -040076
atraczykaeb4a5c2016-12-01 19:17:18 -050077 String^ image_path = Utils::toPlatformString(RingD::instance->getLocalFolder()) + ".vcards\\" + contact->_vcardUID + ".png";
atraczyk890921f2016-12-02 16:26:31 -050078 if (Utils::fileExists(Utils::toString(image_path))) {
79 auto uri = ref new Windows::Foundation::Uri(image_path);
80 _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
81 }
Nicolas Jager7f34b772016-12-09 12:25:25 -050082 else {
83 auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/TESTS/contactAvatar.png");
84 _contactBarAvatar_->ImageSource = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri);
85 }
atraczykaeb4a5c2016-12-01 19:17:18 -050086
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040087 /* show messages */
Nicolas Jager93abfea2016-08-30 12:33:07 -040088 _messagesList_->ItemsSource = contact->_conversation->_messages;
89
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -040090 /* select the associated accountId stored with the contact */
91 auto accountIdAssociated = contact->_accountIdAssociated;
92 auto list = AccountsViewModel::instance->accountsList;
93 unsigned int index = 0;
94 bool found = true;
95
96 for (auto item : list)
97 if (item->accountID_ == accountIdAssociated) {
98 found = list->IndexOf(item, &index);
99 break;
100 }
101
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400102 if (found)
103 _associableAccountsList_->SelectedIndex = index;
104 else
105 ERR_("mismatch between accountIdAssociated and associable accounts!");
106
107 /* scroll to the last message on the page*/
Nicolas Jager7bef1492016-09-14 11:23:29 -0400108 scrollDown();
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400109
Nicolas Jager7bef1492016-09-14 11:23:29 -0400110}
111
112void RingClientUWP::Views::MessageTextPage::scrollDown()
113{
114 _scrollView_->UpdateLayout();
115 _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400116}
117
118void
119RingClientUWP::Views::MessageTextPage::_sendBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
120{
121 sendMessage();
122}
123
124void
125RingClientUWP::Views::MessageTextPage::_messageTextBox__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
126{
127 if (e->Key == Windows::System::VirtualKey::Enter) {
128 sendMessage();
129 }
130}
131
132void
133RingClientUWP::Views::MessageTextPage::sendMessage()
134{
Nicolas Jagerc551c362016-10-01 19:24:50 -0400135 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
136 auto contact = item->_contact;
137
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400138 auto txt = _messageTextBox_->Text;
139
140 /* empty the textbox */
141 _messageTextBox_->Text = "";
142
143 if (!contact || txt->IsEmpty())
144 return;
145
Nicolas Jager655df542016-08-31 10:24:47 -0400146 RingD::instance->sendAccountTextMessage(txt);
Nicolas Jager7bef1492016-09-14 11:23:29 -0400147 scrollDown();
Nicolas Jager8a85e1f2016-08-15 15:11:06 -0400148}
Nicolas Jager7bef1492016-09-14 11:23:29 -0400149
150Object ^ RingClientUWP::Views::BubbleBackground::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
151{
atraczykaeb4a5c2016-12-01 19:17:18 -0500152 auto color1 = Windows::UI::ColorHelper::FromArgb(255, 0, 76, 96);
Nicolas Jager9d85be92016-12-10 16:10:31 -0500153 auto color2 = Windows::UI::ColorHelper::FromArgb(255, 58, 192, 210); // refacto : defines colors used by the application as globals
atraczykaeb4a5c2016-12-01 19:17:18 -0500154 return ((bool)value) ? ref new SolidColorBrush(color1) : ref new SolidColorBrush(color2);
Nicolas Jager7bef1492016-09-14 11:23:29 -0400155}
156
157// we only do OneWay so the next function is not used
158Object ^ RingClientUWP::Views::BubbleBackground::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
159{
160 throw ref new Platform::NotImplementedException();
161}
162
163RingClientUWP::Views::BubbleBackground::BubbleBackground()
164{}
165
166Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
167{
168 return ((bool)value) ? Windows::UI::Xaml::HorizontalAlignment::Left : Windows::UI::Xaml::HorizontalAlignment::Right;
169}
170
171// we only do OneWay so the next function is not used
172Object ^ RingClientUWP::Views::BubbleHorizontalAlignement::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
173{
174 throw ref new Platform::NotImplementedException();
175}
176
177RingClientUWP::Views::BubbleHorizontalAlignement::BubbleHorizontalAlignement()
178{}
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400179
180
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400181void RingClientUWP::Views::MessageTextPage::OnincomingMessage(Platform::String ^callId, Platform::String ^payload)
Nicolas Jager8f805cd2016-10-05 11:54:40 -0400182{
183 scrollDown();
184}
Nicolas Jagerd8e22fd2016-10-30 09:41:10 -0400185
186
187void RingClientUWP::Views::MessageTextPage::OnSelectionChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs ^e)
188{
189 auto account = dynamic_cast<Account^>(_associableAccountsList_->SelectedItem);
190 SmartPanelItemsViewModel::instance->_selectedItem->_contact->_accountIdAssociated = account->accountID_;
Nicolas Jagere72856b2016-10-30 15:06:17 -0400191}
192
193void RingClientUWP::Views::MessageTextPage::_deleteContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
194{
195 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
196 auto contact = item->_contact;
197
atraczykaeb4a5c2016-12-01 19:17:18 -0500198 auto messageDialog = ref new MessageDialog("Are you sure you want to remove this contact?", "Remove Contact");
Nicolas Jagere72856b2016-10-30 15:06:17 -0400199
atraczykaeb4a5c2016-12-01 19:17:18 -0500200 messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
201 {})));
202 messageDialog->Commands->Append(ref new UICommand("Remove", ref new UICommandInvokedHandler([=](IUICommand^ command)
203 {
204 closeMessageTextPage();
205 ContactsViewModel::instance->deleteContact(contact);
206 SmartPanelItemsViewModel::instance->removeItem(item);
207 })));
208
209 messageDialog->DefaultCommandIndex = 1;
210
211 messageDialog->ShowAsync();
Nicolas Jagere72856b2016-10-30 15:06:17 -0400212}
Nicolas Jager1d4ab4a2016-10-30 15:18:26 -0400213
214
215void RingClientUWP::Views::MessageTextPage::_clearConversation__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
216{
217 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
218 auto contact = item->_contact;
atraczykaeb4a5c2016-12-01 19:17:18 -0500219
220 auto messageDialog = ref new MessageDialog("Are you sure you want to clear the conversation?", "Clear conversation");
221
222 messageDialog->Commands->Append(ref new UICommand("Cancel", ref new UICommandInvokedHandler([this](IUICommand^ command)
223 {})));
224 messageDialog->Commands->Append(ref new UICommand("Clear", ref new UICommandInvokedHandler([=](IUICommand^ command)
225 {
226 contact->_conversation->_messages->Clear();
227 contact->saveConversationToFile();
228 })));
229
230 messageDialog->DefaultCommandIndex = 1;
231
232 messageDialog->ShowAsync();
Nicolas Jager1d4ab4a2016-10-30 15:18:26 -0400233}
Nicolas Jager61e6f0e2016-11-04 15:12:08 -0400234
235
236void RingClientUWP::Views::MessageTextPage::_audioCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
237{
238 auto button = dynamic_cast<Button^>(e->OriginalSource);
239 if (button) {
240 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
241 if (item) {
242 auto contact = item->_contact;
243 if (contact)
244 RingD::instance->placeCall(contact);
245 }
246 }
247}
248
249
250void RingClientUWP::Views::MessageTextPage::_videoCall__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
251{
252 auto button = dynamic_cast<Button^>(e->OriginalSource);
253 if (button) {
254 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
255 if (item) {
256 auto contact = item->_contact;
257 if (contact)
258 RingD::instance->placeCall(contact);
259 }
260 }
261}