-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (139 loc) · 5.65 KB
/
main.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"log"
"os/exec"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
// Given the time in seconds, it will schedule the computer to Shutdown in the especified time
func Shutdown(timeInSeconds int, window fyne.Window) {
cmd := exec.Command("shutdown", "-s", "-t", strconv.Itoa(timeInSeconds))
out, err := cmd.Output()
if err != nil {
if strings.Contains(err.Error(), "1190") {
dialog.ShowInformation(string(out), "Erro: O sistema já está com um agendamento em andamento\nCancele e faça um novo\n", window)
}
} else {
dialog.ShowInformation("Gambit", "Você agendou o horario", window)
log.Println("Output: ", string(out))
}
}
// If you want to abort a scheduled shutdown, it will abort it here
func AbortShutdown(window fyne.Window) {
cmd := exec.Command("shutdown", "-a")
out, err := cmd.Output()
if err != nil {
dialog.ShowInformation(string(out), "Erro: Não existem agendamentos feitos\n", window)
} else {
dialog.ShowInformation("Gambit", "Você cancelou os horario agendados", window)
log.Println("Output: ", string(out))
}
}
// In case you want to shutdown now, this function will make the machine shutdown "immediatly"
func ShutdownNow(window fyne.Window) {
cmd := exec.Command("shutdown", "-s", "-t", strconv.Itoa(1))
out, err := cmd.Output()
if err != nil {
dialog.ShowInformation(string(out), "Erro: Não foi possivel desligar o sistema\n", window)
} else {
dialog.ShowInformation("Gambit", "Desligando...", window)
log.Println("Output: ", string(out))
}
}
// Format the scheduled time string in a way that it returns the hour or the minutes for the use in another functions.
// If hourReturn is true, it will return hour, else return minutes.
// It first checks if the unformatted string is not empty and if the lenght is 5, just then it catchs the hour or minute as integer.
func FormatTimeString(scheduledTime string, hourReturn bool) int {
if scheduledTime != "" && len(scheduledTime) == 5 {
if hourReturn == true {
hour, err := strconv.Atoi(scheduledTime[0:2])
if err != nil {
log.Println("Erro no primeiro ATOI(): ", err)
}
return hour
}
minutes, err := strconv.Atoi(scheduledTime[3:5])
if err != nil {
log.Println("Erro no segundo ATOI(): ", err)
}
return minutes
}
return -1
}
// Given an hour and minutes the function constructs a formal standard date type, if the given hour is less the actual system hour, it means it will be that hour from the next day, or else its the same day
func ConstructDate(givenHour int, givenMinute int) time.Time {
if givenHour < time.Now().Hour() {
date := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()+1, givenHour, givenMinute, 0, 0, time.UTC)
return date
}
date := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), givenHour, givenMinute, 0, 0, time.UTC)
return date
}
// Compare a given date with time.Now() and return the time difference between them in seconds
func CompareDate(givenDate time.Time) int {
difDate := time.Date(givenDate.Year()-time.Now().Year(), givenDate.Month()-time.Now().Month(), givenDate.Day()-time.Now().Day(), givenDate.Hour()-time.Now().Hour(), givenDate.Minute()-time.Now().Minute(), givenDate.Second()-time.Now().Second(), givenDate.Nanosecond()-time.Now().Nanosecond(), time.UTC)
//dif := time.Until(givenDate).Abs().Seconds()
difTimeInSeconds := int(difDate.Hour()*60*60 + difDate.Minute()*60 + difDate.Second())
log.Println("dif_time: ", difTimeInSeconds)
return difTimeInSeconds
}
// Validate input veryfying if its in the format hh:mm in the range 00:00 to 23:59
func ValidateTimeInput(input string, window fyne.Window) string {
var scheduledTime string
var invalid = false
//fmt.Println("Digite uma hora para ser agendado o desligamento no formato (hh:mm, exemplo: 22:31)")
//fmt.Scanln(&scheduledTime)
if FormatTimeString(input, true) < 0 || FormatTimeString(input, true) > 23 {
invalid = true
if FormatTimeString(input, true) >= 0 || FormatTimeString(input, true) <= 23 {
invalid = true
if FormatTimeString(input, false) < 0 || FormatTimeString(input, false) > 59 {
invalid = true
}
}
if invalid != false {
dialog.ShowInformation("Gambit", "Hora inválida, digite a hora novamente no formato hh:mm, lembrando que horas vão de 00 a 23 e minutos de 0 a 59", window)
scheduledTime = ""
return scheduledTime
}
}
scheduledTime = input
return scheduledTime
}
func GetFutureHour(scheduledTime string) int {
hour := FormatTimeString(scheduledTime, true)
return hour
}
func GetFutureMinute(scheduledTime string) int {
minutes := FormatTimeString(scheduledTime, false)
return minutes
}
func main() {
myApp := app.New()
window := myApp.NewWindow("Gambit v1.2.2")
window.Resize(fyne.NewSize(1000, 470))
input := widget.NewEntry()
input.SetPlaceHolder("(hh:mm, exemplo: 22:31)")
content := container.NewVBox(input, widget.NewButton("Agendar", func() {
log.Println("Content was:", input.Text)
scheduledTime := ValidateTimeInput(input.Text, window)
if scheduledTime != "" {
scheduled_date := ConstructDate(FormatTimeString(scheduledTime, true), FormatTimeString(scheduledTime, false))
time_in_seconds := CompareDate(scheduled_date)
Shutdown(time_in_seconds, window)
}
}), widget.NewButton("Cancelar agendamentos", func() {
AbortShutdown(window)
}),
widget.NewButton("Desligar agora", func() {
ShutdownNow(window)
}))
window.SetContent(content)
window.ShowAndRun()
}