Wexus2 0.20
wexus2.src/wexus/Context.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_CONTEXT_H__
00009 #define __INCLUDED_WEXUS_CONTEXT_H__
00010 
00011 #include <QTextStream>
00012 
00013 #include <wexus/TR1.h>
00014 
00015 #include <wexus/HTTPRequest.h>
00016 #include <wexus/HTTPReply.h>
00017 #include <wexus/ParamsParser.h>
00018 #include <wexus/Cookies.h>
00019 #include <wexus/SessionManager.h>
00020 
00021 #include <QStringList>
00022 
00023 namespace wexus
00024 {
00025   class Context;
00026 
00027   class Application;  // forward
00028 }
00029 
00030 /**
00031  * The context object sets up and maintains (during its lifetime)
00032  * various thread-local storage variables necesary for the other
00033  * global functions defined in this file.
00034  *
00035  * Does header/footer sending too, over the HTTPReply
00036  *
00037  * @author Aleksander Demko
00038  */ 
00039 class wexus::Context
00040 {
00041   public:
00042     /// constructor
00043     Context(wexus::Application *application, wexus::HTTPRequest &req, wexus::HTTPReply &reply);
00044     /// destructor
00045     ~Context();
00046 
00047     /**
00048      * Returns the "static-like" instance for this thread.
00049      *
00050      * @author Aleksander Demko
00051      */ 
00052     static Context *threadInstance(void);
00053 
00054     /**
00055      * The application instance that spawned this context.
00056      *
00057      * @author Aleksander Demko
00058      */ 
00059     static wexus::Application * application(void) { return threadInstance()->dm_application; }
00060     /**
00061      * The wexus::HTTPRequest object that started this request.
00062      *
00063      * @author Aleksander Demko
00064      */ 
00065     static wexus::HTTPRequest & request(void) { return threadInstance()->dm_req; }
00066     /**
00067      * The wexus::HTTPReply object that will be used to reply
00068      * to the user.
00069      *
00070      * @author Aleksander Demko
00071      */ 
00072     static wexus::HTTPReply & reply(void) { return threadInstance()->dm_reply; }
00073 
00074     /**
00075      * Returns the raw output stream.
00076      *
00077      * @author Aleksander Demko
00078      */ 
00079     static QTextStream & output(void);
00080 
00081     /**
00082      * Returns a stream that HTML escapes all output
00083      * (except HTMLString).
00084      *
00085      * @author Aleksander Demko
00086      */ 
00087     static QTextStream & htmlOutput(void);
00088 
00089   private:
00090     wexus::Application *dm_application;
00091     wexus::HTTPRequest & dm_req;
00092     wexus::HTTPReply & dm_reply;
00093 
00094     std::shared_ptr<QIODevice> dm_htmldevice;
00095     std::shared_ptr<QTextStream> dm_htmloutput;
00096 
00097     bool dm_sentHeader;   // was the common header sent already
00098 
00099   public:
00100     // public things
00101     // cant make these nice-static because they arent methods
00102     // (and I dont really want to convert them to getter-like methods... yet)
00103     // placed her to maintain a proper initialization order
00104 
00105     /**
00106      * The form parameters.
00107      * Object directly accessible so operator[] works.
00108      *
00109      * @author Aleksander Demko
00110      */ 
00111     QVariantMap params;
00112 
00113     /**
00114      * The cookies.
00115      *
00116      * @author Aleksander Demko
00117      */ 
00118     Cookies cookies;
00119 
00120   private:
00121     // this has to be constructed AFTER cookies but before session
00122     SessionManager::Locker dm_sessionlocker;
00123 
00124   public:
00125 
00126     /**
00127      * The session, if any
00128      *
00129      * @author Aleksander Demko
00130      */ 
00131     QVariantMap &session;
00132 
00133     /**
00134      * The incoming flash sent by the previous calls.
00135      *
00136      * @author Aleksander Demko
00137      */ 
00138     const QVariantMap flash;
00139 
00140     /**
00141      * The flash that will be preseved for the next calls.
00142      * In the next call, this will be the contents of inFlash.
00143      *
00144      * @author Aleksander Demko
00145      */ 
00146     QVariantMap setFlash;
00147 
00148     typedef QMap<QString, QStringList> Errors;
00149 
00150     /**
00151      * Errors.
00152      *
00153      * @author Aleksander Demko
00154      */ 
00155     Errors errors;
00156 
00157   private:
00158     void sendHeader(void);
00159     void sendFooter(void);
00160 };
00161 
00162 #endif
00163 
 All Classes Namespaces Functions Variables Enumerations Enumerator