This repository has been archived on 2022-06-29. You can view files and clone it, but cannot push or open issues or pull requests.
urtrator/clipboardwatcher/clipboardwatcher_object.go

131 lines
4.0 KiB
Go
Raw Normal View History

// URTrator - Urban Terror server browser and game launcher, written in
2016-10-11 13:14:08 +05:00
// Go.
//
// Copyright (c) 2016-2018, Stanslav N. a.k.a pztrn (or p0z1tr0n) and
// URTrator contributors.
2016-10-11 13:14:08 +05:00
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2016-10-11 13:14:08 +05:00
package clipboardwatcher
import (
2018-11-10 20:58:15 +05:00
// stdlib
"errors"
"fmt"
"strings"
2016-10-11 13:14:08 +05:00
2018-11-10 20:58:15 +05:00
// other
"github.com/mattn/go-gtk/gdk"
"github.com/mattn/go-gtk/gtk"
2016-10-11 13:14:08 +05:00
)
type ClipboardWatcher struct {
2018-11-10 20:58:15 +05:00
// Clipboard.
clipboard *gtk.Clipboard
// PRIMARY clipboard.
prim_clipboard *gtk.Clipboard
2018-11-10 20:58:15 +05:00
// Flags.
// We have just copy connect string to clipboard.
// Used to ignore clipboard data in check*Input()
just_set bool
2016-10-11 13:14:08 +05:00
}
func (cw *ClipboardWatcher) checkInput() {
2018-11-10 20:58:15 +05:00
if !cw.just_set {
text := cw.clipboard.WaitForText()
cw.parseData(text)
} else {
cw.just_set = false
}
2016-10-11 13:14:08 +05:00
}
func (cw *ClipboardWatcher) checkPrimaryInput() {
2018-11-10 20:58:15 +05:00
if !cw.just_set {
text := cw.prim_clipboard.WaitForText()
cw.parseData(text)
} else {
cw.just_set = false
}
}
func (cw *ClipboardWatcher) CopyServerData(server_address string) error {
2018-11-10 20:58:15 +05:00
server, ok := Cache.Servers[server_address]
if !ok {
// ToDo: show message box?
return errors.New("Server wasn't selected")
}
2018-11-10 20:58:15 +05:00
// 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)
2018-11-10 20:58:15 +05:00
return nil
2016-10-11 13:14:08 +05:00
}
func (cw *ClipboardWatcher) Initialize() {
2018-11-10 20:58:15 +05:00
fmt.Println("Initializing clipboard watcher...")
2016-10-11 13:14:08 +05:00
2018-11-10 20:58:15 +05:00
cw.just_set = false
2018-11-10 20:58:15 +05:00
cw.clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_CLIPBOARD)
cw.clipboard.Connect("owner-change", cw.checkInput)
2016-10-11 13:14:08 +05:00
2018-11-10 20:58:15 +05:00
cw.prim_clipboard = gtk.NewClipboardGetForDisplay(gdk.DisplayGetDefault(), gdk.SELECTION_PRIMARY)
cw.prim_clipboard.Connect("owner-change", cw.checkPrimaryInput)
2016-10-11 13:14:08 +05:00
}
func (cw *ClipboardWatcher) parseData(data string) {
2018-11-10 20:58:15 +05:00
// We should check only first string.
data = strings.Split(data, "\n")[0]
// Checking if we have connection string here.
if strings.Contains(data, "ct ") {
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, "ct ")
if len(srv_splitted) > 1 {
server_raw := strings.Split(srv_splitted[1], " ")[0]
// Get rid of spaces.
server = strings.TrimSpace(server_raw)
}
}
if len(conn_string) > 1 && strings.Contains(data, "password") {
pw_string := strings.Split(data, ";")[1]
pw_splitted := strings.Split(pw_string, "password ")
if len(pw_splitted) > 1 {
password_raw := strings.Split(pw_splitted[1], " ")[0]
// Get rid of spaces.
password = strings.TrimSpace(password_raw)
}
}
Eventer.LaunchEvent("setQuickConnectDetails", map[string]string{"server": server, "password": password})
}
2016-10-11 13:14:08 +05:00
}