ui/main.go

184 lines
5.6 KiB
Go

package main
import (
libapp "cwtch.im/cwtch/app"
"cwtch.im/cwtch/model"
"cwtch.im/ui/go/characters"
"cwtch.im/ui/go/cwutil"
"cwtch.im/ui/go/gobjects"
"cwtch.im/ui/go/gothings"
"cwtch.im/ui/go/the"
"fmt"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/network"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quick"
"github.com/therecipe/qt/quickcontrols2"
"github.com/therecipe/qt/widgets"
"os"
"os/user"
"path"
)
func init() {
// make go-defined types available in qml
gothings.GrandCentralDispatcher_QmlRegisterType2("CustomQmlTypes", 1, 0, "GrandCentralDispatcher")
}
func main() {
log.SetLevel(log.LevelDebug)
// our globals
gcd := gothings.NewGrandCentralDispatcher(nil)
gcd.UIState = gothings.NewUIState(gcd)
the.AcknowledgementIDs = make(map[string]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)
}
// window construction boilerplate
view := initializeQtView()
// variables we want to access from inside qml
view.RootContext().SetContextProperty("gcd", gcd)
// this actually loads the qml
view.SetSource(qmlSource)
// these are long-lived pollers/listeners for incoming messages and status changes
loadCwtchData(gcd)
go characters.IncomingListener(gcd.UIState.AddMessage)
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)
// prevent qt from initiating network connections (possible deanon attempts!)
factory := qml.NewQQmlNetworkAccessManagerFactory()
factory.ConnectCreate(func(parent *core.QObject) *network.QNetworkAccessManager {
nam := network.NewQNetworkAccessManager(parent)
nam.SetNetworkAccessible(network.QNetworkAccessManager__NotAccessible)
proxy := network.NewQNetworkProxy()
proxy.SetHostName("0.0.0.0")
nam.SetProxy(proxy)
//nam.ConnectCreateRequest(func(op network.QNetworkAccessManager__Operation, originalReq *network.QNetworkRequest, outgoingData *core.QIODevice) *network.QNetworkReply {
// log.Errorf("network access request detected - possible remote content insertion bug!!!")
// return nil
//})
return nam
})
view.Engine().SetNetworkAccessManagerFactory(factory)
// here we go!
view.Show()
widgets.QApplication_Exec()
}
// window construction boilerplate
func initializeQtView() *quick.QQuickView {
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)
//view.SetMinimumHeight(800) // these end up getting overridden by main.qml
//view.SetMinimumWidth(1200)
view.SetTitle("cwtch")
return view
}
// 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) {
var err error
if os.Getenv("CWTCH_FOLDER") != "" {
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
} else {
usr, err := user.Current()
if err != nil {
fmt.Printf("\nerror: could not load current user: %v\n", err)
os.Exit(1)
}
the.CwtchDir = path.Join(usr.HomeDir, ".cwtch")
}
/*_, err := app2.NewApp(dirname, "/data/data/org.qtproject.example.go/lib/libtor.so")
if err != nil {
log.Errorf("ERROR CREATING CWTCH APP: %v", err)
}
time.Sleep(time.Second * 10)
*/
os.MkdirAll(the.CwtchDir, 0700)
mn, err := connectivity.StartTor(the.CwtchDir, "")
if err != nil {
log.Errorf("Could not start Tor: %v", err)
os.Exit(1)
}
the.CwtchApp = libapp.NewApp(mn, the.CwtchDir)
err = the.CwtchApp.LoadProfiles("be gay do crime")
if err != nil {
//TODO no more fatalfs
log.Errorf("couldn't load profiles: %v", err)
os.Exit(1)
}
if len(the.CwtchApp.ListPeers()) == 0 {
log.Infoln("couldn't load your config file. attempting to create one now")
the.Peer, err = the.CwtchApp.CreatePeer("alice", "be gay do crime")
if err != nil {
log.Errorf("couldn't create one. is your cwtch folder writable?")
os.Exit(1)
}
} else {
the.Peer = the.CwtchApp.PrimaryIdentity()
}
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion))
the.CwtchApp.LaunchPeers()
contacts := the.Peer.GetContacts()
for i := range contacts {
contact, _ := the.Peer.GetProfile().GetContact(contacts[i])
displayName, _ := contact.GetAttribute("name")
gcd.UIState.AddContact(&gobjects.Contact{
Handle: contacts[i],
DisplayName: displayName,
Image: cwutil.RandomProfileImage(contacts[i]),
Trusted: contact.Trusted,
})
}
groups := the.Peer.GetGroups()
for i := range groups {
group := the.Peer.GetGroup(groups[i])
group.NewMessage = make(chan model.Message)
the.Peer.JoinServer(group.GroupServer)
nick, exists := group.GetAttribute("nick")
if !exists {
nick = group.GroupID[:12]
}
gcd.UIState.AddContact(&gobjects.Contact{
Handle: group.GroupID,
DisplayName: nick,
Image: cwutil.RandomGroupImage(group.GroupID),
Server: group.GroupServer,
Trusted: group.Accepted,
})
}
}