~dhananjaysathe/nuvola-player/TelepathyExt

« back to all changes in this revision

Viewing changes to src/frame.vala

  • Committer: Jiří Janoušek
  • Date: 2011-08-23 23:07:29 UTC
  • Revision ID: janousek.jiri@gmail.com-20110823230729-0lfuuttyps70qd58
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Google Music Frame
 
3
 
 
4
Copyright 2011 Jiří Janoušek <janousek.jiri@gmail.com>
 
5
 
 
6
This program is free software: you can redistribute it and/or modify it 
 
7
under the terms of the GNU General Public License version 3, as published 
 
8
by the Free Software Foundation.
 
9
 
 
10
This program is distributed in the hope that it will be useful, but 
 
11
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
12
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
13
PURPOSE.  See the GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License along 
 
16
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
/* TODO:
 
20
 * Save UI state (window dimensions)
 
21
 * Handle opening links in web browser
 
22
 */
 
23
 
 
24
using Gtk;
 
25
using WebKit;
 
26
using Soup;
 
27
using Gee;
 
28
using Fenryxo;
 
29
 
 
30
namespace GMF{
 
31
        
 
32
        
 
33
        public class Frame: Window{
 
34
                private const string TITLE = "Google Music Frame";
 
35
                private const string HOME_URL = "http://music.google.com/";
 
36
                
 
37
                private ConfigFile config;
 
38
                private WebView view;
 
39
                private unowned WebFrame info_frame;
 
40
                
 
41
                public bool can_prev {get; private set;} 
 
42
                public bool can_next {get; private set;} 
 
43
                public string? playback_state {get; private set;} 
 
44
                public string? album_art {get; private set;} 
 
45
                public string? artist {get; private set;} 
 
46
                public string? song {get; private set;} 
 
47
                
 
48
                public signal void song_changed(string? art, string? artist, string? title);
 
49
                public signal void playback_state_changed(string state, bool can_prev, bool can_next);
 
50
                
 
51
                public class Frame(Application app){
 
52
                        this.config = app.config;
 
53
                        this.init_ui();
 
54
                }
 
55
                
 
56
                private void init_ui(){
 
57
                        this.destroy.connect(Gtk.main_quit);
 
58
                        this.set_default_size(800, 600);
 
59
                        this.title = TITLE;
 
60
                        
 
61
                        this.view = new WebView();
 
62
                        var sw = new ScrolledWindow(null, null);
 
63
                        sw.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
 
64
                        sw.add(this.view);
 
65
                        sw.show_all();
 
66
                        this.add(sw);
 
67
                        
 
68
                        /* Show page title as window title */
 
69
                        this.view.title_changed.connect((source, frame, title) => {
 
70
                                this.title = title;
 
71
                        });
 
72
                        
 
73
                        /* Save current URI to restore previous view on startup */
 
74
                        this.view.notify["uri"].connect((o, p) => {
 
75
                                this.config["last_uri"] = (o as WebView).uri;
 
76
                                this.config.save();
 
77
                                debug("URI: %s\n", (o as WebView).uri);
 
78
                        });
 
79
                        
 
80
                        this.view.document_load_finished.connect(this.load_scripts);
 
81
                }
 
82
                
 
83
                /**
 
84
                 * Load Google Music and show GUI
 
85
                 */
 
86
                public void start(){
 
87
                        this.view.open(this.config["last_uri"] ?? this.HOME_URL);
 
88
                        this.show();
 
89
                        this.view.get_inspector().show();
 
90
        
 
91
                }
 
92
                
 
93
                private void load_scripts(WebFrame frame){
 
94
                        debug("load scripts");
 
95
                        if(frame != this.view.get_main_frame()) return;
 
96
                        var script = get_data_file(APPNAME + "/scripts/main.js");
 
97
                        string data;
 
98
                        if(script.load_contents(null, out data)){
 
99
                                debug("execute script");
 
100
                                this.view.execute_script(data);
 
101
                                debug("get info frame");
 
102
                                unowned WebFrame info_frame = frame.find_frame("GMF_iframe");
 
103
                                if(info_frame != null){
 
104
                                        debug("got info frame");
 
105
                                        this.info_frame = info_frame;
 
106
                                        this.info_frame.title_changed.connect(() => {this.update_info();} );
 
107
                                        this.update_info();
 
108
                                }
 
109
                                else debug("ERROR: getting frame");
 
110
                        }
 
111
                        else debug("ERROR: loading script");
 
112
                        
 
113
                }
 
114
                
 
115
                private void update_info(){
 
116
                        var title = this.info_frame.title;
 
117
                        if(title != null && title != ""){
 
118
                                        string[] parts = this.info_frame.title.split("#@!@#");
 
119
                                        this.playback_state = parts[0];
 
120
                                        this.can_prev = parts[1] == "true";
 
121
                                        this.can_next = parts[2] == "true";
 
122
                                        if(parts.length == 6){
 
123
                                                this.album_art = parts[3];
 
124
                                                this.artist = parts[4];
 
125
                                                this.song = parts[5];
 
126
                                        }
 
127
                        }
 
128
                        else{
 
129
                                this.playback_state = "none";
 
130
                                this.can_prev = false;
 
131
                                this.can_next = false;
 
132
                                this.album_art = null;
 
133
                                this.artist = null;
 
134
                                this.song = null;
 
135
                        }
 
136
                        this.playback_state_changed(this.playback_state, this.can_prev, this.can_next);
 
137
                        this.song_changed(this.album_art, this.artist, this.song);
 
138
                }
 
139
                
 
140
                public void command(string cmd){
 
141
                        debug("Frame command %s", cmd);
 
142
                        this.view.execute_script("SJBpost('" + cmd + "'); $GMF.update();");
 
143
                }
 
144
        }
 
145
 
 
146
 
 
147
        
 
148
}