Skip to content

Commit beced64

Browse files
authored
Restore last played (#814)
1 parent d048a6d commit beced64

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

data/music.gschema.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<summary>The queue from last session to restore</summary>
1818
<description>An array of strings representing the files played last</description>
1919
</key>
20+
<key type="s" name="uri-last-played">
21+
<default>''</default>
22+
<summary>The last song played by Music</summary>
23+
<description>A string representing the uri of the last played music file</description>
24+
</key>
2025

2126
<key name="window-height" type="i">
2227
<default>475</default>

src/AudioObject.vala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public class Music.AudioObject : Object {
1515
public AudioObject (string uri) {
1616
Object (uri: uri);
1717
}
18+
19+
public static bool equal_func (AudioObject a, AudioObject b) {
20+
return (a.uri == b.uri);
21+
}
1822
}

src/PlaybackManager.vala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@ public class Music.PlaybackManager : Object {
530530
update_next_previous_sensitivity ();
531531

532532
play_pause_action.set_enabled (current_audio != null);
533+
534+
var uri_last_played = current_audio != null ? current_audio.uri : "";
535+
settings.set_string ("uri-last-played", uri_last_played);
533536
}
534537

535538
private void save_queue () {
@@ -544,6 +547,10 @@ public class Music.PlaybackManager : Object {
544547
}
545548

546549
public void restore_queue () {
550+
// Restoring the queue overwrites the last played. So we need to retrieve it before taking care of the queue
551+
var uri_last_played = settings.get_string ("uri-last-played");
552+
var file_last_played = File.new_for_uri (uri_last_played);
553+
547554
var last_session_uri = settings.get_strv ("previous-queue");
548555
var last_session_files = new File[last_session_uri.length];
549556

@@ -555,5 +562,19 @@ public class Music.PlaybackManager : Object {
555562

556563
var files_to_play = Application.loop_through_files (last_session_files);
557564
queue_files (files_to_play);
565+
566+
if (uri_last_played != "" && file_last_played.query_exists ()) {
567+
var audio_object = new AudioObject (uri_last_played);
568+
uint position = -1;
569+
if (!queue_liststore.find_with_equal_func (
570+
audio_object,
571+
(EqualFunc<AudioObject>) AudioObject.equal_func,
572+
out position
573+
)) {
574+
return;
575+
}
576+
577+
current_audio = (AudioObject) queue_liststore.get_item (position);
578+
}
558579
}
559580
}

0 commit comments

Comments
 (0)