00001 #include "invitationmanager.h" 00002 00003 #include <controldata.h> 00004 #include <domain/taskmanagerdata.h> 00005 #include <domain/invitation.h> 00006 #include <domain/user.h> 00007 #include <models/invitationmodel.h> 00008 #include <models/taskmodel.h> 00009 #include <models/usermodel.h> 00010 #include <tools/invaliddataexception.h> 00011 #include <tools/functions.h> 00012 00013 using namespace control; 00014 00015 ControllerInterface::ActionType InvitationManager::actionType() const 00016 { 00017 return ManageAction; 00018 } 00019 00020 ControllerInterface::DataType InvitationManager::dataType() const 00021 { 00022 return InvitationData; 00023 } 00024 00025 QString InvitationManager::description() const 00026 { 00027 return "Manage invitations"; 00028 } 00029 00030 QString InvitationManager::name() const 00031 { 00032 return "InvitationManager"; 00033 } 00034 00035 bool InvitationManager::userIsValid() const 00036 { 00037 return userHasLoggedIn() && !userIsAdmin(); 00038 } 00039 00040 void InvitationManager::execute() 00041 { 00042 QList<domain::Task*> tasksForUser = data->tasksForUser(user()); 00043 00044 if (tasksForUser.isEmpty()) 00045 { 00046 ui->showWarning(QString("User %1 does not have any tasks").arg(user()->name())); 00047 return; 00048 } 00049 00050 TaskModel tasks(data, tools::makeConstList(tasksForUser)); 00051 data::SelectionData selectedTasks = ui->getSelection( 00052 "Select a task for managing its invitations", &tasks); 00053 00054 if (!selectedTasks.processData) 00055 return; 00056 00057 if (selectedTasks.selection.isEmpty()) 00058 { 00059 ui->showWarning("No task selected."); 00060 return; 00061 } 00062 00063 Q_ASSERT(selectedTasks.selection.count() == 1); 00064 00065 domain::Task* task = 00066 data->task(tasks.taskForIndex(selectedTasks.selection.first())); 00067 00068 InvitationModel invitations(data, task); 00069 ui->showModel(&invitations, "This is a list of the invitations " 00070 "of the selected task"); 00071 00072 QStringList list; 00073 list << "Add" << "Remove"; 00074 data::StringInputData choice = 00075 ui->getChoice(list,"Do you want to add or remove an invitation?"); 00076 00077 if (!choice.processData) 00078 return; 00079 00080 if ((QString)choice == "Add") 00081 { 00082 UserModel users(data); 00083 data::SelectionData selectedUser = 00084 ui->getSelection("Select a user you want to invite", &users); 00085 00086 if (!selectedUser.processData) 00087 return; 00088 00089 if (selectedUser.selection.isEmpty()) 00090 { 00091 ui->showError("No user selected"); 00092 return; 00093 } 00094 00095 Q_ASSERT(selectedUser.selection.count() == 1); 00096 00097 try 00098 { 00099 domain::User* user = 00100 data->user(users.userForIndex(selectedUser.selection.first())); 00101 00102 data->createInvitation(task, user); 00103 ui->showMessage("Invitation successfully created"); 00104 return; 00105 } 00106 catch(tools::InvalidDataException e) 00107 { 00108 ui->showError(e.message()); 00109 execute(); 00110 return; 00111 } 00112 } 00113 else if ((QString) choice == "Remove") 00114 { 00115 if (task->invitations().isEmpty()) 00116 { 00117 ui->showWarning("No invitations to remove for the selected task"); 00118 return; 00119 } 00120 00121 data::SelectionData selectedInvitation = 00122 ui->getSelection("Select an invitation to remove", &invitations); 00123 00124 if (!selectedInvitation.processData) 00125 return; 00126 00127 if (selectedInvitation.selection.isEmpty()) 00128 { 00129 ui->showError("No invitation selected"); 00130 return; 00131 } 00132 00133 Q_ASSERT(selectedInvitation.selection.count() == 1); 00134 00135 const domain::Invitation* inv = invitations. 00136 invitationForIndex(selectedInvitation.selection.first()); 00137 00138 try 00139 { 00140 data->removeData(inv); 00141 ui->showMessage("Invitation successfully removed"); 00142 } 00143 catch (tools::StringException e) 00144 { 00145 ui->showError(e.message()); 00146 } 00147 00148 execute(); 00149 return; 00150 } 00151 else 00152 { 00153 qFatal("This is embarrasing"); 00154 } 00155 } 00156 00157 Q_EXPORT_PLUGIN2(controller_invitationmanager, InvitationManager)