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

PFolderItem lastItem = nullptr;

CDlgBase *pHelpDialog = nullptr;

class CHelpDialog : public CDlgBase
{
	CCtrlButton m_btnClose;
	CCtrlRichEdit m_redtHelp;

public:
	CHelpDialog() :
		CDlgBase(g_plugin, IDD_VARIABLES_HELP),
		m_btnClose(this, IDCLOSE),
		m_redtHelp(this, IDC_HELP_RICHEDIT)
	{
		m_btnClose.OnClick = Callback(this, &CHelpDialog::OnCloseClick);
	}

	bool OnInitDialog() override
	{
		wchar_t tszMessage[2048];
		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."));
		m_redtHelp.SetText(tszMessage);
		return true;
	}

	bool OnClose() override
	{
		if (pHelpDialog == this)
			pHelpDialog = nullptr;
		return true;
	}

	void OnCloseClick(CCtrlBase*)
	{
		Close();
	}
};

class COptDialog : public CDlgBase
{
	CCtrlListBox m_lbSections, m_lbItems;
	CCtrlEdit m_edtPreview, m_edtEdit;
	CCtrlButton m_btnRefresh, m_btnHelp;

	PFolderItem GetSelectedItem()
	{
		int index = m_lbItems.GetCurSel();
		if (index == LB_ERR)
			return nullptr;

		return (PFolderItem)m_lbItems.GetItemData(index);
	}

	int ContainsSection(const wchar_t *section)
	{
		int index = m_lbSections.SendMsg(LB_FINDSTRINGEXACT, -1, (LPARAM)section);
		return (index != LB_ERR);
	}

	void LoadRegisteredFolderSections()
	{
		for (auto &it : lstRegisteredFolders) {
			wchar_t *translated = mir_a2u(it->GetSection());
			if (!ContainsSection(TranslateW(translated))) {
				int idx = m_lbSections.AddString(TranslateW(translated), 0);
				m_lbSections.SetItemData(idx, (LPARAM)it->GetSection());
			}
			mir_free(translated);
		}
	}

	void LoadRegisteredFolderItems()
	{
		int idx = m_lbSections.GetCurSel();
		if (idx == LB_ERR)
			return;

		char* szSection = (char*)m_lbSections.GetItemData(idx);

		m_lbItems.ResetContent();
		for (auto &it : lstRegisteredFolders) {
			if (!mir_strcmp(szSection, it->GetSection())) {
				idx = m_lbItems.AddString(TranslateW(it->GetUserName()), 0);
				m_lbItems.SetItemData(idx, (LPARAM)it);
			}
		}
		m_lbItems.SetCurSel(0); //select the first item
		OnItemsSelChange(0); //tell the dialog to refresh the preview
	}

	void RefreshPreview()
	{
		wchar_t tmp[MAX_FOLDER_SIZE];
		m_edtEdit.GetText(tmp, _countof(tmp));
		m_edtPreview.SetText(ExpandPath(tmp));
	}

	void LoadItem(PFolderItem item)
	{
		if (!item)
			return;

		m_edtEdit.SetText(item->GetFormat());
		RefreshPreview();
	}

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

		wchar_t buffer[MAX_FOLDER_SIZE];
		m_edtEdit.GetText(buffer, _countof(buffer));
		item->SetFormat(buffer);

		if (bEnableApply)
			NotifyChange();
	}

	int ChangesNotSaved(PFolderItem item)
	{
		if (!item)
			return 0;

		wchar_t buffer[MAX_FOLDER_SIZE];
		m_edtEdit.GetText(buffer, _countof(buffer));
		return mir_wstrcmp(item->GetFormat(), buffer) != 0;
	}

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

public:
	COptDialog() :
		CDlgBase(g_plugin, IDD_OPT_FOLDERS),
		m_lbSections(this, IDC_FOLDERS_SECTIONS_LIST),
		m_lbItems(this, IDC_FOLDERS_ITEMS_LIST),
		m_edtPreview(this, IDC_PREVIEW_EDIT),
		m_edtEdit(this, IDC_FOLDER_EDIT),
		m_btnRefresh(this, IDC_REFRESH_BUTTON),
		m_btnHelp(this, IDC_HELP_BUTTON)
	{
		m_edtEdit.OnChange = Callback(this, &COptDialog::OnEditChange);
		m_btnRefresh.OnClick = Callback(this, &COptDialog::OnRefreshClick);
		m_btnHelp.OnClick = Callback(this, &COptDialog::OnHelpClick);
		m_lbSections.OnSelChange = Callback(this, &COptDialog::OnSectionsSelChange);
		m_lbItems.OnSelChange = Callback(this, &COptDialog::OnItemsSelChange);
	}

	bool OnInitDialog() override
	{
		lastItem = nullptr;
		LoadRegisteredFolderSections();
		return true;
	}

	void OnEditChange(CCtrlBase*)
	{
		RefreshPreview();
	}

	void OnRefreshClick(CCtrlBase*)
	{
		RefreshPreview();
	}

	void OnHelpClick(CCtrlBase*)
	{
		if (pHelpDialog == nullptr) {
			pHelpDialog = new CHelpDialog();
			pHelpDialog->Show();
		}
		else {
			SetForegroundWindow(pHelpDialog->GetHwnd());
			SetFocus(pHelpDialog->GetHwnd());
		}
	}

	void OnSectionsSelChange(CCtrlBase*)
	{
		CheckForChanges();
		LoadRegisteredFolderItems();
	}

	void OnItemsSelChange(CCtrlBase*)
	{
		PFolderItem item = GetSelectedItem();
		if (item != nullptr) {
			CheckForChanges();
			LoadItem(item);
		}
		lastItem = item;
	}

	bool OnApply() override
	{
		PFolderItem item = GetSelectedItem();
		if (item) {
			SaveItem(item, FALSE);
			LoadItem(item);
		}

		for (auto &it : lstRegisteredFolders)
			it->Save();
		CallPathChangedEvents();
		return true;
	}
};

int OnOptionsInitialize(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = {};
	odp.szTitle.a = LPGEN("Folders");
	odp.szGroup.a = LPGEN("Customize");
	odp.flags = ODPF_BOLDGROUPS;
	odp.pDialog = new COptDialog;
	g_plugin.addOptions(wParam, &odp);
	return 0;
}