|
1 #!/usr/bin/env python3 |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # Copyright (c) 2002 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
5 # |
|
6 |
|
7 """ |
|
8 eric Web Browser. |
|
9 |
|
10 This is the main Python script that performs the necessary initialization |
|
11 of the web browser and starts the Qt event loop. This is a standalone version |
|
12 of the integrated web browser. It is based on QtWebEngine. |
|
13 """ |
|
14 |
|
15 import sys |
|
16 import os |
|
17 |
|
18 sys.path.insert(1, os.path.dirname(__file__)) |
|
19 |
|
20 app = None |
|
21 SettingsDir = None |
|
22 |
|
23 for arg in sys.argv[:]: |
|
24 if arg.startswith("--config="): |
|
25 import Globals |
|
26 configDir = arg.replace("--config=", "") |
|
27 Globals.setConfigDir(configDir) |
|
28 sys.argv.remove(arg) |
|
29 elif arg.startswith("--settings="): |
|
30 from PyQt6.QtCore import QSettings |
|
31 SettingsDir = os.path.expanduser(arg.replace("--settings=", "")) |
|
32 if not os.path.isdir(SettingsDir): |
|
33 os.makedirs(SettingsDir) |
|
34 QSettings.setPath( |
|
35 QSettings.Format.IniFormat, QSettings.Scope.UserScope, SettingsDir) |
|
36 sys.argv.remove(arg) |
|
37 |
|
38 try: |
|
39 from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__ |
|
40 except ImportError: |
|
41 if "--quiet" not in sys.argv: |
|
42 from PyQt6.QtCore import QTimer |
|
43 from PyQt6.QtWidgets import QApplication |
|
44 from EricWidgets import EricMessageBox # __IGNORE_WARNING__ |
|
45 app = QApplication([]) |
|
46 QTimer.singleShot(0, lambda: EricMessageBox.critical( |
|
47 None, |
|
48 "eric Web Browser", |
|
49 "QtWebEngineWidgets is not installed but needed to execute the" |
|
50 " web browser.")) |
|
51 app.exec() |
|
52 sys.exit(100) |
|
53 |
|
54 from PyQt6.QtWebEngineCore import QWebEngineUrlScheme |
|
55 |
|
56 import Globals |
|
57 from Globals import AppInfo |
|
58 |
|
59 from EricWidgets.EricApplication import EricApplication |
|
60 |
|
61 from Toolbox import Startup |
|
62 |
|
63 from WebBrowser.WebBrowserSingleApplication import ( |
|
64 WebBrowserSingleApplicationClient |
|
65 ) |
|
66 |
|
67 |
|
68 def createMainWidget(argv): |
|
69 """ |
|
70 Function to create the main widget. |
|
71 |
|
72 @param argv list of command line parameters |
|
73 @type list of str |
|
74 @return reference to the main widget |
|
75 @rtype QWidget |
|
76 """ |
|
77 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
78 |
|
79 searchWord = None |
|
80 private = False |
|
81 qthelp = False |
|
82 single = False |
|
83 name = "" |
|
84 |
|
85 for arg in reversed(argv): |
|
86 if arg.startswith("--search="): |
|
87 searchWord = argv[1].split("=", 1)[1] |
|
88 argv.remove(arg) |
|
89 elif arg.startswith("--name="): |
|
90 name = arg.replace("--name=", "") |
|
91 argv.remove(arg) |
|
92 elif arg == "--private": |
|
93 private = True |
|
94 argv.remove(arg) |
|
95 elif arg == "--qthelp": |
|
96 qthelp = True |
|
97 argv.remove(arg) |
|
98 elif arg == "--single": |
|
99 single = True |
|
100 argv.remove(arg) |
|
101 elif ( |
|
102 arg.startswith(("--newtab=", "--")) or |
|
103 arg == "--quiet" |
|
104 ): |
|
105 # only needed until we reach this point |
|
106 argv.remove(arg) |
|
107 |
|
108 try: |
|
109 home = argv[1] |
|
110 except IndexError: |
|
111 home = "" |
|
112 |
|
113 browser = WebBrowserWindow(home, '.', None, 'web_browser', |
|
114 searchWord=searchWord, private=private, |
|
115 settingsDir=SettingsDir, qthelp=qthelp, |
|
116 single=single, saname=name) |
|
117 return browser |
|
118 |
|
119 |
|
120 def main(): |
|
121 """ |
|
122 Main entry point into the application. |
|
123 """ |
|
124 global app |
|
125 |
|
126 from PyQt6.QtGui import QGuiApplication |
|
127 QGuiApplication.setDesktopFileName("eric7_browser.desktop") |
|
128 |
|
129 options = [ |
|
130 ("--config=configDir", |
|
131 "use the given directory as the one containing the config files"), |
|
132 ("--private", "start the browser in private browsing mode"), |
|
133 ("--qthelp", "start the browser with support for QtHelp"), |
|
134 ("--quiet", "don't show any startup error messages"), |
|
135 ("--search=word", "search for the given word"), |
|
136 ("--settings=settingsDir", |
|
137 "use the given directory to store the settings files"), |
|
138 ("--single", "start the browser as a single application"), |
|
139 ] |
|
140 appinfo = AppInfo.makeAppInfo(sys.argv, |
|
141 "eric Web Browser", |
|
142 "file", |
|
143 "web browser", |
|
144 options) |
|
145 |
|
146 # set the library paths for plugins |
|
147 Startup.setLibraryPaths() |
|
148 |
|
149 scheme = QWebEngineUrlScheme(b"eric") |
|
150 scheme.setSyntax(QWebEngineUrlScheme.Syntax.Path) |
|
151 scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme | |
|
152 QWebEngineUrlScheme.Flag.ContentSecurityPolicyIgnored) |
|
153 QWebEngineUrlScheme.registerScheme(scheme) |
|
154 if "--qthelp" in sys.argv: |
|
155 scheme = QWebEngineUrlScheme(b"qthelp") |
|
156 scheme.setSyntax(QWebEngineUrlScheme.Syntax.Path) |
|
157 scheme.setFlags(QWebEngineUrlScheme.Flag.SecureScheme) |
|
158 QWebEngineUrlScheme.registerScheme(scheme) |
|
159 |
|
160 app = EricApplication(sys.argv) |
|
161 if "--private" not in sys.argv: |
|
162 client = WebBrowserSingleApplicationClient() |
|
163 res = client.connect() |
|
164 if res > 0: |
|
165 if len(sys.argv) > 1: |
|
166 client.processArgs(sys.argv[1:]) |
|
167 sys.exit(0) |
|
168 elif res < 0: |
|
169 print("eric7_browser: {0}".format(client.errstr())) |
|
170 # __IGNORE_WARNING_M801__ |
|
171 sys.exit(res) |
|
172 |
|
173 res = Startup.simpleAppStartup(sys.argv, |
|
174 appinfo, |
|
175 createMainWidget, |
|
176 installErrorHandler=True, |
|
177 app=app) |
|
178 sys.exit(res) |
|
179 |
|
180 if __name__ == '__main__': |
|
181 main() |