summaryrefslogtreecommitdiff
path: root/plugins/WhoUsesMyFiles/src/wumfplug.cpp
blob: 33abd2a516f533bd5cf201e111c5989ea55365dd (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
#include "stdafx.h"

WUMF_OPTIONS WumfOptions = { 0 };
HGENMENU hMenuItem = nullptr;
HWND hDlg;

CMPlugin g_plugin;
int &hLangpack(g_plugin.m_hLang);

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

static PLUGININFOEX pluginInfoEx =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {80DCA515-973A-4A7E-8B85-5D8EC88FC5A7}
	{0x80dca515, 0x973a, 0x4a7e, {0x8b, 0x85, 0x5d, 0x8e, 0xc8, 0x8f, 0xc5, 0xa7}}
};

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

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

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

int OptionsInit(WPARAM, LPARAM);

void LoadOptions()
{
	DBVARIANT dbv = { 0 };
	dbv.type = DBVT_WCHAR;
	memset(&WumfOptions, 0, sizeof(WumfOptions));
	if (db_get_ws(NULL, MODULENAME, OPT_FILE, &dbv) == 0) {
		wcsncpy(WumfOptions.LogFile, dbv.ptszVal, 255);
		db_free(&dbv);
	}
	else
		WumfOptions.LogFile[0] = '\0';

	WumfOptions.PopupsEnabled = db_get_b(NULL, MODULENAME, POPUPS_ENABLED, TRUE);

	WumfOptions.UseDefColor = db_get_b(NULL, MODULENAME, COLOR_DEF, TRUE);
	WumfOptions.UseWinColor = db_get_b(NULL, MODULENAME, COLOR_WIN, FALSE);
	WumfOptions.SelectColor = db_get_b(NULL, MODULENAME, COLOR_SET, FALSE);

	WumfOptions.ColorText = db_get_dw(NULL, MODULENAME, COLOR_TEXT, RGB(0, 0, 0));
	WumfOptions.ColorBack = db_get_dw(NULL, MODULENAME, COLOR_BACK, RGB(255, 255, 255));

	WumfOptions.DelayDef = db_get_b(NULL, MODULENAME, DELAY_DEF, TRUE);
	WumfOptions.DelayInf = db_get_b(NULL, MODULENAME, DELAY_INF, FALSE);
	WumfOptions.DelaySet = db_get_b(NULL, MODULENAME, DELAY_SET, FALSE);
	WumfOptions.DelaySec = db_get_b(NULL, MODULENAME, DELAY_SEC, 0);
	if (!ServiceExists(MS_POPUP_ADDPOPUPT)) {
		WumfOptions.DelayDef = TRUE;
		WumfOptions.DelaySet = FALSE;
		WumfOptions.DelayInf = FALSE;
	}
	WumfOptions.LogToFile = db_get_b(NULL, MODULENAME, LOG_INTO_FILE, FALSE);
	WumfOptions.LogFolders = db_get_b(NULL, MODULENAME, LOG_FOLDER, TRUE);
	WumfOptions.AlertFolders = db_get_b(NULL, MODULENAME, ALERT_FOLDER, TRUE);
	WumfOptions.LogUNC = db_get_b(NULL, MODULENAME, LOG_UNC, FALSE);
	WumfOptions.AlertUNC = db_get_b(NULL, MODULENAME, ALERT_UNC, FALSE);
	WumfOptions.LogComp = db_get_b(NULL, MODULENAME, LOG_COMP, FALSE);
	WumfOptions.AlertComp = db_get_b(NULL, MODULENAME, ALERT_COMP, FALSE);
}

void ExecuteMenu(HWND hWnd)
{
	HMENU hMenu = CreatePopupMenu();
	if (!hMenu) {
		msg(TranslateT("Error creating menu"));
		return;
	}
	AppendMenu(hMenu, MF_STRING, IDM_ABOUT, L"About\0");
	AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);//------------------
	AppendMenu(hMenu, MF_STRING, IDM_SHOW, L"Show connections\0");
	AppendMenu(hMenu, MF_SEPARATOR, 0, nullptr);//------------------
	AppendMenu(hMenu, MF_STRING, IDM_EXIT, L"Dismiss popup\0");

	POINT point;
	GetCursorPos(&point);
	SetForegroundWindow(hWnd);
	TrackPopupMenu(hMenu, TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RIGHTALIGN | TPM_TOPALIGN, point.x, point.y, 0, hWnd, nullptr);
	PostMessage(hWnd, WM_USER, 0, 0);
	DestroyMenu(hMenu);
}

static LRESULT CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDM_ABOUT:
			break;
		case IDM_EXIT:
			PUDeletePopup(hWnd);
			break;
		case IDM_SHOW:
			CallService(MS_WUMF_CONNECTIONSSHOW, 0, 0);
			return TRUE;
		}

		switch (HIWORD(wParam)) {
		case STN_CLICKED:
			PUDeletePopup(hWnd);
			return TRUE;
		}
		break;

	case WM_CONTEXTMENU:
		CallService(MS_WUMF_CONNECTIONSSHOW, 0, 0);
		break;

	case UM_FREEPLUGINDATA:
		return TRUE; //TRUE or FALSE is the same, it gets ignored.
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

void ShowWumfPopup(PWumf w)
{
	wchar_t text[512], title[512];

	if (!WumfOptions.AlertFolders && (w->dwAttr & FILE_ATTRIBUTE_DIRECTORY)) return;
	mir_snwprintf(title, L"%s (%s)", w->szComp, w->szUser);
	mir_snwprintf(text, L"%s (%s)", w->szPath, w->szPerm);
	ShowThePopup(w, title, text);
}

void ShowThePopup(PWumf w, LPTSTR title, LPTSTR text)
{
	POPUPDATAT ppd = { 0 };
	ppd.lchContact = NULL;
	ppd.lchIcon = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_DRIVE));

	if (WumfOptions.DelayInf)
		ppd.iSeconds = -1;
	else if (WumfOptions.DelayDef)
		ppd.iSeconds = 0;
	else if (WumfOptions.DelaySet)
		ppd.iSeconds = WumfOptions.DelaySec;

	mir_wstrncpy(ppd.lptzContactName, title, MAX_CONTACTNAME);
	mir_wstrncpy(ppd.lptzText, text, MAX_SECONDLINE);
	if (WumfOptions.UseWinColor) {
		ppd.colorBack = GetSysColor(COLOR_WINDOW);
		ppd.colorText = GetSysColor(COLOR_WINDOWTEXT);
	}
	else if (WumfOptions.SelectColor) {
		ppd.colorBack = WumfOptions.ColorBack;
		ppd.colorText = WumfOptions.ColorText;
	}

	ppd.PluginWindowProc = PopupDlgProc;
	ppd.PluginData = w;
	PUAddPopupT(&ppd);
}

void ThreadProc(LPVOID)
{
	if (hDlg) {
		ShowWindow(hDlg, SW_SHOWNORMAL);
		SetForegroundWindow(hDlg);
		return;
	}

	hDlg = CreateDialog(g_plugin.getInst(), MAKEINTRESOURCE(IDD_CONNLIST), nullptr, ConnDlgProc);
	SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_DRIVE)));
	ShowWindow(hDlg, SW_SHOW);

	MSG msg;
	while (GetMessage(&msg, nullptr, 0, 0) == TRUE) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	hDlg = nullptr;
}

static INT_PTR WumfShowConnections(WPARAM, LPARAM)
{
	mir_forkthread(ThreadProc);
	CallService(MS_TTB_SETBUTTONSTATE, (WPARAM)hWumfBut, 0);
	return 0;
}

static INT_PTR WumfMenuCommand(WPARAM, LPARAM)
{
	if (WumfOptions.PopupsEnabled == TRUE) {
		WumfOptions.PopupsEnabled = FALSE;
		Menu_ModifyItem(hMenuItem, LPGENW("Enable WUMF popups"), LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_NOPOPUP)));
	}
	else {
		WumfOptions.PopupsEnabled = TRUE;
		Menu_ModifyItem(hMenuItem, LPGENW("Disable WUMF popups"), LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_POPUP)));
	}

	db_set_b(NULL, MODULENAME, POPUPS_ENABLED, (BYTE)WumfOptions.PopupsEnabled);
	return 0;
}

int InitTopToolbar(WPARAM, LPARAM)
{
	TTBButton ttb = {};
	ttb.hIconUp = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_DRIVE));
	ttb.pszService = MS_WUMF_CONNECTIONSSHOW;
	ttb.dwFlags = TTBBF_VISIBLE | TTBBF_SHOWTOOLTIP;
	ttb.name = ttb.pszTooltipUp = LPGEN("Show connections list");
	hWumfBut = TopToolbar_AddButton(&ttb);
	return 0;
}

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

extern "C" __declspec(dllexport) int Load(void)
{
	mir_getLP(&pluginInfoEx);

	LoadOptions();

	CreateServiceFunction(MS_WUMF_SWITCHPOPUP, WumfMenuCommand);
	CreateServiceFunction(MS_WUMF_CONNECTIONSSHOW, WumfShowConnections);

	CMenuItem mi;

	SET_UID(mi, 0xcfce6487, 0x907b, 0x4822, 0xb0, 0x49, 0x18, 0x4e, 0x47, 0x17, 0x0, 0x69);
	mi.root = Menu_CreateRoot(MO_MAIN, LPGENW("Popups"), 1999990000);
	if (WumfOptions.PopupsEnabled == FALSE) {
		mi.name.a = LPGEN("Enable WUMF popups");
		mi.hIcolibItem = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_NOPOPUP));
	}
	else {
		mi.name.a = LPGEN("Disable WUMF popups");
		mi.hIcolibItem = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_POPUP));
	}
	mi.pszService = MS_WUMF_SWITCHPOPUP;
	hMenuItem = Menu_AddMainMenuItem(&mi);

	SET_UID(mi, 0xbf93984c, 0xaa05, 0x447c, 0xbd, 0x5c, 0x5f, 0x43, 0x60, 0x92, 0x6a, 0x12);
	mi.name.a = LPGEN("WUMF: Show connections");
	mi.hIcolibItem = LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_DRIVE));
	mi.pszService = MS_WUMF_CONNECTIONSSHOW;
	Menu_AddMainMenuItem(&mi);

	HookEvent(ME_OPT_INITIALISE, OptionsInit);
	HookEvent(ME_TTB_MODULELOADED, InitTopToolbar);

	if (IsUserAnAdmin())
		SetTimer(nullptr, 777, TIME, TimerProc);
	else
		MessageBox(nullptr, TranslateT("Plugin WhoUsesMyFiles requires admin privileges in order to work."), L"Miranda NG", MB_OK);
	return 0;
}

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

extern "C" __declspec(dllexport) int Unload(void)
{
	KillTimer(nullptr, 777);
	if (hLogger)
		mir_closeLog(hLogger);
	FreeAll();
	return 0;
}