Skip to content

Commit 93bd365

Browse files
committed
Implement safer plasma reloading
1 parent 1bf6789 commit 93bd365

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

konfsave/actions.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -278,32 +278,10 @@ def action_load(argv):
278278
args.profile,
279279
include,
280280
exclude,
281-
overwrite_unsaved_configuration=args.overwrite
281+
overwrite_unsaved_configuration=args.overwrite,
282+
restart=args.restart
282283
)
283284
if success:
284-
if args.restart:
285-
subprocess.run(['sh', '-c', 'plasmashell --replace & disown'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
286-
print('Restarting Plasma shell...')
287-
try:
288-
# Check if Kwin is running
289-
subprocess.run(['ps', '-C', 'kwin_x11'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
290-
except subprocess.CalledProcessError:
291-
logger.info('No running instance of Kwin detected')
292-
else:
293-
# If so, restart Kwin
294-
time.sleep(3) # Allow plasmashell to completely restart
295-
print('Restarting Kwin...')
296-
subprocess.run(['sh', '-c', 'kwin_x11 --replace & disown'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
297-
try:
298-
# Check if Latte Dock is running
299-
subprocess.run(['ps', '-C', 'latte-dock'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
300-
except subprocess.CalledProcessError:
301-
logger.info('No running instance of Latte detected')
302-
else:
303-
# If so, restart Latte Dock
304-
time.sleep(5) # Allow plasmashell and kwin to completely restart
305-
print('Restarting Latte...')
306-
subprocess.run(['sh', '-c', 'latte-dock --replace & disown'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
307285
print('Success')
308286

309287

konfsave/profiles.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import logging
55
import shutil
66
import json
7+
import subprocess
8+
import time
79
from pathlib import Path
810
from typing import Optional, Set, Union, TextIO, Iterable
911

@@ -146,7 +148,7 @@ def save(name=None, include=None, exclude=None, follow_symlinks=False, destinati
146148
f.write(json.dumps(new_info)) # Write only after JSON serialization is successful
147149

148150

149-
def load(name, include=None, exclude=None, overwrite_unsaved_configuration=False) -> bool:
151+
def load(name, include=None, exclude=None, overwrite_unsaved_configuration=False, restart=True) -> bool:
150152
"""
151153
The name is not validated in this function.
152154
True is returned if the user canceled the action.
@@ -176,6 +178,20 @@ def load(name, include=None, exclude=None, overwrite_unsaved_configuration=False
176178
except Exception as e:
177179
logger.error('Refusing to overwrite unsaved configuration')
178180
raise
181+
if restart:
182+
restart_list = []
183+
subprocess.run(
184+
['kquitapp5', 'plasmashell'],
185+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
186+
)
187+
try:
188+
# Check if Latte Dock is running
189+
subprocess.run(['ps', '-C', 'latte-dock'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
190+
except subprocess.CalledProcessError:
191+
logger.info('No running instance of Latte detected')
192+
else:
193+
subprocess.run(['kquitapp5', 'lattedock'])
194+
restart_list.append('latte-dock')
179195
constants.CURRENT_PROFILE_PATH.unlink(missing_ok=True)
180196
for path in map(lambda p: Path(p).relative_to(Path.home()), paths_to_save(include, exclude)):
181197
source = profile_root / path
@@ -184,6 +200,25 @@ def load(name, include=None, exclude=None, overwrite_unsaved_configuration=False
184200
else:
185201
logger.info(f'The file {source} doesn\'t exist. Skipping\n')
186202
shutil.copyfile(profile_root / constants.PROFILE_INFO_FILENAME, constants.CURRENT_PROFILE_PATH)
203+
if restart:
204+
subprocess.run(
205+
['kstart5', 'plasmashell'],
206+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
207+
)
208+
try:
209+
# Check if Kwin is running
210+
subprocess.run(['ps', '-C', 'kwin_x11'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
211+
except subprocess.CalledProcessError:
212+
logger.info('No running instance of KWin detected')
213+
else:
214+
# If so, reload Kwin
215+
subprocess.run(
216+
['dbus-send', '--session', '--dest=org.kde.KWin', '/KWin', 'org.kde.KWin.reloadConfig'],
217+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
218+
)
219+
if 'latte-dock' in restart_list:
220+
time.sleep(3) # Allow KWin to completely restart
221+
subprocess.run(['kstart5', 'latte-dock'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
187222

188223

189224
def change(results, profile=None):

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = konfsave
3-
version = 0.3.2
3+
version = 0.3.3
44
author = selplacei
55
author_email = [email protected]
66
license = Apache 2.0

0 commit comments

Comments
 (0)