1#!/usr/local/bin/python3.8
2
3import fileinput
4import configparser
5import os.path
6import subprocess
7
8# Get the config standard locations
9config_locations = subprocess.check_output(['qtpaths', '--paths', 'ConfigLocation']).decode('utf-8').strip().split(':')
10config_paths = [os.path.join(folder, 'kwinrc') for folder in config_locations]
11
12# Get the desktops information from `kwinrc` config file
13kwinrc = configparser.ConfigParser(strict=False, allow_no_value=True)
14kwinrc.read(config_paths)
15
16num_desktops = int(kwinrc.get('Desktops', 'Number', fallback=''))
17
18# Generete the map from x11ids (ennumeration) to UUIDs
19desktopUUIDs = { -1: "" } # NET::OnAllDesktops -> Empty string
20for i in range(1, num_desktops + 1):
21    uuid = kwinrc.get('Desktops', f'Id_{i}', fallback='')
22    desktopUUIDs[i] = str(uuid)
23
24# Apply the conversion to `kwinrulesrc`
25for line in fileinput.input():
26    # Rename key `desktoprule` to `desktopsrule`
27    if line.startswith("desktoprule="):
28        value = line[len("desktoprule="):].strip()
29        print("desktopsrule=" + value)
30        print("# DELETE desktoprule")
31        continue
32
33    if not line.startswith("desktop="):
34        print(line.strip())
35        continue
36
37    # Convert key `desktop` (x11id) to `desktops` (list of UUIDs)
38    try:
39        value = line[len("desktop="):].strip()
40        x11id = int(value)
41        uuid = desktopUUIDs[x11id]
42    except:
43        print(line.strip())  # If there is some error, print back the line
44        continue
45
46    print("desktops=" + uuid)
47    print("# DELETE desktop")
48