Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mv): enable caching for clapper #1010

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build-aux/clapper/clapper.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"type": "git",
"url": "https://github.com/Rafostar/clapper.git",
"branch": "master"
}
},
{
"type": "shell",
"commands": ["sed -i \"s/version: '0.7.0',/version: '0.8.0',/g\" meson.build"]
}
]
}
}
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ if clapper_support and clapper_dep.found () and clapper_dep.version().version_co
if (clapper_dep.get_variable('features').split().contains('mpris'))
add_project_arguments(['--define=CLAPPER_MPRIS'], language: 'vala')
endif

if clapper_dep.version().version_compare('>=0.8.0')
add_project_arguments(['--define=CLAPPER_0_8'], language: 'vala')
endif
endif

if gtk_dep.version().version_compare('>=4.14')
Expand Down
84 changes: 83 additions & 1 deletion src/Views/MediaViewer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,18 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
}
}

#if CLAPPER_0_8
string clapper_cache_dir;
Gee.HashMap<string, string> clapper_cached_urls;
#endif
construct {
#if CLAPPER
#if CLAPPER_0_8
clapper_cached_urls = new Gee.HashMap<string, string> ();
clapper_cache_dir = GLib.Path.build_path (GLib.Path.DIR_SEPARATOR_S, Tuba.cache_path, "clapper");
clapper_cache_cleanup (true);
#endif

// Clapper can have > 1.0 volumes
last_used_volume = settings.media_viewer_last_used_volume;
#else
Expand Down Expand Up @@ -546,6 +556,11 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
~MediaViewer () {
debug ("Destroying MediaViewer");
context_menu.unparent ();
#if CLAPPER_0_8
clapper_cache_cleanup (true);
clapper_cached_urls.clear ();
clapper_cached_locations.clear ();
#endif
}

private void on_visible_toggle () {
Expand Down Expand Up @@ -899,6 +914,10 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {

items.clear ();
revealed = false;

#if CLAPPER_0_8
clapper_cache_cleanup ();
#endif
}

private void update_revealer_widget (int pos = -1) {
Expand Down Expand Up @@ -944,6 +963,13 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
var video = new ClapperGtk.Video () {
auto_inhibit = true
};

#if CLAPPER_0_8
video.player.download_dir = clapper_cache_dir;
video.player.download_enabled = true;
video.player.download_complete.connect (on_clapper_download_complete);
#endif

#if CLAPPER_MPRIS
var mpris = new Clapper.Mpris (
"org.mpris.MediaPlayer2.Tuba",
Expand Down Expand Up @@ -984,7 +1010,13 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
item = new Item (video, final_friendly_url, final_preview, true);

#if CLAPPER
var clp_item = new Clapper.MediaItem (url);
Clapper.MediaItem clp_item;
#if CLAPPER_0_8
clp_item = new Clapper.MediaItem.cached (url, clapper_cached_urls.get (url));
#else
clp_item = new Clapper.MediaItem (url);
#endif

video.player.queue.add_item (clp_item);
video.player.queue.select_item (clp_item);
add_todo_item (item);
Expand Down Expand Up @@ -1037,6 +1069,56 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
if (as_is || stream) add_todo_item (item);
}

#if CLAPPER_0_8
// libgee's hashmaps do not preserve insertion order.
// We need the order to know what we should cleanup
// (older items get cleaned up first).
Gee.ArrayList<string> clapper_cached_locations = new Gee.ArrayList<string> ();
private void on_clapper_download_complete (Clapper.MediaItem clp_media_item, string location) {
clapper_cached_urls.set (clp_media_item.uri, location);
clapper_cached_locations.add (location);
}

private void clapper_cache_cleanup (bool all = false) {
if (!all) {
int to_remove = clapper_cached_locations.size - 3; // TODO: make it 10 before merging
if (to_remove > 0) {
var sliced = clapper_cached_locations.slice (0, to_remove);
clapper_cached_locations.remove_all (sliced);

clapper_cached_urls.foreach (e => {
if (!clapper_cached_locations.contains (e.value))
clapper_cached_urls.unset (e.key);

return true;
});
}
}

try {
Dir dir = Dir.open (clapper_cache_dir, 0);
string? name = null;

while ((name = dir.read_name ()) != null) {
string path = Path.build_filename (clapper_cache_dir, name);
if (!all && clapper_cached_locations.contains (path)) continue;

File file = File.new_for_path (path);
file.delete_async.begin (Priority.LOW, null, (obj, res) => {
try {
file.delete_async.end (res);
} catch (Error e) {
warning (@"Error while deleting Clapper's cache: $(e.message)");
}
});
}
} catch (FileError e) {
warning (@"Error while opening Clapper's cache: $(e.message)");
}
return;
}
#endif

private Gee.ArrayList<string> todo_items = new Gee.ArrayList<string> ();
private void add_todo_item (Item todo_item) {
if (revealed) {
Expand Down
Loading