diff --git a/README.md b/README.md index f046b5c..089887b 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,15 @@ lines of code. it just uses kdesu to call a helper as root to send the reply, so all requests requires the user to enter the root password. +Stylesheet Customization +------------------------ + +The Qt QInputDialog can be customized via a CSS stylesheet file. Place and name the file as follows: + +`$HOME/.config/polkit-dumb-agent/stylesheet.qss` + +See [Qt Stylesheet Syntax](https://doc.qt.io/qt-5/stylesheet-syntax.html) for reference. + TODO ---- diff --git a/agent.h b/agent.h index a8f6dba..39c7a22 100644 --- a/agent.h +++ b/agent.h @@ -22,6 +22,11 @@ #include #include #include +#include +#include +#include + +#include #include class Agent : public QObject @@ -44,11 +49,50 @@ public slots: const QString safeCookie = QProcess::splitCommand(cookie).first(); // in case someone try to be funny const QByteArray command = QStringList({ responderPath, safeCookie, QString::number(getuid()) }).join(' ').toLocal8Bit(); - for (int num=0; num<3; num++) { - bool ok; - QString password = QInputDialog::getText(nullptr, "Enter the root password", message + "\n" + actionId, QLineEdit::Password, QString(), &ok); - if (!ok) { - return; // user aborted + QString home = std::getenv("HOME"); + QString config = home.append("/.config/polkit-dumb-agent/"); + QString stylesheet = config.append("stylesheet.qss"); + + struct stat buffer; + QString stylesheetContent; + if (stat(stylesheet.toLatin1().constData(), &buffer) == 0) + { + QFile stylesheetFile(stylesheet); + + if (stylesheetFile.open(QFile::ReadOnly | QFile::Text)) + { + QTextStream in(&stylesheetFile); + stylesheetContent = in.readAll(); + } + else + { + qDebug() << "Failed to open " << stylesheet; + } + } + + for (int num=0; num<3; num++) + { + QInputDialog inputDialog(nullptr); + inputDialog.setWindowTitle("Enter the root password"); + inputDialog.setLabelText(message + "\n" + actionId); + inputDialog.setTextEchoMode(QLineEdit::EchoMode::Password); + inputDialog.setTextValue(""); + + if (!stylesheetContent.isEmpty()) + { + inputDialog.setStyleSheet(stylesheetContent); + } + + int dialogResult = inputDialog.exec(); + + QString password; + if (dialogResult == QDialog::DialogCode::Accepted) + { + password = inputDialog.textValue(); + } + else + { + return; //user aborted } KDESu::SuProcess process("root", command);