00001 #include "controllerinterface.h" 00002 00003 #include "unsupportedactionexception.h" 00004 #include "controllerfactory.h" 00005 00006 #include <domain/taskmanagerdata.h> 00007 #include <domain/user.h> 00008 00009 using namespace control; 00010 00011 ControllerInterface::~ControllerInterface() 00012 { 00013 } 00014 00015 void ControllerInterface::init(domain::TaskManagerData* data, 00016 ui::UiInterface* ui, ControllerFactory*) 00017 { 00018 this->data = data; 00019 this->ui = ui; 00020 } 00021 00022 void ControllerInterface::run() 00023 { 00024 if (checkUser()) 00025 execute(); 00026 } 00027 00028 void ControllerInterface::run(const domain::StorableData* data) 00029 { 00030 if (checkUser()) 00031 execute(data); 00032 } 00033 00034 void ControllerInterface::execute(const domain::StorableData*) 00035 { 00036 throw UnsupportedActionException(); 00037 } 00038 00039 bool ControllerInterface::checkUser() const 00040 { 00041 if (!userIsValid()) 00042 { 00043 QString msg; 00044 00045 if (!userHasLoggedIn()) 00046 { 00047 msg = "Cannot execute use case \"%1\" without being logged in"; 00048 msg = msg.arg(description()); 00049 } 00050 else 00051 { 00052 msg = "User %1 is not allowed to execute use case \"%2\""; 00053 msg = msg.arg(user()->name()).arg(description()); 00054 } 00055 00056 ui->showError(msg); 00057 00058 return false; 00059 } 00060 00061 return true; 00062 } 00063 00064 bool ControllerInterface::userHasLoggedIn() const 00065 { 00066 return ui->user() != 0; 00067 } 00068 00069 bool ControllerInterface::userIsAdmin() const 00070 { 00071 return userHasLoggedIn() && user()->isAdmin(); 00072 } 00073 00074 QString control::ControllerInterface::dataTypeString() const 00075 { 00076 return dataTypeToString(dataType()); 00077 } 00078 00079 QString control::ControllerInterface::actionTypeString() const 00080 { 00081 return actionTypeToString(actionType()); 00082 } 00083 00084 QString ControllerInterface::actionTypeToString(ActionType type) 00085 { 00086 switch (type) 00087 { 00088 case CreateAction: return "Create"; 00089 case RemoveAction: return "Remove"; 00090 case ModifyAction: return "Modify"; 00091 case LoadDataAction: return "Load"; 00092 default: return "Other"; 00093 } 00094 } 00095 00096 QString ControllerInterface::dataTypeToString(DataType type) 00097 { 00098 switch (type) 00099 { 00100 case FileData: return "&File"; 00101 case TaskData: return "&Task"; 00102 case ResourceData: return "&Resource"; 00103 case ProjectData: return "&Project"; 00104 case InvitationData: return "&Invitations"; 00105 case ReservationData: return "Reser&vation"; 00106 case UserData: return "&User"; 00107 default: return "&Other"; 00108 } 00109 } 00110 00111 bool ControllerInterface::userCanExecute() const 00112 { 00113 return true; 00114 } 00115 00116 const domain::User* control::ControllerInterface::user() const 00117 { 00118 Q_ASSERT(userHasLoggedIn()); 00119 return ui->user(); 00120 }