From c47b9596dd41f29c25c259ea733dc3cd5debc664 Mon Sep 17 00:00:00 2001 From: Steven Honson Date: Sat, 11 May 2019 22:04:08 +1000 Subject: ui: tunnel cloning Allows a tunnel to be cloned verbatim. It's not part of the toolbar or with a shortcut, as it's a rather obscure action. Signed-off-by: Steven Honson [zx2c4: removed from toolbar and shortcut] Signed-off-by: Jason A. Donenfeld --- ui/editdialog.go | 15 +++++++++++---- ui/tunnelspage.go | 24 ++++++++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'ui') diff --git a/ui/editdialog.go b/ui/editdialog.go index ba868893..b4ed8ebe 100644 --- a/ui/editdialog.go +++ b/ui/editdialog.go @@ -27,7 +27,7 @@ type EditDialog struct { blockUntunneledTraficCheckGuard bool } -func runTunnelEditDialog(owner walk.Form, tunnel *service.Tunnel) *conf.Config { +func runTunnelEditDialog(owner walk.Form, tunnel *service.Tunnel, clone bool) *conf.Config { var ( title string name string @@ -35,14 +35,21 @@ func runTunnelEditDialog(owner walk.Form, tunnel *service.Tunnel) *conf.Config { dlg := &EditDialog{} + if tunnel == nil || clone { + title = "Create new tunnel" + } else { + title = "Edit tunnel" + } + if tunnel == nil { // Creating a new tunnel, create a new private key and use the default template - title = "Create new tunnel" pk, _ := conf.NewPrivateKey() dlg.config = conf.Config{Interface: conf.Interface{PrivateKey: *pk}} } else { - title = "Edit tunnel" name = tunnel.Name + if clone { + name += "-copy" + } dlg.config, _ = tunnel.StoredConfig() } @@ -107,7 +114,7 @@ func runTunnelEditDialog(owner walk.Form, tunnel *service.Tunnel) *conf.Config { dlg.syntaxEdit.BlockUntunneledTrafficStateChanged().Attach(dlg.onBlockUntunneledTrafficStateChanged) dlg.syntaxEdit.SetText(dlg.config.ToWgQuick()) - if tunnel != nil { + if tunnel != nil && !clone { dlg.nameEdit.SetFocus() //TODO: This works around a walk issue with scrolling in weird ways . We should fix this in walk instead of here. dlg.Starting().Attach(func() { diff --git a/ui/tunnelspage.go b/ui/tunnelspage.go index 592e8d4f..32c89694 100644 --- a/ui/tunnelspage.go +++ b/ui/tunnelspage.go @@ -134,6 +134,7 @@ func (tp *TunnelsPage) CreateToolbar() { importAction.SetShortcut(walk.Shortcut{walk.ModControl, walk.KeyO}) importAction.SetDefault(true) importAction.Triggered().Attach(tp.onImport) + addMenu.Actions().Add(importAction) addAction := walk.NewAction() addAction.SetText("Add empty tunnel...") addActionIcon, _ := loadSystemIcon("imageres", 2, imageSize.Width) @@ -141,7 +142,6 @@ func (tp *TunnelsPage) CreateToolbar() { addAction.SetImage(addActionImage) addAction.SetShortcut(walk.Shortcut{walk.ModControl, walk.KeyN}) addAction.Triggered().Attach(tp.onAddTunnel) - addMenu.Actions().Add(importAction) addMenu.Actions().Add(addAction) addMenuAction := walk.NewMenuAction(addMenu) addMenuActionIcon, _ := loadSystemIcon("shell32", 149, imageSize.Width) @@ -204,6 +204,10 @@ func (tp *TunnelsPage) CreateToolbar() { editAction.SetShortcut(walk.Shortcut{walk.ModControl, walk.KeyE}) editAction.Triggered().Attach(tp.onEditTunnel) contextMenu.Actions().Add(editAction) + cloneAction := walk.NewAction() + cloneAction.SetText("Clone selected tunnel...") + cloneAction.Triggered().Attach(tp.onCloneTunnel) + contextMenu.Actions().Add(cloneAction) deleteAction2 := walk.NewAction() deleteAction2.SetText("Remove selected tunnel(s)") deleteAction2.SetShortcut(walk.Shortcut{0, walk.KeyDelete}) @@ -228,6 +232,7 @@ func (tp *TunnelsPage) CreateToolbar() { toggleAction.SetEnabled(selected == 1) selectAllAction.SetEnabled(selected < all) editAction.SetEnabled(selected == 1) + cloneAction.SetEnabled(selected == 1) } tp.listView.SelectedIndexesChanged().Attach(setSelectionOrientedOptions) setSelectionOrientedOptions() @@ -418,11 +423,10 @@ func (tp *TunnelsPage) onTunnelsViewItemActivated() { func (tp *TunnelsPage) onEditTunnel() { tunnel := tp.listView.CurrentTunnel() if tunnel == nil { - // Misfired event? return } - if config := runTunnelEditDialog(tp.Form(), tunnel); config != nil { + if config := runTunnelEditDialog(tp.Form(), tunnel, false); config != nil { go func() { priorState, err := tunnel.State() tunnel.Delete() @@ -435,8 +439,20 @@ func (tp *TunnelsPage) onEditTunnel() { } } +func (tp *TunnelsPage) onCloneTunnel() { + tunnel := tp.listView.CurrentTunnel() + if tunnel == nil { + return + } + + if config := runTunnelEditDialog(tp.Form(), tunnel, true); config != nil { + // Save new + tp.addTunnel(config) + } +} + func (tp *TunnelsPage) onAddTunnel() { - if config := runTunnelEditDialog(tp.Form(), nil); config != nil { + if config := runTunnelEditDialog(tp.Form(), nil, false); config != nil { // Save new tp.addTunnel(config) } -- cgit v1.2.3-59-g8ed1b