1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2014 Pranav Kant
4  * Copyright © 2014 – 2019 Red Hat, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /* Based on code from:
21  *   + Documents
22  */
23 
24 
25 #include "config.h"
26 
27 #include <string.h>
28 
29 #include <gio/gio.h>
30 #include <glib.h>
31 
32 #include "photos-base-manager.h"
33 #include "photos-debug.h"
34 #include "photos-media-server-item.h"
35 #include "photos-search-context.h"
36 #include "photos-source.h"
37 #include "photos-utils.h"
38 
39 
40 struct _PhotosMediaServerItem
41 {
42   PhotosBaseItem parent_instance;
43   PhotosBaseManager *src_mngr;
44 };
45 
46 
47 G_DEFINE_TYPE_WITH_CODE (PhotosMediaServerItem, photos_media_server_item, PHOTOS_TYPE_BASE_ITEM,
48                          photos_utils_ensure_extension_points ();
49                          g_io_extension_point_implement (PHOTOS_BASE_ITEM_EXTENSION_POINT_NAME,
50                                                          g_define_type_id,
51                                                          "media-server",
52                                                          0));
53 
54 
55 static gchar *
photos_media_server_item_create_filename_fallback(PhotosBaseItem * item)56 photos_media_server_item_create_filename_fallback (PhotosBaseItem *item)
57 {
58   g_autoptr (GFile) file = NULL;
59   const gchar *uri;
60   gchar *ret_val;
61 
62   uri = photos_base_item_get_uri (item);
63   file = g_file_new_for_uri (uri);
64   ret_val = g_file_get_basename (file);
65 
66   return ret_val;
67 }
68 
69 
70 static gchar *
photos_media_server_item_create_name_fallback(PhotosBaseItem * item)71 photos_media_server_item_create_name_fallback (PhotosBaseItem *item)
72 {
73   /* TODO: provide a sane fallback */
74   return g_strdup ("");
75 }
76 
77 
78 static gboolean
photos_media_server_item_create_thumbnail(PhotosBaseItem * item,GCancellable * cancellable,GError ** error)79 photos_media_server_item_create_thumbnail (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
80 {
81   g_autoptr (GFile) file = NULL;
82   GQuark orientation;
83   gboolean ret_val;
84   g_autofree gchar *thumbnail_path = NULL;
85   const gchar *mime_type;
86   const gchar *uri;
87   gint64 height;
88   gint64 mtime;
89   gint64 width;
90 
91   uri = photos_base_item_get_uri (item);
92   file = g_file_new_for_uri (uri);
93   mime_type = photos_base_item_get_mime_type (item);
94   mtime = photos_base_item_get_mtime (item);
95   orientation = photos_base_item_get_orientation (item);
96   height = photos_base_item_get_height (item);
97   width = photos_base_item_get_width (item);
98   thumbnail_path = photos_base_item_create_thumbnail_path (item);
99 
100   ret_val = photos_utils_create_thumbnail (file,
101                                            mime_type,
102                                            mtime,
103                                            orientation,
104                                            height,
105                                            width,
106                                            NULL,
107                                            thumbnail_path,
108                                            cancellable,
109                                            error);
110 
111   return ret_val;
112 }
113 
114 
115 static GFile *
photos_media_server_item_download(PhotosBaseItem * item,GCancellable * cancellable,GError ** error)116 photos_media_server_item_download (PhotosBaseItem *item, GCancellable *cancellable, GError **error)
117 {
118   GFile *ret_val = NULL;
119   g_autoptr (GFile) local_file = NULL;
120   g_autoptr (GFile) remote_file = NULL;
121   const gchar *cache_dir;
122   const gchar *uri;
123   g_autofree gchar *local_dir = NULL;
124   g_autofree gchar *local_filename = NULL;
125   g_autofree gchar *local_path = NULL;
126 
127   uri = photos_base_item_get_uri (item);
128   remote_file = g_file_new_for_uri (uri);
129   cache_dir = g_get_user_cache_dir ();
130 
131   local_dir = g_build_filename (cache_dir, PACKAGE_TARNAME, "media-server", NULL);
132   g_mkdir_with_parents (local_dir, 0700);
133 
134   local_filename = g_file_get_basename (remote_file);
135   local_path = g_build_filename (local_dir, local_filename, NULL);
136   local_file = g_file_new_for_path (local_path);
137 
138   if (!g_file_test (local_path, G_FILE_TEST_EXISTS))
139     {
140       photos_debug (PHOTOS_DEBUG_NETWORK, "Downloading %s from Media Server to %s", uri, local_path);
141       if (!g_file_copy (remote_file,
142                         local_file,
143                         G_FILE_COPY_ALL_METADATA | G_FILE_COPY_OVERWRITE,
144                         cancellable,
145                         NULL,
146                         NULL,
147                         error))
148         {
149           g_file_delete (local_file, NULL, NULL);
150           goto out;
151         }
152     }
153 
154   ret_val = g_object_ref (local_file);
155 
156  out:
157   return ret_val;
158 }
159 
160 
161 static GtkWidget *
photos_media_server_item_get_source_widget(PhotosBaseItem * item)162 photos_media_server_item_get_source_widget (PhotosBaseItem *item)
163 {
164   PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (item);
165   GtkWidget *source_widget;
166   const gchar *name;
167 
168   name = photos_utils_get_provider_name (self->src_mngr, item);
169   source_widget = gtk_label_new (name);
170   gtk_widget_set_halign (source_widget, GTK_ALIGN_START);
171 
172   return source_widget;
173 }
174 
175 
176 static void
photos_media_server_item_constructed(GObject * object)177 photos_media_server_item_constructed (GObject *object)
178 {
179   PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (object);
180   const gchar *name;
181 
182   G_OBJECT_CLASS (photos_media_server_item_parent_class)->constructed (object);
183 
184   name = photos_utils_get_provider_name (self->src_mngr, PHOTOS_BASE_ITEM (self));
185   photos_base_item_set_default_app_name (PHOTOS_BASE_ITEM (self), name);
186 }
187 
188 
189 static void
photos_media_server_item_dispose(GObject * object)190 photos_media_server_item_dispose (GObject *object)
191 {
192   PhotosMediaServerItem *self = PHOTOS_MEDIA_SERVER_ITEM (object);
193 
194   g_clear_object (&self->src_mngr);
195 
196   G_OBJECT_CLASS (photos_media_server_item_parent_class)->dispose (object);
197 }
198 
199 
200 static void
photos_media_server_item_init(PhotosMediaServerItem * self)201 photos_media_server_item_init (PhotosMediaServerItem *self)
202 {
203   GApplication *app;
204   PhotosSearchContextState *state;
205 
206   app = g_application_get_default ();
207   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
208 
209   self->src_mngr = g_object_ref (state->src_mngr);
210 }
211 
212 
213 static void
photos_media_server_item_class_init(PhotosMediaServerItemClass * class)214 photos_media_server_item_class_init (PhotosMediaServerItemClass *class)
215 {
216   GObjectClass *object_class = G_OBJECT_CLASS (class);
217   PhotosBaseItemClass *base_item_class = PHOTOS_BASE_ITEM_CLASS (class);
218 
219   base_item_class->miner_name = "org.gnome.OnlineMiners.MediaServer";
220   base_item_class->miner_object_path = "/org/gnome/OnlineMiners/MediaServer";
221 
222   object_class->constructed = photos_media_server_item_constructed;
223   object_class->dispose = photos_media_server_item_dispose;
224   base_item_class->create_filename_fallback = photos_media_server_item_create_filename_fallback;
225   base_item_class->create_name_fallback = photos_media_server_item_create_name_fallback;
226   base_item_class->create_thumbnail = photos_media_server_item_create_thumbnail;
227   base_item_class->download = photos_media_server_item_download;
228   base_item_class->get_source_widget = photos_media_server_item_get_source_widget;
229 }
230