qtmobility/examples/todotool/todotool.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     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 file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include <QDebug>
       
    44 
       
    45 #include <qservicemanager.h>
       
    46 #include <qserviceinterfacedescriptor.h>
       
    47 
       
    48 #include "todotool.h"
       
    49 
       
    50 Q_DECLARE_METATYPE(QServiceInterfaceDescriptor)
       
    51 
       
    52 ToDoTool::ToDoTool(QWidget *parent, Qt::WindowFlags flags)
       
    53     : QWidget(parent, flags)
       
    54 {
       
    55     setupUi(this);
       
    56 
       
    57     serviceManager = new QServiceManager(this);
       
    58 
       
    59     registerExampleServices();
       
    60 
       
    61     setWindowTitle(tr("ToDoTool"));
       
    62 
       
    63     init();
       
    64 }
       
    65 
       
    66 ToDoTool::~ToDoTool()
       
    67 {
       
    68     unregisterExampleServices();
       
    69 }
       
    70 
       
    71 void ToDoTool::soundAlarm(const QDateTime& alarm)
       
    72 {
       
    73     QString message = notesObject->property("message").toString();
       
    74 
       
    75     QMessageBox msgBox;
       
    76     msgBox.setWindowTitle("Alert");
       
    77     msgBox.setText(alarm.toString() + "\n\n" + message);
       
    78     msgBox.exec();
       
    79 }
       
    80 
       
    81 void ToDoTool::init()
       
    82 {
       
    83     QServiceManager mgr;
       
    84     QServiceFilter filter;
       
    85     filter.setServiceName("NotesManagerService");
       
    86     QList<QServiceInterfaceDescriptor> list = serviceManager->findInterfaces(filter);
       
    87 
       
    88     notesObject = mgr.loadInterface(list[0]);
       
    89 
       
    90     if (notesObject)
       
    91         notesObject->setParent(this);
       
    92 
       
    93     {
       
    94         currentNote = 1;
       
    95         searchWord = "";    
       
    96         refreshList();
       
    97 
       
    98         QObject::connect(notesObject, SIGNAL(soundAlarm(QDateTime)), 
       
    99                          this, SLOT(soundAlarm(QDateTime)));
       
   100     }
       
   101 }
       
   102 
       
   103 void ToDoTool::registerExampleServices()
       
   104 {
       
   105     QStringList exampleXmlFiles;
       
   106     exampleXmlFiles << "notesmanagerservice.xml";
       
   107     foreach (const QString &fileName, exampleXmlFiles) {
       
   108         QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
       
   109         serviceManager->addService(path);
       
   110     }
       
   111 }
       
   112 
       
   113 void ToDoTool::unregisterExampleServices()
       
   114 {
       
   115     serviceManager->removeService("DatabaseManagerService");
       
   116 }
       
   117 
       
   118 void ToDoTool::refreshList()
       
   119 {
       
   120         QMetaObject::invokeMethod(notesObject, "getNotes", 
       
   121                                   Q_RETURN_ARG(QList<Note>, ret),
       
   122                                   Q_ARG(QString, searchWord)); 
       
   123 
       
   124         totalNotes = ret.size();
       
   125         
       
   126         if (totalNotes < 1)
       
   127             currentNote = 0;
       
   128         
       
   129         refreshNotes();
       
   130 }
       
   131 
       
   132 void ToDoTool::refreshNotes()
       
   133 {
       
   134     countLabel->setText(QString::number(currentNote) + "/" + QString::number(totalNotes));
       
   135     
       
   136     if (currentNote == 0) {
       
   137         dateLabel->setText("");
       
   138         noteLabel->setText("Click + to add a note");
       
   139     }
       
   140     else {
       
   141         dateLabel->setText(ret[currentNote-1].alert.toString());
       
   142         noteLabel->setText(ret[currentNote-1].message);
       
   143     }
       
   144 }
       
   145 
       
   146 void ToDoTool::on_nextButton_clicked()
       
   147 {
       
   148     if (currentNote < totalNotes) {
       
   149         currentNote++;
       
   150         refreshNotes();
       
   151     }
       
   152 }
       
   153 
       
   154 void ToDoTool::on_prevButton_clicked()
       
   155 {
       
   156     if (currentNote > 1) {
       
   157         currentNote--;
       
   158         refreshNotes();
       
   159     }
       
   160 }
       
   161 
       
   162 void ToDoTool::on_addButton_clicked()
       
   163 {
       
   164     // re-implement date-time input method
       
   165 
       
   166     bool ok;
       
   167     QString newNote = QInputDialog::getText(this, tr("ToDoTool"), 
       
   168                                             tr("Add a new note + alarm of format:\nnote#yyyy-mm-dd#hh:mm"), 
       
   169                                             QLineEdit::Normal, QDir::home().dirName(), &ok);
       
   170     if (ok && !newNote.isEmpty()) {
       
   171         QStringList note = newNote.split(QRegExp("#"));
       
   172       
       
   173         if (note.size() == 3) {
       
   174 
       
   175             QStringList date = note.at(1).split("-");
       
   176             QStringList time = note.at(2).split(":");
       
   177 
       
   178             if (date.size() == 3 && time.size() == 2) {
       
   179                 QDateTime alarm = QDateTime::fromString(note.at(1)+" "+note.at(2),"yyyy-MM-dd HH:mm");
       
   180                 QMetaObject::invokeMethod(notesObject, "addNote",
       
   181                                           Q_ARG(QString, note.at(0)),
       
   182                                           Q_ARG(QDateTime, alarm));
       
   183                 refreshList();
       
   184             }
       
   185         } else {
       
   186                 QMetaObject::invokeMethod(notesObject, "addNote",
       
   187                                           Q_ARG(QString, note.at(0)),
       
   188                                           Q_ARG(QDateTime, QDateTime::currentDateTime()));
       
   189         }
       
   190 
       
   191         refreshList();
       
   192     }
       
   193 }
       
   194 
       
   195 void ToDoTool::on_deleteButton_clicked()
       
   196 {
       
   197     if (currentNote != 0) {
       
   198         QMessageBox msgBox;
       
   199         msgBox.setWindowTitle("ToDoTool");
       
   200         msgBox.setText("Are you sure you want to remove this note item?");
       
   201         msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
       
   202         
       
   203         if (msgBox.exec() == QMessageBox::Ok) {
       
   204             int index = ret[currentNote-1].index;
       
   205 
       
   206             QMetaObject::invokeMethod(notesObject, "removeNote", Q_ARG(int, index));
       
   207             if (currentNote > 1)
       
   208                 currentNote--;
       
   209 
       
   210             refreshList();
       
   211         }
       
   212     }
       
   213 }
       
   214 
       
   215 void ToDoTool::on_searchButton_clicked()
       
   216 {
       
   217     bool ok;
       
   218     QString searchNote = QInputDialog::getText(this, tr("ToDoTool"), tr("Find a note:"),
       
   219                                                QLineEdit::Normal, QDir::home().dirName(), &ok);
       
   220     if (ok) {
       
   221         if (searchNote.isEmpty())
       
   222             searchWord = "";
       
   223         else
       
   224             searchWord = searchNote;
       
   225         
       
   226         currentNote = 1;
       
   227         refreshList();
       
   228     }
       
   229 }
       
   230