Commit 011a3d78 authored by Christian Hergert's avatar Christian Hergert

transfers: add IdeTransferManager and IdeTransfer

This new API should be used by plugins to keep track of transfers that are
in-flight. Such an example could be a download (say flatpak) or
transferring a build to a device.
parent ecd3e85b
......@@ -131,6 +131,10 @@ libide_1_0_la_public_headers = \
template/ide-template-base.h \
template/ide-template-provider.h \
threading/ide-thread-pool.h \
transfers/ide-transfer.c \
transfers/ide-transfer.h \
transfers/ide-transfer-manager.c \
transfers/ide-transfer-manager.h \
tree/ide-tree-builder.h \
tree/ide-tree-node.h \
tree/ide-tree-types.h \
......
......@@ -48,6 +48,7 @@
#include "search/ide-search-engine.h"
#include "search/ide-search-provider.h"
#include "snippets/ide-source-snippets-manager.h"
#include "transfers/ide-transfer-manager.h"
#include "util/ide-async-helper.h"
#include "util/ide-settings.h"
#include "vcs/ide-vcs.h"
......@@ -72,6 +73,7 @@ struct _IdeContext
IdeScriptManager *script_manager;
IdeSearchEngine *search_engine;
IdeSourceSnippetsManager *snippets_manager;
IdeTransferManager *transfer_manager;
IdeProject *project;
GFile *project_file;
gchar *root_build_dir;
......@@ -564,6 +566,7 @@ ide_context_finalize (GObject *object)
g_clear_object (&self->project_file);
g_clear_object (&self->recent_manager);
g_clear_object (&self->runtime_manager);
g_clear_object (&self->transfer_manager);
g_clear_object (&self->unsaved_files);
g_clear_object (&self->vcs);
......@@ -852,6 +855,10 @@ ide_context_init (IdeContext *self)
"context", self,
NULL);
self->transfer_manager = g_object_new (IDE_TYPE_TRANSFER_MANAGER,
"context", self,
NULL);
self->unsaved_files = g_object_new (IDE_TYPE_UNSAVED_FILES,
"context", self,
NULL);
......@@ -2195,3 +2202,18 @@ ide_context_get_run_manager (IdeContext *self)
return self->run_manager;
}
/**
* ide_context_get_transfer_manager:
*
* Gets the #IdeTransferManager for the context.
*
* Returns: (transfer none): An #IdeTransferManager.
*/
IdeTransferManager *
ide_context_get_transfer_manager (IdeContext *self)
{
g_return_val_if_fail (IDE_IS_CONTEXT (self), NULL);
return self->transfer_manager;
}
......@@ -47,6 +47,7 @@ IdeSettings *ide_context_get_settings (IdeContext
const gchar *schema_id,
const gchar *relative_path);
IdeSourceSnippetsManager *ide_context_get_snippets_manager (IdeContext *self);
IdeTransferManager *ide_context_get_transfer_manager (IdeContext *self);
IdeUnsavedFiles *ide_context_get_unsaved_files (IdeContext *self);
IdeVcs *ide_context_get_vcs (IdeContext *self);
const gchar *ide_context_get_root_build_dir (IdeContext *self);
......
......@@ -123,6 +123,8 @@ typedef struct _IdeSymbol IdeSymbol;
typedef struct _IdeSymbolResolver IdeSymbolResolver;
typedef struct _IdeSymbolResolverInterface IdeSymbolResolverInterface;
typedef struct _IdeTransferManager IdeTransferManager;
typedef struct _IdeUnsavedFiles IdeUnsavedFiles;
typedef struct _IdeUnsavedFile IdeUnsavedFile;
......
......@@ -119,6 +119,8 @@ G_BEGIN_DECLS
#include "template/ide-project-template.h"
#include "template/ide-template-provider.h"
#include "threading/ide-thread-pool.h"
#include "transfers/ide-transfer.h"
#include "transfers/ide-transfer-manager.h"
#include "tree/ide-tree-builder.h"
#include "tree/ide-tree-node.h"
#include "tree/ide-tree-types.h"
......
/* ide-transfer-manager.c
*
* Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "ide-transfer-manager"
#include "ide-context.h"
#include "ide-debug.h"
#include "transfers/ide-transfer.h"
#include "transfers/ide-transfer-manager.h"
#define DEFAULT_MAX_ACTIVE 1
struct _IdeTransferManager
{
GObject parent_instance;
guint max_active;
GPtrArray *transfers;
};
static void list_model_iface_init (GListModelInterface *iface);
static void ide_transfer_manager_pump (IdeTransferManager *self);
G_DEFINE_TYPE_EXTENDED (IdeTransferManager, ide_transfer_manager, IDE_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
enum {
PROP_0,
PROP_HAS_ACTIVE,
PROP_MAX_ACTIVE,
PROP_PROGRESS,
N_PROPS
};
enum {
TRANSFER_COMPLETED,
TRANSFER_FAILED,
N_SIGNALS
};
static GParamSpec *properties [N_PROPS];
static guint signals [N_SIGNALS];
#define GET_BOOLEAN(obj,name) (NULL != g_object_get_data(G_OBJECT(obj), name))
#define SET_BOOLEAN(obj,name,val) (g_object_set_data(G_OBJECT(obj), name, GINT_TO_POINTER(val)))
static void
transfer_cancel (IdeTransfer *transfer)
{
GCancellable *cancellable;
g_assert (IDE_IS_TRANSFER (transfer));
cancellable = g_object_get_data (G_OBJECT (transfer), "IDE_TRANSFER_CANCELLABLE");
if (G_IS_CANCELLABLE (cancellable) && !g_cancellable_is_cancelled (cancellable))
g_cancellable_cancel (cancellable);
}
static gboolean
transfer_get_active (IdeTransfer *transfer)
{
return GET_BOOLEAN (transfer, "IDE_TRANSFER_ACTIVE");
}
static void
transfer_set_active (IdeTransfer *transfer,
gboolean active)
{
SET_BOOLEAN (transfer, "IDE_TRANSFER_ACTIVE", active);
}
static void
transfer_set_completed (IdeTransfer *transfer,
gboolean completed)
{
SET_BOOLEAN (transfer, "IDE_TRANSFER_COMPLETED", completed);
}
static guint
ide_transfer_manager_count_active (IdeTransferManager *self)
{
guint active = 0;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
for (guint i = 0; i < self->transfers->len; i++)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
if (transfer_get_active (transfer) && !ide_transfer_has_completed (transfer))
active++;
}
return active;
}
static void
ide_transfer_manager_execute_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
IdeTransfer *transfer = (IdeTransfer *)object;
g_autoptr(IdeTransferManager) self = user_data;
g_autoptr(GError) error = NULL;
IDE_ENTRY;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
g_assert (IDE_IS_TRANSFER (transfer));
transfer_set_completed (transfer, TRUE);
if (!ide_transfer_execute_finish (transfer, result, &error))
{
IdeContext *context;
context = ide_object_get_context (IDE_OBJECT (self));
ide_context_warning (context, "%s", error->message);
}
g_signal_emit (self, signals [TRANSFER_COMPLETED], 0, transfer);
ide_transfer_manager_pump (self);
IDE_EXIT;
}
static void
ide_transfer_manager_begin (IdeTransferManager *self,
IdeTransfer *transfer)
{
GCancellable *cancellable;
IDE_ENTRY;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
g_assert (IDE_IS_TRANSFER (transfer));
transfer_set_active (transfer, TRUE);
cancellable = g_cancellable_new ();
g_object_set_data_full (G_OBJECT (transfer),
"IDE_TRANSFER_CANCELLABLE",
cancellable,
g_object_unref);
ide_transfer_execute_async (transfer,
cancellable,
ide_transfer_manager_execute_cb,
g_object_ref (self));
IDE_EXIT;
}
static void
ide_transfer_manager_pump (IdeTransferManager *self)
{
guint active;
IDE_ENTRY;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
active = ide_transfer_manager_count_active (self);
if (active < self->max_active)
{
for (guint i = 0; i < self->transfers->len; i++)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
if (!transfer_get_active (transfer))
{
active++;
ide_transfer_manager_begin (self, transfer);
if (active >= self->max_active)
break;
}
}
}
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HAS_ACTIVE]);
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_PROGRESS]);
IDE_EXIT;
}
/**
* ide_transfer_manager_get_has_active:
*
* Gets if there are active transfers.
*
* Returns: %TRUE if there are active transfers.
*/
gboolean
ide_transfer_manager_get_has_active (IdeTransferManager *self)
{
g_return_val_if_fail (IDE_IS_TRANSFER_MANAGER (self), FALSE);
for (guint i = 0; i < self->transfers->len; i++)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
if (transfer_get_active (transfer) && !ide_transfer_has_completed (transfer))
return TRUE;
}
return FALSE;
}
guint
ide_transfer_manager_get_max_active (IdeTransferManager *self)
{
g_return_val_if_fail (IDE_IS_TRANSFER_MANAGER (self), 0);
return self->max_active;
}
void
ide_transfer_manager_set_max_active (IdeTransferManager *self,
guint max_active)
{
g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
if (self->max_active != max_active)
{
self->max_active = max_active;
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MAX_ACTIVE]);
ide_transfer_manager_pump (self);
}
}
static void
ide_transfer_manager_finalize (GObject *object)
{
IdeTransferManager *self = (IdeTransferManager *)object;
g_clear_pointer (&self->transfers, g_ptr_array_unref);
G_OBJECT_CLASS (ide_transfer_manager_parent_class)->finalize (object);
}
static void
ide_transfer_manager_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
IdeTransferManager *self = IDE_TRANSFER_MANAGER (object);
switch (prop_id)
{
case PROP_HAS_ACTIVE:
g_value_set_boolean (value, ide_transfer_manager_get_has_active (self));
break;
case PROP_MAX_ACTIVE:
g_value_set_uint (value, ide_transfer_manager_get_max_active (self));
break;
case PROP_PROGRESS:
g_value_set_double (value, ide_transfer_manager_get_progress (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_transfer_manager_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
IdeTransferManager *self = IDE_TRANSFER_MANAGER (object);
switch (prop_id)
{
case PROP_MAX_ACTIVE:
ide_transfer_manager_set_max_active (self, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ide_transfer_manager_class_init (IdeTransferManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ide_transfer_manager_finalize;
object_class->get_property = ide_transfer_manager_get_property;
object_class->set_property = ide_transfer_manager_set_property;
/**
* IdeTransferManager:has-active:
*
* If there are transfers active, this will be set.
*/
properties [PROP_HAS_ACTIVE] =
g_param_spec_boolean ("has-active",
"Has Active",
"Has Active",
FALSE,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
/**
* IdeTransferManager:max-active:
*
* Sets the max number of transfers to have active at one time.
* Set to zero for a sensible default.
*/
properties [PROP_MAX_ACTIVE] =
g_param_spec_uint ("max-active",
"Max Active",
"Max Active",
0,
G_MAXUINT,
0,
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
properties [PROP_PROGRESS] =
g_param_spec_double ("progress",
"Progress",
"Progress",
0.0,
1.0,
0.0,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
/**
* IdeTransferManager::transfer-completed:
* @self: An #IdeTransferManager
* @transfer: An #IdeTransfer
*
* This signal is emitted when a transfer has completed successfully.
*/
signals [TRANSFER_COMPLETED] =
g_signal_new ("transfer-completed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL, NULL,
G_TYPE_NONE, 1, IDE_TYPE_TRANSFER);
}
static void
ide_transfer_manager_init (IdeTransferManager *self)
{
self->max_active = DEFAULT_MAX_ACTIVE;
self->transfers = g_ptr_array_new_with_free_func (g_object_unref);
}
static void
ide_transfer_manager_notify_progress (IdeTransferManager *self,
GParamSpec *pspec,
IdeTransfer *transfer)
{
g_assert (IDE_IS_TRANSFER_MANAGER (self));
g_assert (IDE_IS_TRANSFER (transfer));
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_PROGRESS]);
}
void
ide_transfer_manager_queue (IdeTransferManager *self,
IdeTransfer *transfer)
{
guint position;
IDE_ENTRY;
g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
g_return_if_fail (IDE_IS_TRANSFER (transfer));
g_signal_connect_object (transfer,
"notify::progress",
G_CALLBACK (ide_transfer_manager_notify_progress),
self,
G_CONNECT_SWAPPED);
position = self->transfers->len;
g_ptr_array_add (self->transfers, g_object_ref (transfer));
g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
ide_transfer_manager_pump (self);
IDE_EXIT;
}
void
ide_transfer_manager_cancel_all (IdeTransferManager *self)
{
IDE_ENTRY;
g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
for (guint i = 0; i < self->transfers->len; i++)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
transfer_cancel (transfer);
}
IDE_EXIT;
}
void
ide_transfer_manager_cancel (IdeTransferManager *self,
IdeTransfer *transfer)
{
IDE_ENTRY;
g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
g_return_if_fail (IDE_IS_TRANSFER (transfer));
transfer_cancel (transfer);
IDE_EXIT;
}
/**
* ide_transfer_manager_clear:
*
* Removes all transfers from the manager that are completed.
*/
void
ide_transfer_manager_clear (IdeTransferManager *self)
{
IDE_ENTRY;
g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
for (guint i = self->transfers->len; i > 0; i--)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i - 1);
if (ide_transfer_has_completed (transfer))
{
g_ptr_array_remove_index (self->transfers, i - 1);
g_list_model_items_changed (G_LIST_MODEL (self), i - 1, 1, 0);
}
}
IDE_EXIT;
}
static GType
ide_transfer_manager_get_item_type (GListModel *model)
{
return IDE_TYPE_TRANSFER;
}
static guint
ide_transfer_manager_get_n_items (GListModel *model)
{
IdeTransferManager *self = (IdeTransferManager *)model;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
return self->transfers->len;
}
static gpointer
ide_transfer_manager_get_item (GListModel *model,
guint position)
{
IdeTransferManager *self = (IdeTransferManager *)model;
g_assert (IDE_IS_TRANSFER_MANAGER (self));
if G_UNLIKELY (position >= self->transfers->len)
return NULL;
return g_object_ref (g_ptr_array_index (self->transfers, position));
}
static void
list_model_iface_init (GListModelInterface *iface)
{
iface->get_item_type = ide_transfer_manager_get_item_type;
iface->get_n_items = ide_transfer_manager_get_n_items;
iface->get_item = ide_transfer_manager_get_item;
}
gdouble
ide_transfer_manager_get_progress (IdeTransferManager *self)
{
gdouble total = 0.0;
g_return_val_if_fail (IDE_IS_TRANSFER_MANAGER (self), 0.0);
if (self->transfers->len == 0)
return 0.0;
for (guint i = 0; i < self->transfers->len; i++)
{
IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
gdouble progress;
progress = ide_transfer_get_progress (transfer);
total += MAX (0.0, MIN (1.0, progress));
}
return total / (gdouble)self->transfers->len;
}
/* ide-transfer-manager.h
*
* Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IDE_TRANSFER_MANAGER_H
#define IDE_TRANSFER_MANAGER_H
#include "ide-object.h"
#include "transfers/ide-transfer.h"
G_BEGIN_DECLS
#define IDE_TYPE_TRANSFER_MANAGER (ide_transfer_manager_get_type())
G_DECLARE_FINAL_TYPE (IdeTransferManager, ide_transfer_manager, IDE, TRANSFER_MANAGER, IdeObject)
gdouble ide_transfer_manager_get_progress (IdeTransferManager *self);
gboolean ide_transfer_manager_get_has_active (IdeTransferManager *self);
guint ide_transfer_manager_get_max_active (IdeTransferManager *self);
void ide_transfer_manager_set_max_active (IdeTransferManager *self,
guint max_active);
void ide_transfer_manager_cancel (IdeTransferManager *self,
IdeTransfer *transfer);
void ide_transfer_manager_cancel_all (IdeTransferManager *self);
void ide_transfer_manager_clear (IdeTransferManager *self);
void ide_transfer_manager_queue (IdeTransferManager *self,
IdeTransfer *transfer);
G_END_DECLS
#endif /* IDE_TRANSFER_MANAGER_H */
<
/* ide-transfer.c
*
* Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "ide-transfer"
#include "ide-transfer.h"
G_DEFINE_INTERFACE (IdeTransfer, ide_transfer, G_TYPE_OBJECT)
static void
ide_transfer_real_execute_async (IdeTransfer *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_assert (IDE_IS_TRANSFER (self));
g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
task = g_task_new (self, cancellable, callback, user_data);
g_task_return_boolean (task, TRUE);
}
static gboolean
ide_transfer_real_execute_finish (IdeTransfer *self,
GAsyncResult *result,
GError **error)
{
g_assert (IDE_IS_TRANSFER (self));
g_assert (G_IS_TASK (result));
return g_task_propagate_boolean (G_TASK (result), error);
}
static void
ide_transfer_default_init (IdeTransferInterface *iface)
{
iface->execute_async = ide_transfer_real_execute_async;
iface->execute_finish = ide_transfer_real_execute_finish;
g_object_interface_install_property (iface,
g_param_spec_string ("title",
"Title",
"Title",
NULL,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
g_object_interface_install_property (iface,
g_param_spec_string ("icon-name",
"Icon Name",
"Icon Name",
NULL,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));