1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "yc.h"
#include "client.h"
#include "direction.h"
#include <QXmppMessage.h>
#include <QXmppOmemoElement_p.h>
#include <QXmppRosterManager.h>
#include <QXmppUtils.h>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <variant>
void Yc::connectAccounts(const QList<Credentials::Pair> &pairs)
{
for (const auto &p : pairs)
{
QXmppConfiguration cfg;
cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired);
cfg.setJid(p.first);
cfg.setPassword(p.second);
cfg.setAutoReconnectionEnabled(true);
const auto client = new Client(p.first);
addAccount(client);
client->connectToServer(cfg);
}
}
void Yc::startChat(const QString from, const QString to)
{
bool found = false;
#if 0
for (int i = 0; i < ui.conversations_list->count(); i++)
{
const auto it =
static_cast<const Conversation *>(ui.conversations_list->item(i));
if (it->from == from && it->to == to)
{
found = true;
break;
}
}
if (!found)
new Conversation(from, to, ui.conversations_list);
for (const auto c : clients)
if (c->jidBare() == from)
{
selected = c;
break;
}
ui.sw->setCurrentIndex(Tab::Chat);
ui.jid->setText(to);
ui.messages->scrollToBottom();
#endif
}
void Yc::addInMessage(const QXmppMessage &msg)
{
#if 0
new Message(msg.body(), msg.stamp().toLocalTime(), Direction::In,
ui.messages);
#endif
}
void Yc::addOutMessage(const QString &msg, const QDateTime &dt)
{
#if 0
new Message(msg, dt.toLocalTime(), Direction::Out,
ui.messages);
#endif
}
void Yc::addAccount(Client *const c)
{
c->configuration().setAutoReconnectionEnabled(true);
clients.append(c);
c->connect(c, &Client::messageReceived, this,
[this] (QXmppMessage msg)
{
if (msg.body().isEmpty())
return;
else if (msg.stamp().isNull())
msg.setStamp(QDateTime::currentDateTimeUtc());
storeMessage(msg, Direction::In);
if (selected)
addInMessage(msg);
});
const auto roster = c->findExtension<QXmppRosterManager>();
if (roster)
roster->connect(roster, &QXmppRosterManager::rosterReceived, c,
[this, c, roster]
{
c->database().addToRoster(roster->getRosterBareJids());
});
else
throw std::runtime_error("Expected non-null QXmppRosterManager");
}
void Yc::storeMessage(const QString &from, const QString &to,
const QString &msg, const QDateTime &dt, const Direction dir) const
{
QString jid, contact;
switch (dir)
{
case Direction::In:
jid = QXmppUtils::jidToBareJid(to);
contact = QXmppUtils::jidToBareJid(from);
break;
case Direction::Out:
jid = from;
contact = to;
break;
}
for (const auto c : clients)
{
if (c->jidBare() == jid)
{
const auto &db = c->database();
JidDb::Message m;
m.body = msg;
m.dt = dt;
m.direction = dir;
m.contact = contact;
db.store(m);
}
}
}
void Yc::storeMessage(const QXmppMessage &msg, const Direction dir) const
{
storeMessage(msg.from(), msg.to(), msg.body(), msg.stamp(), dir);
}
void Yc::retrieveConversations()
{
for (const auto c : clients)
{
const auto &db = c->database();
for (const auto &conv : db.conversations())
{
#if 0
new Conversation(db.jid, conv.to,
ui.conversations_list, conv.last_msg, conv.dt);
#else
emit newConversation(db.jid, conv.to, conv.last_msg);
#endif
}
}
}
void Yc::init()
{
creds.load().then(this,
[=] (Credentials::PairListResult &&result)
{
if (std::holds_alternative<Credentials::PairList>(result))
{
const auto &pairs = std::get<Credentials::PairList>(result);
connectAccounts(pairs);
retrieveConversations();
}
else if (std::holds_alternative<Credentials::Error>(result))
{
const auto &error = std::get<Credentials::Error>(result);
std::cerr << qPrintable(error.description) << std::endl;
}
});
}
Yc::~Yc()
{
for (const auto c : clients)
delete c;
}
Yc::Yc(QObject *parent) :
QObject(parent)
{
}
|