/*
 * 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 "everyone-person-set.h"

#include "pimo.h"

#include <KDebug>
#include <KMessageBox>

#include <Nepomuk/Resource>
#include <Nepomuk/Query/QueryServiceClient>
#include <Nepomuk/Query/ResourceTypeTerm>


class EveryonePersonSet::Private {

public:
    Private()
      : query(0)
    { }

    Nepomuk::Query::QueryServiceClient *query;

};

EveryonePersonSet::EveryonePersonSet(QObject* parent)
  : PersonSet(parent),
    d(new Private)
{
    kDebug();

    // FIXME: Get the Nepomuk Resource for myself in the standardised way, once it is standardised.
    Nepomuk::Resource me(QUrl::fromEncoded("nepomuk:/myself"));

    d->query = new Nepomuk::Query::QueryServiceClient(this);
    connect(d->query, SIGNAL(newEntries(QList<Nepomuk::Query::Result>)),
            this, SLOT(onNewEntries(QList<Nepomuk::Query::Result>)));
    connect(d->query, SIGNAL(entriesRemoved(QList<QUrl>)),
            this, SLOT(onEntriesRemoved(QList<QUrl>)));

    // Get all metacontacts
    // FIXME: Should we just get ones which can be contacted through telepathy?
    {
        using namespace Nepomuk::Query;

        Query query(ResourceTypeTerm(Nepomuk::Vocabulary::PIMO::Person()));

        bool queryResult = d->query->query(query);
        kDebug() << "Metacontact query result " << queryResult;

        if (!queryResult) {
            KMessageBox::error(0, i18n("It was not possible to query Nepomuk database. Please check your "
                                       "installation and make sure Nepomuk is running."));
        }
    }
}

EveryonePersonSet::~EveryonePersonSet()
{
    kDebug();

    delete d;
}

void EveryonePersonSet::onNewEntries(const QList<Nepomuk::Query::Result> &results)
{
    kDebug();

    // Create a new PersonPtr for each of the new people and add it to the set.
    Q_FOREACH (const Nepomuk::Query::Result &result, results) {
        kDebug() << "New result:" << result.resource().resourceUri();
        PersonPtr person = PersonPtr(new Person(result.resource()));
        if (person->isValid()) {
            addPerson(person);
        }
    }
}

void EveryonePersonSet::onEntriesRemoved(const QList<QUrl> &resources)
{
    kDebug();

    // Remove each person from the set.
    Q_FOREACH (const QUrl resource, resources) {
        kDebug() << "Removing resource:" << resource;
        removePerson(resource);
    }
}


#include "everyone-person-set.h"

