summaryrefslogtreecommitdiff
path: root/plugins/QuickReplies/src/events.cpp
blob: 6cddb3322f1f5bc90a15365cf5df65926c00ea8f (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
/* 
Copyright (C) 2010 Unsane

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  
*/

#include "stdafx.h"

using namespace std;

BYTE iNumber;

HANDLE hOnOptInitialized;
HANDLE hOnButtonPressed;
HANDLE hQuickRepliesService;

INT_PTR QuickRepliesService(WPARAM, LPARAM)
{
	return TRUE;
}

static IconItem icon = { LPGEN("Button"), "qr_button", IDI_QICON };

int OnModulesLoaded(WPARAM, LPARAM)
{
	UnhookEvent(hOnModulesLoaded);

	if ( !ServiceExists(MS_QUICKREPLIES_SERVICE)) {
		iNumber = 0;
		hQuickRepliesService = CreateServiceFunction(MS_QUICKREPLIES_SERVICE, QuickRepliesService);
	}
	else iNumber = db_get_b(NULL, MODULE, "InstancesCount", 0);
	db_set_b(NULL, MODULE, "InstancesCount", iNumber + 1);

	hOnOptInitialized = HookEvent(ME_OPT_INITIALISE, OnOptInitialized);
	hOnButtonPressed = HookEvent(ME_MSG_BUTTONPRESSED, OnButtonPressed);

	if ( ServiceExists(MS_BB_ADDBUTTON)) {
		Icon_Register(hInstance, "TabSRMM/Quick Replies", &icon, 1);

		char buttonNameTranslated[32], buttonName[32];
		mir_snprintf(buttonNameTranslated, _countof(buttonNameTranslated), "%s %x", Translate("Button"), iNumber + 1);
		mir_snprintf(buttonName, _countof(buttonName), MODULE" %x", iNumber + 1);

		BBButton bbd = {0};
		bbd.cbSize = sizeof(BBButton);
		bbd.bbbFlags = BBBF_ISIMBUTTON | BBBF_ISCHATBUTTON | BBBF_ISLSIDEBUTTON;
		bbd.pszModuleName = buttonName;
		bbd.ptszTooltip = LPGENT("Quick Replies\r\nLeft button - open menu\r\nRight button - options page");
		bbd.hIcon = icon.hIcolib;
		bbd.dwButtonID = iNumber;
		bbd.dwDefPos = 220;
		
		CallService(MS_BB_ADDBUTTON, 0, (LPARAM)&bbd);
	}

	return 0;
}

int OnButtonPressed(WPARAM wParam, LPARAM lParam)
{
	char key[64];
	int count = 0;
	HMENU hMenu = NULL;
	char buttonName[32];
	CMString replies;
	LIST<wchar_t> replyList(1);
	CustomButtonClickData *cbcd = (CustomButtonClickData *)lParam;

	mir_snprintf(buttonName, _countof(buttonName), MODULE" %x", iNumber + 1);
	if (mir_strcmp(cbcd->pszModule, buttonName))
		return 0;

	if (cbcd->dwButtonId != iNumber)
		return 1;

	mir_snprintf(key, _countof(key), "RepliesCount_%x", iNumber);
	count = db_get_w(NULL, MODULE, key, 0);

	{
		if (count == 0 || cbcd->flags & BBCF_RIGHTBUTTON)
		{
			mir_snprintf(buttonName, _countof(buttonName), "%s %x", Translate("Button"), iNumber + 1);

			OPENOPTIONSDIALOG ood = {0};
			ood.cbSize = sizeof(ood);
			ood.pszGroup = "Message Sessions";
			ood.pszPage = "Quick Replies";
			ood.pszTab = buttonName;
			Options_Open(&ood);
			return 0;
		}

		hMenu = CreatePopupMenu();

		for (int i = 0; i < count; i++)
		{
			mir_snprintf(key, _countof(key), "Reply_%x_%x", iNumber, i);
			wchar_t *value = db_get_wsa(NULL, MODULE, key);

			if (!value)
				replyList.insert(mir_wstrdup(_T("")));
			else
				replyList.insert(variables_parsedup(value, 0, wParam));

			if (!mir_tstrcmp(value, _T("---")))
				AppendMenu((HMENU)hMenu, MF_SEPARATOR, i + 1, NULL);
			else
				AppendMenu((HMENU)hMenu, MF_STRING, i + 1, replyList[i]);

			mir_free(value);
		}
	}

	{
		int index = TrackPopupMenu(hMenu, TPM_RETURNCMD, cbcd->pt.x, cbcd->pt.y, 0, cbcd->hwndFrom, NULL);
		if (index > 0)
		{
			if (mir_tstrcmp(replyList[index - 1], _T("")))
			{
				HWND hEdit = GetDlgItem(cbcd->hwndFrom, IDC_MESSAGE);
				if (!hEdit) hEdit = GetDlgItem(cbcd->hwndFrom, IDC_CHATMESSAGE);

				SendMessage(hEdit, EM_REPLACESEL, TRUE, (LPARAM)replyList[index - 1]);

				mir_snprintf(key, _countof(key), "ImmediatelySend_%x", iNumber);
				if ((BYTE)db_get_b(NULL, MODULE, key, 1) || cbcd->flags & BBCF_CONTROLPRESSED)
					SendMessage(cbcd->hwndFrom, WM_COMMAND, IDOK, 0);
			}
		}
	}
	
	for (int i = 0; i < replyList.getCount(); i++)
		mir_free(replyList[i]);
	replyList.destroy();

	return 1;
}

int OnPreShutdown(WPARAM, LPARAM)
{
	if (ServiceExists(MS_BB_REMOVEBUTTON))
	{
		char buttonName[32];
		mir_snprintf(buttonName, _countof(buttonName), MODULE" %x", iNumber + 1);

		BBButton bbd = {0};
		bbd.cbSize = sizeof(BBButton);
		bbd.pszModuleName = buttonName;
		bbd.dwButtonID = iNumber;

		CallService(MS_BB_REMOVEBUTTON, 0, (LPARAM)&bbd);
	}
	UnhookEvent(hOnButtonPressed);
	UnhookEvent(hOnOptInitialized);
	UnhookEvent(hOnPreShutdown);
	return 0;
}