-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftwareCenter.py
More file actions
450 lines (399 loc) · 15.7 KB
/
SoftwareCenter.py
File metadata and controls
450 lines (399 loc) · 15.7 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
# -*- coding: utf-8 -*-
"""SoftwareCenter - Desktop-Organizer für Software-Verknüpfungen."""
__version__ = "1.0.0"
import configparser
import os
import shlex
import subprocess
import sys
from PySide6.QtCore import Qt, QSize, QFileInfo, Signal, QSettings
from PySide6.QtGui import QAction, QActionGroup, QIcon
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QListWidget,
QListWidgetItem, QTabWidget, QFileIconProvider, QToolBar, QInputDialog,
QMessageBox, QMenu
)
def is_linux_desktop_entry(path: str) -> bool:
return sys.platform.startswith("linux") and os.path.isfile(path) and path.lower().endswith(".desktop")
def read_desktop_entry(path: str) -> dict[str, str]:
parser = configparser.ConfigParser(interpolation=None, strict=False)
parser.optionxform = str
try:
with open(path, "r", encoding="utf-8") as handle:
parser.read_file(handle)
except (OSError, configparser.Error, UnicodeDecodeError):
return {}
if not parser.has_section("Desktop Entry"):
return {}
return {key: value for key, value in parser.items("Desktop Entry")}
def desktop_entry_display_name(path: str) -> str | None:
entry = read_desktop_entry(path)
if not entry:
return None
for key in ("Name[de_DE]", "Name[de]", "Name"):
value = entry.get(key)
if value:
return value.strip()
return None
def desktop_entry_icon(path: str) -> QIcon | None:
entry = read_desktop_entry(path)
icon_name = entry.get("Icon", "").strip()
if not icon_name:
return None
if os.path.isabs(icon_name) and os.path.exists(icon_name):
return QIcon(icon_name)
icon = QIcon.fromTheme(icon_name)
if not icon.isNull():
return icon
return None
def desktop_entry_exec_command(path: str) -> list[str] | None:
entry = read_desktop_entry(path)
exec_line = entry.get("Exec", "").strip()
if not exec_line:
return None
cleaned = []
for token in shlex.split(exec_line, posix=True):
if token.startswith("%"):
continue
cleaned.append(token.replace("%%", "%"))
return cleaned or None
def is_supported_launch_target(path: str) -> bool:
if os.path.isfile(path):
return True
return sys.platform == "darwin" and path.lower().endswith(".app") and os.path.isdir(path)
def open_file(path: str) -> None:
if not os.path.exists(path):
QMessageBox.warning(None, "Datei nicht gefunden", f"Pfad existiert nicht:\n{path}")
return
try:
if sys.platform.startswith("win"):
os.startfile(path) # type: ignore[attr-defined]
elif sys.platform == "darwin":
subprocess.Popen(["open", path])
else:
if is_linux_desktop_entry(path):
command = desktop_entry_exec_command(path)
if command:
subprocess.Popen(command)
else:
subprocess.Popen(["xdg-open", path])
elif os.access(path, os.X_OK):
subprocess.Popen([path])
else:
subprocess.Popen(["xdg-open", path])
except Exception as e:
QMessageBox.critical(None, "Fehler beim Starten", f"Konnte nicht starten:\n{path}\n\n{e}")
class SoftwareListWidget(QListWidget):
requestDelete = Signal(list)
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setSelectionMode(QListWidget.ExtendedSelection)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self._on_context_menu)
self.itemActivated.connect(self._on_item_activated)
self._icon_provider = QFileIconProvider()
self.configure_as_tiles()
def configure_as_tiles(self):
self.setViewMode(QListWidget.IconMode)
self.setResizeMode(QListWidget.Adjust)
self.setWrapping(True)
self.setIconSize(QSize(64, 64))
self.setGridSize(QSize(110, 100))
self.setSpacing(8)
self.setUniformItemSizes(False)
def configure_as_list(self):
self.setViewMode(QListWidget.ListMode)
self.setResizeMode(QListWidget.Adjust)
self.setWrapping(False)
self.setIconSize(QSize(24, 24))
self.setGridSize(QSize())
self.setSpacing(2)
self.setUniformItemSizes(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
urls = event.mimeData().urls()
if not urls:
return
paths = []
for url in urls:
if url.isLocalFile():
p = url.toLocalFile()
if is_supported_launch_target(p):
paths.append(p)
if paths:
self.add_paths(paths)
event.acceptProposedAction()
else:
event.ignore()
def _on_context_menu(self, pos):
menu = QMenu(self)
act_open = menu.addAction("Öffnen/Starten")
act_del = menu.addAction("Löschen")
global_pos = self.viewport().mapToGlobal(pos)
action = menu.exec(global_pos)
if action == act_open:
for item in self.selectedItems():
path = item.data(Qt.UserRole)
open_file(path)
elif action == act_del:
paths = [it.data(Qt.UserRole) for it in self.selectedItems()]
if paths:
self.requestDelete.emit(paths)
def _on_item_activated(self, item: QListWidgetItem):
path = item.data(Qt.UserRole)
open_file(path)
def add_paths(self, paths: list[str]):
for path in paths:
if not is_supported_launch_target(path) or self._has_path(path):
continue
self._add_item(path)
def _has_path(self, path: str) -> bool:
for i in range(self.count()):
it = self.item(i)
if it.data(Qt.UserRole) == path:
return True
return False
def _add_item(self, path: str):
info = QFileInfo(path)
icon = desktop_entry_icon(path) if is_linux_desktop_entry(path) else None
if icon is None or icon.isNull():
icon = self._icon_provider.icon(info)
name = desktop_entry_display_name(path) if is_linux_desktop_entry(path) else None
if not name:
name = os.path.splitext(os.path.basename(path))[0]
item = QListWidgetItem(icon, name)
item.setToolTip(path)
item.setData(Qt.UserRole, path)
self.addItem(item)
def remove_paths(self, paths: list[str]):
to_remove = set(paths)
i = 0
while i < self.count():
it = self.item(i)
if it.data(Qt.UserRole) in to_remove:
self.takeItem(i)
else:
i += 1
def get_all_paths(self) -> list[str]:
return [self.item(i).data(Qt.UserRole) for i in range(self.count())]
def set_all_paths(self, paths: list[str]):
for p in paths:
if os.path.exists(p):
self._add_item(p)
class TabPage(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.view_mode = "tiles"
self.list = SoftwareListWidget()
self.list.requestDelete.connect(self.on_request_delete)
layout = QVBoxLayout(self)
layout.setContentsMargins(6, 6, 6, 6)
layout.addWidget(self.list)
def set_view_mode(self, mode: str):
if mode == "tiles":
self.list.configure_as_tiles()
self.view_mode = "tiles"
else:
self.list.configure_as_list()
self.view_mode = "list"
def add_paths(self, paths: list[str]):
self.list.add_paths(paths)
def on_request_delete(self, paths: list[str]):
if not paths:
return
if len(paths) == 1:
msg = f"Diese Verknüpfung entfernen?\n\n{paths[0]}"
else:
msg = f"{len(paths)} Verknüpfungen aus dieser Ansicht entfernen?"
ret = QMessageBox.question(self, "Löschen bestätigen", msg)
if ret == QMessageBox.Yes:
self.list.remove_paths(paths)
class MainWindow(QMainWindow):
def __init__(self, settings: QSettings | None = None):
super().__init__()
self.setWindowTitle("SoftwareCenter")
self.resize(1000, 640)
self.setAcceptDrops(True)
icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon.ico")
if os.path.exists(icon_path):
self.setWindowIcon(QIcon(icon_path))
self.settings = settings or QSettings("LukasGeiger", "SoftwareCenter")
self.tabs = QTabWidget(movable=True, tabsClosable=True)
self.tabs.tabCloseRequested.connect(self.on_close_tab)
self.tabs.tabBarDoubleClicked.connect(self.on_rename_tab)
self.tabs.currentChanged.connect(self._sync_view_actions)
self.setCentralWidget(self.tabs)
self._build_toolbar()
self.load_settings()
def _build_toolbar(self):
tb = QToolBar("Hauptleiste")
tb.setMovable(False)
self.addToolBar(tb)
act_new_tab = QAction("Neuer Tab", self)
act_new_tab.triggered.connect(self.on_new_tab)
tb.addAction(act_new_tab)
act_rename_tab = QAction("Tab umbenennen", self)
act_rename_tab.triggered.connect(self.on_rename_tab_action)
tb.addAction(act_rename_tab)
tb.addSeparator()
self.view_group = QActionGroup(self)
self.view_group.setExclusive(True)
self.act_view_tiles = QAction("Kacheln", self, checkable=True)
self.act_view_list = QAction("Liste", self, checkable=True)
self.view_group.addAction(self.act_view_tiles)
self.view_group.addAction(self.act_view_list)
self.act_view_tiles.setChecked(True)
self.act_view_tiles.triggered.connect(lambda: self.set_current_view("tiles"))
self.act_view_list.triggered.connect(lambda: self.set_current_view("list"))
tb.addAction(self.act_view_tiles)
tb.addAction(self.act_view_list)
def current_page(self) -> TabPage | None:
w = self.tabs.currentWidget()
return w if isinstance(w, TabPage) else None
def _sync_view_actions(self, index: int | None = None):
page = self.current_page()
if not page:
return
is_tiles = page.view_mode == "tiles"
self.act_view_tiles.blockSignals(True)
self.act_view_list.blockSignals(True)
try:
self.act_view_tiles.setChecked(is_tiles)
self.act_view_list.setChecked(not is_tiles)
finally:
self.act_view_tiles.blockSignals(False)
self.act_view_list.blockSignals(False)
def add_new_tab(self, name: str | None = None, view_mode="tiles", paths=None):
page = TabPage()
page.set_view_mode(view_mode)
if paths:
page.list.set_all_paths(paths)
idx = self.tabs.addTab(page, name or "Neuer Tab")
self.tabs.setCurrentIndex(idx)
def on_new_tab(self):
name, ok = QInputDialog.getText(self, "Neuer Tab", "Tab-Name:")
if ok:
name = name.strip() or "Neuer Tab"
self.add_new_tab(name)
self.save_settings() # BUG 6: Settings nach Tab-Erstellung sofort speichern
def on_rename_tab(self, index: int):
if index < 0:
return
current_name = self.tabs.tabText(index)
name, ok = QInputDialog.getText(self, "Tab umbenennen", "Neuer Tab-Name:", text=current_name)
if ok:
name = name.strip() or current_name
self.tabs.setTabText(index, name)
self.save_settings() # BUG 6: Settings nach Umbenennung sofort speichern
def on_rename_tab_action(self):
idx = self.tabs.currentIndex()
if idx >= 0:
self.on_rename_tab(idx)
def on_close_tab(self, index: int):
if self.tabs.count() == 1:
QMessageBox.information(self, "Nicht möglich", "Der letzte Tab kann nicht geschlossen werden.")
return
self.tabs.removeTab(index)
self.save_settings() # BUG 6: Settings nach Tab-Schließen sofort speichern
def set_current_view(self, mode: str):
page = self.current_page()
if not page:
return
page.set_view_mode(mode)
self._sync_view_actions()
self.save_settings()
# ----- Speicherfunktion -----
def save_settings(self):
settings = self.settings
settings.setValue("geometry", self.saveGeometry())
settings.setValue("windowState", self.saveState())
settings.setValue("current_tab", self.tabs.currentIndex())
settings.beginWriteArray("tabs")
for i in range(self.tabs.count()):
settings.setArrayIndex(i)
page = self.tabs.widget(i)
settings.setValue("name", self.tabs.tabText(i))
settings.setValue("view_mode", page.view_mode)
settings.setValue("paths", page.list.get_all_paths())
settings.endArray()
def load_settings(self):
settings = self.settings
if settings.value("geometry"):
self.restoreGeometry(settings.value("geometry"))
if settings.value("windowState"):
self.restoreState(settings.value("windowState"))
current_tab = settings.value("current_tab", -1)
if isinstance(current_tab, str):
try:
current_tab = int(current_tab)
except ValueError:
current_tab = -1
size = settings.beginReadArray("tabs")
if size > 0:
self.tabs.clear()
for i in range(size):
settings.setArrayIndex(i)
name = settings.value("name", "Tab")
view_mode = settings.value("view_mode", "tiles")
paths = settings.value("paths", [])
if isinstance(paths, str): # falls als einzelner String gespeichert
paths = [paths]
self.add_new_tab(name, view_mode, paths)
if isinstance(current_tab, int) and 0 <= current_tab < self.tabs.count():
self.tabs.setCurrentIndex(current_tab)
elif self.tabs.count() > 0:
self.tabs.setCurrentIndex(0)
else:
self.add_new_tab("Allgemein")
settings.endArray()
self._sync_view_actions()
def closeEvent(self, event):
self.save_settings()
super().closeEvent(event)
# ----- Drag & Drop im Hauptfenster -----
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
urls = event.mimeData().urls()
paths = []
for url in urls:
if url.isLocalFile():
p = url.toLocalFile()
if is_supported_launch_target(p):
paths.append(p)
if paths:
page = self.current_page()
if page:
page.add_paths(paths)
self.save_settings()
event.acceptProposedAction()
else:
event.ignore()
def main():
app = QApplication(sys.argv)
icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon.ico")
if os.path.exists(icon_path):
app.setWindowIcon(QIcon(icon_path))
win = MainWindow()
win.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()