-
Notifications
You must be signed in to change notification settings - Fork 37
/
ExclusionsDialog.cpp
219 lines (185 loc) · 10.1 KB
/
ExclusionsDialog.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
#include "ExclusionsDialog.h"
#include "resource.h"
#include "Tools.h"
#include "YouTubeAPI.h"
#include "GdiPlusImageLoader.h"
#include "AIMPYouTube.h"
#include "OptionsDialog.h"
#include <Commctrl.h>
#include <unordered_map>
#include <memory>
typedef __declspec(dllimport) HRESULT(__stdcall *SetWindowThemeFuncPtr)(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList);
HRESULT SetExplorerTheme(HWND hwnd) {
static HMODULE DLL = LoadLibrary(L"UxTheme.dll");
static SetWindowThemeFuncPtr setWindowTheme = nullptr;
if (DLL) {
if (!setWindowTheme)
setWindowTheme = (SetWindowThemeFuncPtr)GetProcAddress(DLL, "SetWindowTheme");
if (setWindowTheme)
return setWindowTheme(hwnd, L"Explorer", NULL);
}
return E_FAIL;
}
extern HINSTANCE g_hInst;
#define WM_SUBCLASSINIT WM_USER + 4
void ExclusionsDialog::Show(HWND parent) {
if (!parent)
parent = Plugin::instance()->GetMainWindowHandle();
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_EXCLUSIONS), parent, DlgProc);
}
BOOL CALLBACK ExclusionsDialog::DlgProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CLOSE:
EndDialog(hwnd, IDCANCEL);
break;
case WM_NOTIFY: {
auto lv = GetDlgItem(hwnd, IDC_LISTVIEW);
if (LPNMHDR(lParam)->hwndFrom == lv) {
switch (LPNMHDR(lParam)->code) {
case NM_RCLICK: {
int i = ListView_GetNextItem(lv, -1, LVNI_SELECTED);
if (i != -1) {
POINT cursor;
GetCursorPos(&cursor);
HMENU popup = CreatePopupMenu();
HMENU playlistsMenu = CreatePopupMenu();
int p = 0;
std::unordered_map<UINT_PTR, IAIMPPlaylist *> plMap;
Plugin::instance()->ForAllPlaylists([&](IAIMPPlaylist *pl, const std::wstring &name) {
UINT_PTR id = 0x57d100 + p++;
AppendMenu(playlistsMenu, MF_STRING, id, name.c_str());
plMap[id] = pl;
});
AppendMenu(popup, MF_STRING, 0x57d001, Plugin::instance()->Lang(L"YouTube.Exclusions\\Remove").c_str());
AppendMenu(popup, MF_STRING | MF_POPUP, (UINT_PTR)playlistsMenu, Plugin::instance()->Lang(L"YouTube.Exclusions\\RemoveAndAddToPlaylist").c_str());
AppendMenu(popup, MF_SEPARATOR, 0, NULL);
AppendMenu(popup, MF_STRING, 0x57d003, Plugin::instance()->Lang(L"YouTube.Menu\\OpenInBrowser").c_str());
auto result = TrackPopupMenu(popup, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, cursor.x, cursor.y, 0, hwnd, NULL);
DestroyMenu(playlistsMenu);
DestroyMenu(popup);
while (i != -1) {
LVITEM selectedItem;
selectedItem.mask = LVIF_PARAM;
selectedItem.iItem = i;
ListView_GetItem(lv, (LVITEM *)&selectedItem);
if (auto ti = reinterpret_cast<Config::TrackInfo *>(selectedItem.lParam)) {
switch (result) {
case 0x57d001: // remove from exclusions
Config::TrackExclusions.erase(ti->Id);
ListView_DeleteItem(lv, i--);
break;
case 0x57d003: // open in web browser
if (!ti->Permalink.empty())
ShellExecute(Plugin::instance()->GetMainWindowHandle(), L"open", ti->Permalink.c_str(), NULL, NULL, SW_SHOWNORMAL);
break;
default:
if (auto pl = plMap[result]) {
Config::TrackExclusions.erase(ti->Id);
auto state = std::make_shared<YouTubeAPI::LoadingState>();
std::wstring url = L"https://www.googleapis.com/youtube/v3/videos?part=contentDetails%2Csnippet&hl=" + Plugin::instance()->Lang(L"YouTube\\YouTubeLang") + L"&id=" + ti->Id;
YouTubeAPI::LoadFromUrl(url, pl, state);
ListView_DeleteItem(lv, i--);
}
break;
}
}
i = ListView_GetNextItem(lv, i, LVNI_SELECTED);
}
Config::SaveExtendedConfig();
}
} break;
} break;
}
} break;
case WM_INITDIALOG: {
SendDlgItemMessage(hwnd, IDC_CREATENEW, BM_SETCHECK, BST_CHECKED, NULL);
GdiPlusImageLoader icon(IDB_ICON, L"PNG");
HICON hiIcon;
HBITMAP hbIcon;
icon->GetHICON(&hiIcon);
icon->GetHBITMAP(NULL, &hbIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hiIcon);
DeleteObject(hiIcon);
SetWindowText (hwnd, Plugin::instance()->Lang(L"YouTube.Exclusions\\Title").c_str());
SetDlgItemText(hwnd, IDOK, Plugin::instance()->Lang(L"YouTube.Exclusions\\OK").c_str());
SetDlgItemText(hwnd, IDC_EXCLUSIONSGROUPBOX, Plugin::instance()->Lang(L"YouTube.Exclusions\\Header").c_str());
auto lv = GetDlgItem(hwnd, IDC_LISTVIEW);
SetWindowSubclass(GetDlgItem(hwnd, IDC_EXCLUSIONSGROUPBOX), OptionsDialog::GroupBoxProc, 0, 0); SendDlgItemMessage(hwnd, IDC_EXCLUSIONSGROUPBOX, WM_SUBCLASSINIT, 0, 0);
SetWindowSubclass(GetDlgItem(hwnd, IDC_LISTVIEW), ListViewProc, 0, 0);
ListView_SetExtendedListViewStyle(lv, LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
SetExplorerTheme(lv);
LVCOLUMN lvc;
lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVIF_IMAGE;
static auto h1 = Plugin::instance()->Lang(L"YouTube.Exclusions\\ListHeader", 0);
static auto h2 = Plugin::instance()->Lang(L"YouTube.Exclusions\\ListHeader", 1);
lvc.iSubItem = 0; lvc.cx = 450; lvc.pszText = const_cast<LPWSTR>(h1.c_str()); ListView_InsertColumn(lv, 0, &lvc);
lvc.iSubItem = 1; lvc.cx = 50; lvc.pszText = const_cast<LPWSTR>(h2.c_str()); ListView_InsertColumn(lv, 1, &lvc);
HIMAGELIST himl = ImageList_Create(24, 24, ILC_COLOR32, 3, 0);
ImageList_Add(himl, hbIcon, (HBITMAP)NULL);
DeleteObject(hbIcon);
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
int i = 0;
wchar_t buf[16];
for (auto x : Config::TrackExclusions) {
if (auto ti = Tools::TrackInfo(x)) {
if (ti->Duration >= 0) {
lvi.pszText = const_cast<wchar_t *>(ti->Name.c_str());
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.iImage = 0;
lvi.lParam = reinterpret_cast<LPARAM>(ti);
ListView_InsertItem(lv, &lvi);
unsigned int hours = floor(ti->Duration / 3600.0);
if (hours > 0) {
swprintf_s(buf, L"%d:%02d:%02d", hours, (uint32_t)floor(fmod(ti->Duration, 3600.0) / 60.0), (uint32_t)floor(fmod(ti->Duration, 60.0)));
} else {
swprintf_s(buf, L"%d:%02d", (uint32_t)floor(fmod(ti->Duration, 3600.0) / 60.0), (uint32_t)floor(fmod(ti->Duration, 60.0)));
}
ListView_SetItemText(lv, i, 1, buf);
}
}
}
ListView_SetImageList(lv, himl, LVSIL_SMALL);
ListView_SetColumnWidth(lv, 1, LVSCW_AUTOSIZE_USEHEADER);
SetFocus(lv);
} break;
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDOK: {
EndDialog(hwnd, wParam);
} break;
}
} break;
default: return FALSE;
}
return TRUE;
}
LRESULT CALLBACK ExclusionsDialog::ListViewProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (uMsg) {
case WM_KEYUP:
switch (wParam) {
case VK_DELETE: {
int i = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
if (i != -1) {
while (i != -1) {
LVITEM selectedItem;
selectedItem.mask = LVIF_PARAM;
selectedItem.iItem = i;
ListView_GetItem(hWnd, (LVITEM *)&selectedItem);
if (auto ti = reinterpret_cast<Config::TrackInfo *>(selectedItem.lParam)) {
Config::TrackExclusions.erase(ti->Id);
ListView_DeleteItem(hWnd, i--);
}
i = ListView_GetNextItem(hWnd, i, LVNI_SELECTED);
}
Config::SaveExtendedConfig();
}
} break;
} break;
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, ListViewProc, uIdSubclass);
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}