┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/admin/bar.cpp
diff options
context:
space:
mode:
authorFelix Ernst <[email protected]>2024-05-11 17:16:35 +0000
committerFelix Ernst <[email protected]>2024-05-11 17:16:35 +0000
commit6c60655ce246a91758f4b9035edf318cb1197a2c (patch)
treef7b6ac7d13cbdc76a67af70765373f735ca56a27 /src/admin/bar.cpp
parent742566eb69e6bf46e7abb74e9ce9293c4b3ed7e8 (diff)
Add "Act as Administrator" toggle action
This commit adds an "Act as Administrator" toggle action to the View menu if kio-admin is installed. The action allows switching between acting as an admin with root-access or not. This was already possible in Dolphin when kio-admin is installed by editing the location bar directly. However this is somewhat unintuitive and there are no warnings at all about the dangers of acting as an administrator. This commit adds a warning dialog when triggering the action. It is somewhat explicit about the risks because this is in fact very dangerous. Furthermore, while acting on a view with administrative privileges, a bar above the view shows up that contains a warning. The bar can be closed to stop acting with elevated privileges. The warning dialog can be disabled and re-enabled from the Dolphin settings but only if the action is even available. There is a lot more to be done to further improve this feature both security-wise as well as when it comes to usability. But considering that we are already encouraging users to use this feature without any warnings at all, I feel like now is a good time to merge this. This work is part of a project funded through the NGI0 Entrust Fund, a fund established by NLnet with financial support from the European Commission's Next Generation Internet programme, under the aegis of DG Communications Networks, Content and Technology. As such, please contact me if you plan on doing related work so what you are doing doesn't collide with work I am being funded to do.
Diffstat (limited to 'src/admin/bar.cpp')
-rw-r--r--src/admin/bar.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/admin/bar.cpp b/src/admin/bar.cpp
new file mode 100644
index 000000000..2b65127f6
--- /dev/null
+++ b/src/admin/bar.cpp
@@ -0,0 +1,99 @@
+/*
+ This file is part of the KDE project
+ SPDX-FileCopyrightText: 2024 Felix Ernst <[email protected]>
+
+ SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
+*/
+
+#include "bar.h"
+
+#include "workerintegration.h"
+
+#include <KColorScheme>
+#include <KContextualHelpButton>
+#include <KLocalizedString>
+
+#include <QEvent>
+#include <QGuiApplication>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QPushButton>
+#include <QStyle>
+#include <QToolButton>
+#include <QWidgetAction>
+
+using namespace Admin;
+
+Bar::Bar(QWidget *parent)
+ : AnimatedHeightWidget{parent}
+{
+ setAutoFillBackground(true);
+ updateColors();
+
+ QWidget *contenntsContainer = prepareContentsContainer();
+
+ m_fullLabelString = i18nc("@info label above the view explaining the state", "Acting as an Administrator – Be careful!");
+ m_shortLabelString = i18nc("@info label above the view explaining the state, keep short", "Acting as Admin");
+ m_label = new QLabel(contenntsContainer);
+ m_label->setMinimumWidth(0);
+ m_label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::LinksAccessibleByKeyboard); // for keyboard accessibility
+
+ m_warningButton = new KContextualHelpButton(warningMessage(), nullptr, contenntsContainer);
+ m_warningButton->setIcon(QIcon::fromTheme(QStringLiteral("emblem-warning")));
+
+ m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contenntsContainer);
+ m_closeButton->setToolTip(i18nc("@action:button", "Stop Acting as an Administrator"));
+ m_closeButton->setFlat(true);
+ connect(m_closeButton, &QAbstractButton::clicked, this, &Bar::activated); // Make sure the view connected to this bar is active before exiting admin mode.
+ connect(m_closeButton, &QAbstractButton::clicked, this, &WorkerIntegration::exitAdminMode);
+
+ QHBoxLayout *layout = new QHBoxLayout(contenntsContainer);
+ auto contentsMargins = layout->contentsMargins();
+ m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
+ m_warningButton->setFixedHeight(m_preferredHeight);
+ m_closeButton->setFixedHeight(m_preferredHeight);
+ layout->setContentsMargins(0, 0, 0, 0);
+
+ layout->addStretch();
+ layout->addWidget(m_label);
+ layout->addWidget(m_warningButton);
+ layout->addStretch();
+ layout->addWidget(m_closeButton);
+}
+
+bool Bar::event(QEvent *event)
+{
+ if (event->type() == QEvent::PaletteChange) {
+ updateColors();
+ }
+ return AnimatedHeightWidget::event(event);
+}
+
+void Bar::resizeEvent(QResizeEvent *resizeEvent)
+{
+ updateLabelString();
+ return QWidget::resizeEvent(resizeEvent);
+}
+
+void Bar::updateColors()
+{
+ QPalette palette = parentWidget()->palette();
+ KColorScheme colorScheme{QPalette::Normal, KColorScheme::ColorSet::Window};
+ colorScheme.adjustBackground(palette, KColorScheme::NegativeBackground, QPalette::Window, KColorScheme::ColorSet::Window);
+ colorScheme.adjustForeground(palette, KColorScheme::NegativeText, QPalette::WindowText, KColorScheme::ColorSet::Window);
+ setPalette(palette);
+}
+
+void Bar::updateLabelString()
+{
+ QFontMetrics fontMetrics = m_label->fontMetrics();
+ if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_warningButton->sizeHint().width() + m_closeButton->sizeHint().width()
+ + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2
+ < width()) {
+ m_label->setText(m_fullLabelString);
+ } else {
+ m_label->setText(m_shortLabelString);
+ }
+}
+
+#include "moc_bar.cpp"