summaryrefslogtreecommitdiff
path: root/plugins/MessageState/src/messagestate.cpp
blob: d1cd032e36f19b6b866cbfb4553d7eb1be9e9598 (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
#include "stdafx.h"

LONGLONG GetLastSentMessageTime(MCONTACT hContact)
{
	for (MEVENT hDbEvent = db_event_last(hContact); hDbEvent; hDbEvent = db_event_prev(hContact, hDbEvent))
	{
		DBEVENTINFO dbei = { sizeof(dbei) };
		db_event_get(hDbEvent, &dbei);
		if (FLAG_CONTAINS(dbei.flags, DBEF_SENT))
			return dbei.timestamp;
	}
	return -1;
}

__forceinline bool CheckProtoSupport(const char *szProto)
{	return ((szProto != NULL) ? FLAG_CONTAINS(CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_4, 0), PF4_READNOTIFY) : false);
}

void SetSRMMIcon(MCONTACT hContact, SRMM_ICON_TYPE type, time_t time = 0)
{
	if (hContact && arMonitoredWindows.getIndex((HANDLE)hContact) != -1)
	{
		StatusIconData sid = { sizeof(sid) };
		sid.szModule = MODULENAME;
		sid.dwId = 1;
		sid.flags = MBF_TCHAR;

		switch (type)
		{
		case ICON_HIDDEN:
			{
				sid.flags |= MBF_HIDDEN;
				break;
			}
		case ICON_READ:
			{
				sid.hIcon = IcoLib_GetIcon("read_icon");
				CMString tooltip;
				if (db_get_dw(hContact, MODULENAME, DBKEY_MESSAGE_READ_TIME_TYPE, -1) == MRD_TYPE_READTIME)
				{
					TCHAR ttime[64];
					_locale_t locale = _create_locale(LC_ALL, "");
					_tcsftime_l(ttime, _countof(ttime), _T("%X %x"), localtime(&time), locale);
					_free_locale(locale);
					tooltip.Format(L"%s %s", TranslateT("Last message read at"), ttime);
				}
				else
				{
					tooltip = TranslateT("Last message read (unknown time)");
				}
				sid.tszTooltip = tooltip.Detach();
				break;
			}
		case ICON_UNREAD:
			{
				sid.hIcon = IcoLib_GetIcon("unread_icon");
				sid.tszTooltip = TranslateT("Last message is not read");
				break;
			}
		case ICON_FAILED:
			{
				sid.hIcon = IcoLib_GetIcon("fail_icon");
				sid.tszTooltip = TranslateT("Last message was not sent.");
				break;
			}
		case ICON_NOSENT:
			{
				sid.hIcon = IcoLib_GetIcon("nosent_icon");
				sid.tszTooltip = TranslateT("Sending...");
				break;
			}
		default:
			return;
		}

		Srmm_ModifyIcon(hContact, &sid);
	}
}

int IconsUpdate(WPARAM hContact, LONGLONG readtime)
{
	if (hContact == NULL) return 1;

	LONGLONG lasttime = GetLastSentMessageTime(hContact);
	if (lasttime != -1 && readtime != 0)
	{
		SetSRMMIcon(hContact, (readtime >= lasttime) ? ICON_READ : ICON_UNREAD, readtime);
	}
	else 
		SetSRMMIcon(hContact, ICON_HIDDEN);
	return 0;
}

int OnProtoAck(WPARAM, LPARAM lParam)
{
	ACKDATA *pAck = (ACKDATA *)lParam;
	if (pAck && pAck->type == ACKTYPE_MESSAGE && CheckProtoSupport(pAck->szModule))
	{
		if (pAck->result == ACKRESULT_SUCCESS)
		{
			SetSRMMIcon(pAck->hContact, ICON_UNREAD);
		}
		else if (pAck->result == ACKRESULT_FAILED)
		{
			SetSRMMIcon(pAck->hContact, ICON_FAILED);
		}
	}
	return 0;
}

int	OnEventFilterAdd(WPARAM hContact, LPARAM lParam)
{
	DBEVENTINFO *dbei = (DBEVENTINFO *)lParam;
	if (FLAG_CONTAINS(dbei->flags, DBEF_SENT) && CheckProtoSupport(dbei->szModule) && db_get_b(hContact, "Tab_SRMsg", "no_ack", 0))
		SetSRMMIcon(hContact, ICON_NOSENT);
	return 0;
}

int OnSrmmWindowEvent(WPARAM, LPARAM lParam)
{
	MessageWindowEventData *event = (MessageWindowEventData *)lParam;
	if (event == NULL)
		return 0;

	if (event->uType == MSG_WINDOW_EVT_OPEN) {
		const char *szProto = GetContactProto(event->hContact);
		if (CheckProtoSupport(szProto))
		{
			arMonitoredWindows.insert((HANDLE)event->hContact);
			IconsUpdate(event->hContact, db_get_dw(event->hContact, MODULENAME, DBKEY_MESSAGE_READ_TIME, 0));
		}
	}
	else if (event->uType == MSG_WINDOW_EVT_CLOSE)
		arMonitoredWindows.remove(event->hContact);

	return 0;
}

INT_PTR UpdateService(WPARAM hContact, LPARAM lParam)
{
	MessageReadData *mrd = (MessageReadData*)lParam;
	db_set_dw(hContact, MODULENAME, DBKEY_MESSAGE_READ_TIME, mrd->dw_lastTime);
	db_set_dw(hContact, MODULENAME, DBKEY_MESSAGE_READ_TIME_TYPE, mrd->iTimeType);
	IconsUpdate(hContact, mrd->dw_lastTime);
	return 0; 
}

int OnModulesLoaded(WPARAM, LPARAM)
{
	HookEvent(ME_MSG_WINDOWEVENT, OnSrmmWindowEvent);
	HookEvent(ME_PROTO_ACK, OnProtoAck);
	HookEvent(ME_DB_EVENT_FILTER_ADD, OnEventFilterAdd);

	// IcoLib support
	for (size_t i = 0; i < _countof(Icons); i++)
		Icon_Register(g_hInst, MODULENAME, &Icons[i], 1);

	StatusIconData sid = { sizeof(sid) };
	sid.szModule = MODULENAME;
	sid.flags = MBF_HIDDEN;
	sid.dwId = 1;
	Srmm_AddIcon(&sid);

	return 0;
}