-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (91 loc) · 2.41 KB
/
main.go
File metadata and controls
111 lines (91 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"path"
"path/filepath"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)
func main() {
// create an fyne app and window
myapp := app.NewWithID("com.Bardia49.DownBit")
window := myapp.NewWindow("DownBit")
// Ensure database directory
databasePath, err := getDatabasePath()
if err != nil {
log.Fatalf("Failed to get database path: %v", err)
}
fmt.Println("Database Path:", databasePath)
// Create a JSON file in the database directory
jsonFilePath, err := createJSONFile(databasePath)
if err != nil {
log.Fatalf("Failed to create JSON file: %v", err)
}
fmt.Println("JSON file created successfully")
//DownloadDirectory
DownBitDownloadsDirectory()
//set Icon
window.SetIcon(resourceDownBitIconPng)
// Config http Client ***
transport := &http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 10,
IdleConnTimeout: 30 * time.Second,
}
client := &http.Client{Transport: transport}
// create MyApp
c := context.Background()
myApp := MyApp{
App: myapp,
AppContext: c,
MainWindow: window,
Client: client,
DownloadStateFilePath: jsonFilePath,
}
// config the main window
myApp.SetWindowConfig()
myApp.makeUI()
// Show and Run window
myApp.MainWindow.ShowAndRun()
}
func (app *MyApp) SetWindowConfig() {
app.MainWindow.Resize(fyne.Size{Height: 500, Width: 600})
app.MainWindow.CenterOnScreen()
}
func DownBitDownloadsDirectory() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to find home directory: %v", err)
}
err = os.MkdirAll(path.Join(homeDir, "Downloads", "/DownBitDownloads"), 0755)
if err != nil {
fmt.Println("Error creating directory:", err)
return fmt.Errorf("unable to make the download directory, err: %v", err)
}
return nil
}
// Returns the database path in user-writable directory
func getDatabasePath() (string, error) {
userHome, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(userHome, "DownBit", "database"), nil
}
// Creates a JSON file in the database directory
func createJSONFile(databasePath string) (string, error) {
os.MkdirAll(databasePath, 0755)
jsonFilePath := filepath.Join(databasePath, "downloads.json")
file, err := os.Create(jsonFilePath)
if err != nil {
return "", err
}
defer file.Close()
_, err = file.WriteString(`{}`)
return jsonFilePath, err
}