2018-03-15 17:06:26 +00:00
|
|
|
#include "slic3r/Utils/Bonjour.hpp" // On Windows, boost needs to be included before wxWidgets headers
|
|
|
|
|
|
|
|
#include "BonjourDialog.hpp"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/button.h>
|
|
|
|
#include <wx/listctrl.h>
|
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/timer.h>
|
2019-03-12 16:41:43 +00:00
|
|
|
#include <wx/wupdlock.h>
|
2018-03-15 17:06:26 +00:00
|
|
|
|
|
|
|
#include "slic3r/GUI/GUI.hpp"
|
2019-03-14 13:46:01 +00:00
|
|
|
#include "slic3r/GUI/GUI_App.hpp"
|
2018-11-26 13:41:58 +00:00
|
|
|
#include "slic3r/GUI/I18N.hpp"
|
2018-03-15 17:06:26 +00:00
|
|
|
#include "slic3r/Utils/Bonjour.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
2018-11-08 19:18:40 +00:00
|
|
|
class BonjourReplyEvent : public wxEvent
|
2018-03-15 17:06:26 +00:00
|
|
|
{
|
2018-11-08 19:18:40 +00:00
|
|
|
public:
|
2018-03-15 17:06:26 +00:00
|
|
|
BonjourReply reply;
|
|
|
|
|
|
|
|
BonjourReplyEvent(wxEventType eventType, int winid, BonjourReply &&reply) :
|
|
|
|
wxEvent(winid, eventType),
|
|
|
|
reply(std::move(reply))
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual wxEvent *Clone() const
|
|
|
|
{
|
|
|
|
return new BonjourReplyEvent(*this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wxDEFINE_EVENT(EVT_BONJOUR_REPLY, BonjourReplyEvent);
|
|
|
|
|
|
|
|
wxDECLARE_EVENT(EVT_BONJOUR_COMPLETE, wxCommandEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_BONJOUR_COMPLETE, wxCommandEvent);
|
|
|
|
|
|
|
|
class ReplySet: public std::set<BonjourReply> {};
|
|
|
|
|
|
|
|
struct LifetimeGuard
|
|
|
|
{
|
|
|
|
std::mutex mutex;
|
|
|
|
BonjourDialog *dialog;
|
|
|
|
|
|
|
|
LifetimeGuard(BonjourDialog *dialog) : dialog(dialog) {}
|
|
|
|
};
|
|
|
|
|
2019-03-12 16:41:43 +00:00
|
|
|
BonjourDialog::BonjourDialog(wxWindow *parent, Slic3r::PrinterTechnology tech)
|
2019-06-03 14:59:27 +00:00
|
|
|
: wxDialog(parent, wxID_ANY, _(L("Network lookup")), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
|
2019-03-14 13:46:01 +00:00
|
|
|
, list(new wxListView(this, wxID_ANY))
|
2019-03-12 16:41:43 +00:00
|
|
|
, replies(new ReplySet)
|
|
|
|
, label(new wxStaticText(this, wxID_ANY, ""))
|
|
|
|
, timer(new wxTimer())
|
|
|
|
, timer_state(0)
|
|
|
|
, tech(tech)
|
2018-03-15 17:06:26 +00:00
|
|
|
{
|
2019-03-14 13:46:01 +00:00
|
|
|
const int em = GUI::wxGetApp().em_unit();
|
|
|
|
list->SetMinSize(wxSize(80 * em, 30 * em));
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
wxBoxSizer *vsizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
2019-03-14 13:46:01 +00:00
|
|
|
vsizer->Add(label, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, em);
|
2018-03-15 17:06:26 +00:00
|
|
|
|
|
|
|
list->SetSingleStyle(wxLC_SINGLE_SEL);
|
|
|
|
list->SetSingleStyle(wxLC_SORT_DESCENDING);
|
2019-03-14 13:46:01 +00:00
|
|
|
list->AppendColumn(_(L("Address")), wxLIST_FORMAT_LEFT, 5 * em);
|
|
|
|
list->AppendColumn(_(L("Hostname")), wxLIST_FORMAT_LEFT, 10 * em);
|
|
|
|
list->AppendColumn(_(L("Service name")), wxLIST_FORMAT_LEFT, 20 * em);
|
2019-03-12 16:41:43 +00:00
|
|
|
if (tech == ptFFF) {
|
2019-03-14 13:46:01 +00:00
|
|
|
list->AppendColumn(_(L("OctoPrint version")), wxLIST_FORMAT_LEFT, 5 * em);
|
2019-03-12 16:41:43 +00:00
|
|
|
}
|
2018-03-15 17:06:26 +00:00
|
|
|
|
2019-03-14 13:46:01 +00:00
|
|
|
vsizer->Add(list, 1, wxEXPAND | wxALL, em);
|
2018-03-15 17:06:26 +00:00
|
|
|
|
|
|
|
wxBoxSizer *button_sizer = new wxBoxSizer(wxHORIZONTAL);
|
2019-03-14 13:46:01 +00:00
|
|
|
button_sizer->Add(new wxButton(this, wxID_OK, "OK"), 0, wxALL, em);
|
|
|
|
button_sizer->Add(new wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, em);
|
2018-03-15 17:06:26 +00:00
|
|
|
// ^ Note: The Ok/Cancel labels are translated by wxWidgets
|
|
|
|
|
|
|
|
vsizer->Add(button_sizer, 0, wxALIGN_CENTER);
|
|
|
|
SetSizerAndFit(vsizer);
|
|
|
|
|
|
|
|
Bind(EVT_BONJOUR_REPLY, &BonjourDialog::on_reply, this);
|
|
|
|
|
|
|
|
Bind(EVT_BONJOUR_COMPLETE, [this](wxCommandEvent &) {
|
|
|
|
this->timer_state = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
Bind(wxEVT_TIMER, &BonjourDialog::on_timer, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
BonjourDialog::~BonjourDialog()
|
|
|
|
{
|
|
|
|
// Needed bacuse of forward defs
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BonjourDialog::show_and_lookup()
|
|
|
|
{
|
|
|
|
Show(); // Because we need GetId() to work before ShowModal()
|
|
|
|
|
|
|
|
timer->Stop();
|
|
|
|
timer->SetOwner(this);
|
|
|
|
timer_state = 1;
|
|
|
|
timer->Start(1000);
|
|
|
|
wxTimerEvent evt_dummy;
|
|
|
|
on_timer(evt_dummy);
|
|
|
|
|
|
|
|
// The background thread needs to queue messages for this dialog
|
|
|
|
// and for that it needs a valid pointer to it (mandated by the wxWidgets API).
|
|
|
|
// Here we put the pointer under a shared_ptr and protect it by a mutex,
|
|
|
|
// so that both threads can access it safely.
|
|
|
|
auto dguard = std::make_shared<LifetimeGuard>(this);
|
|
|
|
|
2019-03-12 16:41:43 +00:00
|
|
|
// Note: More can be done here when we support discovery of hosts other than Octoprint and SL1
|
|
|
|
Bonjour::TxtKeys txt_keys { "version", "model" };
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
bonjour = std::move(Bonjour("octoprint")
|
2019-03-12 16:41:43 +00:00
|
|
|
.set_txt_keys(std::move(txt_keys))
|
2018-03-15 17:06:26 +00:00
|
|
|
.set_retries(3)
|
|
|
|
.set_timeout(4)
|
|
|
|
.on_reply([dguard](BonjourReply &&reply) {
|
|
|
|
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
|
|
|
|
auto dialog = dguard->dialog;
|
|
|
|
if (dialog != nullptr) {
|
|
|
|
auto evt = new BonjourReplyEvent(EVT_BONJOUR_REPLY, dialog->GetId(), std::move(reply));
|
|
|
|
wxQueueEvent(dialog, evt);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on_complete([dguard]() {
|
|
|
|
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
|
|
|
|
auto dialog = dguard->dialog;
|
|
|
|
if (dialog != nullptr) {
|
|
|
|
auto evt = new wxCommandEvent(EVT_BONJOUR_COMPLETE, dialog->GetId());
|
|
|
|
wxQueueEvent(dialog, evt);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.lookup()
|
|
|
|
);
|
|
|
|
|
|
|
|
bool res = ShowModal() == wxID_OK && list->GetFirstSelected() >= 0;
|
|
|
|
{
|
|
|
|
// Tell the background thread the dialog is going away...
|
|
|
|
std::lock_guard<std::mutex> lock_guard(dguard->mutex);
|
|
|
|
dguard->dialog = nullptr;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString BonjourDialog::get_selected() const
|
|
|
|
{
|
|
|
|
auto sel = list->GetFirstSelected();
|
|
|
|
return sel >= 0 ? list->GetItemText(sel) : wxString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
void BonjourDialog::on_reply(BonjourReplyEvent &e)
|
|
|
|
{
|
|
|
|
if (replies->find(e.reply) != replies->end()) {
|
|
|
|
// We already have this reply
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:41:43 +00:00
|
|
|
// Filter replies based on selected technology
|
|
|
|
const auto model = e.reply.txt_data.find("model");
|
|
|
|
const bool sl1 = model != e.reply.txt_data.end() && model->second == "SL1";
|
2019-06-25 15:23:53 +00:00
|
|
|
if ((tech == ptFFF && sl1) || (tech == ptSLA && !sl1)) {
|
2019-03-12 16:41:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
replies->insert(std::move(e.reply));
|
|
|
|
|
|
|
|
auto selected = get_selected();
|
2019-03-12 16:41:43 +00:00
|
|
|
|
|
|
|
wxWindowUpdateLocker freeze_guard(this);
|
|
|
|
(void)freeze_guard;
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
list->DeleteAllItems();
|
|
|
|
|
|
|
|
// The whole list is recreated so that we benefit from it already being sorted in the set.
|
|
|
|
// (And also because wxListView's sorting API is bananas.)
|
|
|
|
for (const auto &reply : *replies) {
|
|
|
|
auto item = list->InsertItem(0, reply.full_address);
|
|
|
|
list->SetItem(item, 1, reply.hostname);
|
|
|
|
list->SetItem(item, 2, reply.service_name);
|
2019-03-12 16:41:43 +00:00
|
|
|
|
|
|
|
if (tech == ptFFF) {
|
|
|
|
const auto it = reply.txt_data.find("version");
|
|
|
|
if (it != reply.txt_data.end()) {
|
|
|
|
list->SetItem(item, 3, GUI::from_u8(it->second));
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 13:46:01 +00:00
|
|
|
const int em = GUI::wxGetApp().em_unit();
|
|
|
|
|
2019-03-12 16:41:43 +00:00
|
|
|
for (int i = 0; i < list->GetColumnCount(); i++) {
|
|
|
|
list->SetColumnWidth(i, wxLIST_AUTOSIZE);
|
2019-03-14 13:46:01 +00:00
|
|
|
if (list->GetColumnWidth(i) < 10 * em) { list->SetColumnWidth(i, 10 * em); }
|
2018-03-15 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!selected.IsEmpty()) {
|
|
|
|
// Attempt to preserve selection
|
|
|
|
auto hit = list->FindItem(-1, selected);
|
|
|
|
if (hit >= 0) { list->SetItemState(hit, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BonjourDialog::on_timer(wxTimerEvent &)
|
|
|
|
{
|
|
|
|
const auto search_str = _(L("Searching for devices"));
|
|
|
|
|
|
|
|
if (timer_state > 0) {
|
|
|
|
const std::string dots(timer_state, '.');
|
|
|
|
label->SetLabel(wxString::Format("%s %s", search_str, dots));
|
|
|
|
timer_state = (timer_state) % 3 + 1;
|
|
|
|
} else {
|
2018-05-02 11:20:36 +00:00
|
|
|
label->SetLabel(wxString::Format("%s: %s", search_str, _(L("Finished"))+"."));
|
2018-03-15 17:06:26 +00:00
|
|
|
timer->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|