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

Add music file[s] from within app #766

Merged
merged 9 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 48 additions & 1 deletion src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class Music.Application : Gtk.Application {
public const string ACTION_PLAY_PAUSE = "action-play-pause";
public const string ACTION_PREVIOUS = "action-previous";
public const string ACTION_SHUFFLE = "action-shuffle";
public const string ACTION_OPEN = "action-open";

private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_PLAY_PAUSE, action_play_pause, null, "false" },
{ ACTION_NEXT, action_next },
{ ACTION_PREVIOUS, action_previous },
{ ACTION_SHUFFLE, action_shuffle }
{ ACTION_SHUFFLE, action_shuffle },
{ ACTION_OPEN, action_open }
};

private PlaybackManager? playback_manager = null;
Expand Down Expand Up @@ -175,6 +177,51 @@ public class Music.Application : Gtk.Application {
playback_manager.shuffle ();
}

private void action_open () {
var all_files_filter = new Gtk.FileFilter () {
name = _("All files"),
};
all_files_filter.add_pattern ("*");
var music_files_filter = new Gtk.FileFilter () {
name = _("Music Files"),
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
};
music_files_filter.add_mime_type ("audio/*");

var filter_model = new ListStore (typeof (Gtk.FileFilter));
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
filter_model.append (all_files_filter);
filter_model.append (music_files_filter);

var file_chooser = new Gtk.FileChooserNative (
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
_("Open some files"),
active_window,
Gtk.FileChooserAction.OPEN,
_("Open"),
_("Cancel")
);
file_chooser.add_filter (music_files_filter);
file_chooser.add_filter (all_files_filter);
file_chooser.select_multiple = true;

file_chooser.response.connect ((response) => {
SList<weak File> file_list = null;
if (response == Gtk.ResponseType.ACCEPT) {
var files = file_chooser.get_files ();
File? file;
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
int index = 0;
while (files.get_item (index) != null) {
file_list.prepend ((File)(files.get_item (index)));
index++;
}

((MainWindow)active_window).queue_files (file_list);
}

file_chooser.destroy ();
});

file_chooser.show ();
}

private void on_bus_acquired (DBusConnection connection, string name) {
try {
connection.register_object ("/org/mpris/MediaPlayer2", new MprisRoot ());
Expand Down
55 changes: 37 additions & 18 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,26 @@ public class Music.MainWindow : Gtk.ApplicationWindow {

var drop_target = new Gtk.DropTarget (typeof (Gdk.FileList), Gdk.DragAction.COPY);

var add_button_label = new Gtk.Label (_("Add Music…"));
var add_button_box = new Gtk.Box (HORIZONTAL, 0);
add_button_box.append (new Gtk.Image.from_icon_name ("list-add-symbolic"));
add_button_box.append (add_button_label);

var add_button = new Gtk.Button () {
child = add_button_box,
action_name = Application.ACTION_PREFIX + Application.ACTION_OPEN
};
add_button.add_css_class (Granite.STYLE_CLASS_FLAT);

var queue_action_bar = new Gtk.ActionBar ();
queue_action_bar.pack_start (add_button);
queue_action_bar.add_css_class (Granite.STYLE_CLASS_FLAT);

var queue = new Gtk.Grid ();
queue.add_css_class (Granite.STYLE_CLASS_VIEW);
queue.attach (queue_header, 0, 0);
queue.attach (scrolled, 0, 1);
queue.attach (queue_action_bar, 0, 2);
queue.add_controller (drop_target);

var error_toast = new Granite.Toast ("");
Expand Down Expand Up @@ -115,24 +131,8 @@ public class Music.MainWindow : Gtk.ApplicationWindow {

drop_target.drop.connect ((target, value, x, y) => {
if (value.type () == typeof (Gdk.FileList)) {
File[] files;
SList<File> file_list = null;
foreach (unowned var file in (SList<File>) value.get_boxed ()) {
var file_type = file.query_file_type (FileQueryInfoFlags.NONE);
if (file_type == FileType.DIRECTORY) {
prepend_directory_files (file, ref file_list);
} else {
file_list.prepend (file);
}
}

file_list.reverse ();
foreach (unowned var file in file_list) {
files += file;
}

playback_manager.queue_files (files);

var list = (Gdk.FileList)value;
queue_files (list.get_files ());
return true;
}

Expand Down Expand Up @@ -161,6 +161,25 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
});
}

public void queue_files (SList<weak File> files) {
File[] file_array = {};
SList<File> file_list = null;
foreach (unowned var file in files) {
var file_type = file.query_file_type (FileQueryInfoFlags.NONE);
if (file_type == FileType.DIRECTORY) {
prepend_directory_files (file, ref file_list);
} else {
file_list.prepend (file);
}
}

file_list.reverse ();
foreach (unowned var file in file_list) {
file_array += file;
}

PlaybackManager.get_default ().queue_files (file_array);
}
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
//Array concatenation not permitted for parameters so use a list instead
private void prepend_directory_files (GLib.File dir, ref SList<File> file_list) {
try {
Expand Down