00001 #include "projectremover.h"
00002
00003 #include <controldata.h>
00004 #include <controllerfactory.h>
00005 #include <models/projectmodel.h>
00006
00007 #include <domain/taskmanagerdata.h>
00008 #include <domain/project.h>
00009
00010 #include <tools/functions.h>
00011 #include <tools/invaliddataexception.h>
00012
00013 using namespace control;
00014
00015 QString ProjectRemover::description() const
00016 {
00017 return "Remove project";
00018 }
00019
00020 ControllerInterface::ActionType ProjectRemover::actionType() const
00021 {
00022 return RemoveAction;
00023 }
00024
00025 ControllerInterface::DataType ProjectRemover::dataType() const
00026 {
00027 return ProjectData;
00028 }
00029
00030 QString ProjectRemover::name() const
00031 {
00032 return "ProjectRemover";
00033 }
00034
00035 bool ProjectRemover::userIsValid() const
00036 {
00037 return userHasLoggedIn() && !userIsAdmin();
00038 }
00039
00040 void ProjectRemover::execute()
00041 {
00042 ProjectModel projects(data);
00043
00044 data::SelectionData selection = ui->getSelection("Select a project", &projects);
00045
00046 if (!selection.processData)
00047 return;
00048
00049 QModelIndexList list = selection.selection;
00050
00051 if (list.isEmpty())
00052 {
00053 ui->showWarning("No project selected.");
00054 return;
00055 }
00056
00057 Q_ASSERT(list.size() == 1);
00058
00059 const domain::Project* project = projects.projectForIndex(list.first());
00060
00061 try
00062 {
00063 execute(project);
00064 }
00065 catch (tools::StringException e)
00066 {
00067 ui->showError(e.message());
00068 }
00069 }
00070
00071 void ProjectRemover::execute(const domain::StorableData* data)
00072 {
00073 Q_ASSERT(taskRemover != 0);
00074
00075 const domain::Project* project = qobject_cast<const domain::Project*>(data);
00076
00077 if (project == 0)
00078 throw tools::InvalidDataException("Given data is not a project.");
00079
00080
00081 Q_FOREACH (const domain::Task* task, project->tasks())
00082 {
00083 Q_FOREACH (const domain::Task* superTask, task->superTasks())
00084 {
00085 this->data->task(superTask)->removeSubTask(this->data->task(task));
00086 }
00087
00088 taskRemover->run(task);
00089 }
00090
00091
00092 this->data->removeData(project);
00093 }
00094
00095 void ProjectRemover::init(domain::TaskManagerData* data, ui::UiInterface* ui,
00096 ControllerFactory* factory)
00097 {
00098 ControllerInterface::init(data, ui, factory);
00099
00100
00101 taskRemover = factory->controllerByType(TaskData, RemoveAction);
00102
00103
00104 if (taskRemover == 0)
00105 throw tools::StringException("No controller to remove a task.");
00106 }
00107
00108 Q_EXPORT_PLUGIN2(controller_projectremover, ProjectRemover)