/*
 * This file is part of people
 *
 * Copyright (C) 2010 Collabora Ltd. <info@collabora.co.uk>
 *   @author George Goldberg <george.goldberg@collabora.co.uk>
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "person.h"

#include "pimo.h"

#include <KDebug>

#include <Nepomuk/Thing>

#include <QtCore/QMultiHash>

class Person::Private {

public:
    Private()
    { }

    Nepomuk::Thing pimoPerson;

    static QMultiHash<Nepomuk::Resource, QWeakPointer<AbstractPersonInterface> > interfaceCache;

};

QMultiHash<Nepomuk::Resource, QWeakPointer<AbstractPersonInterface> > Person::Private::interfaceCache;

Person::Person(const Nepomuk::Resource &pimoPerson, QObject *parent)
  : Entity(pimoPerson, parent),
    d(new Private)
{
    kDebug();

    // Check the resource we have been passed is a valid PIMO:Person.
    if (pimoPerson.hasType(Nepomuk::Vocabulary::PIMO::Person())) {
        // Wrong type. Invalid.
        kDebug() << "We have been passed a valid PIMO:Person";
        setValid(true);
        d->pimoPerson = pimoPerson;
    } else {
        kWarning() << "Person object requires a valid PIMO:Person";
    }
}

Person::~Person()
{
    kDebug();

    delete d;
}

QList<QWeakPointer<AbstractPersonInterface> > Person::cachedInterfaces() const
{
    kDebug();

    return d->interfaceCache.values(resource());
}

void Person::cacheInterface(QSharedPointer<AbstractPersonInterface> interface)
{
    // FIXME: Should we garbage collect the interface cache here?
    d->interfaceCache.insertMulti(resource(), interface.toWeakRef());
}

uint qHash(Person key) {
    return qHash(key.resource());
}

uint qHash(PersonPtr key) {
    return qHash(key->resource());
}


#include "person.moc"

