-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoberLauncher.py
More file actions
1115 lines (923 loc) · 45 KB
/
Copy pathSoberLauncher.py
File metadata and controls
1115 lines (923 loc) · 45 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import sys
import os
import subprocess
import shutil
import json
import re
from PyQt6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFileDialog,
QLineEdit, QMessageBox, QInputDialog, QLabel, QDialog, QSizePolicy, QListWidget,
QAbstractItemView, QCheckBox, QDialogButtonBox, QTabWidget, QMenu
)
from PyQt6.QtGui import QIcon, QPixmap, QPalette, QColor, QBrush
from PyQt6.QtCore import QThread, pyqtSignal, QTimer, Qt
__version__ = "Release V1.5"
class UpdateThread(QThread):
update_failed = pyqtSignal(str)
update_success = pyqtSignal()
def run(self):
try:
subprocess.run(
["python3", os.path.join(os.path.dirname(__file__), "update.py")],
check=True
)
self.update_success.emit()
except subprocess.CalledProcessError as e:
self.update_failed.emit(str(e))
def __init__(self):
super().__init__()
class CreateProfileDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Create Profile")
layout = QVBoxLayout(self)
self.name_input = QLineEdit(self)
self.name_input.setPlaceholderText("Enter the profile name")
layout.addWidget(QLabel("Profile Name:"))
layout.addWidget(self.name_input)
self.copy_checkbox = QCheckBox(
"Copy the main profile's folder (will make Roblox immediately available without having to redownload it after)",
self
)
layout.addWidget(self.copy_checkbox)
self.buttons = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel,
self
)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
layout.addWidget(self.buttons)
def getData(self):
return self.name_input.text().strip(), self.copy_checkbox.isChecked()
class SoberLauncher(QWidget):
def __init__(self):
super().__init__()
# État
self.base_dir = None
self.profiles = []
self.selected_profiles = []
self.processes = {} # profile_name -> subprocess.Popen
self.launched_profiles = set() # profils lancés durant cette session
self.settings_json = "SL_Settings.json"
self.legacy_settings_txt = "SL_Settings.txt"
self.legacy_last_dir_txt = "last_directory.txt"
# Réglages
self.display_name = "[Name]"
self.privateServers = [] # liste de tuples (name, parameter)
self.roblox_player_enabled = False # toggle Roblox Player tab (default off)
self.allow_multi_instance = False # default OFF
# Internal UI refs
self.instances_layout = None
self.bottom_layout_added = False
# Charger réglages (JSON + migration auto)
self.loadSettings()
# UI
self.initUI()
# Charger profils si base_dir connu
self.scanForProfiles()
# Timer pour checker les processus
self.process_timer = QTimer(self)
self.process_timer.timeout.connect(self.checkProcesses)
self.process_timer.start(2000)
# ------------- Réglages (JSON + migration) -------------
def loadSettings(self):
data = {}
# 1) Si JSON existe, on charge
if os.path.exists(self.settings_json):
try:
with open(self.settings_json, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
data = {}
# 2) Sinon, on tente migration depuis SL_Settings.txt + last_directory.txt
else:
base_dir = None
name = "[Name]"
servers = []
# Lire last_directory.txt (hérité)
if os.path.exists(self.legacy_last_dir_txt):
try:
with open(self.legacy_last_dir_txt, "r", encoding="utf-8") as f:
base_dir = os.path.abspath(f.read().strip()) or None
except Exception:
base_dir = None
# Lire SL_Settings.txt (hérité)
if os.path.exists(self.legacy_settings_txt):
try:
with open(self.legacy_settings_txt, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f.readlines()]
for line in lines:
if line.startswith("last_directory="):
v = line[len("last_directory="):].strip()
base_dir = os.path.abspath(v) if v else base_dir
elif line.startswith("Name="):
name = line[len("Name="):] or "[Name]"
elif line.startswith("PrivateServers="):
raw = line[len("PrivateServers="):]
if raw:
for s in raw.split(","):
if "|" in s:
n, p = s.split("|", 1)
servers.append({"name": n, "parameter": p})
except Exception:
pass
data = {
"last_directory": base_dir,
"Name": name,
"PrivateServers": servers,
"roblox_player_enabled": False,
"AllowMultiInstance": False,
}
try:
with open(self.settings_json, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception:
pass
# Appliquer
self.base_dir = data.get("last_directory") or None
self.display_name = data.get("Name", self.display_name)
normalized = []
for item in data.get("PrivateServers", []):
if isinstance(item, dict) and "name" in item and "parameter" in item:
normalized.append((item["name"], item["parameter"]))
elif isinstance(item, (list, tuple)) and len(item) == 2:
normalized.append((item[0], item[1]))
self.privateServers = normalized
self.roblox_player_enabled = bool(data.get("roblox_player_enabled", False))
self.allow_multi_instance = bool(data.get("AllowMultiInstance", False))
def saveSettings(self):
data = {
"last_directory": self.base_dir,
"Name": self.display_name,
"PrivateServers": [{"name": n, "parameter": p} for (n, p) in self.privateServers],
"roblox_player_enabled": self.roblox_player_enabled,
"AllowMultiInstance": self.allow_multi_instance,
}
try:
with open(self.settings_json, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to save settings: {e}")
# ------------- Profils / Processus -------------
def createProfile(self):
if not self.base_dir:
QMessageBox.warning(self, "Error", "Please select a base directory first.")
return
dialog = CreateProfileDialog(self)
if dialog.exec() == QDialog.DialogCode.Accepted:
profile_name, copy_main = dialog.getData()
if not profile_name:
QMessageBox.warning(self, "Error", "Enter a valid profile name.")
return
profile_path = os.path.join(self.base_dir, profile_name)
local_path = os.path.join(profile_path, ".local")
try:
os.makedirs(profile_path, exist_ok=True)
os.makedirs(local_path, exist_ok=True)
if copy_main:
import getpass
user = getpass.getuser()
src = f"/home/{user}/.var/app/org.vinegarhq.Sober/"
dst_parent = os.path.join(profile_path, ".var/app/")
os.makedirs(dst_parent, exist_ok=True)
subprocess.run(["cp", "-r", src, dst_parent], check=True)
appdata_path = os.path.join(dst_parent, "org.vinegarhq.Sober/data/sober/appData")
if os.path.exists(appdata_path):
subprocess.run(["rm", "-rf", appdata_path], check=True)
self.scanForProfiles()
QMessageBox.information(self, "Profile Created", f"Profile '{profile_name}' created successfully!")
except Exception as e:
QMessageBox.warning(self, "Error", f"Failed to create profile directory: {e}")
def system_sober_running(self) -> bool:
"""
Return True if any org.vinegarhq.Sober instance is running on the system,
even if it was not launched from this app.
"""
# Try flatpak ps first
try:
res = subprocess.run(["flatpak", "ps"], capture_output=True, text=True)
if res.returncode == 0 and "org.vinegarhq.Sober" in res.stdout:
return True
except Exception:
pass
# Fallback to pgrep on the command line
try:
res = subprocess.run(["pgrep", "-af", "flatpak run org.vinegarhq.Sober"], capture_output=True, text=True)
if res.returncode == 0 and res.stdout.strip():
return True
except Exception:
pass
# Last resort: grep the process list
try:
res = subprocess.run(["ps", "-eo", "pid,cmd"], capture_output=True, text=True)
if res.returncode == 0 and "org.vinegarhq.Sober" in res.stdout:
return True
except Exception:
pass
return False
def _guard_multi_instance(self, requested_count: int = 1):
"""
Guard against launching multiple instances when disabled.
requested_count: how many instances the user is attempting to launch.
"""
if not self.allow_multi_instance:
if requested_count > 1:
QMessageBox.warning(self, "Error", "A Profile is already running, try closing it before opening a new one")
return False
if self.system_sober_running():
QMessageBox.warning(self, "Error", "A Profile is already running, try closing it before opening a new one")
return False
return True
def launchGame(self):
if not self.selected_profiles:
QMessageBox.warning(self, "Error", "No profiles selected.")
return
# When multi-instance is disabled, block multi-selection entirely
requested = len(self.selected_profiles) if self.allow_multi_instance else 1
if not self._guard_multi_instance(requested_count=requested):
return
targets = self.selected_profiles if self.allow_multi_instance else [self.selected_profiles[0]]
for profile in targets:
if profile in self.processes and self.processes[profile].poll() is None:
continue # already running from this launcher
if profile == "Main Profile":
proc = subprocess.Popen("flatpak run org.vinegarhq.Sober", shell=True)
else:
profile_path = os.path.join(self.base_dir, profile)
command = f'env HOME="{profile_path}" flatpak run org.vinegarhq.Sober'
proc = subprocess.Popen(command, shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
def checkProcesses(self):
closed = [p for p, proc in self.processes.items() if proc.poll() is not None]
for p in closed:
del self.processes[p]
self.updateMissingInstancesLabel()
def runWithConsole(self):
if not self.selected_profiles:
QMessageBox.warning(self, "Error", "No profiles selected.")
return
requested = len(self.selected_profiles) if self.allow_multi_instance else 1
if not self._guard_multi_instance(requested_count=requested):
return
terminal_command = None
if shutil.which("konsole"):
terminal_command = "konsole -e"
elif shutil.which("x-terminal-emulator"):
terminal_command = "x-terminal-emulator -e"
elif shutil.which("gnome-terminal"):
terminal_command = "gnome-terminal --"
else:
QMessageBox.critical(self, "Error", "No compatible terminal emulator found.")
return
targets = self.selected_profiles if self.allow_multi_instance else [self.selected_profiles[0]]
for profile in targets:
if profile in self.processes and self.processes[profile].poll() is None:
continue
if profile == "Main Profile":
proc = subprocess.Popen(f"{terminal_command} flatpak run org.vinegarhq.Sober", shell=True)
else:
profile_path = os.path.join(self.base_dir, profile)
command = f'{terminal_command} env HOME="{profile_path}" flatpak run org.vinegarhq.Sober'
proc = subprocess.Popen(command, shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
def runSpecificGame(self):
if not self.selected_profiles:
QMessageBox.warning(self, "Error", "No profiles selected.")
return
requested = len(self.selected_profiles) if self.allow_multi_instance else 1
if not self._guard_multi_instance(requested_count=requested):
return
url, ok = QInputDialog.getText(self, "Game Link", "Enter the game link:")
if ok and url.strip():
match = re.search(r"games/(\d+)", url.strip())
if not match:
QMessageBox.warning(self, "Error", "Invalid Roblox game link.")
return
place_id = match.group(1)
roblox_command = f'roblox://experience?placeId={place_id}'
targets = self.selected_profiles if self.allow_multi_instance else [self.selected_profiles[0]]
for profile in targets:
if profile in self.processes and self.processes[profile].poll() is None:
continue
if profile == "Main Profile":
proc = subprocess.Popen(f'flatpak run org.vinegarhq.Sober "{roblox_command}"', shell=True)
else:
profile_path = os.path.join(self.base_dir, profile)
command = f'env HOME="{profile_path}" flatpak run org.vinegarhq.Sober "{roblox_command}"'
proc = subprocess.Popen(command, shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
def scanForProfiles(self):
self.profileList.clear()
profiles = []
if self.base_dir and os.path.exists(self.base_dir):
with os.scandir(self.base_dir) as entries:
for entry in entries:
if entry.is_dir():
local_path = os.path.join(entry.path, ".local")
if os.path.exists(local_path) and os.path.isdir(local_path):
profiles.append(entry.name)
def natural_sort_key(s):
return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)]
profiles.sort(key=natural_sort_key)
if "Main Profile" in profiles:
profiles.remove("Main Profile")
profiles.insert(0, "Main Profile")
self.profileList.addItems(profiles)
self.updateMissingInstancesLabel(profiles)
def updateMissingInstancesLabel(self, profiles=None):
# If multi-instancing disabled, do nothing (UI hidden)
if not self.allow_multi_instance:
return
if not hasattr(self, "missingInstancesLabel"):
return
running = list(self.processes.keys())
missing = [p for p in self.launched_profiles if p not in running]
if missing:
text = "Launched instances not running: " + ", ".join(missing)
else:
text = "Launched instances not running: None"
font = self.missingInstancesLabel.font()
base_size = 12
max_len = 60
if len(text) > max_len:
font.setPointSize(max(base_size - (len(text) - max_len) // 8, 7))
else:
font.setPointSize(base_size)
self.missingInstancesLabel.setFont(font)
self.missingInstancesLabel.setText(text)
# Highlight missing profiles in blue in the list
self.colorizeMissingProfiles(missing)
def colorizeMissingProfiles(self, missing):
# Determine default text color from current palette (to restore non-missing)
default_color = self.palette().color(QPalette.ColorRole.WindowText)
for i in range(self.profileList.count()):
item = self.profileList.item(i)
if self.allow_multi_instance and item.text() in missing:
item.setForeground(QBrush(QColor("#1e3a8a"))) # dark blue
else:
item.setForeground(QBrush(default_color))
def runMissingInstances(self):
if not self.allow_multi_instance:
QMessageBox.information(self, "Info", "Multi instancing is disabled.")
return
running = list(self.processes.keys())
missing = [p for p in self.launched_profiles if p not in running]
if not missing:
QMessageBox.information(self, "Info", "No missing instances to run.")
return
for profile in missing:
if profile == "Main Profile":
proc = subprocess.Popen("flatpak run org.vinegarhq.Sober", shell=True)
else:
profile_path = os.path.join(self.base_dir, profile)
command = f'env HOME="{profile_path}" flatpak run org.vinegarhq.Sober'
proc = subprocess.Popen(command, shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
def exitAllSessions(self):
result = QMessageBox.question(
self, "Confirm Exit",
"Do you want to force-close all Sober sessions?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
if result == QMessageBox.StandardButton.Yes:
subprocess.run("flatpak kill org.vinegarhq.Sober", shell=True)
self.launched_profiles.clear()
self.updateMissingInstancesLabel()
QMessageBox.information(self, "Exit", "All Sober sessions have been forcibly closed.")
# ------------- À propos / Update -------------
def showAbout(self):
dialog = QDialog(self)
dialog.setWindowTitle("About Sober Launcher")
layout = QVBoxLayout()
icon_label = QLabel()
icon_label.setPixmap(QPixmap("SoberLauncher.svg"))
title_label = QLabel("<b>Sober Launcher</b><br>An easy launcher to control all your Sober Instances<br><br><i>Author: Taboulet</i>")
version_label = QLabel(f"<b>Current Version:</b> {__version__}")
layout.addWidget(icon_label)
layout.addWidget(title_label)
layout.addWidget(version_label)
# Toggle for Roblox Player stuff
toggle_row = QHBoxLayout()
toggle_label = QLabel("Activate Roblox Player stuff")
self.robloxToggle = QCheckBox()
self.robloxToggle.setChecked(self.roblox_player_enabled)
self.robloxToggle.stateChanged.connect(self.onRobloxToggleChanged)
toggle_row.addWidget(toggle_label)
toggle_row.addWidget(self.robloxToggle)
toggle_row.addStretch(1)
layout.addLayout(toggle_row)
# Toggle for Multi Instancing (broken)
multi_row = QHBoxLayout()
multi_label = QLabel("Enable Multi Instancing (broken)")
self.multiToggle = QCheckBox()
self.multiToggle.setChecked(self.allow_multi_instance)
self.multiToggle.stateChanged.connect(self.onMultiToggleChanged)
multi_row.addWidget(multi_label)
multi_row.addWidget(self.multiToggle)
multi_row.addStretch(1)
layout.addLayout(multi_row)
update_button = QPushButton("Update")
update_button.clicked.connect(self.runUpdateScript)
layout.addWidget(update_button)
dialog.setLayout(layout)
dialog.exec()
def onRobloxToggleChanged(self, state):
self.roblox_player_enabled = (state == Qt.CheckState.Checked.value)
self.saveSettings()
self.updateRobloxTabVisibility()
def onMultiToggleChanged(self, state):
self.allow_multi_instance = (state == Qt.CheckState.Checked.value)
self.saveSettings()
# Update UI to reflect mode immediately and restore/hide bottom layout correctly
self.applyMultiInstanceUIState()
def updateRobloxTabVisibility(self):
# Add or remove the Roblox Player tab depending on the toggle
idx = self.main_tab_widget.indexOf(self.roblox_tab)
if self.roblox_player_enabled:
if idx == -1:
self.main_tab_widget.addTab(self.roblox_tab, "Roblox Player")
else:
if idx != -1:
self.main_tab_widget.removeTab(idx)
# Hide the tab bar when only one tab is visible (no wasted space)
show_tabs = self.main_tab_widget.count() > 1
self.main_tab_widget.tabBar().setVisible(show_tabs)
# Keep focus on the remaining tab
if self.main_tab_widget.count() >= 1 and self.main_tab_widget.currentIndex() == -1:
self.main_tab_widget.setCurrentIndex(0)
def runUpdateScript(self):
self.update_thread = UpdateThread()
self.update_thread.update_failed.connect(lambda error: QMessageBox.critical(self, "Error", f"Failed to run update script: {error}"))
self.update_thread.update_success.connect(lambda: QMessageBox.information(self, "Update", "Update completed successfully."))
self.update_thread.start()
# ------------- Crash windows -------------
def removeCrashWindows(self):
try:
result = subprocess.run(
["xdotool", "search", "--name", "Crash"], capture_output=True, text=True
)
if result.returncode != 0 or not result.stdout.strip():
QMessageBox.information(self, "Info", "No 'Crash' windows found.")
return
window_ids = result.stdout.strip().split("\n")
for window_id in window_ids:
subprocess.run(["xdotool", "windowkill", window_id])
except FileNotFoundError:
QMessageBox.critical(
self, "Error", "The 'xdotool' command is not available. Please ensure it is installed."
)
# ------------- Lancement via lien pour manquants -------------
def runMissingInstancesWithLink(self):
if not self.allow_multi_instance:
QMessageBox.information(self, "Info", "Multi instancing is disabled.")
return
running = list(self.processes.keys())
missing = [p for p in self.launched_profiles if p not in running]
if not missing:
QMessageBox.information(self, "Info", "No missing instances to run.")
return
url, ok = QInputDialog.getText(self, "Game Link", "Enter the game link for all missing instances:")
if not (ok and url.strip()):
return
match = re.search(r"games/(\d+)", url.strip())
if not match:
QMessageBox.warning(self, "Error", "Invalid Roblox game link.")
return
place_id = match.group(1)
roblox_command = f'roblox://experience?placeId={place_id}'
for profile in missing:
if profile == "Main Profile":
proc = subprocess.Popen(f'flatpak run org.vinegarhq.Sober "{roblox_command}"', shell=True)
else:
profile_path = os.path.join(self.base_dir, profile)
command = f'env HOME="{profile_path}" flatpak run org.vinegarhq.Sober "{roblox_command}"'
proc = subprocess.Popen(command, shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
def launchMainProfile(self):
# Count any external Sober instances as running
if not self._guard_multi_instance(requested_count=1):
return
profile = "Main Profile"
if self.allow_multi_instance and profile in self.processes and self.processes[profile].poll() is None:
QMessageBox.information(self, "Info", "Main Profile is already running.")
return
proc = subprocess.Popen("flatpak run org.vinegarhq.Sober", shell=True)
self.processes[profile] = proc
self.launched_profiles.add(profile)
self.updateMissingInstancesLabel()
# ------------- Fix selected profiles (any state) -------------
def fixSelectedProfiles(self):
targets = list(self.selected_profiles)
if not targets:
QMessageBox.information(self, "Info", "Select at least one profile to fix.")
return
# Ask for fix method
msg = QMessageBox(self)
msg.setWindowTitle("Fix Profiles")
msg.setText("Which fix method would you prefer?")
delete_btn = msg.addButton("Delete local files (keeps the data, normally)", QMessageBox.ButtonRole.AcceptRole)
exit_btn = msg.addButton("Exit", QMessageBox.ButtonRole.RejectRole)
msg.setIcon(QMessageBox.Icon.Question)
msg.exec()
if msg.clickedButton() != delete_btn:
return
errors = []
for profile in targets:
try:
self.deleteLocalFilesForProfile(profile)
except Exception as e:
errors.append(f"{profile}: {e}")
if errors:
QMessageBox.warning(self, "Fix Completed with Errors", "Some profiles could not be fully fixed:\n- " + "\n- ".join(errors))
else:
QMessageBox.information(self, "Fix Completed", "Selected profiles were fixed successfully.")
def deleteLocalFilesForProfile(self, profile):
# Compute org.vinegarhq.Sober dir for the profile
if profile == "Main Profile":
home = os.path.expanduser("~")
org_dir = os.path.join(home, ".var", "app", "org.vinegarhq.Sober")
else:
if not self.base_dir:
raise RuntimeError("Base directory is not set.")
profile_path = os.path.join(self.base_dir, profile)
org_dir = os.path.join(profile_path, ".var", "app", "org.vinegarhq.Sober")
to_delete = [".ld.so", ".local", "cache"]
for name in to_delete:
path = os.path.join(org_dir, name)
if os.path.exists(path):
try:
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
else:
os.remove(path)
except Exception as e:
raise RuntimeError(f"Failed to delete {name}: {e}")
# ------------- Nom affiché -------------
def editDisplayName(self):
name, ok = QInputDialog.getText(self, "Edit Name", "Enter your name:", text=self.display_name)
if ok and name.strip():
self.display_name = name.strip()
if hasattr(self, "displayNameLabel"):
self.displayNameLabel.setText(f"Hi, {self.display_name}")
self.saveSettings()
def loadDisplayName(self):
if hasattr(self, "displayNameLabel"):
self.displayNameLabel.setText(f"Hi, {self.display_name}")
# ------------- Serveurs privés -------------
def addPrivateServer(self):
name, ok1 = QInputDialog.getText(self, "Private Server Name", "Enter a name for the private server:")
if not ok1 or not name.strip():
return
parameter, ok2 = QInputDialog.getText(self, "Parameter", "Enter the parameter:")
if not ok2 or not parameter.strip():
return
name = name.strip()
parameter = parameter.strip()
self.privateServers.append((name, parameter))
self.saveSettings()
self.refreshPrivateServerButtons()
def addPrivateServerButtonWidget(self, name, parameter):
btn = QPushButton(name)
btn.setMinimumWidth(120)
btn.clicked.connect(lambda: self.runParameter(parameter))
btn.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
btn.customContextMenuRequested.connect(
lambda pos, b=btn, n=name, p=parameter: self.showPrivateServerContextMenu(b, n, p)
)
self.privateServerButtonsLayout.addWidget(btn)
def showPrivateServerContextMenu(self, button, name, parameter):
menu = QMenu()
remove_action = menu.addAction("Remove")
edit_action = menu.addAction("Edit")
action = menu.exec(button.mapToGlobal(button.rect().bottomLeft()))
if action == remove_action:
self.removePrivateServerButton(name)
elif action == edit_action:
self.editPrivateServerButton(name, parameter)
def removePrivateServerButton(self, name):
self.privateServers = [(n, p) for (n, p) in self.privateServers if n != name]
self.saveSettings()
self.refreshPrivateServerButtons()
def editPrivateServerButton(self, old_name, old_parameter):
name, ok1 = QInputDialog.getText(self, "Edit Private Server Name", "Edit the name:", text=old_name)
if not ok1 or not name.strip():
return
parameter, ok2 = QInputDialog.getText(self, "Edit Parameter", "Edit the parameter:", text=old_parameter)
if not ok2 or not parameter.strip():
return
name = name.strip()
parameter = parameter.strip()
updated = []
for (n, p) in self.privateServers:
if n == old_name:
updated.append((name, parameter))
else:
updated.append((n, p))
self.privateServers = updated
self.saveSettings()
self.refreshPrivateServerButtons()
def runParameter(self, parameter):
command = f'flatpak run org.vinegarhq.Sober "{parameter}"'
subprocess.Popen(command, shell=True)
def quickLaunch(self):
parameter, ok = QInputDialog.getText(self, "Parameter", "Enter the parameter:")
if not ok or not parameter.strip():
return
self.runParameter(parameter.strip())
def refreshPrivateServerButtons(self):
# Nettoyer
if not hasattr(self, "privateServerButtonsLayout"):
return
while self.privateServerButtonsLayout.count():
item = self.privateServerButtonsLayout.takeAt(0)
w = item.widget()
if w is not None:
w.deleteLater()
# Recréer
for name, parameter in self.privateServers:
self.addPrivateServerButtonWidget(name, parameter)
# ------------- Profile context menu -> Desktop entry -------------
def showProfileContextMenu(self, pos):
item = self.profileList.itemAt(pos)
if not item:
return
profile = item.text()
menu = QMenu()
add_action = menu.addAction("Add to desktop entry")
action = menu.exec(self.profileList.mapToGlobal(pos))
if action == add_action:
self.createDesktopEntry(profile)
def createDesktopEntry(self, profile):
# Resolve desktop directory; fallback to home if Desktop doesn't exist
home = os.path.expanduser("~")
desktop_dir = os.path.join(home, "Desktop")
target_dir = desktop_dir if os.path.isdir(desktop_dir) else home
os.makedirs(target_dir, exist_ok=True)
filename = os.path.join(target_dir, f"{profile}.desktop")
if profile == "Main Profile":
exec_cmd = "flatpak run org.vinegarhq.Sober"
else:
profile_path = os.path.join(self.base_dir, profile)
exec_cmd = f'env HOME="{profile_path}" flatpak run org.vinegarhq.Sober'
icon_path = os.path.abspath("SoberLauncher.svg")
content = f"""[Desktop Entry]
Type=Application
Name={profile}
Exec={exec_cmd}
Icon={icon_path}
Terminal=false
"""
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
os.chmod(filename, 0o755)
QMessageBox.information(self, "Desktop Entry", f"Created {filename}")
except Exception as e:
QMessageBox.critical(self, "Desktop Entry", f"Failed to create {filename}:\n{e}")
# ------------- UI -------------
def initUI(self):
self.main_tab_widget = QTabWidget()
# Barre globale (placeholder)
global_top_bar = QHBoxLayout()
global_top_bar.addStretch(1)
# ----- Onglet Instances -----
instances_tab = QWidget()
self.instances_layout = QVBoxLayout(instances_tab)
main_layout = QHBoxLayout()
left_layout = QVBoxLayout()
top_bar = QHBoxLayout()
self.selectDirButton = QPushButton("Select Base Directory")
self.selectDirButton.clicked.connect(self.selectDirectory)
top_bar.addWidget(self.selectDirButton)
self.refreshButton = QPushButton()
self.refreshButton.setIcon(QIcon.fromTheme("view-refresh"))
self.refreshButton.setToolTip("Refresh Profiles")
self.refreshButton.clicked.connect(self.scanForProfiles)
top_bar.addWidget(self.refreshButton)
self.createProfileButton = QPushButton("Create Profile")
self.createProfileButton.clicked.connect(self.createProfile)
top_bar.addWidget(self.createProfileButton)
# Exit button label depends on multi-instance
self.exitAllButton = QPushButton("Exit Current Session" if not self.allow_multi_instance else "Exit All Sessions")
self.exitAllButton.clicked.connect(self.exitAllSessions)
top_bar.addWidget(self.exitAllButton)
self.removeCrashButton = QPushButton("Remove Crash")
self.removeCrashButton.clicked.connect(self.removeCrashWindows)
top_bar.addWidget(self.removeCrashButton)
self.aboutButtonInstances = QPushButton("About")
self.aboutButtonInstances.clicked.connect(self.showAbout)
top_bar.addWidget(self.aboutButtonInstances)
left_layout.addLayout(top_bar)
self.profileList = QListWidget()
self.profileList.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
self.profileList.itemSelectionChanged.connect(self.updateSelectedProfiles)
# Add context menu for desktop entry
self.profileList.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.profileList.customContextMenuRequested.connect(self.showProfileContextMenu)
left_layout.addWidget(self.profileList)
right_layout = QVBoxLayout()
right_layout.setContentsMargins(0, 0, 0, 0)
right_layout.setSpacing(10)
self.selectedProfileLabel = QLabel("Selected Profiles: None")
self.selectedProfileLabel.setWordWrap(True)
self.selectedProfileLabel.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Preferred)
right_layout.addWidget(self.selectedProfileLabel)
self.launchButton = QPushButton("Launch Game")
self.launchButton.clicked.connect(self.launchGame)
right_layout.addWidget(self.launchButton)
self.consoleLaunchButton = QPushButton("Run with Console")
self.consoleLaunchButton.clicked.connect(self.runWithConsole)
right_layout.addWidget(self.consoleLaunchButton)
self.runSpecificGameButton = QPushButton("Run Specific Game")
self.runSpecificGameButton.clicked.connect(self.runSpecificGame)
right_layout.addWidget(self.runSpecificGameButton)
# Fix button (works for any selected profiles)
self.fixButton = QPushButton("Fix")
self.fixButton.setToolTip("Fix selected profiles (delete local files)")
self.fixButton.clicked.connect(self.fixSelectedProfiles)
right_layout.addWidget(self.fixButton)
right_panel_widget = QWidget()
right_panel_widget.setLayout(right_layout)
right_panel_widget.setFixedWidth(300)
main_layout.addLayout(left_layout)
main_layout.addWidget(right_panel_widget)
# Bottom layout (missing instances bar)
self.bottom_layout = QHBoxLayout()
self.missingInstancesLabel = QLabel("Instances not running: None")
self.missingInstancesLabel.setWordWrap(True)
self.missingInstancesLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
self.bottom_layout.addWidget(self.missingInstancesLabel)
self.runMissingButton = QPushButton("Run Missing Instances")
self.runMissingButton.clicked.connect(self.runMissingInstances)
self.bottom_layout.addWidget(self.runMissingButton)
self.runMissingWithLinkButton = QPushButton()
self.runMissingWithLinkButton.setIcon(QIcon.fromTheme("internet-web-browser"))
self.runMissingWithLinkButton.setToolTip("Run Missing Instances with Game Link")
self.runMissingWithLinkButton.clicked.connect(self.runMissingInstancesWithLink)
self.bottom_layout.addWidget(self.runMissingWithLinkButton)
# Add main and bottom sections
self.instances_layout.addLayout(main_layout)
# Add bottom layout only when multi-instance is enabled
if self.allow_multi_instance:
self.instances_layout.addLayout(self.bottom_layout)
self.bottom_layout_added = True
else:
self.bottom_layout_added = False
instances_tab.setLayout(self.instances_layout)
# ----- Roblox Player tab (created once; visibility controlled by toggle) -----
self.roblox_tab = QWidget()
roblox_layout = QVBoxLayout()
self.roblox_tab.setLayout(roblox_layout)
roblox_layout.addStretch(2)
name_row = QHBoxLayout()
self.displayNameLabel = QLabel(f"Hi, {self.display_name}")
self.displayNameLabel.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
font = self.displayNameLabel.font()
font.setPointSize(32)
font.setBold(True)
self.displayNameLabel.setFont(font)
name_row.addWidget(self.displayNameLabel)
pencil_btn = QPushButton()
pencil_btn.setIcon(QIcon.fromTheme("document-edit"))
pencil_btn.setFixedSize(32, 32)
pencil_btn.setToolTip("Edit name")
pencil_btn.clicked.connect(self.editDisplayName)
name_row.addWidget(pencil_btn)
name_row.addStretch(1)
roblox_layout.addLayout(name_row)
roblox_layout.addStretch(1)
play_button = QPushButton("Play")
play_button.setFixedHeight(60)
play_button.setStyleSheet("font-size: 20px;")
play_button.clicked.connect(self.launchMainProfile)
roblox_layout.addWidget(play_button, alignment=Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
button_row = QHBoxLayout()
self.addPrivateServerButton = QPushButton("Add private server")
self.addPrivateServerButton.clicked.connect(self.addPrivateServer)
button_row.addWidget(self.addPrivateServerButton)
self.quickLaunchButton = QPushButton("Quick launch")
self.quickLaunchButton.clicked.connect(self.quickLaunch)
button_row.addWidget(self.quickLaunchButton)
self.privateServerButtonsLayout = QHBoxLayout()
button_row.addLayout(self.privateServerButtonsLayout)
button_row.addStretch(1)
roblox_layout.addLayout(button_row)
roblox_layout.addStretch(6)
# Recharger nom et serveurs privés
QTimer.singleShot(0, self.loadDisplayName)
QTimer.singleShot(0, self.refreshPrivateServerButtons)
# Tabs: always add Instances; Roblox tab added conditionally
self.main_tab_widget.addTab(instances_tab, "Instances")
if self.roblox_player_enabled:
self.main_tab_widget.addTab(self.roblox_tab, "Roblox Player")
# Hide tab bar if only one tab visible
self.updateRobloxTabVisibility()
# Layout wrapper
wrapper_layout = QVBoxLayout()