blob: 723ff670c358fbfe2f8aeb6802724f59deace279 [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) {
atraczyk746f3762017-02-28 14:12:51 -050034 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
35 ref new DispatchedHandler([=]()
36 {
37 output(message);
38 }));
atraczyk61b28422016-08-24 09:25:59 -040039 });
40}
41
42void
43RingConsolePanel::output(Platform::String^ message)
44{
45 try {
46 Run^ inlineText = ref new Run();
47 inlineText->Text = message;
48 Paragraph^ paragraph = ref new Paragraph();
49 paragraph->Inlines->Append(inlineText);
50 _debugWindowOutput_->Blocks->Append(paragraph);
51 _scrollView_->UpdateLayout();
52 _scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
53 }
54 catch (Platform::Exception^ e) {
55 return;
56 }
57}
58
59void RingConsolePanel::_btnSendDbgCmd__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
60{
61 sendCommand();
62}
63
64
65void RingConsolePanel::_sendDbgCmd__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
66{
67 if (e->Key == Windows::System::VirtualKey::Enter && _tBoxDbg_->Text != "") {
68 sendCommand();
69 }
70 else if (e->Key == Windows::System::VirtualKey::PageUp) {
71
72 if (historyLevel < 1)
73 return;
74 if (historyLevel == historyCmds.Size)
75 currentCmd = _tBoxDbg_->Text;
76 historyLevel--;
77 _tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
78
79 }
80 else if (e->Key == Windows::System::VirtualKey::PageDown) {
81 if (historyLevel < historyCmds.Size) {
82 _tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
83 historyLevel++;
84
85 }
86 else {
87 _tBoxDbg_->Text = currentCmd;
88 }
89 return;
90 }
91}
92
93/*\ ADD EACH NEW COMMAND TO THE HELP LIST \*/
94void RingConsolePanel::sendCommand()
95{
Nicolas Jager4569bf52016-11-03 11:13:47 -040096 auto inputConst_str = Utils::toString(_tBoxDbg_->Text);
97 if (inputConst_str.empty())
98 return;
99
100 auto inputConst_cstr = inputConst_str.c_str();
101 char* input_cstr = _strdup(inputConst_cstr); // full string
102 char* input_cstr_nextToken; // tokenized string
103 char* cmd_cstr = strtok_s(input_cstr, " ", &input_cstr_nextToken);
104 char* parameter1_cstr = strtok_s(input_cstr_nextToken, " ", &input_cstr_nextToken);
105 // parameter2...
106
107 if (!cmd_cstr)
108 return;
109
110 std::string input(cmd_cstr);
111 std::string parameter1;
112
113 if (parameter1_cstr)
114 parameter1 = parameter1_cstr;
115
116 free(input_cstr);
117 free(input_cstr_nextToken);
118 //free((char*)inputConst_cstr);
119
atraczyk61b28422016-08-24 09:25:59 -0400120 addCommandToHistory();
121 historyLevel++;
122 _tBoxDbg_->Text = "";
123 currentCmd = "";
124 historyLevel = historyCmds.Size;
125
Nicolas Jager4569bf52016-11-03 11:13:47 -0400126 if (input == "help") {
atraczyk61b28422016-08-24 09:25:59 -0400127 MSG_(">> Help :");
128 MSG_("use PgUp/PgDown for crawling commands history.");
Nicolas Jager4569bf52016-11-03 11:13:47 -0400129 MSG_("getCallsList, switchDebug, killCall [callId, -all], getAccountInfo, getContactsList, placeCall [contact name]");
atraczyk61b28422016-08-24 09:25:59 -0400130 return;
131 }
Nicolas Jager4569bf52016-11-03 11:13:47 -0400132 else if (input == "getCallsList") {
133 RingD::instance->getCallsList();
134 return;
135 }
136 else if (input == "switchDebug") {
137 MSG_(">> switching dameon debug output");
138 RingD::instance->switchDebug();
139 return;
140 }
141 else if (input == "killCall") {
142 if (parameter1.empty()) {
143 MSG_("callId missing");
144 return;
145 }
146 RingD::instance->killCall(Utils::toPlatformString(parameter1));
147 return;
148 }
149 else if (input == "getAccountInfo") {
150 auto id = AccountListItemsViewModel::instance->_selectedItem->_account->accountID_;
151 MSG_("id : "+Utils::toString(id));
152 return;
153 }
atraczyk61b28422016-08-24 09:25:59 -0400154
Nicolas Jager4569bf52016-11-03 11:13:47 -0400155 MSG_(">> error, command \'" + input + "\' not found");
atraczyk61b28422016-08-24 09:25:59 -0400156}
157
158void RingConsolePanel::addCommandToHistory()
159{
160 historyCmds.Append(_tBoxDbg_->Text);
Nicolas Jager32ed1a22016-08-17 08:36:02 -0400161}