<commit_before>/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) 2012  <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "categoriesstrategy.h"
#include <categorymanager.h>
#include <todohelpers.h>
#include <todonode.h>
#include <KIcon>
#include <KUrl>
#include <KLocalizedString>
#include <QMimeData>

#include "reparentingmodel.h"

CategoriesStrategy::CategoriesStrategy()
:   ReparentingStrategy(),
    mInbox(1),
    mRoot(2),
    mRelations(new CategoriesStructure())
{
    CategoryManager::instance().setCategoriesStructure(static_cast<CategoriesStructure*>(mRelations.data()));
    mReparentOnRemoval = true;
    connect(mRelations.data(), SIGNAL(virtualNodeAdded(Id, IdList, QString)), this, SLOT(createVirtualNode(Id, IdList, QString)));
    connect(mRelations.data(), SIGNAL(nodeRemoved(Id)), this, SLOT(doRemoveNode(Id)));
    connect(mRelations.data(), SIGNAL(parentsChanged(Id,IdList)), this, SLOT(doChangeParents(Id, IdList)));
    connect(mRelations.data(), SIGNAL(virtualNodeRenamed(Id,QString)), this, SLOT(doRenameParent(Id, QString)));
    connect(mRelations.data(), SIGNAL(updateItems(IdList)), this, SLOT(doUpdateItems(IdList)));
}

void CategoriesStrategy::init()
{
    ReparentingStrategy::init();
    TodoNode *node = createNode(mInbox, IdList(), "No Context");
    node->setData(i18n("No Context"), 0, Qt::DisplayRole);
    node->setData(KIcon("mail-folder-inbox"), 0, Qt::DecorationRole);
    node->setRowData(Zanshin::Inbox, Zanshin::ItemTypeRole);

    TodoNode *node2 = createNode(mRoot, IdList(), "Contexts");
    node2->setData(i18n("Contexts"), 0, Qt::DisplayRole);
    node2->setData(KIcon("document-multiple"), 0, Qt::DecorationRole);
    node2->setRowData(Zanshin::CategoryRoot, Zanshin::ItemTypeRole);
}

Id translateFrom(Id id)
{
    //TODO proper id mapping
    return id+10;
}

Id translateTo(Id id)
{
    //TODO proper id mapping
    return id-10;
}

IdList translateFrom(IdList l)
{
    IdList list;
    foreach (Id id, l) {
        list << translateFrom(id);
    }
    return list;
}

Id CategoriesStrategy::getId(const QModelIndex &sourceChildIndex)
{
    Zanshin::ItemType type = (Zanshin::ItemType) sourceChildIndex.data(Zanshin::ItemTypeRole).toInt();
    if (type!=Zanshin::StandardTodo /*&& type!=Zanshin::ProjectTodo*/) { //Filter all other items
        return -1;
    }
    return translateFrom(mRelations->addItem(sourceChildIndex.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>()));
}

IdList CategoriesStrategy::getParents(const QModelIndex &sourceChildIndex, const IdList& ignore)
{
    Q_ASSERT(sourceChildIndex.data(Zanshin::ItemTypeRole).toInt()==Zanshin::StandardTodo /*|| sourceChildIndex.data(Zanshin::ItemTypeRole).toInt()==Zanshin::ProjectTodo*/);

    IdList parents = translateFrom(mRelations->getParents(mRelations->getItemId(sourceChildIndex.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>())));

    foreach(Id i, ignore) {
        parents.removeAll(i);
    }
    if (parents.isEmpty()) {
        return IdList() << mInbox;
    }
    return parents;
}

void CategoriesStrategy::createVirtualNode(Id id, IdList parents, const QString& name)
{
    IdList p = translateFrom(parents);
    if (p.isEmpty()) {
        p << mRoot;
    }
    createNode(translateFrom(id), p, name);
}

void CategoriesStrategy::doRemoveNode(Id id)
{
    kDebug() << id;
    ReparentingStrategy::removeNode(translateFrom(id));
}

void CategoriesStrategy::doChangeParents(Id id, IdList parents)
{
    ReparentingStrategy::updateParents(translateFrom(id), translateFrom(parents));
}

void CategoriesStrategy::doRenameParent(Id id, const QString& name)
{
    ReparentingStrategy::renameNode(translateFrom(id), name);
}

void CategoriesStrategy::doUpdateItems(const IdList &itemsToUpdate)
{
    kDebug() << itemsToUpdate;
    foreach (Id id , itemsToUpdate) {        
        Akonadi::Item item = getData(translateFrom(id), Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
        if (!item.isValid()) {
            kWarning() << "could not find item " << id;
            continue;
        }
        Q_ASSERT(item.isValid());
        mRelations->updateRelationTree(item);
        //TODO store item
    }
}


void CategoriesStrategy::setData(TodoNode* node, Id id)
{
    kDebug() << id;
    if (id == mInbox || id == mRoot) {
        return;
    }

    const QString &categoryName = mRelations->getName(translateTo(id));
    node->setData(categoryName, 0, Qt::DisplayRole);
    node->setData(categoryName, 0, Qt::EditRole);<commit_msg>Store relations<commit_after>/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) 2012  <NAME> <<EMAIL>_<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "categoriesstrategy.h"
#include <categorymanager.h>
#include <todohelpers.h>
#include <todonode.h>
#include <KIcon>
#include <KUrl>
#include <KLocalizedString>
#include <QMimeData>

#include "reparentingmodel.h"
#include <Akonadi/ItemModifyJob>

CategoriesStrategy::CategoriesStrategy()
:   ReparentingStrategy(),
    mInbox(1),
    mRoot(2),
    mRelations(new CategoriesStructure())
{
    CategoryManager::instance().setCategoriesStructure(static_cast<CategoriesStructure*>(mRelations.data()));
    mReparentOnRemoval = true;
    connect(mRelations.data(), SIGNAL(virtualNodeAdded(Id, IdList, QString)), this, SLOT(createVirtualNode(Id, IdList, QString)));
    connect(mRelations.data(), SIGNAL(nodeRemoved(Id)), this, SLOT(doRemoveNode(Id)));
    connect(mRelations.data(), SIGNAL(parentsChanged(Id,IdList)), this, SLOT(doChangeParents(Id, IdList)));
    connect(mRelations.data(), SIGNAL(virtualNodeRenamed(Id,QString)), this, SLOT(doRenameParent(Id, QString)));
    connect(mRelations.data(), SIGNAL(updateItems(IdList)), this, SLOT(doUpdateItems(IdList)));
}

void CategoriesStrategy::init()
{
    ReparentingStrategy::init();
    TodoNode *node = createNode(mInbox, IdList(), "No Context");
    node->setData(i18n("No Context"), 0, Qt::DisplayRole);
    node->setData(KIcon("mail-folder-inbox"), 0, Qt::DecorationRole);
    node->setRowData(Zanshin::Inbox, Zanshin::ItemTypeRole);

    TodoNode *node2 = createNode(mRoot, IdList(), "Contexts");
    node2->setData(i18n("Contexts"), 0, Qt::DisplayRole);
    node2->setData(KIcon("document-multiple"), 0, Qt::DecorationRole);
    node2->setRowData(Zanshin::CategoryRoot, Zanshin::ItemTypeRole);
}

Id translateFrom(Id id)
{
    //TODO proper id mapping
    return id+10;
}

Id translateTo(Id id)
{
    //TODO proper id mapping
    return id-10;
}

IdList translateFrom(IdList l)
{
    IdList list;
    foreach (Id id, l) {
        list << translateFrom(id);
    }
    return list;
}

Id CategoriesStrategy::getId(const QModelIndex &sourceChildIndex)
{
    Zanshin::ItemType type = (Zanshin::ItemType) sourceChildIndex.data(Zanshin::ItemTypeRole).toInt();
    if (type!=Zanshin::StandardTodo /*&& type!=Zanshin::ProjectTodo*/) { //Filter all other items
        return -1;
    }
    return translateFrom(mRelations->addItem(sourceChildIndex.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>()));
}

IdList CategoriesStrategy::getParents(const QModelIndex &sourceChildIndex, const IdList& ignore)
{
    Q_ASSERT(sourceChildIndex.data(Zanshin::ItemTypeRole).toInt()==Zanshin::StandardTodo /*|| sourceChildIndex.data(Zanshin::ItemTypeRole).toInt()==Zanshin::ProjectTodo*/);

    IdList parents = translateFrom(mRelations->getParents(mRelations->getItemId(sourceChildIndex.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>())));

    foreach(Id i, ignore) {
        parents.removeAll(i);
    }
    if (parents.isEmpty()) {
        return IdList() << mInbox;
    }
    return parents;
}

void CategoriesStrategy::createVirtualNode(Id id, IdList parents, const QString& name)
{
    IdList p = translateFrom(parents);
    if (p.isEmpty()) {
        p << mRoot;
    }
    createNode(translateFrom(id), p, name);
}

void CategoriesStrategy::doRemoveNode(Id id)
{
    kDebug() << id;
    ReparentingStrategy::removeNode(translateFrom(id));
}

void CategoriesStrategy::doChangeParents(Id id, IdList parents)
{
    ReparentingStrategy::updateParents(translateFrom(id), translateFrom(parents));
}

void CategoriesStrategy::doRenameParent(Id id, const QString& name)
{
    ReparentingStrategy::renameNode(translateFrom(id), name);
}

void CategoriesStrategy::doUpdateItems(const IdList &itemsToUpdate)
{
    kDebug() << itemsToUpdate;
    foreach (Id id , itemsToUpdate) {        
        Akonadi::Item item = getData(translateFrom(id), Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
        if (!item.isValid()) {
            kWarning() << "could not find item " << id;
            continue;
        }
        Q_ASSERT(item.isValid());
        mRelations->updateRelationTree(item);
        new Akonadi::ItemModifyJob(item, this);
    }
}


void CategoriesStrategy::setData(TodoNode* node, Id id)
{
    kDebug() << id;
    if (id == mInbox || id == mRoot) {
        return;
    }

    const QString &categoryName = mRelations->getName(translateTo(id));
    node->setData(categoryName, 0, Qt::DisplayRole);
    node->setData(categoryName, 0, Qt::EditRole);
{'fraction_non_alphanumeric': 0.13227961238122118, 'fraction_numerical': 0.007150249317903848, 'mean_word_length': 4.6094986807387865, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2391364', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3290, 'n_words': 768}