diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2016-02-27 16:03:20 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2016-02-27 16:03:20 +0000 |
commit | ce1317d110f70eee0d835ffa3c761168e2f5e9b7 (patch) | |
tree | ea290bde0b8761edd76cbdd00393d17586299bae /plugins/Dropbox/src | |
parent | 2814cd2a0f6ed89dab0c4dca6b8dbb2c7523bef9 (diff) |
Drobpox:
- added search command
- fixed other commands
git-svn-id: http://svn.miranda-ng.org/main/trunk@16363 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Dropbox/src')
-rw-r--r-- | plugins/Dropbox/src/api/operations.h | 22 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox.cpp | 16 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox.h | 5 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox_commands.cpp | 70 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox_services.cpp | 17 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox_utils.cpp | 15 |
6 files changed, 106 insertions, 39 deletions
diff --git a/plugins/Dropbox/src/api/operations.h b/plugins/Dropbox/src/api/operations.h index 71c455d1e8..cdd35223e4 100644 --- a/plugins/Dropbox/src/api/operations.h +++ b/plugins/Dropbox/src/api/operations.h @@ -21,6 +21,26 @@ public: }
};
+class SearchRequest : public HttpRequest
+{
+public:
+ SearchRequest(const char *token, const char *query) :
+ HttpRequest(REQUEST_POST, DROPBOX_API_RPC "/files/search")
+ {
+ AddBearerAuthHeader(token);
+ AddHeader("Content-Type", "application/json");
+
+ JSONNode params(JSON_NODE);
+ params
+ << JSONNode("path", "")
+ << JSONNode("query", query)
+ << JSONNode("max_results", 10);
+
+ json_string data = params.write();
+ SetData(data.c_str(), data.length());
+ }
+};
+
class DeleteRequest : public HttpRequest
{
public:
@@ -82,7 +102,7 @@ public: AddHeader("Content-Type", "application/json");
JSONNode root(JSON_NODE);
- root << JSONNode("path", path);
+ root << JSONNode("path", "");
json_string data = root.write();
SetData(data.c_str(), data.length());
diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp index 2a4477af53..732c8b8eb7 100644 --- a/plugins/Dropbox/src/dropbox.cpp +++ b/plugins/Dropbox/src/dropbox.cpp @@ -16,14 +16,14 @@ CDropbox::CDropbox() : transfers(1, HandleKeySortT) pd.type = PROTOTYPE_VIRTUAL;
Proto_RegisterModule(&pd);
- CreateProtoServiceFunction(MODULE, PS_GETCAPS, ProtoGetCaps);
- CreateProtoServiceFunction(MODULE, PS_GETNAME, ProtoGetName);
- CreateProtoServiceFunction(MODULE, PS_LOADICON, ProtoLoadIcon);
- CreateProtoServiceFunctionObj(PS_GETSTATUS, GlobalService<&CDropbox::ProtoGetStatus>, this);
- CreateProtoServiceFunctionObj(PSS_FILE, GlobalService<&CDropbox::ProtoSendFile>, this);
- CreateProtoServiceFunctionObj(PSS_FILECANCEL, GlobalService<&CDropbox::ProtoCancelFile>, this);
- CreateProtoServiceFunctionObj(PSS_MESSAGE, GlobalService<&CDropbox::ProtoSendMessage>, this);
- CreateProtoServiceFunction(MODULE, PSR_MESSAGE, ProtoReceiveMessage);
+ CreateServiceFunction(MODULE PS_GETCAPS, ProtoGetCaps);
+ CreateServiceFunction(MODULE PS_GETNAME, ProtoGetName);
+ CreateServiceFunction(MODULE PS_LOADICON, ProtoLoadIcon);
+ CreateServiceFunctionObj(MODULE PS_GETSTATUS, GlobalService<&CDropbox::ProtoGetStatus>, this);
+ CreateServiceFunctionObj(MODULE PSS_FILE, GlobalService<&CDropbox::ProtoSendFile>, this);
+ CreateServiceFunctionObj(MODULE PSS_FILECANCEL, GlobalService<&CDropbox::ProtoCancelFile>, this);
+ CreateServiceFunctionObj(MODULE PSS_MESSAGE, GlobalService<&CDropbox::ProtoSendMessage>, this);
+ CreateServiceFunction(MODULE PSR_MESSAGE, ProtoReceiveMessage);
pd.szName = MODULE"Inteceptor";
pd.type = PROTOTYPE_FILTER;
diff --git a/plugins/Dropbox/src/dropbox.h b/plugins/Dropbox/src/dropbox.h index ee17d2e070..10c40041da 100644 --- a/plugins/Dropbox/src/dropbox.h +++ b/plugins/Dropbox/src/dropbox.h @@ -53,8 +53,6 @@ private: int OnFileDialogSuccessed(WPARAM wParam, LPARAM lParam);
// services
- static HANDLE CreateProtoServiceFunctionObj(const char *szService, MIRANDASERVICEOBJ serviceProc, void *obj);
-
static INT_PTR ProtoGetCaps(WPARAM wParam, LPARAM lParam);
static INT_PTR ProtoGetName(WPARAM wParam, LPARAM lParam);
static INT_PTR ProtoLoadIcon(WPARAM wParam, LPARAM lParam);
@@ -70,8 +68,9 @@ private: // commands
static void CommandHelp(void *arg);
- static void CommandContent(void *arg);
+ static void CommandList(void *arg);
static void CommandShare(void *arg);
+ static void CommandSearch(void *arg);
static void CommandDelete(void *arg);
// access token
diff --git a/plugins/Dropbox/src/dropbox_commands.cpp b/plugins/Dropbox/src/dropbox_commands.cpp index b3bfc41da4..10efb39d44 100644 --- a/plugins/Dropbox/src/dropbox_commands.cpp +++ b/plugins/Dropbox/src/dropbox_commands.cpp @@ -6,17 +6,19 @@ void CDropbox::CommandHelp(void *arg) CMStringA help = (char*)T2Utf(TranslateT("Dropbox supports the following commands:"));
help += "\n";
- help += "\"/list [dir]\" \t- "; help += T2Utf(TranslateT("shows all files in folder \"path\" (\"path\" is relative from root folder)"));
+ help += "\"/list [path]\" \t- "; help += T2Utf(TranslateT("shows all files in folder \"path\" (\"path\" is relative from root and can be omitted for root folder)"));
help += "\n";
help += "\"/share <path>\" \t- "; help += T2Utf(TranslateT("returns download link for file or folder with specified path (\"path\" is relative from root folder)"));
help += "\n";
+ help += "\"/search <query>\" \t- "; help += T2Utf(TranslateT("searches for file or folder matched by query (\"query\" is split on spaces into multiple tokens)"));
+ help += "\n";
help += "\"/delete <path>\" \t- "; help += T2Utf(TranslateT("deletes file or folder with specified path (\"path\" is relative from root folder)"));
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)help.GetBuffer());
}
-void CDropbox::CommandContent(void *arg)
+void CDropbox::CommandList(void *arg)
{
CommandParam *param = (CommandParam*)arg;
@@ -46,17 +48,16 @@ void CDropbox::CommandContent(void *arg) }
CMStringA message;
- JSONNode content = root.at("entries").as_array();
- for (size_t i = 0; i < content.size(); i++) {
- JSONNode item = content[i];
- if (item.empty()) {
- if (i == 0)
- message.AppendFormat("\"%s\" %s", path, T2Utf(TranslateT("is empty")));
- break;
+ JSONNode entries = root.at("entries").as_array();
+ if (entries.empty())
+ message.AppendFormat("\"%s\" %s", path, T2Utf(TranslateT("is empty")));
+ else
+ {
+ for (size_t i = 0; i < entries.size(); i++) {
+ JSONNode entry = entries[i];
+ CMStringA subName(entry.at("path_lower").as_string().c_str());
+ message.AppendFormat("%s\n", (subName[0] == '/') ? subName.Mid(1) : subName);
}
-
- CMStringA subName(item.at("path_lower ").as_string().c_str());
- message.AppendFormat("%s\n", (subName[0] == '/') ? subName.Mid(1) : subName);
}
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
@@ -98,6 +99,51 @@ void CDropbox::CommandShare(void *arg) CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)link.GetBuffer());
}
+void CDropbox::CommandSearch(void *arg)
+{
+ CommandParam *param = (CommandParam*)arg;
+
+ char *query = (char*)param->data;
+ if (query == NULL) {
+ CMStringA error(FORMAT, T2Utf(TranslateT("\"%s\" command has invalid parameter.\nUse \"/help\" for more info.")), "/search");
+ ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
+ CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)error.GetBuffer());
+
+ return;
+ }
+
+ ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
+ //bool useShortUrl = db_get_b(NULL, MODULE, "UseSortLinks", 1) > 0;
+ SearchRequest request(token, query);
+ NLHR_PTR response(request.Send(param->instance->hNetlibConnection));
+
+ if (response == NULL || response->resultCode != HTTP_STATUS_OK) {
+ ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0);
+ return;
+ }
+
+ JSONNode root = JSONNode::parse(response->pData);
+ if (root.empty()) {
+ ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0);
+ return;
+ }
+
+ CMStringA message;
+ JSONNode matches = root.at("matches").as_array();
+ if (matches.empty())
+ message.AppendFormat("\"%s\" %s", query, T2Utf(TranslateT("is not found")));
+ else
+ {
+ for (size_t i = 0; i < matches.size(); i++) {
+ JSONNode metadata = matches[i].at("metadata").as_node();
+ message.AppendFormat("%s\n", metadata.at("path_lower").as_string().c_str());
+ }
+ }
+
+ ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
+ CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
+}
+
void CDropbox::CommandDelete(void *arg)
{
CommandParam *param = (CommandParam*)arg;
diff --git a/plugins/Dropbox/src/dropbox_services.cpp b/plugins/Dropbox/src/dropbox_services.cpp index 7469521487..85c5e1c661 100644 --- a/plugins/Dropbox/src/dropbox_services.cpp +++ b/plugins/Dropbox/src/dropbox_services.cpp @@ -1,13 +1,5 @@ #include "stdafx.h"
-HANDLE CDropbox::CreateProtoServiceFunctionObj(const char *szService, MIRANDASERVICEOBJ serviceProc, void *obj)
-{
- char str[MAXMODULELABELLENGTH];
- mir_snprintf(str, "%s%s", MODULE, szService);
- str[MAXMODULELABELLENGTH - 1] = 0;
- return CreateServiceFunctionObj(str, serviceProc, obj);
-}
-
INT_PTR CDropbox::ProtoGetCaps(WPARAM wParam, LPARAM)
{
switch (wParam) {
@@ -117,7 +109,7 @@ INT_PTR CDropbox::ProtoSendMessage(WPARAM, LPARAM lParam) if (*szMessage == '/') {
// parse commands
char *sep = strchr(szMessage, ' ');
- if (sep != NULL) *sep = 0;
+ //if (sep != NULL) *sep = 0;
struct
{
@@ -127,13 +119,16 @@ INT_PTR CDropbox::ProtoSendMessage(WPARAM, LPARAM lParam) static commands[] =
{
{ "help", &CDropbox::CommandHelp },
- { "list", &CDropbox::CommandContent },
+ { "list", &CDropbox::CommandList },
{ "share", &CDropbox::CommandShare },
+ { "search", &CDropbox::CommandSearch },
{ "delete", &CDropbox::CommandDelete }
};
+ char command[16] = {0};
+ mir_strncpy(command, szMessage + 1, sep ? sep - szMessage : mir_strlen(szMessage));
for (int i = 0; i < _countof(commands); i++) {
- if (!mir_strcmp(szMessage + 1, commands[i].szCommand)) {
+ if (!mir_strcmp(command, commands[i].szCommand)) {
ULONG messageId = InterlockedIncrement(&hMessageProcess);
CommandParam *param = new CommandParam();
diff --git a/plugins/Dropbox/src/dropbox_utils.cpp b/plugins/Dropbox/src/dropbox_utils.cpp index 15e97451d2..ee6fc73a60 100644 --- a/plugins/Dropbox/src/dropbox_utils.cpp +++ b/plugins/Dropbox/src/dropbox_utils.cpp @@ -2,10 +2,17 @@ char* CDropbox::PreparePath(const char *oldPath, char *newPath)
{
- CMStringA result("/");
- result.Append(oldPath);
- result.Replace("\\", "/");
- mir_strcpy(newPath, result);
+ if (oldPath == NULL)
+ mir_strcpy(newPath, "");
+ else if (*oldPath != '/')
+ {
+ CMStringA result("/");
+ result.Append(oldPath);
+ result.Replace("\\", "/");
+ mir_strcpy(newPath, result);
+ }
+ else
+ mir_strcpy(newPath, oldPath);
return newPath;
}
|