-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.pl
executable file
·528 lines (413 loc) · 15 KB
/
config.pl
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/perl -w
use strict;
use warnings;
use Cwd;
use DBI;
use Gtk3 -init;
use constant PERL_ZERO_RECORDS_INSERTED => '0E0';
use Data::Dumper;
my $config_dir;
if ( $ENV{'XDG_CONFIG_HOME'} ) {
$config_dir = $ENV{'XDG_CONFIG_HOME'};
} else {
$config_dir = $ENV{"HOME"} . "/.broadway_session_manager";
}
print "Config dir: [$config_dir]\n";
if ( ! -d $config_dir ) {
mkdir( $config_dir )
|| die( "Failed to create config directory [$config_dir]:\n" . $! );
}
# Turn off output buffering.
# This is so stuff gets dumped to the console / app log immediately instead of somewhat after the event.
select STDERR;
$| = 1;
select STDOUT;
$| = 1;
# Connect to the config database
print "Connecting to config database ...\n";
my $dbh = DBI->connect(
"dbi:SQLite:dbname=" . $config_dir . "/broadway_proxy_config.db"
, undef # username
, undef # password
, {} # options hash
) || die( DBI->errstr );
# Create our 'simple_config' table if it doesn't exist - this is required for our upgrade logic
$dbh->do(
"create table if not exists simple_config (\n"
. " key text primary key\n"
. " , value text\n"
. ")"
) || die( $dbh->errstr );
sub do_upgrades {
# $options will contain:
# {
# current_version => $some_version_integer
# , upgrade_path => $some_path_that_contains_schema_upgrades
# }
#
# This sub performs upgrades to our database schema by parsing filenames in a given directory
# ( filenames must contain a sequence number ). We've been passed our current version ( above ).
# Any files with a sequence higher than our current version number are executed, in order, and we
# then update the current version ( which is stored in simple_config ).
my $options = shift;
{
no warnings 'uninitialized';
print "Checking for upgrades ... current schema version: [" . $options->{current_version} . "]\n";
}
my $upgrade_hash = {};
if ( ! -d $options->{upgrade_path} ) {
return;
}
opendir( DIR, $options->{upgrade_path} ) || warn( $! );
while ( my $file = readdir(DIR) ) {
if ( $file =~ /(\d*)_([\w-]*)\.(dml|ddl)$/i ) {
my ( $sequence, $name, $extension ) = ( $1, $2 );
$upgrade_hash->{$sequence} = $file;
}
}
close DIR;
foreach my $sequence ( sort { $a <=> $b } keys %{$upgrade_hash} ) {
if ( ! defined $options->{current_version} || $sequence > $options->{current_version} ) {
my $this_file = $options->{upgrade_path} . "/" . $upgrade_hash->{$sequence};
local $/;
my $this_fh;
open ( $this_fh, "<$this_file" )
|| die( $! );
my $contents = <$this_fh>;
close $this_fh;
print "Executing schema upgrade: [" . $upgrade_hash->{$sequence} . "]\n";
$dbh->do( $contents )
|| die( "Error upgrading schema:\n" . $dbh->errstr );
# Bump the schema version. Unfortunately we don't have upsert syntax yet ( v3.22.0 vs v3.28.0 )
my $records = $dbh->do( qq{ update simple_config set value = ? where key = 'version' }
, {}
, ( $sequence )
) || die( $dbh->errstr );
if ( $records eq PERL_ZERO_RECORDS_INSERTED ) {
$dbh->do( qq{ insert into simple_config ( key , value ) values ( 'version' , ? ) }
, {}
, ( $sequence )
) || die( $dbh->errstr );
}
}
}
}
# Get our version and upgrade path, and trigger schema upgrades
my $current_dir = cwd();
my $upgrade_path = $current_dir . "/schema_upgrades";
my $sth = $dbh->prepare( "select value from simple_config where key = 'version'" ) || die( $dbh->errstr );
$sth->execute() || die( $sth->errstr );
my $row = $sth->fetchrow_hashref();
my $current_version = $row->{value};
do_upgrades(
{
current_version => $current_version
, upgrade_path => $upgrade_path
}
);
# We're ready to launch the config GUI ...
print "Launching GUI ...\n";
my $window = ConfigWindow->new(
{
dbh => $dbh
, config_dir => $config_dir
, current_dir => $current_dir
}
);
Gtk3->main();
##########################################
package ConfigWindow;
use warnings;
use strict;
use Gtk3::Ex::DBI::Form;
use Gtk3::Ex::DBI::Datasheet;
use Digest::MD5 qw(md5_hex);
use Glib qw/TRUE FALSE/;
sub new {
my ( $class , $options ) = @_;
my $self = {};
$self->{options} = $options;
bless $self, $class;
$self->{builder} = Gtk3::Builder->new;
$self->{builder}->add_objects_from_file(
$self->{options}->{current_dir} . "/config.glade"
, "config"
);
$self->{builder}->get_object( 'config' )->maximize();
$self->{builder}->connect_signals( undef, $self );
$self->{apps} = Gtk3::Ex::DBI::Datasheet->new(
{
dbh => $options->{dbh}
, sql => {
select => "*"
, from => "apps"
}
, auto_incrementing => FALSE
, vbox => $self->{builder}->get_object( 'apps_box' )
, auto_tools_box => TRUE
}
);
$self->{users} = Gtk3::Ex::DBI::Datasheet->new(
{
dbh => $options->{dbh}
, sql => {
select => "*"
, from => "users"
}
, auto_incrementing => FALSE
, vbox => $self->{builder}->get_object( 'users_box' )
, auto_tools_box => TRUE
, before_apply => sub { $self->before_users_apply( @_ ) }
, on_row_select => sub{ $self->on_user_select( @_ ) }
}
);
$self->{user_apps} = Gtk3::Ex::DBI::Datasheet->new(
{
dbh => $options->{dbh}
, sql => {
select => "app_name"
, from => "user_apps"
, where => "username = ?"
, bind_values => [ undef ]
}
, read_only => TRUE
, vbox => $self->{builder}->get_object( 'user_apps_box' )
, auto_tools_box => TRUE
, recordset_tool_items => [ qw ' add_app del_app ' ]
, recordset_extra_tools => {
add_app => {
type => 'button'
, markup => "<span color='darkgreen'>Add</span>"
, icon_name => 'gtk-go-forward'
, coderef => sub { $self->on_add_app_to_user( @_ ) }
}
, del_app => {
type => 'button'
, markup => "<span color='red'>Remove</span>"
, icon_name => 'gtk-go-back'
, coderef => sub { $self->on_del_app_from_user( @_ ) }
}
}
}
);
$self->{simple_config} = Gtk3::Ex::DBI::Datasheet->new(
{
dbh => $options->{dbh}
, sql => {
select => "*"
, from => "simple_config"
}
, auto_incrementing => FALSE
, vbox => $self->{builder}->get_object( 'simple_config_box' )
, auto_tools_box => TRUE
}
);
return $self;
}
sub before_users_apply {
my ( $self , $apply_info ) = @_;
# Here we md5-hash the password ...
$apply_info->{model}->set(
$apply_info->{iter}
, $self->{users}->column_from_column_name( "password" )
, md5_hex(
$apply_info->{model}->get( $apply_info->{iter} , $self->{users}->column_from_column_name( "password" ) )
)
);
return TRUE;
}
sub on_user_select {
my $self = shift;
$self->{user_apps}->query(
{
where => "username = ?"
, bind_values => [ $self->{users}->get_column_value( "username" ) ]
}
);
}
sub on_add_app_to_user {
my $self = shift;
my $app_name = $self->{apps}->get_column_value( "app_name" );
if ( ! $app_name ) {
$self->dialog(
{
title => "No app selected"
, type => "error"
, text => "You need to select an app first ..."
}
);
return TRUE;
}
my $username = $self->{users}->get_column_value( "username" );
if ( ! $username ) {
$self->dialog(
{
title => "No user selected"
, type => "error"
, text => "You need to select a user first ..."
}
);
return TRUE;
}
eval {
$self->{options}->{dbh}->do( "insert into user_apps ( username , app_name ) values ( ? , ? )" , {} , ( $username , $app_name ) )
|| die( $self->{options}->{dbh}->errstr );
};
my $err = $@;
if ( $err ) {
$self->dialog(
{
title => "Couldn't do it"
, type => "error"
, text => $err
}
);
return TRUE;
}
$self->{user_apps}->query();
}
sub on_del_app_from_user {
my $self = shift;
my $app_name = $self->{user_apps}->get_column_value( "app_name" );
if ( ! $app_name ) {
$self->dialog(
{
title => "No app selected"
, type => "error"
, text => "You need to select an app first ..."
}
);
return TRUE;
}
my $username = $self->{users}->get_column_value( "username" );
if ( ! $username ) {
$self->dialog(
{
title => "No user selected"
, type => "error"
, text => "You need to select a user first ..."
}
);
return TRUE;
}
eval {
$self->{options}->{dbh}->do( "delete from user_apps where username = ? and app_name = ?" , {} , ( $username , $app_name ) )
|| die( $self->{options}->{dbh}->errstr );
};
my $err = $@;
if ( $err ) {
$self->dialog(
{
title => "Couldn't do it"
, type => "error"
, text => $err
}
);
return TRUE;
}
$self->{user_apps}->query();
}
sub on_config_destroy {
my $self = shift;
Gtk3::main_quit();
}
sub dialog {
my ( $self, $options ) = @_;
# This is a copy/paste from another project. We don't need most of it, but it also doesn't hurt to have it here.
# Some things have been short-circuited to avoid bringing in other code.
# TODO: port to Gtk3::Dialog as Gtk3 developers are
# getting cranky about the use of images
my $buttons = 'GTK_BUTTONS_OK';
if ( $options->{type} eq 'options' || $options->{type} eq 'input' ) {
$buttons = 'GTK_BUTTONS_OK_CANCEL';
} elsif ( $options->{type} eq 'question' ) {
$buttons = 'GTK_BUTTONS_YES_NO';
}
my $parent_window;
if ( $options->{parent_window} ) {
$parent_window = $options->{parent_window};
} elsif ( $self ) {
# $parent_window = $self->get_window;
$parent_window = $self->{builder}->get_object( 'config' );
}
my $gtk_dialog_type;
if ( $options->{type} eq 'options' || $options->{type} eq 'input' ) {
$gtk_dialog_type = 'question';
} elsif ( $options->{type} eq 'textview' ) {
$gtk_dialog_type = 'other';
} else {
$gtk_dialog_type = $options->{type};
}
my $dialog = Gtk3::MessageDialog->new(
$parent_window
, [ qw/modal destroy-with-parent/ ]
, $gtk_dialog_type
, $buttons
);
if ( $options->{title} ) {
$dialog->set_title( $options->{title} );
}
if ( $options->{type} ne 'textview' && $options->{text} ) {
# $dialog->set_markup( window::escape( undef, $options->{text} ) );
$dialog->set_markup( $options->{text} );
} elsif ( $options->{type} ne 'textview' && $options->{markup} ) {
$dialog->set_markup( $options->{markup} );
}
my ( @radio_buttons, $entry, $sw, $textview );
if ( $options->{type} eq 'options' ) {
my $message_area = $dialog->get_message_area;
my ( $box , $sw );
if ( $options->{orientation} eq 'vertical' ) {
$box = Gtk3::Box->new( 'GTK_ORIENTATION_VERTICAL', 0 );
$sw = Gtk3::ScrolledWindow->new();
$sw->set_size_request( 600, 300 ); # can't see anything unless we do this
} else {
$box = Gtk3::Box->new( 'GTK_ORIENTATION_HORIZONTAL', 0 );
}
foreach my $option ( @{$options->{options}} ) {
my $radio_button = Gtk3::RadioButton->new_with_label_from_widget( $radio_buttons[0], $option );
push @radio_buttons, $radio_button;
$box->pack_end( $radio_button, TRUE, TRUE, 0);
}
if ( $sw ) {
$sw->add( $box );
$message_area->pack_end( $sw, TRUE, TRUE, 0 );
} else {
$message_area->pack_end( $box, TRUE, TRUE, 0 );
}
} elsif ( $options->{type} eq 'input' ) {
my $message_area = $dialog->get_message_area;
$entry = Gtk3::Entry->new;
if ( exists $options->{default} ) {
$entry->set_text( $options->{default} );
}
$message_area->pack_end( $entry, TRUE, TRUE, 0 );
} elsif ( $options->{type} eq 'textview' ) {
my $message_area = $dialog->get_message_area;
$sw = Gtk3::ScrolledWindow->new();
$textview = Gtk3::TextView->new();
$sw->set_size_request( 1024, 768 ); # can't see anything unless we do this
$sw->add( $textview );
$message_area->pack_end( $sw, TRUE, TRUE, 0 );
$textview->get_buffer->set_text( $options->{text} );
}
$dialog->show_all;
my $response = $dialog->run;
if ( $response eq 'cancel' ) {
$dialog->destroy;
return undef;
}
if ( $options->{type} eq 'options' ) {
foreach my $radio_button ( @radio_buttons ) {
if ( $radio_button->get_active ) {
$response = $radio_button->get_label;
last;
}
}
} elsif ( $options->{type} eq 'input' ) {
$response = $entry->get_text;
}
$dialog->destroy;
return $response;
}
1;