/*  This file is part of the KDE project
    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 version 2 as published by the Free Software Foundation.

    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 "gvfs-daemon-query.h"
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>

#include <QtCore/QVariant>
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QByteArray>

#include <kdebug.h>

/* The well known name of the main daemon */
#define G_VFS_DBUS_DAEMON_NAME "org.gtk.vfs.Daemon"

/* The mount tracking interface in the main daemon */
#define G_VFS_DBUS_MOUNTTRACKER_INTERFACE "org.gtk.vfs.MountTracker"
#define G_VFS_DBUS_MOUNTTRACKER_PATH "/org/gtk/vfs/mounttracker"
#define G_VFS_DBUS_MOUNTTRACKER_OP_LOOKUP_MOUNT "lookupMount"
#define G_VFS_DBUS_MOUNTTRACKER_OP_LOOKUP_MOUNT_BY_FUSE_PATH "lookupMountByFusePath"
#define G_VFS_DBUS_MOUNTTRACKER_OP_MOUNT_LOCATION "mountLocation"
#define G_VFS_DBUS_MOUNTTRACKER_OP_LIST_MOUNTS "listMounts"
#define G_VFS_DBUS_MOUNTTRACKER_OP_REGISTER_MOUNT "registerMount"
#define G_VFS_DBUS_MOUNTTRACKER_OP_UNREGISTER_MOUNT "unregisterMount"
#define G_VFS_DBUS_MOUNTTRACKER_OP_LIST_MOUNT_TYPES "listMountTypes"
#define G_VFS_DBUS_MOUNTTRACKER_OP_LIST_MOUNTABLE_INFO "listMountableInfo"
#define G_VFS_DBUS_MOUNTTRACKER_OP_REGISTER_FUSE "registerFuse"
#define G_VFS_DBUS_MOUNTTRACKER_SIGNAL_MOUNTED "mounted"
#define G_VFS_DBUS_MOUNTTRACKER_SIGNAL_UNMOUNTED "unmounted"



int main()
{

    QDBusConnection connection = QDBusConnection::sessionBus();
    QDBusInterface iface (G_VFS_DBUS_DAEMON_NAME,
            G_VFS_DBUS_MOUNTTRACKER_PATH,
            G_VFS_DBUS_MOUNTTRACKER_INTERFACE, connection);
    if (iface.isValid()) {
        QDBusMessage message = iface.call(
                G_VFS_DBUS_MOUNTTRACKER_OP_LIST_MOUNTS
        );

        kDebug() << "Reply message: " << message;

        const QDBusArgument argument = message.arguments ().first().value<QDBusArgument>();

        argument.beginArray ();

        while ( !argument.atEnd() ) {
            argument.beginStructure();

            QString dbus_id;
            QDBusObjectPath obj_path;
            QString display_name;
            QString stable_name;
            QString x_content_types;
            QString icon_str;
            QString prefered_filename_encoding;
            bool user_visible;
            QByteArray fuse_mountpoint;

            argument
            >> dbus_id
            >> obj_path
            >> display_name
            >> stable_name
            >> x_content_types
            >> icon_str
            >> prefered_filename_encoding
            >> user_visible
            >> fuse_mountpoint
            ;

            kDebug() << "dbus_id=" << dbus_id << " obj_path=" << obj_path.path() << " name=" << display_name;

            // read mountspec

            argument.beginStructure();

            QByteArray mount_prefix;
            argument >> mount_prefix;

            kDebug() << "mount_prefix=" << mount_prefix;

            argument.beginMap ();
            while ( !argument.atEnd() )
            {
                argument.beginMapEntry();
                QString key;
                QByteArray value;
                argument >> key >> value;
                kDebug() << "key=" << key << " value=" << value;
                argument.endMapEntry();
            }

            argument.endMap();
            argument.endStructure();

            argument.endStructure();

        }
        return 1;
    }

    return 1;
}
