/*
    Copyright (C) 2013  Martin Klapetek <mklapetek@kde.org>
    Copyright (C) 2013  David Edmundson <davidedmundson@kde.org>

    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 "basepersonsdatasource.h"


#include "backends/abstractcontact.h"
#include "defaultcontactmonitor_p.h"

using namespace KPeople;

class KPeople::BasePersonsDataSourcePrivate
{
public:
    QWeakPointer<AllContactsMonitor> m_allContactsMonitor;
    QHash<QString, QWeakPointer<ContactMonitor> > m_contactMonitors;
};

BasePersonsDataSource::BasePersonsDataSource(QObject *parent, const QVariantList &args)
    : QObject(parent),
      d_ptr(new BasePersonsDataSourcePrivate)
{
    Q_UNUSED(args)
}

BasePersonsDataSource::~BasePersonsDataSource()
{
    delete d_ptr;
}

AllContactsMonitorPtr BasePersonsDataSource::allContactsMonitor()
{
    Q_D(BasePersonsDataSource);

    //if there is currently no watcher, create one
    AllContactsMonitorPtr c = d->m_allContactsMonitor.toStrongRef();
    if (!c) {
        c = AllContactsMonitorPtr(createAllContactsMonitor());
        d->m_allContactsMonitor = c;
    }

    return c;
}

ContactMonitorPtr BasePersonsDataSource::contactMonitor(const QString &contactUri)
{
    Q_D(BasePersonsDataSource);

    ContactMonitorPtr c = d->m_contactMonitors[contactUri].toStrongRef();
    if (!c) {
        c = ContactMonitorPtr(createContactMonitor(contactUri));
        d->m_contactMonitors[contactUri] = c;
    }
    return c;
}

ContactMonitor *BasePersonsDataSource::createContactMonitor(const QString &contactUri)
{
    return new DefaultContactMonitor(contactUri, allContactsMonitor());
}

BasePersonsDataSourceV2::BasePersonsDataSourceV2(QObject* parent, const QVariantList& args)
    : BasePersonsDataSource(parent, args)
{
}
