示例#1
0
void Tracker::onSocketEvent(TcpSocket *sock, SocketEvent evt) {
	CHECK_RET(sock == m_socket.get());

	if (evt == SOCK_CONNECTED) {
		sendGetRequest();
	} else if (evt == SOCK_READ) {
		*m_socket >> m_inBuffer;
		parseBuffer();
	} else if (
void UnfuddleAPIWrapper::requestFinished(QNetworkReply *reply)
{
    QVariant requestTypeV = reply->property("requestType");
    Q_ASSERT(requestTypeV.isValid());
    RequestType requestType = static_cast<RequestType>(requestTypeV.toInt());

    if (reply->error() == QNetworkReply::NoError) {
        if (!isOnline) {
            isOnline = true;
            emit wentOnline();
        }
        // the following block detects if we use wrong protocol - http instead of https
        // FIXME: what if SSL not available for our account and we use https?
        // probably should handle ssl errors too
        QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        if (statusCode.toInt() == 302) {
            this->setUseSsl(!this->getUseSsl());

            // FIXME: the request which is failed may not necessarily be /account
            // BUG POSSIBILITY IS HIGH!!!
            requestGetAccount();

            return;
        }

        QNetworkReply *nr;

        switch (requestType) {
        case ACCOUNT_LOAD:
            emit getAccountComplete(unf::Account::loadFromDom(rootElement(reply)));
            break;
        case CURRENT_PERSON_LOAD:
            emit getCurrentPersonComplete(unf::Person::loadFromDom(rootElement(reply)));
            break;
        case PERSON_INVOLVEMENTS_LOAD:
            {
                involvementsForProjectsForPerson =
                        unf::Involvement::loadListFromDom(rootElement(reply));
                nr = sendGetRequest("/projects");
                nr->setProperty("requestType", PROJECTS_LOAD);
                nr->setProperty("person_id", reply->property("person_id"));
            }
            break;
        case PROJECTS_LOAD:
            {
                QList<unf::Project> projects =
                        unf::Project::loadListFromDom(rootElement(reply));
                projects = unf::Project::filterProjectListWithInvolvements(
                        projects,
                        involvementsForProjectsForPerson);
                emit getProjectsForPersonComplete(projects,
                                                  reply->property("person_id").toInt());
            }
            break;
        case MILESTONES_LOAD: // Milestones are not used in the program
            break;
        case TICKETS_LOAD:
            {
                int person_id = reply->property("person_id").toInt();
                QList<unf::Ticket> tickets = unf::Ticket::loadListFromDom(rootElement(reply));
                QList<unf::Ticket> tmpTickets;  // filter tickets
                foreach (unf::Ticket t, tickets) {
                    if (t.assignee_id == person_id && t.status != "closed")
                        tmpTickets.push_back(t);
                }
                emit getTicketsForPersonComplete(tmpTickets, person_id,
                                                 reply->property("project_id").toInt());
            }
            break;
        case UPDATE_TIME_ENTRY:
            emit saveTimeEntryComplete(reply->property("project_id").toInt(),
                                       reply->property("ticket_id").toInt(),
                                       timeEntryForSaving);
            break;
        case LIST_TIME_ENTRIES:
        case LIST_AFTER_POST_TIME_ENTRY:
            {
                int project_id = reply->property("project_id").toInt();
                int ticket_id = reply->property("ticket_id").toInt();

                QList<unf::TimeEntry> timeEntries =
                        unf::TimeEntry::loadListFromDom(rootElement(reply));

                unf::TimeEntry curTimeEntry;
                bool found = false;
                foreach (unf::TimeEntry te, timeEntries) {
                    if (te.date == timeEntryForSaving.date &&
                        te.person_id == timeEntryForSaving.person_id) {
                            curTimeEntry = te;
                            found = true;
                            break;
                    }
                }

                if (found) {
                    if (requestType == LIST_TIME_ENTRIES) {
                        curTimeEntry.hours += reply->property("hoursAdd").toFloat();
                        requestSaveTimeEntry(project_id, ticket_id, curTimeEntry);
                    }
                    else if (requestType == LIST_AFTER_POST_TIME_ENTRY)
                        emit saveTimeEntryComplete(project_id, ticket_id, curTimeEntry);
                }
                else {
                    timeEntryForSaving.hours = reply->property("hoursAdd").toFloat();
                    nr = sendPostRequest(
                            QString("/projects/%1/tickets/%2/time_entries")
                                .arg(project_id)
                                .arg(ticket_id),
                            timeEntryForSaving.toXml().toUtf8());
                    nr->setProperty("requestType", POST_NEW_TIME_ENTRY);
                    nr->setProperty("project_id", project_id);
                    nr->setProperty("ticket_id", ticket_id);
                }
            }
            break;
        case POST_NEW_TIME_ENTRY:
            {
                int project_id = reply->property("project_id").toInt();
                int ticket_id = reply->property("ticket_id").toInt();

                nr = sendGetRequest(
                        QString("/projects/%1/tickets/%2/time_entries")
                            .arg(project_id)
                            .arg(ticket_id));
                nr->setProperty("requestType", LIST_AFTER_POST_TIME_ENTRY);
                nr->setProperty("project_id", project_id);
                nr->setProperty("ticket_id", ticket_id);
            }
            break;
        case CHANGE_TICKET:
            emit changeTicketComplete(reply->property("project_id").toInt(),
                                      reply->property("ticket_id").toInt());
            break;
        case ADD_TICKET_COMMENT:
            {
                int project_id = reply->property("project_id").toInt();
                int ticket_id = reply->property("ticket_id").toInt();

                nr = sendGetRequest(QString("/projects/%1/tickets/%2/comments")
                                        .arg(project_id).arg(ticket_id));
                nr->setProperty("requestType", LIST_TICKET_COMMENTS);
                nr->setProperty("project_id", project_id);
                nr->setProperty("ticket_id", ticket_id);
            }
            break;
        case LIST_TICKET_COMMENTS:
            {
                int comment_id = unf::Comment::loadFromDom(
                        rootElement(reply).lastChild().toElement()).id;
                emit addTicketCommentComplete(reply->property("project_id").toInt(),
                                              reply->property("ticket_id").toInt(),
                                              comment_id);
            }
            break;
        case UPLOAD_TICKET_COMMENT_ATTACHMENT:
            {
                int project_id = reply->property("project_id").toInt();
                int ticket_id = reply->property("ticket_id").toInt();
                int comment_id = reply->property("comment_id").toInt();

                unf::Attachment a;
                a.filename = reply->property("filename").toString();
                a.content_type = "application/octet-stream";
                a.upload_key = rootElement(reply).firstChildElement("key").text();

                nr = sendPostRequest(
                        QString("/projects/%1/tickets/%2/comments/%3/attachments")
                            .arg(project_id).arg(ticket_id).arg(comment_id),
                        a.toXml().toUtf8());
                nr->setProperty("requestType", ADD_ATTACHMENT_TO_TICKET_COMMENT);
                nr->setProperty("project_id", project_id);
                nr->setProperty("ticket_id", ticket_id);
                nr->setProperty("comment_id", comment_id);
            }
            break;
        case ADD_ATTACHMENT_TO_TICKET_COMMENT:
            emit addAttachmentToTicketCommentComplete(reply->property("project_id").toInt(),
                                                      reply->property("ticket_id").toInt(),
                                                      reply->property("comment_id").toInt());
            break;
        case TICKET_COMMENTS_LOAD:
            emit getCommentsForTicketComplete(
                    unf::Comment::loadListFromDom(rootElement(reply)),
                    reply->property("project_id").toInt(),
                    reply->property("ticket_id").toInt());
            break;
        case ALL_PEOPLE_LOAD:
            emit getAllPeopleComplete(unf::Person::loadListFromDom(rootElement(reply)));
            break;
        }
    }
    else