2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2013-10-12
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
from enum import Enum
|
|
|
|
from os import PathLike
|
2019-09-10 00:54:28 +00:00
|
|
|
import os.path as op
|
|
|
|
import logging
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
class SpecialFolder(Enum):
|
2021-08-21 21:56:27 +00:00
|
|
|
APPDATA = 1
|
|
|
|
CACHE = 2
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def open_url(url: str) -> None:
|
2021-08-15 09:10:18 +00:00
|
|
|
"""Open ``url`` with the default browser."""
|
2019-09-10 00:54:28 +00:00
|
|
|
_open_url(url)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def open_path(path: PathLike) -> None:
|
2021-08-15 09:10:18 +00:00
|
|
|
"""Open ``path`` with its associated application."""
|
2019-09-10 00:54:28 +00:00
|
|
|
_open_path(str(path))
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def reveal_path(path: PathLike) -> None:
|
2021-08-15 09:10:18 +00:00
|
|
|
"""Open the folder containing ``path`` with the default file browser."""
|
2019-09-10 00:54:28 +00:00
|
|
|
_reveal_path(str(path))
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def special_folder_path(special_folder: SpecialFolder, portable: bool = False) -> str:
|
2019-09-10 00:54:28 +00:00
|
|
|
"""Returns the path of ``special_folder``.
|
|
|
|
|
|
|
|
``special_folder`` is a SpecialFolder.* const. The result is the special folder for the current
|
|
|
|
application. The running process' application info is used to determine relevant information.
|
|
|
|
|
|
|
|
You can override the application name with ``appname``. This argument is ingored under Qt.
|
|
|
|
"""
|
2022-05-09 06:15:25 +00:00
|
|
|
return _special_folder_path(special_folder, portable=portable)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
try:
|
2022-07-09 03:17:42 +00:00
|
|
|
from PyQt6.QtCore import QUrl, QStandardPaths
|
|
|
|
from PyQt6.QtGui import QDesktopServices
|
2022-05-09 00:22:08 +00:00
|
|
|
from qt.util import get_appdata
|
2022-03-28 05:47:46 +00:00
|
|
|
from core.util import executable_folder
|
|
|
|
from hscommon.plat import ISWINDOWS, ISOSX
|
|
|
|
import subprocess
|
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _open_url(url: str) -> None:
|
2022-03-28 05:47:46 +00:00
|
|
|
QDesktopServices.openUrl(QUrl(url))
|
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _open_path(path: str) -> None:
|
2022-03-28 05:47:46 +00:00
|
|
|
url = QUrl.fromLocalFile(str(path))
|
|
|
|
QDesktopServices.openUrl(url)
|
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _reveal_path(path: str) -> None:
|
2022-03-28 05:47:46 +00:00
|
|
|
if ISWINDOWS:
|
|
|
|
subprocess.run(["explorer", "/select,", op.abspath(path)])
|
|
|
|
elif ISOSX:
|
|
|
|
subprocess.run(["open", "-R", op.abspath(path)])
|
|
|
|
else:
|
|
|
|
_open_path(op.dirname(str(path)))
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _special_folder_path(special_folder: SpecialFolder, portable: bool = False) -> str:
|
2021-08-21 21:56:27 +00:00
|
|
|
if special_folder == SpecialFolder.CACHE:
|
2022-03-28 05:47:46 +00:00
|
|
|
if ISWINDOWS and portable:
|
|
|
|
folder = op.join(executable_folder(), "cache")
|
|
|
|
else:
|
2022-07-09 03:17:42 +00:00
|
|
|
folder = QStandardPaths.standardLocations(QStandardPaths.StandardLocation.CacheLocation)[0]
|
2019-09-10 00:54:28 +00:00
|
|
|
else:
|
2022-03-28 05:47:46 +00:00
|
|
|
folder = get_appdata(portable)
|
|
|
|
return folder
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
except ImportError:
|
2022-03-28 05:47:46 +00:00
|
|
|
# We're either running tests, and these functions don't matter much or we're in a really
|
|
|
|
# weird situation. Let's just have dummy fallbacks.
|
|
|
|
logging.warning("Can't setup desktop functions!")
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _open_url(url: str) -> None:
|
2022-03-28 05:47:46 +00:00
|
|
|
# Dummy for tests
|
|
|
|
pass
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _open_path(path: str) -> None:
|
2022-03-28 05:47:46 +00:00
|
|
|
# Dummy for tests
|
|
|
|
pass
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2022-05-09 06:15:25 +00:00
|
|
|
def _reveal_path(path: str) -> None:
|
|
|
|
# Dummy for tests
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _special_folder_path(special_folder: SpecialFolder, portable: bool = False) -> str:
|
2022-03-28 05:47:46 +00:00
|
|
|
return "/tmp"
|