BonjourBrowser.pm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package Slic3r::GUI::BonjourBrowser;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use Wx qw(:dialog :id :misc :sizer :choicebook wxTAB_TRAVERSAL);
  6. use Wx::Event qw(EVT_CLOSE);
  7. use base 'Wx::Dialog';
  8. sub new {
  9. my $class = shift;
  10. my ($parent) = @_;
  11. my $self = $class->SUPER::new($parent, -1, "Device Browser", wxDefaultPosition, [350,700], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  12. # look for devices
  13. eval "use Net::Bonjour; 1";
  14. my $res = Net::Bonjour->new('http');
  15. $res->discover;
  16. $self->{devices} = [ $res->entries ];
  17. # label
  18. my $text = Wx::StaticText->new($self, -1, "Choose an OctoPrint device in your network:", wxDefaultPosition, wxDefaultSize);
  19. # selector
  20. $self->{choice} = my $choice = Wx::Choice->new($self, -1, wxDefaultPosition, wxDefaultSize,
  21. [ map $_->name, @{$self->{devices}} ]);
  22. my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
  23. $main_sizer->Add($text, 1, wxEXPAND | wxALL, 10);
  24. $main_sizer->Add($choice, 1, wxEXPAND | wxALL, 10);
  25. $main_sizer->Add($self->CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND);
  26. $self->SetSizer($main_sizer);
  27. $self->SetMinSize($self->GetSize);
  28. $main_sizer->SetSizeHints($self);
  29. # needed to actually free memory
  30. EVT_CLOSE($self, sub {
  31. $self->EndModal(wxID_OK);
  32. $self->Destroy;
  33. });
  34. return $self;
  35. }
  36. sub GetValue {
  37. my ($self) = @_;
  38. return $self->{devices}[ $self->{choice}->GetSelection ]->address;
  39. }
  40. sub GetPort {
  41. my ($self) = @_;
  42. return $self->{devices}[ $self->{choice}->GetSelection ]->port;
  43. }
  44. 1;