/* 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 "kiobackendgio.h"
#include "giostorage.h"
#include "giohardwarestorage.h"
#include "gionetworkstorage.h"
#include "giostoragemonitor.h"

#include <kio/kstorage.h>
#include <kdebug.h>

KIOBackendGio * KIOBackendGio::instance()
{
    static KIOBackendGio * instance = 0;

    if (!instance)
    {
        instance = new KIOBackendGio();
    }
    return instance;
}


///////////////

KIOBackendGio::KIOBackendGio() : storageMonitor(0)
{
    g_type_init ();
}

KIOBackendGio::~KIOBackendGio()
{
    delete storageMonitor;
}

KStorageMonitor * KIOBackendGio::getStorageMonitorInstance()
{
    // initialize on demand
    if (!storageMonitor)
    {
        storageMonitor = new GioStorageMonitor();
    }

    return storageMonitor;
}

bool KIOBackendGio::validNetworkMount(GMount * gMount)
{
    GDrive * gDrive = g_mount_get_drive(gMount);
    if (gDrive)
    {
        g_object_unref(gDrive);
        return false;
    }

    GVolume * gVolume = g_mount_get_volume (gMount);
    if (gVolume)
    {
        g_object_unref(gVolume);
        return false;
    }

    char * uuid = g_mount_get_uuid(gMount);
    if (uuid)
    {
        g_free(uuid);
        return false;
    }

    return true;
}

QList<KStorage> KIOBackendGio::listStorageItems()
{
    QList<KStorage> list;

    GVolumeMonitor* gVolumeMonitor = g_volume_monitor_get();

    {
        GList* el = g_volume_monitor_get_volumes(gVolumeMonitor);

        while(el)
        {
            GVolume * gVolume = (GVolume *) el->data;

            GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
            list << KStorage(gioStorage);

            g_object_unref(gVolume);

            el = g_list_next(el);
        }
    }

    {
        GList* el = g_volume_monitor_get_mounts(gVolumeMonitor);

        while(el)
        {
            GMount * gMount = (GMount *) el->data;

            if (validNetworkMount(gMount))
            {
                GioNetworkStorage * gioStorage = new GioNetworkStorage(gMount);
                list << KStorage(gioStorage);
            }
            g_object_unref(gMount);

            el = g_list_next(el);
        }
    }


    return list;

}

KStorage findStorage(const QString& id)
{
    GVolumeMonitor* gVolumeMonitor = g_volume_monitor_get();

    {
        GList* el = g_volume_monitor_get_volumes(gVolumeMonitor);

        while(el)
        {
            GVolume * gVolume = (GVolume *) el->data;
            GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
            KStorage storage = KStorage(gioStorage);

            kDebug() << "storage: " << storage.id() << endl;
            if (storage.id() == id)
                return storage;

            g_object_unref(gVolume);
            el = g_list_next(el);
        }
    }

    {
        GList* el = g_volume_monitor_get_mounts(gVolumeMonitor);

        while(el)
        {
            GMount * gMount = (GMount *) el->data;
            if (KIOBackendGio::validNetworkMount(gMount))
            {
                GioNetworkStorage * gioStorage = new GioNetworkStorage(gMount);
                KStorage storage = KStorage(gioStorage);

                kDebug() << "storage: " << storage.id() << endl;
                if (storage.id() == id)
                    return storage;
            }
            g_object_unref(gMount);
            el = g_list_next(el);
        }
    }

    return KStorage();
}

KStorage KIOBackendGio::lookupStorage(const QString& id)
{

    GVolumeMonitor* gVolumeMonitor = g_volume_monitor_get();

    GVolume * gVolume = g_volume_monitor_get_volume_for_uuid
    (gVolumeMonitor,
            id.toLocal8Bit ().data());

    kDebug() << "lookup storage: " << id << " gVolume= " << (gVolume!=0) << endl;

    return findStorage(id);

    if (!gVolume) return KStorage();

    GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);

    return KStorage(gioStorage);
}

