QXmppArchiveIq.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  6. *
  7. * Source:
  8. * http://code.google.com/p/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. #include <QDomElement>
  24. #include "QXmppArchiveIq.h"
  25. #include "QXmppConstants.h"
  26. #include "QXmppUtils.h"
  27. QXmppArchiveMessage::QXmppArchiveMessage()
  28. : m_received(false)
  29. {
  30. }
  31. /// Returns the archived message's body.
  32. QString QXmppArchiveMessage::body() const
  33. {
  34. return m_body;
  35. }
  36. /// Sets the archived message's body.
  37. ///
  38. /// \param body
  39. void QXmppArchiveMessage::setBody(const QString &body)
  40. {
  41. m_body = body;
  42. }
  43. /// Returns the archived message's date.
  44. QDateTime QXmppArchiveMessage::date() const
  45. {
  46. return m_date;
  47. }
  48. //// Sets the archived message's date.
  49. ///
  50. /// \param date
  51. void QXmppArchiveMessage::setDate(const QDateTime &date)
  52. {
  53. m_date = date;
  54. }
  55. /// Returns true if the archived message was received, false if it was sent.
  56. bool QXmppArchiveMessage::isReceived() const
  57. {
  58. return m_received;
  59. }
  60. /// Set to true if the archived message was received, false if it was sent.
  61. ///
  62. /// \param isReceived
  63. void QXmppArchiveMessage::setReceived(bool isReceived)
  64. {
  65. m_received = isReceived;
  66. }
  67. QXmppArchiveChat::QXmppArchiveChat()
  68. : m_version(0)
  69. {
  70. }
  71. /// \cond
  72. void QXmppArchiveChat::parse(const QDomElement &element)
  73. {
  74. m_with = element.attribute("with");
  75. m_start = QXmppUtils::datetimeFromString(element.attribute("start"));
  76. m_subject = element.attribute("subject");
  77. m_thread = element.attribute("thread");
  78. m_version = element.attribute("version").toInt();
  79. QDateTime timeAccu = m_start;
  80. QDomElement child = element.firstChildElement();
  81. while (!child.isNull())
  82. {
  83. if ((child.tagName() == "from") || (child.tagName() == "to"))
  84. {
  85. QXmppArchiveMessage message;
  86. message.setBody(child.firstChildElement("body").text());
  87. timeAccu = timeAccu.addSecs(child.attribute("secs").toInt());
  88. message.setDate(timeAccu);
  89. message.setReceived(child.tagName() == "from");
  90. m_messages << message;
  91. }
  92. child = child.nextSiblingElement();
  93. }
  94. }
  95. void QXmppArchiveChat::toXml(QXmlStreamWriter *writer, const QXmppResultSetReply &rsm) const
  96. {
  97. writer->writeStartElement("chat");
  98. writer->writeAttribute("xmlns", ns_archive);
  99. helperToXmlAddAttribute(writer, "with", m_with);
  100. if (m_start.isValid())
  101. helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start));
  102. helperToXmlAddAttribute(writer, "subject", m_subject);
  103. helperToXmlAddAttribute(writer, "thread", m_thread);
  104. if (m_version)
  105. helperToXmlAddAttribute(writer, "version", QString::number(m_version));
  106. QDateTime prevTime = m_start;
  107. foreach (const QXmppArchiveMessage &message, m_messages)
  108. {
  109. writer->writeStartElement(message.isReceived() ? "from" : "to");
  110. helperToXmlAddAttribute(writer, "secs", QString::number(prevTime.secsTo(message.date())));
  111. writer->writeTextElement("body", message.body());
  112. writer->writeEndElement();
  113. prevTime = message.date();
  114. }
  115. if (!rsm.isNull())
  116. rsm.toXml(writer);
  117. writer->writeEndElement();
  118. }
  119. /// \endcond
  120. /// Returns the conversation's messages.
  121. QList<QXmppArchiveMessage> QXmppArchiveChat::messages() const
  122. {
  123. return m_messages;
  124. }
  125. /// Sets the conversation's messages.
  126. void QXmppArchiveChat::setMessages(const QList<QXmppArchiveMessage> &messages)
  127. {
  128. m_messages = messages;
  129. }
  130. /// Returns the start of this conversation.
  131. QDateTime QXmppArchiveChat::start() const
  132. {
  133. return m_start;
  134. }
  135. /// Sets the start of this conversation.
  136. void QXmppArchiveChat::setStart(const QDateTime &start)
  137. {
  138. m_start = start;
  139. }
  140. /// Returns the conversation's subject.
  141. QString QXmppArchiveChat::subject() const
  142. {
  143. return m_subject;
  144. }
  145. /// Sets the conversation's subject.
  146. void QXmppArchiveChat::setSubject(const QString &subject)
  147. {
  148. m_subject = subject;
  149. }
  150. /// Returns the conversation's thread.
  151. QString QXmppArchiveChat::thread() const
  152. {
  153. return m_thread;
  154. }
  155. /// Sets the conversation's thread.
  156. void QXmppArchiveChat::setThread(const QString &thread)
  157. {
  158. m_thread = thread;
  159. }
  160. /// Returns the conversation's version.
  161. int QXmppArchiveChat::version() const
  162. {
  163. return m_version;
  164. }
  165. /// Sets the conversation's version.
  166. void QXmppArchiveChat::setVersion(int version)
  167. {
  168. m_version = version;
  169. }
  170. /// Returns the JID of the remote party.
  171. QString QXmppArchiveChat::with() const
  172. {
  173. return m_with;
  174. }
  175. /// Sets the JID of the remote party.
  176. void QXmppArchiveChat::setWith(const QString &with)
  177. {
  178. m_with = with;
  179. }
  180. /// Returns the chat conversation carried by this IQ.
  181. QXmppArchiveChat QXmppArchiveChatIq::chat() const
  182. {
  183. return m_chat;
  184. }
  185. /// Sets the chat conversation carried by this IQ.
  186. void QXmppArchiveChatIq::setChat(const QXmppArchiveChat &chat)
  187. {
  188. m_chat = chat;
  189. }
  190. /// Returns the result set management reply.
  191. ///
  192. /// This is used for paging through messages.
  193. QXmppResultSetReply QXmppArchiveChatIq::resultSetReply() const
  194. {
  195. return m_rsmReply;
  196. }
  197. /// Sets the result set management reply.
  198. ///
  199. /// This is used for paging through messages.
  200. void QXmppArchiveChatIq::setResultSetReply(const QXmppResultSetReply& rsm)
  201. {
  202. m_rsmReply = rsm;
  203. }
  204. /// \cond
  205. bool QXmppArchiveChatIq::isArchiveChatIq(const QDomElement &element)
  206. {
  207. QDomElement chatElement = element.firstChildElement("chat");
  208. return !chatElement.attribute("with").isEmpty();
  209. //return (chatElement.namespaceURI() == ns_archive);
  210. }
  211. void QXmppArchiveChatIq::parseElementFromChild(const QDomElement &element)
  212. {
  213. QDomElement chatElement = element.firstChildElement("chat");
  214. m_chat.parse(chatElement);
  215. m_rsmReply.parse(chatElement);
  216. }
  217. void QXmppArchiveChatIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  218. {
  219. m_chat.toXml(writer, m_rsmReply);
  220. }
  221. /// \endcond
  222. /// Constructs a QXmppArchiveListIq.
  223. QXmppArchiveListIq::QXmppArchiveListIq()
  224. : QXmppIq(QXmppIq::Get)
  225. {
  226. }
  227. /// Returns the list of chat conversations.
  228. QList<QXmppArchiveChat> QXmppArchiveListIq::chats() const
  229. {
  230. return m_chats;
  231. }
  232. /// Sets the list of chat conversations.
  233. void QXmppArchiveListIq::setChats(const QList<QXmppArchiveChat> &chats)
  234. {
  235. m_chats = chats;
  236. }
  237. /// Returns the JID which archived conversations must match.
  238. ///
  239. QString QXmppArchiveListIq::with() const
  240. {
  241. return m_with;
  242. }
  243. /// Sets the JID which archived conversations must match.
  244. ///
  245. /// \param with
  246. void QXmppArchiveListIq::setWith(const QString &with)
  247. {
  248. m_with = with;
  249. }
  250. /// Returns the start date/time for the archived conversations.
  251. ///
  252. QDateTime QXmppArchiveListIq::start() const
  253. {
  254. return m_start;
  255. }
  256. /// Sets the start date/time for the archived conversations.
  257. ///
  258. /// \param start
  259. void QXmppArchiveListIq::setStart(const QDateTime &start)
  260. {
  261. m_start = start;
  262. }
  263. /// Returns the end date/time for the archived conversations.
  264. ///
  265. QDateTime QXmppArchiveListIq::end() const
  266. {
  267. return m_end;
  268. }
  269. /// Sets the end date/time for the archived conversations.
  270. ///
  271. /// \param end
  272. void QXmppArchiveListIq::setEnd(const QDateTime &end)
  273. {
  274. m_end = end;
  275. }
  276. /// Returns the result set management query.
  277. ///
  278. /// This is used for paging through conversations.
  279. QXmppResultSetQuery QXmppArchiveListIq::resultSetQuery() const
  280. {
  281. return m_rsmQuery;
  282. }
  283. /// Sets the result set management query.
  284. ///
  285. /// This is used for paging through conversations.
  286. void QXmppArchiveListIq::setResultSetQuery(const QXmppResultSetQuery& rsm)
  287. {
  288. m_rsmQuery = rsm;
  289. }
  290. /// Returns the result set management reply.
  291. ///
  292. /// This is used for paging through conversations.
  293. QXmppResultSetReply QXmppArchiveListIq::resultSetReply() const
  294. {
  295. return m_rsmReply;
  296. }
  297. /// Sets the result set management reply.
  298. ///
  299. /// This is used for paging through conversations.
  300. void QXmppArchiveListIq::setResultSetReply(const QXmppResultSetReply& rsm)
  301. {
  302. m_rsmReply = rsm;
  303. }
  304. /// \cond
  305. bool QXmppArchiveListIq::isArchiveListIq(const QDomElement &element)
  306. {
  307. QDomElement listElement = element.firstChildElement("list");
  308. return (listElement.namespaceURI() == ns_archive);
  309. }
  310. void QXmppArchiveListIq::parseElementFromChild(const QDomElement &element)
  311. {
  312. QDomElement listElement = element.firstChildElement("list");
  313. m_with = listElement.attribute("with");
  314. m_start = QXmppUtils::datetimeFromString(listElement.attribute("start"));
  315. m_end = QXmppUtils::datetimeFromString(listElement.attribute("end"));
  316. m_rsmQuery.parse(listElement);
  317. m_rsmReply.parse(listElement);
  318. QDomElement child = listElement.firstChildElement();
  319. while (!child.isNull())
  320. {
  321. if (child.tagName() == "chat")
  322. {
  323. QXmppArchiveChat chat;
  324. chat.parse(child);
  325. m_chats << chat;
  326. }
  327. child = child.nextSiblingElement();
  328. }
  329. }
  330. void QXmppArchiveListIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  331. {
  332. writer->writeStartElement("list");
  333. writer->writeAttribute("xmlns", ns_archive);
  334. if (!m_with.isEmpty())
  335. helperToXmlAddAttribute(writer, "with", m_with);
  336. if (m_start.isValid())
  337. helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start));
  338. if (m_end.isValid())
  339. helperToXmlAddAttribute(writer, "end", QXmppUtils::datetimeToString(m_end));
  340. if (!m_rsmQuery.isNull())
  341. m_rsmQuery.toXml(writer);
  342. else if (!m_rsmReply.isNull())
  343. m_rsmReply.toXml(writer);
  344. foreach (const QXmppArchiveChat &chat, m_chats)
  345. chat.toXml(writer);
  346. writer->writeEndElement();
  347. }
  348. bool QXmppArchivePrefIq::isArchivePrefIq(const QDomElement &element)
  349. {
  350. QDomElement prefElement = element.firstChildElement("pref");
  351. return (prefElement.namespaceURI() == ns_archive);
  352. }
  353. void QXmppArchivePrefIq::parseElementFromChild(const QDomElement &element)
  354. {
  355. QDomElement queryElement = element.firstChildElement("pref");
  356. Q_UNUSED(queryElement);
  357. }
  358. void QXmppArchivePrefIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  359. {
  360. writer->writeStartElement("pref");
  361. writer->writeAttribute("xmlns", ns_archive);
  362. writer->writeEndElement();
  363. }
  364. /// \endcond
  365. /// Returns the JID which archived conversations must match.
  366. ///
  367. QString QXmppArchiveRemoveIq::with() const
  368. {
  369. return m_with;
  370. }
  371. /// Sets the JID which archived conversations must match.
  372. ///
  373. /// \param with
  374. void QXmppArchiveRemoveIq::setWith(const QString &with)
  375. {
  376. m_with = with;
  377. }
  378. /// Returns the start date/time for the archived conversations.
  379. ///
  380. QDateTime QXmppArchiveRemoveIq::start() const
  381. {
  382. return m_start;
  383. }
  384. /// Sets the start date/time for the archived conversations.
  385. ///
  386. /// \param start
  387. void QXmppArchiveRemoveIq::setStart(const QDateTime &start)
  388. {
  389. m_start = start;
  390. }
  391. /// Returns the end date/time for the archived conversations.
  392. ///
  393. QDateTime QXmppArchiveRemoveIq::end() const
  394. {
  395. return m_end;
  396. }
  397. /// Sets the end date/time for the archived conversations.
  398. ///
  399. /// \param end
  400. void QXmppArchiveRemoveIq::setEnd(const QDateTime &end)
  401. {
  402. m_end = end;
  403. }
  404. /// \cond
  405. bool QXmppArchiveRemoveIq::isArchiveRemoveIq(const QDomElement &element)
  406. {
  407. QDomElement retrieveElement = element.firstChildElement("remove");
  408. return (retrieveElement.namespaceURI() == ns_archive);
  409. }
  410. void QXmppArchiveRemoveIq::parseElementFromChild(const QDomElement &element)
  411. {
  412. QDomElement listElement = element.firstChildElement("remove");
  413. m_with = listElement.attribute("with");
  414. m_start = QXmppUtils::datetimeFromString(listElement.attribute("start"));
  415. m_end = QXmppUtils::datetimeFromString(listElement.attribute("end"));
  416. }
  417. void QXmppArchiveRemoveIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  418. {
  419. writer->writeStartElement("remove");
  420. writer->writeAttribute("xmlns", ns_archive);
  421. if (!m_with.isEmpty())
  422. helperToXmlAddAttribute(writer, "with", m_with);
  423. if (m_start.isValid())
  424. helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start));
  425. if (m_end.isValid())
  426. helperToXmlAddAttribute(writer, "end", QXmppUtils::datetimeToString(m_end));
  427. writer->writeEndElement();
  428. }
  429. /// \endcond
  430. QXmppArchiveRetrieveIq::QXmppArchiveRetrieveIq()
  431. : QXmppIq(QXmppIq::Get)
  432. {
  433. }
  434. /// Returns the start date/time for the archived conversations.
  435. ///
  436. QDateTime QXmppArchiveRetrieveIq::start() const
  437. {
  438. return m_start;
  439. }
  440. /// Sets the start date/time for the archived conversations.
  441. ///
  442. /// \param start
  443. void QXmppArchiveRetrieveIq::setStart(const QDateTime &start)
  444. {
  445. m_start = start;
  446. }
  447. /// Returns the JID which archived conversations must match.
  448. ///
  449. QString QXmppArchiveRetrieveIq::with() const
  450. {
  451. return m_with;
  452. }
  453. /// Sets the JID which archived conversations must match.
  454. ///
  455. /// \param with
  456. void QXmppArchiveRetrieveIq::setWith(const QString &with)
  457. {
  458. m_with = with;
  459. }
  460. /// Returns the result set management query.
  461. ///
  462. /// This is used for paging through messages.
  463. QXmppResultSetQuery QXmppArchiveRetrieveIq::resultSetQuery() const
  464. {
  465. return m_rsmQuery;
  466. }
  467. /// Sets the result set management query.
  468. ///
  469. /// This is used for paging through messages.
  470. void QXmppArchiveRetrieveIq::setResultSetQuery(const QXmppResultSetQuery& rsm)
  471. {
  472. m_rsmQuery = rsm;
  473. }
  474. /// \cond
  475. bool QXmppArchiveRetrieveIq::isArchiveRetrieveIq(const QDomElement &element)
  476. {
  477. QDomElement retrieveElement = element.firstChildElement("retrieve");
  478. return (retrieveElement.namespaceURI() == ns_archive);
  479. }
  480. void QXmppArchiveRetrieveIq::parseElementFromChild(const QDomElement &element)
  481. {
  482. QDomElement retrieveElement = element.firstChildElement("retrieve");
  483. m_with = retrieveElement.attribute("with");
  484. m_start = QXmppUtils::datetimeFromString(retrieveElement.attribute("start"));
  485. m_rsmQuery.parse(retrieveElement);
  486. }
  487. void QXmppArchiveRetrieveIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  488. {
  489. writer->writeStartElement("retrieve");
  490. writer->writeAttribute("xmlns", ns_archive);
  491. helperToXmlAddAttribute(writer, "with", m_with);
  492. helperToXmlAddAttribute(writer, "start", QXmppUtils::datetimeToString(m_start));
  493. if (!m_rsmQuery.isNull())
  494. m_rsmQuery.toXml(writer);
  495. writer->writeEndElement();
  496. }
  497. /// \endcond