Skip to content

Commit

Permalink
fixed network list update
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkv committed Jul 1, 2019
1 parent 2acf425 commit 94b1308
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 99 deletions.
7 changes: 6 additions & 1 deletion ui/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (m *filesPanel) doLoadFiles() {

Logger.Info("Loading list of files from: ", string(m.location.current()))

// m.doRefreshSD()
r := &octoprint.FilesRequest{Location: m.location.current(), Recursive: false}
folder, err := r.Do(m.UI.Printer)
if err != nil {
Expand Down Expand Up @@ -116,8 +115,10 @@ func (m *filesPanel) addFile(b *gtk.Box, f *octoprint.FileInformation) {
name := MustLabel(f.Name)
name.SetMarkup(fmt.Sprintf("<big>%s</big>", strEllipsis(f.Name)))
name.SetHExpand(true)
name.SetHAlign(gtk.ALIGN_START)

info := MustLabel("")
info.SetHAlign(gtk.ALIGN_START)
info.SetMarkup(fmt.Sprintf("<small>Uploaded: <b>%s</b> - Size: <b>%s</b></small>",
humanize.Time(f.Date.Time), humanize.Bytes(uint64(f.Size)),
))
Expand All @@ -127,6 +128,7 @@ func (m *filesPanel) addFile(b *gtk.Box, f *octoprint.FileInformation) {
labels.Add(info)
labels.SetVExpand(true)
labels.SetVAlign(gtk.ALIGN_CENTER)
labels.SetHAlign(gtk.ALIGN_START)

actions := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
actions.Add(m.createLoadAndPrintButton("print.svg", f, true))
Expand All @@ -153,8 +155,10 @@ func (m *filesPanel) addFolder(b *gtk.Box, f *octoprint.FileInformation) {
name := MustLabel(f.Name)
name.SetMarkup(fmt.Sprintf("<big>%s</big>", strEllipsis(f.Name)))
name.SetHExpand(true)
name.SetHAlign(gtk.ALIGN_START)

info := MustLabel("")
info.SetHAlign(gtk.ALIGN_START)
info.SetMarkup(fmt.Sprintf("<small>Size: <b>%s</b></small>",
humanize.Bytes(uint64(f.Size)),
))
Expand All @@ -164,6 +168,7 @@ func (m *filesPanel) addFolder(b *gtk.Box, f *octoprint.FileInformation) {
labels.Add(info)
labels.SetVExpand(true)
labels.SetVAlign(gtk.ALIGN_CENTER)
labels.SetHAlign(gtk.ALIGN_START)

actions := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
actions.Add(m.createOpenFolderButton(f))
Expand Down
211 changes: 113 additions & 98 deletions ui/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var networkPanelInstance *networkPanel

type networkPanel struct {
CommonPanel
wpa wpasupplicant.Conn
list *gtk.Box
netStatus, wifiStatus *gtk.Label
}

func NetworkPanel(ui *UI, parent Panel) Panel {
if networkPanelInstance == nil {
m := &networkPanel{CommonPanel: NewCommonPanel(ui, parent)}
m.initialize()
m.b = NewBackgroundTask(time.Second*3, m.update)
networkPanelInstance = m
} else {
networkPanelInstance.p = parent
Expand All @@ -28,66 +30,88 @@ func NetworkPanel(ui *UI, parent Panel) Panel {
return networkPanelInstance
}

func (m *networkPanel) initialize() {
m.wpa, _ = wpasupplicant.Unixgram("wlan0")

m.Grid().Attach(m.createNetworkList(), 0, 0, 3, 1)
m.Grid().Attach(m.createLeftBar(), 3, 0, 2, 1)
}
func (m *networkPanel) update() {
EmptyContainer(&m.list.Container)

func (m *networkPanel) createNetworkList() gtk.IWidget {
netStatus := ""
addrs, _ := net.InterfaceAddrs()

if m.wpa == nil {
return MustLabel("Wifi management is not available\non this hardware")
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
netStatus += fmt.Sprintf("IP Address: %s\n", ipnet.IP.String())
}
}
}

list := MustBox(gtk.ORIENTATION_VERTICAL, 0)
list.SetVExpand(true)
m.netStatus.SetText(netStatus)

scroll, _ := gtk.ScrolledWindowNew(nil, nil)
scroll.SetProperty("overlay-scrolling", false)
scroll.Add(list)
wpa, _ := wpasupplicant.Unixgram("wlan0")

if wpa != nil {
s, _ := wpa.Status()
wifiStatus := "No Wifi Connection"

result, _ := m.wpa.ScanResults()
if s.WPAState() == "COMPLETED" {
wifiStatus = fmt.Sprintf("Wifi Information\nSSID: %s\nIP Address: %s\n",
s.SSID(), s.IPAddr())
} else {
wifiStatus = fmt.Sprintf("Wifi status is: %s\n", s.WPAState())
}

for _, bss := range result {
m.addNetwork(list, bss.SSID())
m.wifiStatus.SetText(wifiStatus)

result, _ := wpa.ScanResults()
for _, bss := range result {
m.addNetwork(m.list, bss.SSID())
}

wpa.Scan()

} else {
m.list.Add(MustLabel("\n\nWifi management is not available\non this hardware"))
}

m.list.ShowAll()
}

func (m *networkPanel) initialize() {
m.Grid().Attach(m.createNetworkList(), 0, 0, 4, 1)
m.Grid().Attach(m.createLeftBar(), 4, 0, 3, 1)
}

func (m *networkPanel) createNetworkList() gtk.IWidget {

m.list = MustBox(gtk.ORIENTATION_VERTICAL, 0)
m.list.SetVExpand(true)

scroll, _ := gtk.ScrolledWindowNew(nil, nil)
scroll.SetProperty("overlay-scrolling", false)
scroll.Add(m.list)

return scroll
}

func (m *networkPanel) addNetwork(b *gtk.Box, n string) {
frame, _ := gtk.FrameNew("")

name := MustLabel(n)
name.SetMarkup(fmt.Sprintf("<big>%s</big>", strEllipsisLen(n, 18)))
name.SetHExpand(true)
clicked := func() { m.UI.Add(ConnectionPanel(m.UI, m, n)) }

actions := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
actions.Add(m.createConnectButton(n))
button := MustButton(MustImageFromFileWithSize("network.svg", m.Scaled(25), m.Scaled(25)), clicked)

network := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
network.SetMarginTop(5)
network.SetMarginEnd(15)
network.SetMarginStart(15)
network.SetMarginBottom(5)
name := MustButtonText(strEllipsisLen(n, 18), clicked)
name.SetHExpand(true)

network := MustBox(gtk.ORIENTATION_HORIZONTAL, 0)
network.SetHExpand(true)

network.Add(MustImageFromFileWithSize("network.svg", m.Scaled(35), m.Scaled(35)))
network.Add(name)
network.Add(actions)
network.Add(button)

frame.Add(network)
b.Add(frame)
}

func (m *networkPanel) createConnectButton(n string) gtk.IWidget {
return MustButton(MustImageFromFileWithSize("open.svg", m.Scaled(40), m.Scaled(40)), func() {
m.UI.Add(ConnectionPanel(m.UI, m, n))
})
}

func (m *networkPanel) createLeftBar() gtk.IWidget {

bar := MustBox(gtk.ORIENTATION_VERTICAL, 5)
Expand Down Expand Up @@ -128,32 +152,15 @@ func (m *networkPanel) createInfoBar() gtk.IWidget {
t1.SetMarginTop(25)

info.Add(t1)
addrs, _ := net.InterfaceAddrs()

for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
info.Add(MustLabel("IP Address: <b>%s</b>", ipnet.IP.String()))
}
}
}

if m.wpa != nil {
s, _ := m.wpa.Status()
text := "<b>No Wifi Connection</b>"

if s.WPAState() == "COMPLETED" {
text = fmt.Sprintf("<b>Wifi Information</b>\nSSID: <b>%s</b>\nIP Address: <b>%s</b>",
s.SSID(), s.IPAddr())
} else {
text = fmt.Sprintf("<b>Wifi status is: %s", s.WPAState())
}
m.netStatus = MustLabel("")
m.netStatus.SetHAlign(gtk.ALIGN_START)
info.Add(m.netStatus)

t2 := MustLabel(text)
t2.SetHAlign(gtk.ALIGN_START)
t2.SetMarginTop(25)
info.Add(t2)
}
m.wifiStatus = MustLabel("")
m.wifiStatus.SetHAlign(gtk.ALIGN_START)
m.wifiStatus.SetMarginTop(25)
info.Add(m.wifiStatus)

return info
}
Expand Down Expand Up @@ -190,6 +197,22 @@ func ConnectionPanel(ui *UI, parent Panel, SSID string) Panel {
}

func (m *connectionPanel) initialize() {

layout := MustBox(gtk.ORIENTATION_VERTICAL, 5)
layout.SetHExpand(true)

layout.Add(m.createTopBar())
layout.Add(m.createKeyBoard())
layout.Add(m.createActionBar())
m.Grid().Add(layout)
}

func (m *connectionPanel) setSSID(SSID string) {
m.SSID = SSID
m.SSIDLabel.SetText(fmt.Sprintf("Enter password for \"%s\": ", strEllipsisLen(m.SSID, 18)))
}

func (m *connectionPanel) createTopBar() gtk.IWidget {
m.pass, _ = gtk.EntryNew()
m.pass.SetProperty("height-request", m.Scaled(40))
m.pass.SetProperty("width-request", m.Scaled(150))
Expand All @@ -203,7 +226,7 @@ func (m *connectionPanel) initialize() {
top.Add(m.SSIDLabel)
top.Add(m.pass)

delButton := MustButton(MustImageFromFileWithSize("backspace.svg", m.Scaled(40), m.Scaled(40)), func() {
backspace := MustButton(MustImageFromFileWithSize("backspace.svg", m.Scaled(40), m.Scaled(40)), func() {
if m.cursorPosition == 0 {
return
}
Expand All @@ -212,20 +235,8 @@ func (m *connectionPanel) initialize() {
m.cursorPosition -= 1
})

top.Add(delButton)

layout := MustBox(gtk.ORIENTATION_VERTICAL, 5)
layout.SetHExpand(true)

layout.Add(top)
layout.Add(m.createKeyBoard())
layout.Add(m.createActionBar())
m.Grid().Add(layout)
}

func (m *connectionPanel) setSSID(SSID string) {
m.SSID = SSID
m.SSIDLabel.SetText(fmt.Sprintf("Enter password for \"%s\": ", strEllipsisLen(m.SSID, 18)))
top.Add(backspace)
return top
}

func (m *connectionPanel) createActionBar() gtk.IWidget {
Expand All @@ -235,15 +246,9 @@ func (m *connectionPanel) createActionBar() gtk.IWidget {

back.SetHAlign(gtk.ALIGN_END)

connect := MustButtonText("Connect", m.connectToNetwork)
ctx, _ := connect.GetStyleContext()
ctx.AddClass("color3")

connect.SetProperty("width-request", m.Scaled(150))

layout := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
layout.SetHAlign(gtk.ALIGN_END)
layout.Add(connect)
layout.Add(m.createConnectToNetworkButtom())
layout.Add(back)
return layout
}
Expand Down Expand Up @@ -272,26 +277,36 @@ func (m *connectionPanel) createKeyBoard() gtk.IWidget {
return keyBoard
}

func (m *connectionPanel) connectToNetwork() {
splash := NewSplashPanel(m.UI)
m.UI.Add(splash)
func (m *connectionPanel) createConnectToNetworkButtom() gtk.IWidget {
var b *gtk.Button

wpa, _ := wpasupplicant.Unixgram("wlan0")
psk, _ := m.pass.GetText()
b = MustButtonText("Connect", func() {
b.SetSensitive(false)
time.Sleep(time.Second * 1)
psk, _ := m.pass.GetText()
wpa, _ := wpasupplicant.Unixgram("wlan0")

if wpa != nil {
wpa.RemoveAllNetworks()
wpa.AddNetwork()
wpa.SetNetwork(0, "ssid", m.SSID)
wpa.SetNetwork(0, "psk", psk)

splash.Label.SetText(fmt.Sprintf("Connecting to %s", wpa))
go wpa.EnableNetwork(0)
time.Sleep(time.Second * 1)
go wpa.SaveConfig()
}

time.Sleep(time.Second * 1)
m.UI.GoHistory()
})

ctx, _ := b.GetStyleContext()
ctx.AddClass("color3")

wpa.RemoveAllNetworks()
wpa.AddNetwork()
wpa.SetNetwork(0, "ssid", m.SSID)
wpa.SetNetwork(0, "psk", psk)
b.SetProperty("width-request", m.Scaled(150))

go wpa.EnableNetwork(0)
time.Sleep(time.Second * 1)
go wpa.SaveConfig()
splash.Label.SetText("Checking status")
time.Sleep(time.Second * 1)
m.UI.GoHistory()
return b
}

type keyButtonHander struct {
Expand Down

0 comments on commit 94b1308

Please sign in to comment.