examples/notesmanagerplugin/notesmanager.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include <QtCore>
       
    42 #include <QDebug>
       
    43 
       
    44 #include "notesmanager.h"
       
    45 
       
    46 NotesManager::NotesManager(QObject *parent)
       
    47     : QObject(parent)
       
    48 {
       
    49     m_search = "";
       
    50 
       
    51     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
       
    52     db.setDatabaseName("todoDB");
       
    53     db.open();
       
    54 
       
    55     QSqlQuery create;
       
    56     create.exec("CREATE TABLE todolist(id INTEGER PRIMARY KEY, notes VARCHAR(255), date VARCHAR(255))");
       
    57 
       
    58     nextAlarm();
       
    59 
       
    60     QTimer *timer = new QTimer(this);
       
    61     QObject::connect(timer, SIGNAL(timeout()), this, SLOT(checkAlarm()));
       
    62     timer->start(1000);
       
    63 }
       
    64 
       
    65 void NotesManager::nextAlarm()
       
    66 {
       
    67     QSqlQuery alarmQuery("SELECT * FROM todolist WHERE date > DATETIME('now', 'localtime') ORDER BY date");
       
    68     if (alarmQuery.next()) { 
       
    69         setAlarmTime(QDateTime::fromString(alarmQuery.value(2).toString(), "yyyy-MM-dd HH:mm:ss"));
       
    70         setAlarmMessage(alarmQuery.value(1).toString());
       
    71     }
       
    72 }
       
    73 
       
    74 void NotesManager::checkAlarm()
       
    75 {
       
    76     QString currStr = QDateTime::currentDateTime().toString(Qt::ISODate);
       
    77     QDateTime curr = QDateTime::fromString(currStr, Qt::ISODate);
       
    78 
       
    79     if (getAlarmTime() == curr)
       
    80         emit soundAlarm(getAlarmTime());
       
    81 
       
    82     nextAlarm();
       
    83 }
       
    84 
       
    85 QDateTime NotesManager::getAlarmTime() const
       
    86 {
       
    87     return m_alarmTime;
       
    88 }
       
    89 
       
    90 void NotesManager::setAlarmTime(const QDateTime& alarm)
       
    91 {
       
    92     m_alarmTime = alarm;
       
    93 }
       
    94 
       
    95 QString NotesManager::getAlarmMessage() const
       
    96 {
       
    97     return m_alarmMessage;
       
    98 }
       
    99 
       
   100 void NotesManager::setAlarmMessage(const QString& message)
       
   101 {
       
   102     m_alarmMessage = message;
       
   103 }
       
   104 
       
   105 void NotesManager::addNote(const QString& note, const QDateTime& alarm)
       
   106 {
       
   107     QString alert = alarm.toString("yyyy-MM-dd HH:mm:ss");
       
   108     QSqlQuery query("INSERT INTO todolist(notes, date) VALUES ('" + note + "', '" + alert + "')");
       
   109 }
       
   110 
       
   111 void NotesManager::removeNote(int id)
       
   112 {
       
   113     QSqlQuery query("DELETE FROM todolist WHERE id='" + QString::number(id) + "'");
       
   114 }
       
   115 
       
   116 void NotesManager::setSearch(const QString& search)
       
   117 {
       
   118     m_search = search;
       
   119 }
       
   120 
       
   121 QList<QObject*> NotesManager::getNotes(const QString& search)
       
   122 {
       
   123     m_notes.clear();
       
   124     setSearch(search);
       
   125 
       
   126     QString queryString = "SELECT * FROM todolist";
       
   127     if (m_search != "") queryString += " WHERE notes LIKE '%" + m_search + "%'"; 
       
   128     queryString += " ORDER BY date";
       
   129 
       
   130     QSqlQuery query(queryString);
       
   131     while (query.next()) {
       
   132         Note *entry = new Note(this);
       
   133         entry->setIndex(query.value(0).toInt());
       
   134         entry->setMessage(query.value(1).toString());
       
   135         entry->setAlarm(QDateTime::fromString(query.value(2).toString(), "yyyy-MM-dd HH:mm:ss"));
       
   136 
       
   137         m_notes << entry;
       
   138     }
       
   139    
       
   140     return m_notes;
       
   141 }
       
   142 
       
   143 #ifdef DECLARATIVE
       
   144 QDeclarativeListProperty<QObject> NotesManager::noteSet()
       
   145 {
       
   146     m_notes = getNotes(m_search);
       
   147     return QDeclarativeListProperty<QObject>(this, m_notes);
       
   148 }
       
   149 #endif