task.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include "task.h"
  20. #include "task_p.h"
  21. #include <KDebug>
  22. #include "request.h"
  23. #include "session.h"
  24. #include "xmlreaders.h"
  25. RTM::Task::Task(RTM::Session* session)
  26. : QObject(session),
  27. d(new TaskPrivate(this))
  28. {
  29. d->session = session;
  30. }
  31. RTM::Task::~Task() {
  32. delete d;
  33. }
  34. QDateTime RTM::Task::completed() const {
  35. return d->completed;
  36. }
  37. QDateTime RTM::Task::deleted() const {
  38. return d->deleted;
  39. }
  40. QDateTime RTM::Task::due() const {
  41. return d->due;
  42. }
  43. QString RTM::Task::estimate() const {
  44. return d->estimate;
  45. }
  46. RTM::TaskId RTM::Task::id() const {
  47. return d->taskId;
  48. }
  49. bool RTM::Task::isCompleted() const {
  50. return d->completed.isValid();
  51. }
  52. bool RTM::Task::isDeleted() const {
  53. return d->deleted.isValid();
  54. }
  55. RTM::ListId RTM::Task::listId() const {
  56. return d->listId;
  57. }
  58. RTM::LocationId RTM::Task::locationId() const {
  59. return d->locationId;
  60. }
  61. QString RTM::Task::name() const {
  62. return d->name;
  63. }
  64. Notes RTM::Task::notes() const {
  65. return d->notes;
  66. }
  67. int RTM::Task::priority() const {
  68. return d->priority;
  69. }
  70. QString RTM::Task::repeatString() const {
  71. return d->repeatString;
  72. }
  73. RTM::TaskSeriesId RTM::Task::seriesId() const {
  74. return d->seriesId;
  75. }
  76. QStringList RTM::Task::tags() const {
  77. return QStringList(d->tags);
  78. }
  79. QString RTM::Task::url() const {
  80. return d->url;
  81. }
  82. int RTM::Task::decreasePriority() {
  83. d->priority--;
  84. RTM::Request *request = d->standardRequest("rtm.tasks.movePriority");
  85. request->addArgument("direction", "down");
  86. request->sendRequest();
  87. return d->priority;
  88. }
  89. int RTM::Task::increasePriority() {
  90. d->priority++;
  91. RTM::Request *request = d->standardRequest("rtm.tasks.movePriority");
  92. request->addArgument("direction", "up");
  93. request->sendRequest();
  94. return d->priority;
  95. }
  96. void RTM::Task::setCompleted(bool completed) {
  97. if (isCompleted() == completed)
  98. return;
  99. RTM::Request *request;
  100. if (completed) {
  101. d->completed = QDateTime::currentDateTime();
  102. request = d->standardRequest("rtm.tasks.complete");
  103. }
  104. else {
  105. d->completed = QDateTime();
  106. request = d->standardRequest("rtm.tasks.uncomplete");
  107. }
  108. request->sendRequest();
  109. }
  110. void RTM::Task::setDeleted(bool deleted) {
  111. if (isCompleted() == deleted)
  112. return;
  113. RTM::Request *request;
  114. if (deleted) {
  115. d->deleted = QDateTime::currentDateTime();
  116. request = d->standardRequest("rtm.tasks.delete");
  117. }
  118. else {
  119. kDebug() << "ERROR: RTM Does not allow undeleting tasks!";
  120. d->deleted = QDateTime();
  121. request = d->standardRequest("rtm.tasks.undelete");
  122. }
  123. request->sendRequest();
  124. }
  125. void RTM::Task::setList(RTM::ListId listId) {
  126. RTM::ListId oldlist = d->listId;
  127. d->listId = listId;
  128. RTM::Request *request = d->session->request("rtm.tasks.moveTo");
  129. request->addArgument("timeline", QString::number(d->session->getTimeline()));
  130. request->addArgument("from_list_id", QString::number(oldlist));
  131. request->addArgument("to_list_id", QString::number(listId));
  132. request->addArgument("taskseries_id", QString::number(d->seriesId));
  133. request->addArgument("task_id", QString::number(d->taskId));
  134. }
  135. void RTM::Task::postpone() {
  136. //WARNING: Doesn't update task, only sends network request
  137. RTM::Request *request = d->standardRequest("rtm.tasks.postpone");
  138. request->sendRequest();
  139. }
  140. void RTM::Task::addTag(const RTM::Tag& tag) {
  141. if (d->tags.contains(tag))
  142. return;
  143. d->tags.append(tag);
  144. RTM::Request *request = d->standardRequest("rtm.tasks.addTags");
  145. request->addArgument("tags", tag);
  146. request->sendRequest();
  147. }
  148. void RTM::Task::removeAllTags() {
  149. d->tags.clear();
  150. RTM::Request *request = d->standardRequest("rtm.tasks.setTags");
  151. request->addArgument("tags", "");
  152. request->sendRequest();
  153. }
  154. bool RTM::Task::removeTag(const RTM::Tag& tag) {
  155. if (!d->tags.contains(tag))
  156. return false;
  157. d->tags.removeAll(tag);
  158. RTM::Request *request = d->standardRequest("rtm.tasks.removeTags");
  159. request->addArgument("tags", tag);
  160. request->sendRequest();
  161. return true;
  162. }
  163. void RTM::Task::setTags(const QStringList &tags) {
  164. RTM::Request *request = d->standardRequest("rtm.tasks.setTags");
  165. request->addArgument("tags", tags.join(","));
  166. request->sendRequest();
  167. }
  168. void RTM::Task::setNotes(const Notes& notes) {
  169. Q_UNUSED(notes)
  170. kError() << "NOT IMPLEMENTED"; //FIXME Implement
  171. }
  172. void RTM::Task::addNote(const QString& title, const QString& text) {
  173. //notes.insert(note.getId(), note);
  174. RTM::Request *request = d->standardRequest("rtm.tasks.notes.add");
  175. request->addArgument("note_title", title);
  176. request->addArgument("note_text", text);
  177. request->sendRequest();
  178. }
  179. bool RTM::Task::editNote(RTM::NoteId noteid, const QString& newTitle, const QString& newText) {
  180. if (!d->notes.contains(noteid))
  181. return false;
  182. RTM::Request *request = d->session->request("rtm.tasks.notes.edit"); // Only a simple request is needed
  183. request->addArgument("timeline", QString::number(d->session->getTimeline()));
  184. request->addArgument("note_id", QString::number(noteid));
  185. request->addArgument("note_title", newTitle);
  186. request->addArgument("note_text", newText);
  187. request->sendRequest();
  188. return true;
  189. }
  190. void RTM::Task::removeAllNotes() {
  191. foreach(const RTM::NoteId &id, d->notes.keys())
  192. removeNote(id);
  193. }
  194. bool RTM::Task::removeNote(RTM::NoteId noteid) {
  195. if (!d->notes.contains(noteid))
  196. return false;
  197. RTM::Request *request = d->session->request("rtm.tasks.notes.delete"); // Only a simple request is needed
  198. request->addArgument("timeline", QString::number(d->session->getTimeline()));
  199. request->addArgument("note_id", QString::number(noteid));
  200. request->sendRequest();
  201. return true;
  202. }
  203. void RTM::Task::setDue(const QDateTime& due) {
  204. if (d->due == due)
  205. return;
  206. d->due = due;
  207. RTM::Request *request = d->standardRequest("rtm.tasks.setDueDate");
  208. if (due.isValid()) {
  209. request->addArgument("due", d->due.toString(Qt::ISODate));
  210. if (due.time().isValid())
  211. request->addArgument("has_due_time", "1");
  212. }
  213. request->sendRequest();
  214. }
  215. void RTM::Task::setDue(const QString& date) {
  216. RTM::Request *request = d->standardRequest("rtm.tasks.setDueDate");
  217. request->addArgument("due", date);
  218. request->addArgument("parse", "1");
  219. request->sendRequest();
  220. }
  221. void RTM::Task::setEstimate(const QString& estimate) {
  222. if (d->estimate == estimate)
  223. return;
  224. d->estimate == estimate;
  225. kDebug() << "Setting Estimate to: " << estimate;
  226. RTM::Request *request = d->standardRequest("rtm.tasks.setEstimate");
  227. request->addArgument("estimate", estimate);
  228. request->sendRequest();
  229. }
  230. void RTM::Task::setLocationId(RTM::LocationId locationid) {
  231. d->locationId = locationid;
  232. RTM::Request *request = d->standardRequest("rtm.tasks.setLocation");
  233. request->addArgument("location_id", QString::number(locationid));
  234. }
  235. void RTM::Task::setName(const QString& name) {
  236. if (d->name == name)
  237. return;
  238. d->name = name;
  239. RTM::Request *request = d->standardRequest("rtm.tasks.setName");
  240. request->addArgument("name", name);
  241. request->sendRequest();
  242. }
  243. void RTM::Task::setPriority(int priority) {
  244. if (d->priority == priority)
  245. return;
  246. d->priority = priority;
  247. RTM::Request *request = d->standardRequest("rtm.tasks.setPriority");
  248. request->addArgument("priority", QString::number(priority));
  249. request->sendRequest();
  250. }
  251. void RTM::Task::setRepeatString(const QString& repeatString) {
  252. if (d->repeatString == repeatString)
  253. return;
  254. d->repeatString = repeatString;
  255. RTM::Request *request = d->standardRequest("rtm.tasks.setRecurrence");
  256. request->addArgument("repeat", repeatString);
  257. }
  258. void RTM::Task::setUrl(const QString& url) {
  259. if (d->url == url)
  260. return;
  261. d->url = url;
  262. RTM::Request *request = d->standardRequest("rtm.tasks.setUrl");
  263. request->addArgument("url", url);
  264. }
  265. void RTM::Task::undoLastAction() {
  266. //TODO: Implement
  267. }
  268. RTM::Task* RTM::Task::createSyncTaskFromString(RTM::Session* session, const QString& task) {
  269. RTM::Request *request = session->request("rtm.tasks.add"); // auth token is done for us
  270. request->addArgument("name", task);
  271. request->addArgument("parse", "1");
  272. request->addArgument("timeline", QString::number(session->getTimeline()));
  273. request->sendSynchronousRequest();
  274. RTM::TasksReader reader(request, session);
  275. reader.read();
  276. //return reader.getReadTasks()->values().first();
  277. return new Task(session); //TODO: rewrite async.
  278. }
  279. RTM::Task* RTM::Task::uninitializedTask(RTM::Session* session) {
  280. return new Task(session);
  281. }
  282. #include "task.moc"