redmineclass_time_entry.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "redmineclass_time_entry.h"
  2. #include "errno.h"
  3. void RedmineClass_TimeEntry::init()
  4. {
  5. this->redmine = NULL;
  6. this->id = 0;
  7. this->saveReply = NULL;
  8. return;
  9. }
  10. RedmineClass_TimeEntry::RedmineClass_TimeEntry()
  11. {
  12. this->init();
  13. return;
  14. }
  15. int RedmineClass_TimeEntry::setRedmine ( Redmine *redmine )
  16. {
  17. if (this->redmine != NULL) {
  18. qCritical("This case is not implemented, yet.");
  19. return ECANCELED;
  20. }
  21. this->redmine = redmine;
  22. connect ( this->redmine, SIGNAL ( callback_call ( void*, callback_t, QNetworkReply*, QJsonDocument*, void* ) ),
  23. this, SLOT ( callback_dispatcher ( void*, callback_t, QNetworkReply*, QJsonDocument*, void* ) ) );
  24. return 0;
  25. }
  26. RedmineClass_TimeEntry::RedmineClass_TimeEntry ( Redmine *redmine )
  27. {
  28. this->init();
  29. this->setRedmine ( redmine );
  30. return;
  31. }
  32. RedmineClass_TimeEntry::~RedmineClass_TimeEntry()
  33. {
  34. return;
  35. }
  36. int RedmineClass_TimeEntry::setProjectId ( int projectId )
  37. {
  38. this->projectId = projectId;
  39. return 0;
  40. }
  41. int RedmineClass_TimeEntry::getProjectId()
  42. {
  43. return this->projectId;
  44. }
  45. int RedmineClass_TimeEntry::setIssueId ( int issueId )
  46. {
  47. this->issueId = issueId;
  48. return 0;
  49. }
  50. int RedmineClass_TimeEntry::getIssueId()
  51. {
  52. return this->issueId;
  53. }
  54. void RedmineClass_TimeEntry::saveCallback( QNetworkReply *reply, QJsonDocument *timeEntry_doc, void *_null )
  55. {
  56. ( void ) reply; ( void ) timeEntry_doc; ( void ) _null;
  57. qDebug ( "RedmineClass_TimeEntry::saveCallback()" );
  58. this->saveTimer.stop();
  59. QJsonObject timeEntry = timeEntry_doc->object()["time_entry"].toObject();
  60. if (timeEntry.empty()) {
  61. qDebug ( "RedmineClass_TimeEntry::saveCallback(): timeEntry.empty()" );
  62. this->on_saveFailure(reply);
  63. return;
  64. }
  65. int timeEntryId = timeEntry["id"].toInt();
  66. if (timeEntryId == 0) {
  67. qDebug ( "RedmineClass_TimeEntry::saveCallback(): timeEntryId == 0" );
  68. this->on_saveFailure(reply);
  69. return;
  70. }
  71. qDebug ( "RedmineClass_TimeEntry::saveCallback(): ok" );
  72. this->on_saveSuccess();
  73. return;
  74. }
  75. void RedmineClass_TimeEntry::saveTimeout()
  76. {
  77. qDebug ( "RedmineClass_TimeEntry::saveTimeout()" );
  78. this->on_saveTimeout();
  79. return;
  80. }
  81. int RedmineClass_TimeEntry::save()
  82. {
  83. if ( this->redmine == NULL )
  84. return EHOSTUNREACH;
  85. if ( this->saveTimer.isActive() ) {
  86. qDebug("RedmineClass_TimeEntry::save(): Saving is already in progress. Cancelling the new request.");
  87. return EINPROGRESS;
  88. }
  89. QVariantMap timeEntries, timeEntry;
  90. QString uri;
  91. RedmineClient::EMode mode = ( this->id != 0 ? RedmineClient::EMode::PUT : RedmineClient::EMode::POST );
  92. if ( this->id != 0 ) {
  93. uri = "time_entries/" + QString ( this->id );
  94. timeEntry["id"] = QVariant ( this->id );
  95. } else {
  96. uri = "time_entries";
  97. }
  98. timeEntry["hours"] = QVariant ( this->hours );
  99. timeEntry["endtime"] = QVariant ( this->endtime );
  100. if ( this->issueId ) {
  101. timeEntry["issue_id"] = QVariant ( this->issueId );
  102. } else {
  103. if ( !this->projectId ) {
  104. qDebug ( "RedmineClass_TimeEntry::save(): ERROR: this->issueId == 0; this->projectId == 0" );
  105. return EINVAL;
  106. }
  107. timeEntry["project_id"] = QVariant ( this->projectId );
  108. }
  109. timeEntry["comments"] = QVariant ( this->comment );
  110. timeEntry["spent_on"] = QVariant ( this->endtime.date() );
  111. timeEntry["activity_id"] = QVariant ( this->activityId );
  112. timeEntries["time_entry"] = timeEntry;
  113. this->saveReply = this->redmine->request ( mode, "time_entries", this, (Redmine::callback_t)&RedmineClass_TimeEntry::saveCallback, NULL, false, "", timeEntries );
  114. this->saveTimer.setSingleShot ( true );
  115. connect ( &this->saveTimer, SIGNAL ( timeout() ), this, SLOT ( saveTimeout() ) );
  116. this->saveTimer.start ( REDMINE_BASETIMEOUT );
  117. return 0;
  118. }
  119. int RedmineClass_TimeEntry::set ( QDateTime timeFrom, QDateTime timeTo, int projectId, int issueId, QString comment, int activityId )
  120. {
  121. this->hours = ( ( float ) ( ( time_t ) ( timeTo.toTime_t() - timeFrom.toTime_t() ) ) ) / 3600;
  122. this->endtime = timeTo;
  123. if ( issueId != -1 )
  124. this->issueId = issueId;
  125. if ( projectId != -1 )
  126. this->projectId = projectId;
  127. this->comment = comment;
  128. this->activityId = activityId;
  129. if ( issueId == 0 ) {
  130. qDebug ( "RedmineClass_TimeEntry::set(): issueId is not set. Trying the default." );
  131. QJsonValueRef defaultIssueIdObj = this->redmine->me() ["default_issue_id"];
  132. if ( !defaultIssueIdObj.toInt() ) {
  133. qDebug ( "RedmineClass_TimeEntry::set(): The default issueId is not set, too. ERROR." );
  134. return EINVAL;
  135. }
  136. issueId = defaultIssueIdObj.toInt();
  137. }
  138. return 0;
  139. }
  140. /*
  141. int RedmineClass_TimeEntry::set(QJsonObject *json) {
  142. return 0;
  143. }*/