-
Notifications
You must be signed in to change notification settings - Fork 37
/
YTAPIDialog.cpp
120 lines (102 loc) · 5 KB
/
YTAPIDialog.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
#include "YTAPIDialog.h"
#include "resource.h"
#include "AIMPYouTube.h"
extern HINSTANCE g_hInst;
void YTAPIDialog::Show(OptionsDialog *dialog) {
HWND parent = Plugin::instance()->GetMainWindowHandle();
DialogBoxParamW(g_hInst, MAKEINTRESOURCE(IDD_YTAPISETTINGS), parent, DlgProc, (LPARAM)dialog);
}
BOOL CALLBACK YTAPIDialog::DlgProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
static OptionsDialog* dialog = nullptr;
switch (Msg) {
case WM_CLOSE:
EndDialog(hwnd, IDCANCEL);
break;
case WM_INITDIALOG: {
dialog = (OptionsDialog*)lParam;
std::wstring key = Config::GetString(L"YouTubeKey", L"");
std::wstring clientID = Config::GetString(L"YouTubeClientID", L"");
std::wstring clientSecret = Config::GetString(L"YouTubeClientSecret", L"");
SendDlgItemMessage(hwnd, IDC_YTAPIKEY, WM_SETTEXT, 0, (LPARAM)key.c_str());
SendDlgItemMessage(hwnd, IDC_YTAPICLIENTID, WM_SETTEXT, 0, (LPARAM)clientID.c_str());
SendDlgItemMessage(hwnd, IDC_YTAPICLIENTSECRET, WM_SETTEXT, 0, (LPARAM)clientSecret.c_str());
SetWindowText(hwnd, Plugin::instance()->Lang(L"YouTube.YTAPI\\Title").c_str());
SetDlgItemText(hwnd, IDC_YTAPIKEYTEXT, Plugin::instance()->Lang(L"YouTube.YTAPI\\Key").c_str());
SetDlgItemText(hwnd, IDC_YTAPICLIENTIDTEXT, Plugin::instance()->Lang(L"YouTube.YTAPI\\ClientID").c_str());
SetDlgItemText(hwnd, IDC_YTAPICLIENTSECRETTEXT, Plugin::instance()->Lang(L"YouTube.YTAPI\\ClientSecret").c_str());
SetDlgItemText(hwnd, IDOK, Plugin::instance()->Lang(L"YouTube.YTAPI\\OK").c_str());
SetDlgItemText(hwnd, IDCANCEL, Plugin::instance()->Lang(L"YouTube.YTAPI\\Cancel").c_str());
SetFocus(GetDlgItem(hwnd, IDC_YTAPIKEY));
} break;
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDOK: {
wchar_t buf[1024];
GetDlgItemText(hwnd, IDC_YTAPIKEY, buf, 1024);
std::wstring key(buf);
GetDlgItemText(hwnd, IDC_YTAPICLIENTID, buf, 1024);
std::wstring clientID(buf);
GetDlgItemText(hwnd, IDC_YTAPICLIENTSECRET, buf, 1024);
std::wstring clientSecret(buf);
std::wstring oldClientID = Config::GetString(L"YouTubeClientID", L"");
if (key.empty()) {
Config::Delete(L"YouTubeKey");
} else {
Config::SetString(L"YouTubeKey", key);
}
if (clientID.empty()) {
Config::Delete(L"YouTubeClientID");
Config::Delete(L"YouTubeClientSecret");
} else {
Config::SetString(L"YouTubeClientID", clientID);
Config::SetString(L"YouTubeClientSecret", clientSecret);
}
if (clientID != oldClientID) {
dialog->LogoutProfile();
}
EndDialog(hwnd, wParam);
} break;
case IDCANCEL:
EndDialog(hwnd, wParam);
break;
}
switch (HIWORD(wParam)) {
case EN_CHANGE: {
wchar_t buf[1024];
GetDlgItemText(hwnd, IDC_YTAPIKEY, buf, 1024);
std::wstring key(buf);
GetDlgItemText(hwnd, IDC_YTAPICLIENTID, buf, 1024);
std::wstring clientID(buf);
GetDlgItemText(hwnd, IDC_YTAPICLIENTSECRET, buf, 1024);
std::wstring clientSecret(buf);
UpdateHint(hwnd, key, clientID, clientSecret);
} break;
}
} break;
default: return FALSE;
}
return TRUE;
}
void YTAPIDialog::UpdateHint(HWND hwnd, std::wstring &key, std::wstring &clientID, std::wstring &clientSecret) {
// Only a key or key and client ID and secret are required
std::wstring langKey;
bool isValid = true;
bool keySet = !key.empty();
bool clientIDSet = !clientID.empty();
bool clientSecretSet = !clientSecret.empty();
if (!keySet && !clientIDSet && !clientSecretSet) {
langKey = L"YouTube.YTAPI\\HintNone";
} else if ((clientIDSet || clientSecretSet) && !keySet) {
langKey = L"YouTube.YTAPI\\HintMissingKey";
isValid = false;
} else if ((clientIDSet || clientSecretSet) && (!clientIDSet || !clientSecretSet)) {
langKey = L"YouTube.YTAPI\\HintIncompleteOAuth";
isValid = false;
} else if (!clientIDSet && !clientSecretSet && keySet) {
langKey = L"YouTube.YTAPI\\HintSetKey";
} else {
langKey = L"YouTube.YTAPI\\HintSetAll";
}
SetDlgItemText(hwnd, IDC_YTAPIUSAGE, Plugin::instance()->Lang(langKey).c_str());
EnableWindow(GetDlgItem(hwnd, IDOK), isValid);
}