1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
TaskListMerger.cpp
This file is part of Charm, a task-based time tracking application.
Copyright (C) 2008-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Mirko Boehm <mirko.boehm@kdab.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TaskListMerger.h"
#include "CharmExceptions.h"
TaskListMerger::TaskListMerger()
{
}
void TaskListMerger::setOldTasks(const TaskList &tasks)
{
verifyTaskList(tasks);
m_oldTasks = tasks;
for (TaskList::iterator it = m_oldTasks.begin(); it != m_oldTasks.end(); ++it)
(*it).setSubscribed(false);
qSort(m_oldTasks.begin(), m_oldTasks.end(), Task::lowerTaskId);
m_resultsValid = false;
}
void TaskListMerger::setNewTasks(const TaskList &tasks)
{
verifyTaskList(tasks);
m_newTasks = tasks;
for (TaskList::iterator it = m_newTasks.begin(); it != m_newTasks.end(); ++it)
(*it).setSubscribed(false);
qSort(m_newTasks.begin(), m_newTasks.end(), Task::lowerTaskId);
m_resultsValid = false;
}
void TaskListMerger::calculateResults() const
{
if (m_resultsValid) return;
// insert sentinels at end of list:
const TaskId maxId = qMax(
m_oldTasks.isEmpty() ? 0 : m_oldTasks.last().id(),
m_newTasks.isEmpty() ? 0 : m_newTasks.last().id());
const TaskId sentinelId = maxId + 1;
const Task sentinel(sentinelId, QObject::tr("Sentinel Task"));
TaskList oldTasks(m_oldTasks);
TaskList newTasks(m_newTasks);
oldTasks << sentinel;
newTasks << sentinel;
TaskList::iterator oldIt = oldTasks.begin();
TaskList::iterator newIt = newTasks.begin();
do {
if ((*oldIt).id() < (*newIt).id()) {
// there is a task in the old task list that is not an
// element of the new task list, ignore (the user added it
// manually)
++oldIt;
} else if ((*oldIt).id() == (*newIt).id()) {
//
if (*oldIt != *newIt) {
m_modifiedTasks << (*oldIt);
*oldIt = *newIt;
}
++oldIt;
++newIt;
} else {
// there are tasks in newtasks that are not in oldtasks,
// so they are new
m_addedTasks << *newIt;
++newIt;
}
} while (oldIt != oldTasks.end() || newIt != newTasks.end());
oldTasks.pop_back(); // remove sentinel
m_results = oldTasks + m_addedTasks;
// one last check: if tasks where modified through the new task
// lists, maybe local-only tasks have become orphans?
if (!Task::checkForUniqueTaskIds(m_results))
throw InvalidTaskListException(QObject::tr(
"the merged task list is invalid, it contains duplicate task ids"));
if (!Task::checkForTreeness(m_results))
throw InvalidTaskListException(QObject::tr(
"the merged tasks database is not a directed graph, this is seriously bad, go fix it"));
m_resultsValid = true;
}
void TaskListMerger::verifyTaskList(const TaskList &tasks)
{
if (!Task::checkForUniqueTaskIds(tasks))
throw InvalidTaskListException(QObject::tr("task list contains duplicate task ids"));
if (!Task::checkForTreeness(tasks))
throw InvalidTaskListException(QObject::tr(
"task list is not a directed graph, this is seriously bad, go fix it"));
}
TaskList TaskListMerger::addedTasks() const
{
calculateResults();
return m_addedTasks;
}
TaskList TaskListMerger::modifiedTasks() const
{
calculateResults();
return m_modifiedTasks;
}
TaskList TaskListMerger::mergedTaskList() const
{
calculateResults();
return m_results;
}
|