checks for admin rights

This commit is contained in:
sezanzeb 2020-11-08 20:16:45 +01:00
parent 1d6d627e9c
commit 73dc4da1b2
2 changed files with 152 additions and 4 deletions

View File

@ -23,6 +23,7 @@
import sys import sys
import getpass
from argparse import ArgumentParser from argparse import ArgumentParser
import gi import gi
@ -42,9 +43,6 @@ from keymapper.linux import get_devices, KeycodeReader
window = None window = None
# TODO check for sudo rights
def gtk_iteration(): def gtk_iteration():
"""Iterate while events are pending.""" """Iterate while events are pending."""
while Gtk.events_pending(): while Gtk.events_pending():
@ -406,6 +404,27 @@ class Window:
) )
class ErrorDialog:
"""An Error that closes the application afterwards."""
def __init__(self, primary, secondary):
"""
Parameters
----------
primary : string
secondary : string
"""
gladefile = get_data_path('key-mapper.glade')
builder = Gtk.Builder()
builder.add_from_file(gladefile)
error_dialog = builder.get_object('error_dialog')
error_dialog.show()
builder.get_object('primary_error_label').set_text(primary)
builder.get_object('secondary_error_label').set_text(secondary)
error_dialog.run()
error_dialog.hide()
exit(1)
if __name__ == '__main__': if __name__ == '__main__':
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument( parser.add_argument(
@ -418,5 +437,13 @@ if __name__ == '__main__':
update_verbosity(options.debug) update_verbosity(options.debug)
log_info() log_info()
if getpass.getuser() != 'root' and 'unittest' not in sys.modules.keys():
logger.error('Needs to run with sudo')
ErrorDialog(
'Error',
'Key Mapper needs administrator privileges to run.'
)
else:
window = Window() window = Window()
Gtk.main() Gtk.main()

View File

@ -2,6 +2,127 @@
<!-- Generated with glade 3.36.0 --> <!-- Generated with glade 3.36.0 -->
<interface> <interface>
<requires lib="gtk+" version="3.22"/> <requires lib="gtk+" version="3.22"/>
<object class="GtkDialog" id="error_dialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Key Mapper</property>
<property name="modal">True</property>
<property name="window_position">center</property>
<property name="icon_name">mouse</property>
<property name="type_hint">dialog</property>
<property name="urgency_hint">True</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="close_error_dialog">
<property name="label">gtk-close</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
<property name="receives_default">False</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImage" id="error-image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="yalign">0</property>
<property name="xpad">12</property>
<property name="ypad">12</property>
<property name="icon_name">mouse</property>
<property name="icon_size">6</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_end">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="primary_error_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="ypad">6</property>
<property name="use_markup">True</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="secondary_error_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="ypad">6</property>
<property name="use_markup">True</property>
<property name="xalign">0</property>
<property name="yalign">0</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-7">close_error_dialog</action-widget>
</action-widgets>
<child type="titlebar">
<placeholder/>
</child>
</object>
<object class="GtkWindow" id="window"> <object class="GtkWindow" id="window">
<property name="width_request">450</property> <property name="width_request">450</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>