File indexing completed on 2024-05-05 04:39:55

0001 /*
0002     SPDX-FileCopyrightText: 2012-2013 Miquel Sabaté <mikisabate@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef GH_RESOURCE_H
0008 #define GH_RESOURCE_H
0009 
0010 
0011 #include <QObject>
0012 
0013 class KJob;
0014 namespace KIO {
0015     class Job;
0016     class TransferJob;
0017 }
0018 
0019 namespace gh
0020 {
0021 class ProviderModel;
0022 
0023 /**
0024  * @class Resource
0025  *
0026  * This class provides methods that extract information from the given
0027  * Github's JSON responses.
0028  */
0029 class Resource : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     /**
0035      * Constructor.
0036      *
0037      * @param parent The QObject this Resource is parented to.
0038      * @param model The model to be used in the other methods.
0039      */
0040     explicit Resource(QObject *parent, ProviderModel *model);
0041 
0042     /**
0043      * Search repos by calling the Github API. When successful, it will re-fill
0044      * the model provided in the constructor. Otherwise, if something went
0045      * wrong, it will do nothing.
0046      *
0047      * @param uri A string containing the URI to be called to retrieve
0048      * the repos. Therefore, this parameter also determines whether the repos
0049      * belong to a user or a organization.
0050      * @param token The authorization token.
0051      */
0052     void searchRepos(const QString &uri, const QString &token);
0053 
0054     /**
0055      * Get public and private organizations for the currently authenticated
0056      * user and save it to its local account.
0057      *
0058      * @param token The authorization token.
0059      */
0060     void getOrgs(const QString &token);
0061 
0062     /**
0063      * Authenticate the current user with the given name and password.
0064      * This method will eventually emit the authenticated signal.
0065      *
0066      * @param name The username of the current user.
0067      * @param password The password used to login the current user.
0068      */
0069     void authenticate(const QString &name, const QString &password);
0070 
0071     /**
0072      * Authenticate the current user with the two factor authentication code.
0073      * Must be called after a call to authenticate.
0074      *
0075      * @param code The two factor authentication code.
0076      */
0077     void twoFactorAuthenticate(const QString &transferHeader, const QString &code);
0078 
0079     /**
0080      * Revoke an access to the Github API (a.k.a. log out the current user).
0081      *
0082      * @param id The id of the currently valid authorization.
0083      * @param name The name of the current user.
0084      * @param password The Github API requires the password again to perform
0085      * this action.
0086      */
0087     void revokeAccess(const QString &id, const QString &name, const QString &password);
0088 
0089 private:
0090     /**
0091      * Get a TransferJob for the given path and authorization token.
0092      *
0093      * @param path The path to be requested (i.e. "/user/orgs")
0094      * @param token The authorization token to be set on the headers.
0095      */
0096     KIO::TransferJob * getTransferJob(const QString &path, const QString &token) const;
0097 
0098     /**
0099      * Retrieve the info of the name and the URL of each repo and append
0100      * it in the model that was provided in the constructor.
0101      *
0102      * @param data A Github response in JSON format.
0103      */
0104     void retrieveRepos(const QByteArray &data);
0105 
0106     /**
0107      * Retrieve the organizations for the current user from the given
0108      * JSON data. It will emit the orgsUpdated signal.
0109      *
0110      * @param data A Github response in JSON format.
0111      */
0112     void retrieveOrgs(const QByteArray &data);
0113 
0114 Q_SIGNALS:
0115     /**
0116      * This signal will eventually be sent when the authentication process
0117      * has finished. An empty id means that the authentication wasn't
0118      * successful.
0119      *
0120      * @param id The id of the authorization. Empty if something went wrong.
0121      * @param token The authorization token (e.g. "14bf8e87e2ec5fe30f8a6755bda63b5bc4d02e22").
0122      *   Empty if something went wrong.
0123      * @param tokenName The authorization token (e.g. "KDevelop Github Provider : machinename - Thu Nov 17 23:18:03 2016 GMT").
0124      *   Empty if something went wrong.
0125      */
0126     void authenticated(const QByteArray &id, const QByteArray &token, const QString &tokenName);
0127 
0128     /**
0129      * This signal is sent if two factor authentication is requested by GitHub.
0130      */
0131     void twoFactorAuthRequested(const QString &transferHeader);
0132 
0133     /**
0134      * This signal is emitted when the model containing repos has
0135      * been updated.
0136      */
0137     void reposUpdated();
0138 
0139     /**
0140      * This signal is fired when the list of organizations for the current
0141      * user has been updated.
0142      *
0143      * @param orgs A list of the names of the organizations.
0144      */
0145     void orgsUpdated(const QStringList &orgs);
0146 
0147 private Q_SLOTS:
0148     /**
0149      * Handle the response of the Github authentication process.
0150      *
0151      * @param job The KJob responsible from the authentication.
0152      */
0153     void slotAuthenticate(KJob *job);
0154 
0155     /**
0156      * Handle the response of retrieving repos from Github.
0157      *
0158      * @param job The job returned after the HTTP request has finished.
0159      * @param data The data returned by the HTTP response.
0160      */
0161     void slotRepos(KIO::Job *job, const QByteArray &data);
0162 
0163     /**
0164      * Handle the response of retrieving orgs for the currently
0165      * authenticated user.
0166      *
0167      * @param job The job returned after the HTTP request has finished.
0168      * @param data The data returned by the HTTP response.
0169      */
0170     void slotOrgs(KIO::Job *job, const QByteArray &data);
0171 
0172 private:
0173     ProviderModel *m_model;
0174     QByteArray m_temp, m_orgTemp;
0175 };
0176 
0177 } // End of namespace gh
0178 
0179 
0180 #endif // GH_RESOURCE_H