main.go (5882B)
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/signal" 7 "path/filepath" 8 "runtime" 9 "sync" 10 "syscall" 11 12 "github.com/Nordgedanken/Morpheus/matrix" 13 "github.com/Nordgedanken/Morpheus/matrix/db" 14 "github.com/Nordgedanken/Morpheus/ui" 15 "github.com/dgraph-io/badger" 16 "github.com/Nordgedanken/dugong" 17 "github.com/matrix-org/gomatrix" 18 "github.com/shibukawa/configdir" 19 log "github.com/sirupsen/logrus" 20 "github.com/therecipe/qt/core" 21 "github.com/therecipe/qt/gui" 22 "github.com/therecipe/qt/widgets" 23 ) 24 25 var window *widgets.QMainWindow 26 27 func main() { 28 runtime.GOMAXPROCS(128) 29 30 // Init Logs 31 configDirs := configdir.New("Nordgedanken", "Morpheus") 32 33 log.SetFormatter(&log.TextFormatter{ 34 TimestampFormat: "2006-01-02 15:04:05.000000", 35 DisableColors: false, 36 DisableTimestamp: true, 37 DisableSorting: true, 38 QuoteEmptyFields: true, 39 }) 40 41 log.AddHook(dugong.NewFSHook( 42 filepath.Join(filepath.ToSlash(configDirs.QueryFolders(configdir.Global)[0].Path)+"/log/", "info.log"), 43 filepath.Join(filepath.ToSlash(configDirs.QueryFolders(configdir.Global)[0].Path)+"/log/", "warn.log"), 44 filepath.Join(filepath.ToSlash(configDirs.QueryFolders(configdir.Global)[0].Path)+"/log/", "error.log"), 45 &log.TextFormatter{ 46 TimestampFormat: "2006-01-02 15:04:05.000000", 47 DisableColors: true, 48 DisableTimestamp: false, 49 DisableSorting: false, 50 }, &dugong.DailyRotationSchedule{GZip: false}, 51 )) 52 53 c := make(chan os.Signal, 2) 54 signal.Notify(c, os.Interrupt, syscall.SIGTERM) 55 go func() { 56 <-c 57 cleanup() 58 close(c) 59 os.Exit(1) 60 }() 61 62 UserDB, DBOpenErr := db.OpenUserDB() 63 if DBOpenErr != nil { 64 log.Fatalln(DBOpenErr) 65 } 66 67 CacheDB, DBOpenErr := db.OpenCacheDB() 68 if DBOpenErr != nil { 69 log.Fatalln(DBOpenErr) 70 } 71 72 log.Infoln("Starting Morpheus") 73 74 app := widgets.NewQApplication(len(os.Args), os.Args) 75 76 app.SetAttribute(core.Qt__AA_UseHighDpiPixmaps, true) 77 app.SetApplicationName("Morpheus") 78 app.SetApplicationVersion("0.0.1") 79 appIcon := gui.NewQIcon5(":/qml/resources/logos/MorpheusBig.png") 80 app.SetWindowIcon(appIcon) 81 82 window = widgets.NewQMainWindow(nil, 0) 83 /*window.SetWindowFlags(core.Qt__FramelessWindowHint) 84 var dragPositionX int 85 var dragPositionY int 86 window.ConnectMousePressEvent(func(event *gui.QMouseEvent) { 87 if event.Button() == core.Qt__LeftButton { 88 dragPositionX = event.GlobalX() - window.FrameGeometry().TopLeft().X() 89 dragPositionY = event.GlobalY() - window.FrameGeometry().TopLeft().Y() 90 event.Accept() 91 } 92 }) 93 94 window.ConnectMouseMoveEvent(func(event *gui.QMouseEvent) { 95 if event.Button() == core.Qt__LeftButton { 96 window.Move2(event.GlobalX()-dragPositionX, event.GlobalY()-dragPositionY) 97 event.Accept() 98 } 99 })*/ 100 101 windowHeight := 600 102 windowWidth := 950 103 104 desktopApp := widgets.QApplication_Desktop() 105 primaryScreen := desktopApp.PrimaryScreen() 106 screen := desktopApp.Screen(primaryScreen) 107 windowX := (screen.Width() - windowHeight) / 2 108 windowY := (screen.Height() - windowWidth) / 2 109 110 window.Move2(windowX, windowY) 111 112 var accessToken string 113 var homeserverURL string 114 var userID string 115 116 // Get cache 117 DBErr := UserDB.View(func(txn *badger.Txn) error { 118 accessTokenItem, QueryErr := txn.Get([]byte("user|accessToken")) 119 if QueryErr != nil && QueryErr != badger.ErrKeyNotFound { 120 return QueryErr 121 } 122 if QueryErr != badger.ErrKeyNotFound { 123 accessTokenByte, accessTokenErr := accessTokenItem.Value() 124 accessToken = fmt.Sprintf("%s", accessTokenByte) 125 if accessTokenErr != nil { 126 return accessTokenErr 127 } 128 } 129 130 homeserverURLItem, QueryErr := txn.Get([]byte("user|homeserverURL")) 131 if QueryErr != nil && QueryErr != badger.ErrKeyNotFound { 132 return QueryErr 133 } 134 if QueryErr != badger.ErrKeyNotFound { 135 homeserverURLByte, homeserverURLErr := homeserverURLItem.Value() 136 homeserverURL = fmt.Sprintf("%s", homeserverURLByte) 137 if homeserverURLErr != nil { 138 return homeserverURLErr 139 } 140 } 141 142 userIDItem, QueryErr := txn.Get([]byte("user|userID")) 143 if QueryErr != nil && QueryErr != badger.ErrKeyNotFound { 144 return QueryErr 145 } 146 if QueryErr != badger.ErrKeyNotFound { 147 userIDByte, userIDErr := userIDItem.Value() 148 userID = fmt.Sprintf("%s", userIDByte) 149 return userIDErr 150 } 151 return nil 152 }) 153 if DBErr != nil { 154 log.Errorln("Login: ", DBErr) 155 } 156 157 if accessToken != "" && homeserverURL != "" && userID != "" { 158 var wg sync.WaitGroup 159 log.Infoln("Starting Auto Login Sequenze in background") 160 results := make(chan *gomatrix.Client) 161 162 wg.Add(1) 163 go matrix.DoLogin("", "", homeserverURL, userID, accessToken, results, &wg) 164 165 go func() { 166 wg.Wait() // wait for each execTask to return 167 close(results) // then close the results channel 168 }() 169 170 //Show MainUI 171 for result := range results { 172 //TODO Don't switch screen on wrong login data. 173 MainUIStruct := ui.NewMainUIStruct(windowWidth, windowHeight, window) 174 MainUIStruct.SetCli(result) 175 mainUIErr := MainUIStruct.NewUI() 176 if mainUIErr != nil { 177 log.Errorln("mainUI: ", mainUIErr) 178 return 179 } 180 MainUIStruct.GetWidget().Resize2(windowWidth, windowHeight) 181 window.SetCentralWidget(MainUIStruct.GetWidget()) 182 } 183 } else { 184 //Show loginUI 185 LoginUIStruct := ui.NewLoginUIStruct(windowWidth, windowHeight, window) 186 loginUIErr := LoginUIStruct.NewUI() 187 if loginUIErr != nil { 188 log.Errorln("Login Err: ", loginUIErr) 189 return 190 } 191 LoginUIStruct.GetWidget().Resize2(windowWidth, windowHeight) 192 window.SetCentralWidget(LoginUIStruct.GetWidget()) 193 } 194 195 window.Resize2(windowWidth, windowHeight) 196 window.Show() 197 198 //enter the main event loop 199 _ = widgets.QApplication_Exec() 200 defer UserDB.Close() 201 defer CacheDB.Close() 202 log.Infoln("Stopping Morpheus") 203 } 204 205 func cleanup() { 206 log.Infoln("cleanup") 207 UserDB, DBOpenErr := db.OpenUserDB() 208 if DBOpenErr != nil { 209 log.Errorln(DBOpenErr) 210 } 211 212 CacheDB, DBOpenErr := db.OpenCacheDB() 213 if DBOpenErr != nil { 214 log.Errorln(DBOpenErr) 215 } 216 217 UserDB.Close() 218 CacheDB.Close() 219 }