summaryrefslogtreecommitdiff
path: root/plugins/Folders/src/dlg_handlers.cpp
blob: ea47820fdb1853128b4f15e8313460bfabe2e8db (plain)
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
#include "stdafx.h"

PFolderItem lastItem = nullptr;

static int bInitializing = 0;

static PFolderItem GetSelectedItem(HWND hWnd)
{
	int index = SendDlgItemMessage(hWnd, IDC_FOLDERS_ITEMS_LIST, LB_GETCURSEL, 0, 0);
	if (index == LB_ERR)
		return nullptr;

	return (PFolderItem)SendDlgItemMessage(hWnd, IDC_FOLDERS_ITEMS_LIST, LB_GETITEMDATA, index, 0);
}

static void GetEditText(HWND hWnd, wchar_t *buffer, int size)
{
	GetWindowText(GetDlgItem(hWnd, IDC_FOLDER_EDIT), buffer, size);
}

static void SetEditText(HWND hWnd, const wchar_t *buffer)
{
	bInitializing = 1;
	SetDlgItemText(hWnd, IDC_FOLDER_EDIT, buffer);
	bInitializing = 0;
}

static int ContainsSection(HWND hWnd, const wchar_t *section)
{
	int index = SendDlgItemMessage(hWnd, IDC_FOLDERS_SECTIONS_LIST, LB_FINDSTRINGEXACT, -1, (LPARAM)section);
	return (index != LB_ERR);
}

static void LoadRegisteredFolderSections(HWND hWnd)
{
	HWND hwndList = GetDlgItem(hWnd, IDC_FOLDERS_SECTIONS_LIST);

	for (int i = 0; i < lstRegisteredFolders.getCount(); i++) {
		CFolderItem &tmp = lstRegisteredFolders[i];
		wchar_t *translated = mir_a2u(tmp.GetSection());
		if (!ContainsSection(hWnd, TranslateW(translated))) {
			int idx = SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)TranslateW(translated));
			SendMessage(hwndList, LB_SETITEMDATA, idx, (LPARAM)tmp.GetSection());
		}

		mir_free(translated);
	}
}

static void LoadRegisteredFolderItems(HWND hWnd)
{
	int idx = SendDlgItemMessage(hWnd, IDC_FOLDERS_SECTIONS_LIST, LB_GETCURSEL, 0, 0);
	if (idx == LB_ERR)
		return;

	char* szSection = (char*)SendDlgItemMessage(hWnd, IDC_FOLDERS_SECTIONS_LIST, LB_GETITEMDATA, idx, 0);

	HWND hwndItems = GetDlgItem(hWnd, IDC_FOLDERS_ITEMS_LIST);
	SendMessage(hwndItems, LB_RESETCONTENT, 0, 0);

	for (int i = 0; i < lstRegisteredFolders.getCount(); i++) {
		CFolderItem &item = lstRegisteredFolders[i];
		if (!mir_strcmp(szSection, item.GetSection())) {
			idx = SendMessage(hwndItems, LB_ADDSTRING, 0, (LPARAM)TranslateW(item.GetUserName()));
			SendMessage(hwndItems, LB_SETITEMDATA, idx, (LPARAM)&item);
		}
	}
	SendMessage(hwndItems, LB_SETCURSEL, 0, 0); //select the first item
	PostMessage(hWnd, WM_COMMAND, MAKEWPARAM(IDC_FOLDERS_ITEMS_LIST, LBN_SELCHANGE), 0); //tell the dialog to refresh the preview
}

static void RefreshPreview(HWND hWnd)
{
	wchar_t tmp[MAX_FOLDER_SIZE];
	GetEditText(hWnd, tmp, MAX_FOLDER_SIZE);
	SetDlgItemText(hWnd, IDC_PREVIEW_EDIT, ExpandPath(tmp));
}

static void LoadItem(HWND hWnd, PFolderItem item)
{
	if (!item)
		return;

	SetEditText(hWnd, item->GetFormat());
	RefreshPreview(hWnd);
}

static void SaveItem(HWND hWnd, PFolderItem item, int bEnableApply)
{
	if (!item)
		return;

	wchar_t buffer[MAX_FOLDER_SIZE];
	GetEditText(hWnd, buffer, _countof(buffer));
	item->SetFormat(buffer);

	if (bEnableApply)
		SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
}

static int ChangesNotSaved(HWND hWnd, PFolderItem item)
{
	if (!item)
		return 0;

	wchar_t buffer[MAX_FOLDER_SIZE];
	GetEditText(hWnd, buffer, MAX_FOLDER_SIZE);
	return mir_wstrcmp(item->GetFormat(), buffer) != 0;
}

static void CheckForChanges(HWND hWnd, int bNeedConfirmation = 1)
{
	if (ChangesNotSaved(hWnd, lastItem))
		if ((!bNeedConfirmation) || MessageBox(hWnd, TranslateT("Some changes weren't saved. Apply the changes now?"), TranslateT("Changes not saved"), MB_YESNO | MB_ICONINFORMATION) == IDYES)
			SaveItem(hWnd, lastItem, TRUE);
}

/************************************** DIALOG HANDLERS *************************************/

static INT_PTR CALLBACK DlgProcVariables(HWND hWnd, UINT msg, WPARAM wParam, LPARAM)
{
	wchar_t tszMessage[2048];

	switch (msg) {
	case WM_INITDIALOG:
		mir_snwprintf(tszMessage, L"%s\r\n%s\r\n\r\n%s\t\t%s\r\n%%miranda_path%%\t\t%s\r\n%%profile_path%%\t\t%s\r\n\t\t\t%s\r\n%%current_profile%%\t\t%s\r\n\t\t\t%s\r\n\r\n\r\n%s\r\n%s\r\n%s\r\n%s\r\n%s\r\n%s\r\n\r\n%s\r\n%s\r\n%s\r\n%%miranda_path%%\t\t\t%s\r\n%%profile_path%%\t\t\t%s\r\n%%current_profile%%\t\t\t%s\r\n%%temp%%\t\t\t\t%s\r\n%%profile_path%%\\%%current_profile%%\t%s\r\n%%miranda_path%%\\plugins\\config\t%s\r\n'   %%miranda_path%%\\\\\\\\     '\t\t%s\r\n\r\n%s",
			TranslateT("Don't forget to click on Apply to save the changes. If you don't then the changes won't"),
			TranslateT("be saved to the database, they will only be valid for this session."),
			TranslateT("Variable string"),
			TranslateT("What it expands to:"),
			TranslateT("Expands to your Miranda path (e.g., c:\\program files\\miranda ng)."),
			TranslateT("Expands to your profile path - the value found in mirandaboot.ini,"),
			TranslateT("ProfileDir section (usually inside Miranda's folder)."),
			TranslateT("Expands to your current profile name without the extenstion."),
			TranslateT("(e.g., default if your your profile is default.dat)."),
			TranslateT("Environment variables"),
			TranslateT("The plugin can also expand environment variables; the variables are specified like in any other"),
			TranslateT("program that can use environment variables, i.e., %<env variable>%."),
			TranslateT("Note: Environment variables are expanded before any Miranda variables. So if you have, for"),
			TranslateT("example, %profile_path% defined as a system variable then it will be expanded to that value"),
			TranslateT("instead of expanding to Miranda's profile path."),
			TranslateT("Examples:"),
			TranslateT("If the value for the ProfileDir inside mirandaboot.ini, ProfileDir section is '.\\profiles\\', current"),
			TranslateT("profile is 'default.dat' and Miranda path is 'c:\\program files\\miranda ng\\' then:"),
			TranslateT("will expand to 'c:\\program files\\miranda ng'"),
			TranslateT("will expand to 'c:\\program files\\miranda ng\\profiles'"),
			TranslateT("will expand to 'default'"),
			TranslateT("will expand to the temp folder of the current user."),
			TranslateT("will expand to 'c:\\program files\\miranda ng\\profiles\\default'"),
			TranslateT("will expand to 'c:\\program files\\miranda ng\\plugins\\config'"),
			TranslateT("will expand to 'c:\\program files\\miranda ng'"),
			TranslateT("Notice that the spaces at the beginning and the end of the string are trimmed, as well as the last."));
		SetDlgItemText(hWnd, IDC_HELP_RICHEDIT, tszMessage);
		TranslateDialogDefault(hWnd);
		break;

	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDCLOSE)
			DestroyWindow(hWnd);
		break;
	}

	return 0;
}

static INT_PTR CALLBACK DlgProcOpts(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PFolderItem item;

	switch (msg) {
	case WM_INITDIALOG:
		lastItem = nullptr;
		TranslateDialogDefault(hWnd);
		bInitializing = 1;
		LoadRegisteredFolderSections(hWnd);
		bInitializing = 0;
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_FOLDER_EDIT:
			switch (HIWORD(wParam)) {
			case EN_CHANGE:
				RefreshPreview(hWnd);
				if (!bInitializing)
					SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0); //show the apply button.
			}
			break;

		case IDC_REFRESH_BUTTON:
			RefreshPreview(hWnd);
			break;

		case IDC_HELP_BUTTON:
			ShowWindow(CreateDialog(hInstance, MAKEINTRESOURCE(IDD_VARIABLES_HELP), hWnd, DlgProcVariables), SW_SHOW);
			break;

		case IDC_FOLDERS_SECTIONS_LIST:
			switch (HIWORD(wParam)) {
			case LBN_SELCHANGE:
				CheckForChanges(hWnd);
				LoadRegisteredFolderItems(hWnd);
				lastItem = nullptr;
				SetEditText(hWnd, L"");
				RefreshPreview(hWnd);
			}
			break;

		case IDC_FOLDERS_ITEMS_LIST:
			switch (HIWORD(wParam)) {
			case LBN_SELCHANGE:
				item = GetSelectedItem(hWnd);
				if (item != nullptr) {
					CheckForChanges(hWnd);
					LoadItem(hWnd, item);
				}
				lastItem = item;
			}
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				item = GetSelectedItem(hWnd);
				if (item) {
					SaveItem(hWnd, item, FALSE);
					LoadItem(hWnd, item);
				}

				for (int i = 0; i < lstRegisteredFolders.getCount(); i++)
					lstRegisteredFolders[i].Save();
				CallPathChangedEvents();
			}
		}
		break;
	}

	return 0;
}

static int OnOptionsInitialize(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 100000000;
	odp.hInstance = hInstance;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_FOLDERS);
	odp.szTitle.a = LPGEN("Folders");
	odp.szGroup.a = LPGEN("Customize");
	odp.flags = ODPF_BOLDGROUPS;
	odp.pfnDlgProc = DlgProcOpts;
	Options_AddPage(wParam, &odp);
	return 0;
}

void InitOptions()
{
	HookEvent(ME_OPT_INITIALISE, OnOptionsInitialize);
}