[gnome-photos] Add PhotosMediaServerItem



commit c827d594545271e129239dc4d3db3f4831d9920e
Author: Pranav Kant <pranav913 gmail com>
Date:   Fri Jul 4 15:15:40 2014 +0530

    Add PhotosMediaServerItem
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728913

 src/Makefile.am                |    2 +
 src/photos-media-server-item.c |  190 ++++++++++++++++++++++++++++++++++++++++
 src/photos-media-server-item.h |   73 +++++++++++++++
 src/photos-utils.c             |    2 +
 4 files changed, 267 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 30f6eaf..2f4f09b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -135,6 +135,8 @@ gnome_photos_SOURCES = \
        photos-main-toolbar.h \
        photos-main-window.c \
        photos-main-window.h \
+       photos-media-server-item.c \
+       photos-media-server-item.h \
        photos-mode-controller.c \
        photos-mode-controller.h \
        photos-notification-manager.c \
diff --git a/src/photos-media-server-item.c b/src/photos-media-server-item.c
new file mode 100644
index 0000000..f9cd603
--- /dev/null
+++ b/src/photos-media-server-item.c
@@ -0,0 +1,190 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2014 Pranav Kant
+ *
+ * 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
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+
+#include "config.h"
+
+#include <string.h>
+
+#include <gio/gio.h>
+#include <glib.h>
+
+#include "photos-base-manager.h"
+#include "photos-media-server-item.h"
+#include "photos-search-context.h"
+#include "photos-source.h"
+#include "photos-utils.h"
+
+
+struct _PhotosMediaServerItemPrivate
+{
+  PhotosBaseManager *src_mngr;
+};
+
+
+G_DEFINE_TYPE_WITH_CODE (PhotosMediaServerItem, photos_media_server_item, PHOTOS_TYPE_BASE_ITEM,
+                         G_ADD_PRIVATE (PhotosMediaServerItem)
+                         photos_utils_ensure_extension_points ();
+                         g_io_extension_point_implement (PHOTOS_BASE_ITEM_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "media-server",
+                                                         0));
+
+
+static gboolean
+photos_media_server_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
+{
+  GFile *file;
+  gboolean ret_val;
+  const gchar *uri;
+
+  uri = photos_base_item_get_uri (item);
+  file = g_file_new_for_uri (uri);
+  ret_val = photos_utils_create_thumbnail (file, cancellable, error);
+
+  g_object_unref (file);
+  return ret_val;
+}
+
+
+static gchar *
+photos_media_server_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
+{
+  GFile *local_file = NULL;
+  GFile *remote_file = NULL;
+  const gchar *cache_dir;
+  const gchar *uri;
+  gchar *local_dir = NULL;
+  gchar *local_filename = NULL;
+  gchar *local_path = NULL;
+  gchar *ret_val = NULL;
+
+  uri = photos_base_item_get_uri (item);
+  remote_file = g_file_new_for_uri (uri);
+  cache_dir = g_get_user_cache_dir ();
+
+  local_dir = g_build_filename (cache_dir, PACKAGE_TARNAME, "media-server", NULL);
+  g_mkdir_with_parents (local_dir, 0700);
+
+  local_filename = g_file_get_basename (remote_file);
+  local_path = g_build_filename (local_dir, local_filename, NULL);
+  local_file = g_file_new_for_path (local_path);
+
+  if (!g_file_test (local_path, G_FILE_TEST_EXISTS))
+    {
+      g_debug ("Downloading %s from Media Server to %s", uri, local_path);
+      if (!g_file_copy (remote_file,
+                        local_file,
+                        G_FILE_COPY_ALL_METADATA | G_FILE_COPY_OVERWRITE,
+                        cancellable,
+                        NULL,
+                        NULL,
+                        error))
+        {
+          g_file_delete (local_file, NULL, NULL);
+          goto out;
+        }
+    }
+
+  ret_val = local_path;
+  local_path = NULL;
+
+ out:
+  g_free (local_path);
+  g_free (local_filename);
+  g_free (local_dir);
+  g_object_unref (local_file);
+  g_object_unref (remote_file);
+  return ret_val;
+}
+
+
+static GtkWidget *
+photos_media_server_item_get_source_widget (PhotosBaseItem *item)
+{
+  PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (item);
+  GtkWidget *source_widget;
+  const gchar *name;
+
+  name = photos_utils_get_provider_name (self->priv->src_mngr, item);
+  source_widget = gtk_label_new (name);
+  gtk_widget_set_halign (source_widget, GTK_ALIGN_START);
+
+  return source_widget;
+}
+
+
+static void
+photos_media_server_item_constructed (GObject *object)
+{
+  PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (object);
+  const gchar *name;
+
+  G_OBJECT_CLASS (photos_media_server_item_parent_class)->constructed (object);
+
+  name = photos_utils_get_provider_name (self->priv->src_mngr, PHOTOS_BASE_ITEM (self));
+  photos_base_item_set_default_app_name (PHOTOS_BASE_ITEM (self), name);
+}
+
+
+static void
+photos_media_server_item_dispose (GObject *object)
+{
+  PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (object);
+
+  g_clear_object (&self->priv->src_mngr);
+
+  G_OBJECT_CLASS (photos_media_server_item_parent_class)->dispose (object);
+}
+
+
+static void
+photos_media_server_item_init (PhotosMediaServerItem *self)
+{
+  PhotosMediaServerItemPrivate *priv;
+  GApplication *app;
+  PhotosSearchContextState *state;
+
+  self->priv = photos_media_server_item_get_instance_private (self);
+  priv = self->priv;
+
+  app = g_application_get_default ();
+  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
+
+  priv->src_mngr = g_object_ref (state->src_mngr);
+}
+
+
+static void
+photos_media_server_item_class_init (PhotosMediaServerItemClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PhotosBaseItemClass *base_item_class = PHOTOS_BASE_ITEM_CLASS (class);
+
+  object_class->constructed = photos_media_server_item_constructed;
+  object_class->dispose = photos_media_server_item_dispose;
+  base_item_class->create_thumbnail = photos_media_server_item_create_thumbnail;
+  base_item_class->download = photos_media_server_item_download;
+  base_item_class->get_source_widget = photos_media_server_item_get_source_widget;
+}
diff --git a/src/photos-media-server-item.h b/src/photos-media-server-item.h
new file mode 100644
index 0000000..42389dd
--- /dev/null
+++ b/src/photos-media-server-item.h
@@ -0,0 +1,73 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2014 Pranav Kant
+ *
+ * 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
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+#ifndef PHOTOS_MEDIA_SERVER_ITEM_H
+#define PHOTOS_MEDIA_SERVER_ITEM_H
+
+#include "photos-base-item.h"
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_MEDIA_SERVER_ITEM (photos_media_server_item_get_type ())
+
+#define PHOTOS_MEDIA_SERVER_ITEM(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_MEDIA_SERVER_ITEM, PhotosMediaServerItem))
+
+#define PHOTOS_MEDIA_SERVER_ITEM_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   PHOTOS_TYPE_MEDIA_SERVER_ITEM, PhotosMediaServerItemClass))
+
+#define PHOTOS_IS_MEDIA_SERVER_ITEM(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_MEDIA_SERVER_ITEM))
+
+#define PHOTOS_IS_MEDIA_SERVER_ITEM_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   PHOTOS_TYPE_MEDIA_SERVER_ITEM))
+
+#define PHOTOS_MEDIA_SERVER_ITEM_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   PHOTOS_TYPE_MEDIA_SERVER_ITEM, PhotosMediaServerItemClass))
+
+typedef struct _PhotosMediaServerItem        PhotosMediaServerItem;
+typedef struct _PhotosMediaServerItemClass   PhotosMediaServerItemClass;
+typedef struct _PhotosMediaServerItemPrivate PhotosMediaServerItemPrivate;
+
+struct _PhotosMediaServerItem
+{
+  PhotosBaseItem parent_instance;
+  PhotosMediaServerItemPrivate *priv;
+};
+
+struct _PhotosMediaServerItemClass
+{
+  PhotosBaseItemClass parent_class;
+};
+
+GType               photos_media_server_item_get_type           (void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif /* PHOTOS_MEDIA_SERVER_ITEM_H */
diff --git a/src/photos-utils.c b/src/photos-utils.c
index 5e61c98..325eb7d 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -39,6 +39,7 @@
 #include "photos-flickr-item.h"
 #include "photos-google-item.h"
 #include "photos-local-item.h"
+#include "photos-media-server-item.h"
 #include "photos-query.h"
 #include "photos-source.h"
 #include "photos-tracker-queue.h"
@@ -384,6 +385,7 @@ photos_utils_ensure_builtins (void)
       g_type_ensure (PHOTOS_TYPE_FLICKR_ITEM);
       g_type_ensure (PHOTOS_TYPE_GOOGLE_ITEM);
       g_type_ensure (PHOTOS_TYPE_LOCAL_ITEM);
+      g_type_ensure (PHOTOS_TYPE_MEDIA_SERVER_ITEM);
 
       g_once_init_leave (&once_init_value, 1);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]