summaryrefslogtreecommitdiff
path: root/plugins/AutoRun/src/main.cpp
blob: 5b4cc0f3c0d1f92ea91f0da071b7e3c32bce90d9 (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
#include "stdafx.h"

CMPlugin g_plugin;
HKEY ROOT_KEY = HKEY_CURRENT_USER;

/////////////////////////////////////////////////////////////////////////////////////////

PLUGININFOEX pluginInfoEx = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {EB0465E2-CEEE-11DB-83EF-C1BF55D89593}
	{0xeb0465e2, 0xceee, 0x11db, {0x83, 0xef, 0xc1, 0xbf, 0x55, 0xd8, 0x95, 0x93}}
};

CMPlugin::CMPlugin() :
	PLUGIN<CMPlugin>(MODULENAME, pluginInfoEx)
{}

/////////////////////////////////////////////////////////////////////////////////////////

void GetProfilePath(wchar_t *res, size_t resLen)
{
	wchar_t dbname[MAX_PATH], exename[MAX_PATH];
	Profile_GetNameW(_countof(dbname), dbname);
	GetModuleFileName(nullptr, exename, _countof(exename));

	wchar_t *p = wcsrchr(dbname, '.');
	if (p) *p = 0;

	mir_snwprintf(res, resLen, L"\"%s\" \"/profile:%s\"", exename, dbname);
}

static void SetAutorun(BOOL autorun)
{
	HKEY hKey;
	DWORD dw;
	switch (autorun) {
	case TRUE:
		if (RegCreateKeyEx(ROOT_KEY, SUB_KEY, 0, nullptr, 0, KEY_CREATE_SUB_KEY | KEY_SET_VALUE, nullptr, &hKey, &dw) == ERROR_SUCCESS) {
			wchar_t result[MAX_PATH];
			GetProfilePath(result, _countof(result));
			RegSetValueEx(hKey, L"MirandaNG", 0, REG_SZ, (uint8_t*)result, sizeof(wchar_t)*(uint32_t)mir_wstrlen(result));
			RegCloseKey(hKey);
		}
		break;
	case FALSE:
		if (RegOpenKey(ROOT_KEY, SUB_KEY, &hKey) == ERROR_SUCCESS) {
			RegDeleteValue(hKey, L"MirandaNG");
			RegCloseKey(hKey);
		}
		break;
	}
}

static BOOL CmpCurrentAndRegistry()
{
	HKEY hKey;
	if (RegOpenKeyEx(ROOT_KEY, SUB_KEY, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
		return FALSE;

	wchar_t result[MAX_PATH], dbpath[MAX_PATH];
	DWORD dwBufLen = MAX_PATH;
	if (RegQueryValueEx(hKey, L"MirandaNG", nullptr, nullptr, (LPBYTE)dbpath, &dwBufLen) != ERROR_SUCCESS)
		return FALSE;

	GetProfilePath(result, _countof(result));
	return mir_wstrcmpi(result, dbpath) == 0;
}

static INT_PTR CALLBACK DlgProcAutorunOpts(HWND hwndDlg, UINT msg, WPARAM, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
		CheckDlgButton(hwndDlg, IDC_AUTORUN, CmpCurrentAndRegistry() ? BST_CHECKED : BST_UNCHECKED); // Check chekbox if Registry value exists
		return TRUE;

	case WM_COMMAND:
		SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
		return TRUE;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY: // if "Apply" pressed then...
				SetAutorun(IsDlgButtonChecked(hwndDlg, IDC_AUTORUN)); //Save changes to registry;
				return TRUE;
			}
			break;
		}
		break;
	}
	return FALSE;
}

static int AutorunOptInitialise(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = {};
	odp.position = 100100000;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_AUTORUN);
	odp.szTitle.a = MODULENAME;
	odp.szGroup.a = LPGEN("Services");
	odp.pfnDlgProc = DlgProcAutorunOpts;
	odp.flags = ODPF_BOLDGROUPS;
	g_plugin.addOptions(wParam, &odp);
	return 0;
}

int CMPlugin::Load()
{
	HookEvent(ME_OPT_INITIALISE, AutorunOptInitialise);
	return 0;
}