examples/sfw-notes/sfwnotes.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 <QtGui>
       
    42 
       
    43 #include <qservicemanager.h>
       
    44 #include <qserviceinterfacedescriptor.h>
       
    45 
       
    46 #include "sfwnotes.h"
       
    47 
       
    48 ToDoTool::ToDoTool(QWidget *parent, Qt::WindowFlags flags)
       
    49     : QWidget(parent, flags)
       
    50 {
       
    51     setupUi(this);
       
    52 
       
    53     serviceManager = new QServiceManager(this);
       
    54 
       
    55     registerExampleServices();
       
    56 
       
    57     setWindowTitle(tr("ToDoTool"));
       
    58 
       
    59     init();
       
    60 }
       
    61 
       
    62 ToDoTool::~ToDoTool()
       
    63 {
       
    64     unregisterExampleServices();
       
    65 }
       
    66 
       
    67 void ToDoTool::soundAlarm(const QDateTime& alarm)
       
    68 {
       
    69     QString message = notesManager->property("alarmMessage").toString();
       
    70 
       
    71     QMessageBox msgBox;
       
    72     msgBox.setWindowTitle("Alert");
       
    73     msgBox.setText("ALARM SOUNDED!!!\n\n" +alarm.toString("yyyy-MM-dd HH:mm") + "\n\n" + message);
       
    74     msgBox.resize(200, msgBox.height());
       
    75     msgBox.exec();
       
    76 }
       
    77 
       
    78 void ToDoTool::init()
       
    79 {
       
    80     bool ok;
       
    81     QString interfaceName = QInputDialog::getText(this, tr("ToDoTool"), tr("Specify Notes Manager interface:"),
       
    82                                                   QLineEdit::Normal, "com.nokia.qt.examples.NotesManager", &ok);
       
    83     if (ok) {
       
    84         QServiceInterfaceDescriptor desc = serviceManager->interfaceDefault(interfaceName);
       
    85         
       
    86         if (desc.isValid()) {
       
    87             QServiceManager mgr;
       
    88             notesManager = mgr.loadInterface(desc);
       
    89 
       
    90             if (notesManager)
       
    91                 notesManager->setParent(this);
       
    92 
       
    93             currentNote = 1;
       
    94             searchWord = "";    
       
    95             refreshList();
       
    96 
       
    97             addButton->setEnabled(true);
       
    98             deleteButton->setEnabled(true);
       
    99             searchButton->setEnabled(true);
       
   100             nextButton->setEnabled(true);
       
   101             prevButton->setEnabled(true);
       
   102 
       
   103             QObject::connect(notesManager, SIGNAL(soundAlarm(QDateTime)), 
       
   104                             this, SLOT(soundAlarm(QDateTime)));
       
   105         } else {
       
   106             QMessageBox msgBox;
       
   107             msgBox.setWindowTitle(tr("ToDoTool"));
       
   108             msgBox.setText("No valid default interface for:\n\n\"" + interfaceName + "\"");
       
   109             msgBox.exec();
       
   110         }
       
   111     }
       
   112 }
       
   113 
       
   114 void ToDoTool::registerExampleServices()
       
   115 {
       
   116     QStringList exampleXmlFiles;
       
   117     exampleXmlFiles << "notesmanagerservice.xml";
       
   118     foreach (const QString &fileName, exampleXmlFiles) {
       
   119         const QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
       
   120         serviceManager->addService(path);
       
   121     }
       
   122 }
       
   123 
       
   124 void ToDoTool::unregisterExampleServices()
       
   125 {
       
   126     serviceManager->removeService("NotesManagerService");
       
   127 }
       
   128 
       
   129 void ToDoTool::refreshList()
       
   130 {
       
   131         QMetaObject::invokeMethod(notesManager, "getNotes", 
       
   132                                   Q_RETURN_ARG(QList<QObject*>, ret),
       
   133                                   Q_ARG(QString, searchWord)); 
       
   134 
       
   135         totalNotes = ret.size();
       
   136     
       
   137         if (totalNotes < 1) { currentNote = 0; }
       
   138         else if (totalNotes > 0 && currentNote == 0) { currentNote = 1; }
       
   139         
       
   140         refreshNotes();
       
   141 }
       
   142 
       
   143 void ToDoTool::refreshNotes()
       
   144 {
       
   145     countLabel->setText(QString::number(currentNote) + "/" + QString::number(totalNotes));
       
   146     
       
   147     if (currentNote == 0) {
       
   148         dateLabel->setText("");
       
   149         noteLabel->setText("Click + to add a note");
       
   150     }
       
   151     else {
       
   152         QDateTime alarm;
       
   153         QMetaObject::invokeMethod(ret[currentNote-1], "alarm", Q_RETURN_ARG(QDateTime, alarm));
       
   154         dateLabel->setText(alarm.toString("yyyy-MM-dd HH:mm"));
       
   155 
       
   156         QString note;
       
   157         QMetaObject::invokeMethod(ret[currentNote-1], "message", Q_RETURN_ARG(QString, note));
       
   158         noteLabel->setText(note);
       
   159     }
       
   160 }
       
   161 
       
   162 void ToDoTool::on_nextButton_clicked()
       
   163 {
       
   164     if (currentNote < totalNotes) {
       
   165         currentNote++;
       
   166         refreshNotes();
       
   167     }
       
   168 }
       
   169 
       
   170 void ToDoTool::on_prevButton_clicked()
       
   171 {
       
   172     if (currentNote > 1) {
       
   173         currentNote--;
       
   174         refreshNotes();
       
   175     }
       
   176 }
       
   177 
       
   178 void ToDoTool::on_addButton_clicked()
       
   179 {
       
   180     // re-implement date-time input method
       
   181 
       
   182     bool ok;
       
   183     QString newNote = QInputDialog::getText(this, tr("ToDoTool"), 
       
   184                                             tr("Add a new note + alarm of format:\nnote#yyyy-mm-dd#hh:mm"), 
       
   185                                             QLineEdit::Normal, "", &ok);
       
   186 
       
   187     if (ok && !newNote.isEmpty()) {
       
   188         QStringList note = newNote.split(QRegExp("#"));
       
   189       
       
   190         if (note.size() == 3) {
       
   191 
       
   192             QStringList date = note.at(1).split("-");
       
   193             QStringList time = note.at(2).split(":");
       
   194 
       
   195             if (date.size() == 3 && time.size() == 2) {
       
   196                 QDateTime alarm = QDateTime::fromString(note.at(1)+" "+note.at(2),"yyyy-MM-dd HH:mm");
       
   197                 QMetaObject::invokeMethod(notesManager, "addNote",
       
   198                                           Q_ARG(QString, note.at(0)),
       
   199                                           Q_ARG(QDateTime, alarm));
       
   200             }
       
   201         } else {
       
   202                 QMetaObject::invokeMethod(notesManager, "addNote",
       
   203                                           Q_ARG(QString, note.at(0)),
       
   204                                           Q_ARG(QDateTime, QDateTime::currentDateTime()));
       
   205         }
       
   206 
       
   207         refreshList();
       
   208     }
       
   209 }
       
   210 
       
   211 void ToDoTool::on_deleteButton_clicked()
       
   212 {
       
   213     if (currentNote != 0) {
       
   214         QMessageBox msgBox;
       
   215         msgBox.setText("Confirm removing this note?");
       
   216         msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
       
   217         
       
   218         if (msgBox.exec() == QMessageBox::Ok) {
       
   219             int index;
       
   220             QMetaObject::invokeMethod(ret[currentNote-1], "index", Q_RETURN_ARG(int, index));
       
   221 
       
   222             QMetaObject::invokeMethod(notesManager, "removeNote", Q_ARG(int, index));
       
   223             if (currentNote > 1)
       
   224                 currentNote--;
       
   225 
       
   226             refreshList();
       
   227         }
       
   228     }
       
   229 }
       
   230 
       
   231 void ToDoTool::on_searchButton_clicked()
       
   232 {
       
   233     bool ok;
       
   234     QString searchNote = QInputDialog::getText(this, tr("ToDoTool"), tr("Find a note:"),
       
   235                                                QLineEdit::Normal, "", &ok);
       
   236     if (ok) {
       
   237         if (searchNote.isEmpty())
       
   238             searchWord = "";
       
   239         else
       
   240             searchWord = searchNote;
       
   241         
       
   242         currentNote = 1;
       
   243         refreshList();
       
   244     }
       
   245 }
       
   246