-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlist.vala
339 lines (285 loc) · 7.67 KB
/
list.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Gtk;
using TagLib;
namespace BabeList
{
private const Gtk.TargetEntry[] targets = {
{"text/uri-list",0,0}
};
public class BList : Gtk.ScrolledWindow
{
public int c;
private Gtk.ListStore main_list;
private Gtk.CellRendererText main_list_cell;
private Gtk.TreeView main_list_view;
public Gtk.TreeIter iter;
private Gtk.FileChooserDialog chooser;
public Gtk.EventBox add_music_event;
public Gtk.Stack stack;
public string title;
public string artist;
public string album;
public string song;
public string next_song;
private Gtk.TreeModel model;
public BList(bool state, string playlist_path)//whether the list has to be populate from start//useful for saved playlists(true=populate/false=start empty, the path to the playlist)
{
Object(hadjustment: null, vadjustment: null);
c=0;
this.set_min_content_height(200);
this.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
Gtk.drag_dest_set (this,Gtk.DestDefaults.ALL, targets, Gdk.DragAction.COPY);
this.drag_data_received.connect(on_drag_data_received);
main_list = new Gtk.ListStore (5, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string));
main_list_cell = new Gtk.CellRendererText ();
main_list_view= new Gtk.TreeView.with_model (main_list);
main_list_view.insert_column_with_attributes (-1, "Title", main_list_cell, "text", 0);
main_list_view.set_grid_lines (TreeViewGridLines.BOTH);
main_list_view.set_reorderable(true);
main_list_view.set_headers_visible(false);
main_list_view.set_enable_search(true);
main_list_view.row_activated.connect(this.on_row_activated);
//var label=new Gtk.Label("Add Music");// use this in future if i can make it use the font wanted
/*
* EVENTS
* */
this.add_events (Gdk.EventType.BUTTON_PRESS);
main_list_view.add_events (Gdk.EventType.BUTTON_PRESS);
var add_music_img = new Gtk.Image();
add_music_img.set_from_file("img/add.png");
add_music_event = new Gtk.EventBox();
add_music_event.add(add_music_img);
add_music_event.button_press_event.connect (() => {
on_open();
return true;
});
stack=new Gtk.Stack();
stack.set_vexpand(true);//main list
stack.set_vexpand(true);//add music event/message
stack.add_named(add_music_event, "add");
stack.add_named(main_list_view, "list");
if((state)&&playlist_path.strip().length>0)
{
populate_playlist( playlist_path);
}
GLib.Timeout.add (1000, (SourceFunc) this.check_list);
this.add(stack);
}
private void on_row_activated (Gtk.TreeView treeview, Gtk.TreePath path, Gtk.TreeViewColumn column) //double click starts playback
{
model=main_list_view.get_model();
if (treeview.model.get_iter (out iter, path)) {
model.get (iter,
4, out title,
1, out artist,
2, out song,
3, out album);
}
//print(title+" by "+artist+" @ "+song);
}
public string get_next_song()
{
model=main_list_view.get_model();
if(model.iter_next (ref iter))
{
model.get (iter,
4, out title,
1, out artist,
2, out song,
3, out album);
print("Playing next song->\n");
print("Upcoming song: "+title+"\n");
return song;
}else
{
return "null";
}
}
public BList get_BList_object()
{
return this;
}
public string get_song()
{
return song;
}
public string get_artist()
{
return artist;
}
public string get_title()
{
return title;
}
public string get_album()
{
return album;
}
public void on_open()
{
chooser = new Gtk.FileChooserDialog (
"Select your favorite file", null, Gtk.FileChooserAction.OPEN,
"_Cancel",
Gtk.ResponseType.CANCEL,
"_Open",
Gtk.ResponseType.ACCEPT);
chooser.select_multiple = true;
Gtk.FileFilter filter = new Gtk.FileFilter ();
filter.set_filter_name ("Audio");
filter.add_pattern ("*.mp3");
filter.add_pattern ("*.flac");
chooser.add_filter (filter);
if (chooser.run () == Gtk.ResponseType.ACCEPT)
{
SList<string> uris = chooser.get_uris ();
//stdout.printf ("Selection on_open:\n");
foreach (unowned string uri in uris)
{
populate(uri);
}
}
chooser.close ();
}
public void modify_c()
{
c--;
check_list();
}
public int get_c()
{
return c;
}
public async void populate(string uri)
{
var file = GLib.File.new_for_uri (uri); //check if file exists
var type = GLib.ContentType.guess(uri, null,null); //check if file is audio file
if (file.query_exists()&& type.contains("audio"))
{
main_list.append (out iter);
main_list.set (iter, 0, get_song_info(uri).tag.title+"\nby "+ get_song_info(uri).tag.artist, 1, get_song_info(uri).tag.artist, 2, uri, 3,get_song_info(uri).tag.album,4,get_song_info(uri).tag.title);
c++;
}else
{
warning("no existe");
}
check_list();
}
public TagLib.File get_song_info(string uri)//it actually turns a uri into a path to be able to get the tags
{
var gfile = GLib.File.new_for_uri (uri);
string nm=gfile.get_path() ;
var info = new TagLib.File(nm);
return info;
}
public Gtk.TreeView get_treeview()
{
return main_list_view;
}
public Gtk.ListStore get_liststore()
{
return main_list;
}
public bool list_is_empty()
{
if(c==0)
{
return true;
}else
{
//print(c.to_string()+"//");
return false;
}
}
public void check_list()
{
if(list_is_empty())
{
stack.set_visible_child_name("add");
}else
{
stack.set_visible_child_name("list");
}
}
private async void on_drag_data_received (Gdk.DragContext drag_context, int x, int y,
Gtk.SelectionData data, uint info, uint time)
{
//loop through list of URIs
foreach(string uri in data.get_uris ())
{
var file = GLib.File.new_for_uri (uri);
if(file.query_file_type (0) == GLib.FileType.REGULAR )
{
populate(uri);
}
if(file.query_file_type (0) == GLib.FileType.DIRECTORY )
{
print("es un directorio");
try
{
var directory = GLib.File.new_for_uri (uri);
var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
FileInfo file_info;
while ((file_info = enumerator.next_file ()) != null)
{
GLib.File child = enumerator.get_child (file_info);
populate(child.get_uri());
}
} catch (Error e)
{
stderr.printf ("Error: %s\n", e.message);
}
}
}
Gtk.drag_finish (drag_context, true, false, time);
}
public void populate_playlist(string playlist_path)
{
//babes_list.clear();
//var file = File.new_for_path (".Babes.txt");
var file = GLib.File.new_for_path (playlist_path);
if (!(file.query_exists()))
{
file.create (FileCreateFlags.NONE);
}
var dis = new DataInputStream (file.read());
string line;
// Read lines until end of file (null) is reached
while ((line = dis.read_line (null)) != null) {
//stdout.printf ("%s\n", line);
file=GLib.File.new_for_uri(line);
if(file.query_exists())
{
populate(line);
}else
{
print("File missing: "+line+"\n");
}
}
}
public void clean_list()
{
get_liststore().clear();
c=0;
}
public void proff()
{
print("proof/!");
}
}
}
/*
static int main(string[] args)
{
Gtk.init(ref args);
BList empty_list=new BList(false, "");
var babe_list= new BList(true, ".Babes.txt");
var window=new Gtk.Window();
var box=new Gtk.Box(Gtk.Orientation.VERTICAL,0);
empty_list.playlist();
box.add(empty_list);
box.add(babe_list);
window.add(box);
window.show_all();
Gtk.main();
return 0;
}
*/