|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this fi |
|
13 #include <QList>le in accordance with the terms and conditions |
|
14 ** contained in the Technology Preview License Agreement accompanying |
|
15 ** this package. |
|
16 #include <QSqlRecord> |
|
17 ** |
|
18 ** GNU Lesser General Public License Usage |
|
19 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
20 ** General Public License version 2.1 as published by the Free Software |
|
21 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
22 ** packaging of this file. Please review the following information to |
|
23 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
24 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
25 *#include <QStringList> |
|
26 * |
|
27 ** In addition, as a special exception, Nokia gives you certain additional |
|
28 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
29 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
30 ** |
|
31 ** If you have questions regarding the use of this file, please contact |
|
32 ** Nokia at qt-info@nokia.com. |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** |
|
39 ** |
|
40 ** $QT_END_LICENSE$ |
|
41 ** |
|
42 ****************************************************************************/ |
|
43 |
|
44 #include <QtCore> |
|
45 #include <QDebug> |
|
46 |
|
47 #include "notesmanager.h" |
|
48 |
|
49 NotesManager::NotesManager(QObject *parent) |
|
50 : QObject(parent) |
|
51 { |
|
52 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
|
53 db.setDatabaseName("todoDB"); |
|
54 db.open(); |
|
55 |
|
56 QSqlQuery create; |
|
57 create.exec("CREATE TABLE todolist(id INTEGER PRIMARY KEY, notes VARCHAR(255), date VARCHAR(255))"); |
|
58 |
|
59 nextAlarm(); |
|
60 |
|
61 QTimer *timer = new QTimer(this); |
|
62 QObject::connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm())); |
|
63 timer->start(1000); |
|
64 } |
|
65 |
|
66 void NotesManager::nextAlarm() |
|
67 { |
|
68 QSqlQuery alarmQuery("SELECT * FROM todolist WHERE date > DATETIME('now', 'localtime') ORDER BY date"); |
|
69 if (alarmQuery.next()) { |
|
70 setAlarm(QDateTime::fromString(alarmQuery.value(2).toString(), "yyyy-MM-dd HH:mm:ss")); |
|
71 setMessage(alarmQuery.value(1).toString()); |
|
72 } |
|
73 } |
|
74 |
|
75 void NotesManager::checkAlarm() |
|
76 { |
|
77 QString currStr = QDateTime::currentDateTime().toString(Qt::ISODate); |
|
78 QDateTime curr = QDateTime::fromString(currStr, Qt::ISODate); |
|
79 |
|
80 //qDebug() << "CHECKING..." << getAlarm() << " against now " << curr; |
|
81 if (getAlarm() == curr) |
|
82 emit soundAlarm(getAlarm()); |
|
83 |
|
84 nextAlarm(); |
|
85 } |
|
86 |
|
87 QDateTime NotesManager::getAlarm() const |
|
88 { |
|
89 return m_alarm; |
|
90 } |
|
91 |
|
92 void NotesManager::setAlarm(const QDateTime& alarm) |
|
93 { |
|
94 m_alarm = alarm; |
|
95 } |
|
96 |
|
97 QString NotesManager::getMessage() const |
|
98 { |
|
99 return m_message; |
|
100 } |
|
101 |
|
102 void NotesManager::setMessage(const QString& message) |
|
103 { |
|
104 m_message = message; |
|
105 } |
|
106 |
|
107 void NotesManager::addNote(const QString& note, const QDateTime& alarm) |
|
108 { |
|
109 QString alert = alarm.toString("yyyy-MM-dd HH:mm:ss"); |
|
110 QSqlQuery query("INSERT INTO todolist(notes, date) VALUES ('" + note + "', '" + alert + "')"); |
|
111 } |
|
112 |
|
113 void NotesManager::removeNote(int id) |
|
114 { |
|
115 QSqlQuery query("DELETE FROM todolist WHERE id='" + QString::number(id) + "'"); |
|
116 } |
|
117 |
|
118 QList<Note> NotesManager::getNotes(const QString& search) const |
|
119 { |
|
120 QList<Note> list; |
|
121 |
|
122 QString queryString = "SELECT * FROM todolist"; |
|
123 if (search != "") queryString += " WHERE notes LIKE '%" + search + "%'"; |
|
124 queryString += " ORDER BY date"; |
|
125 |
|
126 QSqlQuery query(queryString); |
|
127 while (query.next()) { |
|
128 Note entry; |
|
129 entry.index = query.value(0).toInt(); |
|
130 entry.message = query.value(1).toString(); |
|
131 entry.alert = QDateTime::fromString(query.value(2).toString(), "yyyy-MM-dd HH:mm:ss"); |
|
132 |
|
133 list << entry; |
|
134 } |
|
135 |
|
136 return list; |
|
137 } |
|
138 |