summaryrefslogtreecommitdiff
path: root/plugins/NewXstatusNotify/src/popup.cpp
blob: d691d12118d8f1f8bd4599db8fab2bb54ef9f5c7 (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
/*
	NewXstatusNotify YM - Plugin for Miranda IM
	Copyright (c) 2001-2004 Luca Santarelli
	Copyright (c) 2005-2007 Vasilich
	Copyright (c) 2007-2011 yaho

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

	This program 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 General Public License for more details.

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

#include "common.h"

static int AwayMsgHook(WPARAM wParam, LPARAM lParam, LPARAM pObj)
{
	PLUGINDATA *pdp = (PLUGINDATA*)pObj;
	if (pdp == NULL)
		return 0;

	ACKDATA *ack = (ACKDATA *)lParam;
	if (ack->type != ACKTYPE_AWAYMSG || ack->hProcess != pdp->hAwayMsgProcess)
		return 0;

	//The first thing we go is removing the hook from the chain to avoid useless calls.
	UnhookEvent(pdp->hAwayMsgHook);
	pdp->hAwayMsgHook = NULL;

	if (ack->result != ACKRESULT_SUCCESS)
		return 0;

	MCONTACT hContact = PUGetContact(pdp->hWnd);
	ptrT pstzLast( db_get_tsa(hContact, MODULE, "LastPopupText"));

	TCHAR *tszStatus = (TCHAR*)ack->lParam;
	if (tszStatus == NULL || *tszStatus == 0)
		return 0;

	TCHAR stzText[1024];
	if (pstzLast)
		mir_sntprintf(stzText, SIZEOF(stzText), _T("%s\n%s"), pstzLast, tszStatus);
	else
		_tcsncpy(stzText, tszStatus, SIZEOF(stzText));
	SendMessage(pdp->hWnd, WM_SETREDRAW, FALSE, 0);
	PUChangeTextT(pdp->hWnd, stzText);
	SendMessage(pdp->hWnd, WM_SETREDRAW, TRUE, 0);
	return 0;
}

void QueryAwayMessage(HWND hWnd, PLUGINDATA *pdp)
{
	MCONTACT hContact = PUGetContact(hWnd);
	char *szProto = GetContactProto(hContact);
	if (szProto)
	{
		if ((CallProtoService(szProto, PS_GETCAPS,PFLAGNUM_1, 0) & PF1_MODEMSGRECV) &&
			(CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_3, 0) & Proto_Status2Flag(pdp->newStatus)))
		{
			pdp->hWnd = hWnd;
			//The following HookEventMessage will hook the ME_PROTO_ACK event and send a WM_AWAYMSG to hWnd when the hooks get notified.
			pdp->hAwayMsgHook = HookEventParam(ME_PROTO_ACK, AwayMsgHook, (LPARAM)pdp);
			//The following instruction asks Miranda to retrieve the away message and associates a hProcess (handle) to this request. This handle will appear in the ME_PROTO_ACK event.
			pdp->hAwayMsgProcess = (HANDLE)CallContactService(hContact, PSS_GETAWAYMSG, 0, 0);
		}
	}
}

void PopupAction(HWND hWnd, BYTE action)
{
	MCONTACT hContact = PUGetContact(hWnd);
	if (hContact && hContact != INVALID_CONTACT_ID) {
		switch (action) {
		case PCA_OPENMESSAGEWND:
			CallServiceSync(MS_MSG_SENDMESSAGET, hContact, 0);
			break;

		case PCA_OPENMENU:
			{
				POINT pt = {0};
				HMENU hMenu = (HMENU)CallService(MS_CLIST_MENUBUILDCONTACT, hContact, 0);
				GetCursorPos(&pt);
				TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
				DestroyMenu(hMenu);
			}
			return;

		case PCA_OPENDETAILS:
			CallServiceSync(MS_USERINFO_SHOWDIALOG, hContact, 0);
			break;

		case PCA_OPENHISTORY:
			CallServiceSync(MS_HISTORY_SHOWCONTACTHISTORY, hContact, 0);
			break;

		case PCA_CLOSEPOPUP:
			break;

		case PCA_DONOTHING:
			return;
		}

		PUDeletePopup(hWnd);
	}
}

LRESULT CALLBACK PopupDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PLUGINDATA *pdp = NULL;

	switch(message) {
	case WM_MEASUREITEM: //Needed by the contact's context menu
		return CallService(MS_CLIST_MENUMEASUREITEM, wParam, lParam);

	case WM_DRAWITEM: //Needed by the contact's context menu
		return CallService(MS_CLIST_MENUDRAWITEM, wParam, lParam);

	case WM_COMMAND:
		//This one returns TRUE if it processed the menu command, and FALSE if it did not process it.
		if (CallServiceSync(MS_CLIST_MENUPROCESSCOMMAND, MAKEWPARAM(LOWORD(wParam), MPCF_CONTACTMENU), (LPARAM)PUGetContact(hwnd)))
			break;

		PopupAction(hwnd, opt.LeftClickAction);
		break;

	case WM_CONTEXTMENU:
		PopupAction(hwnd, opt.RightClickAction);
		break;

	case UM_FREEPLUGINDATA:
		pdp = (PLUGINDATA *)PUGetPluginData(hwnd);
		if (pdp != NULL) {
			if (pdp->hAwayMsgHook != NULL) {
				UnhookEvent(pdp->hAwayMsgHook);
				pdp->hAwayMsgHook = NULL;
			}

			mir_free(pdp);
		}
		return FALSE;

	case UM_INITPOPUP:
		pdp = (PLUGINDATA *)PUGetPluginData(hwnd);
		if (pdp != NULL) {
			char *szProto = GetContactProto( PUGetContact(hwnd));
			if (szProto && opt.ReadAwayMsg && StatusHasAwayMessage(szProto, pdp->newStatus)) {
				WORD myStatus = (WORD)CallProtoService(szProto, PS_GETSTATUS, 0, 0);
				if (myStatus != ID_STATUS_INVISIBLE)
					QueryAwayMessage(hwnd, pdp);
			}
		}

		return FALSE;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}