2024-01-14 16:31:07 +06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-18 01:39:20 +06:00
|
|
|
"errors"
|
2024-01-14 16:31:07 +06:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/app"
|
2024-01-28 22:01:16 +06:00
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
2024-02-03 18:40:44 +06:00
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/convertor"
|
|
|
|
error2 "git.kor-elf.net/kor-elf/gui-for-ffmpeg/error"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/handler"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/localizer"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/menu"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/migration"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/setting"
|
2024-01-18 01:39:20 +06:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2024-01-28 22:01:16 +06:00
|
|
|
"golang.org/x/text/language"
|
2024-01-18 01:39:20 +06:00
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"os"
|
2024-01-14 16:31:07 +06:00
|
|
|
)
|
|
|
|
|
2024-02-03 17:47:32 +06:00
|
|
|
const appVersion string = "0.3.0"
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
a := app.New()
|
2024-01-23 21:33:30 +06:00
|
|
|
iconResource, err := fyne.LoadResourceFromPath("icon.png")
|
|
|
|
if err == nil {
|
|
|
|
a.SetIcon(iconResource)
|
|
|
|
}
|
2024-02-03 17:47:32 +06:00
|
|
|
w := a.NewWindow("GUI for FFmpeg")
|
2024-01-18 20:23:23 +06:00
|
|
|
w.Resize(fyne.Size{Width: 800, Height: 600})
|
2024-01-20 01:53:25 +06:00
|
|
|
w.CenterOnScreen()
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
localizerService, err := localizer.NewService("languages", language.Russian)
|
|
|
|
if err != nil {
|
|
|
|
panicErrorLang(w, err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
errorView := error2.NewView(w, localizerService)
|
2024-01-18 01:39:20 +06:00
|
|
|
if canCreateFile("data/database") != true {
|
2024-01-28 22:01:16 +06:00
|
|
|
errorView.PanicErrorWriteDirectoryData()
|
2024-01-18 01:39:20 +06:00
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := gorm.Open(sqlite.Open("data/database"), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
defer appCloseWithDb(db)
|
2024-01-18 01:39:20 +06:00
|
|
|
|
|
|
|
err = migration.Run(db)
|
|
|
|
if err != nil {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
settingRepository := setting.NewRepository(db)
|
2024-02-01 00:23:28 +06:00
|
|
|
convertorRepository := convertor.NewRepository(settingRepository)
|
|
|
|
pathFFmpeg, err := convertorRepository.GetPathFfmpeg()
|
2024-01-18 01:39:20 +06:00
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) == false {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
2024-02-01 00:23:28 +06:00
|
|
|
pathFFprobe, err := convertorRepository.GetPathFfprobe()
|
2024-01-18 01:39:20 +06:00
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) == false {
|
|
|
|
errorView.PanicError(err)
|
|
|
|
w.ShowAndRun()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ffPathUtilities := convertor.FFPathUtilities{FFmpeg: pathFFmpeg, FFprobe: pathFFprobe}
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
localizerView := localizer.NewView(w, localizerService)
|
|
|
|
convertorView := convertor.NewView(w, localizerService)
|
2024-01-18 01:39:20 +06:00
|
|
|
convertorService := convertor.NewService(ffPathUtilities)
|
2024-01-20 14:58:16 +06:00
|
|
|
defer appCloseWithConvert(convertorService)
|
2024-02-01 00:23:28 +06:00
|
|
|
convertorHandler := handler.NewConvertorHandler(convertorService, convertorView, convertorRepository, localizerService)
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-01-31 21:02:12 +06:00
|
|
|
localizerRepository := localizer.NewRepository(settingRepository)
|
2024-02-03 17:47:32 +06:00
|
|
|
menuView := menu.NewView(w, a, appVersion, localizerService)
|
|
|
|
mainMenu := handler.NewMenuHandler(convertorHandler, menuView, localizerService, localizerView, localizerRepository)
|
2024-01-31 21:02:12 +06:00
|
|
|
|
|
|
|
mainHandler := handler.NewMainHandler(convertorHandler, mainMenu, localizerRepository, localizerService)
|
|
|
|
mainHandler.Start()
|
|
|
|
|
|
|
|
w.SetMainMenu(mainMenu.GetMainMenu())
|
2024-01-14 16:31:07 +06:00
|
|
|
|
|
|
|
w.ShowAndRun()
|
|
|
|
}
|
2024-01-18 01:39:20 +06:00
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
func appCloseWithDb(db *gorm.DB) {
|
2024-01-18 01:39:20 +06:00
|
|
|
sqlDB, err := db.DB()
|
|
|
|
if err == nil {
|
|
|
|
_ = sqlDB.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:58:16 +06:00
|
|
|
func appCloseWithConvert(convertorService convertor.ServiceContract) {
|
|
|
|
for _, cmd := range convertorService.GetRunningProcesses() {
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 01:39:20 +06:00
|
|
|
func canCreateFile(path string) bool {
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_ = file.Close()
|
|
|
|
return true
|
|
|
|
}
|
2024-01-28 22:01:16 +06:00
|
|
|
|
|
|
|
func panicErrorLang(w fyne.Window, err error) {
|
|
|
|
w.SetContent(container.NewVBox(
|
|
|
|
widget.NewLabel("Произошла ошибка!"),
|
|
|
|
widget.NewLabel("произошла ошибка при получении языковых переводах. \n\r"+err.Error()),
|
|
|
|
))
|
|
|
|
}
|