Repository navigation
Expand file tree
/
Copy pathPastebinPanel.cpp
More file actions
300 lines (255 loc) · 12 KB
/
Copy pathPastebinPanel.cpp
File metadata and controls
300 lines (255 loc) · 12 KB
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
#include "PastebinPanel.h"
#include "resource.h"
#include "PluginDefinition.h"
#include "PastebinThread.h"
#include "CredentialManager.h"
#include "ScintillaUtils.h"
#include "PluginInterface.h"
#include "Notepad_plus_msgs.h"
#include "menuCmdID.h"
#include <commctrl.h>
#include <vector>
#include <string>
#include <memory>
#include <ctime>
// Defined in main.cpp
extern void registerEditingPaste(LRESULT bufferId, const PasteInfo& info);
extern void command_showSettings();
#pragma comment(lib, "comctl32.lib")
extern NppData nppData;
HWND PastebinPanel::_hDlg = NULL;
HWND PastebinPanel::_hList = NULL;
HWND PastebinPanel::_hRefreshBtn = NULL;
HWND PastebinPanel::_hOpenBtn = NULL;
HWND PastebinPanel::_hBrowserBtn = NULL;
HWND PastebinPanel::_hDeleteBtn = NULL;
std::vector<PasteInfo> PastebinPanel::_currentPastes;
void PastebinPanel::init(HWND hParent, HINSTANCE hInst) {
_hDlg = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_PASTES_PANEL), hParent, dlgProc, 0);
}
void PastebinPanel::toggleVisibility() {
if (!_hDlg) return;
bool visible = isVisible();
ShowWindow(_hDlg, visible ? SW_HIDE : SW_SHOW);
}
bool PastebinPanel::isVisible() {
return _hDlg && IsWindowVisible(_hDlg);
}
void PastebinPanel::refreshList() {
std::string devKey = retrieveApiKey();
std::string userKey = retrieveCredential(PASTEBIN_CRED_USER_KEY);
if (devKey.empty() || userKey.empty()) {
command_showSettings();
devKey = retrieveApiKey();
userKey = retrieveCredential(PASTEBIN_CRED_USER_KEY);
}
if (devKey.empty() || userKey.empty()) return;
startPastebinListThread(_hDlg, devKey, userKey);
EnableWindow(_hRefreshBtn, FALSE);
}
void PastebinPanel::updateButtonStates() {
int index = ListView_GetNextItem(_hList, -1, LVNI_SELECTED);
BOOL hasSelection = (index != -1);
EnableWindow(_hOpenBtn, hasSelection);
EnableWindow(_hBrowserBtn, hasSelection);
EnableWindow(_hDeleteBtn, hasSelection);
}
void PastebinPanel::openInEditor() {
int index = ListView_GetNextItem(_hList, -1, LVNI_SELECTED);
if (index == -1) return;
std::string devKey = retrieveApiKey();
std::string userKey = retrieveCredential(PASTEBIN_CRED_USER_KEY);
if (devKey.empty() || userKey.empty()) {
MessageBox(_hDlg, L"Please configure your API Key and login in Settings first.", L"Pastebin++", MB_OK | MB_ICONWARNING);
return;
}
EnableWindow(_hOpenBtn, FALSE);
startPastebinGetRawThread(_hDlg, devKey, userKey, _currentPastes[index].key, _currentPastes[index]);
}
void PastebinPanel::openInBrowser() {
int index = ListView_GetNextItem(_hList, -1, LVNI_SELECTED);
if (index != -1) {
ShellExecute(NULL, L"open", utf8ToWide(_currentPastes[index].url).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
}
void PastebinPanel::deleteSelectedPaste() {
int index = ListView_GetNextItem(_hList, -1, LVNI_SELECTED);
if (index == -1) return;
if (MessageBox(_hDlg, L"Are you sure you want to delete this paste?", L"Confirm Delete", MB_YESNO | MB_ICONQUESTION) == IDYES) {
std::string devKey = retrieveApiKey();
std::string userKey = retrieveCredential(PASTEBIN_CRED_USER_KEY);
startPastebinDeleteThread(_hDlg, devKey, userKey, _currentPastes[index].key);
EnableWindow(_hDeleteBtn, FALSE);
}
}
INT_PTR CALLBACK PastebinPanel::dlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_INITDIALOG: {
_hList = GetDlgItem(hDlg, IDC_PASTE_LIST);
_hRefreshBtn = GetDlgItem(hDlg, IDC_REFRESH_BTN);
_hOpenBtn = GetDlgItem(hDlg, IDC_OPEN_BTN);
_hBrowserBtn = GetDlgItem(hDlg, IDC_BROWSER_BTN);
_hDeleteBtn = GetDlgItem(hDlg, IDC_DELETE_BTN);
ListView_SetExtendedListViewStyle(_hList, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
struct { const wchar_t* title; int width; } cols[] = {
{L"Title", 160}, {L"Format", 60}, {L"Date", 90}, {L"Privacy", 55}
};
for (int i = 0; i < 4; ++i) {
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = (LPWSTR)cols[i].title;
lvc.cx = cols[i].width;
ListView_InsertColumn(_hList, i, &lvc);
}
updateButtonStates();
return (INT_PTR)TRUE;
}
case WM_SIZE:
onResize(hDlg);
return (INT_PTR)TRUE;
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDC_REFRESH_BTN: refreshList(); break;
case IDC_OPEN_BTN: openInEditor(); break;
case IDC_BROWSER_BTN: openInBrowser(); break;
case IDC_DELETE_BTN: deleteSelectedPaste(); break;
}
return (INT_PTR)TRUE;
}
case WM_NOTIFY: {
LPNMHDR lpnmh = (LPNMHDR)lParam;
if (lpnmh->hwndFrom == _hList) {
if (lpnmh->code == NM_DBLCLK) openInEditor();
else if (lpnmh->code == LVN_ITEMCHANGED) updateButtonStates();
}
break;
}
case WM_CONTEXTMENU: {
if ((HWND)wParam == _hList) {
int index = ListView_GetNextItem(_hList, -1, LVNI_SELECTED);
if (index != -1) {
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 1, L"Open in Editor");
AppendMenu(hMenu, MF_STRING, 2, L"Open in Browser");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, 3, L"Delete");
POINT pt;
GetCursorPos(&pt);
int sel = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hDlg, NULL);
DestroyMenu(hMenu);
if (sel == 1) openInEditor();
else if (sel == 2) openInBrowser();
else if (sel == 3) deleteSelectedPaste();
}
}
break;
}
case WM_PASTEBIN_LIST_FINISHED: {
std::unique_ptr<ListResult> result((ListResult*)wParam);
EnableWindow(_hRefreshBtn, TRUE);
if (result->success) {
populateList(result->pastes);
} else if (result->error.find("Not logged in") != std::string::npos) {
// Auth failure — open settings so the user can correct credentials.
// The settings dialog attempts login on OK; only retry the list fetch
// if it produced a fresh user key (login succeeded).
command_showSettings();
std::string devKey = retrieveApiKey();
std::string userKey = retrieveCredential(PASTEBIN_CRED_USER_KEY);
if (!devKey.empty() && !userKey.empty()) {
startPastebinListThread(hDlg, devKey, userKey);
EnableWindow(_hRefreshBtn, FALSE);
}
} else {
MessageBox(hDlg, (L"Failed to fetch pastes: " + utf8ToWide(result->error)).c_str(), L"Pastebin++", MB_OK | MB_ICONERROR);
}
updateButtonStates();
return (INT_PTR)TRUE;
}
case WM_PASTEBIN_GET_RAW_FINISHED: {
EnableWindow(_hOpenBtn, TRUE);
std::unique_ptr<RawResult> result((RawResult*)wParam);
if (result->success) {
// Build a temp file path so NPP uses the paste title as the tab name
// and can apply syntax highlighting by extension.
// Path: %TEMP%\PastebinPlusPlus\<sanitized_title>_<pastekey>.<ext>
wchar_t tempBase[MAX_PATH];
GetTempPathW(MAX_PATH, tempBase);
std::wstring dir = std::wstring(tempBase) + L"PastebinPlusPlus\\";
CreateDirectoryW(dir.c_str(), NULL); // ok if already exists
std::wstring titleW = utf8ToWide(result->meta.title);
std::wstring ext = extractTitleExt(titleW);
std::wstring safeTitle = sanitizePasteTitle(titleW);
if (ext.empty()) ext = pasteFormatToExt(result->meta.format);
std::wstring filePath = dir + safeTitle + L"_" + utf8ToWide(result->pasteKey) + ext;
// Write UTF-8 content to the temp file
HANDLE hFile = CreateFileW(filePath.c_str(), GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD written;
WriteFile(hFile, result->content.c_str(), (DWORD)result->content.size(), &written, NULL);
CloseHandle(hFile);
}
// Open the file in NPP — this sets the tab title to the filename
::SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, (LPARAM)filePath.c_str());
// Get the buffer ID NPP just assigned to the opened file
LRESULT bufferId = ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0);
// Mark the document clean so NPP doesn't nag about unsaved changes
int which = 0;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
if (curScintilla)
::SendMessage(curScintilla, SCI_SETSAVEPOINT, 0, 0);
// Register so "Paste to Pastebin" treats this buffer as an existing paste
registerEditingPaste(bufferId, result->meta);
} else {
std::wstring msg = L"Failed to download paste: " + utf8ToWide(result->error);
MessageBox(hDlg, msg.c_str(), L"Pastebin++", MB_OK | MB_ICONERROR);
}
return (INT_PTR)TRUE;
}
case WM_PASTEBIN_DELETE_FINISHED: {
std::unique_ptr<DeleteResult> result((DeleteResult*)wParam);
EnableWindow(_hDeleteBtn, TRUE);
if (result->success) refreshList();
else MessageBox(hDlg, (L"Failed to delete paste: " + utf8ToWide(result->error)).c_str(), L"Error", MB_OK | MB_ICONERROR);
return (INT_PTR)TRUE;
}
}
return (INT_PTR)FALSE;
}
void PastebinPanel::onResize(HWND hDlg) {
RECT rc;
GetClientRect(hDlg, &rc);
int bw = (rc.right - 10) / 4;
MoveWindow(_hRefreshBtn, 5, 5, bw - 2, 22, TRUE);
MoveWindow(_hOpenBtn, 5 + bw, 5, bw - 2, 22, TRUE);
MoveWindow(_hBrowserBtn, 5 + 2 * bw, 5, bw - 2, 22, TRUE);
MoveWindow(_hDeleteBtn, 5 + 3 * bw, 5, bw - 2, 22, TRUE);
MoveWindow(_hList, 5, 32, rc.right - 10, rc.bottom - 37, TRUE);
}
void PastebinPanel::populateList(const std::vector<PasteInfo>& pastes) {
ListView_DeleteAllItems(_hList);
_currentPastes = pastes;
for (int i = 0; i < (int)pastes.size(); ++i) {
LVITEM lvi = { 0 };
lvi.mask = LVIF_TEXT;
lvi.iItem = i;
std::wstring wTitle = utf8ToWide(pastes[i].title);
lvi.pszText = (LPWSTR)wTitle.c_str();
ListView_InsertItem(_hList, &lvi);
ListView_SetItemText(_hList, i, 1, (LPWSTR)utf8ToWide(pastes[i].format).c_str());
// Format Date
time_t t = std::stoll(pastes[i].date.empty() ? "0" : pastes[i].date);
struct tm tm_info;
localtime_s(&tm_info, &t);
wchar_t dateBuf[32];
wcsftime(dateBuf, 32, L"%Y-%m-%d", &tm_info);
ListView_SetItemText(_hList, i, 2, dateBuf);
// Format Privacy
const wchar_t* priv = L"Public";
if (pastes[i].privacy == "1") priv = L"Unlisted";
else if (pastes[i].privacy == "2") priv = L"Private";
ListView_SetItemText(_hList, i, 3, (LPWSTR)priv);
}
}