audrey/src/application.vala

147 lines
4.9 KiB
Vala
Raw Normal View History

2024-10-06 11:21:53 +00:00
/* application.vala
*
* Copyright 2024 Erica Z
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
2024-10-10 20:04:55 +00:00
Gst.Element playbin;
2024-10-11 09:09:47 +00:00
Wavelet.Subsonic public_api;
2024-10-06 11:21:53 +00:00
public class Wavelet.Application : Adw.Application {
public Application () {
Object (
application_id: "eu.callcc.Wavelet",
flags: ApplicationFlags.DEFAULT_FLAGS
);
}
construct {
ActionEntry[] action_entries = {
{ "about", this.on_about_action },
{ "preferences", this.on_preferences_action },
{ "quit", this.quit }
};
this.add_action_entries (action_entries, this);
this.set_accels_for_action ("app.quit", {"<primary>q"});
}
public override void activate () {
base.activate ();
// FIXME edit back if issues var win = this.active_window ?? new Wavelet.Window (this);
var win = new Wavelet.Window (this);
win.present ();
2024-10-11 06:57:01 +00:00
playbin = Gst.ElementFactory.make ("playbin3", null);
assert (playbin != null);
win.notify["volume"].connect ((s, p) => {
// gst docs: Volume sliders should usually use a cubic volume.
((Gst.Audio.StreamVolume) playbin).set_volume (Gst.Audio.StreamVolumeFormat.CUBIC, win.volume);
});
win.mute.clicked.connect (() => {
var vol = (Gst.Audio.StreamVolume) playbin;
if (vol.get_mute ()) {
win.show_unmute ();
vol.set_mute (false);
} else {
win.show_mute ();
vol.set_mute (true);
}
});
Timeout.add (100, () => {
int64 position_ns;
if (playbin.query_position (Gst.Format.TIME, out position_ns)) {
2024-10-11 08:22:05 +00:00
win.play_position_ms = (int) (position_ns / 1000000);
2024-10-11 06:57:01 +00:00
}
return Source.CONTINUE;
});
playbin.get_bus ().add_watch (Priority.DEFAULT, (bus, message) => {
switch (message.type) {
case Gst.MessageType.ASYNC_DONE:
win.play_position.sensitive = true;
int64 duration_ns;
2024-10-11 09:09:47 +00:00
if (playbin.query_duration (Gst.Format.TIME, out duration_ns)) {
win.play_position_ms = 0;
win.play_duration_ms = (int) (duration_ns / 1000000);
} else {
warning ("could not query playbin duration after ASYNC_DONE");
}
2024-10-11 06:57:01 +00:00
break;
default:
break;
}
return true;
});
2024-10-11 07:53:30 +00:00
win.setup.connected.connect ((api) => {
2024-10-11 09:09:47 +00:00
public_api = api;
2024-10-11 07:53:30 +00:00
win.shuffle_all_tracks.sensitive = true;
win.shuffle_all_tracks.activated.connect (() => {
win.shuffle_all_tracks.sensitive = false;
win.play_queue.clear ();
api.get_random_songs.begin (null, (song) => {
win.play_queue.queue (song);
}, (obj, res) => {
2024-10-11 09:09:47 +00:00
try {
api.get_random_songs.end (res);
} catch (Error e) {
error ("could not get random songs: %s", e.message);
}
2024-10-11 07:53:30 +00:00
win.shuffle_all_tracks.sensitive = true;
});
});
win.play_queue.play_now.connect ((song) => {
win.play_position.sensitive = false;
playbin.set_state (Gst.State.READY);
playbin.set ("uri", api.stream_uri (song.id));
playbin.set_state (Gst.State.PLAYING);
win.song = song;
});
});
2024-10-11 09:20:42 +00:00
win.setup.load ();
2024-10-06 11:21:53 +00:00
}
private void on_about_action () {
string[] developers = { "Erica Z" };
var about = new Adw.AboutDialog () {
application_name = "wavelet",
application_icon = "eu.callcc.Wavelet",
developer_name = "Erica Z",
translator_credits = _("translator-credits"),
version = "0.1.0",
developers = developers,
copyright = "© 2024 Erica Z",
};
about.present (this.active_window);
}
private void on_preferences_action () {
message ("app.preferences action activated");
}
}