00001 #include "taskremover.h" 00002 00003 #include <controldata.h> 00004 #include <models/taskmodel.h> 00005 00006 #include <domain/taskmanagerdata.h> 00007 #include <domain/dependentdataexception.h> 00008 #include <domain/task.h> 00009 00010 #include <tools/invaliddataexception.h> 00011 #include <tools/functions.h> 00012 #include <tools/stringexception.h> 00013 00014 #include <QDebug> 00015 00016 using namespace control; 00017 00018 00019 ControllerInterface::ActionType TaskRemover::actionType() const 00020 { 00021 return RemoveAction; 00022 } 00023 00024 ControllerInterface::DataType TaskRemover::dataType() const 00025 { 00026 return TaskData; 00027 } 00028 00029 QString TaskRemover::description() const 00030 { 00031 return "Remove task"; 00032 } 00033 00034 bool TaskRemover::userIsValid() const 00035 { 00036 return userHasLoggedIn() && !userIsAdmin(); 00037 } 00038 00039 void TaskRemover::execute(const domain::StorableData* data) 00040 { 00041 const domain::Task* task = qobject_cast<const domain::Task*>(data); 00042 00043 if (task == 0) 00044 throw tools::InvalidDataException("Given data is not a task."); 00045 00046 int size = task->superTasks().size(); 00047 while (!task->superTasks().isEmpty()) 00048 { 00049 execute(task->superTasks().first()); 00050 int newSize = task->superTasks().size(); 00051 Q_ASSERT(newSize < size); 00052 size = newSize; 00053 } 00054 00055 this->data->removeData(task); 00056 } 00057 00058 void TaskRemover::execute() 00059 { 00060 QList<domain::Task*> tasksForUser = data->tasksForUser(user()); 00061 00062 TaskModel tasks(data, tools::makeConstList(tasksForUser)); 00063 data::SelectionData selectedTasks = ui->getSelection("Select a task", &tasks); 00064 00065 if (!selectedTasks.processData) 00066 return; 00067 00068 if (selectedTasks.selection.isEmpty()) 00069 { 00070 ui->showWarning("No task selected."); 00071 return; 00072 } 00073 00074 Q_ASSERT(selectedTasks.selection.count() == 1); 00075 00076 const domain::Task* task = 00077 tasks.taskForIndex(selectedTasks.selection.first()); 00078 00079 if (!task->superTasks().isEmpty()) 00080 { 00081 TaskModel superTasks(data, task->allSuperTasks()); 00082 QString before = "The selected task has the following depending tasks."; 00083 QString after = "Do you want to delete them all?"; 00084 00085 if (!ui->askQuestion(before, &superTasks, after)) 00086 { 00087 QList<domain::Task*> removedSuperTasks; 00088 00089 Q_FOREACH (const domain::Task* constSuperTask, task->superTasks()) 00090 { 00091 try 00092 { 00093 domain::Task* superTask = this->data->task(constSuperTask); 00094 superTask->removeSubTask(this->data->task(task)); 00095 removedSuperTasks << superTask; 00096 } 00097 catch (tools::StringException& e) 00098 { 00099 Q_FOREACH (domain::Task* superTask, removedSuperTasks) 00100 superTask->addSubTask(this->data->task(task)); 00101 00102 ui->showError(QString("Unable to remove link with one of the " 00103 "super tasks:\n%1").arg(e.message())); 00104 00105 return; 00106 } 00107 } 00108 } 00109 } 00110 00111 execute(task); 00112 } 00113 00114 QString TaskRemover::name() const 00115 { 00116 return "TaskRemover"; 00117 } 00118 00119 Q_EXPORT_PLUGIN2(controller_taskremover, TaskRemover) 00120