~ubuntu-branches/ubuntu/lucid/gmusicbrowser/lucid

« back to all changes in this revision

Viewing changes to plugins/mozembed.pm

  • Committer: Bazaar Package Importer
  • Author(s): James Westby, Arnaud Soyez, James Westby
  • Date: 2008-11-27 00:53:40 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20081127005340-odrpldkb6gjs6zdj
Tags: 1.0.1-0ubuntu1
[ Arnaud Soyez ]
* New upstream version. (LP: #302422)

[ James Westby ]
* Drop suggests on flac123 as it has been removed, remove it from the
  description ogg123 in vorbis-tools (already suggested is plays flac)
  (LP: #301652)
* Change gmusicbrowser_123.pm to use ogg123 instead of flac123.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2007 Quentin Sculo <squentin@free.fr>
2
 
#
3
 
# This file is part of Gmusicbrowser.
4
 
# Gmusicbrowser is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License version 3, as
6
 
# published by the Free Software Foundation
7
 
 
8
 
=gmbplugin MozEmbed
9
 
MozEmbed
10
 
Mozilla plugin
11
 
provide context views using MozEmbed
12
 
=cut
13
 
 
14
 
BEGIN
15
 
{       # test if mozembed is working
16
 
        system(q(perl -e 'use Gtk2 "-init"; use Gtk2::MozEmbed;$w=Gtk2::Window->new;my $e=Gtk2::MozEmbed->new; $w->add($e);$w->show_all;')); #this segfault when mozembed doesn't find its libs
17
 
        if (($? & 127) ==11) {die "Error : mozembed libraries not found. You need to add the mozilla path in /etc/ld.so.conf and run ldconfig (as root) or add the mozilla libraries path to the LD_LIBRARY_PATH environment variable.\n"}
18
 
}
19
 
 
20
 
package GMB::Plugin::MozEmbed;
21
 
use strict;
22
 
use warnings;
23
 
require 'simple_http.pm';
24
 
use Gtk2::MozEmbed;
25
 
use base 'Gtk2::VBox';
26
 
use constant
27
 
{       OPT => 'PLUGIN_MozEmbed_',
28
 
};
29
 
 
30
 
our $Embed;
31
 
Gtk2::MozEmbed->set_profile_path ($::HomeDir,'mozilla_profile');
32
 
if ($Gtk2::MozEmbed::VERSION>=0.06) {Gtk2::MozEmbed->push_startup}
33
 
else {$Embed=Gtk2::MozEmbed->new;} #needed to keep a Gtk2::MozEmbed to prevent xpcom from shutting down with the last gtkmozembed
34
 
 
35
 
my $active;
36
 
::SetDefaultOptions(OPT, StrippedWiki => 1);
37
 
 
38
 
sub Start
39
 
{       $active=1;
40
 
        update_Context_hash();
41
 
        &set_stripped_wiki;
42
 
}
43
 
sub Stop
44
 
{       $active=0;
45
 
        update_Context_hash(1);
46
 
}
47
 
 
48
 
sub update_Context_hash
49
 
{       for my $ref
50
 
                (       ['DisableLyrics','MozLyrics','GMB::Plugin::MozEmbed::Lyrics'],
51
 
                        ['DisableWiki','MozWikipedia','GMB::Plugin::MozEmbed::Wikipedia'],
52
 
                        ['DisableLastfm','MozLastfm','GMB::Plugin::MozEmbed::Lastfm'],
53
 
                )
54
 
        {       my ($option,$key,$package)=@$ref;
55
 
                my $on= !$::Options{OPT.$option} && $active;
56
 
                if (exists $GMB::Context::Contexts{$key})
57
 
                {       if (!$on)
58
 
                        {       GMB::Context::RemovePackage($key);
59
 
                        }
60
 
                }
61
 
                elsif ($on)
62
 
                {       GMB::Context::AddPackage($package,$key);
63
 
                }
64
 
        }
65
 
}
66
 
 
67
 
sub set_stripped_wiki   #FIXME use print version instead ?
68
 
{       my $content='';
69
 
        if ($::Options{OPT.'StrippedWiki'})
70
 
        { $content="/*@-moz-document domain(wikipedia.org) { */
71
 
.portlet {display: none !important;}
72
 
#f-list {display: none !important;}
73
 
#footer {display: none !important;}
74
 
#content {margin: 0 0 0 0 !important; padding: 0 0 0 0 !important}
75
 
/* } */ ";
76
 
        }
77
 
        open my $fh,'>',join(::SLASH,$::HomeDir,'mozilla_profile','chrome','userContent.css') or return;
78
 
        print $fh $content;
79
 
        close $fh;
80
 
}
81
 
 
82
 
sub new
83
 
{       my ($class)=@_;
84
 
        my $self = bless Gtk2::VBox->new, $class;
85
 
        my $hbox = Gtk2::HBox->new;
86
 
        my $status=$self->{status}=Gtk2::Statusbar->new;
87
 
        $status->{id}=$status->get_context_id('link');
88
 
        my $embed=$self->{embed}=Gtk2::MozEmbed->new;
89
 
        my $back= $self->{BBack}=Gtk2::ToolButton->new_from_stock('gtk-go-back');
90
 
        my $next= $self->{BNext}=Gtk2::ToolButton->new_from_stock('gtk-go-forward');
91
 
        my $stop= $self->{BStop}=Gtk2::ToolButton->new_from_stock('gtk-stop');
92
 
        my $open= $self->{BOpen}=Gtk2::ToolButton->new_from_stock('gtk-jump-to');
93
 
        $open->set_tooltip($::Tooltips,_"Open this page in the web browser",'');
94
 
        #$open->set_use_drag_window(1);
95
 
        #::set_drag($open,source=>[::DRAG_FILE,sub {$embed->get_location;}]);
96
 
        $self->{$_}->set_sensitive(0) for qw/BBack BNext BStop BOpen/;
97
 
        $hbox->pack_start($_,::FALSE,::FALSE,4) for grep defined, $back,$next,$stop,$open,$self->addtoolbar;
98
 
        $self->pack_start($hbox,::FALSE,::FALSE,1);
99
 
        $self->add($embed);
100
 
        $self->pack_end($status,::FALSE,::FALSE,1) if $::Options{OPT.'StatusBar'};
101
 
        $self->show_all;
102
 
        $back->signal_connect(clicked => sub { $embed->go_back });
103
 
        $next->signal_connect(clicked => sub { $embed->go_forward });
104
 
        $stop->signal_connect(clicked => sub { $embed->stop_load });
105
 
        $open->signal_connect(clicked => sub { my $url=$embed->get_location; ::openurl($url) if $url=~m#^https?://# });
106
 
        $embed->signal_connect(link_message => \&link_message_cb);
107
 
        $embed->signal_connect(net_stop => \&net_startstop_cb,0);
108
 
        $embed->signal_connect(net_start => \&net_startstop_cb,1);
109
 
        return $self;
110
 
}
111
 
 
112
 
sub prefbox
113
 
{       my $vbox=Gtk2::VBox->new(::FALSE, 2);
114
 
        #my $combo=::NewPrefCombo(OPT.'Site',[sort keys %sites],'site : ',sub {$ID=undef;&Changed;});
115
 
        my $check=::NewPrefCheckButton(OPT.'StrippedWiki',_"Strip wikipedia pages",\&set_stripped_wiki,_"Remove header, footer and left column from wikipedia pages");
116
 
        my $Bopen=Gtk2::Button->new(_"open context window");
117
 
        $Bopen->signal_connect(clicked => sub { ::ContextWindow; });
118
 
        my $check1=::NewPrefCheckButton(OPT.'DisableWiki',_"Disable wikipedia context tab",\&update_Context_hash);
119
 
        my $check2=::NewPrefCheckButton(OPT.'DisableLyrics',_"Disable Lyrics context tab",\&update_Context_hash);
120
 
        my $check3=::NewPrefCheckButton(OPT.'DisableLastfm',_"Disable Last.fm context tab",\&update_Context_hash);
121
 
        $vbox->pack_start($_,::FALSE,::FALSE,1) for $check1,$check2,$check3,$check,$Bopen;
122
 
        return $vbox;
123
 
}
124
 
 
125
 
sub load_url
126
 
{       my ($self,$url,$post)=@_;
127
 
        $self->{url}=$url;
128
 
        $self->{post}=$post;
129
 
        if ($post)
130
 
        { Simple_http::get_with_cb(cb => sub {$self->loaded(@_)},url => $url,post => $post); }
131
 
        else {$self->{embed}->load_url($url);}
132
 
}
133
 
 
134
 
sub loaded
135
 
{       my ($self,$data,$type)=@_;
136
 
        my $embed=$self->{embed};
137
 
        $embed->render_data($data,$self->{url},$type)
138
 
#       $embed->open_stream ($self->{url}, $type);
139
 
#       $embed->append_data ($data);
140
 
#       $embed->close_stream;
141
 
}
142
 
 
143
 
sub link_message_cb
144
 
{       my $embed=$_[0];
145
 
        my $self=::find_ancestor($embed,__PACKAGE__);
146
 
        my $statusbar=$self->{status};
147
 
        $statusbar->pop( $statusbar->{id} );
148
 
        $statusbar->push( $statusbar->{id}, $embed->get_link_message );
149
 
}
150
 
 
151
 
sub net_startstop_cb
152
 
{       my ($embed,$loading)=@_;
153
 
        my $self=::find_ancestor($embed,__PACKAGE__);
154
 
        $self->{BStop}->set_sensitive( $loading );
155
 
        $self->{BBack}->set_sensitive( $embed->can_go_back );
156
 
        $self->{BNext}->set_sensitive( $embed->can_go_forward );
157
 
        my $loc=$embed->get_location;
158
 
        $loc= $loc=~m#^https?://# ? 1 : 0 ;
159
 
        $self->{BOpen}->set_sensitive($loc);
160
 
}
161
 
 
162
 
package GMB::Plugin::MozEmbed::Lyrics;
163
 
our @ISA=('GMB::Plugin::MozEmbed');
164
 
 
165
 
use constant
166
 
{       title => _"Lyrics",
167
 
        OPT => GMB::Plugin::MozEmbed::OPT,  #FIXME
168
 
};
169
 
 
170
 
my %sites=
171
 
(       #lyrc => ['lyrc','http://lyrc.com.ar/en/tema1en.php','artist=%a&songname=%s'],
172
 
        lyrc => ['lyrc','http://lyrc.com.ar/en/tema1en.php?artist=%a&songname=%s'],
173
 
        #leoslyrics => ['leolyrics','http://api.leoslyrics.com/api_search.php?artist=%a&songtitle=%s'],
174
 
        google  => ['google','http://www.google.com/search?q="%a"+"%s"'],
175
 
        googlemusic  => ['google music','http://www.google.com/musicsearch?q="%a"+"%s"'],
176
 
        lyriki  => ['lyriki','http://lyriki.com/index.php?title=%a:%s'],
177
 
        lyricwiki => ['lyricwiki','http://lyricwiki.org/%a:%s'],
178
 
);
179
 
 
180
 
::SetDefaultOptions(OPT, LyricSite => 'google');
181
 
 
182
 
sub addtoolbar
183
 
{       #my $self=$_[0];
184
 
        my %h= map {$_=>$sites{$_}[0]} keys %sites;
185
 
        my $cb=sub
186
 
         {      my $self=::find_ancestor($_[0],__PACKAGE__);
187
 
                $self->SongChanged($self->{ID},1);
188
 
         };
189
 
        my $combo=::NewPrefCombo( OPT.'LyricSite', \%h, undef, $cb);
190
 
        return $combo;
191
 
}
192
 
 
193
 
sub SongChanged
194
 
{       my ($self,$ID,$force)=@_;
195
 
        return unless defined $ID;
196
 
        return if defined $self->{ID} && $ID==$self->{ID} && !$force;
197
 
        $self->{ID}=$ID;
198
 
        my $ref=$::Songs[$ID];
199
 
        my $title=$ref->[::SONG_TITLE];
200
 
        return if !defined $title || $title eq '';
201
 
        my $artist=$ref->[::SONG_ARTIST];
202
 
        $artist=::url_escapeall($artist);
203
 
        $title= ::url_escapeall($title);
204
 
        my (undef,$url,$post)=@{$sites{$::Options{OPT.'LyricSite'}}};
205
 
        for ($url,$post) { next unless defined $_; s/%a/$artist/; s/%s/$title/; }
206
 
        ::IdleDo('8_mozlyrics'.$self,1000,sub {$self->load_url($url,$post)});
207
 
}
208
 
 
209
 
package GMB::Plugin::MozEmbed::Wikipedia;
210
 
our @ISA=('GMB::Plugin::MozEmbed');
211
 
 
212
 
use constant
213
 
{       title => _"Wikipedia",
214
 
        OPT => GMB::Plugin::MozEmbed::OPT,  #FIXME
215
 
};
216
 
 
217
 
my %locales=
218
 
(       en => 'English',
219
 
        fr => 'Fran�ais',
220
 
        de => 'Deutsch',
221
 
        pl => 'Polski',
222
 
        nl => 'Nederlands',
223
 
        sv => 'Svenska',
224
 
        it => 'Italiano',
225
 
        pt => 'Portugu�s',
226
 
        es => 'Espa�ol',
227
 
#       ja => "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\x0a",
228
 
);
229
 
#::_utf8_on( $locales{ja} );
230
 
 
231
 
::SetDefaultOptions(OPT, WikiLocale => 'en');
232
 
 
233
 
sub addtoolbar
234
 
{       #my $self=$_[0];
235
 
        my $cb=sub
236
 
         {      my $self=::find_ancestor($_[0],__PACKAGE__);
237
 
                $self->SongChanged($self->{ID},1);
238
 
         };
239
 
        my $combo=::NewPrefCombo( OPT.'WikiLocale', \%locales, undef, $cb);
240
 
        return $combo;
241
 
}
242
 
 
243
 
sub SongChanged
244
 
{       my ($self,$ID,$force)=@_;
245
 
        return unless defined $ID;
246
 
        $self->{ID}=$ID;
247
 
        my $artist=$::Songs[$ID][::SONG_ARTIST];
248
 
        return if !defined $artist || $artist eq '' || $artist eq '<Unknown>';
249
 
        return if defined $self->{Artist} && $artist eq $self->{Artist} && !$force;
250
 
        ($artist)=split /$::re_artist/o,$artist; #FIXME add a way to choose artist ?
251
 
        $self->{Artist}=$artist;
252
 
        $artist=::url_escapeall($artist);
253
 
        my $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/wiki/'.$artist;
254
 
        #my $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/w/index.php?title='.$artist.'&action=render';
255
 
        ::IdleDo('8_mozpedia'.$self,1000,sub {$self->load_url($url)});
256
 
        #::IdleDo('8_mozpedia'.$self,1000,sub {$self->wikiload});
257
 
}
258
 
 
259
 
sub wikiload    #not used for now
260
 
{       my $self=$_[0];
261
 
        my $url=::url_escapeall($self->{Artist});
262
 
        $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/wiki/'.$url;
263
 
        #$url='http://google.com/search?q='.$url;
264
 
        $self->{url}=$url;
265
 
        Simple_http::get_with_cb(cb => sub
266
 
                {       my $cb=sub { $self->wikifilter(@_) };
267
 
                        if (!$_[0] || $_[0]=~m/No page with that title exists/)
268
 
                        {       Simple_http::get_with_cb(cb => $cb, url => $url);
269
 
                        }
270
 
                        else { $self->{url}.='_(band)'; goto $cb }
271
 
                },url => $url.'_(band)');
272
 
}
273
 
 
274
 
sub wikifilter  #not used for now
275
 
{       my ($self,$data,$type)=@_;
276
 
        return unless $data;    #FIXME
277
 
        #$data='<style type="text/css">.firstHeading {display: none}</style>'.$data;
278
 
        $self->loaded($data,$type);
279
 
}
280
 
 
281
 
package GMB::Plugin::MozEmbed::Lastfm;
282
 
our @ISA=('GMB::Plugin::MozEmbed');
283
 
 
284
 
use constant
285
 
{       title => "Last.fm",
286
 
#       OPT => GMB::Plugin::MozEmbed::OPT,  #FIXME
287
 
};
288
 
 
289
 
sub addtoolbar
290
 
{       #my $self=$_[0];
291
 
        #my $combo=Gtk2::ComboBox->new_text;
292
 
        #return $combo;
293
 
}
294
 
 
295
 
sub SongChanged
296
 
{       my ($self,$ID,$force)=@_;
297
 
        return unless defined $ID;
298
 
        $self->{ID}=$ID;
299
 
        my $artist=$::Songs[$ID][::SONG_ARTIST];
300
 
        return if !defined $artist || $artist eq '' || $artist eq '<Unknown>';
301
 
        return if defined $self->{Artist} && $artist eq $self->{Artist} && !$force;
302
 
        $self->{Artist}=$artist;
303
 
        $artist=~s/ /+/g;
304
 
        $artist=::url_escapeall($artist);
305
 
        my $url='http://www.last.fm/music/'.$artist;
306
 
        ::IdleDo('8_mozlastfm'.$self,1000,sub {$self->load_url($url)});
307
 
}
308
 
 
309
 
 
310
 
1
311
 
 
312
 
# FIXME use google music : http://www.google.com/musicsearch?q="%a"