example/LastFmAuthApp/inc/requestSP.h
changeset 26 83d6a149c755
equal deleted inserted replaced
25:a180113055cb 26:83d6a149c755
       
     1 /**
       
     2 * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "{License}"
       
     6 * which accompanies  this distribution, and is available
       
     7 * at the URL "{LicenseUrl}".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Siddhartha Chandra, Sasken Communication Technologies Ltd
       
    14 * Description:
       
    15 * class to handle calls to rest Server API's
       
    16 */
       
    17 #ifndef FBREQUEST_H
       
    18 #define FBREQUEST_H
       
    19 
       
    20 #include <QObject>
       
    21 #include <QString>
       
    22 #include <QDateTime>
       
    23 #include <QHttp>
       
    24 #include <QHash>
       
    25 #include <QNetworkAccessManager>
       
    26 #include <QNetworkReply>
       
    27 #include "progressbar.h"
       
    28 #include "authAppConstants.h"
       
    29 #include "xmlParser.h"
       
    30 
       
    31 // FORWARD DECLARATIONS
       
    32 class FBSession;
       
    33 class FBError;
       
    34 
       
    35 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    36 class  FBRequest : public QObject
       
    37 {
       
    38     Q_OBJECT
       
    39 
       
    40 private:
       
    41 
       
    42     /* pointer to the session that owns this request */
       
    43     FBSession*  iSession;
       
    44 
       
    45     /**
       
    46      * The URL which will be contacted to execute the request.
       
    47      */
       
    48     QString iUrl;
       
    49 
       
    50     /**
       
    51      * The API method which will be called.
       
    52      */
       
    53     QString iMethod;
       
    54 
       
    55     /**
       
    56      * An object used by the user of the request to help identify the meaning of the request.
       
    57      */
       
    58     void*   iUserInfo;
       
    59 
       
    60     /**
       
    61      * The dictionary of parameters to pass to the method.
       
    62      *
       
    63      * These values in the dictionary will be converted to strings using the
       
    64      * standard Objective-C object-to-string conversion facilities.
       
    65      */
       
    66     Dictionary  iParams;
       
    67 
       
    68     /**
       
    69      * A data parameter.
       
    70      *
       
    71      * Used for methods such as photos.upload, video.upload, events.create, and
       
    72      * events.edit.
       
    73      */
       
    74     QByteArray  iDataParam;
       
    75 
       
    76     /**
       
    77       * true if iDataParam holds picture data
       
    78       */
       
    79     bool        iDataParamPicture;
       
    80 
       
    81     /**
       
    82      * The timestamp of when the request was sent to the server.
       
    83      */
       
    84     QDateTime   iTimestamp;
       
    85     QHttp       iConnection;
       
    86     QByteArray  iResponseText;
       
    87 
       
    88     QNetworkAccessManager iNetworkAccessManager;
       
    89     progressbar* pbar;
       
    90 
       
    91 signals: /* the signals ommitted by FBRequest */
       
    92 
       
    93     /**
       
    94      * Called just before the request is sent to the server.
       
    95      */
       
    96     void requestLoading();
       
    97 
       
    98     /**
       
    99      * Called when an error prevents the request from completing successfully.
       
   100      */
       
   101     void requestFailedWithNetworkError( QNetworkReply::NetworkError code );
       
   102     void requestFailedWithFlickrError ( const FBError& aError );
       
   103 
       
   104     /**
       
   105      * Called when a request returns and its response has been parsed into an object.
       
   106      *
       
   107      * The resulting object may be a dictionary, an array, a string, or a number, depending
       
   108      * on thee format of the API response.
       
   109      */
       
   110     void requestDidLoad ( const QVariant& aResult);
       
   111     //void requestDidLoadSession( const QVariant& aResult);
       
   112 
       
   113 
       
   114 private slots:
       
   115     void networkReplyError ( QNetworkReply::NetworkError code );
       
   116     void networkReplyFinished ();
       
   117 
       
   118 public: /* class functions */
       
   119     /**
       
   120      * Creates a new API request for the global session.
       
   121      */
       
   122     static FBRequest* request();
       
   123 
       
   124     /**
       
   125      * Creates a new API request for a particular session.
       
   126      */
       
   127     static FBRequest* requestWithSession (FBSession* aSession);
       
   128 
       
   129 public: /* instance functions */
       
   130 
       
   131     /**
       
   132      * Creates a new request paired to a session.
       
   133      */
       
   134     FBRequest (FBSession* aSession);
       
   135 
       
   136     /**
       
   137      * Calls a method on the server asynchronously.
       
   138      *
       
   139      * The delegate will be called for each stage of the loading process.
       
   140      */
       
   141     void call (const QString& aMethod, const Dictionary& aParams);
       
   142     
       
   143     void callforToken ();
       
   144 
       
   145     /**
       
   146      * Calls a method on the server asynchronously, with a file upload component.
       
   147      *
       
   148      * The delegate will be called for each stage of the loading process.
       
   149      */
       
   150     void callWithDataParams (const QString& aMethod, const Dictionary& aParams, const QByteArray& aDataParam, bool aDataParamPicture);
       
   151 
       
   152     /**
       
   153      * Calls a URL on the server asynchronously.
       
   154      *
       
   155      * The delegate will be called for each stage of the loading process.
       
   156      */
       
   157     void post( const QString& aUrl, const Dictionary& aParams);
       
   158 
       
   159     /**
       
   160      * Stops an active request before the response has returned.
       
   161      */
       
   162     void cancel();
       
   163 
       
   164     /**
       
   165       * returns the time stamp of when the request was sent to the server
       
   166       */
       
   167     const QDateTime& timeStamp() const;
       
   168 
       
   169     void connect();
       
   170     
       
   171     QString generateSig(Dictionary Params);
       
   172     
       
   173     void proxysettings();
       
   174 
       
   175 private:
       
   176     /**
       
   177       * Given a string returns its hex coded md5 hash
       
   178       */
       
   179     static QString md5(const QString&);
       
   180 
       
   181     /**
       
   182       * @return true if the current request method is a special method
       
   183       */
       
   184     bool isSpecialMethod() const;
       
   185 
       
   186     /**
       
   187       * @return QString a url to use for the given method
       
   188       */
       
   189     QString urlForMethod (const QString& aMethod) const;
       
   190 
       
   191     /**
       
   192       * @return the Get Url for the request
       
   193       */
       
   194     QString generateGetURL() const;
       
   195 
       
   196     QString generateCallId() const;
       
   197     
       
   198 
       
   199     void generatePostBody(QByteArray& body);
       
   200 
       
   201     /**
       
   202       * handles the data received from the server
       
   203       * @param aResponseData is the data received from the server
       
   204       */
       
   205     void handleResponseData( const QByteArray& aResponseData );
       
   206 
       
   207     /**
       
   208       * @param aResponseData is the data received from the server
       
   209       * @param aError will get error codes if any error occurs ( this will change in the future )
       
   210       * @return a void* pointer, this will change
       
   211       */
       
   212     QVariant parseXMLResponse ( const QByteArray& aResponseData, FBError& aError);
       
   213 
       
   214 };
       
   215 
       
   216 #endif // FBREQUEST_H