From 6f67d4c2eee1d770d4c04328ac226d9436317f8f Mon Sep 17 00:00:00 2001 From: Pier Luigi Fiorini Date: Sun, 3 Feb 2013 13:41:19 +0100 Subject: [PATCH] Administrative privileges Handle administrative privileges for the modules that require it. Issue: #11 --- CMakeLists.txt | 7 +++++ src/app/CMakeLists.txt | 2 ++ src/app/mainwindow.cpp | 59 ++++++++++++++++++++++++++++++------------ src/app/mainwindow.h | 9 +++++++ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b0d03c..86457f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,13 @@ endif() macro_log_feature(VibeCore_FOUND "VibeCore" "Support for VibeCore" "http://www.maui-project.org" "") macro_log_feature(VibeWidgets_FOUND "VibeWidgets" "Support for VibeWidgets" "http://www.maui-project.org" "") +# Find PolicyKit wrapper for Qt +find_package(PolkitQt-1 REQUIRED) +if(NOT PolkitQt-1_FOUND) + message(FATAL_ERROR "PolkitQt-1 module is required!") +endif() +macro_log_feature(PolkitQt-1_FOUND "PolkitQt-1" "Support for PolicyKit" "http://www.freedesktop.org/wiki/Software/PolicyKit" "") + # Subdirectories add_subdirectory(headers) add_subdirectory(src) diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index e9cf31d..1c33092 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -2,6 +2,7 @@ include_directories( ${CMAKE_SOURCE_DIR}/headers ${CMAKE_BINARY_DIR}/src/lib ${VibeWidgets_INCLUDE_DIRS} + ${POLKITQT-1_INCLUDE_DIR} ) # Create the cmakedirs.h header file @@ -26,6 +27,7 @@ add_executable(hawaii-system-preferences ${SOURCES} ${QM_FILES}) qt5_use_modules(hawaii-system-preferences Widgets) target_link_libraries(hawaii-system-preferences ${VibeWidgets_LIBRARIES} + ${POLKITQT-1_GUI_LIBRARY} HawaiiSystemPreferences ) diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 2ee588e..9e32ab8 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -37,6 +37,8 @@ #include #include +#include + #include #include @@ -53,6 +55,7 @@ using namespace Hawaii::SystemPreferences; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , m_translator(0) + , m_unlockAction(0) { // Load translations loadTranslations(); @@ -120,12 +123,12 @@ MainWindow::~MainWindow() void MainWindow::changeEvent(QEvent *event) { switch (event->type()) { - case QEvent::LanguageChange: - case QEvent::LocaleChange: - loadTranslations(); - break; - default: - break; + case QEvent::LanguageChange: + case QEvent::LocaleChange: + loadTranslations(); + break; + default: + break; } QMainWindow::changeEvent(event); @@ -151,9 +154,9 @@ void MainWindow::loadTranslations() // Load our translations for the current locale m_translator = new QTranslator(this); QString localeDir = QStandardPaths::locate( - QStandardPaths::GenericDataLocation, - QLatin1String("hawaii-system-preferences/translations"), - QStandardPaths::LocateDirectory); + QStandardPaths::GenericDataLocation, + QLatin1String("hawaii-system-preferences/translations"), + QStandardPaths::LocateDirectory); m_translator->load(locale, localeDir); QCoreApplication::instance()->installTranslator(m_translator); } @@ -166,21 +169,37 @@ void MainWindow::createActions() this, SLOT(slotOverviewTriggered())); } +void MainWindow::createUnlockAction(PreferencesModule *module) +{ + if (!module->requiresAdministrativePrivileges()) + return; + + m_unlockAction = new PolkitQt1::Gui::Action(module->administrativeActionId(), this); + m_unlockAction->setIcon(QIcon::fromTheme("changes-allow")); + m_unlockAction->setToolTip(tr("Dialog is locked, click to make changes")); + m_unlockAction->setTargetPID(QCoreApplication::instance()->applicationPid()); + m_toolBar->addAction(m_unlockAction); + connect(m_unlockAction, SIGNAL(triggered()), + m_unlockAction, SLOT(activate())); +} + void MainWindow::createToolBar() { - QToolBar *toolBar = new QToolBar(tr("Tool Bar"), this); - toolBar->addAction(m_overviewAction); + m_toolBar = new QToolBar(tr("Tool Bar"), this); + m_toolBar->setMovable(false); + + m_toolBar->addAction(m_overviewAction); QWidget *spacerWidget = new QWidget(this); spacerWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum); - toolBar->addWidget(spacerWidget); + m_toolBar->addWidget(spacerWidget); - QAction *searchAction = toolBar->addWidget(m_search); + QAction *searchAction = m_toolBar->addWidget(m_search); searchAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F)); connect(searchAction, SIGNAL(triggered()), m_search, SLOT(setFocus())); - addToolBar(toolBar); + addToolBar(m_toolBar); } void MainWindow::populate() @@ -223,6 +242,13 @@ void MainWindow::slotOverviewTriggered() // Now that we are on the first page the action must be disabled m_overviewAction->setEnabled(false); + // Hide the unlock button + if (m_unlockAction) { + m_toolBar->removeAction(m_unlockAction); + delete m_unlockAction; + m_unlockAction = 0; + } + // Show the search field because now we need it m_search->show(); } @@ -237,10 +263,11 @@ void MainWindow::slotListViewClicked(const QModelIndex &index) MenuItem *item = index.data(Qt::UserRole).value(); if (item->module()) { // Show the module - m_stackedWidget->setCurrentWidget((PreferencesModule *)item->module()); + m_stackedWidget->setCurrentWidget(const_cast(item->module())); - // Enable the action to go to the first page + // Enable the overview button and show the unlock button if neccessary m_overviewAction->setEnabled(true); + createUnlockAction(const_cast(item->module())); // Hide the search field because it cannot be used now m_search->hide(); diff --git a/src/app/mainwindow.h b/src/app/mainwindow.h index 459a0f2..bc2634b 100644 --- a/src/app/mainwindow.h +++ b/src/app/mainwindow.h @@ -36,6 +36,12 @@ class QLineEdit; class QStackedWidget; class QTranslator; +namespace PolkitQt1 { + namespace Gui { + class Action; + } +} + class VCategorizedView; class MenuItem; @@ -60,6 +66,8 @@ private slots: private: QTranslator *m_translator; QAction *m_overviewAction; + PolkitQt1::Gui::Action *m_unlockAction; + QToolBar *m_toolBar; QLineEdit *m_search; QStackedWidget *m_stackedWidget; QMap m_categories; @@ -70,6 +78,7 @@ private slots: void loadTranslations(); void createActions(); + void createUnlockAction(Hawaii::SystemPreferences::PreferencesModule *module); void createToolBar(); void populate(); };