C++ / fmt (pull_request) Failing after 6s
C++ / build (pull_request) Failing after 10s
C++ / clang-tidy (pull_request) Failing after 9s
JS/TS / fmt (pull_request) Successful in 10s
JS/TS / lint (pull_request) Successful in 8s
Python / static (pull_request) Successful in 30s
Rust / build (pull_request) Successful in 49s
Python / verify (pull_request) Successful in 1m35s
Rust / fmt (pull_request) Successful in 36s
Rust / clippy (pull_request) Successful in 54s
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#include "clipboard.hpp"
|
|
|
|
#include <QDebug>
|
|
#include <QClipboard>
|
|
#include <QDateTime>
|
|
#include <QDir>
|
|
#include <QGuiApplication>
|
|
#include <QImage>
|
|
#include <QMimeData>
|
|
#include <QUrl>
|
|
|
|
namespace ZShell::llm::clipboard {
|
|
|
|
bool hasAttachment() {
|
|
QClipboard* clipboard = QGuiApplication::clipboard();
|
|
if (!clipboard) return false;
|
|
const QMimeData* data = clipboard->mimeData();
|
|
if (!data) return false;
|
|
if (data->hasImage()) return true;
|
|
for (const QUrl& url : data->urls())
|
|
if (url.isLocalFile()) return true;
|
|
return false;
|
|
}
|
|
|
|
QStringList attachmentPaths(QString& error) {
|
|
error.clear();
|
|
QClipboard* clipboard = QGuiApplication::clipboard();
|
|
const QMimeData* data = clipboard ? clipboard->mimeData() : nullptr;
|
|
if (!data) {
|
|
qWarning() << "clipboard: no clipboard data";
|
|
return {};
|
|
}
|
|
|
|
QStringList paths;
|
|
if (data->hasImage()) {
|
|
const QImage image = clipboard->image();
|
|
if (image.isNull()) {
|
|
error = QStringLiteral("Clipboard image could not be decoded");
|
|
return {};
|
|
}
|
|
const QString tempPath =
|
|
QDir::tempPath() + QStringLiteral("/zshell-pasted-image-%1.png")
|
|
.arg(QDateTime::currentMSecsSinceEpoch());
|
|
if (!image.save(tempPath)) {
|
|
error = QStringLiteral("Could not save the pasted image");
|
|
return {};
|
|
}
|
|
paths.append(tempPath);
|
|
} else if (data->hasUrls()) {
|
|
for (const QUrl& url : data->urls())
|
|
if (url.isLocalFile()) paths.append(url.toLocalFile());
|
|
}
|
|
|
|
if (paths.isEmpty() && (data->hasImage() || data->hasUrls()))
|
|
error = QStringLiteral(
|
|
"Nothing usable on the clipboard (expected an image "
|
|
"or copied files)");
|
|
return paths;
|
|
}
|
|
|
|
} // namespace ZShell::llm::clipboard
|