summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/history_array.h
blob: 955e63b1b5838cb4b351cb2fe76cebe923acb51b (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
202
203
204
205
206
207
208
209
210
211
212
#ifndef __history_array__
#define __history_array__

enum EventLoadMode
{
	ELM_NOTHING,
	ELM_INFO,
	ELM_DATA
};

enum
{
	HIF_SELECTED = 0x01,

	FILTER_TIME = 0x01,
	FILTER_TYPE = 0x02,
	FILTER_DIRECTION = 0x04,
	FILTER_TEXT = 0x08,
	FILTER_UNICODE = 0x10,

	FTYPE_MESSAGE = 0x01,
	FTYPE_FILE = 0x02,
	FTYPE_URL = 0x04,
	FTYPE_STATUS = 0x08,
	FTYPE_OTHER = 0x10,
	FTYPE_INCOMING = 0x20,
	FTYPE_OUTGOING = 0x40
};

class HistoryArray
{
public:
	struct ItemData
	{
		BYTE flags = 0;

		MCONTACT hContact = 0;
		MEVENT hEvent = 0;

		bool dbeOk = false;
		DBEVENTINFO dbe;

		bool atext_del = false, wtext_del = false;
		char *atext = 0;
		WCHAR *wtext = 0;

		HANDLE data = 0;

		ItemData() { memset(&dbe, 0, sizeof(dbe)); }
		~ItemData();

		bool load(EventLoadMode mode);
		inline bool loadInline(EventLoadMode mode)
		{
			if (((mode >= ELM_INFO) && !dbeOk) || ((mode == ELM_DATA) && !dbe.pBlob))
				return load(mode);
			return true;
		}
		inline TCHAR *getTBuf()
		{
			loadInline(ELM_DATA);
			#ifdef UNICODE
			return wtext;
			#else
			return atext;
			#endif
		}
		inline char *getBuf()
		{
			loadInline(ELM_DATA);
			return atext;
		}
		inline WCHAR *getWBuf()
		{
			loadInline(ELM_DATA);
			return wtext;
		}
	};

	struct ItemBlock
	{
		ItemData *items;
		int count;
		ItemBlock *prev, *next;
	};

	class Filter
	{
	public:
		enum
		{
			INCOMING = 0x001,
			OUTGOING = 0x002,
			MESSAGES = 0x004,
			FILES = 0x008,
			STATUS = 0x020,
			OTHER = 0x040,
			EVENTTEXT = 0x080,
			EVENTONLY = 0x100,
		};
		Filter(WORD aFlags, TCHAR *aText)
		{
			refCount = new int(0);
			flags = aFlags;
			text = new TCHAR[lstrlen(aText) + 1];
			lstrcpy(text, aText);
		}
		Filter(const Filter &other)
		{
			flags = other.flags;
			refCount = other.refCount;
			text = other.text;
			++*refCount;
		}
		Filter &operator=(const Filter &other)
		{
			flags = other.flags;
			refCount = other.refCount;
			text = other.text;
			++*refCount;
		}
		~Filter()
		{
			if (!--*refCount) {
				delete refCount;
				if (text) delete[] text;
			}
		}
		inline bool check(ItemData *item)
		{
			if (!item) return false;
			if (!(flags & EVENTONLY)) {
				if (item->dbe.flags & DBEF_SENT) {
					if (!(flags & OUTGOING))
						return false;
				}
				else {
					if (!(flags & INCOMING))
						return false;
				}
				switch (item->dbe.eventType) {
				case EVENTTYPE_MESSAGE:
					if (!(flags & MESSAGES))
						return false;
					break;
				case EVENTTYPE_FILE:
					if (!(flags & FILES))
						return false;
					break;
				case EVENTTYPE_STATUSCHANGE:
					if (!(flags & STATUS))
						return false;
					break;
				default:
					if (!(flags & OTHER))
						return false;
				}
			}
			if (flags & (EVENTTEXT | EVENTONLY)) {
				item->loadInline(ELM_DATA);
				return CheckFilter(item->getTBuf(), text);
			}
			return true;
		};

	private:
		WORD flags;
		int *refCount;
		TCHAR *text;
	};

private:
	ItemBlock *head = 0, *tail = 0;
	ItemBlock *preBlock = 0;
	int preIndex = 0;
	bool allocateBlock(int count);

public:
	HistoryArray();
	~HistoryArray();

	void clear();
	bool addHistory(MCONTACT hContact, EventLoadMode mode = ELM_NOTHING);
	bool addEvent(MCONTACT hContact, MEVENT hEvent, EventLoadMode mode = ELM_NOTHING);

	//	bool preloadEvents(int count = 10);

	ItemData *get(int id, EventLoadMode mode = ELM_NOTHING);
	ItemData *operator[] (int id) { return get(id, ELM_DATA); }
	ItemData *operator() (int id) { return get(id, ELM_INFO); }

	int FindRel(int id, int dir, Filter filter)
	{
		int count = getCount();
		for (int i = id + dir; (i >= 0) && (i < count); i += dir)
			if (filter.check(get(i)))
				return i;
		return -1;
	}
	int FindNext(int id, Filter filter) { return FindRel(id, +1, filter); }
	int FindPrev(int id, Filter filter) { return FindRel(id, -1, filter); }

	int getCount()
	{
		int res = 0;
		for (ItemBlock *p = head; p; p = p->next)
			res += p->count;
		return res;
	}
};

#endif // __history_array__