From a64c842e19914184366e5b9bc6a6aaa6f1916b76 Mon Sep 17 00:00:00 2001 From: pztrn Date: Wed, 12 Oct 2016 19:23:10 +0500 Subject: [PATCH] Messageboxes fixes and copy server's creds to clipboard. Fixed all messageboxes that can use struct object as self name (e.g. m for messageboxes in mainwindow). Should help to avoid some errors. Added possibility to copy server's credentials into clipboard for ease of sharing. --- clipboardwatcher/clipboardwatcher_object.go | 40 +++++++++++++++++-- clipboardwatcher/exported.go | 9 +++-- context/context_object.go | 2 +- ui/mainwindow.go | 7 ++++ ui/mainwindow_init.go | 11 +++++- ui/mainwindow_launch.go | 44 ++++++++++----------- 6 files changed, 81 insertions(+), 32 deletions(-) diff --git a/clipboardwatcher/clipboardwatcher_object.go b/clipboardwatcher/clipboardwatcher_object.go index 3954172..2507e75 100644 --- a/clipboardwatcher/clipboardwatcher_object.go +++ b/clipboardwatcher/clipboardwatcher_object.go @@ -11,6 +11,7 @@ package clipboardwatcher import ( // stdlib + "errors" "fmt" "strings" @@ -24,21 +25,52 @@ type ClipboardWatcher struct { clipboard *gtk.Clipboard // PRIMARY clipboard. prim_clipboard *gtk.Clipboard + + // Flags. + // We have just copy connect string to clipboard. + // Used to ignore clipboard data in check*Input() + just_set bool } func (cw *ClipboardWatcher) checkInput() { - text := cw.clipboard.WaitForText() - cw.parseData(text) + if !cw.just_set { + text := cw.clipboard.WaitForText() + cw.parseData(text) + } } func (cw *ClipboardWatcher) checkPrimaryInput() { - text := cw.prim_clipboard.WaitForText() - cw.parseData(text) + if !cw.just_set { + text := cw.prim_clipboard.WaitForText() + cw.parseData(text) + } +} + +func (cw *ClipboardWatcher) CopyServerData(server_address string) error { + server, ok := Cache.Servers[server_address] + if !ok { + // ToDo: show message box? + return errors.New("Server wasn't selected") + } + + // Composing connection string. + var connect_string string = "" + connect_string += "/connect " + server.Server.Ip + ":" + server.Server.Port + if len(server.Server.Password) >= 1 { + connect_string += ";password " + server.Server.Password + } + fmt.Println("Connect string: ", connect_string) + cw.just_set = true + cw.clipboard.SetText(connect_string) + + return nil } func (cw *ClipboardWatcher) Initialize() { fmt.Println("Initializing clipboard watcher...") + cw.just_set = false + cw.clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_CLIPBOARD) cw.clipboard.Connect("owner-change", cw.checkInput) diff --git a/clipboardwatcher/exported.go b/clipboardwatcher/exported.go index 5169057..98afd46 100644 --- a/clipboardwatcher/exported.go +++ b/clipboardwatcher/exported.go @@ -11,15 +11,18 @@ package clipboardwatcher import( // local + "github.com/pztrn/urtrator/cache" "github.com/pztrn/urtrator/eventer" ) var ( + Cache *cache.Cache Eventer *eventer.Eventer ) -func New(e *eventer.Eventer) *ClipboardWatcher { +func New(c *cache.Cache, e *eventer.Eventer) *ClipboardWatcher { + Cache = c Eventer = e - c := ClipboardWatcher{} - return &c + cw := ClipboardWatcher{} + return &cw } diff --git a/context/context_object.go b/context/context_object.go index 90d2b53..22789be 100644 --- a/context/context_object.go +++ b/context/context_object.go @@ -69,7 +69,7 @@ func (ctx *Context) initializeCache() { } func (ctx *Context) InitializeClipboardWatcher() { - ctx.Clipboard = clipboardwatcher.New(ctx.Eventer) + ctx.Clipboard = clipboardwatcher.New(ctx.Cache, ctx.Eventer) ctx.Clipboard.Initialize() } diff --git a/ui/mainwindow.go b/ui/mainwindow.go index 216bf8c..7d8e057 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -202,6 +202,13 @@ func (m *MainWindow) Close() { ctx.Close() } +func (m *MainWindow) copyServerCredentialsToClipboard() { + fmt.Println("Copying server's credentials to clipboard...") + current_tab := m.tab_widget.GetTabLabelText(m.tab_widget.GetNthPage(m.tab_widget.GetCurrentPage())) + server_address := m.getIpFromServersList(current_tab) + ctx.Clipboard.CopyServerData(server_address) +} + // Deleting server from favorites. func (m *MainWindow) deleteFromFavorites() { fmt.Println("Removing server from favorites...") diff --git a/ui/mainwindow_init.go b/ui/mainwindow_init.go index a863763..955bbd2 100644 --- a/ui/mainwindow_init.go +++ b/ui/mainwindow_init.go @@ -560,15 +560,22 @@ func (m *MainWindow) InitializeToolbar() { fav_delete_button.OnClicked(m.deleteFromFavorites) m.toolbar.Insert(fav_delete_button, 5) + // Copy server address button. + copy_srv_addr_button := gtk.NewToolButtonFromStock(gtk.STOCK_COPY) + copy_srv_addr_button.SetLabel("Copy server's creds") + copy_srv_addr_button.SetTooltipText("Copy server's credentials to clipboard for sharing") + copy_srv_addr_button.OnClicked(m.copyServerCredentialsToClipboard) + m.toolbar.Insert(copy_srv_addr_button, 6) + // Separator for toolbar's label and buttons. toolbar_separator_toolitem := gtk.NewToolItem() toolbar_separator_toolitem.SetExpand(true) - m.toolbar.Insert(toolbar_separator_toolitem, 6) + m.toolbar.Insert(toolbar_separator_toolitem, 7) // Toolbar's label. m.toolbar_label = gtk.NewLabel("URTrator is ready") toolbar_label_toolitem := gtk.NewToolItem() toolbar_label_toolitem.Add(m.toolbar_label) - m.toolbar.Insert(toolbar_label_toolitem, 7) + m.toolbar.Insert(toolbar_label_toolitem, 8) } // Tray icon initialization. diff --git a/ui/mainwindow_launch.go b/ui/mainwindow_launch.go index b41c014..ea081c8 100644 --- a/ui/mainwindow_launch.go +++ b/ui/mainwindow_launch.go @@ -58,18 +58,18 @@ func (m *MainWindow) launchAsUsual() error { if len(server_profile.Name) == 0 { var will_continue bool = false mbox_string := "Selected server is offline.\n\nWould you still want to launch Urban Terror?\nIt will just launch a game, without connecting to\nany server." - m := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string) - m.Connect("response", func(resp *glib.CallbackContext) { + messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string) + messagebox.Connect("response", func(resp *glib.CallbackContext) { if resp.Args(0) == 4294967287 { will_continue = false } else { will_continue = true } }) - m.Response(func() { - m.Destroy() + messagebox.Response(func() { + messagebox.Destroy() }) - m.Run() + messagebox.Run() if !will_continue { return errors.New("User declined to connect to offline server") } @@ -84,11 +84,11 @@ func (m *MainWindow) launchAsUsual() error { // have profiles defined (see next). if len(profile_name) == 0 { mbox_string := "Invalid game profile selected.\n\nPlease, select profile and retry." - m := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) - m.Response(func() { - m.Destroy() + messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) + messagebox.Response(func() { + messagebox.Destroy() }) - m.Run() + messagebox.Run() return errors.New("User didn't select valid profile.") } user_profile = ctx.Cache.Profiles[profile_name].Profile @@ -98,11 +98,11 @@ func (m *MainWindow) launchAsUsual() error { user_profile_cached, ok := ctx.Cache.Profiles[server_profile.ProfileToUse] if !ok { mbox_string := "Invalid game profile specified for favorite server.\n\nPlease, edit your favorite server, select valid profile and retry." - m := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) - m.Response(func() { - m.Destroy() + messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) + messagebox.Response(func() { + messagebox.Destroy() }) - m.Run() + messagebox.Run() return errors.New("User didn't select valid profile.") } user_profile = user_profile_cached.Profile @@ -155,18 +155,18 @@ func (m *MainWindow) launchActually(server_profile *datamodels.Server, user_prof if server_profile.Name == "" { var will_continue bool = false mbox_string := "Selected server is offline.\n\nWould you still want to launch Urban Terror?\nIt will just launch a game, without connecting to\nany server." - m := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string) - m.Connect("response", func(resp *glib.CallbackContext) { + messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string) + messagebox.Connect("response", func(resp *glib.CallbackContext) { if resp.Args(0) == 4294967287 { will_continue = false } else { will_continue = true } }) - m.Response(func() { - m.Destroy() + messagebox.Response(func() { + messagebox.Destroy() }) - m.Run() + messagebox.Run() if !will_continue { return errors.New("User declined to connect to offline server") } @@ -175,11 +175,11 @@ func (m *MainWindow) launchActually(server_profile *datamodels.Server, user_prof // Check if server is applicable for selected profile. if server_profile.Version != user_profile.Version { mbox_string := "Invalid game profile selected.\n\nSelected profile have different game version than server.\nPlease, select valid profile and retry." - m := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) - m.Response(func() { - m.Destroy() + messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string) + messagebox.Response(func() { + messagebox.Destroy() }) - m.Run() + messagebox.Run() return errors.New("User didn't select valid profile, mismatch with server's version.") }