def get_notebook_list(): '''Returns a list of known notebooks as a L{NotebookInfoList} This will load the list from the default X{notebooks.list} file ''' file = ConfigManager.get_config_file('notebooks.list') return NotebookInfoList(file)
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. ''' properties['Type'] = 'X-Zim-CustomTool' dir = XDG_CONFIG_HOME.subdir('zim/customtools') tool = _create_application(dir, Name, '', klass=CustomTool, 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 __init__(self): self._names = [] self._tools = {} self._listfile = ConfigManager.get_config_file( 'customtools/customtools.list') self._read_list() self._listfile.connect('changed', self._on_list_changed)
def get_notebook_list(): '''Returns a list of known notebooks as a L{NotebookInfoList} This will load the list from the default X{notebooks.list} file ''' config = ConfigManager() # XXX should be passed in file = config.get_config_file('notebooks.list') return NotebookInfoList(file)
def init_uistate(self): # Initialize all the uistate parameters # delayed till show or show_all because all this needs real # uistate to be in place and plugins to be loaded # Run between loading plugins and actually presenting the window to the user if not self._geometry_set: # Ignore this if an explicit geometry was specified to the constructor if self.uistate['windowpos'] is not None: x, y = self.uistate['windowpos'] self.move(x, y) w, h = self.uistate['windowsize'] self.set_default_size(w, h) if self.uistate['windowmaximized']: self.maximize() Window.init_uistate(self) # takes care of sidepane positions etc self.toggle_fullscreen(self._set_fullscreen) # And hook to notebook properties self.on_notebook_properties_changed(self.notebook.properties) self.notebook.properties.connect('changed', self.on_notebook_properties_changed) # Notify plugins self.emit('init-uistate') # Update menus etc. self.uimanager.ensure_update() # Prevent flashing when the toolbar is loaded after showing the window # and do this before connecting signal below for accelmap. # Load accelmap config and setup saving it # TODO - this probably belongs in the application class, not here accelmap = ConfigManager.get_config_file('accelmap').file logger.debug('Accelmap: %s', accelmap.path) if accelmap.exists(): Gtk.AccelMap.load(accelmap.path) def on_accel_map_changed(o, path, key, mod): logger.info('Accelerator changed for %s', path) Gtk.AccelMap.save(accelmap.path) Gtk.AccelMap.get().connect('changed', on_accel_map_changed)
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 load_file(self): self.symbols = {} self.symbol_order = [] file = ConfigManager.get_config_file('symbols.list') for line in file.readlines(): line = line.strip() if not line or line.startswith('#'): continue try: if '#' in line: line, _ = line.split('#', 1) line = line.strip() shortcut, code = line.split() symbol = chr(int(code)) if not shortcut in self.symbols: self.symbols[shortcut] = symbol self.symbol_order.append(shortcut) else: logger.exception('Shortcut defined twice: %s', shortcut) except: logger.exception('Could not parse symbol: %s', line)
class CustomToolManager(object): '''Manager for dealing with the desktop files which are used to store custom tools. Custom tools are external commands that are intended to show in the "Tools" menu in zim (and optionally in the tool bar). They are defined as desktop entry files in a special folder (typically "~/.local/share/zim/customtools") and use several non standard keys. See L{CustomTool} for details. This object is iterable and maintains a specific order for tools to be shown in in the user interface. ''' def __init__(self): self.config = ConfigManager() # XXX should be passed in self.names = [] self.tools = {} self._read_list() def _read_list(self): file = self.config.get_config_file('customtools/customtools.list') seen = set() for line in file.readlines(): name = line.strip() if not name in seen: seen.add(name) self.names.append(name) def _write_list(self): file = self.config.get_config_file('customtools/customtools.list') file.writelines([name + '\n' for name in self.names]) def __iter__(self): for name in self.names: tool = self.get_tool(name) if tool and tool.isvalid(): yield tool def get_tool(self, name): '''Get a L{CustomTool} by name. @param name: the tool name @returns: a L{CustomTool} object ''' if not name in self.tools: file = self.config.get_config_file('customtools/%s.desktop' % name) tool = CustomTool(file) self.tools[name] = tool return self.tools[name] 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. ''' properties['Type'] = 'X-Zim-CustomTool' dir = XDG_CONFIG_HOME.subdir('zim/customtools') tool = _create_application(dir, Name, '', klass=CustomTool, NoDisplay=False, **properties) self.tools[tool.key] = tool self.names.append(tool.key) self._write_list() return tool def delete(self, tool): '''Remove a custom tool from the list and delete the definition file. @param tool: a custom tool name or L{CustomTool} object ''' if not isinstance(tool, CustomTool): tool = self.get_tool(tool) tool.file.remove() self.tools.pop(tool.key) self.names.remove(tool.key) self._write_list() def index(self, tool): '''Get the position of a specific tool in the list. @param tool: a custom tool name or L{CustomTool} object @returns: an integer for the position ''' if isinstance(tool, CustomTool): tool = tool.key return self.names.index(tool) def reorder(self, tool, i): '''Change the position of a tool in the list. @param tool: a custom tool name or L{CustomTool} object @param i: the new position as integer ''' if not 0 <= i < len(self.names): return if isinstance(tool, CustomTool): tool = tool.key j = self.names.index(tool) self.names.pop(j) self.names.insert(i, tool) # Insert before i. If i was before old position indeed before # old item at that position. However if i was after old position # if shifted due to the pop(), now it inserts after the old item. # This is intended behavior to make all moves possible. self._write_list()
def setUp(self): config = ConfigManager() list = config.get_config_file('notebooks.list') file = list.file if file.exists(): file.remove()
class CustomToolManager(object): '''Manager for dealing with the desktop files which are used to store custom tools. Custom tools are external commands that are intended to show in the "Tools" menu in zim (and optionally in the tool bar). They are defined as desktop entry files in a special folder (typically "~/.local/share/zim/customtools") and use several non standard keys. See L{CustomTool} for details. This object is iterable and maintains a specific order for tools to be shown in in the user interface. ''' def __init__(self): self.config = ConfigManager() # XXX should be passed in self.names = [] self.tools = {} self._read_list() def _read_list(self): file = self.config.get_config_file('customtools/customtools.list') seen = set() for line in file.readlines(): name = line.strip() if not name in seen: seen.add(name) self.names.append(name) def _write_list(self): file = self.config.get_config_file('customtools/customtools.list') file.writelines([name + '\n' for name in self.names]) def __iter__(self): for name in self.names: tool = self.get_tool(name) if tool and tool.isvalid(): yield tool def get_tool(self, name): '''Get a L{CustomTool} by name. @param name: the tool name @returns: a L{CustomTool} object ''' if not name in self.tools: file = self.config.get_config_file('customtools/%s.desktop' % name) tool = CustomTool(file) self.tools[name] = tool return self.tools[name] 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. ''' properties['Type'] = 'X-Zim-CustomTool' dir = XDG_CONFIG_HOME.subdir('zim/customtools') tool = _create_application(dir, Name, '', klass=CustomTool, NoDisplay=False, **properties) self.tools[tool.key] = tool self.names.append(tool.key) self._write_list() return tool def delete(self, tool): '''Remove a custom tool from the list and delete the definition file. @param tool: a custom tool name or L{CustomTool} object ''' if not isinstance(tool, CustomTool): tool = self.get_tool(tool) tool.file.remove() self.tools.pop(tool.key) self.names.remove(tool.key) self._write_list() def index(self, tool): '''Get the position of a specific tool in the list. @param tool: a custom tool name or L{CustomTool} object @returns: an integer for the position ''' if isinstance(tool, CustomTool): tool = tool.key return self.names.index(tool) def reorder(self, tool, i): '''Change the position of a tool in the list. @param tool: a custom tool name or L{CustomTool} object @param i: the new position as integer ''' if not 0 <= i < len(self.names): return if isinstance(tool, CustomTool): tool = tool.key j = self.names.index(tool) self.names.pop(j) self.names.insert(i, tool) # Insert before i. If i was before old position indeed before # old item at that position. However if i was after old position # if shifted due to the pop(), now it inserts after the old item. # This is intended behavior to make all moves possible. self._write_list()
def init_uistate(self): # Initialize all the uistate parameters # delayed till show or show_all because all this needs real # uistate to be in place and plugins to be loaded # Run between loading plugins and actually presenting the window to the user if not self._geometry_set: # Ignore this if an explicit geometry was specified to the constructor if self.uistate['windowpos'] is not None: x, y = self.uistate['windowpos'] self.move(x, y) w, h = self.uistate['windowsize'] self.set_default_size(w, h) if self.uistate['windowmaximized']: self.maximize() # For these two "None" means system default, but we don't know what that default is :( self.preferences.setdefault( 'toolbar_style', None, (TOOLBAR_ICONS_ONLY, TOOLBAR_ICONS_AND_TEXT, TOOLBAR_TEXT_ONLY)) self.preferences.setdefault( 'toolbar_size', None, (TOOLBAR_ICONS_TINY, TOOLBAR_ICONS_SMALL, TOOLBAR_ICONS_LARGE)) self.toggle_toolbar(self.uistate['show_toolbar']) self.toggle_statusbar(self.uistate['show_statusbar']) Window.init_uistate(self) # takes care of sidepane positions etc if self.preferences['toolbar_style'] is not None: self.set_toolbar_style(self.preferences['toolbar_style']) if self.preferences['toolbar_size'] is not None: self.set_toolbar_icon_size(self.preferences['toolbar_size']) self.toggle_fullscreen(self._set_fullscreen) if self.notebook.readonly: self.toggle_editable(False) action = self.actiongroup.get_action('toggle_editable') action.set_sensitive(False) else: self.toggle_editable(not self.uistate['readonly']) # And hook to notebook properties self.on_notebook_properties_changed(self.notebook.properties) self.notebook.properties.connect('changed', self.on_notebook_properties_changed) # Hook up the statusbar self.connect('page-changed', self.do_update_statusbar) self.connect('readonly-changed', self.do_update_statusbar) self.pageview.connect('modified-changed', self.do_update_statusbar) self.notebook.connect_after('stored-page', self.do_update_statusbar) # Notify plugins self.emit('init-uistate') # Update menus etc. self.uimanager.ensure_update() # Prevent flashing when the toolbar is loaded after showing the window # and do this before connecting signal below for accelmap. # Add search bar onec toolbar is loaded space = Gtk.SeparatorToolItem() space.set_draw(False) space.set_expand(True) self.toolbar.insert(space, -1) from zim.gui.widgets import InputEntry entry = InputEntry(placeholder_text=_('Search')) entry.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, Gtk.STOCK_FIND) entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True) entry.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, _('Search Pages...')) # T: label in search entry inline_search = lambda e, *a: self._uiactions.show_search( query=e.get_text() or None) entry.connect('activate', inline_search) entry.connect('icon-release', inline_search) entry.show() item = Gtk.ToolItem() item.add(entry) self.toolbar.insert(item, -1) # Load accelmap config and setup saving it # TODO - this probably belongs in the application class, not here accelmap = ConfigManager.get_config_file('accelmap').file logger.debug('Accelmap: %s', accelmap.path) if accelmap.exists(): Gtk.AccelMap.load(accelmap.path) def on_accel_map_changed(o, path, key, mod): logger.info('Accelerator changed for %s', path) Gtk.AccelMap.save(accelmap.path) Gtk.AccelMap.get().connect('changed', on_accel_map_changed) self.do_update_statusbar()
def setUp(self): config = ConfigManager() list = config.get_config_file('notebooks.list') file = list.file if file.exists(): file.remove()
def on_edit(self, button): file = ConfigManager.get_config_file('symbols.list') if edit_config_file(self, file): self.plugin.load_file() self.load_symbols()