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

void copyModule(const char *module, MCONTACT hContactFrom, MCONTACT hContactTo)
{
	ModuleSettingLL msll;
	if (IsModuleEmpty(hContactFrom, module) || !EnumSettings(hContactFrom, module, &msll))
		return;

	DBVARIANT dbv;
	for (ModSetLinkLinkItem *setting = msll.first; setting; setting = setting->next) {
		if (!db_get_s(hContactFrom, module, setting->name, &dbv, 0)) {
			db_set(hContactTo, module, setting->name, &dbv);
			db_free(&dbv);
		}
	}
	FreeModuleSettingLL(&msll);
}

INT_PTR CALLBACK copyModDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	ModuleAndContact *mac = (ModuleAndContact *)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch (uMsg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
		{
			mac = (ModuleAndContact *)lParam;
			wchar_t name[NAME_SIZE], msg[MSG_SIZE];

			mir_snwprintf(msg, TranslateT("Copy module \"%s\""), _A2T(mac->module).get());
			SetWindowText(hwnd, msg);

			for (auto &hContact : Contacts()) {
				if (ApplyProtoFilter(hContact))
					continue;

				GetContactName(hContact, nullptr, name, _countof(name));

				int index = SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_ADDSTRING, 0, (LPARAM)name);
				SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_SETITEMDATA, index, hContact);
			}

			GetContactName(NULL, nullptr, name, _countof(name));
			int index = (int)SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_INSERTSTRING, 0, (LPARAM)name);
			SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_SETITEMDATA, index, 0);
			SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_SETCURSEL, index, 0);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case CHK_COPY2ALL:
			EnableWindow(GetDlgItem(hwnd, IDC_CONTACTS), BST_UNCHECKED == IsDlgButtonChecked(hwnd, CHK_COPY2ALL));
			break;

		case IDOK:
			if (BST_UNCHECKED == IsDlgButtonChecked(hwnd, CHK_COPY2ALL)) {
				MCONTACT hContact = (MCONTACT)SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_GETITEMDATA, SendDlgItemMessage(hwnd, IDC_CONTACTS, CB_GETCURSEL, 0, 0), 0);
				copyModule(mac->module, mac->hContact, hContact);
			}
			else {
				SetCursor(LoadCursor(nullptr, IDC_WAIT));
				for (auto &hContact : Contacts())
					copyModule(mac->module, mac->hContact, hContact);

				SetCursor(LoadCursor(nullptr, IDC_ARROW));
			}
			refreshTree(1);
			__fallthrough;

		case IDCANCEL:
			mir_free(mac);
			DestroyWindow(hwnd);
			break;
		}
		break;
	}
	return 0;
}

void copyModuleMenuItem(HWND hwndParent, MCONTACT hContact, const char *module)
{
	ModuleAndContact *mac = (ModuleAndContact *)mir_calloc(sizeof(ModuleAndContact));
	mac->hContact = hContact;
	mir_strncpy(mac->module, module, sizeof(mac->module));

	CreateDialogParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_COPY_MOD), hwndParent, copyModDlgProc, (LPARAM)mac);
}

int CloneContact(MCONTACT hContact)
{
	MCONTACT newContact = db_add_contact();
	if (!newContact)
		return 0;

	// enum all the modules
	ModuleSettingLL modlist;
	if (!EnumModules(&modlist))
		return 0;

	ModSetLinkLinkItem *mod = modlist.first;
	while (mod) {
		copyModule(mod->name, hContact, newContact);
		mod = (ModSetLinkLinkItem *)mod->next;
	}

	FreeModuleSettingLL(&modlist);
	return 1;
}