Improve local file open fallback
This commit is contained in:
+38
-2
@@ -8,7 +8,7 @@ import re
|
||||
from pathlib import Path
|
||||
from urllib.parse import unquote, urlsplit
|
||||
|
||||
from PySide6.QtCore import QFileSystemWatcher, QSettings, QSignalBlocker, QTimer, QUrl, Qt, Signal, Slot
|
||||
from PySide6.QtCore import QFileSystemWatcher, QProcess, QSettings, QSignalBlocker, QTimer, QUrl, Qt, Signal, Slot
|
||||
from PySide6.QtGui import QAction, QCloseEvent, QDesktopServices
|
||||
from PySide6.QtWebChannel import QWebChannel
|
||||
from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineSettings
|
||||
@@ -589,7 +589,43 @@ class MainWindow(QMainWindow):
|
||||
os.startfile(str(path))
|
||||
except OSError as exc:
|
||||
LOGGER.exception("Failed to open local file: %s", path)
|
||||
QMessageBox.warning(self, "打开失败", f"无法用系统默认程序打开:\n{path}\n\n{exc}")
|
||||
self._offer_select_application_after_open_failure(path, exc)
|
||||
|
||||
def _offer_select_application_after_open_failure(self, path: Path, error: OSError) -> None:
|
||||
message = QMessageBox(self)
|
||||
message.setIcon(QMessageBox.Icon.Warning)
|
||||
message.setWindowTitle("无法用默认程序打开")
|
||||
message.setText("Windows 没有为此文件类型配置可用的默认应用,或默认应用启动失败。")
|
||||
message.setInformativeText(
|
||||
f"文件:\n{path}\n\n"
|
||||
f"系统错误:\n{error}\n\n"
|
||||
"你可以选择一个本地程序来打开此文件。"
|
||||
)
|
||||
choose_button = message.addButton("选择程序打开", QMessageBox.ButtonRole.AcceptRole)
|
||||
message.addButton("取消", QMessageBox.ButtonRole.RejectRole)
|
||||
message.exec()
|
||||
|
||||
if message.clickedButton() == choose_button:
|
||||
self._open_with_selected_application(path)
|
||||
|
||||
def _open_with_selected_application(self, path: Path) -> None:
|
||||
program, _ = QFileDialog.getOpenFileName(
|
||||
self,
|
||||
"选择用于打开文件的程序",
|
||||
str(Path.home()),
|
||||
"可执行程序 (*.exe);;所有文件 (*)",
|
||||
)
|
||||
if not program:
|
||||
LOGGER.info("User canceled selecting application for: %s", path)
|
||||
return
|
||||
|
||||
LOGGER.info("Opening local file with selected application: %s -> %s", program, path)
|
||||
if not QProcess.startDetached(program, [str(path)]):
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"打开失败",
|
||||
f"无法启动所选程序:\n{program}\n\n要打开的文件:\n{path}",
|
||||
)
|
||||
|
||||
def _open_webpage_file_now(self, path: Path, new_tab: bool = False) -> None:
|
||||
path = path.resolve()
|
||||
|
||||
Reference in New Issue
Block a user