def _get_app_list(self, directory, user): for root, dirs, files in os.walk(directory): for name in files: if name.endswith(".desktop"): app_path = root + "/" + name # setup desktop entry to access its elements xgd_de = DesktopEntry(app_path) # self.app_entry = Desktop_Entry( name, xgd_de.getName( ), xgd_de.getGenericName( ), xgd_de.getNoDisplay( ), xgd_de.getHidden( ), xgd_de.getOnlyShowIn( ), xgd_de.getNotShowIn( ), xgd_de.getCategories( ), app_path, user, False ) # Just as a note, skip no display or hidden .desktop if not (self.app_entry.de_nodisp or self.app_entry.de_hidden): self._add_entry(self.app_entry)
def get_browser_desktop_list(): browsers_xdg_files = [] for DIR in APPLICATIONS_DIRS: for f in DIR.iterdir(): if not f.is_file(): continue try: de = DesktopEntry(f) except xdg.Exceptions.ParsingError: continue if 'WebBrowser' in de.getCategories(): browsers_xdg_files.append(de) return browsers_xdg_files
def test_values(self): entry = DesktopEntry(self.test_file) self.assertEqual(entry.getName(), 'gedit') self.assertEqual(entry.getGenericName(), 'Text Editor') self.assertEqual(entry.getNoDisplay(), False) self.assertEqual(entry.getComment(), 'Edit text files') self.assertEqual(entry.getIcon(), 'accessories-text-editor') self.assertEqual(entry.getHidden(), False) self.assertEqual(entry.getOnlyShowIn(), []) self.assertEqual(entry.getExec(), 'gedit %U') self.assertEqual(entry.getTerminal(), False) self.assertEqual(entry.getMimeTypes(), ['text/plain']) self.assertEqual(entry.getCategories(), ['GNOME', 'GTK', 'Utility', 'TextEditor']) self.assertEqual(entry.getTerminal(), False)
def listAndUpdateDesktopFiles(self, path, menuObjs): for f in listdir(path): fPath = path + f if isfile(fPath) and f.endswith(".desktop"): xdgObj = DesktopEntry(fPath) title = xdgObj.getName() groups = xdgObj.getCategories() comment = xdgObj.getComment() icon = xdgObj.getIcon() mainExec = xdgObj.getExec() tryExec = xdgObj.getTryExec() group = "" if "Accessories" in groups or "Utility" in groups: group = "Accessories" elif "Multimedia" in groups or "Video" in groups or "Audio" in groups: group = "Multimedia" elif "Development" in groups: group = "Development" elif "Game" in groups: group = "Game" elif "Internet" in groups or "Network" in groups: group = "Internet" elif "Graphics" in groups: group = "Graphics" elif "Office" in groups: group = "Office" elif "System" in groups: group = "System" elif "Settings" in groups: group = "Settings" elif "Wine" in groups: group = "Wine" else: group = "Other" menuObjs[group].append( {"title": title, "groups": groups, "comment": comment, "exec": mainExec, "tryExec": tryExec, "fileName": f, "filePath": fPath, "icon": icon})
def __post_init__(self): if isinstance(self.icon_size, str): self.icon_size = utils.parse_size(self.icon_size) if isinstance(self.launcher_size, str): self.launcher_size = utils.parse_size(self.launcher_size) # Load in details from a .desktop file, if there is one. if self.desktop_file: try: de = DesktopEntry(self.desktop_file) except PermissionError as e: utils.debug( "Access denied on desktop file: {}, {}" .format(self.desktop_file, e) ) else: self.name = de.getName() self.comment = de.getComment() self.icon = de.getIcon() self.command = de.getExec() self.categories = [c.lower() for c in de.getCategories()]
class MenuEntry: "Wrapper for 'Menu Style' Desktop Entries" def __init__(self, filename, dir="", prefix=""): # Create entry self.DesktopEntry = DesktopEntry(os.path.join(dir,filename)) self.setAttributes(filename, dir, prefix) # Can be one of Deleted/Hidden/Empty/NotShowIn/NoExec or True self.Show = True # Semi-Private self.Original = None self.Parents = [] # Private Stuff self.Allocated = False self.Add = False self.MatchedInclude = False # Caching self.Categories = self.DesktopEntry.getCategories() def save(self): """Save any changes to the desktop entry.""" if self.DesktopEntry.tainted == True: self.DesktopEntry.write() def getDir(self): """Return the directory containing the desktop entry file.""" return self.DesktopEntry.filename.replace(self.Filename, '') def getType(self): """Return the type of MenuEntry, System/User/Both""" if xdg.Config.root_mode == False: if self.Original: return "Both" elif xdg_data_dirs[0] in self.DesktopEntry.filename: return "User" else: return "System" else: return "User" def setAttributes(self, filename, dir="", prefix=""): self.Filename = filename self.Prefix = prefix self.DesktopFileID = os.path.join(prefix,filename).replace("/", "-") if not os.path.isabs(self.DesktopEntry.filename): self.__setFilename() def updateAttributes(self): if self.getType() == "System": self.Original = MenuEntry(self.Filename, self.getDir(), self.Prefix) self.__setFilename() def __setFilename(self): if xdg.Config.root_mode == False: path = xdg_data_dirs[0] else: path= xdg_data_dirs[1] if self.DesktopEntry.getType() == "Application": dir = os.path.join(path, "applications") else: dir = os.path.join(path, "desktop-directories") self.DesktopEntry.filename = os.path.join(dir, self.Filename) def __cmp__(self, other): return locale.strcoll(self.DesktopEntry.getName(), other.DesktopEntry.getName()) def _key(self): """Key function for locale-aware sorting.""" return _strxfrm(self.DesktopEntry.getName()) def __lt__(self, other): try: other = other._key() except AttributeError: pass return self._key() < other def __eq__(self, other): if self.DesktopFileID == str(other): return True else: return False def __repr__(self): return self.DesktopFileID
def __init__(self, parent=None, **kwargs): """Construct a LaunchButton""" super(LaunchButton, self).__init__(parent) self.setObjectName("LaunchButton") self.launcher_size = kwargs.get("launcher_size") self.icon_size = kwargs.get("icon_size") self.name = None self.comment = None self.icon = None self.command = None self.categories = None # Load in details from a .desktop file, if there is one. desktop_file = kwargs.get("desktop_file") if desktop_file: if os.access(desktop_file, os.R_OK): de = DesktopEntry(desktop_file) self.name = de.getName() self.comment = de.getComment() self.icon = de.getIcon() self.command = de.getExec() self.categories = [c.lower() for c in de.getCategories()] else: sys.stderr.write( "Read access denied on manually-specified " "desktop file {}. Button may be missing data.\n" .format(desktop_file)) # This allows for overriding the settings in DesktopEntry self.name = kwargs.get("name", self.name) self.comment = kwargs.get("comment", self.comment) self.icon = kwargs.get("icon", self.icon) self.command = kwargs.get("command", self.command) # Create the layouts and widgets to hold the information toplayout = QHBoxLayout() leftlayout = QVBoxLayout() # The button's title title = QLabel(self.name) title.setObjectName("LaunchButtonTitle") leftlayout.addWidget(title) # The button's descriptive comment comment = QLabel(self.comment) comment.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) comment.setWordWrap(True) comment.setObjectName("LaunchButtonDescription") leftlayout.addWidget(comment) # The button's icon, if there is one iconpane = QLabel() icon = ( self.icon and icon_anyway_you_can( self.icon, kwargs.get("aggressive_icon_search", False) ) ) or QIcon() pixmap = icon.pixmap(*self.icon_size) if not pixmap.isNull(): pixmap = pixmap.scaled(*self.icon_size) iconpane.setPixmap(pixmap) # Add everything to layouts and layouts to the button toplayout.addWidget(iconpane) toplayout.addLayout(leftlayout) self.setLayout(toplayout) # Set the button's size from config. self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.setMinimumSize(QSize(*self.launcher_size)) # Connect the callback self.clicked.connect(self.callback)
def __init__(self, parent=None, **kwargs): """Construct a LaunchButton""" super(LaunchButton, self).__init__(parent) self.setObjectName("LaunchButton") self.launcher_size = kwargs.get("launcher_size") self.icon_size = kwargs.get("icon_size") self.name = None self.comment = None self.icon = None self.command = None self.categories = None # Load in details from a .desktop file, if there is one. desktop_file = kwargs.get("desktop_file") if desktop_file: if os.access(desktop_file, os.R_OK): de = DesktopEntry(desktop_file) self.name = de.getName() self.comment = de.getComment() self.icon = de.getIcon() self.command = de.getExec() self.categories = [c.lower() for c in de.getCategories()] else: sys.stderr.write( "Read access denied on manually-specified " "desktop file {}. Button may be missing data.\n".format( desktop_file)) # This allows for overriding the settings in DesktopEntry self.name = kwargs.get("name", self.name) self.comment = kwargs.get("comment", self.comment) self.icon = kwargs.get("icon", self.icon) self.command = kwargs.get("command", self.command) # Create the layouts and widgets to hold the information toplayout = QHBoxLayout() leftlayout = QVBoxLayout() # The button's title title = QLabel(self.name) title.setObjectName("LaunchButtonTitle") leftlayout.addWidget(title) # The button's descriptive comment comment = QLabel(self.comment) comment.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) comment.setWordWrap(True) comment.setObjectName("LaunchButtonDescription") leftlayout.addWidget(comment) # The button's icon, if there is one iconpane = QLabel() icon = (self.icon and icon_anyway_you_can( self.icon, kwargs.get("aggressive_icon_search", False))) or QIcon() pixmap = icon.pixmap(*self.icon_size) if not pixmap.isNull(): pixmap = pixmap.scaled(*self.icon_size) iconpane.setPixmap(pixmap) # Add everything to layouts and layouts to the button toplayout.addWidget(iconpane) toplayout.addLayout(leftlayout) self.setLayout(toplayout) # Set the button's size from config. self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.setMinimumSize(QSize(*self.launcher_size)) # Connect the callback self.clicked.connect(self.callback)
if de.getTerminal(): exc = TERM_CMD + exc if de.getPath(): exc = "cd '"+de.getPath()+"'; "+exc execs[hsh] = hsh + " " + exc + "\n" categories = {} for dir in SEARCH_DIRS: fnames = glob.glob(dir+'/*/*.desktop') for fname in fnames: de = DesktopEntry(fname) cats = de.getCategories() or ["All"] for cat in cats: if not cat in categories: categories[cat] = [] categories[cat].append(de) for k, v in sorted(categories.items()): catName = k for de in v: if de.getNoDisplay() or de.getHidden(): continue mainGroup = de.defaultGroup printEntry(catName, de, True)
subCats = [] #List of only subcategories topNCats = [] #List of top level numeric categories topCats = [] #List of top level nonnumeric categories uniqueCats = [] #List of only unique desktop categories relatedCats = [] #List of related desktop categories elemList = [ ] #List of XML Elements for use as parent objects when SubElementing topLevelsWithSubLevels = { } #Key-Value pair for storing top level directories (string keys) and their related sublevels (values as list) for file in glob.glob("/usr/share/applications/*.desktop"): de = DesktopEntry( file ) #GLOB through each .desktop file in the directory and create a Desktop Entry for each apps.append(de) #Append each Desktop entry to apps cats = de.getCategories() #Get the categories related to each DE if (len(cats) != 0): #If categories field is not empty mainCat = cats[ 0] #Set the main category for the application to its first listed category uniqueCats.append(mainCat) #Generate a list of every apps categories uniqueCats = sorted( set(uniqueCats) ) #Use set to get rid of duplicate categories and sorted to organize/alphabetize them with open('/root/.config/compiz/boxmenu/menu.xml', "w") as f: f.write('<menu></menu>') tree = XML.parse('/root/.config/compiz/boxmenu/menu.xml' ) #Parse base XML file which should contain <menu></menu> root = tree.getroot() #Get <menu></menu> as root
exc = TERM_CMD + exc if de.getPath(): exc = "cd '" + de.getPath() + "'; " + exc execs[hsh] = hsh + " " + exc + "\n" categories = {} for dir in SEARCH_DIRS: fnames = glob.glob(dir + '/*/*.desktop') for fname in fnames: de = DesktopEntry(fname) cats = de.getCategories() or ["All"] for cat in cats: if not cat in categories: categories[cat] = [] categories[cat].append(de) for k, v in sorted(categories.items()): catName = k for de in v: if de.getNoDisplay() or de.getHidden(): continue mainGroup = de.defaultGroup printEntry(catName, de, True)