summaryrefslogtreecommitdiff
path: root/plugins/AutoRun/main.cpp
blob: 478fc7aef59093b1a925eb94b5f7797d093b0892 (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
#include "autorun.h"

HINSTANCE hInst;
PLUGINLINK *pluginLink;
HANDLE hHookOptionInit = NULL;
int hLangpack;
struct MM_INTERFACE mmi;

PLUGININFOEX pluginInfoEx=
{ 	// about plugin
	sizeof(PLUGININFOEX),
	"Autorun",
	PLUGIN_MAKE_VERSION(0,1,0,1),
	"This plugin is a simple way to enable/disable to launch Miranda IM with system startup.",
	"Sergey V. Gershovich a.k.a. Jazzy$ (fixed by Wolfram3D)",
	"",
	"Copyright © 2002-2007 Sergey V. Gershovich a.k.a. Jazzy$",
	"http://miranda-im.org/download/index.php?action=viewfile&id=285",
	0, 0,
	MIID_AUTORUN
};

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
	hInst = hinstDLL;
	return TRUE;
}
void GetProfilePath(TCHAR *res)
{
  	TCHAR dbname[MAX_PATH], dbpath[MAX_PATH], exename[MAX_PATH];
	CallService(MS_DB_GETPROFILENAMET, SIZEOF(dbname), (LPARAM)(TCHAR*) dbname);
	CallService(MS_DB_GETPROFILEPATHT, SIZEOF(dbpath), (LPARAM)(TCHAR*) dbpath);
	GetModuleFileName(NULL,exename, SIZEOF(exename));
	lstrcat(dbpath, _T("\\"));		
	lstrcpyn(dbpath + lstrlen(dbpath), dbname, lstrlen(dbname)-3);
	lstrcat(dbpath, _T("\\"));
	lstrcat(dbpath, dbname);		// path + profile name
	mir_sntprintf(res, lstrlen(exename) + lstrlen(dbpath) + 5, _T("\"%s\" \"%s\""), exename, dbpath);
}

static void SetAutorun(BOOL autorun)
{
  HKEY hKey;
  DWORD dw;
  switch (autorun)
  {
  case TRUE:
	if (RegCreateKeyEx(ROOT_KEY, SUB_KEY, 0, NULL, 0, KEY_CREATE_SUB_KEY|KEY_SET_VALUE,NULL,&hKey,&dw) == ERROR_SUCCESS)
	{
	  	TCHAR result[MAX_PATH];
		GetProfilePath(result);
		RegSetValueEx(hKey, _T("MirandaIM"), 0, REG_SZ, (BYTE*)result, lstrlen(result)+1);
		RegCloseKey(hKey);
		break;
	}
  case FALSE:
	if (RegOpenKey(ROOT_KEY, SUB_KEY, &hKey) == ERROR_SUCCESS)
	{
		RegDeleteValue(hKey, _T("MirandaIM"));
		RegCloseKey(hKey);
		break;
	}
  }
}

static BOOL CmpCurrentAndRegistry()
{
	HKEY hKey;
	DWORD dwBufLen = MAX_PATH;
	TCHAR result[MAX_PATH], dbpath[MAX_PATH];
	GetProfilePath(result);
	
	if (RegOpenKeyEx(ROOT_KEY, SUB_KEY, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
	{
		if (RegQueryValueEx(hKey, _T("MirandaIM"), NULL, NULL, (LPBYTE)dbpath, &dwBufLen) == ERROR_SUCCESS)
		{
			return (lstrcmpi(result,dbpath) == 0 ? TRUE : FALSE);
		} 
		else 
			return FALSE;
	} 
	else 
		return FALSE;
}

static INT_PTR CALLBACK DlgProcAutorunOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
        CheckDlgButton(hwndDlg,IDC_AUTORUN,CmpCurrentAndRegistry()); // 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;
		break;
	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 lParam)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.cbSize = sizeof(odp);
	odp.position = 100100000;
	odp.hInstance = hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_AUTORUN);
	odp.ptszTitle = _T(ModuleName);
	odp.ptszGroup = LPGENT("Plugins");
	odp.pfnDlgProc = DlgProcAutorunOpts;
	odp.flags = ODPF_BOLDGROUPS;	
	Options_AddPage(wParam, &odp);
	return 0;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
{
	return &pluginInfoEx;
}

extern "C" __declspec(dllexport) int Load(PLUGINLINK *link)
{
	pluginLink = link;
	mir_getLP(&pluginInfoEx);
	mir_getMMI(&mmi);
	hHookOptionInit = HookEvent(ME_OPT_INITIALISE, AutorunOptInitialise);
	return 0;
}

extern "C" __declspec(dllexport) int Unload(void)
{
	if (hHookOptionInit)
		UnhookEvent(hHookOptionInit);    
	return 0;
}