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.
This commit is contained in:
Stanislav Nikitin 2016-10-12 19:23:10 +05:00
parent 1330699f41
commit a64c842e19
6 changed files with 81 additions and 32 deletions

View File

@ -11,6 +11,7 @@ package clipboardwatcher
import ( import (
// stdlib // stdlib
"errors"
"fmt" "fmt"
"strings" "strings"
@ -24,21 +25,52 @@ type ClipboardWatcher struct {
clipboard *gtk.Clipboard clipboard *gtk.Clipboard
// PRIMARY clipboard. // PRIMARY clipboard.
prim_clipboard *gtk.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() { func (cw *ClipboardWatcher) checkInput() {
text := cw.clipboard.WaitForText() if !cw.just_set {
cw.parseData(text) text := cw.clipboard.WaitForText()
cw.parseData(text)
}
} }
func (cw *ClipboardWatcher) checkPrimaryInput() { func (cw *ClipboardWatcher) checkPrimaryInput() {
text := cw.prim_clipboard.WaitForText() if !cw.just_set {
cw.parseData(text) 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() { func (cw *ClipboardWatcher) Initialize() {
fmt.Println("Initializing clipboard watcher...") fmt.Println("Initializing clipboard watcher...")
cw.just_set = false
cw.clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_CLIPBOARD) cw.clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_CLIPBOARD)
cw.clipboard.Connect("owner-change", cw.checkInput) cw.clipboard.Connect("owner-change", cw.checkInput)

View File

@ -11,15 +11,18 @@ package clipboardwatcher
import( import(
// local // local
"github.com/pztrn/urtrator/cache"
"github.com/pztrn/urtrator/eventer" "github.com/pztrn/urtrator/eventer"
) )
var ( var (
Cache *cache.Cache
Eventer *eventer.Eventer Eventer *eventer.Eventer
) )
func New(e *eventer.Eventer) *ClipboardWatcher { func New(c *cache.Cache, e *eventer.Eventer) *ClipboardWatcher {
Cache = c
Eventer = e Eventer = e
c := ClipboardWatcher{} cw := ClipboardWatcher{}
return &c return &cw
} }

View File

@ -69,7 +69,7 @@ func (ctx *Context) initializeCache() {
} }
func (ctx *Context) InitializeClipboardWatcher() { func (ctx *Context) InitializeClipboardWatcher() {
ctx.Clipboard = clipboardwatcher.New(ctx.Eventer) ctx.Clipboard = clipboardwatcher.New(ctx.Cache, ctx.Eventer)
ctx.Clipboard.Initialize() ctx.Clipboard.Initialize()
} }

View File

@ -202,6 +202,13 @@ func (m *MainWindow) Close() {
ctx.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. // Deleting server from favorites.
func (m *MainWindow) deleteFromFavorites() { func (m *MainWindow) deleteFromFavorites() {
fmt.Println("Removing server from favorites...") fmt.Println("Removing server from favorites...")

View File

@ -560,15 +560,22 @@ func (m *MainWindow) InitializeToolbar() {
fav_delete_button.OnClicked(m.deleteFromFavorites) fav_delete_button.OnClicked(m.deleteFromFavorites)
m.toolbar.Insert(fav_delete_button, 5) 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. // Separator for toolbar's label and buttons.
toolbar_separator_toolitem := gtk.NewToolItem() toolbar_separator_toolitem := gtk.NewToolItem()
toolbar_separator_toolitem.SetExpand(true) toolbar_separator_toolitem.SetExpand(true)
m.toolbar.Insert(toolbar_separator_toolitem, 6) m.toolbar.Insert(toolbar_separator_toolitem, 7)
// Toolbar's label. // Toolbar's label.
m.toolbar_label = gtk.NewLabel("URTrator is ready") m.toolbar_label = gtk.NewLabel("URTrator is ready")
toolbar_label_toolitem := gtk.NewToolItem() toolbar_label_toolitem := gtk.NewToolItem()
toolbar_label_toolitem.Add(m.toolbar_label) toolbar_label_toolitem.Add(m.toolbar_label)
m.toolbar.Insert(toolbar_label_toolitem, 7) m.toolbar.Insert(toolbar_label_toolitem, 8)
} }
// Tray icon initialization. // Tray icon initialization.

View File

@ -58,18 +58,18 @@ func (m *MainWindow) launchAsUsual() error {
if len(server_profile.Name) == 0 { if len(server_profile.Name) == 0 {
var will_continue bool = false 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." 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) messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string)
m.Connect("response", func(resp *glib.CallbackContext) { messagebox.Connect("response", func(resp *glib.CallbackContext) {
if resp.Args(0) == 4294967287 { if resp.Args(0) == 4294967287 {
will_continue = false will_continue = false
} else { } else {
will_continue = true will_continue = true
} }
}) })
m.Response(func() { messagebox.Response(func() {
m.Destroy() messagebox.Destroy()
}) })
m.Run() messagebox.Run()
if !will_continue { if !will_continue {
return errors.New("User declined to connect to offline server") return errors.New("User declined to connect to offline server")
} }
@ -84,11 +84,11 @@ func (m *MainWindow) launchAsUsual() error {
// have profiles defined (see next). // have profiles defined (see next).
if len(profile_name) == 0 { if len(profile_name) == 0 {
mbox_string := "Invalid game profile selected.\n\nPlease, select profile and retry." 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) messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string)
m.Response(func() { messagebox.Response(func() {
m.Destroy() messagebox.Destroy()
}) })
m.Run() messagebox.Run()
return errors.New("User didn't select valid profile.") return errors.New("User didn't select valid profile.")
} }
user_profile = ctx.Cache.Profiles[profile_name].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] user_profile_cached, ok := ctx.Cache.Profiles[server_profile.ProfileToUse]
if !ok { if !ok {
mbox_string := "Invalid game profile specified for favorite server.\n\nPlease, edit your favorite server, select valid profile and retry." 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) messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string)
m.Response(func() { messagebox.Response(func() {
m.Destroy() messagebox.Destroy()
}) })
m.Run() messagebox.Run()
return errors.New("User didn't select valid profile.") return errors.New("User didn't select valid profile.")
} }
user_profile = user_profile_cached.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 == "" { if server_profile.Name == "" {
var will_continue bool = false 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." 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) messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_YES_NO, mbox_string)
m.Connect("response", func(resp *glib.CallbackContext) { messagebox.Connect("response", func(resp *glib.CallbackContext) {
if resp.Args(0) == 4294967287 { if resp.Args(0) == 4294967287 {
will_continue = false will_continue = false
} else { } else {
will_continue = true will_continue = true
} }
}) })
m.Response(func() { messagebox.Response(func() {
m.Destroy() messagebox.Destroy()
}) })
m.Run() messagebox.Run()
if !will_continue { if !will_continue {
return errors.New("User declined to connect to offline server") 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. // Check if server is applicable for selected profile.
if server_profile.Version != user_profile.Version { 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." 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) messagebox := gtk.NewMessageDialog(m.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, mbox_string)
m.Response(func() { messagebox.Response(func() {
m.Destroy() messagebox.Destroy()
}) })
m.Run() messagebox.Run()
return errors.New("User didn't select valid profile, mismatch with server's version.") return errors.New("User didn't select valid profile, mismatch with server's version.")
} }