File: downloader.pl

package info (click to toggle)
libgnome2-vfs-perl 1.080-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 468 kB
  • ctags: 71
  • sloc: perl: 1,195; ansic: 446; makefile: 53
file content (221 lines) | stat: -rw-r--r-- 6,449 bytes parent folder | download | duplicates (6)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl -w

use Switch;
use Gtk2 -init;
use Gnome2::VFS -init;

###############################################################################

package Gtk2::CellRendererProgress;

use Glib::Object::Subclass
  Gtk2::CellRenderer::,
  properties => [
    Glib::ParamSpec -> double("progress",
                              "Progress",
                              "Are we there, yet?",
                              0.0, 1.0, 0.0,
                              [qw(readable writable)])
  ]
;

use constant x_padding => 2;
use constant y_padding => 3;

sub INIT_INSTANCE {
  my ($self) = @_;
  $self -> { progress } = 0.0;
}

sub GET_SIZE {
  my ($cell, $widget, $cell_area) = @_;
  my ($width, $height) = (100, 10);

  if (defined($cell_area)) {
    $width = $cell_area -> width() - x_padding * 2;
    $height = $cell_area -> height() - y_padding * 2;
  }

  return (0,
          0,
          $width + x_padding * 2,
          $height + y_padding * 2);
}

sub RENDER {
  my ($cell, $window, $widget, $background_area, $cell_area, $expose_area, $flags) = @_;
  my ($x_offset, $y_offset, $width, $height) = $cell -> GET_SIZE($widget, $cell_area);

  if ($cell -> { progress } > 0) {
    $widget -> get_style -> paint_box($window,
                                      $flags & "selected" ? "normal" : "prelight",
                                      "out",
                                      $cell_area,
                                      $widget,
                                      undef,
                                      $cell_area -> x() + $x_offset,
                                      $cell_area -> y() + $y_offset,
                                      $width * $cell -> { progress },
                                      $height - 1);
  }
}

###############################################################################

package main;

use Cwd qw(cwd);

use constant {
  COLUMN_ADDRESS => 0,
  COLUMN_STATUS => 1,
  COLUMN_PROGRESS => 2
};

my $window = Gtk2::Window -> new("toplevel");

$window -> set_title("Async Downloader");
$window -> set_border_width(5);
$window -> set_default_size(100, 200);

$window -> signal_connect(delete_event => sub { quit(); });

my $vbox = Gtk2::VBox -> new(0, 5);

my $hbox = Gtk2::HBox -> new(0, 5);

my $entry = Gtk2::Entry -> new();
my $button = Gtk2::Button -> new("_Download");

$button -> signal_connect(clicked => sub {
  start_new_download($entry -> get_text());
});
$entry -> signal_connect(activate => sub {
  $button->clicked;
});

$hbox -> pack_start($entry, 1, 1, 0);
$hbox -> pack_start($button, 0, 0, 0);

$vbox -> pack_start($hbox, 0, 0, 0);

my $model = Gtk2::ListStore -> new(qw(Glib::String Glib::String Glib::Double));
my $view = Gtk2::TreeView -> new($model);

my $column_address =
  Gtk2::TreeViewColumn -> new_with_attributes("Address",
                                              Gtk2::CellRendererText -> new(),
                                              text => COLUMN_ADDRESS);

my $column_status =
  Gtk2::TreeViewColumn -> new_with_attributes("Status",
                                              Gtk2::CellRendererText -> new(),
                                              text => COLUMN_STATUS);

my $column_progress =
  Gtk2::TreeViewColumn -> new_with_attributes("Progress",
                                              Gtk2::CellRendererProgress -> new(),
                                              progress => COLUMN_PROGRESS);

$column_address -> set_sizing("autosize");
$column_status -> set_sizing("autosize");

$view -> append_column($column_address);
$view -> append_column($column_status);
$view -> append_column($column_progress);

$vbox -> pack_start($view, 1, 1, 0);

$window -> add($vbox);
$window -> show_all();

Gtk2 -> main();

###############################################################################

my %iters;

sub start_new_download {
  my ($address) = @_;

  return unless ($address ne "");

  my $source = Gnome2::VFS::URI -> new($address);
  my $target = Gnome2::VFS::URI -> new(cwd() . "/" . $source -> extract_short_path_name());

  my $source_string = $source -> to_string();

  my ($result, $handle) = Gnome2::VFS::Async -> xfer([$source],
                                                     [$target],
                                                     qw(default),
                                                     qw(query),
                                                     qw(query),
                                                     0,
                                                     \&progress_update,
                                                     $source_string,
                                                     \&progress_sync,
                                                     $source_string);

  if ($result eq "ok") {
    my $iter = $model -> append();

    $entry -> set_text("");
    $model -> set($iter, COLUMN_ADDRESS, $source_string);

    $iters{ $source_string } = $iter; # -> copy();
  }
  else {
    $handle -> cancel();
  }
}

sub progress_update {
  my ($handle, $info, $source_string) = @_;
  my $status;

  switch ($info -> { phase }) {
    case "phase-completed"      { $status = "Completed"; }
    case "phase-initial"        { $status = "Initializing"; }
    case "checking-destination" { $status = "Checking destination"; }
    case "phase-collecting"     { $status = "Collecting"; }
    case "phase-readytogo"      { $status = "Ready to go"; }
    case "phase-opentarget"     { $status = "Opening target"; }
    case "phase-copying"        { $status = "Copying"; }
    else                        { warn $info -> { phase }; $status = ""; }
  }

  $model -> set($iters{ $source_string },
                COLUMN_STATUS, $status);

  if (defined($info -> { file_size }) &&
      defined($info -> { bytes_copied }) &&
      $info -> { file_size } != 0) {
    $model -> set($iters{ $source_string },
                  COLUMN_PROGRESS,
                  $info -> { bytes_copied } / $info -> { file_size });
  }
}

sub progress_sync {
  my ($info) = @_;

  if ($info -> { status } eq "ok") {
    return 1;
  }
  elsif ($info -> { status } eq "vfserror") {
    warn $info -> { vfs_status };
    return "abort";
  }
  elsif ($info -> { status } eq "overwrite") {
    return "abort";
  }

  return 0;
}

###############################################################################

sub quit {
  Gtk2 -> main_quit();
  Gnome2::VFS -> shutdown();
}