def create(self, Name, **properties): '''Create a new custom tool @param Name: the name to show in the Tools menu @param properties: properties for the custom tool, e.g.: - Comment - Icon - X-Zim-ExecTool - X-Zim-ReadOnly - X-Zim-ShowInToolBar @returns: a new L{CustomTool} object. ''' dir = XDG_CONFIG_HOME.subdir('zim/customtools') basename = cleanup_filename(Name.lower()) + '-usercreated.desktop' tool = _create_application(dir, basename, Name, '', NoDisplay=False, **properties) # XXX - hack to ensure we link to configmanager file = ConfigManager.get_config_file('customtools/' + tool.file.basename) tool.file = file file.connect('changed', partial(self._on_tool_changed, tool)) self._tools[tool.key] = tool self._names.append(tool.key) self._write_list() return tool
def _create_application(dir, Name, Exec, klass=None, NoDisplay=True, **param): n = cleanup_filename(Name.lower()) + '-usercreated' key = n file = dir.file(key + '.desktop') i = 0 while file.exists(): assert i < 1000, 'BUG: Infinite loop ?' i += 1 key = n + '-' + str(i) file = dir.file(key + '.desktop') if klass is None: klass = DesktopEntryFile entry = klass(file) type = param.pop('Type', 'Application') entry.update( Type=type, Version=1.0, NoDisplay=NoDisplay, Name=Name, Exec=Exec, **param ) assert entry.isvalid(), 'BUG: created invalid desktop entry' entry.write() if param.get('MimeType'): # Update mimetype cache cache = dir.file('mimeinfo.cache') if not cache.exists(): lines = ['[MIME Cache]\n'] else: lines = cache.readlines() mimetype = param.get('MimeType') for i, line in enumerate(lines): if line.startswith(mimetype + '='): lines[i] = line.strip() + ';' + key + '.desktop\n' break else: lines.append(mimetype + '=' + key + '.desktop\n') cache.writelines(lines) return entry
def _create_application(dir, Name, Exec, klass=None, NoDisplay=True, **param): n = cleanup_filename(Name.lower()) + '-usercreated' key = n file = dir.file(key + '.desktop') i = 0 while file.exists(): assert i < 1000, 'BUG: Infinite loop ?' i += 1 key = n + '-' + str(i) file = dir.file(key + '.desktop') if klass is None: klass = DesktopEntryFile entry = klass(file) entry.update( Type=param.pop('Type', 'Application'), Version=1.0, NoDisplay=NoDisplay, Name=Name, Exec=Exec, **param ) assert entry.isvalid(), 'BUG: created invalid desktop entry' entry.write() if param.get('MimeType'): # Update mimetype cache cache = dir.file('mimeinfo.cache') if not cache.exists(): lines = ['[MIME Cache]\n'] else: lines = cache.readlines() mimetype = param.get('MimeType') for i, line in enumerate(lines): if line.startswith(mimetype + '='): lines[i] = line.strip() + ';' + key + '.desktop\n' break else: lines.append(mimetype + '=' + key + '.desktop\n') cache.writelines(lines) return entry
def get_tool(self, name): '''Get a L{CustomTool} by name. @param name: the tool name @returns: a L{CustomTool} object or C{None} ''' if not '-usercreated' in name: name = cleanup_filename(name.lower()) + '-usercreated' if not name in self._tools: file = ConfigManager.get_config_file('customtools/%s.desktop' % name) if file.exists(): tool = CustomTool(file) self._tools[name] = tool file.connect('changed', partial(self._on_tool_changed, tool)) else: return None return self._tools[name]
def create(mimetype, Name, Exec, **param): '''Create a new usercreated desktop entry which defines a custom command to handle a certain file type. Note that the name under which this definition is stored is not the same as C{Name}. Check the 'C{key}' attribute of the returned object if you want the name to retrieve this application later. @param mimetype: the file mime-type to handle with this command @param Name: the name to show in e.g. the "Open With.." menu @param Exec: the command to run as string (will be split on whitespace, so quote arguments that may contain a space). @param param: any additional keys for the desktop entry @returns: the L{DesktopEntryFile} object with some sensible defaults for a user created application entry. ''' dir = XDG_DATA_HOME.subdir('applications') param['MimeType'] = mimetype basename = cleanup_filename(Name.lower()) + '-usercreated.desktop' file = _create_application(dir, basename, Name, Exec, **param) return file