summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/drakdvb
blob: 1f4ded8fc8c6bd767a3b6aeeee890812123a9d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/perl
#
# Copyright (C) 2009 Mandriva
#                    Pascal Terjan <pterjan@mandriva.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use lib qw(/usr/lib/libDrakX);

use common;
use standalone;
use mygtk2;
use interactive;
use ugtk2 qw(:create :helpers :wrappers);
use Gtk2::SimpleList;

my $title = N("DVB");
$ugtk2::wm_icon = "/usr/share/mcc/themes/default/tv-mdk.png";
my $w = ugtk2->new($title);
$w->{window}->signal_connect('destroy' => \&exitapp);
$::main_window = $w->{real_window};
my $in = 'interactive'->vnew;

my $config_file = "$ENV{HOME}/.mplayer/channels.conf";

my $channel_list = Gtk2::SimpleList->new(N("Channel") => "text", "id" => "hidden");
my %buttons;
my $pid;
my $_w;

sub get_selected_channel() {
    my ($index) = $channel_list->get_selected_indices;
    defined $index && $channel_list->{data}[$index][1];
}

sub exitapp() {
    local $SIG{TERM} = 'IGNORE';
    kill TERM => -$$;
    Gtk2->main_quit;
}

sub detect_channels() {
    if (-e $config_file) {
        $in->ask_okcancel(N("Warning"), N("%s already exists and its contents will be lost", $config_file)) or return;
    }
    gtkset_mousecursor_wait($w->{window}->window);
    $channel_list->set_sensitive(0);
    $_->set_sensitive(0) foreach values %buttons;

    gtkflush();

    $SIG{CHLD} = sub {
        $SIG{CHLD} = 'IGNORE';
        $_w->{window}->hide;
        waitpid($pid, 0);
        if (($? >> 8) == 0) {
            load_channels();
        } else {
            $in->ask_warn(N("Error"), N("Could not get the list of available channels"));
        }
        gtkset_mousecursor_normal($w->{window}->window);
        $buttons{detect}->set_sensitive(1);
        $channel_list->set_sensitive(1);
    };
    $_w = ugtk2->new(N("Please wait"), grab => 1);
    $_w->{window}->signal_connect('destroy' => \&exitapp);
    if ($pid = fork()) {
        gtkadd($_w->{window},
               gtkpack($_w->create_box_with_title(N("Detecting DVB channels, this will take a few minutes")),
                       my $w = gtknew('Button', text => N("Cancel"), clicked => \&exitapp),
               )
            );
        $_w->main;
    } else {
        $SIG{CHLD} = 'DEFAULT';
        mkdir_p(dirname($config_file));
        my $ret = system("w_scan -X > $config_file");
        sleep(1);
        POSIX::_exit($ret >> 8);
    }
}

sub load_channels() {
    @{$channel_list->{data}} = ();
    open(my $CHANNELCONF, "<$config_file");
    while (<$CHANNELCONF>) {
        my $line = $_;
        if ($line =~ /^([^:]*?)(\([^(:]*\))?:/) {
            push @{$channel_list->{data}}, [ $1, $1 . $2 ];
        }
    }
    close($CHANNELCONF);
    if (defined @{$channel_list->{data}}[0]) {
        $channel_list->select(0);
    }
}

sub launch_tv() {
    system('mplayer "dvb://' . get_selected_channel() . '"&');
}

my $view_channel_button;

gtkadd($w->{window},
       gtknew('VBox', spacing => 5, children => [
                  $::isEmbedded ? () : (0, Gtk2::Banner->new($ugtk2::wm_icon, $title)),
                  1, gtknew('ScrolledWindow', width => 300, height => 400, child => $channel_list),
                  0, gtknew('HButtonBox', layout => 'end', children_loose => [
                                $buttons{detect} = gtknew('Button', text => N("Detect Channels"), clicked => \&detect_channels),
                                gtknew('Button', text => N("Quit"), clicked => \&exitapp),
                                $buttons{view} = gtknew('Button', text => N("View Channel"), clicked => \&launch_tv),
                            ]),
              ]),
    );

$buttons{view}->set_sensitive(0);

$channel_list->get_selection->signal_connect('changed' => sub {
    my ($index) = $channel_list->get_selected_indices;
    $buttons{view}->set_sensitive(defined $index);
});

$channel_list->signal_connect('row-activated' => sub {
    launch_tv();
});

load_channels();

$w->main;