~voluntatefaber/beat-box/bug952329

« back to all changes in this revision

Viewing changes to src/Dialogs/PreferencesWindow.vala

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gtk;
 
2
 
 
3
public class BeatBox.PreferencesWindow : Window {
 
4
        private BeatBox.LibraryManager _lm;
 
5
        
 
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.
 
9
        
 
10
        //for padding around notebook mostly
 
11
        private VBox vert;
 
12
        private HBox horiz;
 
13
        
 
14
        private Notebook notebook;
 
15
        private Viewport generalPage;
 
16
        private Viewport lastfmPage;
 
17
        
 
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;
 
25
        
 
26
        private Button lastfmLogin;
 
27
        
 
28
        private Button saveChanges;
 
29
        private Button cancelChanges;
 
30
        
 
31
        private string lastfm_token;
 
32
        
 
33
        public PreferencesWindow(LibraryManager lm) {
 
34
                this._lm = lm;
 
35
                origLocations = _lm.settings.getMusicFoldersList();
 
36
                
 
37
                buildUI();
 
38
        }
 
39
        
 
40
        public void buildUI() {
 
41
                // set the title
 
42
                set_title("Preferences");
 
43
                
 
44
                this.window_position = WindowPosition.CENTER;
 
45
                
 
46
                // set the size
 
47
                set_size_request(400, 300);
 
48
                allow_shrink = true;
 
49
                
 
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);
 
55
                
 
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");
 
63
                
 
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");
 
66
                
 
67
                saveChanges = new Button.with_label("Save");
 
68
                cancelChanges = new Button.with_label("Cancel");
 
69
                
 
70
                /** build general page **/
 
71
                VBox genVert = new VBox(false, 3);
 
72
                notebook.append_page(generalPage, new Label("General"));
 
73
                generalPage.add(genVert);
 
74
                
 
75
                musicLocationsView.insert_column_with_attributes(-1, "Music Folder", new CellRendererText(), "text", 0, null);
 
76
                musicLocationsView.set_model(musicLocationsModel);
 
77
                
 
78
                foreach(string s in _lm.settings.getMusicFoldersList()) {
 
79
                        TreeIter added;
 
80
                        musicLocationsModel.append(out added);
 
81
                        musicLocationsModel.set(added, 0, s);
 
82
                }
 
83
                
 
84
                musicLocationsScroll.add(musicLocationsView);
 
85
                musicLocationsScroll.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
 
86
                
 
87
                VBox musicFolderButtonsBox = new VBox(false, 0);
 
88
                musicFolderButtonsBox.pack_start(musicLocationsAdd, false, false, 0);
 
89
                musicFolderButtonsBox.pack_start(musicLocationsRemove, false, false, 0);
 
90
                
 
91
                HBox musicFolderBox = new HBox(false, 0);
 
92
                musicFolderBox.pack_start(musicLocationsScroll, true, true, 5);
 
93
                musicFolderBox.pack_start(musicFolderButtonsBox, false, false, 0);
 
94
                
 
95
                updateFolderNames.set_active(_lm.settings.getUpdateFolderHierarchy());
 
96
                copyImportedMusic.set_active(_lm.settings.getCopyImportedMusic());
 
97
                
 
98
                genVert.pack_start(musicFolderBox, true, true, 0);
 
99
                genVert.pack_start(updateFolderNames, false, true, 0);
 
100
                genVert.pack_start(copyImportedMusic, false, true, 0);
 
101
                
 
102
                /** build lastfm page **/
 
103
                VBox lastfmVert = new VBox(false, 3);
 
104
                notebook.append_page(lastfmPage, new Label("Last FM"));
 
105
                lastfmPage.add(lastfmVert);
 
106
                
 
107
                lastfmInfo.set_line_wrap(true);
 
108
                
 
109
                lastfmVert.pack_start(lastfmInfo, false, true, 0);
 
110
                lastfmVert.pack_start(lastfmLogin, false, true, 0);
 
111
                
 
112
                lastfmLogin.clicked.connect(lastfmLoginClick);
 
113
                
 
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);
 
118
                
 
119
                /** put it all together **/
 
120
                vert.pack_start(notebook, true, true, 0);
 
121
                vert.pack_start(bottomButtons, false, true, 0);
 
122
                
 
123
                horiz.pack_start(vert, true, true, 10);
 
124
                
 
125
                this.add(horiz);
 
126
                show_all();
 
127
                
 
128
                saveChanges.clicked.connect(saveClicked);
 
129
                cancelChanges.clicked.connect(cancelClicked);
 
130
                musicLocationsAdd.clicked.connect(musicLocationsAddClicked);
 
131
                musicLocationsRemove.clicked.connect(musicLocationsRemoveClicked);
 
132
        }
 
133
        
 
134
        public virtual void lastfmLoginClick() {
 
135
                if(_lm.settings.getLastFMSessionKey() != "")
 
136
                        stdout.printf("We already have a key this is pointless...\n");
 
137
                
 
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");
 
143
                        }
 
144
                        else {
 
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);
 
147
                                
 
148
                                //set button text. we are done this time around. next time we get session key
 
149
                                lastfmLogin.set_label("Complete login");
 
150
                        }
 
151
                }
 
152
                else {
 
153
                        if(lastfm_token == null) {
 
154
                                lastfmLogin.set_label("Unsuccessful. Click to try again.");
 
155
                                stdout.printf("Invalid token. Cannot continue\n");
 
156
                        }
 
157
                        else {
 
158
                                var sk = _lm.lfm.getSessionKey(lastfm_token);
 
159
                                if(sk == null) {
 
160
                                        lastfmLogin.set_label("Unsuccessful. Click to try again.");
 
161
                                        stdout.printf("Could not get Last FM session key\n");
 
162
                                }
 
163
                                else {
 
164
                                        _lm.settings.setLastFMSessionKey(sk);
 
165
                                        stdout.printf("Successfully obtained a sessionkey\n");
 
166
                                        lastfmLogin.set_sensitive(false);
 
167
                                        lastfmLogin.set_label("Success!");
 
168
                                }
 
169
                        }
 
170
                }
 
171
        }
 
172
        
 
173
        public virtual void musicLocationsAddClicked() {
 
174
                string folder = "";
 
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();
 
181
        }
 
182
        file_chooser.destroy ();
 
183
        
 
184
        if(folder != "") {
 
185
                        TreeIter added;
 
186
                        musicLocationsModel.append(out added);
 
187
                        musicLocationsModel.set(added, 0, folder);
 
188
                }
 
189
        }
 
190
        
 
191
        public virtual void musicLocationsRemoveClicked() {
 
192
                TreeModel model;
 
193
                TreeIter iter;
 
194
                musicLocationsView.get_selection().get_selected(out model, out iter);
 
195
                
 
196
                musicLocationsModel.remove(iter);
 
197
        }
 
198
                
 
199
        public bool buildFoldersChanges(TreeModel model, TreePath path, TreeIter iter) {
 
200
                string loc;
 
201
                model.get(iter, 0, out loc);
 
202
                
 
203
                newLocations.add(loc);
 
204
                
 
205
                if(loc in origLocations)
 
206
                        removed.remove(loc);
 
207
                        
 
208
                return false;
 
209
        }
 
210
                
 
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
 
215
                 */
 
216
                
 
217
                removed = origLocations; //as we find folders from before, remove them from removed.
 
218
                newLocations = new Gee.LinkedList<string>();
 
219
                musicLocationsModel.foreach(buildFoldersChanges);
 
220
                
 
221
                _lm.settings.setMusicFoldersFromList(newLocations);
 
222
                
 
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()) {
 
226
                        bool remove = false;
 
227
                        foreach(string removeLoc in removed) {
 
228
                                if(removeLoc in s.file) {
 
229
                                        remove = true;
 
230
                                        stdout.printf("removed:%s in %s\n", s.file, removeLoc);
 
231
                                        break;
 
232
                                }
 
233
                        }
 
234
                        
 
235
                        if(remove)
 
236
                                removed_songs.add(s.rowid);
 
237
                }
 
238
                
 
239
                foreach(int id in removed_songs) {
 
240
                        _lm.remove_song_from_id(id);
 
241
                }
 
242
                
 
243
                //consolidate files lazily
 
244
                //save new songs to db and reload
 
245
                _lm.save_songs();
 
246
                _lm.clear_songs();
 
247
                foreach(Song s in _lm.dbm.load_songs()) {
 
248
                        _lm.add_song(s);
 
249
                }
 
250
                
 
251
                //now rescan the folders
 
252
                _lm.rescan_music_folders();
 
253
                
 
254
                _lm.settings.setUpdateFolderHierarchy(updateFolderNames.get_active());
 
255
                stdout.printf("no setting for copying imported music\n");
 
256
                this.destroy();
 
257
        }
 
258
        
 
259
        public virtual void cancelClicked() {
 
260
                this.destroy();
 
261
        }
 
262
}