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