Wexus2 0.20
wexus2.src/wexus/Application.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_APPLICATION_H__
00009 #define __INCLUDED_WEXUS_APPLICATION_H__
00010 
00011 #include <wexus/Controller.h>
00012 #include <wexus/HTTPRequest.h>
00013 #include <wexus/HTTPReply.h>
00014 #include <wexus/Registry.h>
00015 #include <wexus/OpenDatabases.h>
00016 
00017 #include <QSqlDatabase>
00018 
00019 namespace wexus
00020 {
00021   class Application;
00022 }
00023 
00024 /**
00025  * Controllers are groupped into applications. Applications
00026  * are the installed, at a certain mount point (prefix)
00027  * into a Site.
00028  *
00029  * Each user application decends from this class,
00030  * registers the application subclass via.
00031  * The application then registers controllers in 
00032  * its constructor via registerController.
00033  *
00034  * @author Aleksander Demko
00035  */ 
00036 class wexus::Application
00037 {
00038   public:
00039     /// destructor
00040     virtual ~Application();
00041 
00042     /**
00043      * Called by wexus::Site during addApplication
00044      *
00045      * Decendants may overide this to add additional functionality.
00046      *
00047      * @author Aleksander Demko
00048      */
00049     virtual void setMountPoint(const QString &mountPoint);
00050 
00051     /**
00052      * Returns the mount point.
00053      * Mount points always end in /
00054      *
00055      * @author Aleksander Demko
00056      */
00057     const QString &mountPoint(void) const { return dm_mountpoint; }
00058 
00059     /**
00060      * Called shortly after creation. This saves a copy
00061      * of the settings reference (acceisble via settings())
00062      * and also opens the database.
00063      *
00064      * Decendants may overide this to add additional functionality.
00065      * They should call this version, first, however.
00066      *
00067      * @author Aleksander Demko
00068      */
00069     virtual void init(const QVariantMap &settings);
00070 
00071     /**
00072      * This minimalist version of init() can be called by decendant's init functions
00073      * if instead of Application::init(). This version simply sets
00074      * the settings variable and does not perform any other
00075      * functions, like database initialization.
00076      *
00077      * @author Aleksander Demko
00078      */ 
00079     void initBasic(const QVariantMap &settings);
00080 
00081     /**
00082      * Returns the current settings
00083      *
00084      * Some interesting fields, assigned at boot:
00085      *   app= the appname of the started class
00086      *   mountpoint= the url mount point, same as mountPoint()
00087      *   sitedir= the directory (on disk) of the site
00088      *   appdir= the directory (on disk) of the app within the site
00089      *
00090      * @author Aleksander Demko
00091      */ 
00092     const QVariantMap & settings(void) const { return dm_settings; }
00093 
00094     /**
00095      * A enhanced handleApplicationRequest() call from the wexus::Site to wexus::Application.
00096      * This implements the controll-handler system.
00097      * This method is not typically overriden, but can be if you want to layer over the
00098      * default controller-handler system.
00099      *
00100      * filteredRequest contains just the action call. It always starts with atleast a /
00101      *
00102      * @author Aleksander Demko
00103      */ 
00104     virtual void handleApplicationRequest(QString &filteredRequest, wexus::HTTPRequest &req, wexus::HTTPReply &reply);
00105 
00106     SessionManager & sessionManager(void) { return dm_sessionmanager; }
00107 
00108     QSqlDatabase & database(void) { return dm_db.database(); }
00109 
00110     std::shared_ptr<Registry::AppInfo> appInfo(void);
00111 
00112   protected:
00113     /**
00114      * Decendants can instantite this in their constructors
00115      * to build a routing table.
00116      *
00117      * Route will be matched in order, with the first ones
00118      * given first chance/priority.
00119      *
00120      * @author Aleksander Demko
00121      */ 
00122     class RouteBuilder
00123     {
00124       public:
00125         /**
00126          * Start building a new route for the given app.
00127          * Any previous routing table will be destroyed.
00128          *
00129          * @author Aleksander Demko
00130          */ 
00131         RouteBuilder(Application &app);
00132 
00133         /// destructor
00134         ~RouteBuilder();
00135 
00136         /**
00137          * Adds a basic match route.
00138          *
00139          * It typically looks like /blah/:var/?:id
00140          * Where blah would be matched directly, :var will be assigned to
00141          * a variable and ? is optional.
00142          *
00143          * @param matchString the match string
00144          * @param defaults the default values for any params. This will be
00145          * overriden with matched vars, ofcourse.
00146          *
00147          * @author Aleksander Demko
00148          */ 
00149         void addMatch(const QString &matchString, const QVariantMap &defaults = QVariantMap());
00150 
00151         /**
00152          * Adds a bunch of default routes.
00153          *
00154          * @author Aleksander Demko
00155          */
00156         void addStandardRoutes(void);
00157 
00158       protected:
00159         Application &dm_app;
00160     };
00161 
00162     /// inherited constructor
00163     Application(void);
00164 
00165     /// called by initSettings to open (or reopen) the db
00166     void openDB(void);
00167 
00168   private:
00169     // all members here are RO (readonly), unless otherwise noted
00170     QString dm_mountpoint;
00171     QVariantMap dm_settings;
00172 
00173     SessionManager dm_sessionmanager;
00174     OpenDatabases::Handle dm_db;
00175 
00176     std::shared_ptr<Registry::AppInfo> dm_appinfo;
00177 
00178     class Route;
00179     class MatchingRoute;
00180     typedef QList<std::shared_ptr<Route> > RouteList;
00181 
00182     RouteList dm_routes;
00183 };
00184 
00185 #endif
00186 
 All Classes Namespaces Functions Variables Enumerations Enumerator