-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBaseNppExplorerCommandHandler.cpp
178 lines (137 loc) · 4.79 KB
/
BaseNppExplorerCommandHandler.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
#include "pch.h"
#include "resource.h"
#include "BaseNppExplorerCommandHandler.h"
#include "PathHelper.h"
#include "ThreadUILanguageChanger.h"
using namespace NppShell::CommandHandlers;
using namespace NppShell::Helpers;
extern HMODULE g_module;
BaseNppExplorerCommandHandler::BaseNppExplorerCommandHandler()
{
counter = make_unique<SharedCounter>();
state = make_unique<SharedState>();
}
const wstring BaseNppExplorerCommandHandler::GetNppExecutableFullPath()
{
const wstring path = GetApplicationPath();
const wstring fileName = L"\\notepad++.exe";
return path + fileName;
}
bool ReadLanguageOverrideFile(wstring& content)
{
const wstring fileFullName = GetContextMenuPath() + L"\\overrideMenuEntryLanguage.txt";
// Open the file for reading.
HANDLE file = CreateFileW(fileFullName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
// The file does not exist, so no override.
return false;
}
// Allocate a buffer for the file content.
// Since we only need 6 chars ("xx-XX" followed by a null byte, maybe double if unicode encoded), so we use 32 bytes, to be on the safe side.
const DWORD bufferSize = 32;
vector<BYTE> buffer(bufferSize);
// Read the first 32 chars into the buffer.
DWORD bytesRead;
if (!ReadFile(file, buffer.data(), bufferSize - 1, &bytesRead, NULL))
{
CloseHandle(file);
return false;
}
// Close the file handle
CloseHandle(file);
// Null-terminate the buffer
buffer[bytesRead] = '\0';
// Convert the buffer to a wstring
content = wstring(buffer.begin(), buffer.begin() + bytesRead);
if (content == L"")
{
// The file exists, but is empty, so we default to "en-US";
content = L"en-US";
}
return true;
}
const wstring BaseNppExplorerCommandHandler::Title()
{
// A buffer size of 1024 should be enough to hold the for all languages.
constexpr int bufferSize = 1024;
// Buffer to store the string resource into.
WCHAR buffer[bufferSize];
// Read the override file, and if success, we change the language we use.
wstring overrideLanguage;
if (ReadLanguageOverrideFile(overrideLanguage))
{
// Change the language
ThreadUILanguageChanger languageChanger(overrideLanguage);
// Load the string from the resource matching the override language.
LoadStringW(g_module, IDS_EDIT_WITH_NOTEPADPLUSPLUS, buffer, bufferSize);
}
else
{
// Load the string from the resource matching the current language.
LoadStringW(g_module, IDS_EDIT_WITH_NOTEPADPLUSPLUS, buffer, bufferSize);
}
// Finally we convert the buffer into a wstring that we can return.
return wstring(buffer);
}
const wstring BaseNppExplorerCommandHandler::Icon()
{
const wstring fileName = GetNppExecutableFullPath();
return fileName;
}
const wstring BaseNppExplorerCommandHandler::GetCommandLine(const wstring& itemName)
{
const wstring fileName = GetNppExecutableFullPath();
const wstring parameters = L"\"" + itemName + L"\"";
return L"\"" + fileName + L"\" " + parameters;
}
IFACEMETHODIMP BaseNppExplorerCommandHandler::Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept try
{
UNREFERENCED_PARAMETER(pbc);
if (!psiItemArray)
{
return S_OK;
}
DWORD count;
RETURN_IF_FAILED(psiItemArray->GetCount(&count));
IShellItem* psi = nullptr;
LPWSTR file2OpenPath;
wstring appPath = L"\"";
appPath += GetNppExecutableFullPath().c_str();
appPath += L"\"";
wstring filePathsArg = appPath;
filePathsArg += L" ";
for (DWORD i = 0; i < count; ++i)
{
psiItemArray->GetItemAt(i, &psi);
RETURN_IF_FAILED(psi->GetDisplayName(SIGDN_FILESYSPATH, &file2OpenPath));
// Release the IShellItem pointer, since we are done with it as well.
psi->Release();
filePathsArg += L"\"";
filePathsArg += file2OpenPath;
filePathsArg += L"\" ";
// Cleanup itemName, since we are done with it.
if (file2OpenPath)
{
CoTaskMemFree(file2OpenPath);
}
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcessW(GetNppExecutableFullPath().c_str(), (LPWSTR)filePathsArg.c_str(), nullptr, nullptr, false, CREATE_NEW_PROCESS_GROUP, nullptr, nullptr, &si, &pi))
{
return S_OK;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return S_OK;
}
CATCH_RETURN();
const EXPCMDSTATE BaseNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
{
UNREFERENCED_PARAMETER(psiItemArray);
throw L"State must be overridden in all implementations";
}