summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_bookmarks.cpp
blob: 7abca8bb6eed9797fc6a024480004188bdb6c3b5 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2007  Michael Stepura, George Hazan
Copyright (C) 2012-20 Miranda NG team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"
#include "jabber_iq.h"

/////////////////////////////////////////////////////////////////////////////////////////
// Bookmarks editor window

struct JabberAddBookmarkDlgParam
{
	CJabberProto *ppro;
	JABBER_LIST_ITEM* m_item;
};

static INT_PTR CALLBACK JabberAddBookmarkDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	JabberAddBookmarkDlgParam* param = (JabberAddBookmarkDlgParam*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);

	wchar_t text[512];
	JABBER_LIST_ITEM *item;

	switch (msg) {
	case WM_INITDIALOG:
		param = (JabberAddBookmarkDlgParam*)lParam;
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);

		param->ppro->m_hwndJabberAddBookmark = hwndDlg;
		TranslateDialogDefault(hwndDlg);
		if (item = param->m_item) {
			if (!mir_strcmp(item->type, "conference")) {
				if (!strchr(item->jid, '@')) {	  //no room name - consider it is transport
					CheckDlgButton(hwndDlg, IDC_AGENT_RADIO, BST_CHECKED);
					EnableWindow(GetDlgItem(hwndDlg, IDC_NICK), FALSE);
					EnableWindow(GetDlgItem(hwndDlg, IDC_PASSWORD), FALSE);
				}
				else CheckDlgButton(hwndDlg, IDC_ROOM_RADIO, BST_CHECKED);
			}
			else {
				CheckDlgButton(hwndDlg, IDC_URL_RADIO, BST_CHECKED);
				EnableWindow(GetDlgItem(hwndDlg, IDC_NICK), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_PASSWORD), FALSE);
				CheckDlgButton(hwndDlg, IDC_CHECK_BM_AUTOJOIN, BST_UNCHECKED);
				EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_BM_AUTOJOIN), FALSE);
			}

			EnableWindow(GetDlgItem(hwndDlg, IDC_ROOM_RADIO), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_URL_RADIO), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_AGENT_RADIO), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_BM_AUTOJOIN), FALSE);

			if (item->jid) SetDlgItemTextUtf(hwndDlg, IDC_ROOM_JID, item->jid);
			if (item->name) SetDlgItemTextW(hwndDlg, IDC_NAME, item->name);
			if (item->nick) SetDlgItemTextUtf(hwndDlg, IDC_NICK, item->nick);
			if (item->password) SetDlgItemTextUtf(hwndDlg, IDC_PASSWORD, item->password);
			if (item->bAutoJoin) CheckDlgButton(hwndDlg, IDC_CHECK_BM_AUTOJOIN, BST_CHECKED);
			if (IsDlgButtonChecked(hwndDlg, IDC_ROOM_RADIO) == BST_CHECKED)
				EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_BM_AUTOJOIN), TRUE);
		}
		else {
			EnableWindow(GetDlgItem(hwndDlg, IDOK), FALSE);
			CheckDlgButton(hwndDlg, IDC_ROOM_RADIO, BST_CHECKED);
		}
		return TRUE;

	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case BN_CLICKED:
			switch (LOWORD(wParam)) {
			case IDC_ROOM_RADIO:
				EnableWindow(GetDlgItem(hwndDlg, IDC_NICK), TRUE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_PASSWORD), TRUE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_BM_AUTOJOIN), TRUE);
				break;
			case IDC_AGENT_RADIO:
			case IDC_URL_RADIO:
				EnableWindow(GetDlgItem(hwndDlg, IDC_NICK), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_PASSWORD), FALSE);
				CheckDlgButton(hwndDlg, IDC_CHECK_BM_AUTOJOIN, BST_UNCHECKED);
				EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_BM_AUTOJOIN), FALSE);
				break;
			}
		}

		switch (LOWORD(wParam)) {
		case IDC_ROOM_JID:
			if ((HWND)lParam == GetFocus() && HIWORD(wParam) == EN_CHANGE)
				EnableWindow(GetDlgItem(hwndDlg, IDOK), GetDlgItemText(hwndDlg, IDC_ROOM_JID, text, _countof(text)));
			break;

		case IDOK:
			{
				GetDlgItemText(hwndDlg, IDC_ROOM_JID, text, _countof(text));
				T2Utf roomJID(text);

				if (param->m_item)
					param->ppro->ListRemove(LIST_BOOKMARK, param->m_item->jid);

				item = param->ppro->ListAdd(LIST_BOOKMARK, roomJID);

				if (IsDlgButtonChecked(hwndDlg, IDC_URL_RADIO) == BST_CHECKED)
					replaceStr(item->type, "url");
				else
					replaceStr(item->type, "conference");

				GetDlgItemText(hwndDlg, IDC_NICK, text, _countof(text));
				replaceStr(item->nick, T2Utf(text));

				GetDlgItemText(hwndDlg, IDC_PASSWORD, text, _countof(text));
				replaceStr(item->password, T2Utf(text));

				GetDlgItemText(hwndDlg, IDC_NAME, text, _countof(text));
				replaceStrW(item->name, (text[0] == 0) ? Utf2T(roomJID) : text);

				item->bAutoJoin = IsDlgButtonChecked(hwndDlg, IDC_CHECK_BM_AUTOJOIN) == BST_CHECKED;

				XmlNodeIq iq(param->ppro->AddIQ(&CJabberProto::OnIqResultSetBookmarks, JABBER_IQ_TYPE_SET));
				param->ppro->SetBookmarkRequest(iq);
				param->ppro->m_ThreadInfo->send(iq);
			}
			__fallthrough;

		case IDCANCEL:
			EndDialog(hwndDlg, 0);
			break;
		}
		break;

	case WM_PROTO_CHECK_ONLINE:
		if (!param->ppro->m_bJabberOnline)
			EndDialog(hwndDlg, 0);
		break;

	case WM_DESTROY:
		param->ppro->m_hwndJabberAddBookmark = nullptr;
		break;
	}
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Bookmarks manager window

class CJabberDlgBookmarks : public CJabberDlgBase
{
	typedef CJabberDlgBase CSuper;

	CCtrlMButton m_btnAdd, m_btnEdit, m_btnRemove;
	CCtrlListView m_lvBookmarks;

public:
	CJabberDlgBookmarks(CJabberProto *proto) :
		CSuper(proto, IDD_BOOKMARKS),
		m_btnAdd(this, IDC_ADD, SKINICON_OTHER_ADDCONTACT, LPGEN("Add")),
		m_btnEdit(this, IDC_EDIT, SKINICON_OTHER_RENAME, LPGEN("Edit")),
		m_btnRemove(this, IDC_REMOVE, SKINICON_OTHER_DELETE, LPGEN("Remove")),
		m_lvBookmarks(this, IDC_BM_LIST)
	{
		SetMinSize(451, 320);

		m_lvBookmarks.OnItemActivate = Callback(this, &CJabberDlgBookmarks::lvBookmarks_OnDoubleClick);
		m_btnAdd.OnClick = Callback(this, &CJabberDlgBookmarks::btnAdd_OnClick);
		m_btnEdit.OnClick = Callback(this, &CJabberDlgBookmarks::btnEdit_OnClick);
		m_btnRemove.OnClick = Callback(this, &CJabberDlgBookmarks::btnRemove_OnClick);
	}

	bool OnInitDialog() override
	{
		CSuper::OnInitDialog();

		Window_SetIcon_IcoLib(m_hwnd, g_plugin.getIconHandle(IDI_BOOKMARKS));

		m_btnAdd.Disable();
		m_btnEdit.Disable();
		m_btnRemove.Disable();

		m_lvBookmarks.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER);

		HIMAGELIST hIml = m_lvBookmarks.CreateImageList(LVSIL_SMALL);
		g_plugin.addImgListIcon(hIml, IDI_GROUP);
		ImageList_AddSkinIcon(hIml, SKINICON_EVENT_URL);

		m_lvBookmarks.AddColumn(0, TranslateT("Bookmark Name"), m_proto->getWord("bookmarksWnd_cx0", 120));
		m_lvBookmarks.AddColumn(1, TranslateT("Address (JID or URL)"), m_proto->getWord("bookmarksWnd_cx1", 210));
		m_lvBookmarks.AddColumn(2, TranslateT("Nickname"), m_proto->getWord("bookmarksWnd_cx2", 90));

		m_lvBookmarks.EnableGroupView(TRUE);
		m_lvBookmarks.AddGroup(0, TranslateT("Conferences"));
		m_lvBookmarks.AddGroup(1, TranslateT("Links"));

		Utils_RestoreWindowPosition(m_hwnd, 0, m_proto->m_szModuleName, "bookmarksWnd_");
		return true;
	}

	bool OnClose() override
	{
		LVCOLUMN lvc = { 0 };
		lvc.mask = LVCF_WIDTH;
		m_lvBookmarks.GetColumn(0, &lvc);
		m_proto->setWord("bookmarksWnd_cx0", lvc.cx);
		m_lvBookmarks.GetColumn(1, &lvc);
		m_proto->setWord("bookmarksWnd_cx1", lvc.cx);
		m_lvBookmarks.GetColumn(2, &lvc);
		m_proto->setWord("bookmarksWnd_cx2", lvc.cx);

		Utils_SaveWindowPosition(m_hwnd, 0, m_proto->m_szModuleName, "bookmarksWnd_");

		return CSuper::OnClose();
	}

	void OnDestroy() override
	{
		m_proto->m_pDlgBookmarks = nullptr;
		CSuper::OnDestroy();
	}

	INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) override
	{
		switch (msg) {
		case WM_COMMAND:
			switch (LOWORD(wParam)) {
			case IDOK:
				OpenBookmark();
				return TRUE;
			}
			break;
		}

		return CSuper::DlgProc(msg, wParam, lParam);
	}

	int Resizer(UTILRESIZECONTROL *urc) override
	{
		switch (urc->wId) {
		case IDC_BM_LIST:
			return RD_ANCHORX_WIDTH | RD_ANCHORY_HEIGHT;

		case IDCANCEL:
			return RD_ANCHORX_RIGHT | RD_ANCHORY_BOTTOM;

		case IDC_ADD:
		case IDC_EDIT:
		case IDC_REMOVE:
			return RD_ANCHORX_LEFT | RD_ANCHORY_BOTTOM;
		}
		return CSuper::Resizer(urc);
	}

	void lvBookmarks_OnDoubleClick(CCtrlButton*)
	{
		OpenBookmark();
	}

	void btnAdd_OnClick(CCtrlButton*)
	{
		if (!m_proto->m_bJabberOnline) return;

		JabberAddBookmarkDlgParam param;
		param.ppro = m_proto;
		param.m_item = nullptr;
		DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_BOOKMARK_ADD), m_hwnd, JabberAddBookmarkDlgProc, (LPARAM)&param);
	}

	void btnEdit_OnClick(CCtrlButton*)
	{
		if (!m_proto->m_bJabberOnline)
			return;

		int iItem = m_lvBookmarks.GetNextItem(-1, LVNI_SELECTED);
		if (iItem < 0)
			return;

		char *address = (char*)m_lvBookmarks.GetItemData(iItem);
		if (address == nullptr)
			return;

		JABBER_LIST_ITEM *item = m_proto->ListGetItemPtr(LIST_BOOKMARK, address);
		if (item == nullptr)
			return;

		JabberAddBookmarkDlgParam param;
		param.ppro = m_proto;
		param.m_item = item;
		DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_BOOKMARK_ADD), m_hwnd, JabberAddBookmarkDlgProc, (LPARAM)&param);
	}

	void btnRemove_OnClick(CCtrlButton*)
	{
		if (!m_proto->m_bJabberOnline)
			return;

		int iItem = m_lvBookmarks.GetNextItem(-1, LVNI_SELECTED);
		if (iItem < 0)
			return;

		char *address = (char*)m_lvBookmarks.GetItemData(iItem);
		if (address == nullptr)
			return;

		JABBER_LIST_ITEM *item = m_proto->ListGetItemPtr(LIST_BOOKMARK, address);
		if (item == nullptr)
			return;

		m_btnAdd.Disable();
		m_btnEdit.Disable();
		m_btnRemove.Disable();

		m_proto->ListRemove(LIST_BOOKMARK, address);

		m_lvBookmarks.SetItemState(iItem, 0, LVIS_SELECTED); // Unselect the item

		XmlNodeIq iq(m_proto->AddIQ(&CJabberProto::OnIqResultSetBookmarks, JABBER_IQ_TYPE_SET));
		m_proto->SetBookmarkRequest(iq);
		m_proto->m_ThreadInfo->send(iq);
	}

	void OnProtoRefresh(WPARAM, LPARAM) override
	{
		m_lvBookmarks.DeleteAllItems();

		JABBER_LIST_ITEM *item = nullptr;
		LISTFOREACH(i, m_proto, LIST_BOOKMARK)
		{
			if (item = m_proto->ListGetItemPtrFromIndex(i)) {
				int itemType = mir_strcmpi(item->type, "conference") ? 1 : 0;
				m_proto->debugLogA("BOOKMARK #%d: %d %s", i, itemType, item->jid);

				int iItem = m_lvBookmarks.AddItem(item->name, itemType, (LPARAM)item->jid, itemType);
				m_lvBookmarks.SetItem(iItem, 1, Utf2T(item->jid));
				if (itemType == 0)
					m_lvBookmarks.SetItem(iItem, 2, Utf2T(item->nick));
			}
		}

		if (item) {
			m_btnEdit.Enable();
			m_btnRemove.Enable();
		}

		m_btnAdd.Enable();
	}

	void OnProtoCheckOnline(WPARAM, LPARAM) override
	{
		if (!m_proto->m_bJabberOnline) {
			m_btnAdd.Disable();
			m_btnEdit.Disable();
			m_btnRemove.Disable();
		}
		else UpdateData();
	}

	void OpenBookmark()
	{
		int iItem = m_lvBookmarks.GetNextItem(-1, LVNI_SELECTED);
		if (iItem < 0)
			return;

		char *address = (char*)m_lvBookmarks.GetItemData(iItem);
		if (address == nullptr)
			return;

		JABBER_LIST_ITEM *item = m_proto->ListGetItemPtr(LIST_BOOKMARK, address);
		if (item == nullptr)
			return;

		if (!mir_strcmpi(item->type, "conference")) {
			m_lvBookmarks.SetItemState(iItem, 0, LVIS_SELECTED); // Unselect the item

																				  /* some hack for using bookmark to transport not under XEP-0048 */
			if (!strchr(item->jid, '@'))
				//the room name is not provided let consider that it is transport and send request to registration
				m_proto->RegisterAgent(nullptr, item->jid);
			else {
				char *room = NEWSTR_ALLOCA(item->jid);
				char *server = strchr(room, '@');
				*(server++) = 0;

				if (item->nick && *item->nick)
					m_proto->GroupchatJoinRoom(server, room, item->nick, item->password);
				else
					m_proto->GroupchatJoinRoom(server, room, ptrA(JabberNickFromJID(m_proto->m_szJabberJID)), item->password);
			}
		}
		else Utils_OpenUrl(item->jid);
	}

	void UpdateData()
	{
		if (!m_proto->m_bJabberOnline) return;

		m_proto->m_ThreadInfo->send(
			XmlNodeIq(m_proto->AddIQ(&CJabberProto::OnIqResultDiscoBookmarks, JABBER_IQ_TYPE_GET))
			<< XQUERY(JABBER_FEAT_PRIVATE_STORAGE) << XCHILDNS("storage", "storage:bookmarks"));
	}
};

/////////////////////////////////////////////////////////////////////////////////////////
// Launches the Bookmarks manager window

INT_PTR __cdecl CJabberProto::OnMenuHandleBookmarks(WPARAM, LPARAM)
{
	UI_SAFE_OPEN_EX(CJabberDlgBookmarks, m_pDlgBookmarks, pDlg);
	pDlg->UpdateData();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Launches the Bookmark details window, lParam is JABBER_BOOKMARK_ITEM*

int CJabberProto::AddEditBookmark(JABBER_LIST_ITEM *item)
{
	if (m_bJabberOnline) {
		JabberAddBookmarkDlgParam param;
		param.ppro = this;
		param.m_item = item;//(JABBER_LIST_ITEM*)lParam;
		DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_BOOKMARK_ADD), nullptr, JabberAddBookmarkDlgProc, (LPARAM)&param);
	}
	return 0;
}