summaryrefslogtreecommitdiff
path: root/plugins/Alarms/src/alarmlist.h
blob: 22f3b8d1c2f84798eda0492c837114b2a56d7d88 (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
#ifndef _ALARMLIST_INC
#define _ALARMLIST_INC

#include "options.h"
#include "time_utils.h"
#include "alarm_win.h"

#include "m_alarms.h"

void free_alarm_data(ALARM *alarm);

void copy_alarm_data(ALARM *dest, ALARM *src);

int MinutesInFuture(SYSTEMTIME time, Occurrence occ, int selected_days = 0);
void TimeForMinutesInFuture(int mins, SYSTEMTIME *time);

static bool operator<(const ALARM &a1, const ALARM &a2) {
	return MinutesInFuture(a1.time, a1.occurrence, a1.day_mask) < MinutesInFuture(a2.time, a2.occurrence, a2.day_mask); // less-than inverted 'cause we want ascending order
}

class AlarmList {
public:
	AlarmList(): head(0), tail(0), count(0) {}

	virtual ~AlarmList() {clear();}

	void sort() {
		if (count < 2) return;

		Node *c1 = head, *c2;
		// bubble sort...hey, i'm lazy :)
		while(c1) {
			c2 = c1->next;
			while(c2) {
				if (c2->alarm < c1->alarm) {
					swap (c1, c2);
				}
				c2 = c2->next;
			}
			c1 = c1->next;
		}
	}

	void clear() {
		Node *current;
		while(head) {
			current = head;
			head = head->next;
			free_alarm_data(&current->alarm);
			delete current;
		}
		count = 0;
		tail = 0;
		reset();
	}

	int size() {return count;}

	ALARM &at(int index) {
		int i = 0;
		Node *current = head;
		while(i < index && i < count && current) {
			current = current->next;
			i++;
		}
		return current->alarm;
	}

	void reset() {it_current = head;}
	ALARM *current() {return (it_current ? &it_current->alarm : 0);}
	void next() {it_current = it_current->next;}
	void erase() {
		if (it_current) {
			if (it_current->next) it_current->next->prev = it_current->prev;
			if (it_current->prev) it_current->prev->next = it_current->next;

			if (tail == it_current) tail = tail->prev;
			if (head == it_current) head = head->next;

			free_alarm_data(&it_current->alarm);
			delete it_current;
			count--;
			reset();
		}
	}

	// copies the alarm into the list
	void push_back(ALARM *alarm) {
		Node *nn = new Node;
		memset(&nn->alarm, 0, sizeof(ALARM));
		copy_alarm_data(&nn->alarm, alarm);

		nn->prev = tail;
		if (tail) tail->next = nn;
		tail = nn;

		if (!head) head = tail;
		count++;
	}

protected:
	class Node {
	public:
		Node(): next(0), prev(0) {}
		ALARM alarm;
		Node *next, *prev;
	};

	Node *head, *tail, *it_current;
	int count;

	void swap(Node *n1, Node *n2) {
		ALARM temp = n1->alarm;
		n1->alarm = n2->alarm;
		n2->alarm = temp;
	}
};

//extern AlarmList alarms;

void LoadAlarms();
void SaveAlarms();

void InitList();
void DeinitList();

void copy_list(AlarmList &copy);
void copy_list(AlarmList &copy, SYSTEMTIME &start, SYSTEMTIME &end);

void set_list(AlarmList &copy);

void append_to_list(ALARM *alarm);
void alter_alarm_list(ALARM *alarm);
void remove(unsigned short alarm_id);

void suspend(unsigned short alarm_id);

void GetPluginTime(SYSTEMTIME *t);

// increase 'time' to next occurrence
bool UpdateAlarm(SYSTEMTIME &time, Occurrence occ, int selected_days = 0);


const ULARGE_INTEGER mult = { 600000000, 0}; // number of 100 microsecond blocks in a minute

extern unsigned short next_alarm_id;

#endif