~voluntatefaber/beat-box/bug952329

« back to all changes in this revision

Viewing changes to src/Widgets/FilterView.vala

  • Committer: Scott Ringwelski
  • Date: 2011-06-12 21:37:52 UTC
  • Revision ID: sgringwe@mtu.edu-20110612213752-sg5mee9st2cy1cf2
cleaned up and added gpl notice or whatever license at the top

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Gtk;
2
 
using Gee;
3
 
using WebKit;
4
 
 
5
 
public class BeatBox.FilterView : ScrolledWindow {
6
 
        LibraryManager lm;
7
 
        LibraryWindow lw;
8
 
        LinkedList<int> songs;
9
 
        
10
 
        WebView view;
11
 
        Table table;
12
 
        
13
 
        private Collection<int> showingSongs;
14
 
        private string last_search;
15
 
        LinkedList<string> timeout_search;
16
 
        
17
 
        string defaultPath;
18
 
        
19
 
        public bool isCurrentView;
20
 
        public bool needsUpdate;
21
 
        
22
 
        public signal void itemClicked(string artist, string album);
23
 
        
24
 
        /* songs should be mutable, as we will be sorting it */
25
 
        public FilterView(LibraryManager lmm, LibraryWindow lww, LinkedList<int> ssongs) {
26
 
                lm = lmm;
27
 
                lw = lww;
28
 
                songs = ssongs;
29
 
                
30
 
                showingSongs = new LinkedList<int>();
31
 
                last_search = "";
32
 
                timeout_search = new LinkedList<string>();
33
 
                
34
 
                defaultPath = GLib.Path.build_filename("/usr", "share", "icons", "hicolor", "128x128", "mimetypes", "media-audio.svg", null);
35
 
                
36
 
                buildUI();
37
 
        }
38
 
        
39
 
        public void buildUI() {
40
 
                view = new WebView();
41
 
                Viewport v = new Viewport(null, null);
42
 
                
43
 
        set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
44
 
                
45
 
                view.settings.enable_default_context_menu = false;
46
 
                
47
 
                v.set_shadow_type(ShadowType.NONE);
48
 
                v.add(view);
49
 
                add(v);
50
 
                
51
 
                show_all();
52
 
                
53
 
                view.navigation_requested.connect(navigationRequested);
54
 
                lw.searchField.changed.connect(searchFieldChanged);
55
 
        }
56
 
        
57
 
        /** Goes through the hashmap and generates html. If artist,album, or genre
58
 
         * is set, makes sure that only items that fit those filters are
59
 
         * shown
60
 
        */
61
 
        public void generateHTML(LinkedList<Song> toShow) {
62
 
                
63
 
                /** NOTE: This could have a bad effect if user coincidentally
64
 
                 * searches for something that has same number of results as 
65
 
                 * a different search. However, this cuts lots of unecessary
66
 
                 * loading of lists/icon lists */
67
 
                if(showingSongs.size == toShow.size)
68
 
                        return;
69
 
                
70
 
                string html = """<!DOCTYPE html> <html lang="en"><head> 
71
 
        <style media="screen" type="text/css"> 
72
 
            body { 
73
 
                background: #fff; 
74
 
                font-family: "Droid Sans",sans-serif; 
75
 
                margin-top: 10px;
76
 
            }
77
 
            #main {
78
 
                                margin: 0px auto;
79
 
                                width: 100%;
80
 
                        }
81
 
            #main ul {
82
 
                padding-bottom: 10px;
83
 
            }
84
 
            #main ul li {
85
 
                float: left;
86
 
                width: 150px;
87
 
                height: 200px;
88
 
                display: inline-block;
89
 
                list-style-type: none;
90
 
                padding-right: 10px;
91
 
                padding-left: 10px;
92
 
                padding-bottom: 5px;
93
 
                overflow: hidden;
94
 
            }
95
 
            #main ul li img {
96
 
                width: 150px;
97
 
                height: 150px;
98
 
            }
99
 
            #main ul li p {
100
 
                clear: both;
101
 
                overflow: hidden;
102
 
                text-align: center;
103
 
                margin-top: 0px;
104
 
                font-size: 12px;
105
 
                margin-bottom: 0px;
106
 
            }
107
 
        </style></head><body><div id="main"><ul>""";
108
 
        
109
 
        // first sort the songs so we know they are grouped by artists, then albums
110
 
                toShow.sort((CompareFunc)songCompareFunc);
111
 
                
112
 
                string previousAlbum = "";
113
 
                foreach(Song s in toShow) {
114
 
                        if(s.album != previousAlbum) {
115
 
                                html += "<li><a href=\"" + s.album + "<seperater>" + s.artist + "\"><img width=\"150\" height=\"150\" src=\"file://" + s.getAlbumArtPath() + "\" /></a><p>" + ( (s.album == "") ? "Unknown" : s.album) + "</p><p>" + s.artist + "</p></li>";
116
 
                                previousAlbum = s.album;
117
 
                        }
118
 
                }
119
 
                
120
 
                html += "</ul></div></body></html>"; // finish up the last song, finish up html
121
 
                
122
 
                view.load_html_string(html, "file://");
123
 
                needsUpdate = false;
124
 
                
125
 
                showingSongs = toShow;
126
 
        }
127
 
        
128
 
        public static int songCompareFunc(Song a, Song b) {
129
 
                if(a.artist.down() == b.artist.down())
130
 
                        return (a.album.down() > b.album.down()) ? 1 : -1;
131
 
                else
132
 
                        return (a.artist.down() > b.artist.down()) ? 1 : -1;
133
 
        }
134
 
        
135
 
        public virtual NavigationResponse navigationRequested(WebFrame frame, NetworkRequest request) {
136
 
                if(request.uri.contains("<seperater>")) {
137
 
                        // switch the view
138
 
                        string[] splitUp = request.uri.split("<seperater>", 0);
139
 
                        
140
 
                        itemClicked(splitUp[0], splitUp[1]);
141
 
                        
142
 
                        return WebKit.NavigationResponse.IGNORE;
143
 
                }
144
 
                
145
 
                return WebKit.NavigationResponse.ACCEPT;
146
 
        }
147
 
        
148
 
        public virtual void searchFieldChanged() {
149
 
                if(isCurrentView && lw.searchField.get_text().length != 1) {
150
 
                        timeout_search.offer_head(lw.searchField.get_text().down());
151
 
                        Timeout.add(100, () => {
152
 
                                string to_search = timeout_search.poll_tail();
153
 
                                stdout.printf("searching for %s\n", to_search);
154
 
                                
155
 
                                if(timeout_search.size == 0) {
156
 
                                        var toSearch = new LinkedList<Song>();
157
 
                                        foreach(int id in lm.songs_from_search(to_search, "All Genres", 
158
 
                                                                                                                "All Artists",
159
 
                                                                                                                "All Albums",
160
 
                                                                                                                songs)) {
161
 
                                                
162
 
                                                toSearch.add(lm.song_from_id(id));
163
 
                                        }
164
 
                                        
165
 
                                        if(showingSongs.size != toSearch.size) {
166
 
                                                generateHTML(toSearch);
167
 
                                        }
168
 
                                }
169
 
                                
170
 
                                return false;
171
 
                        });
172
 
                }
173
 
        }
174
 
        
175
 
}