3
.import QtQuick.LocalStorage 2.0 as LS
5
Array.prototype.append = function(x) { this.push(x) }
10
function getEvents(termStart, termEnd, events)
16
result = tx.executeSql('\
18
where (? <= startTime and startTime < ?) or \
19
(? < endTime and endTime <= ?) or \
20
(startTime <= ? and ? <= endTime) \
22
[ termStart, termEnd, termStart, termEnd, termStart, termEnd ]
29
for (var i = 0; i < result.rows.length; ++i) {
30
var e = result.rows.item(i)
31
e.startTime = new Date(e.startTime)
32
e.endTime = new Date(e.endTime)
39
function getAttendees(event, attendees)
45
result = tx.executeSql('\
46
select c.* from Attendance a, Contact c \
47
where a.eventId = ? and a.contactId = c.id \
54
attendees = attendees || []
56
for (var i = 0; i < result.rows.length; ++i)
57
attendees.append(result.rows.item(i))
62
function addEvent(event)
68
result = tx.executeSql('\
69
insert into Event(title, message, startTime, endTime) \
71
[ event.title, event.message, event.startTime, event.endTime ]
76
event.id = result.insertId
78
eventsNotifier().dataChanged()
83
function removeEvent(event)
88
'delete from Event where id = ?',
92
'delete from Attendance where eventId = ?',
96
'delete from Venue where eventId = ?',
104
eventsNotifier().dataChanged()
107
function getContacts(contacts)
111
db().readTransaction(
113
result = tx.executeSql('select * from Contact order by name')
117
contacts = contacts || []
119
for (var i = 0; i < result.rows.length; ++i)
120
contacts.append(result.rows.item(i))
125
function addAttendee(event, contact)
130
'insert into Attendance(eventId, contactId) values (?, ?)',
131
[ event.id, contact.id ]
137
function removeAttendee(event, contact)
142
'delete from Attendance where eventId = ? and contactId = ?',
143
[ event.id, contact.id ]
149
function getPlaces(places)
153
db().readTransaction(
155
result = tx.executeSql('select * from Place')
159
places = places || []
161
for (var i = 0; i < result.rows.length; ++i)
162
places.append(result.rows.item(i))
167
function addPlace(place)
171
if (typeof place.address == 'undefined') place.address = null
172
if (typeof place.latitude == 'undefined') place.latitude = null
173
if (typeof place.longitude == 'undefined') place.longitude = null
177
result = tx.executeSql(
178
'insert into Place(name, address, latitude, longitude) values(?, ?, ?, ?)',
179
[ place.name, place.address, place.latitude, place.longitude ]
184
place.id = result.insertId
189
function removePlace(place)
194
'delete from Place where id = ?',
198
'delete from Venue where placeId = ?',
207
function addVenue(event, place)
212
'insert into Venue(eventId, placeId) values(?, ?)',
213
[ event.id, place.id ]
219
function removeVenue(event, place)
224
'delete from Venue where eventId = ? and placeId = ?',
225
[ event.id, place.id ]
231
function getVenues(event, venues)
235
db().readTransaction(
237
result = tx.executeSql('\
239
from Venue v, Place p \
240
where v.eventId = ? and p.id = v.placeId \
247
venues = venues || []
249
for (var i = 0; i < result.rows.length; ++i)
250
venues.append(result.rows.item(i))
255
function printEvent(event)
257
console.log('Event', event)
258
console.log(' id:', event.id)
259
console.log(' title:', event.title)
260
console.log(' message:', event.message)
261
console.log(' startTime:', new Date(event.startTime).toLocaleString())
262
console.log(' endTime:', new Date(event.endTime).toLocaleString())
266
getAttendees(event, attendees)
267
getVenues(event, venues)
268
for (var j = 0; j < attendees.length; ++j)
269
printContact(attendees[j])
270
for (var j = 0; j < venues.length; ++j)
271
printPlace(venues[j])
275
function printContact(contact)
277
console.log('Contact', contact)
278
console.log(' id:', contact.id)
279
console.log(' name:', contact.name)
280
console.log(' surname:', contact.surname)
281
console.log(' avatar:', contact.avatar)
284
function printPlace(place)
286
console.log('Place', place)
287
console.log(' name:', place.name)
288
console.log(' address:', place.address)
289
console.log(' latitude:', place.latitude)
290
console.log(' longitude:', place.longitude)
293
function __createFirstTime(tx)
297
id integer primary key,\
302
category text default "Events"\
305
create index EventStartTimeIndex on Event(startTime);\
306
create index EventEndTimeIndex on Event(endTime);\
309
id integer primary key,\
316
create table Contact(\
317
id integer primary key,\
323
create table Attendance(\
324
id integer primary key,\
325
eventId integer references Event(id) on delete cascade,\
326
contactId integer references Contact(id) on delete cascade,\
327
placeId integer references Place(id) on delete set null\
331
id integer primary key,\
332
eventId integer references Event(id) on delete cascade,\
333
placeId integer references Place(id) on delete cascade\
338
for (var i = 0; i < schema.length; ++i) {
347
function eventsNotifier()
349
if (!eventsNotifier.hasOwnProperty("instance"))
350
eventsNotifier.instance = Qt.createQmlObject('import QtQuick 2.0; QtObject { signal dataChanged }', Qt.application, 'DataService.eventsNotifier()')
351
return eventsNotifier.instance
356
if (!db.hasOwnProperty("instance")) {
357
db.instance = LS.LocalStorage.openDatabaseSync("Calendar", "", "Offline Calendar", 100000)
358
if (db.instance.version == "") db.instance.changeVersion("", "0.1", __createFirstTime)