session_p.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2009 Andrew Stromme <astromme@chatonka.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Library General Public License as
  6. * published by the Free Software Foundation; either version 2, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #ifndef RTM_SESSION_P_H
  20. #define RTM_SESSION_P_H
  21. #include "session.h"
  22. #include "xmlreaders.h"
  23. #include "request.h"
  24. #include "auth.h"
  25. #include "task.h"
  26. #include <QUrl>
  27. #include <QStringList>
  28. #include <QString>
  29. #include <QByteArray>
  30. #include <QCryptographicHash>
  31. #include <QBuffer>
  32. #include <QCoreApplication>
  33. #include <QDomDocument>
  34. #include <QDomNode>
  35. #include <QDomElement>
  36. #include <QDomNodeList>
  37. #include <KSystemTimeZones>
  38. #include <KTimeZone>
  39. #include <KDebug>
  40. class RTM::SessionPrivate {
  41. SessionPrivate(Session *parent)
  42. : q(parent),
  43. auth(0)
  44. {
  45. }
  46. ~SessionPrivate() {
  47. if (auth)
  48. auth->deleteLater();
  49. }
  50. void populateSmartList(List * list)
  51. {
  52. kDebug() << "Populating Smart List: " << list->name();
  53. // We do this next bit manually so it doesn't get auto-connected to taskUpdate()
  54. RTM::Request *smartListRequest = new RTM::Request("rtm.tasks.getList", q->apiKey(), q->sharedSecret());
  55. smartListRequest->addArgument("auth_token", q->token());
  56. smartListRequest->addArgument("rtm_internal_list_id", QString::number(list->id()));
  57. smartListRequest->addArgument("filter", list->filter());
  58. QObject::connect(smartListRequest, SIGNAL(replyReceived(RTM::Request*)), q, SLOT(smartListReply(RTM::Request*)));
  59. smartListRequest->sendRequest();
  60. }
  61. void taskUpdate(RTM::Request* reply) {
  62. TasksReader reader(reply, q);
  63. reader.read();
  64. lastRefresh = QDateTime::currentDateTime();
  65. reply->deleteLater();
  66. }
  67. void listUpdate(RTM::Request* reply) {
  68. TasksReader reader(reply, q);
  69. reader.read();
  70. reply->deleteLater();
  71. }
  72. void smartListReply(RTM::Request* reply) {
  73. QStringList parts = reply->requestUrl().split("&");
  74. RTM::ListId id = 0;
  75. foreach(const QString &part, parts)
  76. if (part.contains("rtm_internal_list_id"))
  77. id = part.split("=").last().toLongLong();
  78. kDebug() << id;
  79. TasksReader reader(reply, q);
  80. reader.read();
  81. RTM::List* list = lists.value(id);
  82. foreach(RTM::Task* task, reader.readTasks()) {
  83. list->tasks.insert(task->id(), task);
  84. }
  85. emit q->listChanged(list);
  86. emit q->listsChanged();
  87. reply->deleteLater();
  88. }
  89. void refreshSettings() {
  90. RTM::Request settingsRequest("rtm.settings.getList", q->apiKey(), q->sharedSecret());
  91. settingsRequest.addArgument("auth_token", q->token());
  92. QString reply = settingsRequest.sendSynchronousRequest();
  93. // We're basically assuming no error here.... FIXME
  94. QString timezone = reply.remove(0, reply.indexOf("<timezone>")+10);
  95. timezone.truncate(timezone.indexOf("</timezone>"));
  96. QString dateformat = reply.remove(0, reply.indexOf("<dateformat>"+12));
  97. dateformat.truncate(dateformat.indexOf("</dateformat>"));
  98. QString timeformat = reply.remove(0, reply.indexOf("<timeformat>"+12));
  99. timeformat.truncate(timeformat.indexOf("</timeformat>"));
  100. QString defaultlist = reply.remove(0, reply.indexOf("<defaultlist>"+13));
  101. defaultlist.truncate(defaultlist.indexOf("</defaultlist>"));
  102. this->timezone = KSystemTimeZones::zone(timezone);
  103. kDebug() << "Timezone Set To: " << timezone << " i.e. " << this->timezone.name();
  104. }
  105. friend class TasksReader;
  106. friend class Session;
  107. Session *q;
  108. RTM::Auth *auth;
  109. QString authUrl;
  110. QString frob;
  111. QString apiKey;
  112. QString sharedSecret;
  113. QString token;
  114. QDateTime lastRefresh;
  115. RTM::Permissions permissions;
  116. KTimeZone timezone;
  117. RTM::Timeline timeline;
  118. QHash<RTM::TaskId,RTM::Task*> tasks;
  119. QHash<RTM::ListId,RTM::List*> lists;
  120. };
  121. #endif