erinn
/
ui
forked from cwtch.im/ui
1
0
Fork 0
ui/main.go

185 lines
5.3 KiB
Go
Raw Normal View History

2018-10-23 18:52:13 +00:00
package main
import (
2018-11-22 00:01:17 +00:00
libapp "cwtch.im/cwtch/app"
"cwtch.im/cwtch/model"
2018-11-28 22:14:02 +00:00
"cwtch.im/ui/go/characters"
"cwtch.im/ui/go/cwtchthings"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/gothings"
"cwtch.im/ui/go/the"
"fmt"
2018-11-22 00:01:17 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/application"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
2018-11-28 22:14:02 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"github.com/therecipe/qt/core"
2018-10-25 00:13:03 +00:00
"github.com/therecipe/qt/quick"
"github.com/therecipe/qt/quickcontrols2"
"github.com/therecipe/qt/widgets"
2018-11-22 00:01:17 +00:00
"log"
2018-10-25 00:13:03 +00:00
"os"
"os/user"
"path"
2018-10-28 02:49:14 +00:00
)
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
func init() {
// make go-defined types available in qml
gothings.GrandCentralDispatcher_QmlRegisterType2("CustomQmlTypes", 1, 0, "GrandCentralDispatcher")
2018-10-23 18:52:13 +00:00
}
2018-11-22 00:01:17 +00:00
func main() {
// our globals
gcd := gothings.NewGrandCentralDispatcher(nil)
gcd.UIState = gothings.NewUIState(gcd)
the.AcknowledgementIDs = make(map[uint32]uint)
gcd.OutgoingMessages = make(chan gobjects.Letter, 1000)
//TODO: put theme stuff somewhere better
gcd.SetThemeScale(1.0)
// this is to load local qml files quickly when developing
var qmlSource *core.QUrl
if len(os.Args) == 2 && os.Args[1] == "-local" {
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
} else {
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
}
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// window construction boilerplate
view := initializeQtView()
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// variables we want to access from inside qml
view.RootContext().SetContextProperty("gcd", gcd)
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// this actually loads the qml
view.SetSource(qmlSource)
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// these are long-lived pollers/listeners for incoming messages and status changes
loadCwtchData(gcd)
go characters.PostmanPat(gcd.OutgoingMessages)
go characters.TorStatusPoller(gcd.TorStatus)
go characters.PresencePoller(gcd.UIState.GetContact, gcd.UIState.AddContact, gcd.UIState.UpdateContact)
go characters.GroupPoller(gcd.UIState.GetContact, gcd.UIState.UpdateContact)
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// here we go!
view.Show()
widgets.QApplication_Exec()
}
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
// window construction boilerplate
func initializeQtView() *quick.QQuickView {
2018-10-23 18:52:13 +00:00
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
widgets.NewQApplication(len(os.Args), os.Args)
quickcontrols2.QQuickStyle_SetStyle("Universe")
view := quick.NewQQuickView(nil)
view.SetResizeMode(quick.QQuickView__SizeRootObjectToView)
2018-10-29 18:00:21 +00:00
view.SetMinimumHeight(280)
view.SetMinimumWidth(300)
view.SetTitle("cwtch")
2018-10-25 00:13:03 +00:00
2018-11-22 00:01:17 +00:00
return view
2018-10-25 00:13:03 +00:00
}
2018-11-22 00:01:17 +00:00
// this is mostly going to get factored out when we add profile support
// for now, it loads a single peer and fills the ui with its data
func loadCwtchData(gcd *gothings.GrandCentralDispatcher) {
2018-10-30 19:48:37 +00:00
var err error
2018-10-29 18:00:21 +00:00
if os.Getenv("CWTCH_FOLDER") != "" {
2018-11-22 00:01:17 +00:00
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
2018-10-23 18:52:13 +00:00
} else {
usr, err := user.Current()
if err != nil {
fmt.Printf("\nerror: could not load current user: %v\n", err)
os.Exit(1)
}
2018-11-22 00:01:17 +00:00
the.CwtchDir = path.Join(usr.HomeDir, ".cwtch")
2018-10-23 18:52:13 +00:00
}
2018-10-30 19:48:37 +00:00
/*_, err := app2.NewApp(dirname, "/data/data/org.qtproject.example.go/lib/libtor.so")
if err != nil {
log.Printf("ERROR CREATING CWTCH APP: %v", err)
}
time.Sleep(time.Second * 10)
*/
2018-11-22 00:01:17 +00:00
os.MkdirAll(the.CwtchDir, 0700)
2018-11-26 21:57:28 +00:00
mn, err := connectivity.StartTor(the.CwtchDir, "")
2018-11-22 00:01:17 +00:00
if err != nil {
2018-11-26 21:57:28 +00:00
log.Printf("[!!!] Could not start Tor: %v", err)
2018-11-22 00:01:17 +00:00
}
2018-11-26 21:57:28 +00:00
the.CwtchApp = libapp.NewApp(mn, the.CwtchDir)
2018-11-22 00:01:17 +00:00
err = the.CwtchApp.LoadProfiles("be gay do crime")
2018-10-23 18:52:13 +00:00
if err != nil {
2018-11-22 00:01:17 +00:00
//TODO no more fatalfs
log.Fatalf("couldn't load profiles: %v", err)
}
2018-10-23 18:52:13 +00:00
2018-11-22 00:01:17 +00:00
if len(the.CwtchApp.ListPeers()) == 0 {
log.Printf("couldn't load your config file. attempting to create one now")
the.Peer, err = the.CwtchApp.CreatePeer("alice", "be gay do crime")
2018-10-23 18:52:13 +00:00
if err != nil {
2018-11-22 00:01:17 +00:00
log.Fatalf("couldn't create one. is your cwtch folder writable?")
2018-10-23 18:52:13 +00:00
}
2018-11-22 00:01:17 +00:00
the.CwtchApp.SaveProfile(the.Peer)
} else {
the.Peer = the.CwtchApp.PrimaryIdentity()
2018-10-23 18:52:13 +00:00
}
2018-11-22 00:01:17 +00:00
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion))
2018-10-23 18:52:13 +00:00
2018-10-28 02:49:14 +00:00
aif := application.ApplicationInstanceFactory{}
aif.Init()
app := new(application.RicochetApplication)
aif.AddHandler("im.ricochet.chat", func(rai *application.ApplicationInstance) func() channels.Handler {
2018-11-22 00:01:17 +00:00
ccl := new(cwtchthings.ChatChannelListener)
ccl.Init(rai, app, gcd.UIState.AddMessage, gcd.Acknowledged)
2018-10-28 02:49:14 +00:00
return func() channels.Handler {
chat := new(channels.ChatChannel)
chat.Handler = ccl
return chat
}
})
2018-11-22 00:01:17 +00:00
the.Peer.SetApplicationInstanceFactory(aif)
the.CwtchApp.LaunchPeers()
2018-10-28 02:49:14 +00:00
2018-11-22 00:01:17 +00:00
contacts := the.Peer.GetContacts()
2018-10-23 18:52:13 +00:00
for i := range contacts {
2018-11-22 00:01:17 +00:00
contact, _ := the.Peer.GetProfile().GetContact(contacts[i])
displayName, _ := contact.GetAttribute("name")
gcd.UIState.AddContact(&gobjects.Contact{
contacts[i],
displayName,
cwutil.RandomProfileImage(contacts[i]),
"",
0,
0,
contact.Trusted,
})
2018-10-28 02:49:14 +00:00
}
2018-11-22 00:01:17 +00:00
groups := the.Peer.GetGroups()
2018-10-28 02:49:14 +00:00
for i := range groups {
2018-11-22 00:01:17 +00:00
log.Printf("adding saved group %v", groups[i])
group := the.Peer.GetGroup(groups[i])
2018-10-28 02:49:14 +00:00
group.NewMessage = make(chan model.Message)
2018-11-22 00:01:17 +00:00
go characters.CwtchListener(gcd.UIState.AddMessage, groups[i], group.NewMessage)
the.Peer.JoinServer(group.GroupServer)
2018-11-28 21:29:32 +00:00
nick, exists := group.GetAttribute("nick")
if !exists {
nick = group.GroupID[:12]
}
2018-11-22 00:01:17 +00:00
gcd.UIState.AddContact(&gobjects.Contact{
group.GroupID,
2018-11-28 21:29:32 +00:00
nick,
2018-11-22 00:01:17 +00:00
cwutil.RandomGroupImage(group.GroupID),
group.GroupServer,
0,
0,
group.Accepted,
})
2018-10-28 02:49:14 +00:00
}
2018-11-28 22:14:02 +00:00
}