summaryrefslogtreecommitdiff
path: root/plugins/Scriver/src/sendqueue.cpp
blob: d96cb670e333cc4250720577c023f2c9242de9f3 (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
/*
Scriver

Copyright (c) 2000-09 Miranda ICQ/IM project,

all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "stdafx.h"

static MessageSendQueueItem *global_sendQueue = NULL;
static mir_cs queueMutex;

TCHAR* GetSendBufferMsg(MessageSendQueueItem *item)
{
	return mir_utf8decodeW(item->sendBuffer);
}

MessageSendQueueItem* CreateSendQueueItem(HWND hwndSender)
{
	MessageSendQueueItem *item = (MessageSendQueueItem*)mir_calloc(sizeof(MessageSendQueueItem));

	mir_cslock lck(queueMutex);
	item->hwndSender = hwndSender;
	item->next = global_sendQueue;
	if (global_sendQueue != NULL)
		global_sendQueue->prev = item;

	global_sendQueue = item;
	return item;
}

MessageSendQueueItem* FindOldestPendingSendQueueItem(HWND hwndSender, MCONTACT hContact)
{
	mir_cslock lck(queueMutex);
	for (MessageSendQueueItem *item = global_sendQueue; item != NULL; item = item->next)
		if (item->hwndSender == hwndSender && item->hContact == hContact && item->hwndErrorDlg == NULL)
			return item;

	return NULL;
}

MessageSendQueueItem* FindSendQueueItem(MCONTACT hContact, HANDLE hSendId)
{
	mir_cslock lock(queueMutex);
	for (MessageSendQueueItem *item = global_sendQueue; item != NULL; item = item->next)
		if (item->hContact == hContact && item->hSendId == hSendId)
			return item;

	return NULL;
}

BOOL RemoveSendQueueItem(MessageSendQueueItem* item)
{
	HWND hwndSender = item->hwndSender;

	mir_cslock lock(queueMutex);
	if (item->prev != NULL)
		item->prev->next = item->next;
	else
		global_sendQueue = item->next;

	if (item->next != NULL)
		item->next->prev = item->prev;

	mir_free(item->sendBuffer);
	mir_free(item->proto);
	mir_free(item);

	for (item = global_sendQueue; item != NULL; item = item->next)
		if (item->hwndSender == hwndSender)
			return FALSE;

	return TRUE;
}

void ReportSendQueueTimeouts(HWND hwndSender)
{
	MessageSendQueueItem *item, *item2;
	int timeout = db_get_dw(NULL, SRMMMOD, SRMSGSET_MSGTIMEOUT, SRMSGDEFSET_MSGTIMEOUT);

	mir_cslock lock(queueMutex);

	for (item = global_sendQueue; item != NULL; item = item2) {
		item2 = item->next;
		if (item->timeout < timeout) {
			item->timeout += 1000;
			if (item->timeout >= timeout) {
				if (item->hwndSender == hwndSender && item->hwndErrorDlg == NULL) {
					if (hwndSender != NULL) {
						ErrorWindowData *ewd = (ErrorWindowData *)mir_alloc(sizeof(ErrorWindowData));
						ewd->szName = GetNickname(item->hContact, item->proto);
						ewd->szDescription = mir_tstrdup(TranslateT("The message send timed out."));
						ewd->szText = GetSendBufferMsg(item);
						ewd->hwndParent = hwndSender;
						ewd->queueItem = item;
						PostMessage(hwndSender, DM_SHOWERRORMESSAGE, 0, (LPARAM)ewd);
					}
					else {
						/* TODO: Handle errors outside messaging window in a better way */
						RemoveSendQueueItem(item);
					}
				}
			}
		}
	}
}

void ReleaseSendQueueItems(HWND hwndSender)
{
	mir_cslock lock(queueMutex);

	for (MessageSendQueueItem *item = global_sendQueue; item != NULL; item = item->next) {
		if (item->hwndSender == hwndSender) {
			item->hwndSender = NULL;
			if (item->hwndErrorDlg != NULL)
				DestroyWindow(item->hwndErrorDlg);

			item->hwndErrorDlg = NULL;
		}
	}
}

int ReattachSendQueueItems(HWND hwndSender, MCONTACT hContact)
{
	int count = 0;

	mir_cslock lock(queueMutex);

	for (MessageSendQueueItem *item = global_sendQueue; item != NULL; item = item->next) {
		if (item->hContact == hContact && item->hwndSender == NULL) {
			item->hwndSender = hwndSender;
			item->timeout = 0;
			count++;
		}
	}
	return count;
}

void RemoveAllSendQueueItems()
{
	MessageSendQueueItem *item, *item2;
	mir_cslock lock(queueMutex);
	for (item = global_sendQueue; item != NULL; item = item2) {
		item2 = item->next;
		RemoveSendQueueItem(item);
	}
}

void SendSendQueueItem(MessageSendQueueItem* item)
{
	mir_cslockfull lock(queueMutex);
	item->timeout = 0;

	if (item->prev != NULL) {
		item->prev->next = item->next;
		if (item->next != NULL)
			item->next->prev = item->prev;

		item->next = global_sendQueue;
		item->prev = NULL;
		if (global_sendQueue != NULL)
			global_sendQueue->prev = item;

		global_sendQueue = item;
	}
	lock.unlock();

	item->hSendId = (HANDLE)CallContactService(item->hContact, PSS_MESSAGE, item->flags, (LPARAM)item->sendBuffer);
}