summaryrefslogtreecommitdiff
path: root/plugins/QuickReplies/src/events.cpp
blob: f925b6a2fdb654d41961e56fa5674fc0d0ce2abd (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
/*
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;

static IconItem iconList[] = 
{
	{ LPGEN("Button"), "qr_button", IDI_QICON }
};

int iNumber = 0;

int OnModulesLoaded(WPARAM, LPARAM)
{
	HookEvent(ME_OPT_INITIALISE, OnOptInitialized);
	HookEvent(ME_MSG_BUTTONPRESSED, OnButtonPressed);

	g_plugin.registerIcon("TabSRMM/Quick Replies", iconList);

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

	BBButton bbd = {};
	bbd.bbbFlags = BBBF_ISIMBUTTON | BBBF_ISCHATBUTTON;
	bbd.pszModuleName = buttonName;
	bbd.pwszTooltip = LPGENW("Quick Replies");
	bbd.hIcon = iconList[0].hIcolib;
	bbd.dwButtonID = iNumber;
	bbd.dwDefPos = 220;
	Srmm_AddButton(&bbd, g_plugin.m_hLang);
	return 0;
}

int OnButtonPressed(WPARAM wParam, LPARAM lParam)
{
	CustomButtonClickData *cbcd = (CustomButtonClickData *)lParam;

	char buttonName[32];
	mir_snprintf(buttonName, MODULENAME" %x", iNumber + 1);
	if (mir_strcmp(cbcd->pszModule, buttonName))
		return 0;

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

	char key[64];
	mir_snprintf(key, "RepliesCount_%x", iNumber);
	int count = db_get_w(NULL, MODULENAME, key, 0);

	if (count == 0 || cbcd->flags & BBCF_RIGHTBUTTON) {
		mir_snprintf(buttonName, "%s %x", Translate("Button"), iNumber + 1);
		g_plugin.openOptions(L"Message sessions", L"Quick Replies", _A2T(buttonName));
		return 0;
	}

	HMENU hMenu = CreatePopupMenu();

	LIST<wchar_t> replyList(1);
	for (int i = 0; i < count; i++) {
		mir_snprintf(key, "Reply_%x_%x", iNumber, i);
		ptrW value(db_get_wsa(NULL, MODULENAME, key));
		if (value == nullptr)
			replyList.insert(mir_wstrdup(L""));
		else
			replyList.insert(variables_parsedup(value, nullptr, wParam));

		if (!mir_wstrcmp(value, L"---"))
			AppendMenu((HMENU)hMenu, MF_SEPARATOR, i + 1, nullptr);
		else
			AppendMenu((HMENU)hMenu, MF_STRING, i + 1, replyList[i]);
	}

	int index = TrackPopupMenu(hMenu, TPM_RETURNCMD, cbcd->pt.x, cbcd->pt.y, 0, cbcd->hwndFrom, nullptr);
	if (index > 0) {
		if (mir_wstrcmp(replyList[index - 1], L"")) {
			CallService(MS_MSG_SENDMESSAGEW, cbcd->hContact, (LPARAM)replyList[index - 1]);

			mir_snprintf(key, "ImmediatelySend_%x", iNumber);
			if (db_get_b(NULL, MODULENAME, key, 1) || cbcd->flags & BBCF_CONTROLPRESSED)
				SendMessage(cbcd->hwndFrom, WM_COMMAND, IDOK, 0);
		}
	}

	for (auto &it : replyList)
		mir_free(it);
	replyList.destroy();

	return 1;
}