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

« back to all changes in this revision

Viewing changes to plugins/webcontext.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-2008 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 WebContext
 
9
Web context
 
10
Web context plugin
 
11
Provides context views using MozEmbed or WebKit
 
12
=cut
 
13
 
 
14
my ($OKMoz,$OKWebKit,$CrashMoz);
 
15
BEGIN
 
16
{       {       last unless (grep -f $_.'/Gtk2/MozEmbed.pm',@INC);
 
17
                # test if mozembed is working
 
18
                system(q(GNOME_DISABLE_CRASH_DIALOG=1 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
 
19
                #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
                if (($? & 127) ==11) { $CrashMoz=1; last; }
 
21
                $OKMoz=1;
 
22
        }
 
23
        $OKWebKit=1 if grep -f $_.'/Gtk2/WebKit.pm',@INC;
 
24
}
 
25
 
 
26
use strict;
 
27
use warnings;
 
28
 
 
29
package GMB::Plugin::WebContext::MozEmbed;
 
30
#use Gtk2::MozEmbed;
 
31
 
 
32
our $Embed;
 
33
sub init
 
34
{       Gtk2::MozEmbed->set_profile_path ($::HomeDir,'mozilla_profile');
 
35
        if ($Gtk2::MozEmbed::VERSION>=0.06) {Gtk2::MozEmbed->push_startup}
 
36
        else {$Embed||=Gtk2::MozEmbed->new;} #needed to keep a Gtk2::MozEmbed to prevent xpcom from shutting down with the last gtkmozembed
 
37
}
 
38
 
 
39
sub new_embed
 
40
{       my $embed=Gtk2::MozEmbed->new;
 
41
        $embed->signal_connect(link_message => \&link_message_cb);
 
42
        $embed->signal_connect(net_stop => \&net_startstop_cb,0);
 
43
        $embed->signal_connect(net_start => \&net_startstop_cb,1);
 
44
        return $embed;
 
45
}
 
46
 
 
47
sub link_message_cb
 
48
{       my $embed=$_[0];
 
49
        my $self=::find_ancestor($embed,__PACKAGE__);
 
50
        $self->link_message( $embed->get_link_message );
 
51
}
 
52
 
 
53
sub net_startstop_cb
 
54
{       my ($embed,$loading)=@_;
 
55
        my $self=::find_ancestor($embed,__PACKAGE__);
 
56
        $self->{BStop}->set_sensitive( $loading );
 
57
        $self->{BBack}->set_sensitive( $embed->can_go_back );
 
58
        $self->{BNext}->set_sensitive( $embed->can_go_forward );
 
59
        my $uri=$embed->get_location;
 
60
        $uri= $uri=~m#^https?://# ? 1 : 0 ;
 
61
        $self->{BOpen}->set_sensitive($uri);
 
62
}
 
63
 
 
64
sub loaded
 
65
{       my ($self,$data,$type)=@_;
 
66
        my $embed=$self->{embed};
 
67
        $embed->render_data($data,$self->{url},$type)
 
68
#       $embed->open_stream ($self->{url}, $type);
 
69
#       $embed->append_data ($data);
 
70
#       $embed->close_stream;
 
71
}
 
72
 
 
73
sub go_back     { $_[0]{embed}->go_back }
 
74
sub go_forward  { $_[0]{embed}->go_forward }
 
75
sub stop_load   { $_[0]{embed}->stop_load }
 
76
sub get_location{ $_[0]{embed}->get_location }
 
77
sub Open        { $_[0]{embed}->load_url($_[1]); }
 
78
 
 
79
sub set_stripped_wiki   #FIXME use print version of the wikipedia page instead ?
 
80
{       my $stripped=$_[0];
 
81
        my $content='';
 
82
        if ($stripped)
 
83
        { $content="/*@-moz-document domain(wikipedia.org) { */
 
84
.portlet {display: none !important;}
 
85
#f-list {display: none !important;}
 
86
#footer {display: none !important;}
 
87
#content {margin: 0 0 0 0 !important; padding: 0 0 0 0 !important}
 
88
/* } */ ";
 
89
        }
 
90
        open my $fh,'>',join(::SLASH,$::HomeDir,'mozilla_profile','chrome','userContent.css') or return;
 
91
        print $fh $content;
 
92
        close $fh;
 
93
}
 
94
 
 
95
package GMB::Plugin::WebContext::WebKit;
 
96
#use Gtk2::WebKit;
 
97
 
 
98
sub new_embed
 
99
{       my $embed=Gtk2::WebKit::WebView->new;
 
100
        $embed->signal_connect(hovering_over_link => \&link_message_cb);
 
101
        $embed->signal_connect(load_finished => \&net_startstop_cb,0);
 
102
        $embed->signal_connect(load_committed => \&net_startstop_cb,1);
 
103
        my $sw= Gtk2::ScrolledWindow->new;
 
104
        $sw->set_shadow_type('etched-in');
 
105
        $sw->set_policy('automatic','automatic');
 
106
        $sw->add($embed);
 
107
        return $embed,$sw;
 
108
}
 
109
 
 
110
sub link_message_cb
 
111
{       my ($embed,undef,$msg)=@_;
 
112
        my $self=::find_ancestor($embed,__PACKAGE__);
 
113
        $self->link_message($msg);
 
114
}
 
115
 
 
116
sub net_startstop_cb
 
117
{       my ($embed,$frame,$loading)=@_;
 
118
        my $self=::find_ancestor($embed,__PACKAGE__);
 
119
        $self->{BStop}->set_sensitive( $loading );
 
120
        $self->{BBack}->set_sensitive( $embed->can_go_back );
 
121
        $self->{BNext}->set_sensitive( $embed->can_go_forward );
 
122
        my $uri=$frame->get_uri;
 
123
        $uri= $uri=~m#^https?://# ? 1 : 0 ;
 
124
        $self->{BOpen}->set_sensitive($uri);
 
125
}
 
126
 
 
127
sub loaded
 
128
{       my ($self,$data,$type)=@_;
 
129
        my $embed=$self->{embed};
 
130
        $embed->load_html_string($data,$self->{url}); #FIXME doesn't check type
 
131
}
 
132
 
 
133
 
 
134
sub go_back     { $_[0]{embed}->go_back }
 
135
sub go_forward  { $_[0]{embed}->go_forward }
 
136
sub stop_load   { $_[0]{embed}->stop_loading }
 
137
sub get_location{ $_[0]{embed}->get_focused_frame->get_uri }
 
138
sub Open        { $_[0]{embed}->open($_[1]); }
 
139
 
 
140
sub set_stripped_wiki {}        #FIXME use print version of the wikipedia page instead ?
 
141
# $::Options{OPT.'StrippedWiki'}
 
142
 
 
143
package GMB::Plugin::WebContext;
 
144
require 'simple_http.pm';
 
145
use base 'Gtk2::VBox';
 
146
use constant
 
147
{       OPT => 'PLUGIN_WebContext_',
 
148
};
 
149
our @ISA;
 
150
#push @ISA, 'GMB::Plugin::WebContext::'.$Backend;
 
151
 
 
152
 
 
153
my $active;
 
154
::SetDefaultOptions(OPT, StrippedWiki => 1);
 
155
UpdateBackend();
 
156
 
 
157
sub UpdateBackend
 
158
{       my $backend= $::Options{OPT.'Backend'} || '';
 
159
        $backend='' if          !$OKMoz && $backend eq 'MozEmbed'
 
160
                        ||      !$OKWebKit && $backend eq 'WebKit';
 
161
        unless ($backend)
 
162
        {       if ($OKWebKit) {$backend='WebKit'}
 
163
                elsif ($OKMoz) {$backend='MozEmbed'}
 
164
        }
 
165
        $::Options{OPT.'Backend'}=$backend;
 
166
 
 
167
        my $was_active=$active;
 
168
        Stop() if $active;
 
169
        if ($OKMoz && $backend eq 'MozEmbed')
 
170
        {       require Gtk2::MozEmbed; Gtk2::MozEmbed->import;
 
171
                GMB::Plugin::WebContext::MozEmbed::init();
 
172
                @ISA= grep $_ ne 'GMB::Plugin::WebContext::WebKit', @ISA;
 
173
                push @ISA, 'GMB::Plugin::WebContext::MozEmbed';
 
174
        }
 
175
        elsif ($OKWebKit)
 
176
        {       require Gtk2::WebKit; Gtk2::WebKit->import;
 
177
                @ISA= grep $_ ne 'GMB::Plugin::WebContext::MozEmbed', @ISA;
 
178
                push @ISA, 'GMB::Plugin::WebContext::WebKit';
 
179
        }
 
180
        Start() if $was_active;
 
181
}
 
182
 
 
183
sub Start
 
184
{       $active=1;
 
185
        update_Context_hash();
 
186
        &set_stripped_wiki;
 
187
}
 
188
sub Stop
 
189
{       $active=0;
 
190
        update_Context_hash();
 
191
}
 
192
 
 
193
sub update_Context_hash
 
194
{       for my $ref
 
195
                (       ['DisableLyrics','MozLyrics','GMB::Plugin::WebContext::Lyrics'],
 
196
                        ['DisableWiki','MozWikipedia','GMB::Plugin::WebContext::Wikipedia'],
 
197
                        ['DisableLastfm','MozLastfm','GMB::Plugin::WebContext::Lastfm'],
 
198
                )
 
199
        {       my ($option,$key,$package)=@$ref;
 
200
                my $on= !$::Options{OPT.$option} && $active;
 
201
                if (exists $GMB::Context::Contexts{$key})
 
202
                {       if (!$on)
 
203
                        {       GMB::Context::RemovePackage($key);
 
204
                        }
 
205
                }
 
206
                elsif ($on && ($OKMoz || $OKWebKit))
 
207
                {       GMB::Context::AddPackage($package,$key);
 
208
                }
 
209
        }
 
210
}
 
211
 
 
212
sub new
 
213
{       my ($class)=@_;
 
214
        my $self = bless Gtk2::VBox->new, $class;
 
215
        my $hbox = Gtk2::HBox->new;
 
216
        my $status=$self->{status}=Gtk2::Statusbar->new;
 
217
        $status->{id}=$status->get_context_id('link');
 
218
        ($self->{embed},my $container)= $self->new_embed;
 
219
        $container||=$self->{embed};
 
220
        my $back= $self->{BBack}=Gtk2::ToolButton->new_from_stock('gtk-go-back');
 
221
        my $next= $self->{BNext}=Gtk2::ToolButton->new_from_stock('gtk-go-forward');
 
222
        my $stop= $self->{BStop}=Gtk2::ToolButton->new_from_stock('gtk-stop');
 
223
        my $open= $self->{BOpen}=Gtk2::ToolButton->new_from_stock('gtk-jump-to');
 
224
        $open->set_tooltip($::Tooltips,_"Open this page in the web browser",'');
 
225
        #$open->set_use_drag_window(1);
 
226
        #::set_drag($open,source=>[::DRAG_FILE,sub {$embed->get_location;}]);
 
227
        $self->{$_}->set_sensitive(0) for qw/BBack BNext BStop BOpen/;
 
228
        $hbox->pack_start($_,::FALSE,::FALSE,4) for grep defined, $back,$next,$stop,$open,$self->addtoolbar;
 
229
        $self->pack_start($hbox,::FALSE,::FALSE,1);
 
230
        $self->add( $container );
 
231
        $self->pack_end($status,::FALSE,::FALSE,1) if $::Options{OPT.'StatusBar'};
 
232
        $self->show_all;
 
233
        $back->signal_connect(clicked => sub { ::find_ancestor($_[0],__PACKAGE__)->go_back });
 
234
        $next->signal_connect(clicked => sub { ::find_ancestor($_[0],__PACKAGE__)->go_forward });
 
235
        $stop->signal_connect(clicked => sub { ::find_ancestor($_[0],__PACKAGE__)->stop_load });
 
236
        $open->signal_connect(clicked => sub { my $url=::find_ancestor($_[0],__PACKAGE__)->get_location; ::openurl($url) if $url=~m#^https?://# });
 
237
        return $self;
 
238
}
 
239
 
 
240
sub prefbox
 
241
{       my $vbox=Gtk2::VBox->new(::FALSE, 2);
 
242
        #my $combo=::NewPrefCombo(OPT.'Site',[sort keys %sites],'site : ',sub {$ID=undef;&Changed;});
 
243
        my $check=::NewPrefCheckButton(OPT.'StrippedWiki',_"Strip wikipedia pages",\&set_stripped_wiki,_"Remove header, footer and left column from wikipedia pages");
 
244
        my $Bopen=Gtk2::Button->new(_"open context window");
 
245
        $Bopen->signal_connect(clicked => sub { ::ContextWindow; });
 
246
        my ($radio_wk,$radio_moz)=
 
247
         ::NewPrefRadio( OPT.'Backend',
 
248
                sub { $check->set_sensitive($::Options{OPT.'Backend'} eq 'MozEmbed'); UpdateBackend(); },
 
249
                _"Use WebKit",          'WebKit',
 
250
                _"Use MozEmbed",        'MozEmbed');
 
251
        my $label_wk= $OKWebKit ? '' : _"Not found";
 
252
        my $label_moz= $OKMoz ? '' : $CrashMoz ? _"Found but not working" : _"Not found";
 
253
        $::Tooltips->set_tip($radio_wk,$label_wk) if $label_wk;
 
254
        $::Tooltips->set_tip($radio_moz,$label_moz) if $label_moz;
 
255
        $radio_wk->set_sensitive($OKWebKit);
 
256
        $radio_moz->set_sensitive($OKMoz);
 
257
        $check->set_sensitive($::Options{OPT.'Backend'} eq 'MozEmbed');
 
258
        my $check1=::NewPrefCheckButton(OPT.'DisableWiki',_"Disable wikipedia context tab",\&update_Context_hash);
 
259
        my $check2=::NewPrefCheckButton(OPT.'DisableLyrics',_"Disable Lyrics context tab",\&update_Context_hash);
 
260
        my $check3=::NewPrefCheckButton(OPT.'DisableLastfm',_"Disable Last.fm context tab",\&update_Context_hash);
 
261
        $vbox->pack_start($_,::FALSE,::FALSE,1) for $radio_wk,$radio_moz,Gtk2::VSeparator->new,$check1,$check2,$check3,$check,$Bopen;
 
262
        $vbox->set_sensitive( $OKMoz || $OKWebKit );
 
263
        return $vbox;
 
264
}
 
265
 
 
266
sub load_url
 
267
{       my ($self,$url,$post)=@_;
 
268
        $self->{url}=$url;
 
269
        $self->{post}=$post;
 
270
        if ($post)
 
271
        { Simple_http::get_with_cb(cb => sub {$self->loaded(@_)},url => $url,post => $post); }
 
272
        else {$self->Open($url);}
 
273
}
 
274
 
 
275
sub link_message
 
276
{       my ($self,$msg)=@_;
 
277
        $msg='' unless defined $msg;
 
278
        my $statusbar=$self->{status};
 
279
        $statusbar->pop( $statusbar->{id} );
 
280
        $statusbar->push( $statusbar->{id}, $msg );
 
281
}
 
282
 
 
283
sub set_stripped_wiki { GMB::Plugin::WebContext::MozEmbed::set_stripped_wiki( $::Options{OPT.'StrippedWiki'} ); } #FIXME
 
284
 
 
285
package GMB::Plugin::WebContext::Lyrics;
 
286
our @ISA=('GMB::Plugin::WebContext');
 
287
 
 
288
use constant
 
289
{       title => _"Lyrics",
 
290
        OPT => GMB::Plugin::WebContext::OPT,  #FIXME
 
291
};
 
292
 
 
293
my %sites=
 
294
(       #lyrc => ['lyrc','http://lyrc.com.ar/en/tema1en.php','artist=%a&songname=%s'],
 
295
        lyrc => ['lyrc','http://lyrc.com.ar/en/tema1en.php?artist=%a&songname=%s'],
 
296
        #leoslyrics => ['leolyrics','http://api.leoslyrics.com/api_search.php?artist=%a&songtitle=%s'],
 
297
        google  => ['google','http://www.google.com/search?q="%a"+"%s"'],
 
298
        googlemusic  => ['google music','http://www.google.com/musicsearch?q="%a"+"%s"'],
 
299
        lyriki  => ['lyriki','http://lyriki.com/index.php?title=%a:%s'],
 
300
        lyricwiki => ['lyricwiki','http://lyricwiki.org/%a:%s'],
 
301
);
 
302
 
 
303
::SetDefaultOptions(OPT, LyricSite => 'google');
 
304
 
 
305
sub addtoolbar
 
306
{       #my $self=$_[0];
 
307
        my %h= map {$_=>$sites{$_}[0]} keys %sites;
 
308
        my $cb=sub
 
309
         {      my $self=::find_ancestor($_[0],__PACKAGE__);
 
310
                $self->SongChanged($self->{ID},1);
 
311
         };
 
312
        my $combo=::NewPrefCombo( OPT.'LyricSite', \%h, undef, $cb);
 
313
        return $combo;
 
314
}
 
315
 
 
316
sub SongChanged
 
317
{       my ($self,$ID,$force)=@_;
 
318
        return unless defined $ID;
 
319
        return if defined $self->{ID} && $ID==$self->{ID} && !$force;
 
320
        $self->{ID}=$ID;
 
321
        my $ref=$::Songs[$ID];
 
322
        my $title=$ref->[::SONG_TITLE];
 
323
        return if !defined $title || $title eq '';
 
324
        my $artist=$ref->[::SONG_ARTIST];
 
325
        $artist=::url_escapeall($artist);
 
326
        $title= ::url_escapeall($title);
 
327
        my (undef,$url,$post)=@{$sites{$::Options{OPT.'LyricSite'}}};
 
328
        for ($url,$post) { next unless defined $_; s/%a/$artist/; s/%s/$title/; }
 
329
        ::IdleDo('8_mozlyrics'.$self,1000,sub {$self->load_url($url,$post)});
 
330
}
 
331
 
 
332
package GMB::Plugin::WebContext::Wikipedia;
 
333
our @ISA=('GMB::Plugin::WebContext');
 
334
 
 
335
use constant
 
336
{       title => _"Wikipedia",
 
337
        OPT => GMB::Plugin::WebContext::OPT,  #FIXME
 
338
};
 
339
 
 
340
my %locales=
 
341
(       en => 'English',
 
342
        fr => 'Fran�ais',
 
343
        de => 'Deutsch',
 
344
        pl => 'Polski',
 
345
        nl => 'Nederlands',
 
346
        sv => 'Svenska',
 
347
        it => 'Italiano',
 
348
        pt => 'Portugu�s',
 
349
        es => 'Espa�ol',
 
350
#       ja => "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\x0a",
 
351
);
 
352
#::_utf8_on( $locales{ja} );
 
353
 
 
354
::SetDefaultOptions(OPT, WikiLocale => 'en');
 
355
 
 
356
sub addtoolbar
 
357
{       #my $self=$_[0];
 
358
        my $cb=sub
 
359
         {      my $self=::find_ancestor($_[0],__PACKAGE__);
 
360
                $self->SongChanged($self->{ID},1);
 
361
         };
 
362
        my $combo=::NewPrefCombo( OPT.'WikiLocale', \%locales, undef, $cb);
 
363
        return $combo;
 
364
}
 
365
 
 
366
sub SongChanged
 
367
{       my ($self,$ID,$force)=@_;
 
368
        return unless defined $ID;
 
369
        $self->{ID}=$ID;
 
370
        my $artist=$::Songs[$ID][::SONG_ARTIST];
 
371
        return if !defined $artist || $artist eq '' || $artist eq '<Unknown>';
 
372
        return if defined $self->{Artist} && $artist eq $self->{Artist} && !$force;
 
373
        ($artist)=split /$::re_artist/o,$artist; #FIXME add a way to choose artist ?
 
374
        $self->{Artist}=$artist;
 
375
        $artist=::url_escapeall($artist);
 
376
        my $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/wiki/'.$artist;
 
377
        #my $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/w/index.php?title='.$artist.'&action=render';
 
378
        ::IdleDo('8_mozpedia'.$self,1000,sub {$self->load_url($url)});
 
379
        #::IdleDo('8_mozpedia'.$self,1000,sub {$self->wikiload});
 
380
}
 
381
 
 
382
sub wikiload    #not used for now
 
383
{       my $self=$_[0];
 
384
        my $url=::url_escapeall($self->{Artist});
 
385
        $url='http://'.$::Options{OPT.'WikiLocale'}.'.wikipedia.org/wiki/'.$url;
 
386
        #$url='http://google.com/search?q='.$url;
 
387
        $self->{url}=$url;
 
388
        Simple_http::get_with_cb(cb => sub
 
389
                {       my $cb=sub { $self->wikifilter(@_) };
 
390
                        if (!$_[0] || $_[0]=~m/No page with that title exists/)
 
391
                        {       Simple_http::get_with_cb(cb => $cb, url => $url);
 
392
                        }
 
393
                        else { $self->{url}.='_(band)'; goto $cb }
 
394
                },url => $url.'_(band)');
 
395
}
 
396
 
 
397
sub wikifilter  #not used for now
 
398
{       my ($self,$data,$type)=@_;
 
399
        return unless $data;    #FIXME
 
400
        #$data='<style type="text/css">.firstHeading {display: none}</style>'.$data;
 
401
        $self->loaded($data,$type);
 
402
}
 
403
 
 
404
package GMB::Plugin::WebContext::Lastfm;
 
405
our @ISA=('GMB::Plugin::WebContext');
 
406
 
 
407
use constant
 
408
{       title => "Last.fm",
 
409
#       OPT => GMB::Plugin::WebContext::OPT,  #FIXME
 
410
};
 
411
 
 
412
sub addtoolbar
 
413
{       #my $self=$_[0];
 
414
        #my $combo=Gtk2::ComboBox->new_text;
 
415
        #return $combo;
 
416
}
 
417
 
 
418
sub SongChanged
 
419
{       my ($self,$ID,$force)=@_;
 
420
        return unless defined $ID;
 
421
        $self->{ID}=$ID;
 
422
        my $artist=$::Songs[$ID][::SONG_ARTIST];
 
423
        return if !defined $artist || $artist eq '' || $artist eq '<Unknown>';
 
424
        return if defined $self->{Artist} && $artist eq $self->{Artist} && !$force;
 
425
        $self->{Artist}=$artist;
 
426
        $artist=~s/ /+/g;
 
427
        $artist=::url_escapeall($artist);
 
428
        my $url='http://www.last.fm/music/'.$artist;
 
429
        ::IdleDo('8_mozlastfm'.$self,1000,sub {$self->load_url($url)});
 
430
}
 
431
 
 
432
 
 
433
1
 
434