TheChess
|
00001 /* 00002 * thechess, chess game web application written in C++ and based on Wt 00003 * Copyright (C) 2010 Boris Nagaev 00004 * 00005 * thechess is licensed under the GNU GPL Version 2. 00006 * Other versions of the GPL do not apply. 00007 * See the LICENSE file for terms of use. 00008 */ 00009 00010 #ifndef THECHESS_WAPPLICATION_HPP_ 00011 #define THECHESS_WAPPLICATION_HPP_ 00012 00013 #include <vector> 00014 #include <map> 00015 #include <set> 00016 00017 namespace Wt { 00018 class WEnvironment; 00019 class WApplication; 00020 class WContainerWidget; 00021 class WImage; 00022 class WBorderLayout; 00023 class WWidget; 00024 } 00025 #include <Wt/WApplication> 00026 #include <Wt/WSignal> 00027 #include <Wt/Dbo/ptr> 00028 namespace dbo = Wt::Dbo; 00029 00030 #include "ThechessNotifier.hpp" 00031 #include "model/Object.hpp" 00032 #include "model/User.hpp" 00033 #include "model/Game.hpp" 00034 #include "model/Competition.hpp" 00035 #include "model/Object.hpp" 00036 #include "ThechessServer.hpp" 00037 #include "ThechessSession.hpp" 00038 #include "widgets/MainMenu.hpp" 00039 00040 #define tApp ((ThechessApplication*)wApp) 00041 00042 namespace thechess { 00043 00044 using model::UserPtr; 00045 using model::GamePtr; 00046 using model::CompetitionPtr; 00047 00048 class ThechessSession; 00049 00050 class Notifiable { 00051 public: 00052 Notifiable(const model::Object& object); 00053 Notifiable(model::ObjectType ot, int id); 00054 00055 virtual ~Notifiable(); 00056 virtual void notify()=0; // under Transaction 00057 00058 private: 00059 const model::Object object_; 00060 00061 void add_to_application_(); 00062 }; 00063 00064 class ThechessApplication : public Wt::WApplication { 00065 public: 00066 ThechessApplication(const Wt::WEnvironment& env, ThechessServer& server); 00067 virtual ~ThechessApplication(); 00068 00069 ThechessSession& session() { 00070 return session_; 00071 } 00072 ThechessServer& server() { 00073 return server_; 00074 } 00075 00076 UserPtr user() const { 00077 return user_; 00078 } 00079 void after_user_change_(); 00080 void set_user(UserPtr user); 00081 void logout(); 00082 00083 void view(UserPtr user); 00084 void view(GamePtr game); 00085 void view(CompetitionPtr competition); 00086 00087 template<typename M> void list_view() { 00088 } 00089 00090 static void thechess_notify(model::Object object); 00091 00092 protected: 00093 virtual void notify(const Wt::WEvent& e); 00094 00095 private: 00096 ThechessServer& server_; 00097 ThechessSession session_; 00098 UserPtr user_; 00099 Wt::WBorderLayout* layout_; 00100 typedef std::multimap<model::Object, Notifiable*> O2N; 00101 O2N notifiables_; 00102 bool active_; 00103 std::set<Notifiable*> waiting_notifiables_; 00104 const model::Object* notifying_object_; 00105 00106 void add_notifiable_(Notifiable* notifiable, 00107 const model::Object& object); 00108 void remove_notifiable_(Notifiable* notifiable, 00109 const model::Object& object); 00110 00111 void cookie_session_read_(); 00112 void cookie_session_write_(); 00113 00114 void add_my_games_(); 00115 void remove_my_games_(); 00116 00117 void onPathChange_(); 00118 Wt::WContainerWidget* mainpanel_(); 00119 void set_mainpanel_(Wt::WWidget* widget); 00120 00121 template<typename W> 00122 void show_(std::string path) { 00123 setInternalPath(path); 00124 set_mainpanel_(new W()); 00125 } 00126 00127 template<typename W, typename I> 00128 void show_(I item, std::string path) { 00129 setInternalPath(path); 00130 set_mainpanel_(new W(item)); 00131 } 00132 00133 template<typename M> 00134 void object_view_(const char* path) { 00135 std::string id_str = internalPathNextPart(path); 00136 if (id_str.empty()) { 00137 list_view<M>(); 00138 } else { 00139 int id = atoi(id_str.c_str()); 00140 if (id) { 00141 dbo::Transaction t(tApp->session()); 00142 dbo::ptr<M> o = tApp->session().find<M>() 00143 .where("id = ?").bind(id); 00144 if (o) { 00145 view(o); 00146 } 00147 t.commit(); 00148 } 00149 } 00150 } 00151 00152 friend class widgets::MainMenuImpl; 00153 friend class Notifiable; 00154 }; 00155 00156 template<> void ThechessApplication::list_view<model::Game>(); 00157 template<> void ThechessApplication::list_view<model::Competition>(); 00158 00159 } 00160 00161 #endif