Created
September 14, 2021 19:27
-
-
Save N0rbert/847c5466189eb653efec4ce222d6f71d to your computer and use it in GitHub Desktop.
Start Lock Screen menu item for Caja on Desktop (solution to https://ubuntu-mate.community/t/how-to-change-the-menu-right-click-on-the-desktop-from-ubuntu-mate-20-04/24561)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Start Lock Screen menu item for Caja, shown only on ~/Desktop | |
# | |
# Copyleft 2021, Norbert | |
# | |
# Based on dejadup.py from deja-dup-caja deb-package. | |
import subprocess, os | |
# Python 2 or 3 | |
try: | |
from urllib import unquote | |
except ImportError: | |
from urllib.parse import unquote | |
from gi.repository import Caja, GObject | |
class StartLockScreen: | |
def __init__(self): | |
pass | |
def _run_cmd(self, cmd, background=True): | |
"""Run command into shell""" | |
if background: | |
proc = subprocess.Popen(cmd, shell=False) | |
else: | |
proc = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout,stderr = proc.communicate() | |
return stdout,stderr | |
def restore_missing(self, item): | |
"""Lock session call""" | |
self._run_cmd(['mate-screensaver-command', '--lock'], background=True) | |
class Utils: | |
def __init__(self): | |
pass | |
def get_filepath(self, items): | |
"""Get just the path from a complete path and filename""" | |
try: | |
item = items[0] | |
except: | |
item = items | |
return unquote(item.get_uri()[7:]) | |
class StartLockScreenCaja(GObject.GObject, Caja.MenuProvider): | |
def __init__(self): | |
self.startlockscreen = StartLockScreen() | |
self.utils = Utils() | |
def _missing_activate(self, menu, item): | |
"""Missing items""" | |
self.startlockscreen.restore_missing(item) | |
def get_background_items(self, window, current_folder): | |
"""Caja invoke this when user clicks in an empty area""" | |
Menu = Caja.MenuItem( | |
name="StartLockScreen::Start", | |
label="Start Lock Screen", | |
icon="system-lock-screen") | |
# Get complete path | |
item = self.utils.get_filepath(current_folder) | |
if item == "desktop:///": | |
Menu.connect('activate', self._missing_activate, item) | |
return [Menu] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment