2015-01-22 20:44:54 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Daniel Molkentin <danimo@owncloud.com>. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
#include "NCClientInterface.h"
|
2015-01-22 20:44:54 +03:00
|
|
|
|
|
|
|
#include "CommunicationSocket.h"
|
|
|
|
#include "StringUtil.h"
|
|
|
|
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
|
|
|
#include <Strsafe.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#define PIPE_TIMEOUT 5*1000 //ms
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
NCClientInterface::ContextMenuInfo NCClientInterface::FetchInfo(const std::wstring &files)
|
2015-01-22 20:44:54 +03:00
|
|
|
{
|
2017-01-05 19:28:28 +03:00
|
|
|
auto pipename = CommunicationSocket::DefaultPipePath();
|
2015-01-22 20:44:54 +03:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
CommunicationSocket socket;
|
|
|
|
if (!WaitNamedPipe(pipename.data(), PIPE_TIMEOUT)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (!socket.Connect(pipename)) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-19 21:44:10 +03:00
|
|
|
socket.SendMsg(L"GET_STRINGS:CONTEXT_MENU_TITLE\n");
|
|
|
|
socket.SendMsg((L"GET_MENU_ITEMS:" + files + L"\n").data());
|
2015-02-13 18:00:22 +03:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
ContextMenuInfo info;
|
|
|
|
std::wstring response;
|
|
|
|
int sleptCount = 0;
|
|
|
|
while (sleptCount < 5) {
|
|
|
|
if (socket.ReadLine(&response)) {
|
|
|
|
if (StringUtil::begins_with(response, wstring(L"REGISTER_PATH:"))) {
|
|
|
|
wstring responsePath = response.substr(14); // length of REGISTER_PATH
|
|
|
|
info.watchedDirectories.push_back(responsePath);
|
|
|
|
}
|
2017-07-04 17:58:55 +03:00
|
|
|
else if (StringUtil::begins_with(response, wstring(L"STRING:"))) {
|
|
|
|
wstring stringName, stringValue;
|
|
|
|
if (!StringUtil::extractChunks(response, stringName, stringValue))
|
|
|
|
continue;
|
2018-01-19 21:44:10 +03:00
|
|
|
if (stringName == L"CONTEXT_MENU_TITLE")
|
2017-07-04 17:58:55 +03:00
|
|
|
info.contextMenuTitle = move(stringValue);
|
2018-01-19 21:44:10 +03:00
|
|
|
} else if (StringUtil::begins_with(response, wstring(L"MENU_ITEM:"))) {
|
|
|
|
wstring commandName, flags, title;
|
|
|
|
if (!StringUtil::extractChunks(response, commandName, flags, title))
|
|
|
|
continue;
|
|
|
|
info.menuItems.push_back({ commandName, flags, title });
|
|
|
|
} else if (StringUtil::begins_with(response, wstring(L"GET_MENU_ITEMS:END"))) {
|
2017-07-04 17:58:55 +03:00
|
|
|
break; // Stop once we completely received the last sent request
|
2017-01-05 19:28:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Sleep(50);
|
|
|
|
++sleptCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
2015-01-22 20:44:54 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
void NCClientInterface::SendRequest(const wchar_t *verb, const std::wstring &path)
|
2015-01-22 20:44:54 +03:00
|
|
|
{
|
2017-01-05 19:28:28 +03:00
|
|
|
auto pipename = CommunicationSocket::DefaultPipePath();
|
2015-01-22 20:44:54 +03:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
CommunicationSocket socket;
|
|
|
|
if (!WaitNamedPipe(pipename.data(), PIPE_TIMEOUT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!socket.Connect(pipename)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-22 20:44:54 +03:00
|
|
|
|
2018-09-20 18:14:05 +03:00
|
|
|
socket.SendMsg((verb + (L":" + path + L"\n")).data());
|
2015-01-22 20:44:54 +03:00
|
|
|
}
|