blob: 18292d198b0f9b5f945509a1c685a028737bd2c3 [file] [log] [blame]
atraczyk61b28422016-08-24 09:25:59 -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"
19
20#include "RingConsolePanel.xaml.h"
21
22using namespace RingClientUWP;
23using namespace RingClientUWP::Views;
Nicolas Jager4569bf52016-11-03 11:13:47 -040024using namespace RingClientUWP::ViewModel;
atraczyk61b28422016-08-24 09:25:59 -040025using namespace Windows::ApplicationModel::Core;
26using namespace Windows::UI::Core;
27using namespace Windows::UI::Xaml::Documents;
28
29RingConsolePanel::RingConsolePanel()
30{
31 InitializeComponent();
32
33 RingDebug::instance->messageToScreen += ref new debugMessageToScreen([this](Platform::String^ message) {
34 output(message);
35 });
36}
37
38void
39RingConsolePanel::output(Platform::String^ message)
40{
41 try {
42 Run^ inlineText = ref new Run();
43 inlineText->Text = message;
44 Paragraph^ paragraph = ref new Paragraph();
45 paragraph->Inlines->Append(inlineText);
46 _debugWindowOutput_->Blocks->Append(paragraph);
47 _scrollView_->UpdateLayout();
48 _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
49 }
50 catch (Platform::Exception^ e) {
51 return;
52 }
53}
54
55void RingConsolePanel::_btnSendDbgCmd__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
56{
57 sendCommand();
58}
59
60
61void RingConsolePanel::_sendDbgCmd__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
62{
63 if (e->Key == Windows::System::VirtualKey::Enter && _tBoxDbg_->Text != "") {
64 sendCommand();
65 }
66 else if (e->Key == Windows::System::VirtualKey::PageUp) {
67
68 if (historyLevel < 1)
69 return;
70 if (historyLevel == historyCmds.Size)
71 currentCmd = _tBoxDbg_->Text;
72 historyLevel--;
73 _tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
74
75 }
76 else if (e->Key == Windows::System::VirtualKey::PageDown) {
77 if (historyLevel < historyCmds.Size) {
78 _tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
79 historyLevel++;
80
81 }
82 else {
83 _tBoxDbg_->Text = currentCmd;
84 }
85 return;
86 }
87}
88
89/*\ ADD EACH NEW COMMAND TO THE HELP LIST \*/
90void RingConsolePanel::sendCommand()
91{
Nicolas Jager4569bf52016-11-03 11:13:47 -040092 auto inputConst_str = Utils::toString(_tBoxDbg_->Text);
93 if (inputConst_str.empty())
94 return;
95
96 auto inputConst_cstr = inputConst_str.c_str();
97 char* input_cstr = _strdup(inputConst_cstr); // full string
98 char* input_cstr_nextToken; // tokenized string
99 char* cmd_cstr = strtok_s(input_cstr, " ", &input_cstr_nextToken);
100 char* parameter1_cstr = strtok_s(input_cstr_nextToken, " ", &input_cstr_nextToken);
101 // parameter2...
102
103 if (!cmd_cstr)
104 return;
105
106 std::string input(cmd_cstr);
107 std::string parameter1;
108
109 if (parameter1_cstr)
110 parameter1 = parameter1_cstr;
111
112 free(input_cstr);
113 free(input_cstr_nextToken);
114 //free((char*)inputConst_cstr);
115
atraczyk61b28422016-08-24 09:25:59 -0400116 addCommandToHistory();
117 historyLevel++;
118 _tBoxDbg_->Text = "";
119 currentCmd = "";
120 historyLevel = historyCmds.Size;
121
Nicolas Jager4569bf52016-11-03 11:13:47 -0400122 if (input == "help") {
atraczyk61b28422016-08-24 09:25:59 -0400123 MSG_(">> Help :");
124 MSG_("use PgUp/PgDown for crawling commands history.");
Nicolas Jager4569bf52016-11-03 11:13:47 -0400125 MSG_("getCallsList, switchDebug, killCall [callId, -all], getAccountInfo, getContactsList, placeCall [contact name]");
atraczyk61b28422016-08-24 09:25:59 -0400126 return;
127 }
Nicolas Jager4569bf52016-11-03 11:13:47 -0400128 else if (input == "getCallsList") {
129 RingD::instance->getCallsList();
130 return;
131 }
132 else if (input == "switchDebug") {
133 MSG_(">> switching dameon debug output");
134 RingD::instance->switchDebug();
135 return;
136 }
137 else if (input == "killCall") {
138 if (parameter1.empty()) {
139 MSG_("callId missing");
140 return;
141 }
142 RingD::instance->killCall(Utils::toPlatformString(parameter1));
143 return;
144 }
145 else if (input == "getAccountInfo") {
146 auto id = AccountListItemsViewModel::instance->_selectedItem->_account->accountID_;
147 MSG_("id : "+Utils::toString(id));
148 return;
149 }
150 else if (input == "getContactsList") {
151 auto list = ContactsViewModel::instance->contactsList;
152 MSG_("list of calls returned by the daemon :");
153 for (auto contact : list) {
154 MSG_("name : " + Utils::toString(contact->name_));
155 MSG_("ringId : " + Utils::toString(contact->ringID_));
156 }
157 return;
158 }
159 else if (input == "placeCall") {
160 if (parameter1.empty()) {
161 MSG_("contact name missing");
162 return;
163 }
164 auto contact = ContactsViewModel::instance->findContactByName(Utils::toPlatformString(parameter1));
165 if (!contact) {
166 MSG_("contact "+parameter1+" not found");
167 return;
168 }
169 RingD::instance->placeCall(contact);
170 }
atraczyk61b28422016-08-24 09:25:59 -0400171
Nicolas Jager4569bf52016-11-03 11:13:47 -0400172 MSG_(">> error, command \'" + input + "\' not found");
atraczyk61b28422016-08-24 09:25:59 -0400173}
174
175void RingConsolePanel::addCommandToHistory()
176{
177 historyCmds.Append(_tBoxDbg_->Text);
Nicolas Jager32ed1a22016-08-17 08:36:02 -0400178}