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

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

CMPlugin g_plugin;

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

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),
	bPopups(MODULENAME, POPUPS_ENABLED, true)
{
}

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

int OptionsInit(WPARAM, LPARAM);

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

	WumfOptions.UseDefColor = g_plugin.getByte(COLOR_DEF, TRUE);
	WumfOptions.UseWinColor = g_plugin.getByte(COLOR_WIN, FALSE);
	WumfOptions.SelectColor = g_plugin.getByte(COLOR_SET, FALSE);

	WumfOptions.ColorText = g_plugin.getDword(COLOR_TEXT, RGB(0, 0, 0));
	WumfOptions.ColorBack = g_plugin.getDword(COLOR_BACK, RGB(255, 255, 255));

	WumfOptions.DelayDef = g_plugin.getByte(DELAY_DEF, TRUE);
	WumfOptions.DelayInf = g_plugin.getByte(DELAY_INF, FALSE);
	WumfOptions.DelaySet = g_plugin.getByte(DELAY_SET, FALSE);
	WumfOptions.DelaySec = g_plugin.getByte(DELAY_SEC, 0);
	WumfOptions.LogToFile = g_plugin.getByte(LOG_INTO_FILE, FALSE);
	WumfOptions.LogFolders = g_plugin.getByte(LOG_FOLDER, TRUE);
	WumfOptions.AlertFolders = g_plugin.getByte(ALERT_FOLDER, TRUE);
}

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)
{
	POPUPDATAW ppd;
	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.lpwzContactName, title, MAX_CONTACTNAME);
	mir_wstrncpy(ppd.lpwzText, 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;
	PUAddPopupW(&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;
}

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 = g_plugin.addTTB(&ttb);
	return 0;
}

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

int CMPlugin::Load()
{
	LoadOptions();

	CreateServiceFunction(MS_WUMF_CONNECTIONSSHOW, WumfShowConnections);

	CMenuItem mi(&g_plugin);

	addPopupOption(LPGEN("WUMF popups"), bPopups);
	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;
}

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

int CMPlugin::Unload()
{
	KillTimer(nullptr, 777);
	if (hLogger)
		mir_closeLog(hLogger);
	FreeAll();
	return 0;
}