Skip to content

Commit 8cd61dc

Browse files
authored
Merge pull request #1 from ryonakano/m3u-support-suggestion
M3u support suggestion
2 parents 94f4673 + 3bdf7d9 commit 8cd61dc

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

po/POTFILES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ src/PlaybackManager.vala
44
src/Views/NowPlayingView.vala
55
src/Widgets/AlbumImage.vala
66
src/Widgets/SeekBar.vala
7-
src/Services/M3U.vala

src/Application.vala

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,15 @@ public class Music.Application : Gtk.Application {
163163
continue;
164164
}
165165

166-
if ((file_path.ascii_down ().has_suffix (".m3u")) || (file_path.ascii_down ().has_suffix (".m3u8"))) {
167-
foreach (var track in M3U.parse_playlist (file)) {
166+
// Check if the file has M3U suffix: "foo.m3u", "bar.M3U8", etc.
167+
var m3u_suffix = /^.+.m3u8?$/i;
168+
if (m3u_suffix.match (file_path)) {
169+
File[] tracks = M3U.parse_playlist (file);
170+
if (tracks == null) {
171+
continue;
172+
}
173+
174+
foreach (var track in tracks) {
168175
elements += track;
169176
}
170177

@@ -213,7 +220,35 @@ public class Music.Application : Gtk.Application {
213220
}
214221

215222
private void action_save_m3u_playlist () {
216-
M3U.save_playlist ((MainWindow)active_window, playback_manager.queue_liststore);
223+
var save_dialog = new Gtk.FileDialog () {
224+
initial_name = _("New playlist.m3u")
225+
};
226+
227+
save_dialog.save.begin (active_window, null, (obj, res) => {
228+
File? file;
229+
try {
230+
file = save_dialog.save.end (res);
231+
M3U.save_playlist (playback_manager.queue_liststore, file);
232+
} catch (Error err) {
233+
if (err.matches (Gtk.DialogError.quark (), Gtk.DialogError.DISMISSED)) {
234+
return;
235+
}
236+
237+
warning ("Failed to save playlist: %s", err.message);
238+
239+
var dialog = new Granite.MessageDialog (
240+
_("Couldn't save playlist"),
241+
err.message,
242+
new ThemedIcon ("playlist-queue")
243+
) {
244+
badge_icon = new ThemedIcon ("dialog-error"),
245+
modal = true,
246+
transient_for = active_window
247+
};
248+
dialog.present ();
249+
dialog.response.connect (dialog.destroy);
250+
}
251+
});
217252
}
218253

219254
private void on_bus_acquired (DBusConnection connection, string name) {

src/Services/M3U.vala

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Music.M3U {
77

88
// Standard specification here: https://en.wikipedia.org/wiki/M3U
9-
public File[] parse_playlist (File playlist) {
9+
public File[]? parse_playlist (File playlist) {
1010
debug ("Parsing playlist: %s", playlist.get_path ());
1111
File[] list = {};
1212

@@ -16,71 +16,52 @@ namespace Music.M3U {
1616
string line;
1717

1818
while ((line = dis.read_line ()) != null) {
19-
print ("%s\n", line);
19+
debug ("%s", line);
2020

21-
// Skip extended
21+
// Skip extended
2222
if (line.has_prefix ("#EXT")) {
23-
print ("Skipping EXTM3U: " + line + "\n");
24-
25-
} else {
26-
File target;
27-
28-
if (line.ascii_down ().has_prefix ("file:///")) {
29-
target = File.new_for_uri (line);
30-
31-
//FIXME: URL get skipped.
32-
//} else if (line.ascii_down ().has_prefix ("http")) {
33-
// print ("URL are currently unsupported:" + line + "\n");
34-
35-
} else {
36-
target = File.new_for_path (line);
23+
debug ("Skipping EXTM3U: " + line);
24+
continue;
25+
}
3726

38-
};
27+
File target;
3928

40-
// We do not need to test yet whether files exist
41-
list += target;
29+
if (line.ascii_down ().has_prefix ("file:///")) {
30+
target = File.new_for_uri (line);
31+
//FIXME: URL get skipped.
32+
//} else if (line.ascii_down ().has_prefix ("http")) {
33+
// debug ("URL are currently unsupported:" + line);
34+
} else {
35+
target = File.new_for_path (line);
4236
}
43-
}
4437

38+
// We do not need to test yet whether files exist
39+
list += target;
40+
}
4541
} catch (Error e) {
46-
print ("Error: %s\n", e.message);
42+
warning ("Error: %s", e.message);
43+
return null;
4744
}
4845

4946
return list;
50-
5147
}
5248

53-
public void save_playlist (MainWindow parent, ListStore queue_liststore) {
54-
debug ("Saving queue as playlist" + "\n");
49+
public void save_playlist (ListStore queue_liststore, File playlist) throws Error {
50+
debug ("Saving queue as playlist");
5551
string content = "";
5652

5753
for (var i = 0; i < queue_liststore.n_items; i++) {
5854
var item = (Music.AudioObject)queue_liststore.get_item (i);
5955
content = content + item.uri + "\n";
6056
}
6157

62-
var save_dialog = new Gtk.FileDialog () {
63-
initial_name = _("New playlist.m3u")
64-
};
65-
66-
save_dialog.save.begin (parent, null, (obj, res) => {
67-
try {
68-
var file = save_dialog.save.end (res);
69-
var dostream = new DataOutputStream (
70-
file.replace (
71-
null,
72-
false,
73-
GLib.FileCreateFlags.REPLACE_DESTINATION
74-
)
75-
);
76-
58+
try {
59+
var ostream = playlist.replace (null, false, GLib.FileCreateFlags.REPLACE_DESTINATION);
60+
var dostream = new DataOutputStream (ostream);
7761
dostream.put_string (content);
78-
79-
} catch (Error err) {
80-
warning ("Failed to save file: %s", err.message);
81-
}
82-
});
83-
84-
62+
} catch (Error err) {
63+
warning ("Failed to writing to playlist: %s", err.message);
64+
throw err;
65+
}
8566
}
8667
}

0 commit comments

Comments
 (0)