From 68bd842c1f862e15bb85ef8d6cd26fb7afe8b169 Mon Sep 17 00:00:00 2001 From: pztrn Date: Tue, 11 Oct 2016 13:14:08 +0500 Subject: [PATCH] First variant of clipboard watching. --- clipboardwatcher/clipboardwatcher_object.go | 89 +++++++++++++++++++++ clipboardwatcher/exported.go | 25 ++++++ context/context_object.go | 8 ++ ui/mainwindow.go | 6 ++ ui/mainwindow_init.go | 2 + 5 files changed, 130 insertions(+) create mode 100644 clipboardwatcher/clipboardwatcher_object.go create mode 100644 clipboardwatcher/exported.go diff --git a/clipboardwatcher/clipboardwatcher_object.go b/clipboardwatcher/clipboardwatcher_object.go new file mode 100644 index 0000000..1a150b1 --- /dev/null +++ b/clipboardwatcher/clipboardwatcher_object.go @@ -0,0 +1,89 @@ +// URTator - Urban Terror server browser and game launcher, written in +// Go. +// +// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n) +// All rights reserved. +// +// Licensed under Terms and Conditions of GNU General Public License +// version 3 or any higher. +// ToDo: put full text of license here. +package clipboardwatcher + +import ( + // stdlib + "fmt" + "strings" + + // other + "github.com/mattn/go-gtk/gdk" + "github.com/mattn/go-gtk/gtk" +) + +type ClipboardWatcher struct { + // Clipboard. + clipboard *gtk.Clipboard + // PRIMARY clipboard. + prim_clipboard *gtk.Clipboard +} + +func (cw *ClipboardWatcher) checkInput() { + text := cw.clipboard.WaitForText() + cw.parseData(text) +} + +func (cw *ClipboardWatcher) checkPrimaryInput() { + text := cw.prim_clipboard.WaitForText() + cw.parseData(text) +} + +func (cw *ClipboardWatcher) Initialize() { + fmt.Println("Initializing clipboard watcher...") + + cw.clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_CLIPBOARD) + cw.clipboard.Connect("owner-change", cw.checkInput) + + cw.prim_clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_PRIMARY) + cw.prim_clipboard.Connect("owner-change", cw.checkPrimaryInput) +} + +func (cw *ClipboardWatcher) parseData(data string) { + fmt.Println(data) + // Checking if we have connection string here. + if strings.Contains(data, "connect") && strings.Contains(data, "password") { + fmt.Println("Connection string detected!") + var server string = "" + var password string = "" + conn_string := strings.Split(data, ";") + if len(conn_string) > 0 { + srv_string := strings.Split(data, ";")[0] + srv_splitted := strings.Split(srv_string, "connect ") + if len(srv_splitted) > 1 { + server_raw := srv_splitted[1] + // Get rid of spaces. + server_raw_splitted := strings.Split(server_raw, " ") + for i := range server_raw_splitted { + if server_raw_splitted[i] == "" { + continue + } + server = server_raw_splitted[i] + } + } + } + if len(conn_string) > 1 { + pw_string := strings.Split(data, ";")[1] + pw_splitted := strings.Split(pw_string, "password ") + if len(pw_splitted) > 1 { + password_raw := pw_splitted[1] + // Get rid of spaces. + password_raw_splitted := strings.Split(password_raw, " ") + for i := range password_raw_splitted { + if password_raw_splitted[i] == "" { + continue + } + password = password_raw_splitted[i] + } + } + } + Eventer.LaunchEvent("setQuickConnectDetails", map[string]string{"server": server, "password": password}) + } +} diff --git a/clipboardwatcher/exported.go b/clipboardwatcher/exported.go new file mode 100644 index 0000000..5169057 --- /dev/null +++ b/clipboardwatcher/exported.go @@ -0,0 +1,25 @@ +// URTator - Urban Terror server browser and game launcher, written in +// Go. +// +// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n) +// All rights reserved. +// +// Licensed under Terms and Conditions of GNU General Public License +// version 3 or any higher. +// ToDo: put full text of license here. +package clipboardwatcher + +import( + // local + "github.com/pztrn/urtrator/eventer" +) + +var ( + Eventer *eventer.Eventer +) + +func New(e *eventer.Eventer) *ClipboardWatcher { + Eventer = e + c := ClipboardWatcher{} + return &c +} diff --git a/context/context_object.go b/context/context_object.go index c3930ad..90d2b53 100644 --- a/context/context_object.go +++ b/context/context_object.go @@ -16,6 +16,7 @@ import ( // local "github.com/pztrn/urtrator/cache" + "github.com/pztrn/urtrator/clipboardwatcher" "github.com/pztrn/urtrator/colorizer" "github.com/pztrn/urtrator/configuration" "github.com/pztrn/urtrator/database" @@ -30,6 +31,8 @@ import ( type Context struct { // Caching. Cache *cache.Cache + // Clipboard watcher. + Clipboard *clipboardwatcher.ClipboardWatcher // Colors parser and prettifier. Colorizer *colorizer.Colorizer // Configuration. @@ -65,6 +68,11 @@ func (ctx *Context) initializeCache() { ctx.Cache.Initialize() } +func (ctx *Context) InitializeClipboardWatcher() { + ctx.Clipboard = clipboardwatcher.New(ctx.Eventer) + ctx.Clipboard.Initialize() +} + func (ctx *Context) initializeColorizer() { ctx.Colorizer = colorizer.New() ctx.Colorizer.Initialize() diff --git a/ui/mainwindow.go b/ui/mainwindow.go index ae90d65..216bf8c 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -437,6 +437,12 @@ func (m *MainWindow) serversUpdateCompleted(data map[string]string) { ctx.Eventer.LaunchEvent("setToolbarLabelText", map[string]string{"text": "Servers updated."}) } +func (m *MainWindow) setQuickConnectDetails(data map[string]string) { + fmt.Println("Setting quick connect data...") + m.qc_server_address.SetText(data["server"]) + m.qc_password.SetText(data["password"]) +} + func (m *MainWindow) setToolbarLabelText(data map[string]string) { fmt.Println("Setting toolbar's label text...") if strings.Contains(data["text"], "") { diff --git a/ui/mainwindow_init.go b/ui/mainwindow_init.go index 1271126..a863763 100644 --- a/ui/mainwindow_init.go +++ b/ui/mainwindow_init.go @@ -21,6 +21,7 @@ func (m *MainWindow) Initialize() { gtk.Init(nil) m.initializeStorages() + ctx.InitializeClipboardWatcher() m.window = gtk.NewWindow(gtk.WINDOW_TOPLEVEL) m.window.SetTitle("URTrator") @@ -173,6 +174,7 @@ func (m *MainWindow) initializeEvents() { ctx.Eventer.AddEventHandler("loadFavoriteServers", m.loadFavoriteServers) ctx.Eventer.AddEventHandler("loadProfilesIntoMainWindow", m.loadProfiles) ctx.Eventer.AddEventHandler("serversUpdateCompleted", m.serversUpdateCompleted) + ctx.Eventer.AddEventHandler("setQuickConnectDetails", m.setQuickConnectDetails) ctx.Eventer.AddEventHandler("setToolbarLabelText", m.setToolbarLabelText) }