First variant of clipboard watching.

This commit is contained in:
Stanislav Nikitin 2016-10-11 13:14:08 +05:00
parent 9be55c982e
commit 68bd842c1f
5 changed files with 130 additions and 0 deletions

View File

@ -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})
}
}

View File

@ -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
}

View File

@ -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()

View File

@ -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"], "<markup>") {

View File

@ -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)
}