Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbus Screen shot Test for linux #239

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ target_link_libraries ( HELPERS
Qt::GuiPrivate
)

if(UNIX)
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS DBus)
target_link_libraries(HELPERS PRIVATE Qt::DBus)
endif()

if(APPLE)
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS
DBus
)
target_link_libraries(HELPERS PRIVATE Qt::DBus ${CARBON_LIBRARY})
target_link_libraries(HELPERS PRIVATE ${CARBON_LIBRARY})
elseif(UNIX AND NOT APPLE)
target_link_libraries(HELPERS PRIVATE xcb xcb-keysyms pthread)
elseif(WIN32)
Expand Down
48 changes: 47 additions & 1 deletion src/helpers/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,40 @@
#include "helpers/string_helpers.h"
#include "helpers/system_helpers.h"

#ifndef Q_OS_WIN
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#endif

Screenshot::Screenshot(QObject *parent) : QObject(parent) {}

void Screenshot::captureArea() { basicScreenshot(AppConfig::value(CONFIG::COMMAND_SCREENSHOT)); }

void Screenshot::captureWindow() { basicScreenshot(AppConfig::value(CONFIG::COMMAND_CAPTUREWINDOW)); }
void Screenshot::captureWindow() {
auto captureCommand = AppConfig::value(CONFIG::COMMAND_CAPTUREWINDOW);
if(!captureCommand.startsWith("DBUS")) {
basicScreenshot(AppConfig::value(CONFIG::COMMAND_CAPTUREWINDOW));
return;
}

#ifndef Q_OS_WIN
// Attempt Dbus Capture
QDBusConnection bus = QDBusConnection::sessionBus();

QDBusInterface *i = new QDBusInterface("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Screenshot", bus, NULL);

QVariantMap options;
options["interactive"] = captureCommand.endsWith(QStringLiteral("INTERACTIVE")) ? true : false;

QDBusReply<QDBusObjectPath> repl = i->call("Screenshot", "", options);
if(repl.isValid()) {
bus.connect("", repl.value().path(), "org.freedesktop.portal.Request", "Response", this, SLOT(dbusScreenShot(uint, QVariantMap)));
} else {
qDebug() << "Something is wrong: " << repl.error();
}
#endif
}

QString Screenshot::mkName()
{
Expand Down Expand Up @@ -55,3 +84,20 @@ void Screenshot::basicScreenshot(QString cmdProto)
});
connect(ssTool, &QProcess::aboutToClose, ssTool, &QProcess::deleteLater);
}

void Screenshot::dbusScreenShot(uint responseCode, QVariantMap results)
{
if(responseCode != 0) {
qDebug() << "Unable to take a screenshot";
return;
}
auto baseDir = SystemHelpers::pathToEvidence();
if(!QDir().mkpath(baseDir))
return;
auto newName = mkName();
auto tempFile = QDir::toNativeSeparators(m_fileTemplate.arg(QDir::tempPath(), newName));
auto oldFile = results["uri"].toString().mid(7);
QFile::copy(oldFile, tempFile);
QFile::remove(oldFile);
Q_EMIT onScreenshotCaptured(tempFile);
}
4 changes: 3 additions & 1 deletion src/helpers/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Screenshot : public QObject {
static QString mkName();
static QString extension() { return QStringLiteral("png"); }
static QString contentType() { return QStringLiteral("image"); }

signals:
void onScreenshotCaptured(QString filepath);

Expand All @@ -24,4 +23,7 @@ class Screenshot : public QObject {
inline static const QString m_fileTemplate = QStringLiteral("%1/%2");
inline static const QString m_doubleQuote = QStringLiteral("\"");
inline static const QString m_space = QStringLiteral(" ");

private slots:
void dbusScreenShot(uint responseCode, QVariantMap results);
};