This repository was archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalluvion-gtk.pl
More file actions
executable file
·1323 lines (1065 loc) · 39.4 KB
/
Copy pathalluvion-gtk.pl
File metadata and controls
executable file
·1323 lines (1065 loc) · 39.4 KB
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
# --------------------------------------------------------------------
# Alluvion 0.3
# Perl/Gtk2 torrent search utility (strike API)
# --------------------------------------------------------------------
# Usage:
# ./$0 <option>
# --------------------------------------------------------------------
# Options:
# -d | --debug enable debug output
# -v | --version print version
# -h | --help show help
# -r | --reset rewrite user config with default settings
#
# --------------------------------------------------------------------
# Strike API information : https://getstrike.net/api/
# Alluvion @ GitHub : https://github.com/Jigoku/alluvion
# --------------------------------------------------------------------
# Copyright (C) 2015 Ricky K. Thomson
#
# This program 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
# (at your option) any later version.
# This program 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.
# u should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
my $VERSION = 0.3;
use feature ":5.10";
use strict;
use warnings;
use FindBin qw($Bin);
use File::Basename;
use JSON;
use threads;
use LWP::UserAgent;
use LWP::Protocol::socks;
use URI::Escape;
use Gtk2 qw(-threads-init -init);
use Glib qw(TRUE FALSE);
$|++;
die "[ -] Glib::Object thread safety failed"
unless Glib::Object->set_threadsafe (TRUE);
# default paths
my $data = $Bin . "/data/";
my $xml = $data . "alluvion.glade";
my $confdir = $ENV{ HOME } . "/.alluvion/";
# config files
my $conf = $confdir . "config";
my $bookmarks = $confdir . "bookmarks";
# default user settings
my %settings = (
"timeout" => "10",
"filesize_type" => "mb",
"magnet_exec" => "/usr/bin/xdg-open",
"proxy_enabled" => 0,
"proxy_type" => "none",
"http_proxy_addr" => "0.0.0.0",
"http_proxy_port" => 8080,
"socks4_proxy_addr" => "0.0.0.0",
"socks4_proxy_port" => 1080,
"socks5_proxy_addr" => "0.0.0.0",
"socks5_proxy_port" => 1080,
"statusbar" => 1,
"category_filter" => 1,
"api_index" => "https://getstrike.net/api/v2/torrents/count/",
"api_hash" => "https://getstrike.net/api/v2/torrents/info/?hashes=",
"api_query" => "https://getstrike.net/api/v2/torrents/search/?phrase=",
"api_file" => "https://getstrike.net/torrents/api/download/",
);
my ($category_filter, $subcategory_filter) = ("","");
my $debug = 0;
# global objects
my (
$builder,
$window,
$preferences,
$ua,
$filechooser,
$filechooser_get,
@threads,
@bookmark,
);
# command line arguments
my $helpmsg = (
"Alluvion ".$VERSION." https://jigoku.github.io/alluvion/\n".
"Copyright (C) 2015, Ricky K. Thomson\n\n".
"Usage: ".$0." [-d|-h|-v]\n\n".
"Options:\t-d | --debug\t\t print verbose information\n".
"\t\t-h | --help\t\t show help\n".
"\t\t-v | --version\t\t show version\n"
);
foreach my $arg (@ARGV) {
if ($arg =~ m/^(--version|-v)$/) { print $VERSION."\n"; exit(0); }
if ($arg =~ m/^(--help|-h)$/) { print $helpmsg; exit(0); }
if ($arg =~ m/^(--reset|-r)$/) { no warnings; write_config($conf); print "Wrote default settings to $conf\n"; exit(0); }
if ($arg =~ m/^(--debug|-d)$/) { print "Alluvion ". $VERSION ."\n" . ("-"x50) ."\n"; $debug = 1; }
}
# sleeping thread, for some reason this stops segfaults at exit
# and several random GLib-GObject-CRITICAL errors, as long as a thread
# is started befor eth interface is visible, this seems to work.
# -- useful for showing momentary debug output too.
my $sleeper = threads->create({'void' => 1},
sub {
while (1) {
if ($debug == 1) {
# print mem usage
chomp( my $size = `grep VmSize /proc/$$/status`);
chomp( my $peak = `grep VmPeak /proc/$$/status`);
chomp( my $threads = `grep Threads /proc/$$/status`);
print "[ &] PID:\t".$$." | ".$size." | ".$peak." | ".$threads ."\n";
}
sleep 1;
}
}
)->detach;
main();
sub main {
# create local config directory
if ( ! -e $confdir ) { mkdir $confdir or die $!; };
# read ~/.alluvion config
if (-e $conf) {
open FILE, "<$conf" or die "[ -] $conf: $!\n";
for (<FILE>) {
no warnings;
# perl 5.10 experimental functions
given($_) {
when (m/^timeout=\"(\d+)\"/) { $settings{"timeout"} = $1; }
when (m/^magnet_exec=\"(.+)\"/) { $settings{"magnet_exec"} = $1; }
when (m/^filesize_type=\"(.+)\"/) { $settings{"filesize_type"} = $1; }
when (m/^proxy_enabled=\"(\d+)\"/) { $settings{"proxy_enabled"} = $1; }
when (m/^proxy_type=\"(.+)\"/) { $settings{"proxy_type"} = $1; }
when (m/^http_proxy_addr=\"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\"/) { $settings{"http_proxy_addr"} = $1; }
when (m/^http_proxy_port=\"(\d+)\"/) { $settings{"http_proxy_port"} = $1; }
when (m/^socks4_proxy_addr=\"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\"/) { $settings{"socks4_proxy_addr"} = $1; }
when (m/^socks4_proxy_port=\"(\d+)\"/) { $settings{"socks4_proxy_port"} = $1; }
when (m/^socks5_proxy_addr=\"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\"/) { $settings{"socks5_proxy_addr"} = $1; }
when (m/^socks5_proxy_port=\"(\d+)\"/) { $settings{"socks5_proxy_port"} = $1; }
when (m/^statusbar=\"(.+)\"/) { $settings{"statusbar"} = $1; }
when (m/^category_filter=\"(.+)\"/) { $settings{"category_filter"} = $1; }
when (m/^api_index=\"(.+)\"/) { $settings{"api_index"} = $1; }
when (m/^api_hash=\"(.+)\"/) { $settings{"api_hash"} = $1; }
when (m/^api_query=\"(.+)\"/) { $settings{"api_query"} = $1; }
when (m/^api_file=\"(.+)\"/) { $settings{"api_file"} = $1; }
}
}
}
# check gtkbuilder interface exists
if ( ! -e $xml ) { die "Interface: '$xml' $!"; }
$builder = Gtk2::Builder->new();
# load gtkbuilder interface
$builder->add_from_file( $xml );
# get top level objects
$window = $builder->get_object( 'window' );
$filechooser = $builder->get_object( 'filechooserdialog' );
$builder->connect_signals( undef );
# adjust user settings for interface
$builder->get_object( 'menu_view_statusbar' )->set_active($settings{"statusbar"});
$builder->get_object( 'menu_view_category' )->set_active($settings{"category_filter"});
# restore saved bookmarks
if (-e $bookmarks) {
open FILE, "<$bookmarks" or die "[ -] $bookmarks: $!\n";
for (<FILE>) {
push @bookmark, $_;
}
}
# add them to the interface
populate_bookmarks();
# draw the window
$window->show();
# set previous proxy settings
assign_proxy();
# start thread for statusbar display
set_index_total();
# main loop
Gtk2->main(); gtk_main_quit();
}
sub debug_proxy_address {
if ($debug eq 1 && $settings{'proxy_enabled'} eq 1) {
print "[ ~] checking end address...\n";
my $response = $ua->get("http://checkip.dyndns.org/");
if ($response->is_success) {
if ($response->decoded_content =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
print "[ !] $1\n";
}
} else {
print "[ -] $!\n";
}
}
}
sub on_menu_help_check_release_activate {
# check if there is a newer release
my $tag = json_request("https://api.github.com/repos/jigoku/alluvion/releases/latest");
if ($tag eq "error" || $tag eq "connection") {
spawn_dialog("error", "close", "Error", "Could not determine latest version\n");
return;
}
# prompt there is a newer release
if ($tag->{tag_name} > $VERSION) {
spawn_dialog("info", "ok", "Information", "A new release is available\nVersion: ".$tag->{tag_name});
} else {
spawn_dialog("info", "ok", "Information", "You are running the latest version\n");
}
}
sub file_request($) {
# file to fetch
my $file_uri = shift;
# start a new thread
my $thread = threads->create(
sub {
debug("[ !] thread #". threads->self->tid ." started\n");
# check if we have a connection to network
if (!($ua->is_online)) { return 3; }
# get the file
my $response = $ua->get($file_uri);
if ($response->is_success) {
return $response->content;
} else {
return 2; # error code
}
}
);
# track the thread
push (@threads, $thread);
my $tid = $thread->tid;
# continue to update the interface while thread is alive
while ($thread->is_running) {
Gtk2->main_iteration while (Gtk2->events_pending);
}
# thread has finished
debug( "[ !] thread #" .$tid ." finished\n");
# stop tracking the thread
splice_thread($thread);
return $thread->join;
}
sub json_request($) {
# api uri twhich should contain json output
my $api_uri = shift;
# start a new thread
my $thread = threads->create(
sub {
debug("[ !] thread #". threads->self->tid ." started\n");
# check if we have a connection to network
if (!($ua->is_online)) { return 3; }
my $response = $ua->get($api_uri);
if ($response->is_success) {
# the json text as a string
debug($response->decoded_content);
return $response->decoded_content;
} else {
return 2; # error code
}
}
);
# track the thread
push (@threads, $thread);
my $tid = $thread->tid;
# continue to update the interface while thread is alive
while ($thread->is_running) {
Gtk2->main_iteration while (Gtk2->events_pending);
}
# thread has finished
debug( "[ !] thread #" .$tid ." finished\n");
# stop tracking the thread
splice_thread($thread);
my $data = $thread->join;
# check for error from thread
if ($data eq 2) { return "error"; }
if ($data eq 3) { return "connection"; }
# otherwise return api result as JSON object
my $json = JSON->new;
return $json->decode($data);
}
sub populate_bookmarks {
my $vbox = $builder->get_object( 'vbox_bookmarks' );
destroy_children($vbox);
chomp(@bookmark);
for my $item (@bookmark) {
my $hbox = Gtk2::HBox->new;
my $label = Gtk2::Label->new;
$label->set_markup("<span size='large'><b>".$item."</b></span>");
my $button_search = Gtk2::Button->new;
#$button_search->set_label("search");
$button_search->set_image(Gtk2::Image->new_from_stock("gtk-apply", 'button'));
$button_search->signal_connect('clicked',
sub {
$builder->get_object( 'notebook' )->set_current_page(0);
$builder->get_object( 'entry_query' )->set_text($item);
on_button_query_clicked();
}
);
my $button_remove = Gtk2::Button->new;
#$button_remove->set_label("remove");
$button_remove->set_image(Gtk2::Image->new_from_stock("gtk-clear", 'button'));
$button_remove->signal_connect('clicked',
sub {
remove_bookmark($item);
populate_bookmarks();
}
);
my $hseparator = new Gtk2::HSeparator();
$hbox->pack_start($label, FALSE,FALSE,0);
$hbox->pack_end($button_remove, FALSE,FALSE,0);
$hbox->pack_end($button_search, FALSE,FALSE,0);
$vbox->pack_start ($hbox, FALSE, FALSE, 0);
$vbox->pack_start($hseparator, FALSE,FALSE,5);
}
$vbox->show_all;
}
sub ua_init {
# reinitialize when called
$ua = LWP::UserAgent->new(
keep_alive => 1,
);
# request timeout
$ua->timeout($settings{"timeout"});
# provide user agent
# (cloudflare blocks libwww-perl/*.*)
$ua->agent("Alluvion/".$VERSION." https://jigoku.github.io/alluvion/");
$ua->protocols_allowed( [ 'https', 'http' ] );
}
sub ua_init_env {
ua_init();
$ua->proxy( ['http, https'], $ENV{HTTP_PROXY} ) if exists $ENV{HTTP_PROXY};
$settings{"proxy_type"} = "env";
}
sub ua_init_http {
ua_init();
$ua->proxy([ 'http', 'https' ], "http://".$settings{"http_proxy_addr"}.":".$settings{"http_proxy_port"});
$settings{"proxy_type"} = "http";
}
sub ua_init_socks4 {
ua_init();
$ua->proxy([ 'http', 'https' ], "socks4://".$settings{"socks4_proxy_addr"}.":".$settings{"socks4_proxy_port"});
$settings{"proxy_type"} = "socks4";
}
sub ua_init_socks5 {
ua_init();
$ua->proxy([ 'http', 'https' ], "socks://".$settings{"socks5_proxy_addr"}.":".$settings{"socks5_proxy_port"});
$settings{"proxy_type"} = "socks5";
}
sub assign_proxy {
debug("[ !] proxy mode: ".$settings{"proxy_type"} . "\n");
no warnings;
given($settings{"proxy_type"}) {
# set http proxy
when (m/^http$/) {
$builder->get_object( 'radio_http' )->set_active(1);
debug("[ !] setting ua_init_http()\n");
ua_init_http();
}
# set socks4
when (m/^socks4$/) {
$builder->get_object( 'radio_socks4' )->set_active(1);
debug("[ !] setting ua_init_socks4()\n");
ua_init_socks4();
}
# set socks 5
when (m/^socks5$/) {
$builder->get_object( 'radio_socks5' )->set_active(1);
debug("[ !] setting ua_init_socks5()\n");
ua_init_socks5();
}
# set env HTTP_PROXY
when (m/^env$/) {
$builder->get_object( 'radio_env' )->set_active(1);
debug("[ !] setting ua_init_env()\n");
ua_init_env();
}
when (m/^none$/) {
$builder->get_object( 'vbox_http_proxy' )->set_sensitive(0);
$builder->get_object( 'vbox_socks4_proxy' )->set_sensitive(0);
$builder->get_object( 'vbox_socks5_proxy' )->set_sensitive(0);
$builder->get_object( 'vbox_env_proxy' )->set_sensitive(0);
# initialize LWP::UserAgent with no proxy
ua_init();
}
}
}
sub set_index_total {
debug_proxy_address();
my $index_total = $builder->get_object( 'label_indexed_total' );
my $spinner = $builder->get_object( 'spinner' );
$spinner->set_visible(1);
$spinner->start;
$index_total->set_text("Updating index total...");
# threaded request
my $data = json_request($settings{"api_index"});
if ($data eq "error") {
$index_total->set_text("Could not set index total!");
$spinner->set_visible(0);
$spinner->stop;
debug("[ -] Failed to parse json data\n");
return;
}
if ($data eq "connection") {
$index_total->set_text("Connection failed");
$spinner->set_visible(0);
$spinner->stop;
debug("[ -] Failed to get a connection\n");
return;
}
# display the indexed total in statusbar
for ($data) {
$index_total->set_markup("".commify($_->{message}) . " indexed torrents");
}
$spinner->set_visible(0);
$spinner->stop;
}
sub on_button_hash_clicked {
my $vbox = $builder->get_object( 'vbox_hashinfo' );
destroy_children($vbox);
my $vbox_spinner = Gtk2::Spinner->new;
my $hash = $builder->get_object( 'entry_hash' )->get_text;
my $pending = $builder->get_object( 'label_pending' );
my $spinner = $builder->get_object( 'spinner' );
my $button = $builder->get_object( 'button_hash' );
# check for valid info hash before doing anything
if ((length($hash) != 40) || ($hash =~ m/[^a-zA-Z0-9]/)) {
spawn_dialog("error", "close", "Error", "Invalid info hash");
return;
}
$button->set_sensitive(FALSE);
$pending->set_text("Loading");
$vbox_spinner->set_visible(TRUE);
$vbox_spinner->start;
$vbox->pack_start($vbox_spinner, TRUE, TRUE, 175);
$spinner->set_visible(TRUE);
$spinner->start;
$vbox->show_all;
# returns data from threaded request
my $data = json_request($settings{"api_hash"}.$hash);
$vbox_spinner->destroy;
$button->set_sensitive(TRUE);
$pending->set_text("");
$spinner->set_visible(FALSE);
$spinner->stop;
if ($data eq "error") { spawn_dialog("error", "close", "Error", "Failed to find info hash\n"); return; }
if ($data eq "connection") {
spawn_dialog("error", "close", "Error", "Could not establish a connection\n");
return;
}
# apply data to labels
for (@{$data->{torrents}}) {
my $label_title = Gtk2::Label->new;
$label_title->set_markup("<b>Torrent title</b>\n$_->{torrent_title}");
$label_title->set_alignment(0, 0.5);
$vbox->pack_start($label_title, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_cat= Gtk2::Label->new;
$label_cat->set_markup("<b>Category</b>\n$_->{category}");
$label_cat->set_alignment(0, 0.5);
$vbox->pack_start($label_cat, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_subcat= Gtk2::Label->new;
$label_subcat->set_markup("<b>Subcategory</b>\n$_->{sub_category}");
$label_subcat->set_alignment(0, 0.5);
$vbox->pack_start($label_subcat, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_size= Gtk2::Label->new;
$label_size->set_markup("<b>Size</b>\n".commify(convert_bytes($_->{size})));
$label_size->set_alignment(0, 0.5);
$vbox->pack_start($label_size, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_seeds= Gtk2::Label->new;
$label_seeds->set_markup("<b>Seeders</b>\n<span color='green'>".$_->{seeds}."</span>");
$label_seeds->set_alignment(0, 0.5);
$vbox->pack_start($label_seeds, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_leechers= Gtk2::Label->new;
$label_leechers->set_markup("<b>Leechers</b>\n<span color='red'>".$_->{leeches}."</span>");
$label_leechers->set_alignment(0, 0.5);
$vbox->pack_start($label_leechers, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_filecount= Gtk2::Label->new;
$label_filecount->set_markup("<b>File Count</b>\n$_->{file_count}");
$label_filecount->set_alignment(0, 0.5);
$vbox->pack_start($label_filecount, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
for ($_->{file_info}) {
my (@file_names, @file_lengths) = ();
# remove whitespace from start of string aswell...
for (@{$_->{file_names}}) { $_ =~ s/^\s+|\s+$//g; push @file_names, $_; }
for (@{$_->{file_lengths}}) { push @file_lengths, $_; }
my $file_list;
for (my $i=0; $i<@file_names; $i++) {
$file_list .= "<i>".$file_names[$i]."</i>" . "\t(".commify(convert_bytes($file_lengths[$i])).")\n";
}
my $label_fileinfo = Gtk2::Label->new;
$label_fileinfo->set_markup("<b>File Info</b>\n".$file_list);
$label_fileinfo->set_alignment(0, 0.5);
$vbox->pack_start($label_fileinfo, FALSE,FALSE,5);
}
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_uploader= Gtk2::Label->new;
$label_uploader->set_markup("<b>Uploaded by</b>\n$_->{uploader_username}");
$label_uploader->set_alignment(0, 0.5);
$vbox->pack_start($label_uploader, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_uploaddate= Gtk2::Label->new;
$label_uploaddate->set_markup("<b>Upload Date</b>\n$_->{upload_date}");
$label_uploaddate->set_alignment(0, 0.5);
$vbox->pack_start($label_uploaddate, FALSE,FALSE,5);
$vbox->pack_start(Gtk2::HSeparator->new, FALSE,FALSE,5);
my $label_magnet= Gtk2::Label->new;
my $textview = Gtk2::TextView->new;
$label_magnet->set_markup("<b>Magnet URI</b>");
$label_magnet->set_alignment(0, 0.5);
$vbox->pack_start($label_magnet, FALSE,FALSE,5);
$textview->get_buffer->set_text($_->{magnet_uri});
$textview->set_wrap_mode("GTK_WRAP_CHAR");
$textview->set_border_width(10);
$textview->set_editable(0);
$vbox->pack_start($textview, FALSE,FALSE,5);
my $button_clipboard = Gtk2::Button->new;
$button_clipboard->set_image(Gtk2::Image->new_from_stock("gtk-copy", 'button'));
$button_clipboard->set_label("Copy to clipboard");
$button_clipboard->signal_connect('clicked',
sub {
my $clipboard = Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_CLIPBOARD);
$clipboard->set_text($textview->get_buffer->get_text(
$textview->get_buffer->get_start_iter,
$textview->get_buffer->get_end_iter,
undef
));
}
);
$vbox->pack_start($button_clipboard, FALSE,FALSE,5);
}
$vbox->show_all;
}
sub destroy_children($) {
# remove children from object
my @children = shift->get_children();
foreach my $child (@children) {
$child->destroy;
}
}
sub on_button_query_clicked {
my $query = $builder->get_object( 'entry_query' )->get_text;
my $button = $builder->get_object( 'button_query' );
my $button_clear = $builder->get_object( 'button_query_clear' );
my $spinner = $builder->get_object( 'spinner' );
my $vbox_spinner = Gtk2::Spinner->new;
my $pending = $builder->get_object( 'label_pending' );
# get top level container ready for packing results
my $vbox = $builder->get_object('vbox_query_results');
# remove previous results
destroy_children($vbox);
# must be at least 4 characters for API
if (length($query) < 4) {
my $label = Gtk2::Label->new;
$label->set_markup("<span size='large'><b>Query must be at least 4 characters.</b></span>");
$vbox->pack_start($label, FALSE, FALSE, 5);
$vbox->show_all;
return;
}
# setup progress spinner for vbox
$vbox->pack_start($vbox_spinner, TRUE, TRUE, 175);
$vbox_spinner->start;
$vbox_spinner->set_visible(TRUE);
$vbox->show_all;
$pending->set_text("Loading");
$button->set_sensitive(FALSE);
$button_clear->set_sensitive(FALSE);
$spinner->start;
$spinner->set_visible(TRUE);
# retrusn data from threaded request
my $data = json_request($settings{"api_query"}.uri_escape($query)."&category=".$category_filter."&subcategory=".$subcategory_filter);
$pending->set_text("");
$button->set_sensitive(TRUE);
$button_clear->set_sensitive(TRUE);
$spinner->set_visible(FALSE);
$spinner->stop;
$vbox_spinner->destroy;
if ($data eq "error") {
$vbox_spinner->destroy;
my $label = Gtk2::Label->new;
$label->set_markup("<span size='large'><b>0 torrents found</b></span>");
$vbox->pack_start($label, FALSE, FALSE, 5);
$vbox->show_all;
return;
}
if ($data eq "connection") {
$vbox_spinner->destroy;
my $label = Gtk2::Label->new;
$label->set_markup("<span size='large'><b>Could not establish a connection</b></span>");
$vbox->pack_start($label, FALSE, FALSE, 5);
$vbox->show_all;
return;
}
# results header "X torrent(s) found"
for ($data) {
my $label = Gtk2::Label->new;
my $results = $_->{results};
if (not defined $results) { $results = 0; }
$label->set_markup("<span size='large'><b>".($results == 1 ? "1 torrent" : $results . " torrents")." found</b></span>");
$vbox->pack_start($label, FALSE, FALSE, 5);
}
# bookmark button
my $button_bookmark = Gtk2::Button->new;
$button_bookmark->set_label("Bookmark this search");
$button_bookmark->set_image(Gtk2::Image->new_from_stock("gtk-add", 'button'));
$button_bookmark->signal_connect('clicked',
sub {
my $query = $builder->get_object( 'entry_query' )->get_text;
push @bookmark, $query;
populate_bookmarks();
}
);
$vbox->pack_start($button_bookmark, FALSE, FALSE, 5);
# the results
my $n = 0;
for (@{$data->{torrents}}) {
$n++;
add_separated_item(
$vbox, # container to append result
$n, # number of item result
$_->{torrent_title},
"Seeders: <span color='green'><b>". commify($_->{seeds}) ."</b></span> | Leechers: <span color='red'><b>". commify($_->{leeches}) ."</b></span> | Size: <b>" . commify(convert_bytes($_->{size})) ."</b> | Uploaded: " . $_->{upload_date},
$_->{magnet_uri},
uc $_->{torrent_hash}
);
}
}
sub on_button_query_clear_clicked {
# clear the search results/entry
my $query = $builder->get_object( 'entry_query' );
$query->set_text("");
my $vbox = $builder->get_object('vbox_query_results');
destroy_children($vbox);
my $label = Gtk2::Label->new;
$label->set_markup("<span size='large'><b>Enter a search query</b></span>");
$vbox->pack_start ($label, FALSE, FALSE, 5);
$vbox->show_all;
}
sub splice_thread($) {
my $thread = shift;
my $i = 0;
$i++ until $threads[$i] eq $thread or $i > $#threads;
splice @threads, $i, 1;
}
sub remove_bookmark($) {
my $item = shift;
my $i = 0;
$i++ until $bookmark[$i] eq $item or $i > $#bookmark;
splice @bookmark, $i, 1;
}
sub add_separated_item($$$$$$) {
# adds a label with markup and separator to a vbox
# (For list of search results)
my ($vbox, $n, $torrent_title, $torrent_info, $magnet_uri, $hash) = @_;
my $eventbox = Gtk2::EventBox->new;
my $tooltip_title = Gtk2::Tooltips->new;
$tooltip_title->set_tip( $eventbox, $torrent_title );
my $hseparator = new Gtk2::HSeparator();
my $hbox = Gtk2::HBox->new;
$hbox->set_homogeneous(FALSE);
$hbox->set_homogeneous(0);
# item number of result
my $number = Gtk2::Label->new;
$number->set_markup("<span size='large'>".$n.".</span>");
$number->set_alignment(0,.5);
$number->set_width_chars(4);
my $vboxinfo = Gtk2::VBox->new;
# create new label for truncated title with tooltip
my $label_title = Gtk2::Label->new;
$label_title->set_markup("<span size='large'><b>".convert_special_char($torrent_title) ."</b></span>");
my ($width, undef) = $window->get_size;
my $width_chars = int ($width * .09);
$label_title->set_width_chars($width_chars); # label character limit before truncated
$label_title->set_ellipsize("PANGO_ELLIPSIZE_END");
$label_title->set_alignment(0,.5);
#$label_title->set_line_wrap(1);
#$label_title->set_line_wrap_mode("word");
# create new label for aditional info
my $label = Gtk2::Label->new;
$label->set_markup($torrent_info);
$label->set_alignment(0,.5);
# magnet uri
my $button_magnet = Gtk2::Button->new;
$button_magnet->set_image(Gtk2::Image->new_from_stock("gtk-execute", 'button'));
#$button_magnet->set_label("Launch");
$button_magnet->signal_connect('clicked', sub { launch_magnet($magnet_uri); });
my $tooltip_magnet = Gtk2::Tooltips->new;
$tooltip_magnet->set_tip( $button_magnet, "Open magnet URI" );
# *.torrent
my $button_torrent = Gtk2::Button->new;
$button_torrent->set_image(Gtk2::Image->new_from_stock("gtk-save", 'button'));
#$button_torrent->set_label("Download");
$button_torrent->signal_connect('clicked',
sub {
apply_filefilter("*.torrent", "torrent files", $filechooser);
apply_filefilter("*", "all files", $filechooser);
$filechooser->set_current_name($torrent_title.".torrent");
$filechooser_get = $settings{"api_file"}.$hash .".torrent";
$filechooser->run;
$filechooser->hide;
}
);
my $tooltip_torrent = Gtk2::Tooltips->new;
$tooltip_torrent->set_tip( $button_torrent, "Save *.torrent file" );
# info hash
my $button_hash = Gtk2::Button->new;
$button_hash->set_image(Gtk2::Image->new_from_stock("gtk-info", 'button'));
#$button_hash->set_label("Info");
$button_hash->signal_connect('clicked',
sub {
#my $clipboard = Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_CLIPBOARD);
#$clipboard->set_text($hash);
$builder->get_object( 'entry_hash' )->set_text($hash);
$builder->get_object( 'notebook' )->set_current_page(1);
on_button_hash_clicked();
}
);
my $tooltip_hash = Gtk2::Tooltips->new;
$tooltip_hash->set_tip( $button_hash, "View torrent information" );
# container for buttons
my $buttonbox = Gtk2::HBox->new;
$buttonbox->set_homogeneous(FALSE);
# pack everything
$vbox->pack_start($hseparator, FALSE, FALSE, 0);
$hbox->pack_start($number, FALSE, FALSE, 5);
$vboxinfo->pack_start($label_title, FALSE, FALSE, 0);
$vboxinfo->pack_start ($label, FALSE, FALSE, 0);
$buttonbox->pack_end ($button_magnet, FALSE, FALSE, 0);
$buttonbox->pack_end ($button_torrent, FALSE, FALSE, 0);
$buttonbox->pack_end ($button_hash, FALSE, FALSE, 0);
$hbox->pack_start ($vboxinfo, FALSE, FALSE, 0);
$hbox->pack_end ($buttonbox, FALSE, FALSE, 0);
$eventbox->add ($hbox);
$vbox->pack_start ($eventbox, FALSE, FALSE, 0);
$vbox->set_homogeneous(FALSE);
$vbox->show_all;
}
sub on_menu_edit_preferences_activate {
# initialize the preferences dialog with currently stored settings
my $preferences = $builder->get_object( 'preferences' );
$builder->get_object( 'entry_timeout' )->set_value($settings{"timeout"});
$builder->get_object( 'entry_client' )->set_text($settings{"magnet_exec"});
if ($settings{"proxy_enabled"} eq 1) {
$builder->get_object( 'checkbutton_proxy' )->set_active(1);
} else {
$builder->get_object( 'checkbutton_proxy' )->set_active(0);
}
if ($settings{"filesize_type"} eq "bytes") {
$builder->get_object( 'radio_bytes' )->set_active(TRUE);
} elsif ($settings{"filesize_type"} eq "kb") {
$builder->get_object( 'radio_kb' )->set_active(TRUE);
} elsif ($settings{"filesize_type"} eq "mb") {
$builder->get_object( 'radio_mb' )->set_active(TRUE);
} elsif ($settings{"filesize_type"} eq "gb") {
$builder->get_object( 'radio_gb' )->set_active(TRUE);
}
$builder->get_object( 'entry_http_proxy_addr' )->set_text($settings{"http_proxy_addr"});
$builder->get_object( 'entry_http_proxy_port' )->set_value($settings{"http_proxy_port"});
$builder->get_object( 'entry_socks4_proxy_addr' )->set_text($settings{"socks4_proxy_addr"});
$builder->get_object( 'entry_socks4_proxy_port' )->set_value($settings{"socks4_proxy_port"});
$builder->get_object( 'entry_socks5_proxy_addr' )->set_text($settings{"socks5_proxy_addr"});
$builder->get_object( 'entry_socks5_proxy_port' )->set_value($settings{"socks5_proxy_port"});
$builder->get_object( 'entry_apiindex' )->set_text($settings{"api_index"});
$builder->get_object( 'entry_apihash' )->set_text($settings{"api_hash"});
$builder->get_object( 'entry_apiquery' )->set_text($settings{"api_query"});
$builder->get_object( 'entry_apifile' )->set_text($settings{"api_file"});
$preferences->run; # loops here
$preferences->hide;
}
sub on_button_pref_ok_clicked {
# update the changed settings within preferences
$settings{"timeout"} = $builder->get_object( 'entry_timeout' )->get_value();
$settings{"magnet_exec"} = $builder->get_object( 'entry_client' )->get_text();
$settings{"http_proxy_addr"} = $builder->get_object( 'entry_http_proxy_addr' )->get_text();
$settings{"http_proxy_port"} = $builder->get_object( 'entry_http_proxy_port' )->get_value();
$settings{"socks4_proxy_addr"} = $builder->get_object( 'entry_socks4_proxy_addr' )->get_text();
$settings{"socks4_proxy_port"} = $builder->get_object( 'entry_socks4_proxy_port' )->get_value();
$settings{"socks5_proxy_addr"} = $builder->get_object( 'entry_socks5_proxy_addr' )->get_text();
$settings{"socks5_proxy_port"} = $builder->get_object( 'entry_socks5_proxy_port' )->get_value();
$settings{"api_index"} = $builder->get_object( 'entry_apiindex' )->get_text();
$settings{"api_hash"} = $builder->get_object( 'entry_apihash' )->get_text();
$settings{"api_query"} = $builder->get_object( 'entry_apiquery' )->get_text();
$settings{"api_file"} = $builder->get_object( 'entry_apifile' )->get_text();
if ($builder->get_object( 'radio_bytes' )->get_active() == TRUE ) { $settings{"filesize_type"} = "bytes"; }
if ($builder->get_object( 'radio_kb' )->get_active() == TRUE ) { $settings{"filesize_type"} = "kb"; }
if ($builder->get_object( 'radio_mb' )->get_active() == TRUE ) { $settings{"filesize_type"} = "mb"; }
if ($builder->get_object( 'radio_gb' )->get_active() == TRUE ) { $settings{"filesize_type"} = "gb"; }
# check if we enabled/disabled the HTTP/HTTPS proxy option
if ($builder->get_object( 'checkbutton_proxy' )->get_active() == TRUE) {
$settings{"proxy_enabled"} = 1;
} else {
$settings{"proxy_enabled"} = 0;
# reinitialize without proxy
ua_init();
}
# update the config on disk now, if application crashes
# before shutdown, updated settings won't be lost.
write_config($conf);
}
sub on_checkbutton_proxy_toggled {
# filter input when proxy toggle state changed
if ($builder->get_object( 'checkbutton_proxy' )->get_active() == TRUE) {
$builder->get_object( 'vbox_http_proxy' )->set_sensitive(1);
$builder->get_object( 'vbox_socks4_proxy' )->set_sensitive(1);
$builder->get_object( 'vbox_socks5_proxy' )->set_sensitive(1);
$builder->get_object( 'vbox_env_proxy' )->set_sensitive(1);