def get_bookmarks(): logging.info("Loading gtk-bookmarks") basename = os.path.basename places = [] with open(BOOKMARKS_PATH) as f: for line in f: if not line.strip(): continue items = line.strip().split(" ", 1) uri = items[0] with gtk_lock: gfile = gio.File( uri ) # IGNORE:E1101 @UndefinedVariable Keep PyLint and PyDev happy if len(items) > 1: title = items[1].rstrip() else: disp = gfile.get_parse_name() title = basename(disp) locpath = gfile.get_path() new_uri = gfile.get_uri() if locpath: shortcut = shortcuts.Shortcut("%s [places]" % title, shortcuts.SHORTCUT_TYPE_FOLDER, locpath, category=SHORTCUT_CATEGORY) else: shortcut = shortcuts.Shortcut("%s [places]" % title, shortcuts.SHORTCUT_TYPE_URL, new_uri, category=SHORTCUT_CATEGORY) #print shortcut places.append(shortcut) logging.info("Loaded %d gtk-bookmarks" % len(places)) return places
def _get_runnable_shortcuts_from_dir(directory, category): result = [] # Optimization splitext = os.path.splitext pathjoin = os.path.join get_app_info = gio.unix.desktop_app_info_new_from_filename for f in os.listdir(directory): if splitext(f)[1] != ".desktop": continue f = pathjoin(directory, f) with gtk_lock: try: #print f ds = get_app_info(f) #print ds except RuntimeError: continue try: name = ds.get_name().lower() #print name executable = ds.get_executable() #print executable t = get_shortcut_type(executable) except AttributeError: continue #cmdline = ds.get_commandline() shortcut = shortcuts.Shortcut(name, t, f, category=category) result.append(shortcut) return result
def get_recent_documents(max_days=None, for_application_named=None): items = recent_manager.get_items() item_leaves = [] for item in items: if for_application_named and for_application_named.lower() not in set( a.lower() for a in item.get_applications()): continue day_age = item.get_age() if max_days >= 0 and day_age > max_days: continue if not item.exists(): continue uri = item.get_uri() name = item.get_short_name() if item.is_local(): leaf = gio.File(uri).get_path() else: leaf = uri target_type = shortcuts.SHORTCUT_TYPE_DOCUMENT shortcut = shortcuts.Shortcut(name, target_type, leaf, category=SHORTCUT_CATEGORY) item_leaves.append(shortcut) return item_leaves
def get_shortcuts(): result = [] for file_name in os.listdir(LEARN_AS_DIR): name = os.path.basename(file_name).lower() filepath = os.path.join(LEARN_AS_DIR, file_name) shortcut_type = get_shortcut_type(filepath) shortcut = shortcuts.Shortcut(name, shortcut_type, filepath) result.append(shortcut) return result
def get_applications(): applications = glob.glob('/Applications/*.app') result = [] for file_name in applications: name = os.path.splitext(os.path.basename(file_name))[0].lower() filepath = os.path.join(LEARN_AS_DIR, file_name) shortcut_type = get_shortcut_type(filepath) shortcut = shortcuts.Shortcut(name, shortcut_type, filepath) result.append(shortcut) return result
def get_desktop_shortcuts(): logging.info("open-command: Loading desktop shortcuts") s = _get_runnable_shortcuts_from_dir(DESKTOP_DIR, SHORTCUT_CATEGORY_DESKTOP) s.append( shortcuts.Shortcut("Desktop", shortcuts.SHORTCUT_TYPE_FOLDER, DESKTOP_DIR, category=SHORTCUT_CATEGORY_DESKTOP)) logging.info("open-command: Loaded %d desktop shortcuts" % len(s)) return s
def _save_shortcut(self, shortcut_name, file_name): shortcut_file_path = os.path.join(self._get_learn_as_dir(), shortcut_name) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() os.symlink(file, shortcut_file_path) return shortcuts.Shortcut(shortcut_name, self._get_shortcut_type(file), shortcut_file_path)
def read_task_links_xml(xml_file=None, xml=None): shortcuts_list = [] APPS_NS = "http://schemas.microsoft.com/windows/cpltasks/v1" TASKS_NS = "http://schemas.microsoft.com/windows/tasks/v1" TASKS_NS2 = "http://schemas.microsoft.com/windows/tasks/v2" if xml_file: tree = ElementTree.ElementTree().parse(xml_file) elif xml: tree = ElementTree.fromstring(xml) else: raise AssertionError("Provide xml_file or xml parameter.") for app in tree.findall("{%s}application" % APPS_NS): for item in app.findall('{%s}task' % TASKS_NS): name = item.findtext("{%s}name" % TASKS_NS) cmd = item.findtext("{%s}command" % TASKS_NS) cp = item.find("{%s}controlpanel" % TASKS_NS2) if cmd is not None or cp is not None: name = read_mui_string_from_dll(name) name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore').lower() if cp is not None: cname = cp.get('name') cpage = cp.get('page') if cpage: cmd = "control.exe /name %s /page %s" % (cname, cpage) else: cmd = "control.exe /name %s" % cname else: pass #print cmd sh = shortcuts.Shortcut(u"%s (control panel)" % name, shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, cmd) shortcuts_list.append(sh) return shortcuts_list
def get_learned_shortcuts(): logging.info("Loading learn-as shortcuts") result = [] # Optimization splitext = os.path.splitext pathjoin = os.path.join basename = os.path.basename for f in os.listdir(LEARN_AS_DIR): name = splitext(basename(f).lower())[0] filepath = pathjoin(LEARN_AS_DIR, f) t = get_file_type(filepath) shortcut = shortcuts.Shortcut(name, t, filepath, shortcut_filename=filepath, category=SHORTCUT_CATEGORY) result.append(shortcut) #print result logging.info("Loaded %d shortcuts" % len(result)) return result
def _save_shortcut(self, shortcut_name, text): target = text file_type = self._get_shortcut_type(target) if file_type == shortcuts.SHORTCUT_TYPE_EXECUTABLE: shortcut_file_path = os.path.join(self._get_learn_as_dir(), shortcut_name + (".desktop")) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() dsk = xdg.DesktopEntry.DesktopEntry(shortcut_file_path) dsk.set("Version", "1.0") dsk.set("Name", shortcut_name) dsk.set("Type", "Application") dsk.set("Icon", "application-x-executable") dsk.set("Exec", target) dsk.set("Terminal", "0") dsk.set("Path", os.path.abspath(os.path.dirname(target))) dsk.write(trusted=True) with suppress(Exception): subprocess.Popen( ["gnome-desktop-item-edit", shortcut_file_path]) elif file_type == shortcuts.SHORTCUT_TYPE_FOLDER: shortcut_file_path = os.path.join(self._get_learn_as_dir(), shortcut_name + (".desktop")) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() dsk = xdg.DesktopEntry.DesktopEntry(shortcut_file_path) dsk.set("Version", "1.0") dsk.set("Name", shortcut_name) dsk.set("Type", "Application") dsk.set("Icon", "gtk-directory") dsk.set("Exec", "xdg-open \"%s\"" % os.path.abspath(target)) dsk.set("Terminal", "0") dsk.set("Path", os.path.abspath(target)) file_type = shortcuts.SHORTCUT_TYPE_EXECUTABLE dsk.write(trusted=True) with suppress(Exception): subprocess.Popen( ["gnome-desktop-item-edit", shortcut_file_path]) elif file_type == shortcuts.SHORTCUT_TYPE_URL: shortcut_file_path = os.path.join(self._get_learn_as_dir(), shortcut_name + (".desktop")) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() dsk = xdg.DesktopEntry.DesktopEntry(shortcut_file_path) dsk.set("Version", "1.0") dsk.set("Name", shortcut_name) dsk.set("Icon", "applications-internet") dsk.set("URL", target) dsk.set("Type", "Link") dsk.write(trusted=True) with suppress(Exception): subprocess.Popen( ["gnome-desktop-item-edit", shortcut_file_path]) else: """ #FIXME: This is probably not a good idea to create Application type .desktop entry for a document shortcut_file_path = os.path.join( self._get_learn_as_dir(), shortcut_name + (".desktop") ) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() dsk = xdg.DesktopEntry.DesktopEntry(shortcut_file_path) dsk.set("Version", "1.0") dsk.set("Name", shortcut_name) dsk.set("Type", "Application") dsk.set("Icon", "gtk-directory") dsk.set("Exec", "xdg-open \"%s\"" % os.path.abspath(target)) dsk.set("Terminal", "0") dsk.set("Path", os.path.abspath(target)) file_type = shortcuts.SHORTCUT_TYPE_EXECUTABLE dsk.write(trusted=True) with suppress(Exception): subprocess.Popen(["gnome-desktop-item-edit", shortcut_file_path]) """ # This would be better, but problem is that on load, it's determined ax executable instead of a document. shortcut_file_path = os.path.join(self._get_learn_as_dir(), shortcut_name) if os.path.isfile(shortcut_file_path): raise ShortcutAlreadyExistsError() os.symlink(target, shortcut_file_path) return shortcuts.Shortcut(shortcut_name, file_type, shortcut_file_path, shortcut_file_path, category=learned_shortcuts.SHORTCUT_CATEGORY) """
def get_control_panel_applets(): """ http://msdn.microsoft.com/en-us/library/cc144195(v=VS.85).aspx """ control_panel_applets = [] cpi = ControlPanelInfo() for clsid in registry.walk_keys(win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ControlPanel\\NameSpace", key_filter_regex=r"\{[A-F0-9-]+\}"): canonical_name = registry.get_value( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "System.ApplicationName") if canonical_name: canonical_name = canonical_name[1] display_name = registry.get_value( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "") if display_name and display_name[1].startswith("@"): display_name = read_mui_string_from_reg( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "") elif display_name: display_name = unicode(display_name[1]) try: localized_name = registry.get_value( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "LocalizedString") if localized_name[0] == win32con.REG_EXPAND_SZ: localized_name = localized_name[2] else: localized_name = localized_name[1] if localized_name.startswith("@"): localized_name = read_mui_string_from_reg( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "LocalizedString") except: localized_name = None try: command = registry.get_value( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s\\Shell\\Open\\Command" % clsid, "") if command[0] == win32con.REG_EXPAND_SZ: command = command[2] else: command = command[1] except: command = None if localized_name: logging.error(u"%s: %s", canonical_name, unicodedata.normalize('NFKD', localized_name).encode('ascii', 'ignore')) #logging.error(u"name: %s", localized_name) else: logging.error(u"%s: %s", canonical_name, display_name) #print clsid, canonical_name, display_name, localized_name, command if not (localized_name or display_name): continue name = unicodedata.normalize( 'NFKD', localized_name if localized_name else display_name ).encode('ascii', 'ignore').lower() control_panel_applets.append( shortcuts.Shortcut( u"%s (control panel)" % name, shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, command if command else "control.exe /name %s" % canonical_name )) try: tasks_file = registry.get_value( win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "System.Software.TasksFileUrl") if tasks_file: if tasks_file[0] == win32con.REG_EXPAND_SZ: tasks_file = tasks_file[2] else: tasks_file = tasks_file[1] if os.path.isfile(tasks_file) and os.path.splitext(tasks_file)[1].lower() == ".xml": control_panel_applets.extend(read_task_links_xml(tasks_file)) except: tasks_file = None # In Windows7 all *.cpl files in windows/system32 directory are disabled # by default due to different means of accessing their functionality # (see above code). However, we are going to process them anyway cpl_disabled_panels = set( cplfile.lower() for cplfile,_,_ in registry.walk_values(win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\don't load", valuetypes = (win32con.REG_SZ,)) ) # List control-panel applets from system directory cpl_files = [cpl for cpl in glob.iglob(os.path.join(os.path.expandvars("${WINDIR}"), "system32", "*.cpl")) if os.path.basename(cpl).lower() not in cpl_disabled_panels] # Add control-panel applets from custom locations, read from registry for _, cplfile, _ in registry.walk_values( win32con.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls", valuetypes = (win32con.REG_EXPAND_SZ, win32con.REG_SZ), autoexpand = True): if not os.path.isfile(cplfile): continue if os.path.basename(cplfile).lower() in cpl_disabled_panels: continue cpl_files.append(cplfile) # Read descriptions of control-panel applets from the .cpl files directly for cplfile in cpl_files: for file, name, desc, index in cpi.get_cplinfo(cplfile): name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') name = name.lower() #name = xml_escape(name) control_panel_applets.append( shortcuts.Shortcut( name + " (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "rundll32.exe shell32.dll,Control_RunDLL \"%s\",@%d" % (cplfile, index) )) del cpi # Extend control-panel applets list with few useful shortcuts that can't be # reached from the control-panel directly control_panel_applets.extend([ shortcuts.Shortcut( u"control panel", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "rundll32.exe shell32.dll,Control_RunDLL"), shortcuts.Shortcut( u"about microsoft windows (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "rundll32.exe shell32.dll,ShellAboutW "), shortcuts.Shortcut( u"safely remove hardware", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "rundll32.exe shell32.dll,Control_RunDLL HotPlug.dll"), ]) """ shortcuts.Shortcut( u"unplug or eject hardware", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "rundll32.exe shell32.dll,Control_RunDLL HotPlug.dll"), shortcuts.Shortcut( u"device manager (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, #"rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl,,1" "rundll32.exe devmgr.dll DeviceManager_Execute"), shortcuts.Shortcut( u"disk management (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "diskmgmt.msc"), shortcuts.Shortcut( u"scheduled tasks (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "control.exe schedtasks"), shortcuts.Shortcut( u"scanners and cameras (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "control.exe sticpl.cpl"), shortcuts.Shortcut( u"removable storage (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "ntmsmgr.msc"), shortcuts.Shortcut( u"performance monitor (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "perfmon.msc"), shortcuts.Shortcut( u"private character editor (control panel)", shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, "eudcedit.msc"), """ control_panel_applets.extend( read_task_links_xml(xml=read_default_windows7_tasklist_xml())) return control_panel_applets
def get_applications(): logging.info("open-command: Loading application shortcuts") # Add this to the default whitelist = set([ # if you set/reset default handler for folders it is useful "nautilus-folder-handler.desktop", # we think that these are useful to show "eog.desktop", "evince.desktop", "gnome-about.desktop", "gstreamer-properties.desktop", "notification-properties.desktop", ]) blacklist = set([ "nautilus-home.desktop", ]) result = [] if DESKTOP_ENVIRONMENT: desktop_app_info_set_desktop_env(DESKTOP_ENVIRONMENT) isabs = os.path.isabs isfile = os.path.isfile islink = os.path.islink with gtk_lock: for item in app_info_get_all(): id_ = item.get_id() if id_ in whitelist or (item.should_show() and id_ not in blacklist): name = item.get_name().lower() filepath = item.get_executable() shortcut_filename = None try: shortcut_filename = item.props.filename except: try: shortcut_filename = item.get_property("filename") except: try: shortcut_filename = item.get_filename() except: pass #print filepath,";",item.get_commandline(),";",item.get_description() # Need to check for existence of the file, as sometimes the app does not disappear from the list if not ptoperly uninstalled if filepath and filepath.strip() != "": if isabs(filepath): if not (isfile(filepath) or islink(filepath)): continue else: if not find_executable(filepath): continue applications_dict[name] = item s_type = shortcuts.SHORTCUT_TYPE_EXECUTABLE # get_shortcut_type(filepath) shortcut = shortcuts.Shortcut( name, s_type, filepath.strip(), shortcut_filename=shortcut_filename, category=SHORTCUT_CATEGORY) result.append(shortcut) #print "\n".join(sorted(str(s) for s in result)) logging.info("open-command: Loaded %d application shortcuts" % len(result)) return result
'ascii', 'ignore')) #logging.error(u"name: %s", localized_name) else: logging.error(u"%s: %s", canonical_name, display_name) #print clsid, canonical_name, display_name, localized_name, command if not (localized_name or display_name): continue name = localized_name if localized_name else display_name name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore').lower() control_panel_applets.append( shortcuts.Shortcut( (u"%s (control panel)" % name) if use_categories else name, shortcuts.SHORTCUT_TYPE_CONTROL_PANEL, command if command else "control.exe /name %s" % canonical_name)) try: tasks_file = registry.get_value(win32con.HKEY_CLASSES_ROOT, "CLSID\\%s" % clsid, "System.Software.TasksFileUrl") if tasks_file: if tasks_file[0] == win32con.REG_EXPAND_SZ: tasks_file = tasks_file[2] else: tasks_file = tasks_file[1] if os.path.isfile(tasks_file) and os.path.splitext( tasks_file)[1].lower() == ".xml": control_panel_applets.extend(