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

Play video from folder #122

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public class Torrential.MainWindow : Gtk.Window {
list_box.torrent_removed.connect ((torrent) => torrent_manager.remove_torrent (torrent));
list_box.open_torrent.connect ((id) => torrent_manager.open_torrent (id));
list_box.open_torrent_location.connect ((id) => torrent_manager.open_torrent_location (id));
list_box.open_torrent_file.connect ((id, file_name) => torrent_manager.open_torrent_file (id, file_name));
list_box.link_copied.connect (on_link_copied);
list_box_scroll = new Gtk.ScrolledWindow (null, null);
list_box_scroll.add (list_box);
Expand Down
54 changes: 37 additions & 17 deletions src/TorrentManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -274,23 +274,11 @@ public class Torrential.TorrentManager : Object {
if (torrent.stat.activity == Transmission.Activity.SEED && torrent.info.files.length == 1) {
var files = torrent.info.files;
if (files != null && files.length > 0) {
bool certain = false;
var content_type = ContentType.guess (files[0].name, null, out certain);
var appinfo = AppInfo.get_default_for_type (content_type, true);
if (appinfo != null) {
var path = Path.build_path (Path.DIR_SEPARATOR_S, torrent.download_dir, files[0].name);
var file = File.new_for_path (path);
if (file.query_exists ()) {
var file_list = new List<string> ();
file_list.append (file.get_uri ());
try {
appinfo.launch_uris (file_list, null);
return;
} catch (Error e) {
warning ("Unable to launch default handler for %s, falling back to file manager", content_type);
open_torrent_location (torrent_id);
}
}
var file_name = files[0].name;
var path = Path.build_path (Path.DIR_SEPARATOR_S, torrent.download_dir, file_name);
var success = open_file (path);
if (success) {
return;
}
}
}
Expand Down Expand Up @@ -353,6 +341,38 @@ public class Torrential.TorrentManager : Object {
}
}

public void open_torrent_file (int torrent_id, string file_name) {
foreach (unowned Transmission.Torrent torrent in added_torrents) {
if (torrent.id == torrent_id) {
if (torrent.stat.activity == Transmission.Activity.SEED) {
var path = Path.build_path (Path.DIR_SEPARATOR_S, torrent.download_dir, file_name);
open_file (path);
}
break;
}
}
}

private bool open_file (string file_path) {
bool certain = false;
var content_type = ContentType.guess (file_path, null, out certain);
var appinfo = AppInfo.get_default_for_type (content_type, true);
if (appinfo != null) {
var file = File.new_for_path (file_path);
if (file.query_exists ()) {
var file_list = new List<string> ();
file_list.append (file.get_uri ());
try {
appinfo.launch_uris (file_list, null);
return true;
} catch (Error e) {
warning ("Unable to launch default handler for %s", content_type);
}
}
}
return false;
}

public float get_overall_progress () {
if (added_torrents.size == 0) {
return 0.0f;
Expand Down
20 changes: 18 additions & 2 deletions src/Widgets/TorrentListBox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Torrential.Widgets.TorrentListBox : Gtk.ListBox {
public signal void torrent_removed (Torrent torrent);
public signal void open_torrent (int id);
public signal void open_torrent_location (int id);
public signal void open_torrent_file (int id, string file_name);
public signal void link_copied ();

public enum FilterType {
Expand Down Expand Up @@ -134,6 +135,15 @@ public class Torrential.Widgets.TorrentListBox : Gtk.ListBox {
}
});

var play_item = new Gtk.MenuItem.with_label (_("Play video"));
play_item.activate.connect (() => {
var selected_row = get_selected_row () as TorrentListRow;
if (selected_row != null) {
var file_name = selected_row.playable_file_name;
open_torrent_file (selected_row.id, file_name);
}
});

var open_item = new Gtk.MenuItem.with_label (_("Show in File Browser"));
open_item.activate.connect (() => {
var selected_row = get_selected_row ();
Expand Down Expand Up @@ -161,8 +171,14 @@ public class Torrential.Widgets.TorrentListBox : Gtk.ListBox {
if (items.length () < 2) {
var selected_row = get_selected_row () as TorrentListRow;

if (selected_row != null && selected_row.multi_file_torrent) {
menu.add (edit_files_item);
if (selected_row != null) {
if (selected_row.multi_file_torrent) {
menu.add (edit_files_item);
}

if (selected_row.is_playable) {
menu.add (play_item);
}
}

menu.add (copy_magnet_item);
Expand Down
22 changes: 22 additions & 0 deletions src/Widgets/TorrentListRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Torrential.Widgets.TorrentListRow : Gtk.ListBoxRow {
return torrent.file_count > 1;
}
}
public bool is_playable;
public string playable_file_name;

public TorrentListRow (Torrent torrent) {
this.torrent = torrent;
Expand Down Expand Up @@ -124,6 +126,26 @@ public class Torrential.Widgets.TorrentListRow : Gtk.ListBoxRow {
} else {
progress.get_style_context ().remove_provider (green_progress_provider);
}

// check for playable files
if (torrent.files != null && torrent.progress == 1 && !is_playable) {
for (int i = 0; i < torrent.file_count; i++) {
var file = torrent.files [i];
bool certain = false;
var content_type = ContentType.guess (file.name, null, out certain);
// TODO What if guessed content_type is invalid?
var super_type = content_type.split ("/")[0];
switch (super_type) {
case "video":
is_playable = true;
// store first playable file
if (playable_file_name == null) {
playable_file_name = file.name;
}
break;
}
}
}
}

public void edit_files () {
Expand Down