Skip to content

Commit

Permalink
Administrative privileges
Browse files Browse the repository at this point in the history
Handle administrative privileges for the modules that require it.

Issue: #11
  • Loading branch information
plfiorini committed Feb 3, 2013
1 parent 604a6ca commit 6f67d4c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand Down
59 changes: 43 additions & 16 deletions src/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <QStyledItemDelegate>
#include <QTranslator>

#include <PolkitQt1/Gui/Action>

#include <VCategorizedView>

#include <Hawaii/SystemPreferences/PreferencesModulePlugin>
Expand All @@ -53,6 +55,7 @@ using namespace Hawaii::SystemPreferences;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_translator(0)
, m_unlockAction(0)
{
// Load translations
loadTranslations();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}
Expand All @@ -237,10 +263,11 @@ void MainWindow::slotListViewClicked(const QModelIndex &index)
MenuItem *item = index.data(Qt::UserRole).value<MenuItem *>();
if (item->module()) {
// Show the module
m_stackedWidget->setCurrentWidget((PreferencesModule *)item->module());
m_stackedWidget->setCurrentWidget(const_cast<PreferencesModule *>(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<PreferencesModule *>(item->module()));

// Hide the search field because it cannot be used now
m_search->hide();
Expand Down
9 changes: 9 additions & 0 deletions src/app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class QLineEdit;
class QStackedWidget;
class QTranslator;

namespace PolkitQt1 {
namespace Gui {
class Action;
}
}

class VCategorizedView;

class MenuItem;
Expand All @@ -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<Hawaii::SystemPreferences::PreferencesModule::Category, MenuItem *> m_categories;
Expand All @@ -70,6 +78,7 @@ private slots:

void loadTranslations();
void createActions();
void createUnlockAction(Hawaii::SystemPreferences::PreferencesModule *module);
void createToolBar();
void populate();
};
Expand Down

0 comments on commit 6f67d4c

Please sign in to comment.