Commit a6616143 by PotatoGim

wxPerl 트위터 예제(from Damien LearnsPerl) 추가.

parent 62ec9797
......@@ -2,7 +2,8 @@
# NOTE: http://damienlearnsperl.blogspot.kr/2009/03/wxperl-adding-gui-to-twitter-script.html
use Wx;
my $VERSION = "1.0";
my $RELEASE_DATE = "Aug 31th 2015";
package MyApp;
......@@ -11,24 +12,27 @@ use base qw/Wx::App/;
sub OnInit
{
my $self = shift;
my $dialog = MyDialog->new("Twit", wxDefaultPosition);
my $self = shift;
my $frame = MyFrame->new("Twit", wxDefaultPosition);
$self->SetTopWindow($dialog);
$dialog->Show(1);
1;
$self->SetTopWindow($frame);
$frame->Show(1);
return 1;
}
1;
package MyDialog;
package MyFrame;
use Wx qw/:everything/;
use Wx::Event qw/:everything/;
use base qw/Wx::Dialog/;
use base qw/Wx::Frame/;
use constant MAX_POST_LENGTH => 140;
use Net::Twitter::Lite;
sub new
{
my $class = shift;
......@@ -53,6 +57,23 @@ sub new
# Display the Wx icon on the application window
$self->SetIcon(Wx::GetWxPerlIcon());
# Menu Bar
my $menu_bar = Wx::MenuBar->new();
my $file_menu = Wx::Menu->new();
my $help_menu = Wx::Menu->new();
$file_menu->Append(11, "E&xit");
$help_menu->Append(21, "&About");
$menu_bar->Append($file_menu, "&File");
$menu_bar->Append($help_menu, "&Help");
# wxFrame method to show a given menu bar
$self->SetMenuBar($menu_bar);
EVT_MENU($self, 11, \&OnQuit);
EVT_MENU($self, 21, \&OnAbout);
# Static box to contain message text box + twitter and identica checkboxes
# A static box is a rectangle drawn around other panel items to denote a
......@@ -141,32 +162,108 @@ sub new
);
# Event associated to "Send" button
EVT_BUTTON($self, $self->{button}{Send}, \&SendMsg);
EVT_BUTTON($self, $self->{button}{Send}, \&SendMsgButtonClicked);
EVT_CLOSE($self, \&OnClose);
$self->{login} = {
username =>
{
twitter => "Twitter.com user name",
identica => "Identica user name",
},
password =>
{
twitter => "Twitter.com password",
identica => "Identica password",
}
};
return $self;
}
sub SendMsg
sub SendMsgButtonClicked
{
my ($self, $event) = @_;
my $twitter, $identica;
my $message1 = "";
my $message2 = "";
$twitter = $self->{checkbox}{twitter}->GetValue();
$identica = $self->{checkbox}{identica}->GetValue();
$message1 = SendMessage("Twitter", $self) if ($self->{checkbox}->{twitter}->GetValue());
$message2 = SendMessage("Identica", $self) if ($self->{checkbox}->{identica}->GetValue());
# For now, just display a MessageBox to test event on button press
Wx::MessageBox(
# text
"Test:\ntwitter is set to $twitter\nidentica is set to $identica",
# title in titlebar
"Caption",
# buttons to display on form
"$message1\n$message2",
"Update Status",
wxOK,
# parent
$self
);
return;
}
sub SendMessage
{
my $return_string;
my ($site_name, $self) = @_;
my $instance = CreateTwitObject($site_name, $self);
if ($instance)
{
$return_string = SendUpdate($instance, $self->{text}->GetValue());
}
else
{
$return_string = "Error with $site_name creation process.";
}
return $return_string;
}
sub SendUpdate
{
my $site = shift;
my $message = shift;
my $site_name = ($site->{identica}) ? "identi.ca" : "twitter.com";
# There's a hard limit on the size of twits for both twitter and identica
if (!defined($message))
{
return "$site_name update: FAILED (no message)";
}
if (length($message) > MAX_POST_LENGTH)
{
return "$site_name update: FAILED (message over MAX_POST_LENGTH chars)";
}
if ($site->update($message))
{
return "$site_name update: OK";
}
else
{
return "$site_name update: FAIL";
}
}
sub CreateTwitObject
{
my $site_instance = 0;
my ($input_string, $self) = @_;
$input_string = lc($input_string);
return undef unless ($input_string =~ m/^(twitter|identica)$/);
$site_instance = Net::Twitter::Lite->new(
identica => ($input_string eq "identica") ? 1 : 0,
username => $self->{login}{username}{$input_string},
password => $self->{login}{password}{$input_string},
legacy_lists_api => 0,
);
return $site_instance;
}
sub CountCharsLeft
......@@ -197,6 +294,24 @@ sub OnClose
$self->Destroy();
}
sub OnQuit
{
my $self = shift;
$self->Close(1);
}
sub OnAbout
{
my $self = shift;
Wx::MessageBox(
"twit GUI $VERSION\n$RELEASE_DATE",
"About",
wxOK | wxCENTRE,
$self
);
}
1;
package main;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment