From ab22d844899e7f287b927d64a04cbfd8baf62aa6 Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 4 Jun 2007 20:33:23 +0000 Subject: import del programma git-svn-id: svn://luxion/repos/URLQueue/trunk@244 fcb26f47-9200-0410-b104-b98ab5b095f3 --- lib/URLQueue/MainController.pm | 171 +++++++++++++++++++++++++++++++++++++++++ lib/URLQueue/main.glade | 85 ++++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 lib/URLQueue/MainController.pm create mode 100644 lib/URLQueue/main.glade (limited to 'lib') 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 diff --git a/lib/URLQueue/main.glade b/lib/URLQueue/main.glade new file mode 100644 index 0000000..616d274 --- /dev/null +++ b/lib/URLQueue/main.glade @@ -0,0 +1,85 @@ + + + + + + + True + URLQueue + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_NONE + False + True + False + False + False + False + GDK_WINDOW_TYPE_HINT_UTILITY + GDK_GRAVITY_NORTH_WEST + True + False + + + + + True + False + 0 + + + + True + True + GTK_POLICY_ALWAYS + GTK_POLICY_ALWAYS + GTK_SHADOW_IN + GTK_CORNER_TOP_LEFT + + + + True + True + False + False + False + GTK_JUSTIFY_LEFT + GTK_WRAP_NONE + False + 0 + 0 + 0 + 0 + 0 + 0 + + + + + + + + + + + 0 + True + True + + + + + + True + False + + + 0 + False + False + + + + + + + -- cgit v1.2.3