[epiphany/wip/downloads: 3/10] downloads: Add EphyDownloadsManager
- From: Carlos Garcia Campos <carlosgc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/downloads: 3/10] downloads: Add EphyDownloadsManager
- Date: Wed, 7 Oct 2015 15:25:32 +0000 (UTC)
commit bbf501f08b4cd3ee6570ea78a86573e648f83600
Author: Carlos Garcia Campos <cgarcia igalia com>
Date: Wed Oct 7 10:29:42 2015 +0200
downloads: Add EphyDownloadsManager
It's a global manager provided by EphyEmbedShell that will be used to
handle the downloads globally. Downloads are currently associated to a
particular window.
embed/Makefile.am | 2 +
embed/ephy-downloads-manager.c | 155 ++++++++++++++++++++++++++++++++++++++++
embed/ephy-downloads-manager.h | 49 +++++++++++++
embed/ephy-embed-shell.c | 10 +++
embed/ephy-embed-shell.h | 2 +
5 files changed, 218 insertions(+), 0 deletions(-)
---
diff --git a/embed/Makefile.am b/embed/Makefile.am
index cbfe392..8d54893 100644
--- a/embed/Makefile.am
+++ b/embed/Makefile.am
@@ -9,6 +9,7 @@ noinst_LTLIBRARIES = libephyembed.la
NOINST_H_FILES = \
ephy-about-handler.h \
+ ephy-downloads-manager.h
ephy-embed-dialog.h \
ephy-embed-private.h \
ephy-encoding.h \
@@ -34,6 +35,7 @@ BUILT_SOURCES = \
libephyembed_la_SOURCES = \
ephy-about-handler.c \
+ ephy-downloads-manager.c \
ephy-download.c \
ephy-embed.c \
ephy-embed-container.c \
diff --git a/embed/ephy-downloads-manager.c b/embed/ephy-downloads-manager.c
new file mode 100644
index 0000000..f8b54bb
--- /dev/null
+++ b/embed/ephy-downloads-manager.c
@@ -0,0 +1,155 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright © 2015 Igalia S.L.
+ *
+ * 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 2, 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "config.h"
+#include "ephy-downloads-manager.h"
+
+enum {
+ DOWNLOAD_ADDED,
+ DOWNLOAD_REMOVED,
+
+ LAST_SIGNAL
+};
+
+struct _EphyDownloadsManager
+{
+ GObject parent;
+
+ GList *downloads;
+};
+
+struct _EphyDownloadsManagerClass
+{
+ GObjectClass parent_class;
+};
+
+static guint signals[LAST_SIGNAL];
+
+G_DEFINE_TYPE (EphyDownloadsManager, ephy_downloads_manager, G_TYPE_OBJECT)
+
+static void
+ephy_downloads_manager_init (EphyDownloadsManager *manager)
+{
+}
+
+static void
+ephy_downloads_manager_dispose (GObject *object)
+{
+ EphyDownloadsManager *manager = EPHY_DOWNLOADS_MANAGER (object);
+
+ g_list_free_full (manager->downloads, g_object_unref);
+
+ G_OBJECT_CLASS (ephy_downloads_manager_parent_class)->dispose (object);
+}
+
+static void
+ephy_downloads_manager_class_init (EphyDownloadsManagerClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = ephy_downloads_manager_dispose;
+
+ signals[DOWNLOAD_ADDED] =
+ g_signal_new ("download-added",
+ EPHY_TYPE_DOWNLOADS_MANAGER,
+ G_SIGNAL_RUN_LAST,
+ 0, NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE, 1,
+ EPHY_TYPE_DOWNLOAD);
+
+ signals[DOWNLOAD_REMOVED] =
+ g_signal_new ("download-removed",
+ EPHY_TYPE_DOWNLOADS_MANAGER,
+ G_SIGNAL_RUN_LAST,
+ 0, NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE, 1,
+ EPHY_TYPE_DOWNLOAD);
+}
+
+static void
+download_failed_cb (EphyDownload *download,
+ GError *error,
+ EphyDownloadsManager *manager)
+{
+ if (g_error_matches (error, WEBKIT_DOWNLOAD_ERROR, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER))
+ ephy_downloads_manager_remove_download (manager, download);
+}
+
+void
+ephy_downloads_manager_add_download (EphyDownloadsManager *manager,
+ EphyDownload *download)
+{
+ g_return_if_fail (EPHY_IS_DOWNLOADS_MANAGER (manager));
+ g_return_if_fail (EPHY_IS_DOWNLOAD (download));
+
+ if (g_list_find (manager->downloads, download))
+ return;
+
+ manager->downloads = g_list_prepend (manager->downloads, g_object_ref (download));
+ g_signal_connect (download, "error",
+ G_CALLBACK (download_failed_cb),
+ manager);
+ g_signal_emit (manager, signals[DOWNLOAD_ADDED], 0, download);
+}
+
+void
+ephy_downloads_manager_remove_download (EphyDownloadsManager *manager,
+ EphyDownload *download)
+{
+ GList *download_link;
+
+ g_return_if_fail (EPHY_IS_DOWNLOADS_MANAGER (manager));
+ g_return_if_fail (EPHY_IS_DOWNLOAD (download));
+
+ download_link = g_list_find (manager->downloads, download);
+ if (!download_link)
+ return;
+
+ manager->downloads = g_list_remove_link (manager->downloads, download_link);
+ g_signal_emit (manager, signals[DOWNLOAD_REMOVED], 0, download);
+ g_list_free_full (download_link, g_object_unref);
+}
+
+gboolean
+ephy_downloads_manager_has_active_downloads (EphyDownloadsManager *manager)
+{
+ GList *l;
+
+ g_return_val_if_fail (EPHY_IS_DOWNLOADS_MANAGER (manager), FALSE);
+
+ for (l = manager->downloads; l; l = g_list_next (l)) {
+ EphyDownload *download = EPHY_DOWNLOAD (l->data);
+
+ if (ephy_download_is_active (download))
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+GList *
+ephy_downloads_manager_get_downloads (EphyDownloadsManager *manager)
+{
+ g_return_val_if_fail (EPHY_IS_DOWNLOADS_MANAGER (manager), NULL);
+
+ return manager->downloads;
+}
diff --git a/embed/ephy-downloads-manager.h b/embed/ephy-downloads-manager.h
new file mode 100644
index 0000000..2763807
--- /dev/null
+++ b/embed/ephy-downloads-manager.h
@@ -0,0 +1,49 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright © 2015 Igalia S.L.
+ *
+ * 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 2, 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef EPHY_DOWNLOADS_MANAGER_H
+#define EPHY_DOWNLOADS_MANAGER_H
+
+#include "ephy-download.h"
+
+G_BEGIN_DECLS
+
+#define EPHY_TYPE_DOWNLOADS_MANAGER (ephy_downloads_manager_get_type ())
+#define EPHY_DOWNLOADS_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_DOWNLOADS_MANAGER,
EphyDownloadsManager))
+#define EPHY_DOWNLOADS_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EPHY_TYPE_DOWNLOADS_MANAGER,
EphyDownloadsManagerClass))
+#define EPHY_IS_DOWNLOADS_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_DOWNLOADS_MANAGER))
+#define EPHY_IS_DOWNLOADS_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_DOWNLOADS_MANAGER))
+#define EPHY_DOWNLOADS_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_DOWNLOADS_MANAGER,
EphyDownloadsManagerClass))
+
+typedef struct _EphyDownloadsManagerClass EphyDownloadsManagerClass;
+typedef struct _EphyDownloadsManager EphyDownloadsManager;
+
+GType ephy_downloads_manager_get_type (void);
+
+void ephy_downloads_manager_add_download (EphyDownloadsManager *manager,
+ EphyDownload *download);
+void ephy_downloads_manager_remove_download (EphyDownloadsManager *manager,
+ EphyDownload *download);
+gboolean ephy_downloads_manager_has_active_downloads (EphyDownloadsManager *manager);
+GList *ephy_downloads_manager_get_downloads (EphyDownloadsManager *manager);
+
+G_END_DECLS
+
+#endif /* EPHY_DOWNLOADS_MANAGER_H */
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 5371744..eb7b3c6 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -57,6 +57,7 @@ struct _EphyEmbedShellPrivate
GtkPrintSettings *print_settings;
EphyEmbedShellMode mode;
WebKitUserContentManager *user_content;
+ EphyDownloadsManager *downloads_manager;
EphyAboutHandler *about_handler;
guint update_overview_timeout_id;
guint hiding_overview_item;
@@ -108,6 +109,7 @@ ephy_embed_shell_dispose (GObject *object)
g_clear_object (&priv->global_history_service);
g_clear_object (&priv->about_handler);
g_clear_object (&priv->user_content);
+ g_clear_object (&priv->downloads_manager);
g_clear_object (&priv->web_context);
G_OBJECT_CLASS (ephy_embed_shell_parent_class)->dispose (object);
@@ -1129,3 +1131,11 @@ ephy_embed_shell_get_web_context (EphyEmbedShell *shell)
{
return shell->priv->web_context;
}
+
+EphyDownloadsManager *
+ephy_embed_shell_get_downloads_manager (EphyEmbedShell *shell)
+{
+ if (!shell->priv->downloads_manager)
+ shell->priv->downloads_manager = EPHY_DOWNLOADS_MANAGER (g_object_new (EPHY_TYPE_DOWNLOADS_MANAGER,
NULL));
+ return shell->priv->downloads_manager;
+}
diff --git a/embed/ephy-embed-shell.h b/embed/ephy-embed-shell.h
index ac695fe..499ac53 100644
--- a/embed/ephy-embed-shell.h
+++ b/embed/ephy-embed-shell.h
@@ -27,6 +27,7 @@
#define EPHY_EMBED_SHELL_H
#include <webkit2/webkit2.h>
+#include "ephy-downloads-manager.h"
G_BEGIN_DECLS
@@ -96,6 +97,7 @@ void ephy_embed_shell_set_thumbanil_path (EphyEmbedShell
time_t mtime,
const char *path);
WebKitUserContentManager *ephy_embed_shell_get_user_content_manager (EphyEmbedShell *shell);
+EphyDownloadsManager *ephy_embed_shell_get_downloads_manager (EphyEmbedShell *shell);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]