static gboolean add_single_file (BgPicturesSource *bg_source, GFile *file, const gchar *content_type, guint64 mtime, GtkTreeRowReference **ret_row_ref) { CcBackgroundItem *item = NULL; CcBackgroundItemFlags flags = 0; GtkListStore *store; GtkTreeIter iter; GtkTreePath *path = NULL; GtkTreeRowReference *row_ref = NULL; cairo_surface_t *surface = NULL; char *source_uri = NULL; char *uri = NULL; gboolean needs_download; gboolean retval = FALSE; GFile *pictures_dir, *cache_dir; GrlMedia *media; /* find png and jpeg files */ if (!content_type) goto out; if (!in_content_types (content_type)) goto out; /* create a new CcBackgroundItem */ uri = g_file_get_uri (file); pictures_dir = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)); cache_dir = bg_pictures_source_get_cache_file (); needs_download = !g_file_has_parent (file, pictures_dir) && !g_file_has_parent (file, cache_dir); g_object_unref (pictures_dir); g_object_unref (cache_dir); if (!needs_download) { source_uri = g_strdup (uri); flags |= CC_BACKGROUND_ITEM_HAS_URI; } else { source_uri = uri; uri = NULL; } item = cc_background_item_new (uri); flags |= CC_BACKGROUND_ITEM_HAS_SHADING | CC_BACKGROUND_ITEM_HAS_PLACEMENT; g_object_set (G_OBJECT (item), "flags", flags, "shading", G_DESKTOP_BACKGROUND_SHADING_SOLID, "placement", G_DESKTOP_BACKGROUND_STYLE_ZOOM, "modified", mtime, "needs-download", needs_download, "source-url", source_uri, NULL); if (!ret_row_ref && in_screenshot_types (content_type)) goto read_file; surface = get_content_loading_icon (BG_SOURCE (bg_source)); store = bg_source_get_liststore (BG_SOURCE (bg_source)); /* insert the item into the liststore */ gtk_list_store_insert_with_values (store, &iter, -1, 0, surface, 1, item, -1); path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter); row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path); g_object_set_data_full (G_OBJECT (item), "row-ref", row_ref, (GDestroyNotify) gtk_tree_row_reference_free); read_file: media = g_object_get_data (G_OBJECT (file), "grl-media"); if (media == NULL) { g_object_set_data_full (G_OBJECT (file), "item", g_object_ref (item), g_object_unref); g_file_read_async (file, G_PRIORITY_DEFAULT, bg_source->priv->cancellable, picture_opened_for_read, bg_source); } else { GFile *native_file; GFile *thumbnail_file = NULL; gchar *native_dir; gchar *native_path; const gchar *title; const gchar *thumbnail_uri; title = grl_media_get_title (media); g_object_set (G_OBJECT (item), "name", title, NULL); thumbnail_uri = grl_media_get_thumbnail (media); thumbnail_file = g_file_new_for_uri (thumbnail_uri); native_path = gnome_desktop_thumbnail_path_for_uri (source_uri, GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); native_file = g_file_new_for_path (native_path); native_dir = g_path_get_dirname (native_path); g_mkdir_with_parents (native_dir, USER_DIR_MODE); g_object_set_data_full (G_OBJECT (thumbnail_file), "item", g_object_ref (item), g_object_unref); g_object_set_data_full (G_OBJECT (thumbnail_file), "native-file", g_object_ref (native_file), g_object_unref); g_file_copy_async (thumbnail_file, native_file, G_FILE_COPY_ALL_METADATA, G_PRIORITY_DEFAULT, bg_source->priv->cancellable, NULL, NULL, picture_copied_for_read, bg_source); g_clear_object (&thumbnail_file); g_object_unref (native_file); g_free (native_dir); g_free (native_path); } retval = TRUE; out: if (ret_row_ref) { if (row_ref && retval != FALSE) *ret_row_ref = gtk_tree_row_reference_copy (row_ref); else *ret_row_ref = NULL; } gtk_tree_path_free (path); g_clear_pointer (&surface, (GDestroyNotify) cairo_surface_destroy); g_clear_object (&item); g_object_unref (file); g_free (source_uri); g_free (uri); return retval; }
static void store_bookmark (GrlBookmarksSource *bookmarks_source, GList **keylist, GrlMediaBox *parent, GrlMedia *bookmark, GError **error) { GomResource *resource; const gchar *title; const gchar *url; const gchar *desc; const gchar *thumb; GTimeVal now; gint64 parent_id; const gchar *mime; gchar *date; guint type; gint64 id; gchar *str_id; GError *local_error = NULL; gboolean ret; GRL_DEBUG ("store_bookmark"); title = grl_media_get_title (bookmark); url = grl_media_get_url (bookmark); thumb = grl_media_get_thumbnail (bookmark); desc = grl_media_get_description (bookmark); mime = grl_media_get_mime (bookmark); g_get_current_time (&now); date = g_time_val_to_iso8601 (&now); if (!parent) { parent_id = 0; } else { parent_id = g_ascii_strtoll (grl_media_get_id (GRL_MEDIA (parent)), NULL, 0); } if (parent_id < 0) { parent_id = 0; } GRL_DEBUG ("URL: '%s'", url); if (GRL_IS_MEDIA_BOX (bookmark)) { type = BOOKMARK_TYPE_CATEGORY; } else { type = BOOKMARK_TYPE_STREAM; } resource = g_object_new (BOOKMARKS_TYPE_RESOURCE, "repository", bookmarks_source->priv->repository, "parent", parent_id, "type", type, NULL); if (type == BOOKMARK_TYPE_STREAM) { g_object_set (G_OBJECT (resource), "url", url, NULL); *keylist = g_list_remove (*keylist, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_URL)); } if (title) { g_object_set (G_OBJECT (resource), "title", title, NULL); *keylist = g_list_remove (*keylist, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_TITLE)); } else if (url) { g_object_set (G_OBJECT (resource), "title", url, NULL); } else { g_object_set (G_OBJECT (resource), "title", "(unknown)", NULL); } if (date) { g_object_set (G_OBJECT (resource), "date", date, NULL); } if (mime) { g_object_set (G_OBJECT (resource), "mime", mime, NULL); *keylist = g_list_remove (*keylist, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_MIME)); } if (desc) { g_object_set (G_OBJECT (resource), "desc", desc, NULL); *keylist = g_list_remove (*keylist, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_DESCRIPTION)); } if (thumb) { g_object_set (G_OBJECT (resource), "thumbnail-url", desc, NULL); *keylist = g_list_remove (*keylist, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_THUMBNAIL)); } ret = gom_resource_save_sync (resource, &local_error); if (!ret) { GRL_WARNING ("Failed to store bookmark '%s': %s", title, local_error->message); *error = g_error_new (GRL_CORE_ERROR, GRL_CORE_ERROR_STORE_FAILED, _("Failed to store: %s"), local_error->message); g_error_free (local_error); g_object_unref (resource); return; } g_object_get (resource, "id", &id, NULL); str_id = g_strdup_printf ("%" G_GINT64_FORMAT, id); grl_media_set_id (bookmark, str_id); g_free (str_id); g_object_unref (resource); if (bookmarks_source->priv->notify_changes) { grl_source_notify_change (GRL_SOURCE (bookmarks_source), bookmark, GRL_CONTENT_ADDED, FALSE); } }