forked from jshiffer/go-xmpp
add gui example.
This commit is contained in:
parent
fc3904b3a3
commit
4ddb93ef9d
104
example/example-gui.go
Normal file
104
example/example-gui.go
Normal file
@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattn/go-xmpp"
|
||||
"github.com/mattn/go-gtk/gtk"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gtk.Init(&os.Args)
|
||||
|
||||
window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
|
||||
window.SetTitle("GoTalk")
|
||||
window.Connect("destroy", func() {
|
||||
gtk.MainQuit()
|
||||
})
|
||||
vbox := gtk.VBox(false, 1)
|
||||
scrolledwin := gtk.ScrolledWindow(nil, nil)
|
||||
textview := gtk.TextView()
|
||||
textview.SetEditable(false)
|
||||
textview.SetCursorVisible(false)
|
||||
scrolledwin.Add(textview)
|
||||
vbox.Add(scrolledwin)
|
||||
|
||||
buffer := textview.GetBuffer()
|
||||
|
||||
entry := gtk.Entry()
|
||||
vbox.PackEnd(entry, false, false, 0)
|
||||
|
||||
window.Add(vbox)
|
||||
window.SetSizeRequest(300, 400)
|
||||
window.ShowAll()
|
||||
|
||||
dialog := gtk.Dialog()
|
||||
dialog.SetTitle(window.GetTitle())
|
||||
sgroup := gtk.SizeGroup(gtk.GTK_SIZE_GROUP_HORIZONTAL)
|
||||
|
||||
hbox := gtk.HBox(false, 1)
|
||||
dialog.GetVBox().Add(hbox)
|
||||
label := gtk.Label("username:")
|
||||
sgroup.Add(label)
|
||||
hbox.Add(label)
|
||||
username := gtk.Entry()
|
||||
hbox.Add(username)
|
||||
|
||||
hbox = gtk.HBox(false, 1)
|
||||
dialog.GetVBox().Add(hbox)
|
||||
label = gtk.Label("password:")
|
||||
sgroup.Add(label)
|
||||
hbox.Add(label)
|
||||
password := gtk.Entry()
|
||||
password.SetVisibility(false)
|
||||
hbox.Add(password)
|
||||
|
||||
dialog.AddButton(gtk.GTK_STOCK_OK, int(gtk.GTK_RESPONSE_OK))
|
||||
dialog.AddButton(gtk.GTK_STOCK_CANCEL, int(gtk.GTK_RESPONSE_CANCEL))
|
||||
dialog.SetDefaultResponse(int(gtk.GTK_RESPONSE_OK))
|
||||
dialog.SetTransientFor(window)
|
||||
dialog.ShowAll()
|
||||
res := dialog.Run()
|
||||
username_ := username.GetText()
|
||||
password_ := password.GetText()
|
||||
dialog.Destroy()
|
||||
if res != int(gtk.GTK_RESPONSE_OK) {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
talk, err := xmpp.NewClient("talk.google.com:443", username_, password_)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
entry.Connect("activate", func() {
|
||||
text := entry.GetText()
|
||||
tokens := strings.SplitN(text, " ", 2)
|
||||
if len(tokens) == 2 {
|
||||
func() {
|
||||
defer recover()
|
||||
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
|
||||
entry.SetText("")
|
||||
}()
|
||||
}
|
||||
})
|
||||
|
||||
go func() {
|
||||
for {
|
||||
func() {
|
||||
defer recover()
|
||||
chat, err := talk.Recv()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var iter gtk.GtkTextIter
|
||||
buffer.GetStartIter(&iter)
|
||||
buffer.Insert(&iter, chat.Remote+": "+chat.Text+"\n")
|
||||
}()
|
||||
}
|
||||
}()
|
||||
|
||||
gtk.Main()
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"flag"
|
||||
"github.com/kless/go-readin/readin"
|
||||
"github.com/mattn/go-xmpp"
|
||||
"github.com/mattn/go-iconv/iconv"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
@ -14,6 +15,26 @@ var server = flag.String("server", "talk.google.com:443", "server")
|
||||
var username = flag.String("username", "", "username")
|
||||
var password = flag.String("password", "", "password")
|
||||
|
||||
func fromUTF8(s string) string {
|
||||
ic, err := iconv.Open("char", "UTF-8")
|
||||
if err != nil {
|
||||
return s
|
||||
}
|
||||
defer ic.Close()
|
||||
ret, _ := ic.Conv(s)
|
||||
return ret
|
||||
}
|
||||
|
||||
func toUTF8(s string) string {
|
||||
ic, err := iconv.Open("UTF-8", "char")
|
||||
if err != nil {
|
||||
return s
|
||||
}
|
||||
defer ic.Close()
|
||||
ret, _ := ic.Conv(s)
|
||||
return ret
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "usage: example [options]\n")
|
||||
@ -36,7 +57,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(chat.Remote, chat.Text)
|
||||
fmt.Println(chat.Remote, fromUTF8(chat.Text))
|
||||
}
|
||||
}()
|
||||
for {
|
||||
@ -48,7 +69,7 @@ func main() {
|
||||
|
||||
tokens := strings.SplitN(line, " ", 2)
|
||||
if len(tokens) == 2 {
|
||||
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
|
||||
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: toUTF8(tokens[1])})
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user