diff --git a/src/roommodel.cpp b/src/roommodel.cpp index c8662643..a346806c 100644 --- a/src/roommodel.cpp +++ b/src/roommodel.cpp @@ -1,165 +1,165 @@ /* * * Copyright 2016 Riccardo Iaconelli * * 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 the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "roommodel.h" #include #include "userdata.h" Room RoomModel::fromJSon(const QJsonObject& o) { Room r; r.name = o["name"].toString(); r.id = o["id"].toString(); return r; } QByteArray RoomModel::serialize(const Room& r) { QJsonDocument d; QJsonObject o; o["name"] = r.name; o["id"] = r.id; d.setObject(o); return d.toBinaryData(); } RoomModel::RoomModel(QObject* parent) : QAbstractListModel(parent) { reset(); } RoomModel::~RoomModel() { QDir cacheDir(UserData::self()->cacheBasePath()); if (!cacheDir.exists(cacheDir.path())) { cacheDir.mkpath(cacheDir.path()); } QFile f(cacheDir.absoluteFilePath("rooms")); if (f.open(QIODevice::WriteOnly)) { QDataStream out(&f); foreach (const Room m, m_roomsList) { QByteArray ms = RoomModel::serialize(m); out.writeBytes(ms, ms.size()); } } } void RoomModel::reset() { if (UserData::self()->serverURL().isEmpty()) { return; } beginResetModel(); m_roomsList.clear(); endResetModel(); QDir cacheDir(UserData::self()->cacheBasePath()); // load cache if (cacheDir.exists(cacheDir.path())) { QFile f(cacheDir.absoluteFilePath("rooms")); if (f.open(QIODevice::ReadOnly)) { QDataStream in(&f); while (!f.atEnd()) { char * byteArray; quint32 length; in.readBytes(byteArray, length); QByteArray arr = QByteArray::fromRawData(byteArray, length); Room m = RoomModel::fromJSon(QJsonDocument::fromBinaryData(arr).object()); -// qDebug() << m.id << m.name << m.selected; - m_roomsList[m.name] = m; + // This cache creates some instabilities +// m_roomsList[m.name] = m; // addRoom(m.id, m.name, m.selected); } } } } QHash RoomModel::roleNames() const { QHash roles; roles[RoomName] = "name"; roles[RoomID] = "id"; roles[RoomSelected] = "selected"; return roles; } int RoomModel::rowCount(const QModelIndex & parent) const { return m_roomsList.size(); } QVariant RoomModel::data(const QModelIndex & index, int role) const { Room r = m_roomsList.values().at(index.row()); if (role == RoomModel::RoomName) { return r.name; } else if (role == RoomModel::RoomID) { return r.id; } else if (role == RoomModel::RoomSelected) { return r.selected; } else { return QVariant("0"); } } void RoomModel::addRoom(const QString& roomID, const QString& roomName, bool selected) { // qDebug() << m_roomsList.size(); // return; qDebug() << "Adding room" << roomID << roomName << m_roomsList.keys(); if (roomID.isEmpty()) { return; } bool updating = false; if (m_roomsList.contains(roomName)) { // we are doing an update updating = true; } int size = m_roomsList.size(); if (!updating) { beginInsertRows(index(size), size, (size+1)); } Room r; r.id = roomID; r.name = roomName; r.selected = selected; m_roomsList[roomName] = r; if (updating) { //Figure out a better way to update just the really changed message, not EVERYTHING emit dataChanged(createIndex(1, 1), createIndex(rowCount(), 1)); } else { endInsertRows(); } } // #include "roommodel.moc" diff --git a/src/userdata.cpp b/src/userdata.cpp index 9785613f..89b89888 100644 --- a/src/userdata.cpp +++ b/src/userdata.cpp @@ -1,180 +1,189 @@ /* * * Copyright 2016 Riccardo Iaconelli * * 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 the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "userdata.h" #include "roommodel.h" #include "ddpclient.h" UserData *UserData::m_self = 0; QString UserData::authToken() const { return m_authToken; } QString UserData::userName() const { return m_userName; } QString UserData::password() const { return m_password; } void UserData::setAuthToken(const QString& token) { qDebug() << "Setting token to" << token; QSettings s; m_authToken = token; s.setValue("authToken", token); } void UserData::setPassword(const QString& password) { m_password = password; } void UserData::setUserName(const QString& username) { m_userName = username; QSettings s; s.setValue("username", username); emit userNameChanged(); } RoomModel * UserData::roomModel() { if (!m_roomModel) { qDebug() << "creating new RoomModel"; m_roomModel = new RoomModel; qDebug() << m_roomModel; // m_roomModel->reset(); } return m_roomModel; } DDPClient * UserData::ddp() { if (!m_ddp) { m_ddp = new DDPClient(serverURL()); connect(m_ddp, &DDPClient::loginStatusChanged, this, &UserData::loginStatusChanged); // connect(m_ddp, &DDPClient::loginStatusChanged, this, [=](){qDebug() << "Signal received";}); } return m_ddp; } void UserData::sendMessage(const QString &roomID, const QString &message) { QString json = "{\"rid\": \"%1\", \"msg\": \"%2\"}"; json = json.arg(roomID, message); ddp()->method("sendMessage", QJsonDocument::fromJson(json.toUtf8())); } MessageModel * UserData::getModelForRoom(const QString& roomID) { if (m_messageModels.contains(roomID)) { return m_messageModels.value(roomID); } else { // qDebug() << "Creating a new model"; m_messageModels[roomID] = new MessageModel(roomID, this); return m_messageModels[roomID]; } } QString UserData::serverURL() const { return m_serverURL; } void UserData::setServerURL(const QString& serverURL) { if (m_serverURL == serverURL) { return; } QSettings s; s.setValue("serverURL", serverURL); m_serverURL = serverURL; - m_roomModel->reset(); +// m_roomModel->reset(); emit serverURLChanged(); } DDPClient::LoginStatus UserData::loginStatus() { if (m_ddp) { return ddp()->loginStatus(); } else { return DDPClient::LoggedOut; } } void UserData::tryLogin() { qDebug() << "Attempting login" << userName() << "on" << serverURL(); // ddp()->login(); + // Reset data + foreach (const QString key, m_messageModels.keys()) { + MessageModel *m = m_messageModels.take(key); + delete m; + } + delete m_ddp; + m_ddp = 0; +// delete m_roomModel; + ddp(); // This creates a new ddp() object. DDP will automatically try to connect and login. } void UserData::logOut() { setAuthToken(QString()); setPassword(QString()); // m_ddp->logOut(); foreach (const QString key, m_messageModels.keys()) { MessageModel *m = m_messageModels.take(key); delete m; } delete m_ddp; m_ddp = 0; emit loginStatusChanged(); // m_roomModel->reset(); // RoomModel -> reset(); } QString UserData::cacheBasePath() const { return QStandardPaths::writableLocation(QStandardPaths::CacheLocation)+'/'+m_serverURL; } UserData::UserData(QObject* parent) : QObject(parent), m_ddp(0), m_roomModel(0) { QSettings s; m_serverURL = s.value("serverURL", "demo.rocket.chat").toString(); m_userName = s.value("username").toString(); m_authToken = s.value("authToken").toString(); // roomModel()->reset(); } UserData * UserData::self() { if (!m_self) { m_self = new UserData; m_self->ddp(); // Create DDP object so we try to connect at startup } return m_self; }