------------------------------------------------------------ revno: 32 committer: Olivier Duchateau branch nick: xfpanel-switch timestamp: Fri 2015-09-11 18:43:46 +0200 message: Check if user can delete backup, if not display a Gtk.MessageDialog widget. diff: === modified file 'xfpanel-switch/xfpanel-switch.py' --- xfpanel-switch/xfpanel-switch.py 2015-09-11 14:00:18 +0000 +++ xfpanel-switch/xfpanel-switch.py 2015-09-11 16:43:46 +0000 @@ -25,6 +25,8 @@ import shlex import os import datetime +import pwd +import stat from gi.repository import Gtk, GLib, Gio from panelconfig import PanelConfig @@ -221,7 +223,7 @@ dialog.destroy() def load_configuration(self, filename): - if os.path.isfile(filename): + if os.path.exists(filename): PanelConfig.from_file(filename).to_xfconf(self.xfconf) def on_apply_clicked(self, widget): @@ -229,16 +231,43 @@ self.load_configuration(filename) def delete_configuration(self, filename): - if os.path.isfile(filename): - os.remove(filename) + # Get name of current user + current_user = GLib.get_user_name() + + if os.path.exists(filename): + fp = open(filename, "rb") + # File descriptor (required by os.fstat function) + fd = fp.fileno() + + status = os.fstat(fd) + user_name = pwd.getpwuid(status[stat.ST_UID]) + fp.close() + + if current_user == user_name[0]: + os.remove(filename) + return True + else: + return False + + def _error_dialog(self, filename): + dialog = Gtk.MessageDialog(self.window, 0, + Gtk.MessageType.ERROR, + Gtk.ButtonsType.CANCEL, + _("Error")) + dialog.format_secondary_text(_("You are not the owner of %s" % os.path.basename(filename))) + dialog.run() + dialog.destroy() def on_delete_clicked(self, widget): model, treeiter, values = self.get_selected() filename = values[0] if filename == "": return - self.delete_configuration(filename) - model.remove(treeiter) + success = self.delete_configuration(filename) + if success: + model.remove(treeiter) + else: + self._error_dialog(filename) def on_window_destroy(self, *args): '''Exit the application when the window is closed.'''