forked from processtech/runawfe-free-notifier-cpp
/
Connector.cpp
320 lines (297 loc) · 9.81 KB
/
Connector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "StdAfx.h"
#include "Connector.h"
#include "Utils.h"
#include "Logger.h"
#include "RtnResources.h"
Server::State::State(ConnectionState connectionState) {
this->connectionState = connectionState;
this->unreadTasksCount = 0;
this->totalTasksCount = 0;
this->taskIds = NULL;
this->taskNames = NULL;
}
Server::State::State(ns1__getTasksResponse* response) {
this->connectionState = CONNECTION_STATE_CONNECTED;
this->unreadTasksCount = 0;
this->totalTasksCount = response->__sizeresult;
this->taskIds = new LONG64[totalTasksCount];
this->taskNames = new wstring[totalTasksCount];
for (int i = 0; i < totalTasksCount; i++) {
if (response->result[i]->firstOpen) {
this->unreadTasksCount++;
}
this->taskIds[i] = *(response->result[i]->id);
this->taskNames[i] = response->result[i]->name;
}
}
Server::State::~State(void) {
delete[] taskIds;
delete[] taskNames;
}
Server::ConnectionState Server::State::GetConnectionState() {
return connectionState;
}
int Server::State::GetTotalTasksCount() {
return totalTasksCount;
}
int Server::State::GetUnreadTasksCount() {
return unreadTasksCount;
}
bool Server::State::HasChanges(State* previousState) {
if (connectionState != previousState->connectionState) {
return true;
}
if (totalTasksCount != previousState->totalTasksCount) {
return true;
}
if (unreadTasksCount != previousState->unreadTasksCount) {
return true;
}
for (int i = 0; i < totalTasksCount; i++) {
bool foundInPrevious = false;
for (int j = 0; j < previousState->totalTasksCount; j++) {
if (this->taskIds[i] == previousState->taskIds[j]) {
foundInPrevious = true;
break;
}
}
if (!foundInPrevious) {
return true;
}
}
return false;
}
wstring Server::State::GetNotificationMessageAboutNewTasks(State* previousState) {
if (connectionState == CONNECTION_STATE_CONNECTED) {
if (previousState->connectionState != CONNECTION_STATE_CONNECTED && this->unreadTasksCount > 0) {
return RtnResources::GetOption(L"connection.state.connected.having.unread.tasks", L"You have unread tasks");
}
if (this->taskIds != NULL && previousState->taskIds != NULL) {
wstring message = L"";
for (int i = 0; i < totalTasksCount; i++) {
bool foundInPrevious = false;
for (int j = 0; j < previousState->totalTasksCount; j++) {
if (this->taskIds[i] == previousState->taskIds[j]) {
foundInPrevious = true;
break;
}
}
if (!foundInPrevious) {
if (!message.empty()) {
message.append(L"\n");
}
message.append(this->taskNames[i]);
}
}
return message;
}
}
return L"";
}
Server::Connector::Connector(Auth::RtnCredentials* appCredentials) {
this->appCredentials=appCredentials;
user = NULL;
previousState = new State(CONNECTION_STATE_INITIALIZING);
currentState = new State(CONNECTION_STATE_INITIALIZING);
initialized = false;
}
Server::Connector::~Connector(void) {
Reset();
delete previousState;
delete currentState;
}
void Server::Connector::Initialize() {
if (!initialized) {
delete currentState;
this->currentState = new State(CONNECTION_STATE_INITIALIZING);
serverType = RtnResources::GetServerType();
serverVersion = RtnResources::GetServerVersion();
authtype = RtnResources::GetAuthenticationType();
initialized = true;
}
}
void Server::Connector::Reset() {
serverType = L"";
serverVersion = L"";
if (user != NULL) {
delete user;
user = NULL;
}
initialized = false;
}
ns1__user* Server::Connector::AuthenticateByKerberos() {
LOG_DEBUG("call AuthenticateByKerberos ...");
const wstring kerberosServerName = RtnResources::GetOption(L"kerberos.spn", L"");
if (kerberosServerName.empty()) {
throw "Required property 'kerberos.spn' is not set";
}
const string ticket = Auth::GetKerberosTicket(kerberosServerName);
if(ticket.empty()) {
LOG_ERROR("GetKerberosTicket failed");
return false;
}
ns1__authenticateByKerberos request;
const unsigned char* tokenPointer = reinterpret_cast<const unsigned char*>(ticket.c_str());
request.token = new xsd__base64Binary();
request.token->__ptr = const_cast<unsigned char*>(tokenPointer);
request.token->__size = ticket.length();
ns1__authenticateByKerberosResponse response;
string authenticationUrl = IO::ToString(RtnResources::GetWebServiceURL(serverType, serverVersion, L"Authentication"));
ServerAPIBindingProxy authProxy(authenticationUrl.c_str());
int result = authProxy.authenticateByKerberos(&request, &response);
if (result == SOAP_OK) {
LOG_DEBUG("call AuthenticateByKerberos completed");
return response.result;
} else {
LOG_ERROR("call AuthenticateByKerberos failed by '%s'", authenticationUrl.c_str());
Logger::LogWebServiceError(&authProxy);
return NULL;
}
// TODO authProxy.destroy(); if copy constructor for class ns1__user will be available
}
ns1__user* Server::Connector::AuthenticateByLoginAndPassword() {
LOG_DEBUG("call AuthenticateByLogin ...");
ns1__authenticateByLoginPassword request;
wchar_t loginBuffer[200];
wchar_t passBuffer[200];
swprintf(loginBuffer,appCredentials->getLogin().length()+1,L"%s",appCredentials->getLogin().c_str());
swprintf(passBuffer,appCredentials->getPassword().length()+1,L"%s",appCredentials->getPassword().c_str());
request.name = loginBuffer;
request.password = passBuffer;
string authenticationUrl = IO::ToString(RtnResources::GetWebServiceURL(serverType, serverVersion, L"Authentication"));
ServerAPIBindingProxy authProxy(authenticationUrl.c_str());
ns1__authenticateByLoginPasswordResponse response;
int result = authProxy.authenticateByLoginPassword(&request, &response);
if (result == SOAP_OK) {
LOG_DEBUG("call AuthenticateByKerberos completed");
return response.result;
} else {
LOG_ERROR("call AuthenticateByKerberos failed by '%s'", authenticationUrl.c_str());
Logger::LogWebServiceError(&authProxy);
return NULL;
}
}
Server::State* Server::Connector::GetTasks() {
LOG_DEBUG("call GetTasks ...");
ns1__getTasks request;
string executionUrl = IO::ToString(RtnResources::GetWebServiceURL(serverType, serverVersion, L"Execution"));
ServerAPIBindingProxy executionProxy(executionUrl.c_str());
request.user = user;
ns1__getTasksResponse response;
int result = executionProxy.getTasks(&request, &response);
State* state;
if (result == SOAP_OK) {
LOG_DEBUG("call GetTasks completed");
state = new Server::State(&response);
} else {
LOG_ERROR("call GetTasks failed by url '%s'", executionUrl.c_str());
Logger::LogWebServiceError(&executionProxy);
state = new Server::State(CONNECTION_STATE_ERROR);
}
executionProxy.destroy();
return state;
}
Server::State* Server::Connector::RequestState() {
State* state;
try {
LOG_DEBUG("call RequestState ...");
Initialize();
if (user == NULL) {
if(authtype.compare(BASIC_AUTH)==0){
this->user = AuthenticateByLoginAndPassword();
}else{
this->user = AuthenticateByKerberos();
}
if (user == NULL) {
throw "User is NULL";
}
}
state = GetTasks();
} catch (const exception& e) {
LOG_ERROR("exception: %s", e.what());
state = new Server::State(CONNECTION_STATE_ERROR);
} catch (const char* e) {
LOG_ERROR("exception: %s", e);
state = new Server::State(CONNECTION_STATE_ERROR);
} catch (const string e) {
LOG_ERROR("exception: %s", e.c_str());
state = new Server::State(CONNECTION_STATE_ERROR);
} catch (const wstring e) {
LOG_ERROR_W(L"exception: " + e);
state = new Server::State(CONNECTION_STATE_ERROR);
} catch(...) {
LOG_ERROR("General exception catched");
state = new Server::State(CONNECTION_STATE_ERROR);
}
if (state->GetConnectionState() == CONNECTION_STATE_ERROR) {
Reset();
}
return state;
}
void Server::Connector::UpdateState() {
State* newState = RequestState();
delete previousState;
previousState = currentState;
currentState = newState;
}
bool Server::Connector::HasChanges() {
return this->currentState->HasChanges(this->previousState);
}
bool Server::Connector::HasNotificationAboutNewTasks() {
return !this->currentState->GetNotificationMessageAboutNewTasks(this->previousState).empty();
}
wstring Server::Connector::GetNotificationTooltipMessage() {
wstring message = RtnResources::GetApplicationTitle();
message.append(L"\n");
switch(currentState->GetConnectionState()) {
case CONNECTION_STATE_INITIALIZING:
message.append(RtnResources::GetOption(L"connection.state.initializing", L"Initializing application"));
break;
case CONNECTION_STATE_CONNECTED: {
message.append(RtnResources::GetOption(L"connection.state.connected.total.tasks", L"Total tasks"));
message.append(L": ");
wchar_t buf[2 * _MAX_INT_DIG];
std::swprintf(buf, L"%d", currentState->GetTotalTasksCount());
message.append(wstring(buf));
message.append(L"\n");
message.append(RtnResources::GetOption(L"connection.state.connected.unread.tasks", L"Unread tasks"));
message.append(L": ");
wchar_t buf1[2 * _MAX_INT_DIG];
std::swprintf(buf1, L"%d", currentState->GetUnreadTasksCount());
message.append(wstring(buf1));
break;
}
case CONNECTION_STATE_ERROR:
message.append(RtnResources::GetOption(L"connection.state.error", L"Error"));
break;
}
return message;
}
HICON Server::Connector::GetNotificationIcon() {
wstring fileName;
switch(currentState->GetConnectionState()) {
case CONNECTION_STATE_INITIALIZING:
fileName = L"rtn.initializing.ico";
break;
case CONNECTION_STATE_CONNECTED: {
if (currentState->GetUnreadTasksCount() == 0) {
fileName = L"rtn.app.ico";
} else {
fileName = L"rtn.connected.has.new.tasks.ico";
}
break;
}
case CONNECTION_STATE_ERROR:
fileName = L"rtn.error.ico";
break;
}
wstring filePath = IO::GetFilePath(fileName);
HICON icon = (HICON) LoadImage(NULL, filePath.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
if (!icon) {
LOG_ERROR_W(L"Unable to load icon '" + fileName + L"'");
}
return icon;
}
wstring Server::Connector::GetNotificationMessageAboutNewTasks() {
return this->currentState->GetNotificationMessageAboutNewTasks(this->previousState);
}