#include <QtGui>
#include <qservicemanager.h>
#include <qserviceinterfacedescriptor.h>
#include "sfwnotes.h"
ToDoTool::ToDoTool(QWidget *parent, Qt::WindowFlags flags)
: QWidget(parent, flags)
{
setupUi(this);
serviceManager = new QServiceManager(this);
registerExampleServices();
#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MEAMO_6)
titleLabel->setFont(QFont("Nimbus Roman No9 L", 40, QFont::Bold, true));
noteLabel->setFont(QFont("Comic Sans MS", 30, QFont::Normal, true));
#endif
init();
}
ToDoTool::~ToDoTool()
{
unregisterExampleServices();
}
void ToDoTool::soundAlarm(const QDateTime& alarm)
{
QString message = notesManager->property("alarmMessage").toString();
QMessageBox msgBox;
msgBox.setWindowTitle("Alert");
msgBox.setText("ALARM SOUNDED!!!\n\n" +alarm.toString("yyyy-MM-dd HH:mm") + "\n\n" + message);
msgBox.resize(200, msgBox.height());
msgBox.exec();
}
void ToDoTool::init()
{
bool ok;
QString interfaceName = QInputDialog::getText(this, tr("ToDoTool"), tr("Specify Notes Manager interface:"),
QLineEdit::Normal, "com.nokia.qt.examples.NotesManager", &ok);
if (ok) {
QServiceInterfaceDescriptor desc = serviceManager->interfaceDefault(interfaceName);
if (desc.isValid()) {
QServiceManager mgr;
notesManager = mgr.loadInterface(desc);
if (notesManager)
notesManager->setParent(this);
bool connect;
QMetaObject::invokeMethod(notesManager, "init", Q_RETURN_ARG(bool, connect));
if (connect) {
currentNote = 1;
searchWord = "";
refreshList();
addButton->setEnabled(true);
deleteButton->setEnabled(true);
searchButton->setEnabled(true);
nextButton->setEnabled(true);
prevButton->setEnabled(true);
QObject::connect(notesManager, SIGNAL(soundAlarm(QDateTime)),
this, SLOT(soundAlarm(QDateTime)));
} else {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("ToDoTool"));
msgBox.setText("Unable to access notes manager database\n in current or home directory");
msgBox.exec();
}
} else {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("ToDoTool"));
msgBox.setText("No valid default interface for:\n\n\"" + interfaceName + "\"");
msgBox.exec();
}
}
}
void ToDoTool::registerExampleServices()
{
unregisterExampleServices();
QStringList exampleXmlFiles;
exampleXmlFiles << "notesmanagerservice.xml";
foreach (const QString &fileName, exampleXmlFiles) {
const QString path = QCoreApplication::applicationDirPath() + "/xmldata/" + fileName;
if (!serviceManager->addService(path))
qDebug() << "Unable to register notes manager service";
}
}
void ToDoTool::unregisterExampleServices()
{
serviceManager->removeService("NotesManagerService");
}
void ToDoTool::refreshList(bool view)
{
QMetaObject::invokeMethod(notesManager, "getNotes",
Q_RETURN_ARG(QList<QObject*>, ret),
Q_ARG(QString, searchWord));
totalNotes = ret.size();
if (totalNotes < 1) { currentNote = 0; }
else if (totalNotes > 0 && currentNote == 0) { currentNote = 1; }
if (!view)
currentNote = totalNotes;
refreshNotes();
}
void ToDoTool::refreshNotes()
{
countLabel->setText(QString::number(currentNote) + "/" + QString::number(totalNotes));
if (currentNote == 0) {
dateLabel->setText("");
if (searchWord == "")
noteLabel->setText("Click + to add a note");
else
noteLabel->setText("No matching note for:\n'" + searchWord + "'");
}
else {
QDateTime alarm;
QMetaObject::invokeMethod(ret[currentNote-1], "alarm", Q_RETURN_ARG(QDateTime, alarm));
dateLabel->setText(alarm.toString("yyyy-MM-dd HH:mm"));
QString note;
QMetaObject::invokeMethod(ret[currentNote-1], "message", Q_RETURN_ARG(QString, note));
noteLabel->setText(note);
}
}
void ToDoTool::on_nextButton_clicked()
{
if (currentNote < totalNotes) {
currentNote++;
refreshNotes();
}
}
void ToDoTool::on_prevButton_clicked()
{
if (currentNote > 1) {
currentNote--;
refreshNotes();
}
}
void ToDoTool::on_addButton_clicked()
{
bool ok;
QString newNote = QInputDialog::getText(this, tr("ToDoTool"),
tr("Add a new note + alarm of format:\nnote#yyyy-mm-dd#hh:mm"),
QLineEdit::Normal, "", &ok);
if (ok && !newNote.isEmpty()) {
QStringList note = newNote.split(QRegExp("#"));
if (note.size() == 3) {
QStringList date = note.at(1).split("-");
QStringList time = note.at(2).split(":");
if (date.size() == 3 && time.size() == 2) {
QDateTime alarm = QDateTime::fromString(note.at(1)+" "+note.at(2),"yyyy-MM-dd HH:mm");
QMetaObject::invokeMethod(notesManager, "addNote",
Q_ARG(QString, note.at(0)),
Q_ARG(QDateTime, alarm));
}
} else {
QMetaObject::invokeMethod(notesManager, "addNote",
Q_ARG(QString, note.at(0)),
Q_ARG(QDateTime, QDateTime::currentDateTime()));
}
searchWord = "";
refreshList(false);
}
}
void ToDoTool::on_deleteButton_clicked()
{
if (currentNote != 0) {
QMessageBox msgBox;
msgBox.setText("Confirm removing this note?");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
if (msgBox.exec() == QMessageBox::Ok) {
int index;
QMetaObject::invokeMethod(ret[currentNote-1], "index", Q_RETURN_ARG(int, index));
QMetaObject::invokeMethod(notesManager, "removeNote", Q_ARG(int, index));
if (currentNote > 1)
currentNote--;
refreshList();
}
}
}
void ToDoTool::on_searchButton_clicked()
{
bool ok;
QString searchNote = QInputDialog::getText(this, tr("ToDoTool"), tr("Find a note:"),
QLineEdit::Normal, "", &ok);
if (ok) {
if (searchNote.isEmpty())
searchWord = "";
else
searchWord = searchNote;
currentNote = 1;
refreshList();
}
}