Blame src/nautilus-dbus-manager.c

Packit a189e0
/*
Packit a189e0
 * nautilus-dbus-manager: nautilus DBus interface
Packit a189e0
 *
Packit a189e0
 * Copyright (C) 2010, Red Hat, Inc.
Packit a189e0
 *
Packit a189e0
 * Nautilus is free software; you can redistribute it and/or
Packit a189e0
 * modify it under the terms of the GNU General Public License as
Packit a189e0
 * published by the Free Software Foundation; either version 2 of the
Packit a189e0
 * License, or (at your option) any later version.
Packit a189e0
 *
Packit a189e0
 * Nautilus is distributed in the hope that it will be useful,
Packit a189e0
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a189e0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a189e0
 * General Public License for more details.
Packit a189e0
 *
Packit a189e0
 * You should have received a copy of the GNU General Public License
Packit a189e0
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit a189e0
 *
Packit a189e0
 * Author: Cosimo Cecchi <cosimoc@redhat.com>
Packit a189e0
 *
Packit a189e0
 */
Packit a189e0
Packit a189e0
#include <config.h>
Packit a189e0
Packit a189e0
#include "nautilus-dbus-manager.h"
Packit a189e0
#include "nautilus-generated.h"
Packit a189e0
Packit a189e0
#include "nautilus-file-operations.h"
Packit Service 0a2193
#include "nautilus-file-undo-manager.h"
Packit Service 10d738
#include "nautilus-file.h"
Packit a189e0
Packit a189e0
#define DEBUG_FLAG NAUTILUS_DEBUG_DBUS
Packit a189e0
#include "nautilus-debug.h"
Packit a189e0
Packit a189e0
#include <gio/gio.h>
Packit a189e0
Packit a189e0
struct _NautilusDBusManager
Packit a189e0
{
Packit a189e0
    GObject parent;
Packit a189e0
Packit a189e0
    NautilusDBusFileOperations *file_operations;
Packit a189e0
};
Packit a189e0
Packit a189e0
struct _NautilusDBusManagerClass
Packit a189e0
{
Packit a189e0
    GObjectClass parent_class;
Packit a189e0
};
Packit a189e0
Packit a189e0
G_DEFINE_TYPE (NautilusDBusManager, nautilus_dbus_manager, G_TYPE_OBJECT);
Packit a189e0
Packit a189e0
static void
Packit a189e0
nautilus_dbus_manager_dispose (GObject *object)
Packit a189e0
{
Packit a189e0
    NautilusDBusManager *self = (NautilusDBusManager *) object;
Packit a189e0
Packit a189e0
    if (self->file_operations)
Packit a189e0
    {
Packit a189e0
        g_object_unref (self->file_operations);
Packit a189e0
        self->file_operations = NULL;
Packit a189e0
    }
Packit a189e0
Packit a189e0
    G_OBJECT_CLASS (nautilus_dbus_manager_parent_class)->dispose (object);
Packit a189e0
}
Packit a189e0
Packit Service be7f18
static void
Packit Service be7f18
undo_redo_on_finished (gpointer user_data)
Packit Service be7f18
{
Packit Service 345a52
    NautilusFileUndoManager *undo_manager = NULL;
Packit Service be7f18
    int *handler_id = (int *) user_data;
Packit Service be7f18
Packit Service be7f18
    undo_manager = nautilus_file_undo_manager_get ();
Packit Service be7f18
    g_signal_handler_disconnect (undo_manager, *handler_id);
Packit Service be7f18
    g_application_release (g_application_get_default ());
Packit Service be7f18
    g_free (handler_id);
Packit Service be7f18
}
Packit Service be7f18
Packit 6dc997
static gboolean
Packit Service 0a2193
handle_redo (NautilusDBusFileOperations *object,
Packit Service 0a2193
             GDBusMethodInvocation      *invocation)
Packit Service 0a2193
{
Packit Service 345a52
    NautilusFileUndoManager *undo_manager = NULL;
Packit Service be7f18
    gint *handler_id = g_new0(int, 1);
Packit Service 0a2193
Packit Service 9ff898
    g_application_hold (g_application_get_default ());
Packit Service 9ff898
Packit Service 0a2193
    undo_manager = nautilus_file_undo_manager_get ();
Packit Service be7f18
    *handler_id = g_signal_connect_swapped (undo_manager, "undo-changed",
Packit Service be7f18
                                            G_CALLBACK (undo_redo_on_finished),
Packit Service be7f18
                                            handler_id);
Packit Service 0a2193
    nautilus_file_undo_manager_redo (NULL);
Packit Service 0a2193
Packit Service 0a2193
    nautilus_dbus_file_operations_complete_redo (object, invocation);
Packit Service 0a2193
    return TRUE; /* invocation was handled */
Packit Service 0a2193
}
Packit Service 0a2193
Packit Service 0a2193
static gboolean
Packit Service 0a2193
handle_undo (NautilusDBusFileOperations *object,
Packit Service 0a2193
             GDBusMethodInvocation      *invocation)
Packit Service 0a2193
{
Packit Service 345a52
    NautilusFileUndoManager *undo_manager = NULL;
Packit Service be7f18
    gint *handler_id = g_new0(int, 1);
Packit Service 0a2193
Packit Service 9ff898
    g_application_hold (g_application_get_default ());
Packit Service 9ff898
Packit Service 0a2193
    undo_manager = nautilus_file_undo_manager_get ();
Packit Service be7f18
    *handler_id = g_signal_connect_swapped (undo_manager, "undo-changed",
Packit Service be7f18
                                            G_CALLBACK (undo_redo_on_finished),
Packit Service be7f18
                                            handler_id);
Packit Service 0a2193
    nautilus_file_undo_manager_undo (NULL);
Packit Service 0a2193
Packit Service 0a2193
    nautilus_dbus_file_operations_complete_undo (object, invocation);
Packit Service 0a2193
    return TRUE; /* invocation was handled */
Packit Service 0a2193
}
Packit Service 0a2193
Packit Service be7f18
static void
Packit Service be7f18
create_folder_on_finished (GFile    *new_file,
Packit Service be7f18
                           gboolean  success,
Packit Service be7f18
                           gpointer  callback_data)
Packit Service be7f18
{
Packit Service be7f18
    g_application_release (g_application_get_default ());
Packit Service be7f18
}
Packit Service be7f18
Packit Service 0a2193
static gboolean
Packit Service 32add5
handle_create_folder (NautilusDBusFileOperations *object,
Packit Service 32add5
                      GDBusMethodInvocation      *invocation,
Packit Service 32add5
                      const gchar                *uri)
Packit Service 32add5
{
Packit Service 32add5
    g_autoptr (GFile) file = NULL;
Packit Service 32add5
    g_autoptr (GFile) parent_file = NULL;
Packit Service 32add5
    g_autofree gchar *basename = NULL;
Packit Service 32add5
    g_autofree gchar *parent_file_uri = NULL;
Packit Service 32add5
Packit Service 32add5
    file = g_file_new_for_uri (uri);
Packit Service 32add5
    basename = g_file_get_basename (file);
Packit Service 32add5
    parent_file = g_file_get_parent (file);
Packit Service 32add5
    parent_file_uri = g_file_get_uri (parent_file);
Packit Service 32add5
Packit Service be7f18
    g_application_hold (g_application_get_default ());
Packit Service 32add5
    nautilus_file_operations_new_folder (NULL, parent_file_uri, basename,
Packit Service be7f18
                                         create_folder_on_finished, NULL);
Packit Service 32add5
Packit Service 32add5
    nautilus_dbus_file_operations_complete_create_folder (object, invocation);
Packit Service 32add5
    return TRUE; /* invocation was handled */
Packit Service 32add5
}
Packit Service 32add5
Packit Service be7f18
static void
Packit Service 7b21ed
copy_move_on_finished (GHashTable *debutting_uris,
Packit Service 7b21ed
                       gboolean    success,
Packit Service 7b21ed
                       gpointer    callback_data)
Packit Service be7f18
{
Packit Service be7f18
    g_application_release (g_application_get_default ());
Packit Service be7f18
}
Packit Service be7f18
Packit Service 32add5
static gboolean
Packit a189e0
handle_copy_uris (NautilusDBusFileOperations  *object,
Packit a189e0
                  GDBusMethodInvocation       *invocation,
Packit a189e0
                  const gchar                **sources,
Packit a189e0
                  const gchar                 *destination)
Packit a189e0
{
Packit a189e0
    GList *source_files = NULL;
Packit a189e0
    gint idx;
Packit a189e0
Packit 137163
    for (idx = 0; sources[idx] != NULL; idx++)
Packit 137163
    {
Packit Service 9ee24d
        source_files = g_list_prepend (source_files, g_strdup (sources[idx]));
Packit 137163
    }
Packit 137163
Packit Service be7f18
    g_application_hold (g_application_get_default ());
Packit Service 9ee24d
    nautilus_file_operations_copy_move (source_files, destination,
Packit Service 7b21ed
                                        GDK_ACTION_COPY, NULL, copy_move_on_finished, NULL);
Packit Service 7b21ed
Packit Service 7b21ed
    g_list_free_full (source_files, g_free);
Packit Service 7b21ed
    nautilus_dbus_file_operations_complete_copy_uris (object, invocation);
Packit Service 7b21ed
    return TRUE; /* invocation was handled */
Packit Service 7b21ed
}
Packit Service 7b21ed
Packit Service 7b21ed
static gboolean
Packit Service 7b21ed
handle_move_uris (NautilusDBusFileOperations  *object,
Packit Service 7b21ed
                  GDBusMethodInvocation       *invocation,
Packit Service 7b21ed
                  const gchar                **sources,
Packit Service 7b21ed
                  const gchar                 *destination)
Packit Service 7b21ed
{
Packit Service 7b21ed
    GList *source_files = NULL;
Packit Service 7b21ed
    gint idx;
Packit Service 7b21ed
Packit Service 7b21ed
    for (idx = 0; sources[idx] != NULL; idx++)
Packit Service 7b21ed
    {
Packit Service 7b21ed
        source_files = g_list_prepend (source_files, g_strdup (sources[idx]));
Packit Service 7b21ed
    }
Packit Service 7b21ed
Packit Service 7b21ed
    g_application_hold (g_application_get_default ());
Packit Service 7b21ed
    nautilus_file_operations_copy_move (source_files, destination,
Packit Service 7b21ed
                                        GDK_ACTION_MOVE, NULL, copy_move_on_finished, NULL);
Packit a189e0
Packit Service 9ee24d
    g_list_free_full (source_files, g_free);
Packit a189e0
    nautilus_dbus_file_operations_complete_copy_uris (object, invocation);
Packit a189e0
    return TRUE; /* invocation was handled */
Packit a189e0
}
Packit a189e0
Packit Service be7f18
/* FIXME: Needs a callback for maintaining alive the application */
Packit a189e0
static gboolean
Packit a189e0
handle_empty_trash (NautilusDBusFileOperations *object,
Packit a189e0
                    GDBusMethodInvocation      *invocation)
Packit a189e0
{
Packit a189e0
    nautilus_file_operations_empty_trash (NULL);
Packit a189e0
Packit a189e0
    nautilus_dbus_file_operations_complete_empty_trash (object, invocation);
Packit a189e0
    return TRUE; /* invocation was handled */
Packit a189e0
}
Packit a189e0
Packit Service be7f18
static void
Packit Service be7f18
trash_on_finished (GHashTable *debutting_uris,
Packit Service be7f18
                   gboolean    user_cancel,
Packit Service be7f18
                   gpointer    callback_data)
Packit Service be7f18
{
Packit Service be7f18
    g_application_release (g_application_get_default ());
Packit Service be7f18
}
Packit Service be7f18
Packit Service 3080ca
static gboolean
Packit Service 3080ca
handle_trash_files (NautilusDBusFileOperations  *object,
Packit Service 3080ca
                    GDBusMethodInvocation       *invocation,
Packit Service 3080ca
                    const gchar                **sources)
Packit Service 3080ca
{
Packit Service 3080ca
    g_autolist (GFile) source_files = NULL;
Packit Service 3080ca
    gint idx;
Packit Service 3080ca
Packit Service 3080ca
    for (idx = 0; sources[idx] != NULL; idx++)
Packit Service 3080ca
    {
Packit Service 3080ca
        source_files = g_list_prepend (source_files,
Packit Service 3080ca
                                       g_file_new_for_uri (sources[idx]));
Packit Service 3080ca
    }
Packit Service 3080ca
Packit Service be7f18
    g_application_hold (g_application_get_default ());
Packit Service be7f18
    nautilus_file_operations_trash_or_delete (source_files, NULL,
Packit Service be7f18
                                              trash_on_finished, NULL);
Packit Service 3080ca
Packit Service 3080ca
    nautilus_dbus_file_operations_complete_trash_files (object, invocation);
Packit Service 3080ca
    return TRUE; /* invocation was handled */
Packit Service 3080ca
}
Packit Service 3080ca
Packit Service 10d738
static void
Packit Service 10d738
rename_file_on_finished (NautilusFile *file,
Packit Service 10d738
                         GFile        *result_location,
Packit Service 10d738
                         GError       *error,
Packit Service 10d738
                         gpointer      callback_data)
Packit Service 10d738
{
Packit Service 10d738
    g_application_release (g_application_get_default ());
Packit Service 10d738
}
Packit Service 10d738
Packit Service 10d738
static gboolean
Packit Service 10d738
handle_rename_file (NautilusDBusFileOperations *object,
Packit Service 10d738
                    GDBusMethodInvocation      *invocation,
Packit Service 10d738
                    const gchar                *uri,
Packit Service 10d738
                    const gchar                *new_name)
Packit Service 10d738
{
Packit Service 10d738
    NautilusFile *file = NULL;
Packit Service 10d738
Packit Service 10d738
    file = nautilus_file_get_by_uri (uri);
Packit Service 10d738
Packit Service 10d738
    g_application_hold (g_application_get_default ());
Packit Service 10d738
    nautilus_file_rename (file, new_name,
Packit Service 10d738
                          rename_file_on_finished, NULL);
Packit Service 10d738
Packit Service 10d738
    nautilus_dbus_file_operations_complete_rename_file (object, invocation);
Packit Service 10d738
Packit Service 10d738
    return TRUE; /* invocation was handled */
Packit Service 10d738
}
Packit Service 10d738
Packit Service c407c4
Packit Service c407c4
static void
Packit Service c407c4
undo_manager_changed (NautilusDBusManager *self)
Packit Service c407c4
{
Packit Service c407c4
    NautilusFileUndoManagerState undo_state;
Packit Service c407c4
Packit Service c407c4
    undo_state = nautilus_file_undo_manager_get_state ();
Packit Service c407c4
    nautilus_dbus_file_operations_set_undo_status (self->file_operations,
Packit Service c407c4
                                                   undo_state);
Packit Service c407c4
}
Packit Service c407c4
Packit 2a724a
static void
Packit a189e0
nautilus_dbus_manager_init (NautilusDBusManager *self)
Packit a189e0
{
Packit a189e0
    self->file_operations = nautilus_dbus_file_operations_skeleton_new ();
Packit a189e0
Packit a189e0
    g_signal_connect (self->file_operations,
Packit a189e0
                      "handle-copy-uris",
Packit a189e0
                      G_CALLBACK (handle_copy_uris),
Packit a189e0
                      self);
Packit a189e0
    g_signal_connect (self->file_operations,
Packit Service 7b21ed
                      "handle-move-uris",
Packit Service 7b21ed
                      G_CALLBACK (handle_move_uris),
Packit a189e0
                      self);
Packit a189e0
    g_signal_connect (self->file_operations,
Packit a189e0
                      "handle-empty-trash",
Packit a189e0
                      G_CALLBACK (handle_empty_trash),
Packit a189e0
                      self);
Packit Service 3080ca
    g_signal_connect (self->file_operations,
Packit Service 3080ca
                      "handle-trash-files",
Packit Service 3080ca
                      G_CALLBACK (handle_trash_files),
Packit Service 3080ca
                      self);
Packit Service 32add5
    g_signal_connect (self->file_operations,
Packit Service 32add5
                      "handle-create-folder",
Packit Service 32add5
                      G_CALLBACK (handle_create_folder),
Packit Service 32add5
                      self);
Packit Service 0a2193
    g_signal_connect (self->file_operations,
Packit Service 10d738
                      "handle-rename-file",
Packit Service 10d738
                      G_CALLBACK (handle_rename_file),
Packit Service 10d738
                      self);
Packit Service 10d738
    g_signal_connect (self->file_operations,
Packit Service 0a2193
                      "handle-undo",
Packit Service 0a2193
                      G_CALLBACK (handle_undo),
Packit Service 0a2193
                      self);
Packit Service 0a2193
    g_signal_connect (self->file_operations,
Packit Service 0a2193
                      "handle-redo",
Packit Service 0a2193
                      G_CALLBACK (handle_redo),
Packit Service 0a2193
                      self);
Packit a189e0
}
Packit a189e0
Packit a189e0
static void
Packit a189e0
nautilus_dbus_manager_class_init (NautilusDBusManagerClass *klass)
Packit a189e0
{
Packit a189e0
    GObjectClass *oclass = G_OBJECT_CLASS (klass);
Packit a189e0
Packit a189e0
    oclass->dispose = nautilus_dbus_manager_dispose;
Packit a189e0
}
Packit a189e0
Packit a189e0
NautilusDBusManager *
Packit a189e0
nautilus_dbus_manager_new (void)
Packit a189e0
{
Packit a189e0
    return g_object_new (nautilus_dbus_manager_get_type (),
Packit a189e0
                         NULL);
Packit a189e0
}
Packit a189e0
Packit a189e0
gboolean
Packit a189e0
nautilus_dbus_manager_register (NautilusDBusManager  *self,
Packit a189e0
                                GDBusConnection      *connection,
Packit a189e0
                                GError              **error)
Packit a189e0
{
Packit Service c407c4
    gboolean succes;
Packit Service c407c4
Packit Service c407c4
    succes = g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->file_operations),
Packit Service c407c4
                                               connection, "/org/gnome/Nautilus" PROFILE, error);
Packit Service c407c4
    if (succes)
Packit Service c407c4
    {
Packit Service c407c4
        g_signal_connect_object (nautilus_file_undo_manager_get (),
Packit Service c407c4
                                 "undo-changed",
Packit Service c407c4
                                 G_CALLBACK (undo_manager_changed),
Packit Service c407c4
                                 self,
Packit Service c407c4
                                 G_CONNECT_SWAPPED);
Packit Service c407c4
Packit Service c407c4
        undo_manager_changed (self);
Packit Service c407c4
    }
Packit Service c407c4
Packit Service c407c4
    return succes;
Packit a189e0
}
Packit a189e0
Packit a189e0
void
Packit a189e0
nautilus_dbus_manager_unregister (NautilusDBusManager *self)
Packit a189e0
{
Packit a189e0
    g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (self->file_operations));
Packit Service c407c4
Packit Service c407c4
    g_signal_handlers_disconnect_by_data (nautilus_file_undo_manager_get (), self);
Packit a189e0
}