Skip to content

Commit 6cbd95a

Browse files
committed
chore: copy launchctl script on app load
1 parent c3f1b27 commit 6cbd95a

File tree

4 files changed

+86
-30
lines changed

4 files changed

+86
-30
lines changed

app.go

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"desktop/internal/api"
77
"errors"
88
"fmt"
9+
"log"
910
"os"
1011
"os/exec"
12+
"path/filepath"
1113
"strings"
1214

1315
"github.com/wailsapp/wails/v2/pkg/runtime"
@@ -37,24 +39,24 @@ func NewApp() *App {
3739
return &App{}
3840
}
3941

40-
func startSchedulerWorker() {
41-
fmt.Println("Loading picasa scheduler.....")
42-
cmd := exec.Command("/usr/local/bin/picasa_scheduler")
43-
cmd.Stdout = os.Stdout
44-
cmd.Stderr = os.Stderr
45-
err := cmd.Start()
46-
if err != nil {
47-
fmt.Println("Error starting worker:", err)
48-
return
49-
}
42+
// func startSchedulerWorker() {
43+
// fmt.Println("Loading picasa scheduler.....")
44+
// cmd := exec.Command("/usr/local/bin/picasa_scheduler")
45+
// cmd.Stdout = os.Stdout
46+
// cmd.Stderr = os.Stderr
47+
// err := cmd.Start()
48+
// if err != nil {
49+
// fmt.Println("Error starting worker:", err)
50+
// return
51+
// }
5052

51-
err = cmd.Wait()
52-
if err != nil {
53-
fmt.Println("Worker process finished with error:", err)
54-
}
53+
// err = cmd.Wait()
54+
// if err != nil {
55+
// fmt.Println("Worker process finished with error:", err)
56+
// }
5557

56-
fmt.Printf("[Main App] Scheduler started with PID: %d\n", cmd.Process.Pid)
57-
}
58+
// fmt.Printf("[Main App] Scheduler started with PID: %d\n", cmd.Process.Pid)
59+
// }
5860

5961
// startup is called when the app starts. The context is saved
6062
// so we can call the runtime methods
@@ -63,7 +65,45 @@ func (a *App) startup(ctx context.Context) {
6365
menu := SetMenuItem(ctx, a)
6466
runtime.MenuSetApplicationMenu(ctx, menu)
6567
appConf.Init("$HOME/.picasa")
66-
// startSchedulerWorker()
68+
69+
home, _ := os.UserHomeDir()
70+
srcPath := "com.user.wallflex.plist"
71+
destDir := filepath.Join(home, "Library/LaunchAgents")
72+
destPath := filepath.Join(destDir, filepath.Base(srcPath))
73+
74+
if !hasLaunchAgent(destPath) {
75+
setupLaunchctl(srcPath, destPath)
76+
}
77+
}
78+
79+
func setupLaunchctl(srcPath, destPath string) {
80+
cmd := exec.Command("cp", srcPath, destPath)
81+
if err := cmd.Run(); err != nil {
82+
log.Fatal("Unable to copy the launchctl agent script")
83+
return
84+
}
85+
86+
if err := exec.Command("launchctl", "load", destPath).Run(); err != nil {
87+
log.Fatal("Unable to load the launchctl agent script")
88+
return
89+
}
90+
91+
if err := exec.Command("launchctl", "start", destPath).Run(); err != nil {
92+
log.Fatal("Unable to start the launchctl agent script")
93+
return
94+
}
95+
//load the plist file
96+
// start the file
97+
fmt.Println("Copied the launchagency plist file to the right directory....")
98+
}
99+
100+
func hasLaunchAgent(dir string) bool {
101+
_, err := os.Stat(dir)
102+
fmt.Println(os.IsNotExist(err), " =====")
103+
if os.IsNotExist(err) {
104+
return false
105+
}
106+
return err == nil
67107
}
68108

69109
func (a *App) GetDownloadedImages() ([]string, error) {
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
<plist version="1.0">
44
<dict>
55
<key>Disabled</key>
6-
<false/>
6+
<false />
77
<key>LimitLoadToSessionType</key>
88
<string>Aqua</string>
99
<key>Label</key>
10-
<string>com.picasa.scheduler.daemon</string>
10+
<string>com.user.wallflex</string>
11+
1112
<key>ProgramArguments</key>
1213
<array>
13-
<string>/usr/local/bin/picasa_scheduler</string>
14-
<string>--launchd</string>
14+
<string>/usr/local/bin/picasa_scheduler</string>
15+
<string>--launchd</string>
1516
</array>
17+
1618
<key>RunAtLoad</key>
17-
<true/>
18-
19-
<key>KeepAlive</key>
20-
<true/>
19+
<true />
2120

21+
<key>KeepAlive</key>
22+
<true />
2223
<key>StandardOutPath</key>
23-
<string>/tmp/picasa_stdout.log</string>
24+
<string>/tmp/wallflex_stdout.log</string>
2425

2526
<key>StandardErrorPath</key>
26-
<string>/tmp/picasa_stderr.log</string>
27-
</dict>
28-
</plist>
27+
<string>/tmp/wallflex_stderr.log</string>
28+
</dict>
29+
</plist>

frontend/dist/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
--main-color: orange;
1111
}
1212
</style>
13-
<script type="module" crossorigin src="/assets/index.99f74794.js"></script>
13+
<script type="module" crossorigin src="/assets/index.abdc74b1.js"></script>
1414
<link rel="stylesheet" href="/assets/index.849d2c86.css">
1515
</head>
1616
<body class="dark:bg-gray-700 dark:text-gray-50">

frontend/src/pages/Setting.svelte

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
let apikey = '';
1919
let scheduleImageDownloadInterval = '';
2020
let hasAutoDownloadEnabled = false;
21+
let isAutoWallpaperEnabled = false;
2122
2223
async function handleSaveSetting() {
2324
if (imageCategory === '' || totalImageCount === 0 || imageInterval === '') {
@@ -100,6 +101,10 @@
100101
function handleToggleAutoDownload(event) {
101102
hasAutoDownloadEnabled = event.target.checked;
102103
}
104+
105+
function handleToggleAutoWallpaper(event) {
106+
isAutoWallpaperEnabled = event.target.checked;
107+
}
103108
</script>
104109

105110
<template>
@@ -240,6 +245,16 @@
240245
bind:value={apikey}
241246
/>
242247
</div>
248+
249+
<div class="bg-gray-200 p-2 rounded-md w-6/12 mx-auto my-5">
250+
<label class="inline-flex items-center cursor-pointer">
251+
<input type="checkbox" value="" class="sr-only peer" on:change={handleToggleAutoWallpaper} checked={isAutoWallpaperEnabled}/>
252+
<div
253+
class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-green-600 dark:peer-checked:bg-green-600"
254+
></div>
255+
<span class="ms-3 text-sm font-medium">Auto change wallpaper</span>
256+
</label>
257+
</div>
243258
<div class="mt-5">
244259
<button
245260
class="text-gray-100 py-2 px-10 cursor-default mb-3 rounded-md bg-gray-500"

0 commit comments

Comments
 (0)