Skip to content

Commit

Permalink
Dbus Screen shot Test for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Rizzitello committed Oct 3, 2023
1 parent 0f2746a commit acfa8b5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
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);
};

0 comments on commit acfa8b5

Please sign in to comment.