def on_action_new_activate(self, action): """Define a new host""" dialog = UIHost(parent=self.ui.win_main, hosts=self.model_hosts) response = dialog.show(default_name='', default_description='', title=_('Add a new host'), treeiter=None) if response == Gtk.ResponseType.OK: destinations = dialog.model_destinations.dump() associations = dialog.model_associations.dump() host = HostInfo(dialog.name, dialog.description) # Set the associations for values in associations: (destination_name, description, service_name, service_arguments) = associations[values] destination = destinations[destination_name] arguments = json.loads(service_arguments) host.add_association(description=description, destination_name=destination_name, service_name=service_name, arguments=arguments) self.add_host(host=host, destinations=destinations, update_settings=True) # Automatically select the newly added host self.ui.tvw_connections.set_cursor( path=self.model_hosts.get_path_by_name(dialog.name), column=None, start_editing=False) dialog.destroy()
def reload_hosts(self): """Load hosts from the settings files""" self.model_hosts.clear() self.hosts.clear() hosts_path = self.get_current_group_path() # Fix bug where the groups model isn't yet emptied, resulting in # being still used after a clear, then an invalid path if not os.path.isdir(hosts_path): return for filename in os.listdir(hosts_path): # Skip folders, used for groups if os.path.isdir(os.path.join(hosts_path, filename)): continue debug.add_info('Loading host %s' % os.path.join(hosts_path, filename)) settings_host = settings.Settings(filename=os.path.join( hosts_path, filename), case_sensitive=True) name = settings_host.get(SECTION_HOST, OPTION_HOST_NAME) description = settings_host.get(SECTION_HOST, OPTION_HOST_DESCRIPTION) host = HostInfo(name=name, description=description) destinations = {} # Load host destinations if SECTION_DESTINATIONS in settings_host.get_sections(): for option in settings_host.get_options(SECTION_DESTINATIONS): value = settings_host.get(SECTION_DESTINATIONS, option) destinations[option] = DestinationInfo(name=option, value=value) # Load associations association_index = 1 associations_count = settings_host.get_int( section=SECTION_HOST, option=OPTION_HOST_ASSOCIATIONS) while association_index <= associations_count: section = '%s %d' % (SECTION_ASSOCIATION, association_index) host.add_association( description=settings_host.get( section=section, option=OPTION_ASSOCIATION_DESCRIPTION), destination_name=settings_host.get( section=section, option=OPTION_ASSOCIATION_DESTINATION), service_name=settings_host.get( section=section, option=OPTION_ASSOCIATION_SERVICE), arguments=json.loads( settings_host.get( section=section, option=OPTION_ASSOCIATION_ARGUMENTS))) association_index += 1 self.add_host(host, destinations, False)
def on_action_copy_activate(self, action): """Copy the selected host to another""" selected_row = get_treeview_selected_row(self.ui.tvw_connections) if selected_row: if self.is_selected_row_host(): # First level (host) name = self.model_hosts.get_key(selected_row) description = self.model_hosts.get_description(selected_row) selected_iter = self.model_hosts.get_iter(name) expanded = self.ui.tvw_connections.row_expanded( self.model_hosts.get_path(selected_iter)) dialog = UIHost(parent=self.ui.win_main, hosts=self.model_hosts) # Restore the destinations for the selected host destinations = self.hosts[name].destinations for destination_name in destinations: destination = destinations[destination_name] dialog.model_destinations.add_data(destination) # Restore the associations for the selected host for association in self.hosts[name].associations: service_name = association.service_name if service_name in model_services.services: dialog.model_associations.add_data( index=dialog.model_associations.count(), name=association.destination_name, description=association.description, service=model_services.services[service_name], arguments=association.service_arguments) else: debug.add_warning('service %s not found' % service_name) # Show the edit host dialog response = dialog.show(default_name=_('Copy of %s') % name, default_description='', title=_('Copy host'), treeiter=None) if response == Gtk.ResponseType.OK: destinations = dialog.model_destinations.dump() associations = dialog.model_associations.dump() host = HostInfo(dialog.name, dialog.description) # Set the associations for values in associations: (destination_name, description, service_name, service_arguments) = associations[values] destination = destinations[destination_name] arguments = json.loads(service_arguments) host.add_association(description=description, destination_name=destination_name, service_name=service_name, arguments=arguments) self.add_host(host=host, destinations=destinations, update_settings=True) # Get the path of the host tree_path = self.model_hosts.get_path_by_name(dialog.name) # Automatically select again the previously selected host self.ui.tvw_connections.set_cursor(path=tree_path, column=None, start_editing=False) # Automatically expand the row if it was expanded before if expanded: self.ui.tvw_connections.expand_row(tree_path, False) # Collapse the duplicated row self.ui.tvw_connections.collapse_row( self.model_hosts.get_path(selected_iter))