Commit 3dcc95c7 authored by Karel Slaný's avatar Karel Slaný

Message search page has its own separate model now.

parent 4aff508b
......@@ -27,6 +27,7 @@ import QtQuick.Window 2.1
import QtQuick.Dialogs 1.2
import cz.nic.mobileDatovka 1.0
import cz.nic.mobileDatovka.messages 1.0
import cz.nic.mobileDatovka.models 1.0
/*
* Roles are defined in MessageListModel::roleNames() and are accessed directly
......@@ -47,6 +48,14 @@ Component {
Component.onCompleted: {
searchPhraseText.forceActiveFocus()
}
MessageListModel {
id: messageModel
Component.onCompleted: {
}
}
Rectangle {
id: header
anchors.top: parent.top
......@@ -69,7 +78,6 @@ Component {
MouseArea {
anchors.fill: parent
onClicked: {
messages.clearMessagesModel()
statusBar.visible = false
pageView.pop(StackView.Immediate)
}
......@@ -92,7 +100,7 @@ Component {
width: parent.width
height: parent.height * 0.2
function doSearchRequest() {
return (messages.searchMsg(searchPhraseText.text, soughtMsgType) <= 0)
return (messages.searchMsg(messageModel, searchPhraseText.text, soughtMsgType) <= 0)
}
Column {
anchors.horizontalCenter: parent.horizontalCenter
......@@ -190,7 +198,7 @@ Component {
visible: true
width: parent.width
interactive: true
model: messageListModel
model: messageModel
onMsgClicked: {
statusBar.visible = false
......
......@@ -247,6 +247,7 @@ int main(int argc, char *argv[])
/* Register types into QML. */
FileListModel::declareQML();
MessageListModel::declareQML();
Messages::declareQML();
MsgInfo::declareQML();
......@@ -258,6 +259,8 @@ int main(int argc, char *argv[])
QtAppDelegateInitialize(&interactionZfoFile);
#endif
globMessagesModel.setObjectName("messageListModel"); /* TODO -- Remove it. */
/* register classes in QML */
ctx->setContextProperty("isds", &isds);
ctx->setContextProperty("messages", &messages);
......
......@@ -33,6 +33,7 @@
#include "src/messages.h"
#include "src/settings.h"
#include "src/models/accountmodel.h"
#include "src/models/messagemodel.h"
#include "src/sqlite/message_db_container.h"
#include "src/sqlite/file_db_container.h"
......@@ -525,20 +526,28 @@ bool Messages::relocateDatabases(const QString &newLocation,
return relocationSucceeded;
}
int Messages::searchMsg(const QString &phrase, MessageTypes msgTypes)
int Messages::searchMsg(const QVariant &msgModelVariant, const QString &phrase,
MessageTypes msgTypes)
{
qDebug("%s()", __func__);
int msgs = 0;
globMessagesModel.clearAll();
MessageListModel *messageModel =
MessageListModel::fromVariant(msgModelVariant);
if (messageModel == Q_NULLPTR) {
Q_ASSERT(0);
qCritical("%s", "Cannot access message model.");
return 0;
}
messageModel->clearAll();
QStringList userNameList = AccountListModel::globAccounts.keys();
foreach (const QString &userName, userNameList) {
MessageDb *msgDb = Q_NULLPTR;
msgDb = globMessageDbsPtr->accessMessageDb(globSet.dbsLocation,
userName,
MessageDb *msgDb = globMessageDbsPtr->accessMessageDb(
globSet.dbsLocation, userName,
AccountListModel::globAccounts[userName].storeToDisk());
if (msgDb == Q_NULLPTR) {
qDebug() << "ERROR: Message database cannot open!" << userName;
......@@ -546,12 +555,12 @@ int Messages::searchMsg(const QString &phrase, MessageTypes msgTypes)
}
if (msgTypes & TYPE_RECEIVED) {
msgs += msgDb->searchAndAppendMsgs(userName, phrase,
MessageDb::TYPE_RECEIVED);
msgs += msgDb->searchAndAppendMsgs(messageModel,
userName, phrase, MessageDb::TYPE_RECEIVED);
}
if (msgTypes & TYPE_SENT) {
msgs += msgDb->searchAndAppendMsgs(userName, phrase,
MessageDb::TYPE_SENT);
msgs += msgDb->searchAndAppendMsgs(messageModel,
userName, phrase, MessageDb::TYPE_SENT);
}
}
......
......@@ -107,12 +107,15 @@ public:
/*!
* @brief Search messages via all accounts.
*
* @param[in] phrase Search phrase.
* @param[in] msgTypes Sought message types.
* @param[in,out] msgModelVariant QVariant holding message model
* to be set.
* @param[in] phrase Search phrase.
* @param[in] msgTypes Sought message types.
* @return Number of messages in the search result.
*/
Q_INVOKABLE
int searchMsg(const QString &phrase, MessageTypes msgTypes);
int searchMsg(const QVariant &msgModelVariant, const QString &phrase,
MessageTypes msgTypes);
/*!
* @brief Delete messages from all message databases together with files
......
......@@ -205,13 +205,12 @@ void FileListModel::clearAll(void)
endResetModel();
}
FileListModel *FileListModel::fromVariant(
const QVariant &attachModelVariant)
FileListModel *FileListModel::fromVariant(const QVariant &modelVariant)
{
if (!attachModelVariant.canConvert<QObject *>()) {
if (!modelVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(attachModelVariant);
QObject *obj = qvariant_cast<QObject *>(modelVariant);
return qobject_cast<FileListModel *>(obj);
}
......
......@@ -154,11 +154,11 @@ public:
* and no data are passed. That's because we are using a QVariant.
* This function does not allocate a new model.
*
* @param[in] attachModelVariant QVariant holding attachment model.
* @param[in] modelVariant QVariant holding the model.
* @return Pointer to model if it could be acquired, Q_NULLPTR else.
*/
static
FileListModel *fromVariant(const QVariant &attachModelVariant);
FileListModel *fromVariant(const QVariant &modelVariant);
/*!
* @brief Set content from database.
......
/*
* Copyright (C) 2014-2016 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -22,6 +22,7 @@
*/
#include <QDebug>
#include <QQmlEngine> /* qmlRegisterType */
#include <QSqlRecord>
#include "src/models/messagemodel.h"
......@@ -155,11 +156,22 @@ void MessageModelEntry::setMsgType(int msgType)
MessageListModel globMessagesModel;
void MessageListModel::declareQML(void)
{
qmlRegisterType<MessageListModel>("cz.nic.mobileDatovka.models", 1, 0, "MessageListModel");
qRegisterMetaType<MessageListModel>();
}
MessageListModel::MessageListModel(QObject *parent)
: QAbstractListModel(parent)
{
/* Used in QML. */
setObjectName("messageListModel");
}
MessageListModel::MessageListModel(const MessageListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_messages(model.m_messages)
{
}
int MessageListModel::rowCount(const QModelIndex &parent) const
......@@ -373,3 +385,12 @@ bool MessageListModel::overrideDownloaded(qint64 dmId, bool forceDownloaded)
return true;
}
MessageListModel *MessageListModel::fromVariant(const QVariant &modelVariant)
{
if (!modelVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(modelVariant);
return qobject_cast<MessageListModel *>(obj);
}
/*
* Copyright (C) 2014-2016 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -69,9 +69,8 @@ private:
int m_msgType; /*!< Message orientation /sent/received/. */
};
class MessageListModel : public QAbstractListModel
{
Q_OBJECT
class MessageListModel : public QAbstractListModel {
Q_OBJECT
public:
/*!
......@@ -90,12 +89,27 @@ public:
ROLE_MSG_TYPE
};
/* Don't forget to declare various properties to the QML system. */
static
void declareQML(void);
/*!
* @brief Constructor.
*
* @param[in] parent Pointer to parent object.
*/
MessageListModel(QObject *parent = 0);
MessageListModel(QObject *parent = Q_NULLPTR);
/*!
* @brief Copy constructor.
*
* @note Needed for QVariant conversion.
*
* @param[in] model Model to be copied.
* @param[in] parent Pointer to parent object.
*/
explicit MessageListModel(const MessageListModel &model,
QObject *parent = Q_NULLPTR);
/*!
* @brief Return number of rows under the given parent.
......@@ -205,10 +219,27 @@ public:
*/
bool overrideDownloaded(qint64 dmId, bool forceDownloaded);
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
* @note Some weird stuff happens in QML when passing attachment model
* directly as constant reference. Wrong constructors are called
* and no data are passed. That's because we are using a QVariant.
* This function does not allocate a new model.
*
* @param[in] modelVariant QVariant holding the model.
* @return Pointer to model if it could be acquired, Q_NULLPTR else.
*/
static
MessageListModel *fromVariant(const QVariant &modelVariant);
private:
QList<MessageModelEntry> m_messages; /*!< List of messages stored. */
};
/* QML passes its arguments via QVariant. */
Q_DECLARE_METATYPE(MessageListModel)
extern MessageListModel globMessagesModel;
#endif // MESSAGEMODEL_H
......@@ -21,10 +21,10 @@
* the two.
*/
#include <QDebug>
#include "src/settings.h"
#include "src/models/messagemodel.h"
#include "src/net/db_wrapper.h"
#include "src/sqlite/account_db.h"
......
/*
* Copyright (C) 2014-2015 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -34,6 +34,7 @@
#include "src/common.h"
#include "src/io/filesystem.h"
#include "src/models/messagemodel.h"
#include "src/sqlite/message_db.h"
#include "src/sqlite/db_tables.h"
#include "src/sqlite/dbs.h"
......@@ -1128,9 +1129,15 @@ int MessageDb::getDbSizeInBytes(void)
return fi.size();
}
int MessageDb::searchAndAppendMsgs(const QString &userName,
const QString &phrase, enum MessageType messageType)
int MessageDb::searchAndAppendMsgs(MessageListModel *msgModel,
const QString &userName, const QString &phrase,
enum MessageType messageType)
{
if (msgModel == Q_NULLPTR) {
Q_ASSERT(0);
return 0;
}
QSqlQuery query(m_db);
QString queryStr = "SELECT dmID, dmSender, dmRecipient, dmAnnotation, ";
......@@ -1162,7 +1169,7 @@ int MessageDb::searchAndAppendMsgs(const QString &userName,
query.bindValue(":_dmMessageType", messageType);
if (query.exec() && query.isActive()) {
return globMessagesModel.setQuery(userName, query, true);
return msgModel->setQuery(userName, query, true);
}
return 0;
......
/*
* Copyright (C) 2014-2015 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -31,7 +31,8 @@
#include "src/common.h"
#include "src/io/sqlite/db.h"
#include "src/messages.h"
#include "src/models/messagemodel.h"
class MessageListModel; /* Forward declaration. */
/*!
* @brief Encapsulates message database.
......@@ -255,12 +256,14 @@ public:
/*!
* @brief Search and append messages into message model.
*
* @param[in] msgModel Model to append data to.
* @param[in] userName Account user name.
* @param[in] phrase Search phrase.
* @param[in] messageType Type of the sought messages.
* @return Number of results.
*/
int searchAndAppendMsgs(const QString &userName, const QString &phrase,
int searchAndAppendMsgs(MessageListModel *msgModel,
const QString &userName, const QString &phrase,
enum MessageType messageType);
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment