refactoring of main to mainUi() and mainService()

This commit is contained in:
Dan Ballard 2019-05-13 14:01:08 -07:00
parent 8a666a7f37
commit 9400d5aa63
1 changed files with 119 additions and 112 deletions

71
main.go
View File

@ -8,7 +8,6 @@ import (
"cwtch.im/ui/go/gothings/android"
"cwtch.im/ui/go/the"
"flag"
"fmt"
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"github.com/therecipe/qt/core"
@ -49,23 +48,46 @@ func main() {
log.SetLevel(log.LevelInfo)
}
log.Infof("cwtch main args: %v\n", os.Args)
if os.Getenv("CWTCH_FOLDER") != "" {
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
} else if runtime.GOOS == "android" {
the.CwtchDir = path.Join(androidBaseDir, "files")
} else {
usr, err := user.Current()
if err != nil {
log.Errorf("\nerror: could not load current user: %v\n", err)
os.Exit(1)
}
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")
torpath := path.Join(androidBaseDir, "lib/libtor.so")
log.Infoln("Starting Tor...")
acn, _ := connectivity.StartTor(the.CwtchDir, torpath)
loadACN()
log.Infoln("Starting a cwtch app...")
the.CwtchApp = libapp.NewApp(acn, the.CwtchDir)
the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir)
log.Infoln("Sleepinging???")
for true {
time.Sleep(5 * time.Second)
log.Infoln("Wake up, repeat")
}
}
} else {
func mainUi(flagLocal bool) {
log.Infoln("I am the application")
app := gui.NewQGuiApplication(len(os.Args), os.Args)
@ -89,7 +111,7 @@ func main() {
// this is to load local qml files quickly when developing
var qmlSource *core.QUrl
if *flagLocal {
if flagLocal {
qmlSource = core.QUrl_FromLocalFile("./qml/main.qml")
} else {
qmlSource = core.NewQUrl3("qrc:/qml/main.qml", 0)
@ -137,41 +159,19 @@ func main() {
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") != "" {
the.CwtchDir = os.Getenv("CWTCH_FOLDER")
} else if runtime.GOOS == "android" {
the.CwtchDir = path.Join(androidBaseDir, "files")
} 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")
}
func loadACN() {
torpath := "tor"
if runtime.GOOS == "android" {
torpath = path.Join(androidBaseDir, "lib/libtor.so")
@ -197,8 +197,15 @@ func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
log.Errorf("Could not start Tor: %v", err)
os.Exit(1)
}
}
func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) {
if runtime.GOOS != "android" {
loadACN()
}
the.Peer = nil
os.MkdirAll(the.CwtchDir, 0700)
the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir)