example/TwitterAuthApp/inc/sessionSP.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 maintian session & all credential keys.
       
    16 */
       
    17 
       
    18 #ifndef FBSESSION_H
       
    19 #define FBSESSION_H
       
    20 
       
    21 #include <QObject>
       
    22 #include <QList>
       
    23 #include <QString>
       
    24 #include <QSettings>
       
    25 #include <QDateTime>
       
    26 #include "smfcredmgrclient.h"
       
    27 #include "authAppConstants.h"
       
    28 
       
    29 // FORWARD DECLARATIONS
       
    30 class FBRequest;
       
    31 
       
    32 const QString KFacebokkKeysFileName = "c://Data//FacebookKeys.txt";
       
    33 
       
    34 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    35 /**
       
    36  * An FBSession represents a single user's authenticated session for a Facebook application.
       
    37  *
       
    38  * To create a session, you must use the session key of your application (which can
       
    39  * be found on the Facebook developer website).  You may then use the login dialog to ask
       
    40  * the user to enter their email address and password.  If successful, you will get back a
       
    41  * session key which can be used to make requests to the Facebook API.
       
    42  *
       
    43  * Session keys are cached and stored on the disk of the device so that you do not need to ask
       
    44  * the user to login every time they launch the app.  To restore the last active session, call the
       
    45  * resume method after instantiating your session.
       
    46  */
       
    47 class FBSession : public QObject
       
    48 {
       
    49     Q_OBJECT
       
    50 
       
    51 private:
       
    52     /**
       
    53       * the queue of requests
       
    54       */
       
    55     QList<FBRequest*>           iRequestQueue;
       
    56 
       
    57     /**
       
    58      * Your application's API key, as passed to the constructor.
       
    59      */
       
    60     QString iApiKey;
       
    61 
       
    62     /**
       
    63      * Your application's API secret, as passed to the constructor.
       
    64      */
       
    65     QString iApiSecret;
       
    66 
       
    67     /**
       
    68      * The URL to call to create a session key after login.
       
    69      *
       
    70      * This is an alternative to calling auth.getSession directly using the secret key.
       
    71      */
       
    72     QString iGetSessionProxy;
       
    73 
       
    74     /**
       
    75      * The current user's session key.
       
    76      */
       
    77     QString iSessionKey;
       
    78 
       
    79     /**
       
    80      * The current user's session secret.
       
    81      */
       
    82     QString iSessionSecret;
       
    83 
       
    84     /**
       
    85      * The expiration date of the session key.
       
    86      */
       
    87     QDateTime iExpirationDate;
       
    88 
       
    89 
       
    90     /**
       
    91       * the time at which the last request was performed, this is used to prevent
       
    92       * too many requests going to the server at once.
       
    93       */
       
    94     QDateTime   iLastRequestTime;
       
    95     int         iRequestBurstCount;
       
    96 
       
    97 
       
    98     /**
       
    99       * The settings object used to restore session from the disk.
       
   100       */
       
   101     QSettings   iSettings;
       
   102     /**
       
   103       * The Credentail Object used to store auth data
       
   104       */
       
   105     SmfCredMgrClient* m_Client; 
       
   106     /**
       
   107       * Variable to store 
       
   108       */ 
       
   109     QString iCMRegToken;
       
   110 
       
   111 signals: /* the signals ommitted by FBSession */
       
   112 
       
   113     /**
       
   114       * Called when session logged in sucessfully
       
   115       * @param SessionKey is the fb assigned session key
       
   116       */
       
   117     void sessionDidLogin (QString SessionKey);
       
   118 
       
   119     /**
       
   120      * Called when a user closes the login dialog without logging in.
       
   121      */
       
   122     void sessionDidNotLogin ();
       
   123 
       
   124     /**
       
   125      * Called when a session is about to log out.
       
   126      * @param aUid is the fb assigned session id
       
   127      */
       
   128     void sessionWillLogout ();
       
   129 
       
   130     /**
       
   131      * Called when a session has logged out.
       
   132      */
       
   133     void sessionDidLogout ();
       
   134 
       
   135 private slots:
       
   136     /**
       
   137       * handler function for the timer fired from startFlushTimer() function
       
   138       */
       
   139     void requestTimerReady();
       
   140 
       
   141 public: /* class functions */
       
   142 
       
   143     /**
       
   144      * The globally shared session instance.
       
   145      */
       
   146     static FBSession* session();
       
   147 
       
   148     /**
       
   149      * Sets the globally shared session instance.
       
   150      *
       
   151      * This session is not retained, so you are still responsible for retaining it yourself.  The
       
   152      * first session that is created is automatically stored here.
       
   153      */
       
   154     static void setSession(FBSession* aSession);
       
   155 
       
   156     /**
       
   157      * Constructs a session and stores it as the globally shared session instance.
       
   158      *
       
   159      * @param aSessionProxy a url to that proxies auth.getSession
       
   160      */
       
   161     static FBSession* sessionForApplication ( const QString& aKey, const QString& aSecret, const QString& aSessionProxy);
       
   162 
       
   163 public: /* instance functions */
       
   164 
       
   165     /**
       
   166      * Constructs a session for an application.
       
   167      *
       
   168      * @param secret the application secret (optional)
       
   169      * @param getSessionProxy a url to that proxies auth.getSession (optional)
       
   170      */
       
   171     FBSession( const QString& aKey, const QString& aSecret, const QString& aSessionProxy );
       
   172 
       
   173     /**
       
   174       * Destructor
       
   175       */
       
   176     ~FBSession();
       
   177 
       
   178     /**
       
   179      * Begins a session for a user with a given key and secret.
       
   180      */
       
   181     void beginSession ( const QString& aSessionKey, const QString& aSessionSecret, const QDateTime& aExpires );
       
   182 
       
   183     /**
       
   184      * Resumes a previous session whose uid, session key, and secret are cached on disk.
       
   185      */
       
   186      bool resume();
       
   187 
       
   188     /**
       
   189      * Cancels a login (no-op if the login is already complete).
       
   190      */
       
   191     void cancelLogin();
       
   192 
       
   193     /**
       
   194      * Ends the current session and deletes the uid, session key, and secret from disk.
       
   195      */
       
   196     void logout();
       
   197 
       
   198     /**
       
   199      * Sends a fully configured request to the server for execution.
       
   200      */
       
   201     void send (FBRequest* aRequest);
       
   202 
       
   203     /**
       
   204       * @return const QString& http:// URL to the facebook REST server
       
   205       */
       
   206     const QString& apiURL() const;
       
   207 
       
   208     /**
       
   209       * @return const QString& https:// URL to the secure facebook REST server
       
   210       */
       
   211     const QString& apiSecureURL() const;
       
   212 
       
   213     /**
       
   214      * Determines if the session is active and connected.
       
   215      * @return bool true if connected
       
   216      */
       
   217     bool isConnected() const;
       
   218 
       
   219     /**
       
   220       * @return const QString& the api secret
       
   221       */
       
   222     const QString& apiSecret() const { return iApiSecret; }
       
   223 
       
   224     /**
       
   225       * @return const QString& the GET? session proxy
       
   226       */
       
   227     const QString& getSessionProxy() const { return iGetSessionProxy; }
       
   228 
       
   229     /**
       
   230       * @return api key for this session
       
   231       */
       
   232     const QString& apiKey() const { return iApiKey; }
       
   233 
       
   234     /**
       
   235       * @return the session secret
       
   236       */
       
   237     const QString& sessionSecret() const { return iSessionSecret; }
       
   238 
       
   239     /**
       
   240       * @return the session key
       
   241       */
       
   242     const QString& sessionKey() const { return iSessionKey; }
       
   243     /**
       
   244          * Saves the fb connect session information to disk
       
   245          */
       
   246     void save();
       
   247 private:
       
   248 
       
   249    
       
   250     /**
       
   251       * Forgets any fb connect session information saved to disk
       
   252       */
       
   253     void unsave();
       
   254 
       
   255     void startFlushTimer();
       
   256 
       
   257     /**
       
   258       * @param aRequest, the request to add to the session queue, owner ship is transferred to the session
       
   259       */
       
   260     void enqueueRequest(FBRequest* aRequest);
       
   261 
       
   262     /**
       
   263       * @param aRequest, the request to perform.
       
   264       * @param aEnqueue, if true add to queue if cant perform the request right now
       
   265       */
       
   266     bool performRequest(FBRequest* aRequest, bool aEnqueue);
       
   267 
       
   268     /**
       
   269       * flush the queue but performingRequest on the requests in the queue, if cant perform the request, waits a while using
       
   270       * startFlushTimer() and then tries to continue ..
       
   271       */
       
   272     void flushRequestQueue();
       
   273 public:
       
   274     QString stroauth_nonce;
       
   275     QString stroauth_timestamp;
       
   276     QString stroauth_signature;
       
   277     QString stroauth_Token;
       
   278     QString stroauth_TokenSecret;
       
   279     QString stroauth_verifier;
       
   280 public: 
       
   281     void send_req (QString uri,FBRequest* aRequest);
       
   282 };
       
   283 
       
   284 #endif // FBSESSION_H