summaryrefslogtreecommitdiff
path: root/rhimport
blob: 2e4729ef543c3a3f05c38f445ce15c16ca4a2ea0 (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/perl -w
#
#
#  rhimport
#
#  Copyright (C) 2009 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhimport.
#
#  rhimport 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 3 of the License, or
#  any later version.
#
#  rhimport 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 rhimport. If not, see <http://www.gnu.org/licenses/>.
#

use strict;

use Getopt::Long;
use DBI;
use Gtk2;
use Gtk2::GladeXML;

use lib '/usr/local/share/rhimport/';
use rhimport;
my $glade_file = '/usr/local/share/rhimport/rhimport.glade';

my $DBHOST = "airplay";
my $DBUSER = "rivendell";
my $DBPW = "lldriven";
my $DB = "rivendell";
my $HELP = 0;
my $FILE = "";
my $POOL = 0;
my $KEEPCARTS = 0;
my $DROPBOX = "";
my $LISTALLOWED = 0;

GetOptions ("help!" => \$HELP,
            "file=s" => \$FILE,
            "pool!" => \$POOL,
            "keep-carts!" => \$KEEPCARTS,
            "dropbox=s" => \$DROPBOX,
            "list-allowed!" => \$LISTALLOWED,
             ) or die();

if($HELP) {
  print << "EOF";
usage: $0 --file <audio or playlist file> --pool --keep-carts --dropbox <path to dropbox> --list-allowed

When called with no file and/or no dropbox the GUI gets started to select the missing parameters

options:
     -f | --file          the media file or playlist to import
     -p | --pool          pool mode, import media files referneced by playlist
     -k | --keep-carts    in pool mode keep existing carts instead of clearing them before import
     -d | --dropbox       the path to the dropbox to use
     -l | --list-allowed  list allowed dropboxes and exit

EOF
  exit 0;
}

my $user = `/usr/bin/id -un`;
$user =~ s/\n//;

my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr";
my @allowed_dbs = rhimport::get_dropboxes($dbh, $user);

if($LISTALLOWED) {
  for my $href ( @allowed_dbs ) {
    print "$href->{'NAME'} \t-> $href->{'PATH'}\n";
  }
  $dbh->disconnect();
  exit 0;
}

###########################################
## GUI mode

my $guixml;

sub set_mode_playlist_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }
  
  $POOL = 1;
  my $l_mode = $guixml->get_widget('l_mode');
  $l_mode->set_label("<b>Musik Pool</b>");
  my $cb_clear_carts = $guixml->get_widget('cb_clear_carts');
  $cb_clear_carts->set_sensitive(1);
  my $l_file_playlist = $guixml->get_widget('l_file_playlist');
  $l_file_playlist->set_label("<b>Playlist</b>");
  my $filter = Gtk2::FileFilter->new;
  $filter->add_pattern("*.m3u");
  my $filechooser = $guixml->get_widget('filechooser');
  $filechooser->set_filter($filter);
}

sub set_mode_file_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }
  
  $POOL = 0;
  my $l_mode = $guixml->get_widget('l_mode');
  $l_mode->set_label("<b>Sendung</b>");
  my $cb_clear_carts = $guixml->get_widget('cb_clear_carts');
  $cb_clear_carts->set_sensitive(0);
  my $l_file_playlist = $guixml->get_widget('l_file_playlist');
  $l_file_playlist->set_label("<b>Datei</b>");
  my $filter = Gtk2::FileFilter->new;
  $filter->add_pattern("*.wav");
  $filter->add_pattern("*.WAV");
  $filter->add_pattern("*.ogg");
  $filter->add_pattern("*.OGG");
  $filter->add_pattern("*.flac");
  $filter->add_pattern("*.FLAC");
  $filter->add_pattern("*.fla");
  $filter->add_pattern("*.FLA");
  $filter->add_pattern("*.mp*");
  $filter->add_pattern("*.MP*");
  my $filechooser = $guixml->get_widget('filechooser');
  $filechooser->set_filter($filter);
}

sub toggle_mode_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }

  my $co_dropbox = $guixml->get_widget('co_dropbox');
  my $to_cart = $allowed_dbs[$co_dropbox->get_active]->{'TO_CART'};

  my $response = 'no';
  if(($to_cart == 0 && $POOL) ||
     ($to_cart != 0 && !$POOL)) {
    my $question = "Diese Sendung hat ";
    if($to_cart != 0) {
      $question .= "nur ein Cart";
    } else {
      $question .= "mehrere Carts";
    }
    $question .= ". Soll der Modus trotzdem gewechselt werden?";
    
    my $parent_window = $guixml->get_widget('appwin');
    my $dialog = Gtk2::MessageDialog->new ($parent_window, 'destroy-with-parent',
                                           'question', 'yes-no', $question);
    $dialog->set_default_response ('no');
    $response = $dialog->run;
    $dialog->destroy;
  }
  else {
    $response = 'yes';
  }

  if($response eq 'yes') {
    if($POOL) {
      set_mode_file_gui();
    } else {
      set_mode_playlist_gui();
    }
  }
}

my $gui_progress_cb = sub {
  my ($elapsed, $max, $text) = @_;

  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }
  my $progressbar = $guixml->get_widget('progressbar');
  $progressbar->set_text("($elapsed/$max) $text");
  $progressbar->set_fraction($elapsed/$max);
  while (Gtk2->events_pending) {
    Gtk2->main_iteration;
  }
  Gtk2::Gdk->flush;
};

my $gui_error_cb = sub {
  my ($text) = @_;

  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }
  $text .= "\n\nVorgang abbrechen?";
  my $parent_window = $guixml->get_widget('appwin');
  my $dialog = Gtk2::MessageDialog->new ($parent_window, 'destroy-with-parent',
                                         'error', 'yes-no', $text);
  $dialog->set_default_response ('no');
  my $response = $dialog->run;
  $dialog->destroy;

  return 0 if($response eq 'yes');

  return 1;
};

sub start_import_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }

  my $l_status = $guixml->get_widget('l_status');

  my $filechooser = $guixml->get_widget('filechooser');
  my $file = $filechooser->get_filename;
  if(!$file || -d $file) {
    if($POOL) {
      $l_status->set_label("Keine Playlist ausgewhlt!");
    } else {
      $l_status->set_label("Keine Audio Datei ausgewhlt!");
    }
    return 0;
  }
  $filechooser->unselect_all;

  my $co_dropbox = $guixml->get_widget('co_dropbox');
  my $dropbox = $allowed_dbs[$co_dropbox->get_active]->{'PATH'};
  my $group = $allowed_dbs[$co_dropbox->get_active]->{'GROUP'};
  my $to_cart = $allowed_dbs[$co_dropbox->get_active]->{'TO_CART'};

  $l_status->set_label("importiere von $file");

  my $ret;
  my $log;
  if($POOL) {
    my $cb_clear_carts = $guixml->get_widget('cb_clear_carts');
    if($cb_clear_carts->get_active) {
      rhimport::clear_carts($dbh, $group, $to_cart, $gui_progress_cb);
    }
    ($ret, $log) = rhimport::import_playlist($file, $dropbox, $gui_progress_cb, $gui_error_cb);
  } else {
    rhimport::clear_carts($dbh, $group, $to_cart, $gui_progress_cb);
    ($ret, $log) = rhimport::import_single($file, $dropbox, $gui_progress_cb, $gui_error_cb);
  }

  if(!$ret) {
    $l_status->set_label("Fehler beim Importieren");
  }

  return $ret;
}

sub show_used_carts_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }

  my $co_dropbox = $guixml->get_widget('co_dropbox');
  my $group = $allowed_dbs[$co_dropbox->get_active]->{'GROUP'};


  my $l_status = $guixml->get_widget('l_status');
  my ($low_cart, $high_cart) = rhimport::get_cart_range($dbh, $group);
  if($low_cart eq $high_cart) {
    $l_status->set_label("Cart: $low_cart");
  } else {
    $l_status->set_label("Carts: $low_cart - $high_cart");
  }

#  my $usedcartswin = $guixml->get_widget('usedcartswin');
#  $usedcartswin or die "can't find Widget";
#  $usedcartswin->resize(640,480);
#  $usedcartswin->show;
}

sub clear_status_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }

  my $l_status = $guixml->get_widget('l_status');
  $l_status->set_label("");
  
  my $progressbar = $guixml->get_widget('progressbar');
  $progressbar->set_text("");
  $progressbar->set_fraction(0);
}

sub dropbox_updated_gui()
{
  if(!$guixml) {
    print STDERR "no GUI definition found!\n";
    exit 2;
  }

  my $co_dropbox = $guixml->get_widget('co_dropbox');
  my $to_cart = $allowed_dbs[$co_dropbox->get_active]->{'TO_CART'};
  my $group = $allowed_dbs[$co_dropbox->get_active]->{'GROUP'};
  my ($low_cart, $high_cart) = rhimport::get_cart_range($dbh, $group);

  if($to_cart == 0) {
    set_mode_playlist_gui();
  } else {
    set_mode_file_gui();
  }
}

if(!$FILE || !$DROPBOX) {
  Gtk2->init;

  $guixml = Gtk2::GladeXML->new($glade_file);
  $guixml or die "can't load glade xml file";
  require gui_callbacks;

  my $model = Gtk2::ListStore->new('Glib::String', 'Glib::String', 'Glib::String', 'Glib::String');
  for my $href ( @allowed_dbs ) {
    $model->set ($model->append, 0, $href->{'PATH'}, 1, $href->{'GROUP'}, 2, $href->{'TO_CART'}, 3, $href->{'NAME'});
  }
  my $co_dropbox = $guixml->get_widget('co_dropbox');
  $co_dropbox->set_model($model);
  my $renderer = Gtk2::CellRendererText->new;
  $co_dropbox->pack_start($renderer, 1);
  $co_dropbox->add_attribute($renderer, text => 3);
  $co_dropbox->set_active(0);
  dropbox_updated_gui();

  my $cb_clear_carts = $guixml->get_widget('cb_clear_carts');
  if($KEEPCARTS) {
    $cb_clear_carts->set_active(0);
  } else {
    $cb_clear_carts->set_active(1);
  }

  $guixml->signal_autoconnect_from_package('gui_callbacks');
  my $appwin = $guixml->get_widget('appwin');
  $appwin or die "can't find Main Window";
  $appwin->resize(800,600);
  $appwin->show;
  
  Gtk2->main;
  $dbh->disconnect();
  exit 0;
}

###########################################
## command line mode

my $cl_error_cb = sub {
  my ($text) = @_;

  print "\n$text .. cancel operation [Y/n]? ";
  my $x = scalar(<STDIN>);
  $x =~ /^n/i;
};

(-e "$FILE") or die "file '$FILE' not found";
if($POOL) {
  print "will import from playlist $FILE, with user $user\n\n";
} else {
  print "will import $FILE, with user $user\n\n";
}

my $group = '';
my $to_cart = 0;
for my $href (@allowed_dbs) {
  if($href->{'PATH'} eq $DROPBOX) {
    $group = $href->{'GROUP'};
    $to_cart = $href->{'TO_CART'};
    last;
  }
}

if($group eq '') {
  print "Dropbox not found or not allowed\n";
  exit 1
}

my $ret;
my $log;
if($POOL) {
  if(!$KEEPCARTS) {
    rhimport::clear_carts($dbh, $group, $to_cart);
  }

  ($ret, $log) = rhimport::import_playlist($FILE, $DROPBOX, 0, $cl_error_cb);
} else {
  rhimport::clear_carts($dbh, $group, $to_cart);
  ($ret, $log) = rhimport::import_single($FILE, $DROPBOX, 0);
}

$dbh->disconnect();

if(!$ret) {
  exit 1;
}

exit 0;