summaryrefslogtreecommitdiff
path: root/lib/URLQueue/MainController.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/URLQueue/MainController.pm')
-rw-r--r--lib/URLQueue/MainController.pm171
1 files changed, 171 insertions, 0 deletions
diff --git a/lib/URLQueue/MainController.pm b/lib/URLQueue/MainController.pm
new file mode 100644
index 0000000..b9a8dd5
--- /dev/null
+++ b/lib/URLQueue/MainController.pm
@@ -0,0 +1,171 @@
+package URLQueue::MainController;
+use strict;
+use warnings;
+use base 'Gtk2::GladeXML::Simple';
+use Path::Class;
+use URI::Find;
+use URI::Title;
+use Email::Send;
+use POSIX 'strftime';
+
+sub new {
+ my ($class,%params)=@_;
+
+ my $glade_file=file(__FILE__)->parent->file('main.glade');
+ my $self=$class->SUPER::new($glade_file);
+
+ my $target_list=Gtk2::TargetList->new();
+ $target_list->add_uri_targets(1);
+ $target_list->add_text_targets(2);
+ $self->{input}->drag_dest_set('all',
+ [qw(default copy move link private ask)],
+ []);
+ $self->{input}->drag_dest_set_target_list($target_list);
+
+ $self->{glade_dir}=$params{glade_dir};
+
+ return $self;
+}
+
+sub drag_motion {
+ my ($self, $widget, $context, $x, $y, $time) = @_;
+
+ $context->status($context->suggested_action,$time);
+
+ return 1;
+}
+
+sub drag_drop {
+ my ($self, $widget, $context, $x, $y, $time) = @_;
+
+ if (my $atom=$context->targets) {
+ $widget->drag_get_data($context, $atom, $time);
+ return 1;
+ }
+
+ return 0;
+}
+
+sub drag_data_received {
+ my ($self, $widget, $context, $x, $y, $data, $info, $time) = @_;
+
+ if ($info==1) {
+ $self->handle_uris($data->get_uris)
+ }
+ elsif ($info==2) {
+ $self->handle_text($data->get_text);
+ }
+ else {
+ warn "What is $info??";
+ }
+}
+
+sub paste_clipboard {
+ my ($self,$widget)=@_;
+
+ my $clipboard=Gtk2::Clipboard->get();
+ $clipboard->request_text(sub{$self->handle_text($_[1])});
+}
+
+sub button_release {
+ my ($self,$widget,$event)=@_;
+
+ if ($event->button==2) {
+ my $clipboard=Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_PRIMARY);
+ $clipboard->request_text(sub{$self->handle_text($_[1])});
+ }
+}
+
+sub quit {
+ Gtk2->main_quit;
+}
+
+sub handle_text {
+ my ($self,$text)=@_;
+
+ return unless $text;
+
+ # stupid hack to avoid double-drop from Firefox
+ return if ($self->{_last_text} and
+ $self->{_last_text} eq $text);
+ $self->{_last_text}=$text;
+
+ $self->{status}->push(4,'Finding uris');
+
+ {
+ my @uris;
+ my $tmptext=$text;
+ URI::Find->new(sub {
+ push @uris,$_[0]->as_string;
+ return ''
+ })->find(\$tmptext);
+
+ if ($tmptext=~/\A \s* \z/smx) {
+ $self->{status}->pop(4);
+ return $self->handle_uris(@uris);
+ }
+ }
+
+ $self->{status}->pop(4);
+
+ my ($title)=($text =~ m{ \A ^ (.*?) $}smx);
+
+ for ($title,$text) {
+ s{\A \s+}{}smx;
+ s{\s+ \z}{}smx;
+ }
+
+ $self->send_email($title,$text);
+}
+
+sub handle_uris {
+ my ($self,@uris)=@_;
+
+ @uris=grep {$_} @uris;
+
+ return unless @uris;
+
+ $self->{status}->push(5,'Titling '.(scalar @uris).' uris');
+
+ my @titles=map {URI::Title::title($_)} @uris;
+
+ $self->{status}->pop(5);
+
+ my $title=join ' - ',@titles;
+ my $body='';
+ while (@uris) {
+ my $uri=shift @uris;
+ my $name=shift @titles;
+ $body.="$title $uri\n";
+ }
+
+ $self->send_email($title,$body);
+}
+
+sub send_email {
+ my ($self,$title,$body)=@_;
+
+ my $date=strftime('%a, %d %b %Y %H:%M:%S %z',localtime(time));
+
+ my $message=<<"EOM";
+From: urlqueue\@thenautilus.net
+To: dakkar\@thenautilus.net
+Subject: $title
+Date: $date
+
+$body
+EOM
+
+ $self->{status}->push(6,'Sending the message');
+
+ my $sender=Email::Send->new({mailer=>'SMTP'});
+ $sender->mailer_args([Host=>'exelion']);
+ $sender->send($message);
+
+ $self->{status}->pop(6);
+}
+
+1;
+
+__END__
+Date: Sat, 2 Jun 2007 09:48:26 +0200