summarylogtreecommitdiffstats
path: root/app-main-argv.patch
blob: 93690b9e98e6ee45d76f206c9d29fe2a8c1c0cb2 (plain)
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
diff --git a/app/main.py b/app/main.py
index 39f596a..4d516ad 100644
--- a/app/main.py
+++ b/app/main.py
@@ -2,6 +2,7 @@ import sys
 import os
 import locale
 from pathlib import Path
+from PySide6.QtCore import QUrl
 from PySide6.QtWidgets import QApplication
 from PySide6.QtGui import QIcon
 from app.window import BrowserWindow
@@ -29,6 +30,65 @@ def resolve_icon_path() -> Path | None:
     return None
 
 
+def _to_startup_url(value: str) -> QUrl | None:
+    if not value:
+        return None
+
+    value = value.strip().strip('"').strip("'")
+    if not value or value in ("%u", "%U"):
+        return None
+
+    expanded = os.path.expanduser(value)
+    if os.path.exists(expanded):
+        return QUrl.fromLocalFile(os.path.abspath(expanded))
+
+    if value.startswith(("/", "./", "../", "~/")):
+        abs_path = os.path.abspath(os.path.expanduser(value))
+        return QUrl.fromLocalFile(abs_path)
+
+    if value.startswith(("http://", "https://", "ftp://", "file://")):
+        direct = QUrl(value)
+        if direct.isValid() and not direct.isEmpty():
+            return direct
+
+    candidate = QUrl.fromUserInput(value)
+    if candidate.isValid() and not candidate.isEmpty():
+        return candidate
+
+    return None
+
+
+def collect_startup_urls(args: list[str]) -> list[QUrl]:
+    urls: list[QUrl] = []
+    seen: set[str] = set()
+    parse_all = False
+
+    for arg in args:
+        if not arg:
+            continue
+
+        value = arg
+        if arg == "--":
+            parse_all = True
+            continue
+
+        if not parse_all and arg.startswith("-"):
+            # Ignore CLI switches from launchers/desktop environments.
+            continue
+
+        url = _to_startup_url(value)
+        if url is None:
+            continue
+
+        key = url.toString()
+        if key in seen:
+            continue
+        seen.add(key)
+        urls.append(url)
+
+    return urls
+
+
 def main():
     # Устанавливаем локаль UTF-8
     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
@@ -52,6 +112,13 @@ def main():
     if icon_path is not None:
         window.setWindowIcon(QIcon(str(icon_path)))
 
+    startup_urls = collect_startup_urls(sys.argv[1:])
+    if startup_urls:
+        window.open_url_in_current_tab(startup_urls[0])
+        for extra_url in startup_urls[1:]:
+            window.add_tab()
+            window.open_url_in_current_tab(extra_url)
+
     window.show()
     sys.exit(app.exec())