1
# Copyright (C) 2005-2007 Quentin Sculo <squentin@free.fr>
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
11
provide context views using MozEmbed
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"}
20
package GMB::Plugin::MozEmbed;
23
require 'simple_http.pm';
25
use base 'Gtk2::VBox';
27
{ OPT => 'PLUGIN_MozEmbed_',
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
36
::SetDefaultOptions(OPT, StrippedWiki => 1);
40
update_Context_hash();
45
update_Context_hash(1);
48
sub update_Context_hash
50
( ['DisableLyrics','MozLyrics','GMB::Plugin::MozEmbed::Lyrics'],
51
['DisableWiki','MozWikipedia','GMB::Plugin::MozEmbed::Wikipedia'],
52
['DisableLastfm','MozLastfm','GMB::Plugin::MozEmbed::Lastfm'],
54
{ my ($option,$key,$package)=@$ref;
55
my $on= !$::Options{OPT.$option} && $active;
56
if (exists $GMB::Context::Contexts{$key})
58
{ GMB::Context::RemovePackage($key);
62
{ GMB::Context::AddPackage($package,$key);
67
sub set_stripped_wiki #FIXME use print version instead ?
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}
77
open my $fh,'>',join(::SLASH,$::HomeDir,'mozilla_profile','chrome','userContent.css') or return;
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);
100
$self->pack_end($status,::FALSE,::FALSE,1) if $::Options{OPT.'StatusBar'};
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);
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;
126
{ my ($self,$url,$post)=@_;
130
{ Simple_http::get_with_cb(cb => sub {$self->loaded(@_)},url => $url,post => $post); }
131
else {$self->{embed}->load_url($url);}
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;
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 );
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);
162
package GMB::Plugin::MozEmbed::Lyrics;
163
our @ISA=('GMB::Plugin::MozEmbed');
166
{ title => _"Lyrics",
167
OPT => GMB::Plugin::MozEmbed::OPT, #FIXME
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'],
180
::SetDefaultOptions(OPT, LyricSite => 'google');
184
my %h= map {$_=>$sites{$_}[0]} keys %sites;
186
{ my $self=::find_ancestor($_[0],__PACKAGE__);
187
$self->SongChanged($self->{ID},1);
189
my $combo=::NewPrefCombo( OPT.'LyricSite', \%h, undef, $cb);
194
{ my ($self,$ID,$force)=@_;
195
return unless defined $ID;
196
return if defined $self->{ID} && $ID==$self->{ID} && !$force;
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)});
209
package GMB::Plugin::MozEmbed::Wikipedia;
210
our @ISA=('GMB::Plugin::MozEmbed');
213
{ title => _"Wikipedia",
214
OPT => GMB::Plugin::MozEmbed::OPT, #FIXME
227
# ja => "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\x0a",
229
#::_utf8_on( $locales{ja} );
231
::SetDefaultOptions(OPT, WikiLocale => 'en');
236
{ my $self=::find_ancestor($_[0],__PACKAGE__);
237
$self->SongChanged($self->{ID},1);
239
my $combo=::NewPrefCombo( OPT.'WikiLocale', \%locales, undef, $cb);
244
{ my ($self,$ID,$force)=@_;
245
return unless defined $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});
259
sub wikiload #not used for now
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;
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);
270
else { $self->{url}.='_(band)'; goto $cb }
271
},url => $url.'_(band)');
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);
281
package GMB::Plugin::MozEmbed::Lastfm;
282
our @ISA=('GMB::Plugin::MozEmbed');
285
{ title => "Last.fm",
286
# OPT => GMB::Plugin::MozEmbed::OPT, #FIXME
291
#my $combo=Gtk2::ComboBox->new_text;
296
{ my ($self,$ID,$force)=@_;
297
return unless defined $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;
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)});
312
# FIXME use google music : http://www.google.com/musicsearch?q="%a"