summaryrefslogtreecommitdiff
path: root/plugins/Dbrw/contacts.c
blob: 7d849a0174a507695ce243f58ae5ab40c7867ed2 (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
/*
dbRW
Copyright (c) 2005-2009 Robert Rainwater

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

static CRITICAL_SECTION csContactsDb;
static SortedList sContactList;

static int contacts_compare(void* p1, void* p2);

enum {
	SQL_CTC_STMT_DELETE=0,
	SQL_CTC_STMT_ADD,
	SQL_CTC_STMT_NUM
};
static char *ctc_stmts[SQL_CTC_STMT_NUM] = {
	"DELETE FROM dbrw_contacts WHERE id = ?;",
	"INSERT INTO dbrw_contacts VALUES(NULL,?);"
};
static sqlite3_stmt *ctc_stmts_prep[SQL_CTC_STMT_NUM] = {0};

void contacts_init() {
	InitializeCriticalSection(&csContactsDb);
	ZeroMemory(&sContactList, sizeof(sContactList));
	sContactList.increment = 50;
	sContactList.sortFunc = contacts_compare;
	sql_prepare_add(ctc_stmts, ctc_stmts_prep, SQL_CTC_STMT_NUM);
	{
		sqlite3_stmt *st = NULL;
		int id, idx;

		if (sql_prepare(g_sqlite, "SELECT * FROM dbrw_contacts;", &st)!=SQLITE_OK) {
			return;
		}
		while (sql_step(st)==SQLITE_ROW) {
			id = (int)sqlite3_column_int(st, 0);
			if(!li.List_GetIndex(&sContactList, (void*)id, &idx)) {
				li.List_Insert(&sContactList, (void*)id, idx);
			}
		}
		sql_finalize(st);
	}
}

void contacts_destroy() {
	li.List_Destroy(&sContactList);
	DeleteCriticalSection(&csContactsDb);
}

static int contacts_compare(void* p1, void* p2) {
	return (int)p1-(int)p2;
}

static int contacts_isRealContact(int id) {
	int idx;

	if(li.List_GetIndex(&sContactList, (void*)id, &idx))
		return 1;
	log1("Invalid contact id requested (%d)", id);
	return 0;
}

INT_PTR contacts_getCount(WPARAM wParam, LPARAM lParam) {
	int rc;

	EnterCriticalSection(&csContactsDb);
	rc = sContactList.realCount;
	LeaveCriticalSection(&csContactsDb);
	return rc;
}

INT_PTR contacts_findFirst(WPARAM wParam, LPARAM lParam) {
	int rc;

	EnterCriticalSection(&csContactsDb);
	if (sContactList.realCount==0) {
		LeaveCriticalSection(&csContactsDb);
		return 0;
	}
	rc = (int)sContactList.items[0];
	LeaveCriticalSection(&csContactsDb);
	return rc;
}

INT_PTR contacts_findNext(WPARAM wParam, LPARAM lParam) {
	int id = (int)wParam;
	int rc, idx;

	EnterCriticalSection(&csContactsDb);
	if (sContactList.realCount==0) {
		LeaveCriticalSection(&csContactsDb);
		return 0;
	}
	if(!li.List_GetIndex(&sContactList, (void*)id, &idx)) {
		LeaveCriticalSection(&csContactsDb);
		return 0;
	}
	if (idx>=(sContactList.realCount-1)) {
		LeaveCriticalSection(&csContactsDb);
		return 0;
	}
	rc = (int)sContactList.items[idx+1];
	LeaveCriticalSection(&csContactsDb);
	return rc;
}

INT_PTR contacts_delete(WPARAM wParam, LPARAM lParam) {
	int id = (int)wParam;
	int rc = 0;
	
	EnterCriticalSection(&csContactsDb);
	if (!contacts_isRealContact(id)) {
		rc = 1;
	}
	else {
		int idx;
		
        LeaveCriticalSection(&csContactsDb);
		NotifyEventHooks(hContactDeletedEvent, wParam, 0);
        EnterCriticalSection(&csContactsDb);
		li.List_GetIndex(&sContactList, (void*)id, &idx);
		li.List_Remove(&sContactList, idx);
		
        // Begin Transaction
        sql_stmt_begin();
        
		// Delete contact
		sqlite3_bind_int(ctc_stmts_prep[SQL_CTC_STMT_DELETE], 1, id);
		sql_step(ctc_stmts_prep[SQL_CTC_STMT_DELETE]);
		sql_reset(ctc_stmts_prep[SQL_CTC_STMT_DELETE]);
        
		// Delete contact's settings
		settings_deleteContactData((HANDLE)id);

		// Delete contact's events
        events_deleteContactData((HANDLE)id);
        
        // Commit transaction
        sql_stmt_end();
        log1("Deleted contact (%d) and associated data", id);
	}
	LeaveCriticalSection(&csContactsDb);
	return rc;
}

INT_PTR contacts_add(WPARAM wParam, LPARAM lParam) {
	int id = 0;
	int idx;

	EnterCriticalSection(&csContactsDb);
	sqlite3_bind_int(ctc_stmts_prep[SQL_CTC_STMT_ADD], 1, time(NULL));
	if (sql_step(ctc_stmts_prep[SQL_CTC_STMT_ADD])==SQLITE_DONE) {
		id = (int)sqlite3_last_insert_rowid(g_sqlite);
		if (!li.List_GetIndex(&sContactList, (void*)id, &idx))
			li.List_Insert(&sContactList, (void*)id, idx);
	}
	sql_reset(ctc_stmts_prep[SQL_CTC_STMT_ADD]);
	LeaveCriticalSection(&csContactsDb);
	if (id)
		NotifyEventHooks(hContactAddedEvent, (WPARAM)id, 0);
    else 
        log0("Error creating contact");
	return id;
}

INT_PTR contacts_isContact(WPARAM wParam, LPARAM lParam) {
	int id = (int)wParam;
	int rc = 0;

	EnterCriticalSection(&csContactsDb);
	if (contacts_isRealContact(id))
		rc = 1;
	LeaveCriticalSection(&csContactsDb);
	return rc;
}