From 7aa519c4ad34ccd62743654c420c1b97d86aeac5 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 17 Apr 2019 23:08:56 +0300 Subject: [PATCH] finally launches again, started contact abstraction in server --- core/CMakeLists.txt | 1 + core/account.cpp | 9 +++++- core/account.h | 2 ++ core/archive.cpp | 77 ++++++++++++++++++++------------------------- core/archive.h | 1 + core/contact.cpp | 57 +++++++++++++++++++++++++++++++++ core/contact.h | 59 ++++++++++++++++++++++++++++++++++ main.cpp | 27 ---------------- 8 files changed, 162 insertions(+), 71 deletions(-) create mode 100644 core/contact.cpp create mode 100644 core/contact.h diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 0284081..0b77345 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -12,6 +12,7 @@ set(squawkCORE_SRC squawk.cpp account.cpp archive.cpp + contact.cpp ) # Tell CMake to create the helloworld executable diff --git a/core/account.cpp b/core/account.cpp index 54cf38f..58227df 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -14,7 +14,8 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& state(Shared::disconnected), groups(), cm(new QXmppCarbonManager()), - am(new QXmppMamManager()) + am(new QXmppMamManager()), + contacts() { config.setUser(p_login); config.setDomain(p_server); @@ -216,6 +217,12 @@ void Core::Account::onRosterItemRemoved(const QString& bareJid) void Core::Account::addedAccount(const QString& jid) { QXmppRosterManager& rm = client.rosterManager(); + + std::map::const_iterator itr = contacts.find(jid); + if (itr == contacts.end()) { + + } + QXmppRosterIq::Item re = rm.getRosterEntry(jid); QSet gr = re.groups(); unsigned int state = re.subscriptionType(); diff --git a/core/account.h b/core/account.h index 1691be9..104b407 100644 --- a/core/account.h +++ b/core/account.h @@ -10,6 +10,7 @@ #include #include #include "../global.h" +#include "contact.h" namespace Core { @@ -65,6 +66,7 @@ private: std::map> groups; QXmppCarbonManager* cm; QXmppMamManager* am; + std::map contacts; private slots: void onClientConnected(); diff --git a/core/archive.cpp b/core/archive.cpp index 2742c0e..70327a7 100644 --- a/core/archive.cpp +++ b/core/archive.cpp @@ -32,16 +32,17 @@ Core::Archive::Archive(const QString& p_jid, QObject* parent): main(), order() { - mdb_env_create(&environment); } Core::Archive::~Archive() { + close(); } void Core::Archive::open(const QString& account) { if (!opened) { + mdb_env_create(&environment); QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); path += "/" + account + "/" + jid; QDir cache(path); @@ -56,28 +57,25 @@ void Core::Archive::open(const QString& account) mdb_env_set_maxdbs(environment, 2); mdb_env_open(environment, path.toStdString().c_str(), 0, 0664); - int rc; MDB_txn *txn; - rc = mdb_txn_begin(environment, NULL, 0, &txn); - if (rc) { - qDebug() << "opening transaction error " << mdb_strerror(rc); - } - rc = mdb_dbi_open(txn, "main", MDB_CREATE, &main); - if (rc) { - qDebug() << "main opening error " << mdb_strerror(rc); - } - rc = mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order); - if (rc) { - qDebug() << "order opening error " << mdb_strerror(rc); - } - rc = mdb_txn_commit(txn); - if (rc) { - qDebug() << "opening commit transaction error " << mdb_strerror(rc); - } + mdb_txn_begin(environment, NULL, 0, &txn); + mdb_dbi_open(txn, "main", MDB_CREATE, &main); + mdb_dbi_open(txn, "order", MDB_CREATE | MDB_INTEGERKEY, &order); + mdb_txn_commit(txn); opened = true; } } +void Core::Archive::close() +{ + if (opened) { + mdb_dbi_close(environment, order); + mdb_dbi_close(environment, main); + mdb_env_close(environment); + opened = false; + } +} + void Core::Archive::addElement(const Shared::Message& message) { if (!opened) { @@ -89,12 +87,9 @@ void Core::Archive::addElement(const Shared::Message& message) quint64 stamp = message.getTime().toMSecsSinceEpoch(); const std::string& id = message.getId().toStdString(); - qDebug() << "inserting element with id " << id.c_str(); - qDebug() << "data size is " << ba.size(); - MDB_val lmdbKey, lmdbData; lmdbKey.mv_size = id.size(); - lmdbKey.mv_data = (uint8_t*)id.c_str(); + lmdbKey.mv_data = (char*)id.c_str(); lmdbData.mv_size = ba.size(); lmdbData.mv_data = (uint8_t*)ba.data(); MDB_txn *txn; @@ -141,20 +136,15 @@ Shared::Message Core::Archive::getElement(const QString& id) throw Closed("getElement", jid.toStdString()); } - qDebug() << "getting an element with id " << id.toStdString().c_str(); + std::string strKey = id.toStdString(); MDB_val lmdbKey, lmdbData; - lmdbKey.mv_size = id.toStdString().size(); - lmdbKey.mv_data = (uint8_t*)id.toStdString().c_str(); + lmdbKey.mv_size = strKey.size(); + lmdbKey.mv_data = (char*)strKey.c_str(); MDB_txn *txn; int rc; - rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); - if (rc) { - qDebug() <<"Get error: " << mdb_strerror(rc); - mdb_txn_abort(txn); - throw NotFound(id.toStdString(), jid.toStdString()); - } + mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); rc = mdb_get(txn, main, &lmdbKey, &lmdbData); if (rc) { qDebug() <<"Get error: " << mdb_strerror(rc); @@ -191,16 +181,15 @@ QString Core::Archive::newestId() rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST); if (rc) { - std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); - mdb_cursor_close(cursor); - mdb_txn_abort(txn); - qDebug() << "newest id is " << sId.c_str(); - return sId.c_str(); - } else { qDebug() << "Error geting newestId " << mdb_strerror(rc); mdb_cursor_close(cursor); mdb_txn_abort(txn); throw new Empty(jid.toStdString()); + } else { + std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); + mdb_cursor_close(cursor); + mdb_txn_abort(txn); + return sId.c_str(); } } @@ -223,21 +212,23 @@ QString Core::Archive::oldestId() rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST); if (rc) { - std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); - mdb_cursor_close(cursor); - mdb_txn_abort(txn); - qDebug() << "oldest id is " << sId.c_str(); - return sId.c_str(); - } else { qDebug() << "Error geting oldestId " << mdb_strerror(rc); mdb_cursor_close(cursor); mdb_txn_abort(txn); throw new Empty(jid.toStdString()); + } else { + std::string sId((char*)lmdbData.mv_data, lmdbData.mv_size); + mdb_cursor_close(cursor); + mdb_txn_abort(txn); + return sId.c_str(); } } long unsigned int Core::Archive::size() const { + if (!opened) { + throw Closed("size", jid.toStdString()); + } MDB_txn *txn; int rc; rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); diff --git a/core/archive.h b/core/archive.h index bed600a..614ce40 100644 --- a/core/archive.h +++ b/core/archive.h @@ -34,6 +34,7 @@ public: ~Archive(); void open(const QString& account); + void close(); void addElement(const Shared::Message& message); Shared::Message getElement(const QString& id); diff --git a/core/contact.cpp b/core/contact.cpp new file mode 100644 index 0000000..06f7ddd --- /dev/null +++ b/core/contact.cpp @@ -0,0 +1,57 @@ +/* + * + * Copyright (C) 2019 Yury Gubich + * + * 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 3 of the License, or + * (at your option) any later version. + * + * 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 "contact.h" + +Core::Contact::Contact(const QString& pJid, const QString& account, QObject* parent): + QObject(parent), + jid(pJid), + name(), + groups(), + state(empty), + archive(new Archive(jid)) +{ + archive->open(account); +} + +Core::Contact::~Contact() +{ + delete archive; +} + +Core::Contact::ArchiveState Core::Contact::getArchiveState() const +{ + return state; +} + +QSet Core::Contact::getGroups() const +{ + return groups; +} + +QString Core::Contact::getName() const +{ + return name; +} + +void Core::Contact::setName(const QString& n) +{ + if (name != n) { + name = n; + } +} diff --git a/core/contact.h b/core/contact.h new file mode 100644 index 0000000..5b5d5c0 --- /dev/null +++ b/core/contact.h @@ -0,0 +1,59 @@ +/* + * + * Copyright (C) 2019 Yury Gubich + * + * 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 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ + +#ifndef CORE_CONTACT_H +#define CORE_CONTACT_H + +#include +#include +#include "archive.h" + +namespace Core { + +class Contact : public QObject +{ + Q_OBJECT +public: + enum ArchiveState { + empty, //have no messages stored for this contact + chunk, //have some chunk of history, don't have the beginning nor have the end + beginning, //have the history from the very beginning, don't have the end + end, //have the history to the end, but don't have the beginning + complete //have full history locally stored + }; + Contact(const QString& pJid, const QString& account, QObject* parent = 0); + ~Contact(); + + ArchiveState getArchiveState() const; + QString getName() const; + void setName(const QString& n); + QSet getGroups() const; + +public: + const QString jid; + +private: + QString name; + QSet groups; + ArchiveState state; + Archive* archive; +}; + +} + +#endif // CORE_CONTACT_H diff --git a/main.cpp b/main.cpp index 17175ee..7d0dae2 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include "core/archive.h" int main(int argc, char *argv[]) { @@ -57,32 +56,6 @@ int main(int argc, char *argv[]) //qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation); - Core::Archive ar("test1@macaw.me"); - ar.open("Test"); - Shared::Message msg1; - msg1.generateRandomId(); - msg1.setBody("oldest"); - msg1.setTime(QDateTime::currentDateTime().addDays(-7)); - Shared::Message msg2; - msg2.generateRandomId(); - msg2.setBody("Middle"); - msg2.setTime(QDateTime::currentDateTime().addDays(-4)); - Shared::Message msg3; - msg3.generateRandomId(); - msg3.setBody("newest"); - msg3.setTime(QDateTime::currentDateTime()); - - ar.addElement(msg2); - ar.addElement(msg3); - ar.addElement(msg1); - - Shared::Message d0 = ar.getElement(msg1.getId()); - - Shared::Message d1 = ar.newest(); - Shared::Message d2 = ar.oldest(); - - qDebug() << d1.getBody() << ", " << d2.getBody(); - coreThread->start(); int result = app.exec();