3
public class BeatBox.PreferencesWindow : Window {
4
private BeatBox.LibraryManager _lm;
6
private Gee.LinkedList<string> origLocations;
7
private Gee.LinkedList<string> newLocations;
8
Gee.LinkedList<string> removed; //as we find folders from before, remove them from removed.
10
//for padding around notebook mostly
14
private Notebook notebook;
15
private Viewport generalPage;
16
private Viewport lastfmPage;
18
private ScrolledWindow musicLocationsScroll;
19
private TreeView musicLocationsView;
20
private ListStore musicLocationsModel;
21
private Button musicLocationsAdd;
22
private Button musicLocationsRemove;
23
private CheckButton updateFolderNames;
24
private CheckButton copyImportedMusic;
26
private Button lastfmLogin;
28
private Button saveChanges;
29
private Button cancelChanges;
31
private string lastfm_token;
33
public PreferencesWindow(LibraryManager lm) {
35
origLocations = _lm.settings.getMusicFoldersList();
40
public void buildUI() {
42
set_title("Preferences");
44
this.window_position = WindowPosition.CENTER;
47
set_size_request(400, 300);
50
vert = new VBox(false, 10);
51
horiz = new HBox(false, 10);
52
notebook = new Notebook();
53
generalPage = new Viewport(null, null);
54
lastfmPage = new Viewport(null, null);
56
musicLocationsScroll = new ScrolledWindow(null, null);
57
musicLocationsView = new TreeView();
58
musicLocationsModel = new ListStore(1, typeof(string));
59
musicLocationsAdd = new Button.with_label("Add");
60
musicLocationsRemove = new Button.with_label("Remove");
61
updateFolderNames = new CheckButton.with_label("Update folder names based on Metadata");
62
copyImportedMusic = new CheckButton.with_label("Copy imported music to music folder");
64
Label lastfmInfo = new Label("To allow for scrobbling, love, bans, etc. in Last FM, you must give permission to BeatBox. You only need to do this once. It is a two-step process.");
65
lastfmLogin = new Button.with_label("Enable Scrobbling");
67
saveChanges = new Button.with_label("Save");
68
cancelChanges = new Button.with_label("Cancel");
70
/** build general page **/
71
VBox genVert = new VBox(false, 3);
72
notebook.append_page(generalPage, new Label("General"));
73
generalPage.add(genVert);
75
musicLocationsView.insert_column_with_attributes(-1, "Music Folder", new CellRendererText(), "text", 0, null);
76
musicLocationsView.set_model(musicLocationsModel);
78
foreach(string s in _lm.settings.getMusicFoldersList()) {
80
musicLocationsModel.append(out added);
81
musicLocationsModel.set(added, 0, s);
84
musicLocationsScroll.add(musicLocationsView);
85
musicLocationsScroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
87
VBox musicFolderButtonsBox = new VBox(false, 0);
88
musicFolderButtonsBox.pack_start(musicLocationsAdd, false, false, 0);
89
musicFolderButtonsBox.pack_start(musicLocationsRemove, false, false, 0);
91
HBox musicFolderBox = new HBox(false, 0);
92
musicFolderBox.pack_start(musicLocationsScroll, true, true, 5);
93
musicFolderBox.pack_start(musicFolderButtonsBox, false, false, 0);
95
updateFolderNames.set_active(_lm.settings.getUpdateFolderHierarchy());
96
copyImportedMusic.set_active(_lm.settings.getCopyImportedMusic());
98
genVert.pack_start(musicFolderBox, true, true, 0);
99
genVert.pack_start(updateFolderNames, false, true, 0);
100
genVert.pack_start(copyImportedMusic, false, true, 0);
102
/** build lastfm page **/
103
VBox lastfmVert = new VBox(false, 3);
104
notebook.append_page(lastfmPage, new Label("Last FM"));
105
lastfmPage.add(lastfmVert);
107
lastfmInfo.set_line_wrap(true);
109
lastfmVert.pack_start(lastfmInfo, false, true, 0);
110
lastfmVert.pack_start(lastfmLogin, false, true, 0);
112
lastfmLogin.clicked.connect(lastfmLoginClick);
114
/** Add save and cancel buttons **/
115
HButtonBox bottomButtons = new HButtonBox();
116
bottomButtons.pack_start(cancelChanges, false, false, 0);
117
bottomButtons.pack_end(saveChanges, false, false, 0);
119
/** put it all together **/
120
vert.pack_start(notebook, true, true, 0);
121
vert.pack_start(bottomButtons, false, true, 0);
123
horiz.pack_start(vert, true, true, 10);
128
saveChanges.clicked.connect(saveClicked);
129
cancelChanges.clicked.connect(cancelClicked);
130
musicLocationsAdd.clicked.connect(musicLocationsAddClicked);
131
musicLocationsRemove.clicked.connect(musicLocationsRemoveClicked);
134
public virtual void lastfmLoginClick() {
135
if(_lm.settings.getLastFMSessionKey() != "")
136
stdout.printf("We already have a key this is pointless...\n");
138
if(lastfmLogin.get_label() == "Enable Scrobbling" || lastfmLogin.get_label() == "Unsuccessful. Click to try again.") {
139
lastfm_token = _lm.lfm.getToken();
140
if(lastfm_token == null) {
141
lastfmLogin.set_label("Unsuccessful. Click to try again.");
142
stdout.printf("Could not get a token. check internet connection\n");
145
string auth_uri = "http://www.last.fm/api/auth/?api_key=" + LastFM.Core.api + "&token=" + lastfm_token;
146
GLib.AppInfo.launch_default_for_uri (auth_uri, null);
148
//set button text. we are done this time around. next time we get session key
149
lastfmLogin.set_label("Complete login");
153
if(lastfm_token == null) {
154
lastfmLogin.set_label("Unsuccessful. Click to try again.");
155
stdout.printf("Invalid token. Cannot continue\n");
158
var sk = _lm.lfm.getSessionKey(lastfm_token);
160
lastfmLogin.set_label("Unsuccessful. Click to try again.");
161
stdout.printf("Could not get Last FM session key\n");
164
_lm.settings.setLastFMSessionKey(sk);
165
stdout.printf("Successfully obtained a sessionkey\n");
166
lastfmLogin.set_sensitive(false);
167
lastfmLogin.set_label("Success!");
173
public virtual void musicLocationsAddClicked() {
175
var file_chooser = new FileChooserDialog ("Add Music Folder", this,
176
FileChooserAction.SELECT_FOLDER,
177
Gtk.Stock.CANCEL, ResponseType.CANCEL,
178
Gtk.Stock.OPEN, ResponseType.ACCEPT);
179
if (file_chooser.run () == ResponseType.ACCEPT) {
180
folder = file_chooser.get_filename();
182
file_chooser.destroy ();
186
musicLocationsModel.append(out added);
187
musicLocationsModel.set(added, 0, folder);
191
public virtual void musicLocationsRemoveClicked() {
194
musicLocationsView.get_selection().get_selected(out model, out iter);
196
musicLocationsModel.remove(iter);
199
public bool buildFoldersChanges(TreeModel model, TreePath path, TreeIter iter) {
201
model.get(iter, 0, out loc);
203
newLocations.add(loc);
205
if(loc in origLocations)
211
public virtual void saveClicked() {
212
/** loop through all strings in locations view. if a folder was
213
* removed, remove all those songs first. then go through all the new
214
* folders that were added and add the files from there
217
removed = origLocations; //as we find folders from before, remove them from removed.
218
newLocations = new Gee.LinkedList<string>();
219
musicLocationsModel.foreach(buildFoldersChanges);
221
_lm.settings.setMusicFoldersFromList(newLocations);
223
/* remove songs belonging to the folders that were removed */
224
var removed_songs = new Gee.LinkedList<int>();
225
foreach(Song s in _lm.songs()) {
227
foreach(string removeLoc in removed) {
228
if(removeLoc in s.file) {
230
stdout.printf("removed:%s in %s\n", s.file, removeLoc);
236
removed_songs.add(s.rowid);
239
foreach(int id in removed_songs) {
240
_lm.remove_song_from_id(id);
243
//consolidate files lazily
244
//save new songs to db and reload
247
foreach(Song s in _lm.dbm.load_songs()) {
251
//now rescan the folders
252
_lm.rescan_music_folders();
254
_lm.settings.setUpdateFolderHierarchy(updateFolderNames.get_active());
255
stdout.printf("no setting for copying imported music\n");
259
public virtual void cancelClicked() {