1
1
package io.github.vinceglb.autolaunch
2
2
3
+ import co.touchlab.kermit.Logger
3
4
import kotlin.io.path.*
4
5
5
6
internal class PlatformAutoLaunchLinux (private val config : AutoLaunchConfig ) : PlatformAutoLaunch {
7
+ private val logger = Logger .withTag(" PlatformAutoLaunchLinux" )
6
8
// Checks if the application is installed
7
9
// by looking for a corresponding .desktop file in /usr/share/applications and /opt
8
10
private fun isInstalled (): Boolean {
@@ -19,7 +21,7 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
19
21
}
20
22
21
23
val isInstalled = desktopFile != null || optFile != null
22
- println ( " Checking if app is installed: $isInstalled (package: $appPackageName )" )
24
+ logger.d { " Checking if app is installed: $isInstalled (package: $appPackageName )" }
23
25
return isInstalled
24
26
}
25
27
@@ -31,10 +33,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
31
33
it.name.endsWith(" ${appPackageName.replace(" " , " _" )} .desktop" )
32
34
}
33
35
return if (desktopFile != null && desktopFile.exists()) {
34
- println ( " Reading desktop file content from: $desktopFile " )
36
+ logger.d { " Reading desktop file content from: $desktopFile " }
35
37
desktopFile.readText()
36
38
} else {
37
- println ( " Desktop file not found for package: $appPackageName " )
39
+ logger.d { " Desktop file not found for package: $appPackageName " }
38
40
null
39
41
}
40
42
}
@@ -56,10 +58,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
56
58
val key = entry.substringBefore(" =" )
57
59
val existingIndex = updatedContent.indexOfFirst { it.startsWith(key) }
58
60
if (existingIndex != - 1 ) {
59
- println ( " Updating existing entry: $key " )
61
+ logger.d { " Updating existing entry: $key " }
60
62
updatedContent[existingIndex] = entry
61
63
} else {
62
- println ( " Adding new entry: $entry " )
64
+ logger.d { " Adding new entry: $entry " }
63
65
updatedContent.add(entry)
64
66
}
65
67
}
@@ -69,12 +71,12 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
69
71
if (execIndex != - 1 ) {
70
72
val execLine = updatedContent[execIndex]
71
73
if (! execLine.contains(" --autostart=true" )) {
72
- println ( " Adding --autostart=true to the Exec line" )
74
+ logger.d { " Adding --autostart=true to the Exec line" }
73
75
updatedContent[execIndex] = " $execLine --autostart=true"
74
76
}
75
77
} else {
76
78
// If Exec line does not exist, create a new one
77
- println ( " Adding new Exec line with --autostart=true" )
79
+ logger.d { " Adding new Exec line with --autostart=true" }
78
80
updatedContent.add(" Exec=${config.appPath} --autostart=true" )
79
81
}
80
82
@@ -86,11 +88,11 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
86
88
private fun writeAutostartDesktopFile (content : String ) {
87
89
val autostartDirectory = Path (System .getProperty(" user.home" ), " .config/autostart" )
88
90
if (! autostartDirectory.exists()) {
89
- println ( " Creating autostart directory at: $autostartDirectory " )
91
+ logger.d { " Creating autostart directory at: $autostartDirectory " }
90
92
autostartDirectory.createDirectories()
91
93
}
92
94
val autostartFile = autostartDirectory.resolve(" ${config.appPackageName} .desktop" )
93
- println ( " Writing autostart desktop file to: $autostartFile " )
95
+ logger.d { " Writing autostart desktop file to: $autostartFile " }
94
96
autostartFile.writeText(content)
95
97
}
96
98
@@ -113,10 +115,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
113
115
|WantedBy=default.target
114
116
""" .trimMargin()
115
117
if (servicePath.exists()) {
116
- println ( " Service $appPath already exists at $servicePath " )
118
+ logger.d { " Service $appPath already exists at $servicePath " }
117
119
118
120
if (servicePath.readText() == serviceContent) {
119
- println ( " Service $appPath is already up to date" )
121
+ logger.d { " Service $appPath is already up to date" }
120
122
return
121
123
}
122
124
servicePath.deleteExisting()
@@ -132,9 +134,9 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
132
134
servicePath.writeText(serviceContent)
133
135
}
134
136
enableSystemdService(appPath)
135
- println ( " Service $servicePath has been updated" )
137
+ logger.d { " Service $servicePath has been updated" }
136
138
} catch (e: Exception ) {
137
- println ( " Failed to enable auto start as systemd service: $e " )
139
+ logger.d { " Failed to enable auto start as systemd service: $e " }
138
140
}
139
141
}
140
142
@@ -171,7 +173,7 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
171
173
val appPackageName = config.appPackageName
172
174
val autostartFile = Path (System .getProperty(" user.home" ), " .config/autostart/$appPackageName .desktop" )
173
175
val isEnabledDesktop = autostartFile.exists()
174
- println ( " Checking if autostart is enabled: $isEnabledDesktop (path: $autostartFile )" )
176
+ logger.d { " Checking if autostart is enabled: $isEnabledDesktop (path: $autostartFile )" }
175
177
176
178
val appPath = appPackageName.replace(" " , " -" ).lowercase()
177
179
val statusProcess = ProcessBuilder (" systemctl" , " --user" , " status" , " $appPath .service" )
@@ -180,40 +182,40 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
180
182
val statusOutput = statusProcess.inputStream.bufferedReader().readText()
181
183
statusProcess.waitFor()
182
184
val isEnabledSystemd = statusOutput.contains(" Active: active (running)" )
183
- println ( " Checking if systemd is enabled: $isEnabledSystemd (path: $appPath .service)" )
185
+ logger.d { " Checking if systemd is enabled: $isEnabledSystemd (path: $appPath .service)" }
184
186
185
187
val isEnabled = isEnabledDesktop || isEnabledSystemd
186
188
return isEnabled
187
189
}
188
190
189
191
// Enables autostart by copying and modifying the application's .desktop file
190
192
override suspend fun enable () {
191
- println ( " Enabling autostart for app: ${config.appPackageName} " )
193
+ logger.d { " Enabling autostart for app: ${config.appPackageName} " }
192
194
if (isInstalled()) {
193
195
val desktopFileContent = getDesktopFileContent()
194
196
if (desktopFileContent != null ) {
195
197
val modifiedContent = modifyDesktopFileContent(desktopFileContent)
196
198
writeAutostartDesktopFile(modifiedContent)
197
199
} else {
198
- println ( " Desktop file content is null. Trying to enable autostart via systemd service." )
200
+ logger.d { " Desktop file content is null. Trying to enable autostart via systemd service." }
199
201
writeSystemdService()
200
202
}
201
203
} else {
202
- println ( " Failed to enable autostart: app is not installed" )
204
+ logger.d { " Failed to enable autostart: app is not installed" }
203
205
}
204
206
}
205
207
206
208
// Disables autostart by deleting the .desktop file in ~/.config/autostart
207
209
override suspend fun disable () {
208
210
val appPackageName = config.appPackageName
209
- println ( " Disabling autostart for app: $appPackageName " )
211
+ logger.d { " Disabling autostart for app: $appPackageName " }
210
212
val autostartFile = Path (System .getProperty(" user.home" ), " .config/autostart/$appPackageName .desktop" )
211
213
if (autostartFile.exists()) {
212
- println ( " Deleting autostart desktop file at: $autostartFile " )
214
+ logger.d { " Deleting autostart desktop file at: $autostartFile " }
213
215
autostartFile.deleteIfExists()
214
216
} else {
215
- println ( " Autostart desktop file not found at: $autostartFile " )
216
- println ( " Disabling systemd for app: $appPackageName " )
217
+ logger.d { " Autostart desktop file not found at: $autostartFile " }
218
+ logger.d { " Disabling systemd for app: $appPackageName " }
217
219
disableSystemdService(appPackageName.replace(" " , " -" ).lowercase())
218
220
}
219
221
}
0 commit comments