summaryrefslogtreecommitdiff
path: root/rhimport
blob: ee1f552acfc56eb45e7725dcb4c92acfd34977a5 (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
#!/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/share/perl5/rhimport/';
use rhimport;

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

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

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

options:
     --file          the media file or playlist to import
     --pool          pool mode, import media files referneced by playlist
     --dropbox       the path to the dropbox to use
     --list-allowed  list allowed dropboxes and exit

EOF
  exit;
}

my $user = $ENV{'USER'};
$user or die "Username not found in environment";

my $dbh = DBI->connect( "DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr";
my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.PATH from USER_PERMS, DROPBOXES where USER_PERMS.USER_NAME='$user' and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME;};
my $sth = $dbh->prepare($sql);
$sth->execute();
my @allowed_dbs;
while(my ($group, $path) = $sth->fetchrow_array()) {
  $path =~ s/\/\*$//;
  my $perm = {};
  $perm->{'GROUP'} = $group;
  $perm->{'PATH'} = $path;
  push @allowed_dbs, $perm;
}
$sth->finish();

if($LISTALLOWED) {
  for my $href ( @allowed_dbs ) {
    print "$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 0;
  }
  
  $POOL = 1;
  my $l_mode = $guixml->get_widget('l_mode');
  $l_mode->set_label("<b>Musik Pool</b>");
  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 0;
  }
  
  $POOL = 0;
  my $l_mode = $guixml->get_widget('l_mode');
  $l_mode->set_label("<b>Sendung</b>");
  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("*");
  my $filechooser = $guixml->get_widget('filechooser');
  $filechooser->set_filter($filter);
}

sub toggle_mode_gui()
{
  if($POOL) {
    set_mode_file_gui();
  } else {
    set_mode_playlist_gui();
  }
}

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

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

  my $filechooser = $guixml->get_widget('filechooser');
  my $file = $filechooser->get_filename;
  if(!$file || -d $file) {
    $l_status->set_label("No Playlist selected!");
    return 0;
  }
  $filechooser->unselect_all;

  my $co_dropbox = $guixml->get_widget('co_dropbox');
  my $dropbox = $co_dropbox->get_active_text;

  $l_status->set_label("importing from $file");

  my $ret;
  if($POOL) {
    $ret = rhimport::import_playlist($file, $dropbox);
  } else {
    $ret = rhimport::import_file($file, $dropbox);
  }

  return $ret;
}

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

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

  my $model = Gtk2::ListStore->new('Glib::String');
  for my $href ( @allowed_dbs ) {
    $model->set ($model->append, 0, $href->{'PATH'});
  }
  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 => 0);
  $co_dropbox->set_active(0);

  if($POOL) {
    set_mode_playlist_gui();
  } else {
    set_mode_file_gui();
  }

  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

(-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 $ret;
if($POOL) {
  $ret = rhimport::import_playlist($FILE, $DROPBOX);
} else {
  $ret = rhimport::import_file($FILE, $DROPBOX);
}

$dbh->disconnect();
exit $ret;