1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
* SPDX-FileCopyrightText: 2018 Bhushan Shah <bshah@kde.org>
* SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "shellutil.h"
#include <KConfigGroup>
#include <KFileUtils>
#include <KLocalizedString>
#include <KNotification>
#include <KNotificationJobUiDelegate>
#include <QQuickWindow>
#include <QDBusPendingReply>
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QProcess>
#include <QTextDocumentFragment>
#include <QtWaylandClient/private/qwaylandwindow_p.h>
#include <LayerShellQt/Window>
#define FORMAT24H "HH:mm:ss"
ShellUtil::ShellUtil(QObject *parent) : QObject{parent}, m_localeConfig {
KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig) } {
}
void ShellUtil::stackItemBefore(QQuickItem *item1, QQuickItem *item2)
{
if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
return;
}
item1->stackBefore(item2);
}
void ShellUtil::stackItemAfter(QQuickItem *item1, QQuickItem *item2)
{
if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
return;
}
item1->stackAfter(item2);
}
void ShellUtil::executeCommand(const QString &command)
{
qWarning() << "Executing" << command;
const QStringList commandAndArguments = QProcess::splitCommand(command);
QProcess::startDetached(commandAndArguments.front(), commandAndArguments.mid(1));
}
bool ShellUtil::isSystem24HourFormat()
{
// only load the config watcher if this function is actually used once
if (!m_localeConfigWatcher) {
m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig);
// watch for changes to locale config, to update 12/24 hour time
connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group) -> void {
if (group.name() == "Locale") {
// we have to reparse for new changes (from system settings)
m_localeConfig->reparseConfiguration();
Q_EMIT isSystem24HourFormatChanged();
}
});
}
KConfigGroup localeSettings = KConfigGroup(m_localeConfig, "Locale");
QString timeFormat = localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H));
return timeFormat == QStringLiteral(FORMAT24H);
}
void ShellUtil::launchApp(const QString &storageId)
{
KService::Ptr service = KService::serviceByStorageId(storageId);
if (!service) {
qWarning() << "Could not find" << storageId;
return;
}
auto job = new KIO::ApplicationLauncherJob(service, this);
job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
job->start();
}
void ShellUtil::setInputTransparent(QQuickWindow *window, bool transparent)
{
if (!window) {
return;
}
Qt::WindowFlags flags = window->flags();
if (transparent) {
flags |= Qt::WindowTransparentForInput;
} else {
flags &= ~Qt::WindowTransparentForInput;
}
window->setFlags(flags);
}
void ShellUtil::setWindowLayer(QQuickWindow *window, LayerShellQt::Window::Layer layer)
{
if (!window) {
return;
}
auto layerShellWindow = LayerShellQt::Window::get(window);
layerShellWindow->setLayer(layer);
}
void ShellUtil::setInputRegion(QWindow *window, const QRect ®ion)
{
if (!window) {
return;
}
auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
if (!waylandWindow) {
qWarning() << "Failed to retrieve Wayland window handle.";
return;
}
auto waylandDisplay = dynamic_cast<QtWaylandClient::QWaylandDisplay *>(waylandWindow->display());
if (!waylandDisplay) {
qWarning() << "Failed to retrieve Wayland display.";
return;
}
wl_compositor *compositorResource = static_cast<wl_compositor *>(waylandDisplay->compositor()->object());
if (!compositorResource) {
qWarning() << "Failed to retrieve compositor.";
return;
}
wl_surface *surface = waylandWindow->wlSurface();
if (!surface) {
qWarning() << "Failed to retrieve Wayland surface.";
return;
}
if (region.isEmpty()) {
wl_surface_set_input_region(surface, nullptr);
} else {
wl_region *inputRegion = wl_compositor_create_region(compositorResource);
wl_region_add(inputRegion, region.x(), region.y(), region.width(), region.height());
wl_surface_set_input_region(surface, inputRegion);
wl_region_destroy(inputRegion);
}
wl_surface_commit(surface);
}
QString ShellUtil::toPlainText(QString htmlString)
{
return QTextDocumentFragment::fromHtml(htmlString).toPlainText();
}
|