Insert JidDb into Client
This commit is contained in:
parent
8cbcb1f98f
commit
78d9bd4084
11
client.cpp
11
client.cpp
|
@ -3,6 +3,7 @@
|
|||
Client::Client(const QString &jid, QObject *const parent) :
|
||||
QXmppClient(parent),
|
||||
jid(jid),
|
||||
db(jid),
|
||||
trust_db(jid),
|
||||
trust(&trust_db),
|
||||
omemo_db(jid),
|
||||
|
@ -18,3 +19,13 @@ QString Client::jidBare()
|
|||
{
|
||||
return configuration().jidBare();
|
||||
}
|
||||
|
||||
const JidDb &Client::database() const
|
||||
{
|
||||
return db;
|
||||
}
|
||||
|
||||
JidDb &Client::database()
|
||||
{
|
||||
return db;
|
||||
}
|
||||
|
|
4
client.h
4
client.h
|
@ -2,6 +2,7 @@
|
|||
#define CLIENT_H
|
||||
|
||||
#include "omemo_db.h"
|
||||
#include "jiddb.h"
|
||||
#include "trust_db.h"
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
@ -17,9 +18,12 @@ class Client : public QXmppClient
|
|||
public:
|
||||
Client(const QString &jid, QObject *parent = nullptr);
|
||||
QString jidBare();
|
||||
const JidDb &database() const;
|
||||
JidDb &database();
|
||||
|
||||
private:
|
||||
const QString jid;
|
||||
JidDb db;
|
||||
TrustDb trust_db;
|
||||
QXmppTrustManager trust;
|
||||
QXmppPubSubManager pubsub;
|
||||
|
|
|
@ -3,16 +3,16 @@
|
|||
#include <QListWidgetItem>
|
||||
#include <QScroller>
|
||||
|
||||
Contacts::Contacts(const QHash<QString, JidDb *> &databases,
|
||||
Contacts::Contacts(const QList<Client *> &clients,
|
||||
QWidget *const parent) :
|
||||
QDialog(parent)
|
||||
{
|
||||
ui.setupUi(this);
|
||||
QScroller::grabGesture(ui.contacts_list, QScroller::TouchGesture);
|
||||
|
||||
for (const auto db : databases)
|
||||
for (const auto &contact : db->roster())
|
||||
add(db->jid, contact);
|
||||
for (const auto c : clients)
|
||||
for (const auto &contact : c->database().roster())
|
||||
add(c->jidBare(), contact);
|
||||
|
||||
connect(ui.contacts_list, &QListWidget::itemActivated, this,
|
||||
[this]
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#define CONTACTS_H
|
||||
|
||||
#include "ui_contacts.h"
|
||||
#include "jiddb.h"
|
||||
#include "client.h"
|
||||
#include <QDialog>
|
||||
#include <QHash>
|
||||
#include <QString>
|
||||
|
@ -13,7 +13,7 @@ class Contacts : public QDialog
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Contacts(const QHash<QString, JidDb *> &databases,
|
||||
Contacts(const QList<Client *> &clients,
|
||||
QWidget *parent = nullptr);
|
||||
void add(const QString &own, const QString &other);
|
||||
|
||||
|
|
62
xxcc.cpp
62
xxcc.cpp
|
@ -23,7 +23,6 @@ xxcc::xxcc(QWidget *const parent) :
|
|||
ui.setupUi(this);
|
||||
QScroller::grabGesture(ui.conversations_list, QScroller::TouchGesture);
|
||||
QScroller::grabGesture(ui.messages, QScroller::TouchGesture);
|
||||
setupDatabases(pairs);
|
||||
connectAccounts(pairs);
|
||||
retrieveConversations();
|
||||
connect(ui.accounts, &QPushButton::released, this,
|
||||
|
@ -39,16 +38,20 @@ xxcc::xxcc(QWidget *const parent) :
|
|||
connect(ui.contacts, &QPushButton::released, this,
|
||||
[this]
|
||||
{
|
||||
Contacts c(databases, this);
|
||||
Contacts c(clients, this);
|
||||
|
||||
connect(&c, &Contacts::startChat, this, &xxcc::startChat);
|
||||
|
||||
for (const auto db : databases)
|
||||
for (const auto cl : clients)
|
||||
{
|
||||
const auto db = &cl->database();
|
||||
|
||||
c.connect(db, &JidDb::addedToRoster, &c,
|
||||
[&c, db] (const QString jid)
|
||||
{
|
||||
c.add(db->jid, jid);
|
||||
});
|
||||
}
|
||||
|
||||
c.exec();
|
||||
});
|
||||
|
@ -74,11 +77,11 @@ xxcc::xxcc(QWidget *const parent) :
|
|||
break;
|
||||
}
|
||||
|
||||
if (databases.contains(selected->jidBare()))
|
||||
if (selected)
|
||||
{
|
||||
const auto db = databases[selected->jidBare()];
|
||||
const auto &db = selected->database();
|
||||
static const auto n_messages = 20;
|
||||
const auto messages = db->getMessages(conv->to, n_messages);
|
||||
const auto messages = db.getMessages(conv->to, n_messages);
|
||||
|
||||
for (auto it = messages.rbegin(); it != messages.rend(); it++)
|
||||
new Message(it->body, it->dt, it->direction, ui.messages);
|
||||
|
@ -97,9 +100,6 @@ xxcc::~xxcc()
|
|||
{
|
||||
for (const auto c : clients)
|
||||
delete c;
|
||||
|
||||
for (const auto db : databases)
|
||||
delete db;
|
||||
}
|
||||
|
||||
void xxcc::connectAccounts(const QList<Credentials::Pair> &pairs)
|
||||
|
@ -120,12 +120,6 @@ void xxcc::connectAccounts(const QList<Credentials::Pair> &pairs)
|
|||
}
|
||||
}
|
||||
|
||||
void xxcc::setupDatabases(const QList<Credentials::Pair> &pairs)
|
||||
{
|
||||
for (const auto &p : pairs)
|
||||
databases[p.first] = new JidDb(p.first);
|
||||
}
|
||||
|
||||
void xxcc::startChat(const QString from, const QString to)
|
||||
{
|
||||
bool found = false;
|
||||
|
@ -194,12 +188,7 @@ void xxcc::addAccount(Client *const c)
|
|||
roster->connect(roster, &QXmppRosterManager::rosterReceived, c,
|
||||
[this, c, roster]
|
||||
{
|
||||
if (databases.contains(c->jidBare()))
|
||||
{
|
||||
const auto db = databases[c->jidBare()];
|
||||
|
||||
db->addToRoster(roster->getRosterBareJids());
|
||||
}
|
||||
c->database().addToRoster(roster->getRosterBareJids());
|
||||
});
|
||||
else
|
||||
throw std::runtime_error("Expected non-null QXmppRosterManager");
|
||||
|
@ -239,23 +228,30 @@ void xxcc::storeMessage(const QXmppMessage &msg, const Direction dir) const
|
|||
break;
|
||||
}
|
||||
|
||||
if (databases.contains(jid))
|
||||
for (const auto c : clients)
|
||||
{
|
||||
const auto db = databases[jid];
|
||||
JidDb::Message m;
|
||||
if (c->jidBare() == jid)
|
||||
{
|
||||
const auto &db = c->database();
|
||||
JidDb::Message m;
|
||||
|
||||
m.body = msg.body();
|
||||
m.dt = msg.stamp();
|
||||
m.direction = dir;
|
||||
m.contact = contact;
|
||||
db->storeMessage(m);
|
||||
m.body = msg.body();
|
||||
m.dt = msg.stamp();
|
||||
m.direction = dir;
|
||||
m.contact = contact;
|
||||
db.storeMessage(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xxcc::retrieveConversations()
|
||||
{
|
||||
for (const auto db : databases)
|
||||
for (const auto &conv : db->getConversations())
|
||||
new Conversation(db->jid, conv.to,
|
||||
ui.conversations_list, conv.last_msg, conv.dt);
|
||||
for (const auto c : clients)
|
||||
{
|
||||
const auto &db = c->database();
|
||||
|
||||
for (const auto &conv : db.getConversations())
|
||||
new Conversation(db.jid, conv.to,
|
||||
ui.conversations_list, conv.last_msg, conv.dt);
|
||||
}
|
||||
}
|
||||
|
|
3
xxcc.h
3
xxcc.h
|
@ -6,7 +6,6 @@
|
|||
#include "credentials.h"
|
||||
#include "jiddb.h"
|
||||
#include <QDateTime>
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
#include <QWidget>
|
||||
#include <QSqlDatabase>
|
||||
|
@ -25,10 +24,8 @@ private:
|
|||
enum Tab {Conversations, Chat};
|
||||
Ui_main ui;
|
||||
QList<Client *> clients;
|
||||
QHash<QString, JidDb *> databases;
|
||||
Credentials creds;
|
||||
Client *selected;
|
||||
void setupDatabases(const QList<Credentials::Pair> &pairs);
|
||||
void connectAccounts(const QList<Credentials::Pair> &pairs);
|
||||
void storeMessage(const QXmppMessage &msg, Direction dir) const;
|
||||
void retrieveConversations();
|
||||
|
|
Loading…
Reference in New Issue