summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/mir_contactqueue.h
blob: 32ccdd1eaa2bddab9f5e0182e4f9ea7e187cb028 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* 
Copyright ©2006 Ricardo Pescuma Domenecci

Modified  ©2008-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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.
*/


#ifndef __CONTACTASYNCQUEUE_H__
#define __CONTACTASYNCQUEUE_H__

/**
 *
 *
 **/
struct CQueueItem
{
	DWORD	check_time;
	MCONTACT hContact;
	PVOID	param;
};

/**
 *
 *
 **/
class CContactQueue
{
public:

	enum EQueueStatus
	{
		RUNNING		= 0,
		STOPPING	= 1,
		STOPPED		= 2
	};

	CContactQueue				(int initialSize = 10);
	~CContactQueue				();

	inline int			Size	()			const	{ return _queue.getCount();}
	inline int			Remove	(int idx)			{ mir_free(_queue[idx]); return _queue.remove(idx);}
	inline CQueueItem*	Get		(int idx) const		{ return _queue[idx];}


	void RemoveAll();
	
	/**
	 * This function removes all queue items for the hContact.
	 *
	 * @param		hContact		- the contact whose queue items to delete
	 *
	 * @return	nothing
	 **/
	void RemoveAll(MCONTACT hContact);

	/**
	 * This function removes all queue items for the hContact considering the correct parameter.
	 *
	 * @param		hContact		- the contact whose queue items to delete
	 * @param		param			- a caller defined parameter passed to the callback function
	 *
	 * @return	nothing
	 **/
	void RemoveAllConsiderParam(MCONTACT hContact, PVOID param);

	/**
	 * This method adds the desired new item.
	 *
	 * @param		waitTime		- the time to wait until the callback is desired to run
	 * @param		hContact		- the contact to perform the action for
	 * @param		param			- a caller defined parameter passed to the callback function
	 *
	 * @retval		TRUE			- The item is added to the queue successfully.
	 * @retval		FALSE			- The item is not added to the queue.
	 **/
	BOOL Add(int waitTime, MCONTACT hContact, PVOID param = NULL);

	/**
	 * This method adds the desired new item only, if the queue does not yet contain
	 * an item for the contact.
	 *
	 * @param		waitTime		- the time to wait until the callback is desired to run
	 * @param		hContact		- the contact to perform the action for
	 * @param		param			- a caller defined parameter passed to the callback function
	 *
	 * @retval		TRUE			- The item is added to the queue successfully.
	 * @retval		FALSE			- The item is not added to the queue.
	 **/
	BOOL AddIfDontHave(int waitTime, MCONTACT hContact, PVOID param = NULL);

	/**
	 * This method removes all existing queue items for the contact and adds a new queue item
	 * for the given contact. This method might be used to move an existing entry, 
	 * whose check_time has changed.
	 *
	 * @param		waitTime		- the time to wait until the callback is desired to run
	 * @param		hContact		- the contact to perform the action for
	 * @param		param			- a caller defined parameter passed to the callback function
	 *
	 * @return		nothing
	 **/
	BOOL AddUnique(int waitTime, MCONTACT hContact, PVOID param = NULL);

	/**
	 * This method removes all existing queue items for the contact with the same parameter as @e param
	 * and adds a new queue item for the given contact. This method might be used to move an existing
	 * entry, whose check_time has changed.
	 *
	 * @param		waitTime		- the time to wait until the callback is desired to run
	 * @param		hContact		- the contact to perform the action for
	 * @param		param			- a caller defined parameter passed to the callback function
	 *
	 * @return	nothing
	 **/
	BOOL AddUniqueConsiderParam	(int waitTime, MCONTACT hContact, PVOID param = NULL);

	/**
	 * This method resumes the worker thread and immitiatly goes on with the next entry.
	 *
	 * @param		none
	 *
	 * @return		nothing
	 **/
	void ContinueWithNext();

protected:
	
	virtual void OnEmpty		() {};
	virtual void Callback		(MCONTACT hContact, PVOID param) = 0;

	/**
	 * This is the real thread callback function. As long as _status
	 * is set to RUNNING it looks for items in the queue to perform 
	 * the _pfnCallback function on them. If the queue is empty or the
	 * next upcoming item is located in the future, the thread is suspended
	 * in the meanwhile.
	 *
	 * @param		none
	 *
	 * @return	nothing
	 **/
	void Thread();
	
	/**
	 * This is a static method to redirect the thread's callback function
	 * to the desired class object.
	 *
	 * @param		obj	- pointer to the object (instance) of CContactQueue
	 *
	 * @return	nothing
	 **/
	static void ThreadProc(CContactQueue* obj)
	{
		obj->Thread();
	}

	/**
	 * This method suspends the worker thread for the given ammount of time.
	 *
	 * @param		time	- milliseconds to suspend the thread for
	 *
	 * @return	nothing
	 **/
	void Suspend(int time) const;

private:

	LIST<CQueueItem> _queue;

	mir_cs _cs;
	HANDLE _hEvent;
	EQueueStatus _status;

	/**
	 * This member function really adds an item into the time sorted queue list.
	 *
	 * @param		waitTime	- the time to wait until the callback is desired to run
	 * @param		hContact	- the contact to perform the action for
	 * @param		param		- a caller defined parameter passed to the callback function
	 *
	 * @retval		TRUE		- The item is added to the queue successfully.
	 * @retval		FALSE		- The item is not added to the queue.
	 **/
	BOOL InternalAdd(int waitTime, MCONTACT hContact, PVOID param);
};

#endif // __CONTACTASYNCQUEUE_H__