summaryrefslogtreecommitdiff
path: root/yapp/popup_history.cpp
blob: b413727635ba4a470ce99fddea3e7e63caebff77 (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
#define STRICT
#define WIN32_LEAN_AND_MEAN

#include "common.h"
#include "popup_history.h"

PopupHistoryList::PopupHistoryList(int renderer)
{
	this->renderer = renderer;
	size = HISTORY_SIZE; //fixed size (at least for now)
	historyData = (PopupHistoryData *) malloc(size * sizeof(PopupHistoryData)); //alloc space for data
	count = 0;
}

PopupHistoryList::~PopupHistoryList()
{
	Clear(); //clear the data strings
	free(historyData); //deallocate the data list
}

void PopupHistoryList::Clear()
{
	int i;
	for (i = 0; i < count; i++)
	{
		DeleteData(i);
	}
	count = 0;
}

int PopupHistoryList::Count()
{
	return count;
}

int PopupHistoryList::Size()
{
	return size;
}

void PopupHistoryList::RemoveItem(int index)
{
	int i;
	DeleteData(index); //free the mem for that particular item
	for (i = index + 1; i < count; i++)
	{
		historyData[i - 1] = historyData[i]; //shift all items to the left
	}
}

void PopupHistoryList::DeleteData(int index)
{
	PopupHistoryData *item = &historyData[index];
	free(item->titleT);
	free(item->messageT);
	item->timestamp = 0; //invalidate item
	item->title = NULL;
	item->message = NULL;
	item->flags = 0;
}

void PopupHistoryList::AddItem(PopupHistoryData item)
{
	if (count >= size)
	{
		RemoveItem(0); //remove first element - the oldest
		count--; //it will be inc'ed later
	}
	historyData[count++] = item; //item has it's relevant strings dupped()
	RefreshPopupHistory(hHistoryWindow, GetRenderer());
}

void PopupHistoryList::Add(char *title, char *message, time_t timestamp)
{
	PopupHistoryData item = {0}; //create a history item
	item.timestamp = timestamp;
	item.title = strdup(title);
	item.message = strdup(message);
	AddItem(item); //add it (flags = 0)
}

void PopupHistoryList::Add(wchar_t *title, wchar_t *message, time_t timestamp)
{
	PopupHistoryData item = {0}; //create an unicode history item
	item.flags = PHDF_UNICODE; //mark it as unicode
	item.timestamp = timestamp;
	item.titleW = _wcsdup(title);
	item.messageW = _wcsdup(message);
	AddItem(item); //add it
}

PopupHistoryData *PopupHistoryList::Get(int index)
{
	if ((index < 0) || (index >= count)) //a bit of sanity check
	{
		return NULL;
	}
	
	return &historyData[index];
}

int PopupHistoryList::GetRenderer()
{
	return renderer;
}

void PopupHistoryList::SetRenderer(int newRenderer)
{
	renderer = newRenderer;
}