aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_wire/Preferences.cpp
blob: ec7a20ebf65d399a521aa86c69ee098b94719758 (plain) (blame)
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
#include "main.h"
#include "api__ml_wire.h"
#include "../winamp/wa_ipc.h"
#include "Defaults.h"
#include "UpdateTime.h"
#include "UpdateAutoDownload.h"
#include "./cloud.h"
#include <shlobj.h>

extern Cloud cloud;

void Preferences_Init(HWND hwndDlg)
{
	WCHAR szBuffer[256] = {0};
	for (int i = 0;i < Update::TIME_NUMENTRIES;i++)
	{
		const wchar_t *str = Update::GetTitle(i, szBuffer, ARRAYSIZE(szBuffer));
		SendMessage(GetDlgItem(hwndDlg, IDC_UPDATELIST), CB_ADDSTRING, 0, (LPARAM) str);
	}
	int selection = Update::GetSelection(updateTime, autoUpdate);
	SendMessage(GetDlgItem(hwndDlg, IDC_UPDATELIST), CB_SETCURSEL, selection, 0);

	WCHAR adBuffer[256] = {0};
	for (int i = 0;i < UpdateAutoDownload::AUTODOWNLOAD_NUMENTRIES;i++)
	{
		const wchar_t *str = UpdateAutoDownload::GetTitle(i, adBuffer, ARRAYSIZE(adBuffer));
		SendMessage(GetDlgItem(hwndDlg, IDC_AUTODOWNLOADLIST), CB_ADDSTRING, 0, (LPARAM) str);
	}
	selection = UpdateAutoDownload::GetSelection(autoDownloadEpisodes, autoDownload);
	SendMessage(GetDlgItem(hwndDlg, IDC_AUTODOWNLOADLIST), CB_SETCURSEL, selection, 0);

	SetDlgItemText(hwndDlg, IDC_DOWNLOADLOCATION, defaultDownloadPath);
	SetDlgItemText(hwndDlg, IDC_DIRECTORYURL, serviceUrl);
	CheckDlgButton(hwndDlg, IDC_UPDATEONLAUNCH, updateOnLaunch?BST_CHECKED:BST_UNCHECKED);
}

BOOL CALLBACK browseEnumProc(HWND hwnd, LPARAM lParam)
{
	wchar_t cl[32] = {0};
	GetClassNameW(hwnd, cl, ARRAYSIZE(cl));
	if (!lstrcmpiW(cl, WC_TREEVIEW))
	{
		PostMessage(hwnd, TVM_ENSUREVISIBLE, 0, (LPARAM)TreeView_GetSelection(hwnd));
		return FALSE;
	}

	return TRUE;
}

int CALLBACK WINAPI BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	if(uMsg == BFFM_INITIALIZED)
	{
		SendMessageW(hwnd, BFFM_SETSELECTIONW, 1, (LPARAM)defaultDownloadPath);

		// this is not nice but it fixes the selection not working correctly on all OSes
		EnumChildWindows(hwnd, browseEnumProc, 0);
	}
	return 0;
}

void Preferences_Browse(HWND hwndDlg)
{
	wchar_t folder[MAX_PATH] = {0};
	BROWSEINFO browse = {0};
	lstrcpyn(folder, defaultDownloadPath, MAX_PATH);
	browse.hwndOwner = hwndDlg;
	browse.lpszTitle = WASABI_API_LNGSTRINGW(IDS_CHOOSE_FOLDER);
	browse.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
	browse.lpfn = BrowseCallbackProc;
	LPITEMIDLIST itemList = SHBrowseForFolder(&browse);
	if (itemList)
	{
		SHGetPathFromIDList(itemList, folder);
		lstrcpyn(defaultDownloadPath, folder, MAX_PATH);
		SetWindowText(GetDlgItem(hwndDlg, IDC_DOWNLOADLOCATION), folder);
		LPMALLOC malloc;
		SHGetMalloc(&malloc);
		malloc->Free(itemList);
	}
}

void Preferences_UpdateOnLaunch( HWND hwndDlg )
{
	updateOnLaunch = ( IsDlgButtonChecked( hwndDlg, IDC_UPDATEONLAUNCH ) == BST_CHECKED );
}

void Preferences_UpdateList(HWND hwndDlg)
{
	LRESULT timeSelection;
	timeSelection = SendMessage(GetDlgItem(hwndDlg, IDC_UPDATELIST), CB_GETCURSEL, 0, 0);
	if (timeSelection != CB_ERR)
	{
		autoUpdate = Update::GetAutoUpdate(timeSelection);
		updateTime = Update::GetTime(timeSelection);
		if ( autoUpdate )
			cloud.Pulse();	// update the waitable timer
	}
}

void Preferences_AutoDownloadList(HWND hwndDlg)
{
	LRESULT episodeSelection;
	episodeSelection = SendMessage(GetDlgItem(hwndDlg, IDC_AUTODOWNLOADLIST), CB_GETCURSEL, 0, 0);
	if (episodeSelection != CB_ERR)
	{
		autoDownload         = UpdateAutoDownload::GetAutoDownload(episodeSelection);
		autoDownloadEpisodes = UpdateAutoDownload::GetAutoDownloadEpisodes(episodeSelection);
	}
}

BOOL CALLBACK PreferencesDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
		Preferences_Init(hwndDlg);
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_BROWSE:
			Preferences_Browse(hwndDlg);
			break;
		case IDC_UPDATELIST:
			Preferences_UpdateList(hwndDlg);
			break;
		case IDC_AUTODOWNLOADLIST:
			Preferences_AutoDownloadList(hwndDlg);
			break;
		case IDC_UPDATEONLAUNCH:
			Preferences_UpdateOnLaunch(hwndDlg);
			break;
		case IDC_DOWNLOADLOCATION:
			if (HIWORD(wParam) == EN_CHANGE)
			{
				GetDlgItemText(hwndDlg, IDC_DOWNLOADLOCATION, defaultDownloadPath, MAX_PATH);
				if (!PathFileExists(defaultDownloadPath))
				{
					BuildDefaultDownloadPath(plugin.hwndWinampParent);
					SetDlgItemText(hwndDlg, IDC_DOWNLOADLOCATION, defaultDownloadPath);
				}
			}
			break;
		case IDC_DIRECTORYURL:
			if (HIWORD(wParam) == EN_CHANGE)
			{
				GetDlgItemText(hwndDlg, IDC_DIRECTORYURL, serviceUrl, ARRAYSIZE(serviceUrl));
			}
			break;
		}
		break;
	case WM_DESTROY:
		// ensure we save the changed settings
		SaveAll(true);
		break;
	}

	return 0;
}