Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Win32::GUI();
- use Win32::GUI::Constants qw(WS_BORDER MB_OK MB_ICONERROR MB_ICONINFORMATION MB_ICONWARNING ILC_MASK ILC_COLOR24 MB_YESNO MB_ICONQUESTION IDYES ES_READONLY);
- use Cwd;
- use DBI;
- use NET::XMPP;
- #use NET::XMPP::Namespaces qw(add_ns);
- use threads;
- use Text::Iconv;
- #use AnyEvent;
- use Encode qw(decode encode);
- use Win32::Sound;
- use MIME::Base64 qw(encode_base64 decode_base64);
- use warnings;
- use strict;
- #####################
- #use feature 'say';
- #use Data::Dumper;
- #use Lingua::DetectCharset;
- #$| = 1;
- ######################
- my $VERSION = '1.0.0.1';
- use constant {
- PROG_NAME => 'BILCO',
- CHECK_MESSAGES_INTERVAL => 3000,
- MAX_TRANSFER_FILE_SIZE => 10 * 1024 * 1024,
- MAX_TRANSFER_FILE_SIZE_STRING => '10 Мб'
- };
- sub db_connect();
- sub show_error($;$);
- sub wnd_terminate();
- sub xmpp_connect();
- sub xmpp_disconnect_callback();
- sub first_run();
- sub get_option($);
- my $dir = cwd . '\\';
- my $utf8_to_cp1251 = Text::Iconv->new("utf-8", "cp1251");
- my $cp1251_to_utf8 = Text::Iconv->new("cp1251", "utf-8");
- my $query_id = 0;
- my %icons;
- $icons{prog} = new Win32::GUI::Icon($dir . 'icons\\prog.ico');
- #$icons{online} = new Win32::GUI::Icon($dir . 'icons\\online.ico');
- #$icons{offline} = new Win32::GUI::Icon($dir . 'icons\\offline.ico');
- $icons{connected} = new Win32::GUI::Icon($dir . 'icons\\connected.ico');
- $icons{connecting} = new Win32::GUI::Icon($dir . 'icons\\connecting.ico');
- $icons{disconnected} = new Win32::GUI::Icon($dir . 'icons\\disconnected.ico');
- $icons{offline} = new Win32::GUI::Icon($dir . 'icons\\offline.ico');
- $icons{online} = new Win32::GUI::Icon($dir . 'icons\\online.ico');
- $icons{emoticon_grin} = new Win32::GUI::Icon($dir . 'icons\\emoticons\grin.ico');
- $icons{emoticon_smile} = new Win32::GUI::Icon($dir . 'icons\\emoticons\smile.ico');
- $icons{emoticon_surprised} = new Win32::GUI::Icon($dir . 'icons\\emoticons\surprised.ico');
- $icons{emoticon_unhappy} = new Win32::GUI::Icon($dir . 'icons\\emoticons\unhappy.ico');
- $icons{upload} = new Win32::GUI::Icon($dir . 'icons\\upload.ico');
- my $status_imagelist = new Win32::GUI::ImageList(16, 16, ILC_MASK | ILC_COLOR24, 5, 5);
- $status_imagelist->AddIcon($icons{offline});
- $status_imagelist->AddIcon($icons{online});
- my @users_info;
- #my $cv = AnyEvent->condvar();
- my ($timer, $first_run_wnd);
- ##### Создание главного окна программы #####
- my $menu = Win32::GUI::MakeMenu(
- 'Меню' => 0,
- ' > Настройки' => {
- -onClick => \&show_settings_wnd,
- },
- ' > О программе' => {
- -onClick => sub { },
- },
- ' > Выход' => {
- -onClick => \&wnd_terminate
- }
- );
- my $wnd;
- my $accel = new Win32::GUI::AcceleratorTable(
- #'Ctrl-Return' => sub {
- #add_new_line() if ($wnd->GetFocus == $wnd->message->{-handle}); # Перевод строки в поле ввода сообщения по CtrlEnter
- #},
- 'Return' => sub {
- send_message() if ($wnd->GetFocus == $wnd->message->{-handle}); # Отправка по нажатию Enter
- }
- );
- $wnd = new Win32::GUI::Window(
- -name => 'wnd',
- -size => [600, 450],
- -minsize => [600, 450],
- -text => PROG_NAME,
- -maximizebox => 1,
- -minimizebox => 1,
- -sizable => 1,
- -resizable => 1,
- -menu => $menu,
- -onTerminate => \&wnd_terminate,
- -onTimer => \&process,
- -onResize => \&resize,
- -accel => $accel
- );
- $wnd->SetIcon($icons{prog});
- $wnd->AddListView(
- -name => 'userlist',
- -pos => [10, 10],
- -width => 150,
- #-sort => 1,
- -vscroll => 1,
- -popstyle => WS_BORDER,
- -gridlines => 0,
- -nocolumnheader => 1,
- #-singlesel => 1,
- -showselalways => 1,
- -fullrowselect => 1,
- #-oneclickactivate => 1,
- #-subitemimages => 1
- -imagelist => $status_imagelist,
- -onClick => \&user_change_callback
- );
- $wnd->userlist->InsertColumn(
- -width => 100
- );
- $wnd->userlist->InsertColumn(
- -width => 40,
- -align => 'center'
- );
- $wnd->AddRichEdit(
- -name => 'messages',
- -pos => [180, 10],
- #-popstyle => WS_BORDER,
- -vscroll => 1,
- -readonly => 1
- #-pushstyle => ES_READONLY
- );
- $wnd->messages->AutoURLDetect(1);
- $wnd->messages->SetWrapMode(1);
- $wnd->AddTextfield(
- -name => 'message',
- -left => 180,
- -height => 80,
- -popstyle => WS_BORDER,
- -multiline => 1,
- -vscroll => 1
- );
- $wnd->AddButton(
- -name => 'send_file_btn',
- -size => [24, 24],
- -icon => $icons{upload},
- -onClick => \&send_file
- );
- $wnd->AddButton(
- -name => 'send_message_btn',
- -text => 'Отправить',
- -size => [100, 24],
- -onClick => \&send_message
- );
- $wnd->AddButton(
- -name => 'emoticon_smile_btn',
- -left => 180,
- -size => [24, 24],
- -icon => $icons{emoticon_smile},
- -onClick => sub {insert_emoticon('smile');}
- );
- $wnd->AddButton(
- -name => 'emoticon_unhappy_btn',
- -left => 214,
- -size => [24, 24],
- -icon => $icons{emoticon_unhappy},
- -onClick => sub {insert_emoticon('unhappy');}
- );
- $wnd->AddButton(
- -name => 'emoticon_surprised_btn',
- -left => 248,
- -size => [24, 24],
- -icon => $icons{emoticon_surprised},
- -onClick => sub {insert_emoticon('surprised');}
- );
- $wnd->AddButton(
- -name => 'emoticon_grin_btn',
- -left => 282,
- -size => [24, 24],
- -icon => $icons{emoticon_grin},
- -onClick => sub {insert_emoticon('grin');}
- );
- $wnd->AddStatusBar(
- -name => 'statusbar'
- );
- $wnd->statusbar->SetParts(120);
- xmpp_disconnect_callback();
- $wnd->Center;
- $wnd->Show;
- ##### Создание окна настроек #####
- my $settings_wnd = new Win32::GUI::DialogBox(
- -name => 'settings_wnd',
- -title => 'Настройки',
- -size => [400, 300],
- -parent => $wnd,
- -hashelp => 0
- );
- $settings_wnd->SetIcon($icons{prog});
- $settings_wnd->AddGroupbox(
- -name => 'acc_group',
- -text => 'Аккаунт',
- -pos => [10, 10],
- -size => [$settings_wnd->ScaleWidth - 20, 90]
- );
- $settings_wnd->AddTextfield(
- -name => 'login',
- -prompt => ['Логин:', 60],
- -size => [150, 20],
- -pos => [20, 30],
- -popstyle => WS_BORDER,
- -tabstop => 1
- );
- $settings_wnd->AddTextfield(
- -name => 'password',
- -prompt => ['Пароль:', 60],
- -size => [150, 20],
- -pos => [20, 60],
- -popstyle => WS_BORDER,
- -tabstop => 1
- );
- $settings_wnd->AddGroupbox(
- -name => 'connect_group',
- -text => 'Подключение',
- -pos => [10, 120],
- -size => [$settings_wnd->ScaleWidth - 20, 90]
- );
- $settings_wnd->AddTextfield(
- -name => 'server',
- -prompt => ['Сервер:', 60],
- -size => [150, 20],
- -pos => [20, 140],
- -popstyle => WS_BORDER,
- -tabstop => 1
- );
- $settings_wnd->AddTextfield(
- -name => 'port',
- -prompt => ['Порт:', 60],
- -size => [50, 20],
- -pos => [20, 170],
- -popstyle => WS_BORDER,
- -tabstop => 1
- );
- $settings_wnd->AddButton(
- -name => 'save_btn',
- -text => 'Сохранить',
- -pos => [$settings_wnd->ScaleWidth - 220, $settings_wnd->ScaleHeight - 34],
- -size => [100, 24],
- -ok => 1,
- -onClick => \&save_settings,
- -tabstop => 1
- );
- $settings_wnd->AddButton(
- -name => 'cancel_btn',
- -text => 'Отмена',
- -pos => [$settings_wnd->ScaleWidth - 110, $settings_wnd->ScaleHeight - 34],
- -size => [100, 24],
- -cancel => 1,
- -onClick => sub {return -1;},
- -tabstop => 1
- );
- #$settings_wnd->Center;
- #$settings_wnd->Show;
- ##### Выполнение операций при запуске программы #####
- my $dbh;
- unless ($dbh = db_connect()) {
- show_error("Ошибка подключения к БД:\n\n$DBI::errstr");
- wnd_terminate();
- }
- first_run() if get_option('first_run');
- $dbh->do('DELETE FROM `messages` WHERE julianday("now") - julianday(`date`) > 7'); # Удаляем из истории старые сообщения
- my $client = new Net::XMPP::Client(
- #debuglevel => 1,
- #debugfile => $dir . 'xmpp.log'
- );
- $client->SetCallBacks(
- onauth => \&xmpp_connect_callback(),
- ondisconnect => \&xmpp_disconnect_callback(),
- message => \&recieve_message_callback,
- presence => \&presence_callback,
- iq => \&iq_callback
- );
- # Расширение для передачи файлов
- my %args = (
- ns => "file-transfer",
- tag => "file",
- xpath => {
- Filename => {
- path => '@Filename'
- },
- Content => {
- path => 'text()'
- }
- }
- );
- Net::XMPP::Namespaces::add_ns(%args);
- xmpp_connect();
- Win32::GUI::Dialog();
- exit 0;
- sub wnd_terminate() {
- if (Win32::GUI::MessageBox($wnd, 'Вы уверены, что хотите завершить работу?', 'Выход', MB_YESNO | MB_ICONQUESTION) == IDYES) {
- db_disconnect();
- xmpp_disconnect();
- return -1;
- } else {
- return 0;
- }
- }
- sub show_error($;$) {
- my ($message, $w) = @_;
- $w = $wnd unless defined $w;
- Win32::GUI::MessageBox($w, $message, '', MB_OK | MB_ICONERROR);
- }
- sub show_info($;$) {
- my ($message, $w) = @_;
- $w = $wnd unless defined $w;
- Win32::GUI::MessageBox($w, $message, '', MB_OK | MB_ICONINFORMATION);
- }
- sub show_warning($;$) {
- my ($message, $w) = @_;
- $w = $wnd unless defined $w;
- Win32::GUI::MessageBox($w, $message, '', MB_OK | MB_ICONWARNING);
- }
- sub db_connect() {
- my $dsn='dbi:SQLite:dbname=db.sqlite';
- my %opts = (
- PrintError => 1,
- RaiseError => 1
- );
- return DBI->connect($dsn, '', '', \%opts);
- }
- sub db_disconnect() {
- $dbh->disconnect() if defined $dbh;
- }
- sub xmpp_connect() {
- #async {
- $wnd->statusbar->SetIcon(0, $icons{connecting});
- $wnd->statusbar->SetText(0, 'Подключение');
- #my $status = 0;
- my $result = $client->Connect(
- hostname => get_option('server'),
- port => get_option('port'),
- timeout => 7
- );
- if ($result) {
- my @result = $client->AuthSend(
- username => get_option('login'),
- password => get_option('password'),
- resource => 'bilco'
- );
- if ($result[0] eq 'ok') {
- #$status = 1;
- }
- }
- #print "$!\n" unless $status;
- #if ($status) {
- my $status = $client->Connected();
- if ($status) {
- xmpp_connect_callback();
- } else {
- #xmpp_disconnect_callback();
- }
- #};
- return $status;
- }
- sub xmpp_disconnect() {
- #my $pres = Net::XMPP::Presence->new();
- #$pres->SetType('unavailable');
- #$client->Send($pres);
- $client->Disconnect unless defined $client;
- }
- sub xmpp_reconnect() {
- xmpp_disconnect();
- xmpp_connect();
- }
- sub xmpp_connect_callback() {
- if ($client->Connected) {
- $wnd->statusbar->SetIcon(0, $icons{connected});
- $wnd->statusbar->SetText(0, 'Подключён');
- update_user_list();
- my $pres = Net::XMPP::Presence->new();
- $pres->SetType('available');
- $client->Send($pres);
- $wnd->send_message_btn->Enable;
- #$cv->begin;
- #while (1) {
- # if ($client->Connected) {
- # $client->Connect() unless defined $client->Process(1);
- # } else {
- # last unless $client->Connect();
- # }
- #
- # sleep CHECK_MESSAGES_INTERVAL;
- #}
- #$cv->end;
- $timer = $wnd->AddTimer('timer', CHECK_MESSAGES_INTERVAL);
- }
- }
- sub xmpp_disconnect_callback() {
- $wnd->statusbar->SetIcon(0, $icons{disconnected});
- $wnd->statusbar->SetText(0, 'Отключён');
- $wnd->userlist->Clear;
- #$wnd->messages->Clear;
- $wnd->messages->Text('');
- $wnd->userlist->Clear;
- $wnd->send_message_btn->Disable;
- $timer->Kill(1) if defined $timer;
- }
- sub show_settings_wnd() {
- $settings_wnd->login->Text(get_option('login'));
- $settings_wnd->password->Text(get_option('password'));
- $settings_wnd->server->Text(get_option('server'));
- $settings_wnd->port->Text(get_option('port'));
- $settings_wnd->Center($wnd);
- $settings_wnd->Show;
- $settings_wnd->DoModal;
- #return 1;
- }
- sub save_settings() {
- update_option('login', $settings_wnd->login->Text);
- update_option('password', $settings_wnd->password->Text);
- update_option('server', $settings_wnd->server->Text);
- update_option('port', $settings_wnd->port->Text);
- xmpp_reconnect();
- return -1;
- }
- sub get_option($) {
- my ($name) = @_;
- my $sth = $dbh->prepare('SELECT `value` FROM `settings` WHERE `name` = ?');
- $sth->bind_param(1, $name);
- $sth->execute;
- my ($val) = $sth->fetchrow;
- return $val;
- }
- sub update_option($$) {
- my ($name, $value) = @_;
- my $sth = $dbh->prepare('UPDATE `settings` SET `value` = ? WHERE `name` = ?');
- $sth->bind_param(1, $value);
- $sth->bind_param(2, $name);
- $sth->execute;
- }
- sub update_user_list() {
- if ($client->Connected) {
- # Обновновление список контактов
- #$client->RosterRequest();
- my %roster = $client->RosterGet();
- #my $roster = $client->Roster();
- #my $pres = Net::XMPP::Presence->new();
- #my $type = $pres->GetType;
- $wnd->userlist->DeleteAllItems;
- @users_info = ();
- my $server = get_option('server');
- foreach my $jid (sort(keys %roster)) {
- my $user_id = str_replace('@' . $server, '', $jid);
- my $user_name;
- if ($roster{$jid}{name}) {
- $user_name = $roster{$jid}{name};
- } else {
- $user_name = $user_id;
- }
- #$wnd->userlist->Add($utf8_to_cp1251->convert($user_name));
- my $unread_count = 0; # TODO читать сохранённое значение
- $wnd->userlist->InsertItem(
- -image => 0,
- -text => [$utf8_to_cp1251->convert($user_name), $unread_count ? "($unread_count)" : '']
- );
- push(@users_info, {
- id => $user_id,
- name => $user_name,
- unread => $unread_count
- });
- }
- } else {
- $wnd->userlist->DeleteAllItems;
- }
- }
- sub str_replace($$$) {
- my ($search, $replace, $subject) = @_;
- my $pos = index($subject, $search);
- while($pos > -1) {
- substr($subject, $pos, length($search), $replace);
- $pos = index($subject, $search, $pos + length($replace));
- }
- return $subject;
- }
- sub user_change_callback() {
- clear_messages();
- my ($idx) = $wnd->userlist->SelectedItems();
- unless ($idx == -1) {
- my $user_id = $users_info[$idx]{id};
- my $user_name = $users_info[$idx]{name};
- my $sth = $dbh->prepare('SELECT * FROM `messages` WHERE `user_id` = ? ORDER BY `date` ASC');
- $sth->bind_param(1, $user_id);
- $sth->execute;
- while (my $row = $sth->fetchrow_hashref) {
- show_message(
- $row->{income} ? $user_name : get_option('login'), #TODO имя если есть
- $row->{income},
- $row->{date},
- $row->{text}
- );
- }
- $wnd->userlist->SetItemText($idx, '', 1);
- $users_info[$idx]{unread} = 0;
- } else {
- # ...
- }
- }
- sub show_message($$$$) {
- my ($user_name, $income, $date, $text) = @_; #TODO можно имя выбранного пользователя брать
- #$user_name = $utf8_to_cp1251->convert($user_name);
- my $color = $income ? 0x0000D0 : 0xA40000;
- my ($year, $mon, $day, $hour, $min, $sec) = $date =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
- my (undef, undef, undef, $day2, $mon2, $year2) = localtime(time);
- $mon2++;
- $year2 += 1900;
- if ($day == $day2 && $mon == $mon2 && $year == $year2) {
- $date = sprintf('%02d:%02d:%02d', $hour, $min, $sec);
- } else {
- $date = sprintf('%02d.%02d.%04d %02d:%02d:%02d', $day, $mon, $year, $hour, $min, $sec);
- }
- #$text = $utf8_to_cp1251->convert($text);;
- $wnd->messages->SetCharFormat(
- -color => $color
- );
- $wnd->messages->SetSel($wnd->messages->TextLength, $wnd->messages->TextLength);
- $wnd->messages->ReplaceSel("[$date] <$user_name>: ");
- $wnd->messages->SetCharFormat(
- -color => 0x000000
- );
- $wnd->messages->SetSel($wnd->messages->TextLength, $wnd->messages->TextLength);
- $wnd->messages->ReplaceSel("$text\n");
- $wnd->messages->ScrollCaret;
- #$wnd->messages->Redraw(1);
- ######################################
- #say Lingua::DetectCharset::Detect ($text) . " - $text";
- ######################################
- }
- sub clear_messages() {
- #$wnd->messages->Clear;
- $wnd->messages->Text('');
- }
- sub send_message() {
- my ($idx) = $wnd->userlist->SelectedItems();
- my $to = $users_info[$idx]{id} . '@' . get_option('server');
- my $message = $wnd->message->Text();
- $client->MessageSend(
- to => $cp1251_to_utf8->convert($to),
- type => 'chat',
- body => $cp1251_to_utf8->convert($message)
- );
- $wnd->message->Text('');
- #todo меняем на имя
- my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
- $mon++;
- $year += 1900;
- my $date = sprintf('%04d-%02d-%02d %02d:%02d:%02d', $year, $mon, $day, $hour, $min, $sec);
- show_message(get_option('login'), 0, $date, $message); # TODO addmessge -> show_message
- my $sth = $dbh->prepare('INSERT INTO `messages` (`user_id`, `income`, `date`, `text`) VALUES(?, 0, ?, ?)');
- $sth->bind_param(1, $users_info[$idx]{id});
- $sth->bind_param(2, $date);
- $sth->bind_param(3, $message);
- $sth->execute;
- }
- sub recieve_message_callback() {
- my ($mid, $message) = @_;
- my ($current_idx) = $wnd->userlist->SelectedItems;
- my $user_id = $utf8_to_cp1251->convert($message->GetFrom('jid')->GetUserID);
- my $idx = get_user_idx_by_id($user_id);
- my $user_name = $users_info[$idx]{name};
- my $text = $utf8_to_cp1251->convert(decode('utf-8', $message->GetBody));
- my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
- $mon++;
- $year += 1900;
- my $date = sprintf('%04d-%02d-%02d %02d:%02d:%02d', $year, $mon, $day, $hour, $min, $sec);
- my $sth = $dbh->prepare('INSERT INTO `messages` (`user_id`, `income`, `date`, `text`) VALUES(?, 1, ?, ?)');
- $sth->bind_param(1, $user_id);
- $sth->bind_param(2, $date);
- $sth->bind_param(3, $text);
- $sth->execute;
- if ($current_idx eq $idx) {
- #say Lingua::DetectCharset::Detect ($text) . " - $text";
- show_message($user_name, 1, $date, $text); # TODO addmessge -> show_message
- } else {
- my $unread_count = ++$users_info[$idx]{unread};
- $wnd->userlist->SetItemText($idx, "($unread_count)", 1);
- }
- Win32::Sound::Stop;
- Win32::Sound::Play($dir . 'notify.wav');
- }
- sub process() {
- #if ($client->Connected) {
- # $client->Connect() unless defined $client->Process(0);
- #} else {
- # last unless $client->Connect();
- #}
- unless (defined $client->Process(0)) {
- #unless ($client->Connected) {
- $timer->Kill(1);
- ##########
- #print "Disconnected: " . $! . "\n";
- #print "Disconnected: " . $! . "\n";
- ###########
- xmpp_disconnect_callback;
- #}
- }
- }
- sub get_user_idx_by_id($) {
- my ($user_id) = @_;
- foreach (0 .. $wnd->userlist->Count) {
- if ($users_info[$_]{id} eq $user_id) {
- return $_;
- }
- }
- return -1;
- }
- sub presence_callback() {
- my $pres = $_[1];
- my $user_id = $pres->GetFrom("jid")->GetUserID;
- my $type = $pres->GetType;
- #print "$user_id $type\n";
- my $idx = get_user_idx_by_id($user_id);
- unless ($idx == -1) {
- $wnd->userlist->ChangeItem(-image => $type eq 'unavailable' ? 0 : 1);
- }
- }
- sub insert_emoticon($) {
- my ($emoticon) = @_;
- $emoticon = ':-)' if $emoticon eq 'smile';
- $emoticon = ':-(' if $emoticon eq 'unhappy';
- $emoticon = ':-D' if $emoticon eq 'grin';
- $emoticon = ':-o' if $emoticon eq 'surprised';
- $wnd->message->ReplaceSel(" $emoticon ");
- }
- sub resize() {
- my $w = $wnd->ScaleWidth;
- my $h = $wnd->ScaleHeight;
- $wnd->userlist->Height($h - 50);
- $wnd->messages->Resize($w - 190, $h - 180);
- $wnd->message->Top($h - 150);
- $wnd->message->Width($w - 190);
- $wnd->send_message_btn->Move($w - 110, $h - 60);
- $wnd->send_file_btn->Move($w - 150, $h - 60);
- $wnd->emoticon_smile_btn->Top($h - 60);
- $wnd->emoticon_unhappy_btn->Top($h - 60);
- $wnd->emoticon_surprised_btn->Top($h - 60);
- $wnd->emoticon_grin_btn->Top($h - 60);
- $wnd->statusbar->Move(0, $h - $wnd->statusbar->Height());
- $wnd->statusbar->Resize($w, $h);
- }
- sub send_file() {
- my $file_path = Win32::GUI::GetOpenFileName(
- -title => 'Выберите файл для отправки'
- );
- if ($file_path) {
- my $file_size = -s $file_path;
- if ($file_size > MAX_TRANSFER_FILE_SIZE) {
- show_warning('Размер файла не должен превышать ' . MAX_TRANSFER_FILE_SIZE_STRING . '!');
- } else {
- my $iq = new Net::XMPP::IQ;
- my $idx = $wnd->userlist->SelectedItems;
- $iq->SetIQ(
- to => $users_info[$idx]{id} . '@' . get_option('server') . '/bilco',
- from => get_option('login') . '@' . get_option('server') . '/bilco',
- id => $query_id++,
- type => 'set'
- );
- my $query = $iq->NewChild('file-transfer');
- my ($file_name) = $file_path =~ /([^\\]+)$/;
- my $content;
- open(my $fh, "< $file_path"); #TODO сообщения об ошибках
- binmode($fh);
- read($fh, $content, $file_size);
- close($fh);
- $query->SetFilename($file_name);
- $query->SetContent(encode_base64($content));
- $client->Send($iq);
- }
- }
- }
- sub iq_callback() {
- #use Data::Dumper;
- #print "=========IQ=============\n";
- #print Dumper(\@_) . "\n\n";
- my ($id, $iq) = @_;
- my $query = $iq->GetQuery;
- #print "recieved iq " . $iq->GetQueryXMLNS() . "\n";
- if ($iq->GetQueryXMLNS() eq 'file-transfer') {
- if ($iq->DefinedError()) {
- print "file transfer error: (" . $iq->GetErrorCode . ") " . $iq->GetError();
- } else {
- my $file_name = $query->GetFilename();
- print "recieved file $file_name\n";
- # Doing something with file ...
- }
- }
- }
- sub first_run() {
- $first_run_wnd = new Win32::GUI::DialogBox(
- -size => [220, 150],
- #-size => [220, 210],
- -parent => $wnd,
- -hashelp => 0
- );
- $first_run_wnd->AddTextfield(
- -name => 'login',
- -pos => [10, 10],
- -size => [$first_run_wnd->ScaleWidth - 70, 20],
- -prompt => ['Логин:', 50],
- -popstyle => WS_BORDER
- );
- $first_run_wnd->AddTextfield(
- -name => 'password',
- -pos => [10, 40],
- -size => [$first_run_wnd->ScaleWidth - 70, 20],
- -prompt => ['Пароль:', 50],
- -popstyle => WS_BORDER
- );
- $first_run_wnd->AddTextfield(
- -name => 'fio',
- -pos => [10, 70],
- -size => [$first_run_wnd->ScaleWidth - 70, 20],
- -prompt => ['ФИО:', 50],
- -popstyle => WS_BORDER,
- -visible => 0
- );
- $first_run_wnd->AddTextfield(
- -name => 'email',
- -pos => [10, 100],
- -size => [$first_run_wnd->ScaleWidth - 70, 20],
- -prompt => ['Email:', 50],
- -popstyle => WS_BORDER,
- -visible => 0
- );
- #$first_run_wnd->AddButton(
- # -name => 'sign_up_btn',
- # -text => 'Регистрация',
- # -pos => [$first_run_wnd->ScaleWidth / 2 - 50, 140],
- # -size => [100, 24]
- #);
- $first_run_wnd->AddButton(
- -name => 'sign_in_btn',
- -text => 'Войти',
- -pos => [10, 80],
- -size => [80, 24]
- );
- $first_run_wnd->AddButton(
- -name => 'sign_up_btn',
- -text => 'Регистрация',
- -pos => [$first_run_wnd->ScaleWidth - 110, 80],
- -size => [100, 24],
- -onClick => \&show_reg
- );
- $first_run_wnd->Center;
- $first_run_wnd->Show;
- Win32::GUI::Dialog;
- }
- sub show_reg() {
- $first_run_wnd->Height(210);
- $first_run_wnd->fio_Prompt->Show;
- $first_run_wnd->fio->Show;
- $first_run_wnd->email_Prompt->Show;
- $first_run_wnd->email->Show;
- $first_run_wnd->sign_in_btn->Hide;
- $first_run_wnd->sign_up_btn->Move($first_run_wnd->ScaleWidth / 2 - 50, 140);
- }
Advertisement
Add Comment
Please, Sign In to add comment