~ubuntu-branches/ubuntu/maverick/qvamps/maverick

« back to all changes in this revision

Viewing changes to DvdMaker.pm

  • Committer: Bazaar Package Importer
  • Author(s): Claudio Moratti
  • Date: 2006-04-24 14:28:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060424142830-mf5kk65fv3xc2ztz
Tags: upstream-0.97
ImportĀ upstreamĀ versionĀ 0.97

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of qVamps.
 
2
#
 
3
# qVamps is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# qVamps is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with qVamps; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 
 
17
 
 
18
use strict;
 
19
use warnings;
 
20
 
 
21
 
 
22
package DvdMaker;
 
23
use Qt;
 
24
use Qt::isa qw (Qt::Object);
 
25
use Qt::attributes qw (proc proc_running log_viewer
 
26
                       default_color hilite_errors outbuf);
 
27
use Qt::slots read_output => [],
 
28
              proc_exited => [];
 
29
use QVamps qw (read_setting read_list_setting replace error tr);
 
30
use File::Path;
 
31
 
 
32
 
 
33
# DvdMaker (log_viewer, parent, name)
 
34
sub NEW
 
35
{
 
36
  my $this       = shift;
 
37
  my $log_viewer = shift;
 
38
 
 
39
  $this -> SUPER::NEW (@_);
 
40
 
 
41
  my $proc      = Qt::Process (this, "DvdMaker::proc");
 
42
  proc          = $proc;
 
43
  log_viewer    = $log_viewer;
 
44
  default_color = $log_viewer -> paletteForegroundColor ();
 
45
  hilite_errors = 0;
 
46
  outbuf        = [ "", "" ];
 
47
 
 
48
  $proc -> setCommunication (&Qt::Process::Stdout | &Qt::Process::Stderr);
 
49
 
 
50
  this -> connect ($proc, SIGNAL "readyReadStdout()", SLOT "read_output()");
 
51
  this -> connect ($proc, SIGNAL "readyReadStderr()", SLOT "read_output()");
 
52
  this -> connect ($proc, SIGNAL "processExited()", SLOT "proc_exited()");
 
53
 
 
54
  $log_viewer -> clear ();
 
55
}
 
56
 
 
57
 
 
58
sub DESTROY
 
59
{
 
60
#  print STDERR "DvdMaker::DESTROY\n";
 
61
 
 
62
  append_output (0, "\n") if (outbuf -> [0]);
 
63
  append_output (1, "\n") if (outbuf -> [1]);
 
64
 
 
65
  removeChild (proc);
 
66
  parent () -> removeChild (this);
 
67
 
 
68
  proc          = undef;
 
69
  log_viewer    = undef;
 
70
  default_color = undef;
 
71
  outbuf        = undef;
 
72
 
 
73
  SUPER -> DESTROY ();
 
74
}
 
75
 
 
76
 
 
77
sub launch
 
78
{
 
79
  my $args = shift;
 
80
 
 
81
  my $proc = proc;
 
82
 
 
83
  $proc -> setArguments ($args);
 
84
 
 
85
  unless ($proc -> start ())
 
86
  {
 
87
    error (sprintf '%s: "%s"',
 
88
           &tr ("Failed launching command"), join (" ", @{$args}));
 
89
 
 
90
    return 0x200;
 
91
  }
 
92
 
 
93
  my $event_loop = Qt::Application::eventLoop ();
 
94
 
 
95
  proc_running = 1;
 
96
 
 
97
  while (proc_running)
 
98
  {
 
99
    $event_loop -> processEvents (&Qt::EventLoop::WaitForMore);
 
100
  }
 
101
 
 
102
  read_output ();
 
103
 
 
104
  unless ($proc -> normalExit ())
 
105
  {
 
106
    log_viewer -> setColor (Qt::Color (0xff, 0, 0));
 
107
    log_viewer -> append (&tr ("Command execution terminated abnormally"));
 
108
    log_viewer -> setColor (default_color);
 
109
 
 
110
    return 0x100;
 
111
  }
 
112
 
 
113
  return $proc -> exitStatus ();
 
114
}
 
115
 
 
116
 
 
117
sub launch_sh_cmd
 
118
{
 
119
  my $hilite = shift;
 
120
  my $cmd    = shift;
 
121
 
 
122
  hilite_errors = $hilite;
 
123
  my $lv        = log_viewer;
 
124
 
 
125
  $lv -> setColor (Qt::Color (0, 0, 0xff));
 
126
  $lv -> append (sprintf "%s \"%s\"\n\n", &tr ("Executing"), $cmd);
 
127
  $lv -> setColor (default_color);
 
128
 
 
129
  $cmd = "exec $cmd" unless ($cmd =~ /[|;]/);
 
130
 
 
131
  return launch ([ "/bin/sh", "-c", $cmd ]);
 
132
}
 
133
 
 
134
 
 
135
sub try_terminate
 
136
{
 
137
  proc -> tryTerminate ();
 
138
}
 
139
 
 
140
 
 
141
sub append_output
 
142
{
 
143
  my $e = shift;        # 0: stdout, 1: stderr
 
144
  my $s = shift;        # string to append
 
145
 
 
146
  # flush output, if string contains backspace
 
147
  my $flush = ($s =~ s/[\b]+//g);
 
148
 
 
149
  # replace return(s) with a newline and remove unwanted lines
 
150
  $s =~ s/\r+/\n/g;
 
151
  $s =~ s/^libdvdread: .+\n//mg;
 
152
 
 
153
  $s             = outbuf -> [$e] . $s;
 
154
  outbuf -> [$e] = !$flush && ($s =~ s/(.+)\z//) ? $1 : "";
 
155
 
 
156
  my $lv = log_viewer;
 
157
 
 
158
  $lv -> setColor (Qt::Color (0xff, 0, 0)) if ($e && hilite_errors);
 
159
  $lv -> append ($s);
 
160
  $lv -> setColor (default_color);
 
161
}
 
162
 
 
163
 
 
164
sub read_output : SLOT()
 
165
{
 
166
  my $proc = proc;
 
167
 
 
168
  while (my $s = $proc -> readStdout ())
 
169
  {
 
170
    append_output (0, $s);
 
171
  }
 
172
 
 
173
  while (my $s = $proc -> readStderr ())
 
174
  {
 
175
    append_output (1, $s);
 
176
  }
 
177
}
 
178
 
 
179
 
 
180
sub proc_exited : SLOT()
 
181
{
 
182
  proc_running = 0;
 
183
}
 
184
 
 
185
 
 
186
sub run_spumux
 
187
{
 
188
  my $base_path = shift;
 
189
  my $make_menu = shift;
 
190
  my $rm_temps  = shift;
 
191
 
 
192
  my $rc = 0;
 
193
 
 
194
  if ($make_menu)
 
195
  {
 
196
    my $spumux;
 
197
    my $bg_file  = parent () -> background_filename ();
 
198
    my $menu_xml = "$base_path/menu/spumux.xml";
 
199
    my $menu_mpg = "$base_path/menu.mpg";
 
200
 
 
201
    if ($bg_file =~ /\.mpe?g$/)
 
202
    {
 
203
      $spumux = read_setting ("/Dvdauthor/spumux_mpg");
 
204
      $spumux = replace ($spumux, { "i" => $bg_file,
 
205
                                    "x" => $menu_xml,
 
206
                                    "o" => $menu_mpg });
 
207
    }
 
208
    else
 
209
    {
 
210
      my $video_es = "$base_path/menu/video.mpv";
 
211
      my $prefix   = read_setting ("/Installation/prefix");
 
212
      my $audio_es = read_setting ("/Menu/Preferences/audio_es");
 
213
      $audio_es    = replace ($audio_es, { "p" => $prefix });
 
214
      $spumux      = read_setting ("/Dvdauthor/spumux_mpv");
 
215
      $spumux      = replace ($spumux, { "v" => $video_es,
 
216
                                         "a" => $audio_es,
 
217
                                         "x" => $menu_xml,
 
218
                                         "o" => $menu_mpg });
 
219
    }
 
220
 
 
221
    $rc = launch_sh_cmd (0, $spumux);
 
222
 
 
223
    rmtree ("$base_path/menu") if ($rm_temps);
 
224
  }
 
225
 
 
226
  return $rc ? -1 : 0;
 
227
}
 
228
 
 
229
 
 
230
sub run_dvdauthor
 
231
{
 
232
  my $base_path = shift;
 
233
  my $rm_temps  = shift;
 
234
 
 
235
  my $vamps_inj = "$base_path/vamps.inj";
 
236
 
 
237
  rmtree ("$base_path/image");
 
238
  unlink ($vamps_inj);
 
239
 
 
240
  my $dvdauthor_xml = "$base_path/dvdauthor.xml";
 
241
  my $dvdauthor     = read_setting ("/Dvdauthor/command");
 
242
  $dvdauthor        = replace ($dvdauthor, { "x" => $dvdauthor_xml });
 
243
 
 
244
  my $rc = launch_sh_cmd (0, $dvdauthor);
 
245
 
 
246
  unlink ($vamps_inj) if ($rm_temps);
 
247
 
 
248
  return $rc ? -1 : 0;
 
249
}
 
250
 
 
251
 
 
252
sub run_burn_cmd
 
253
{
 
254
  my $base_path = shift;
 
255
  my $vol_id    = shift;
 
256
 
 
257
  my $burner     = read_setting ("/General/DVD_burner");
 
258
  my $burn_speed = read_setting ("/General/DVD_burn_speed");
 
259
  my $img_dir    = "$base_path/image";
 
260
  my $burn_cmds  = read_list_setting ("/Dvdauthor/burn_cmds");
 
261
  my $bcmds_item = read_setting ("/Dvdauthor/burn_cmds_item");
 
262
  my $burn_cmd   = $burn_cmds -> [$bcmds_item];
 
263
  $burn_cmd      = replace ($burn_cmd, { "d" => $burner,
 
264
                                         "s" => $burn_speed,
 
265
                                         "v" => $vol_id,
 
266
                                         "i" => $img_dir });
 
267
 
 
268
  my $rc = launch_sh_cmd (1, $burn_cmd);
 
269
 
 
270
  return $rc ? -1 : 0;
 
271
}
 
272
 
 
273
 
 
274
sub write_script
 
275
{
 
276
  my $base_path   = shift;
 
277
  my $make_menu   = shift;
 
278
  my $disk_change = shift;
 
279
  my $rm_temps    = shift;
 
280
  my $vol_id      = shift;
 
281
 
 
282
  my $make_menu_bool   = $make_menu   ? "true" : "false";
 
283
  my $disk_change_bool = $disk_change ? "true" : "false";
 
284
  my $rm_temps_bool    = $rm_temps    ? "true" : "false";
 
285
  my $burner           = read_setting ("/General/DVD_burner");
 
286
  my $burn_speed       = read_setting ("/General/DVD_burn_speed");
 
287
  my $prefix           = read_setting ("/Installation/prefix");
 
288
  my $audio_es         = read_setting ("/Menu/Preferences/audio_es");
 
289
  $audio_es            = replace ($audio_es, { "p" => $prefix });
 
290
  my $bg_file          = parent () -> background_filename ();
 
291
  my $menu_ps          = $bg_file =~ /\.mpe?g$/ ? $bg_file : "";
 
292
  my $prompt           =
 
293
    &tr ("Please insert blank DVD into burner and press <RETURN> key");
 
294
  my $spumux_mpv       = read_setting ("/Dvdauthor/spumux_mpv");
 
295
  $spumux_mpv          = replace ($spumux_mpv, { "v" => '$video_es',
 
296
                                                 "a" => '$audio_es',
 
297
                                                 "x" => '$menu_xml',
 
298
                                                 "o" => '$menu_mpg' });
 
299
  my $spumux_mpg       = read_setting ("/Dvdauthor/spumux_mpg");
 
300
  $spumux_mpg          = replace ($spumux_mpg, { "i" => '$menu_ps',
 
301
                                                 "x" => '$menu_xml',
 
302
                                                 "o" => '$menu_mpg' });
 
303
  my $dvdauthor        = read_setting ("/Dvdauthor/command");
 
304
  $dvdauthor           = replace ($dvdauthor, { "x" => '$dvdauthor_xml' });
 
305
  my $burn_cmds        = read_list_setting ("/Dvdauthor/burn_cmds");
 
306
  my $bcmds_item       = read_setting ("/Dvdauthor/burn_cmds_item");
 
307
  my $burn_cmd         = $burn_cmds -> [$bcmds_item];
 
308
  $burn_cmd            = replace ($burn_cmd, { "d" => '$burner',
 
309
                                               "s" => '$burn_speed',
 
310
                                               "v" => '$vol_id',
 
311
                                               "i" => '$img_path' });
 
312
 
 
313
  my $file = "$base_path/run_me";
 
314
 
 
315
  unless (open SCR, ">", $file)
 
316
  {
 
317
    error (sprintf "%s: %s: %s",
 
318
           &tr ("Failed creating shell script"), $file, $!);
 
319
 
 
320
    return -1;
 
321
  }
 
322
 
 
323
  print SCR << "EOT";
 
324
#! /bin/sh
 
325
 
 
326
# flags
 
327
make_menu="$make_menu_bool"
 
328
disk_change="$disk_change_bool"
 
329
rm_temps="$rm_temps_bool"
 
330
 
 
331
# settings for burning
 
332
burner="$burner"
 
333
burn_speed="$burn_speed"
 
334
vol_id="$vol_id"
 
335
 
 
336
# paths and files
 
337
base_path="$base_path"
 
338
img_path="\$base_path/image"
 
339
dvdauthor_xml="\$base_path/dvdauthor.xml"
 
340
menu_mpg="\$base_path/menu.mpg"
 
341
vamps_inj="\$base_path/vamps.inj"
 
342
menu_path="\$base_path/menu"
 
343
menu_xml="\$menu_path/spumux.xml"
 
344
video_es="\$menu_path/video.mpv"
 
345
audio_es="$audio_es"
 
346
menu_ps="$menu_ps"
 
347
 
 
348
# other
 
349
prompt="$prompt"
 
350
 
 
351
 
 
352
if \$make_menu; then
 
353
  if [ -z "\$menu_ps" ]; then
 
354
    $spumux_mpv
 
355
  else
 
356
    $spumux_mpg
 
357
  fi
 
358
 
 
359
  \$rm_temps && rm -rf \$menu_path
 
360
fi
 
361
 
 
362
rm -rf \$img_path \$vamps_inj
 
363
 
 
364
$dvdauthor
 
365
 
 
366
\$rm_temps && rm -f \$vamps_inj
 
367
 
 
368
\$disk_change && { echo -n "\$prompt"; read x; }
 
369
 
 
370
$burn_cmd
 
371
EOT
 
372
 
 
373
  CORE::close SCR;
 
374
 
 
375
  chmod 0777 & ~umask (), $file;
 
376
 
 
377
  return 0;
 
378
}
 
379
 
 
380
 
 
381
1;