/*
   Copyright (C) 2008 Norbert Frese <nf2 at scheinwelt at>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License (LGPL) 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 "giobridge-remotedirnotify.h"

#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kdesktopfile.h>
#include <kdirnotify.h>

#include <QtDBus/QtDBus>
#include <QDir>

#include <glib.h>
#include <gio/gio.h>


QString getUdiFromGMount(GMount * gMount)
{
    GFile * gFileRoot = g_mount_get_root(gMount);
    char * pointsToUri = g_file_get_uri(gFileRoot);
    QString qstrPointsToUri = QString(pointsToUri);
    
    g_object_unref(gFileRoot);
    g_free(pointsToUri);  
    
    if (qstrPointsToUri.startsWith("file://")) return QString();
    
    return qstrPointsToUri;
}


static void _monitor_mount_added (GVolumeMonitor *volume_monitor,
    GMount         *mount,
    gpointer        user_data)
{
    
    QString udi = getUdiFromGMount(mount);
    
    qDebug() << "*** GioBridgeRemoteDirNotify mount added udi=" << udi << " ***"<< endl;
    
    if (udi.isNull()) return;
  
    //GioBridgeRemoteDirNotify * dirNotifier = (GioBridgeRemoteDirNotify*) user_data;
    //dirNotifier->changed();
    
    KUrl remoteUrl("remote:/");
    org::kde::KDirNotify::emitFilesAdded( remoteUrl.url() );
}

static void _monitor_mount_changed (GVolumeMonitor *volume_monitor,
    GMount         *mount,
    gpointer        user_data)
{
    QString udi = getUdiFromGMount(mount);
  
    qDebug() << "*** GioBridgeRemoteDirNotify mount changed udi=" << udi << " ***" << endl;
  
    //GioBridgeRemoteDirNotify * dirNotifier = (GioBridgeRemoteDirNotify*) user_data;
    //dirNotifier->changed();
    
    KUrl remoteUrl("remote:/");
    org::kde::KDirNotify::emitFilesAdded( remoteUrl.url() ); // TODO: emitFilesChanged?
  
}

static void _monitor_mount_removed (GVolumeMonitor *volume_monitor,
    GMount         *mount,
    gpointer        user_data)
{
    QString udi = getUdiFromGMount(mount);
  
    qDebug() << "*** GioBridgeRemoteDirNotify mount removed udi=" << udi << " ***" << endl;
    
    if (udi.isNull()) return;
    
    //GioBridgeRemoteDirNotify * dirNotifier = (GioBridgeRemoteDirNotify*) user_data;
    //dirNotifier->changed();
    
    KUrl remoteUrl("remote:/");
    org::kde::KDirNotify::emitFilesAdded( remoteUrl.url() );  // TODO: emitFilesRemoved?
    
    // signal removal of the mounted URL
    GFile *rootGFile = g_mount_get_root(mount);
    char * rootURL = g_file_get_uri(rootGFile);
    
    KUrl mountRootUrl(rootURL);
    mountRootUrl.adjustPath(KUrl::RemoveTrailingSlash);
    if (mountRootUrl.path() == "/")
    {
        mountRootUrl.setPath(QString()); // remove path "/", because of KDirListerCache
    }
    
    qDebug() << "emitFilesRemoved: " << mountRootUrl.url();
    org::kde::KDirNotify::emitFilesRemoved( QStringList(mountRootUrl.url()) );
    g_free(rootURL);
}

GioBridgeRemoteDirNotify::GioBridgeRemoteDirNotify()
{
    kDebug() << "new instance of GioBridgeRemoteDirNotify";
    // init GType system
    g_type_init ();
    
    // get volume monitor
    gVolumeMonitor = g_volume_monitor_get();
  
    // register signals
    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);
    
}

#include "giobridge-remotedirnotify.moc"
