internal: better copy

This commit is contained in:
2 * r + 2 * t 2025-08-27 17:56:46 +10:00
parent 800b5e6515
commit b5d8125867
4 changed files with 29 additions and 6 deletions

View file

@ -4,6 +4,7 @@ import qs.components
import qs.components.filedialog
import qs.config
import qs.utils
import Caelestia
import Quickshell
import Quickshell.Hyprland
import QtQuick
@ -20,8 +21,10 @@ Item {
filterLabel: qsTr("Image files")
filters: Images.validImageExtensions
onAccepted: path => {
Paths.copy(path, `${Paths.home}/.face`);
Quickshell.execDetached(["notify-send", "-a", "caelestia-shell", "-u", "low", "-h", `STRING:image-path:${path}`, "Profile picture changed", `Profile picture changed to ${Paths.shortenHome(path)}`]);
if (CUtils.copyFile(`file://${path}`, `${Paths.home}/.face`))
Quickshell.execDetached(["notify-send", "-a", "caelestia-shell", "-u", "low", "-h", `STRING:image-path:${path}`, "Profile picture changed", `Profile picture changed to ${Paths.shortenHome(path)}`]);
else
Quickshell.execDetached(["notify-send", "-a", "caelestia-shell", "-u", "critical", "Unable to change profile picture", `Failed to change profile picture to ${Paths.shortenHome(path)}`]);
}
}
}

View file

@ -68,3 +68,24 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
}
);
}
bool CUtils::copyFile(const QUrl& source, const QUrl& target) const {
return this->copyFile(source, target, true);
}
bool CUtils::copyFile(const QUrl& source, const QUrl& target, bool overwrite) const {
if (!source.isLocalFile()) {
qWarning() << "CUtils::copyFile: source" << source << "is not a local file";
return false;
}
if (!target.isLocalFile()) {
qWarning() << "CUtils::copyFile: target" << target << "is not a local file";
return false;
}
if (overwrite) {
QFile::remove(target.toLocalFile());
}
return QFile::copy(source.toLocalFile(), target.toLocalFile());
}

View file

@ -15,4 +15,7 @@ public:
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const;
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const;
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const;
Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target) const;
Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const;
};

View file

@ -37,8 +37,4 @@ Singleton {
function strip(path: url): string {
return stringify(path).replace("file://", "");
}
function copy(from: url, to: url): void {
Quickshell.execDetached(["cp", strip(from), strip(to)]);
}
}