refactoring of main to mainUi() and mainService()
This commit is contained in:
parent
8a666a7f37
commit
9400d5aa63
231
main.go
231
main.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"cwtch.im/ui/go/gothings/android"
|
"cwtch.im/ui/go/gothings/android"
|
||||||
"cwtch.im/ui/go/the"
|
"cwtch.im/ui/go/the"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"github.com/therecipe/qt/core"
|
"github.com/therecipe/qt/core"
|
||||||
|
@ -49,116 +48,6 @@ func main() {
|
||||||
log.SetLevel(log.LevelInfo)
|
log.SetLevel(log.LevelInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("cwtch main args: %v\n", os.Args)
|
|
||||||
|
|
||||||
if *flagService {
|
|
||||||
log.Infoln("I am the service")
|
|
||||||
torpath := path.Join(androidBaseDir, "lib/libtor.so")
|
|
||||||
log.Infoln("Starting Tor...")
|
|
||||||
acn, _ := connectivity.StartTor(the.CwtchDir, torpath)
|
|
||||||
log.Infoln("Starting a cwtch app...")
|
|
||||||
the.CwtchApp = libapp.NewApp(acn, the.CwtchDir)
|
|
||||||
|
|
||||||
log.Infoln("Sleepinging???")
|
|
||||||
for true {
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
log.Infoln("Wake up, repeat")
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
log.Infoln("I am the application")
|
|
||||||
|
|
||||||
app := gui.NewQGuiApplication(len(os.Args), os.Args)
|
|
||||||
|
|
||||||
// our globals
|
|
||||||
gcd := gothings.NewGrandCentralDispatcher(nil)
|
|
||||||
gcd.SetOs(runtime.GOOS)
|
|
||||||
if buildVer != "" {
|
|
||||||
gcd.SetVersion(buildVer)
|
|
||||||
gcd.SetBuildDate(buildDate)
|
|
||||||
} else {
|
|
||||||
gcd.SetVersion("development")
|
|
||||||
gcd.SetBuildDate("now")
|
|
||||||
}
|
|
||||||
gcd.UIState = gothings.NewUIState(gcd)
|
|
||||||
the.AcknowledgementIDs = make(map[string][]*the.AckId)
|
|
||||||
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 *flagLocal {
|
|
||||||
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
|
|
||||||
} else {
|
|
||||||
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.SetWindowIcon(gui.NewQIcon5(":/qml/images/cwtch-icon.png"))
|
|
||||||
|
|
||||||
// load english first so it becomes the default in case we don't have a .ts for the user's locale, or if it contains unfinished strings
|
|
||||||
translator := core.NewQTranslator(nil)
|
|
||||||
translator.Load("translation_en", ":/i18n/", "", "")
|
|
||||||
core.QCoreApplication_InstallTranslator(translator)
|
|
||||||
|
|
||||||
gcd.Translator = core.NewQTranslator(nil)
|
|
||||||
gcd.Translator.Load("translation_"+core.QLocale_System().Name(), ":/i18n/", "", "")
|
|
||||||
core.QCoreApplication_InstallTranslator(gcd.Translator)
|
|
||||||
|
|
||||||
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
|
|
||||||
quickcontrols2.QQuickStyle_SetStyle("Universe")
|
|
||||||
engine := qml.NewQQmlApplicationEngine(nil)
|
|
||||||
gcd.QMLEngine = engine
|
|
||||||
|
|
||||||
// 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
|
|
||||||
})
|
|
||||||
engine.SetNetworkAccessManagerFactory(factory)
|
|
||||||
|
|
||||||
// variables we want to access from inside qml
|
|
||||||
if runtime.GOOS == "android" {
|
|
||||||
gcd.SetThemeScale(2.9)
|
|
||||||
} else {
|
|
||||||
gcd.SetThemeScale(1.0)
|
|
||||||
}
|
|
||||||
engine.RootContext().SetContextProperty("gcd", gcd)
|
|
||||||
|
|
||||||
var androidCwtchActivity = android.NewCwtchActivity(nil)
|
|
||||||
engine.RootContext().SetContextProperty("androidCwtchActivity", androidCwtchActivity)
|
|
||||||
/*if runtime.GOOS == "android" {
|
|
||||||
androidCwtchActivity.StartCwtchService()
|
|
||||||
}*/
|
|
||||||
|
|
||||||
//engine.addImportPath(QStringLiteral("qrc:/"));
|
|
||||||
//engine.load(QUrl(QStringLiteral("qrc:/source/qml/main.qml")));
|
|
||||||
|
|
||||||
engine.Load(qmlSource)
|
|
||||||
|
|
||||||
the.ACN = nil
|
|
||||||
go loadNetworkingAndFiles(gcd)
|
|
||||||
|
|
||||||
app.Exec()
|
|
||||||
}
|
|
||||||
|
|
||||||
if the.ACN != nil {
|
|
||||||
the.ACN.Close()
|
|
||||||
}
|
|
||||||
log.Infoln("Exiting main()")
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
|
|
||||||
if os.Getenv("CWTCH_FOLDER") != "" {
|
if os.Getenv("CWTCH_FOLDER") != "" {
|
||||||
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
|
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
|
||||||
} else if runtime.GOOS == "android" {
|
} else if runtime.GOOS == "android" {
|
||||||
|
@ -166,12 +55,123 @@ func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
|
||||||
} else {
|
} else {
|
||||||
usr, err := user.Current()
|
usr, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("\nerror: could not load current user: %v\n", err)
|
log.Errorf("\nerror: could not load current user: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
the.CwtchDir = path.Join(usr.HomeDir, ".cwtch")
|
the.CwtchDir = path.Join(usr.HomeDir, ".cwtch")
|
||||||
}
|
}
|
||||||
|
the.ACN = nil
|
||||||
|
|
||||||
|
if *flagService {
|
||||||
|
mainService()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mainUi(*flagLocal)
|
||||||
|
}
|
||||||
|
|
||||||
|
if the.ACN != nil {
|
||||||
|
the.ACN.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mainService() {
|
||||||
|
log.Infoln("I am the service")
|
||||||
|
loadACN()
|
||||||
|
log.Infoln("Starting a cwtch app...")
|
||||||
|
the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir)
|
||||||
|
|
||||||
|
log.Infoln("Sleepinging???")
|
||||||
|
for true {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
log.Infoln("Wake up, repeat")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mainUi(flagLocal bool) {
|
||||||
|
log.Infoln("I am the application")
|
||||||
|
|
||||||
|
app := gui.NewQGuiApplication(len(os.Args), os.Args)
|
||||||
|
|
||||||
|
// our globals
|
||||||
|
gcd := gothings.NewGrandCentralDispatcher(nil)
|
||||||
|
gcd.SetOs(runtime.GOOS)
|
||||||
|
if buildVer != "" {
|
||||||
|
gcd.SetVersion(buildVer)
|
||||||
|
gcd.SetBuildDate(buildDate)
|
||||||
|
} else {
|
||||||
|
gcd.SetVersion("development")
|
||||||
|
gcd.SetBuildDate("now")
|
||||||
|
}
|
||||||
|
gcd.UIState = gothings.NewUIState(gcd)
|
||||||
|
the.AcknowledgementIDs = make(map[string][]*the.AckId)
|
||||||
|
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 flagLocal {
|
||||||
|
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
|
||||||
|
} else {
|
||||||
|
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.SetWindowIcon(gui.NewQIcon5(":/qml/images/cwtch-icon.png"))
|
||||||
|
|
||||||
|
// load english first so it becomes the default in case we don't have a .ts for the user's locale, or if it contains unfinished strings
|
||||||
|
translator := core.NewQTranslator(nil)
|
||||||
|
translator.Load("translation_en", ":/i18n/", "", "")
|
||||||
|
core.QCoreApplication_InstallTranslator(translator)
|
||||||
|
|
||||||
|
gcd.Translator = core.NewQTranslator(nil)
|
||||||
|
gcd.Translator.Load("translation_"+core.QLocale_System().Name(), ":/i18n/", "", "")
|
||||||
|
core.QCoreApplication_InstallTranslator(gcd.Translator)
|
||||||
|
|
||||||
|
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
|
||||||
|
quickcontrols2.QQuickStyle_SetStyle("Universe")
|
||||||
|
engine := qml.NewQQmlApplicationEngine(nil)
|
||||||
|
gcd.QMLEngine = engine
|
||||||
|
|
||||||
|
// 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
|
||||||
|
})
|
||||||
|
engine.SetNetworkAccessManagerFactory(factory)
|
||||||
|
|
||||||
|
// variables we want to access from inside qml
|
||||||
|
if runtime.GOOS == "android" {
|
||||||
|
gcd.SetThemeScale(2.9)
|
||||||
|
} else {
|
||||||
|
gcd.SetThemeScale(1.0)
|
||||||
|
}
|
||||||
|
engine.RootContext().SetContextProperty("gcd", gcd)
|
||||||
|
|
||||||
|
var androidCwtchActivity = android.NewCwtchActivity(nil)
|
||||||
|
engine.RootContext().SetContextProperty("androidCwtchActivity", androidCwtchActivity)
|
||||||
|
|
||||||
|
//engine.addImportPath(QStringLiteral("qrc:/"));
|
||||||
|
//engine.load(QUrl(QStringLiteral("qrc:/source/qml/main.qml")));
|
||||||
|
|
||||||
|
engine.Load(qmlSource)
|
||||||
|
|
||||||
|
go loadNetworkingAndFiles(gcd)
|
||||||
|
|
||||||
|
app.Exec()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadACN() {
|
||||||
torpath := "tor"
|
torpath := "tor"
|
||||||
if runtime.GOOS == "android" {
|
if runtime.GOOS == "android" {
|
||||||
torpath = path.Join(androidBaseDir, "lib/libtor.so")
|
torpath = path.Join(androidBaseDir, "lib/libtor.so")
|
||||||
|
@ -197,8 +197,15 @@ func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
|
||||||
log.Errorf("Could not start Tor: %v", err)
|
log.Errorf("Could not start Tor: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
|
||||||
|
if runtime.GOOS != "android" {
|
||||||
|
loadACN()
|
||||||
|
}
|
||||||
|
|
||||||
the.Peer = nil
|
the.Peer = nil
|
||||||
|
|
||||||
os.MkdirAll(the.CwtchDir, 0700)
|
os.MkdirAll(the.CwtchDir, 0700)
|
||||||
|
|
||||||
the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir)
|
the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir)
|
||||||
|
|
Reference in New Issue