1 /*
2 * Copyright (C) 2008-2021 The QXmpp developers
3 *
4 * Author:
5 * Jeremy Lainé
6 *
7 * Source:
8 * https://github.com/qxmpp-project/qxmpp
9 *
10 * This file is a part of QXmpp library.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 */
23
24 #include "example_7_archiveHandling.h"
25
26 #include "QXmppArchiveIq.h"
27 #include "QXmppArchiveManager.h"
28
29 #include <QCoreApplication>
30 #include <QDateTime>
31
logStart(const QString & msg)32 static void logStart(const QString &msg)
33 {
34 qDebug("example_7_archiveHandling : %s", qPrintable(msg));
35 }
36
logEnd(const QString & msg)37 static void logEnd(const QString &msg)
38 {
39 qDebug(" => %s", qPrintable(msg));
40 }
41
xmppClient(QObject * parent)42 xmppClient::xmppClient(QObject *parent)
43 : QXmppClient(parent), m_collectionCount(-1), m_pageDirection(PageForwards), m_pageSize(10)
44 {
45
46 // add archive manager
47 archiveManager = new QXmppArchiveManager;
48 addExtension(archiveManager);
49
50 // connect signals
51 connect(this, &QXmppClient::connected,
52 this, &xmppClient::clientConnected);
53
54 connect(archiveManager, &QXmppArchiveManager::archiveChatReceived,
55 this, &xmppClient::archiveChatReceived);
56
57 connect(archiveManager, &QXmppArchiveManager::archiveListReceived,
58 this, &xmppClient::archiveListReceived);
59
60 // set limits
61 m_startDate = QDateTime::currentDateTime().addDays(-21);
62 m_endDate = QDateTime::currentDateTime();
63 }
64
~xmppClient()65 xmppClient::~xmppClient()
66 {
67 }
68
setPageDirection(PageDirection direction)69 void xmppClient::setPageDirection(PageDirection direction)
70 {
71 m_pageDirection = direction;
72 }
73
setPageSize(int size)74 void xmppClient::setPageSize(int size)
75 {
76 m_pageSize = size;
77 }
78
clientConnected()79 void xmppClient::clientConnected()
80 {
81 logEnd("connected");
82
83 // we want 0 results, i.e. only result-set management information (count)
84 logStart("fetching collection count");
85 QXmppResultSetQuery rsmQuery;
86 rsmQuery.setMax(0);
87 archiveManager->listCollections("", m_startDate, m_endDate, rsmQuery);
88 }
89
archiveListReceived(const QList<QXmppArchiveChat> & chats,const QXmppResultSetReply & rsmReply)90 void xmppClient::archiveListReceived(const QList<QXmppArchiveChat> &chats, const QXmppResultSetReply &rsmReply)
91 {
92 if (m_collectionCount < 0) {
93 logEnd(QString::number(rsmReply.count()) + " items");
94 m_collectionCount = rsmReply.count();
95
96 // fetch first page
97 logStart("fetching collection first page");
98 QXmppResultSetQuery rsmQuery;
99 rsmQuery.setMax(m_pageSize);
100 if (m_pageDirection == PageBackwards)
101 rsmQuery.setBefore("");
102 archiveManager->listCollections("", m_startDate, m_endDate, rsmQuery);
103 } else if (!chats.size()) {
104 logEnd("no items");
105 } else {
106 logEnd(QString("items %1 to %2 of %3").arg(QString::number(rsmReply.index()), QString::number(rsmReply.index() + chats.size() - 1), QString::number(rsmReply.count())));
107 for (const auto &chat : chats) {
108 qDebug("chat start %s", qPrintable(chat.start().toString()));
109 // NOTE: to actually retrieve conversations, uncomment this
110 //archiveManager->retrieveCollection(chat.with(), chat.start());
111 }
112 if (!rsmReply.isNull()) {
113 // fetch next page
114 QXmppResultSetQuery rsmQuery;
115 rsmQuery.setMax(m_pageSize);
116 if (m_pageDirection == PageBackwards) {
117 logStart("fetching collection previous page");
118 rsmQuery.setBefore(rsmReply.first());
119 } else {
120 logStart("fetching collection next page");
121 rsmQuery.setAfter(rsmReply.last());
122 }
123 archiveManager->listCollections("", m_startDate, m_endDate, rsmQuery);
124 }
125 }
126 }
127
archiveChatReceived(const QXmppArchiveChat & chat,const QXmppResultSetReply & rsmReply)128 void xmppClient::archiveChatReceived(const QXmppArchiveChat &chat, const QXmppResultSetReply &rsmReply)
129 {
130 logEnd(QString("chat received, RSM count %1")
131 .arg(QString::number(rsmReply.count())));
132
133 const auto messages = chat.messages();
134 for (const auto &msg : messages) {
135 qDebug("example_7_archiveHandling : %s", qPrintable(msg.body()));
136 }
137 }
138
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 QCoreApplication a(argc, argv);
142
143 xmppClient client;
144 client.setPageSize(15);
145 client.setPageDirection(xmppClient::PageBackwards);
146 client.connectToServer("qxmpp.test1@qxmpp.org", "qxmpp123");
147
148 return a.exec();
149 }
150