/* This file is part of the KDE libraries
    Copyright (C) 2009 Norbert Frese <nf2.email@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 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
    Library General Public License for more details.

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

#include "kstorage.h"
#include "kstorageiface.h"
#include "kiobackendgio.h"

#include <QtCore/QSharedData>
#include <QtCore/QPointer>


class KStoragePrivate : public QSharedData
{
public:
    KStoragePrivate(KStorageIface * backendObject)
    : m_backendObject(backendObject) {}

    ~KStoragePrivate()
    {
        delete m_backendObject;
    }

    KStorageIface * m_backendObject;

};


QList<KStorage> KStorage::list()
{
    return KIOBackendGio::instance()->listStorageItems();
}

KStorage KStorage::lookup(const QString& id)
{
    return KIOBackendGio::instance()->lookupStorage(id);
}

KStorage::KStorage()
{
    d = new KStoragePrivate(0);
}


KStorage::KStorage(KStorageIface * backendObject)
{
    d = new KStoragePrivate(0);
    d->m_backendObject = backendObject;
}

KStorage::KStorage(const KStorage &storage)
: d(storage.d)
{
}

KStorage::~KStorage()
{
}

KStorage& KStorage::operator=(const KStorage &storage)
{
    d = storage.d;
    return *this;
}

bool KStorage::operator==(const KStorage &other) const
{
    return id() == other.id();
}

bool KStorage::isNull() const
{
    return d->m_backendObject == 0;
}

bool KStorage::isValid() const
{
    if (isNull()) return false;
    return d->m_backendObject->isValid();
}

QString KStorage::id() const
{
    if (isNull()) return QString();
    return d->m_backendObject->id();
}

QString KStorage::displayName() const
{
    if (isNull()) return QString();
    return d->m_backendObject->displayName();
}

QString KStorage::icon() const
{
    if (isNull()) return QString();
    return d->m_backendObject->icon();
}

QStringList KStorage::emblems() const
{
    if (isNull()) return QStringList();
    return d->m_backendObject->emblems();
}


QUrl KStorage::url() const
{
    if (isNull()) return QUrl();
    return d->m_backendObject->url();
}

QString KStorage::mountPointPath() const
{
    if (isNull()) return QString();
    return d->m_backendObject->mountPointPath();
}


bool KStorage::isRemovable() const
{
    if (isNull()) return false;
    return d->m_backendObject->isRemovable();
}

bool KStorage::isReleasable() const
{
    if (isNull()) return false;
    return d->m_backendObject->isReleasable();
}

bool KStorage::isHotpluggable() const
{
    if (isNull()) return false;
    return d->m_backendObject->isHotpluggable();
}

bool KStorage::isAccessible() const
{
    if (isNull()) return false;
    return d->m_backendObject->isAccessible();
}

void KStorage::setup(QObject * doneSignalTarget, const char * doneQSlot)
{
    if (isNull()) return;
    d->m_backendObject->setup(doneSignalTarget, doneQSlot);
}

void KStorage::teardown(QObject * doneSignalTarget, const char * doneQSlot)
{
    if (isNull()) return;
    d->m_backendObject->tearDown(doneSignalTarget, doneQSlot);
}

void KStorage::eject(QObject * doneSignalTarget, const char * doneQSlot)
{
    if (isNull()) return;
    d->m_backendObject->eject(doneSignalTarget, doneQSlot);
}

void KStorage::connectChangedSignalTo(const QObject * obj, const char * slot)
{
    if (isNull()) return;
    QObject::connect(d->m_backendObject, SIGNAL(changed()), obj, slot);
}

void KStorage::connectRemovedSignalTo(const QObject * obj, const char * slot)
{
    if (isNull()) return;
    QObject::connect(d->m_backendObject, SIGNAL(removed()), obj, slot);
}

uint qHash(const KStorage &storage)
{
    return qHash(storage.id());
}
