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

///// signal handlers /////

static void _monitor_volume_added (GVolumeMonitor *volume_monitor,
        GVolume         *gVolume,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotAdded(gVolume);

}

static void _monitor_volume_changed (GVolumeMonitor *volume_monitor,
        GVolume         *gVolume,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotChanged(gVolume);
}

static void _monitor_volume_removed (GVolumeMonitor *volume_monitor,
        GVolume         *gVolume,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotRemoved(gVolume);
}

///////////

static void _monitor_mount_added (GVolumeMonitor *volume_monitor,
        GMount         *gMount,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotAdded(gMount);

}

static void _monitor_mount_changed (GVolumeMonitor *volume_monitor,
        GMount         *gMount,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotChanged(gMount);
}

static void _monitor_mount_removed (GVolumeMonitor *volume_monitor,
        GMount         *gMount,
        gpointer        user_data)
{
    GioStorageMonitor * monitor = (GioStorageMonitor*) user_data;
    monitor->_slotRemoved(gMount);
}


///// GioStorageMonitor implementation /////

GioStorageMonitor::GioStorageMonitor() : gVolumeMonitor(0)
{
    gVolumeMonitor = g_volume_monitor_get();

    g_signal_connect(G_OBJECT(gVolumeMonitor), "volume-added", G_CALLBACK(_monitor_volume_added), this);
    g_signal_connect(G_OBJECT(gVolumeMonitor), "volume-changed", G_CALLBACK(_monitor_volume_changed), this);
    g_signal_connect(G_OBJECT(gVolumeMonitor), "volume-removed", G_CALLBACK(_monitor_volume_removed), this);

    g_signal_connect(G_OBJECT(gVolumeMonitor), "mount-added", G_CALLBACK(_monitor_mount_added), this);
    g_signal_connect(G_OBJECT(gVolumeMonitor), "mount-changed", G_CALLBACK(_monitor_mount_changed), this);
    g_signal_connect(G_OBJECT(gVolumeMonitor), "mount-removed", G_CALLBACK(_monitor_mount_removed), this);

}

void GioStorageMonitor::_slotAdded(GVolume *gVolume)
{
    GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
    emit added(KStorage(gioStorage));
}

void GioStorageMonitor::_slotChanged(GVolume *gVolume)
{
    GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
    emit changed(KStorage(gioStorage));
}

void GioStorageMonitor::_slotRemoved(GVolume *gVolume)
{
    GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
    emit removed(KStorage(gioStorage));
}

void GioStorageMonitor::_slotAdded(GMount *gMount)
{
    if (KIOBackendGio::validNetworkMount(gMount))
    {
        GioNetworkStorage * gioStorage = new GioNetworkStorage(gMount);
        emit added(KStorage(gioStorage));
    }
    else
    {
        // if this is a mount on a volume,
        // then the volume probably has been mounted
        GVolume * gVolume = g_mount_get_volume (gMount);
        if (gVolume)
        {
            GioHardwareStorage * gioStorage = new GioHardwareStorage(gVolume);
            emit changed(KStorage(gioStorage));

            g_object_unref(gVolume);
        }
    }
}

void GioStorageMonitor::_slotChanged(GMount *gMount)
{
    if (KIOBackendGio::validNetworkMount(gMount))
    {
        GioNetworkStorage * gioStorage = new GioNetworkStorage(gMount);
        emit changed(KStorage(gioStorage));
    }
}

void GioStorageMonitor::_slotRemoved(GMount *gMount)
{
    if (KIOBackendGio::validNetworkMount(gMount))
    {
        GioNetworkStorage * gioStorage = new GioNetworkStorage(gMount);
        emit removed(KStorage(gioStorage));
    }
}

