Example #1
0
    def load_directories(self):
        """Load directories of repository registered in settings."""
        self._directories = {}
        settings = QSettings()
        settings.beginGroup(repo_settings_group())

        # Write online directory first to QSettings if needed
        for online_dir_name in self._online_directories:
            repo_present = False
            for repo_name in settings.childGroups():
                url = settings.value(repo_name + '/url', '', type=unicode)
                if url == self._online_directories[online_dir_name]:
                    repo_present = True
                    break
            if not repo_present:
                self.add_directory(online_dir_name,
                                   self._online_directories[online_dir_name])

        for repo_name in settings.childGroups():
            self._directories[repo_name] = {}
            url = settings.value(repo_name + '/url', '', type=unicode)
            self._directories[repo_name]['url'] = url
            auth_cfg = settings.value(repo_name + '/auth_cfg',
                                      '',
                                      type=unicode).strip()
            self._directories[repo_name]['auth_cfg'] = auth_cfg
        settings.endGroup()
Example #2
0
    def load(self):
        """ populate the mRepositories dict"""
        self.mRepositories = {}
        settings = QSettings()
        settings.beginGroup(reposGroup)
        # first, update repositories in QSettings if needed
        officialRepoPresent = False
        for key in settings.childGroups():
            url = settings.value(key + "/url", "", type=unicode)
            if url == officialRepo[1]:
                officialRepoPresent = True
            if url == officialRepo[2]:
                settings.setValue(key + "/url", officialRepo[1]) # correct a depreciated url
                officialRepoPresent = True
        if not officialRepoPresent:
            settings.setValue(officialRepo[0] + "/url", officialRepo[1])

        for key in settings.childGroups():
            self.mRepositories[key] = {}
            self.mRepositories[key]["url"] = settings.value(key + "/url", "", type=unicode)
            self.mRepositories[key]["authcfg"] = settings.value(key + "/authcfg", "", type=unicode)
            self.mRepositories[key]["enabled"] = settings.value(key + "/enabled", True, type=bool)
            self.mRepositories[key]["valid"] = settings.value(key + "/valid", True, type=bool)
            self.mRepositories[key]["Relay"] = Relay(key)
            self.mRepositories[key]["xmlData"] = None
            self.mRepositories[key]["state"] = 0
            self.mRepositories[key]["error"] = ""
        settings.endGroup()
Example #3
0
    def load(self):
        """ populate the mRepositories dict"""
        self.mRepositories = {}
        settings = QSettings()
        settings.beginGroup(reposGroup)
        # first, update repositories in QSettings if needed
        officialRepoPresent = False
        for key in settings.childGroups():
            url = settings.value(key + "/url", "", type=unicode)
            if url == officialRepo[1]:
                officialRepoPresent = True
            if url == officialRepo[2]:
                settings.setValue(key + "/url",
                                  officialRepo[1])  # correct a depreciated url
                officialRepoPresent = True
        if not officialRepoPresent:
            settings.setValue(officialRepo[0] + "/url", officialRepo[1])

        for key in settings.childGroups():
            self.mRepositories[key] = {}
            self.mRepositories[key]["url"] = settings.value(key + "/url",
                                                            "",
                                                            type=unicode)
            self.mRepositories[key]["enabled"] = settings.value(key +
                                                                "/enabled",
                                                                True,
                                                                type=bool)
            self.mRepositories[key]["valid"] = settings.value(key + "/valid",
                                                              True,
                                                              type=bool)
            self.mRepositories[key]["Relay"] = Relay(key)
            self.mRepositories[key]["xmlData"] = None
            self.mRepositories[key]["state"] = 0
            self.mRepositories[key]["error"] = ""
        settings.endGroup()
Example #4
0
    def selectOutput(self):
        if isinstance(self.output, OutputDirectory):
            self.selectDirectory()
        else:
            popupMenu = QMenu()

            actionSaveToTempFile = QAction(
                self.tr('Save to a temporary file'), self.btnSelect)
            actionSaveToTempFile.triggered.connect(self.saveToTemporaryFile)
            popupMenu.addAction(actionSaveToTempFile)

            actionSaveToFile = QAction(
                self.tr('Save to file...'), self.btnSelect)
            actionSaveToFile.triggered.connect(self.selectFile)
            popupMenu.addAction(actionSaveToFile)

            if isinstance(self.output, OutputVector) \
                    and self.alg.provider.supportsNonFileBasedOutput():
                actionSaveToMemory = QAction(
                    self.tr('Save to memory layer'), self.btnSelect)
                actionSaveToMemory.triggered.connect(self.saveToMemory)
                popupMenu.addAction(actionSaveToMemory)
                actionSaveToPostGIS = QAction(
                    self.tr('Save to PostGIS table...'), self.btnSelect)
                actionSaveToPostGIS.triggered.connect(self.saveToPostGIS)
                settings = QSettings()
                settings.beginGroup('/PostgreSQL/connections/')
                names = settings.childGroups()
                settings.endGroup()
                actionSaveToPostGIS.setEnabled(bool(names))
                popupMenu.addAction(actionSaveToPostGIS)

            popupMenu.exec_(QCursor.pos())
    def onBrowseConnection(self):
        s = QSettings()
        base = "/PostgreSQL/connections"
        s.beginGroup("/PostgreSQL/connections")
        children = s.childGroups()
        connections = {}
        map = {
            "dbname": "database",
            "host": "host",
            "port": "port",
            "service": "service",
            "password": "******",
            "user": "******"
        }
        for g in children:
            s.beginGroup(g)
            cstring = ""
            for k, v in map.items():
                if s.value(v):
                    cstring += k + "=" + s.value(v) + " "
            connections[g] = cstring
            s.endGroup()

        menu = QMenu(self)
        for k in sorted(connections.keys()):
            menu.addAction(k)

        def onMenu(action):
            self.dbConnectionText.setText(connections[action.text()])
            self.reloadBtn.click()

        menu.triggered.connect(onMenu)
        menu.exec_(self.dbConnectionBtn.mapToGlobal(QPoint(0, 0)))
class PluginListWidgetModel(QStandardItemModel):
    """This model will create its own items, from the ``QSettings``
    where plugins is set to *load*.
    """
    def __init__(self, parent=None):
        QStandardItemModel.__init__(self, parent)
        self._settings = QSettings()
        self._settings.beginGroup("plugins")
        self.pluginloader = PluginLoader()
        self.pluginloader.pluginsToLoad = self.__checkToLoad()

        for plugin in self.pluginloader.plugins:
            if plugin.load == True:
                item = QStandardItem(plugin.pluginUserString)
                if plugin.icon:
                    item.setIcon(plugin.icon)
                font = item.font()
                font.setPointSize(font.pointSize() + 4)
                item.setFont(font)
                item.setEditable(False)
                item.plugin = plugin
                self.appendRow(item)

    def __checkToLoad(self):
        pluginlist = []

        # When beginGroup is set to plugins, the childgroups will be
        # each of the plugins.
        for plugin in self._settings.childGroups():
            valueString = str(plugin) + "/load"
            value = self._settings.value(valueString, "True").toString()
            if value == "True":
                pluginlist.append(str(plugin))

        return pluginlist
Example #7
0
    def __init__(self, parent, tablename):
        super(PostgisTableSelector, self).__init__(parent)
        self.connection = None
        self.table = None
        self.schema = None
        self.setupUi(self)
        settings = QSettings()
        settings.beginGroup('/PostgreSQL/connections/')
        names = settings.childGroups()
        settings.endGroup()
        for n in names:
            item = ConnectionItem(n)
            self.treeConnections.addTopLevelItem(item)

        def itemExpanded(item):
            try:
                item.populateSchemas()
            except:
                pass

        self.treeConnections.itemExpanded.connect(itemExpanded)

        self.textTableName.setText(tablename)

        self.buttonBox.accepted.connect(self.okPressed)
        self.buttonBox.rejected.connect(self.cancelPressed)
Example #8
0
def addBoundlessRepository():
    """Add Boundless plugin repository to list of the available
       plugin repositories if it is not presented here
    """
    settings = QSettings('Boundless', 'BoundlessConnect')
    repoUrl = settings.value('repoUrl', '', unicode)

    if repoUrl == '':
        setRepositoryUrl()

    if isRepositoryInDirectory():
        return

    settings = QSettings()
    settings.beginGroup(reposGroup)
    hasBoundlessRepository = False
    for repo in settings.childGroups():
        url = settings.value(repo + '/url', '', unicode)
        if url == repoUrl:
            hasBoundlessRepository = True
    # Boundless repository not found, so we add it to the list
    if not hasBoundlessRepository:
        settings.setValue(boundlessRepoName + '/url', repoUrl)
        settings.setValue(boundlessRepoName + '/authcfg', '')
    settings.endGroup()
 def getQGISDbs(self):
     """Return all PostGIS connection settings stored in QGIS
     :return: connection dict() with name and other settings
     """
     con_settings = []
     settings = QSettings()
     settings.beginGroup('/PostgreSQL/connections')
     for item in settings.childGroups():
         con = dict()
         con['name'] = unicode(item)
         con['host'] = unicode(settings.value(u'%s/host' % unicode(item)))
         con['port'] = unicode(settings.value(u'%s/port' % unicode(item)))
         con['database'] = unicode(
             settings.value(u'%s/database' % unicode(item)))
         con['username'] = unicode(
             settings.value(u'%s/username' % unicode(item)))
         con['password'] = unicode(
             settings.value(u'%s/password' % unicode(item)))
         con_settings.append(con)
     settings.endGroup()
     dbs = {}
     if len(con_settings) > 0:
         for conn in con_settings:
             dbs[conn['name']] = conn
     return dbs
Example #10
0
    def on_removeMissingPushButton_clicked(self):
        '''
        Remove missing databases from QSetttings
        '''
        servers = self.serverWidget.getServers()
        settings = QSettings()
        settings.beginGroup('PostgreSQL/connections')
        candidates = settings.childGroups()
        settings.endGroup()
        removedConn = []
        dbList = []
        try:
            dbList = self.serverWidget.abstractDb.getDbsFromServer()
        except Exception as e:
            QMessageBox.critical(self, self.tr('Critical!'), ':'.join(e.args))

        for candidate in candidates:
            candidateSettings = QSettings()
            candidateSettings.beginGroup('PostgreSQL/connections/' + candidate)
            candidateDb = candidateSettings.value('database')
            if candidateDb not in dbList:
                self.removeConnections(candidate, removedConn)
        if len(removedConn) > 0:
            msg = self.tr(
                '\nThe following databases connections were removed successfully:\n'
            ) + ', '.join(removedConn)
        else:
            msg = self.tr('No connections were removed.')
        QMessageBox.warning(self, self.tr("Warning!"), msg)
Example #11
0
class PluginListWidgetModel(QStandardItemModel):
    """This model will create its own items, from the ``QSettings``
    where plugins is set to *load*.
    """
    def __init__(self, parent = None):
        QStandardItemModel.__init__(self, parent)
        self._settings = QSettings()
        self._settings.beginGroup("plugins")
        self.pluginloader = PluginLoader()
        self.pluginloader.pluginsToLoad = self.__checkToLoad()

        for plugin in self.pluginloader.plugins:
            if plugin.load == True:
                item = QStandardItem(plugin.pluginUserString)
                if plugin.icon:
                    item.setIcon(plugin.icon)
                font = item.font()
                font.setPointSize(font.pointSize() + 4)
                item.setFont(font)
                item.setEditable(False)
                item.plugin = plugin
                self.appendRow(item)

    def __checkToLoad(self):
        pluginlist = []

        # When beginGroup is set to plugins, the childgroups will be
        # each of the plugins.
        for plugin in self._settings.childGroups():
            valueString = str(plugin) + "/load"
            value = self._settings.value(valueString, "True").toString()
            if value == "True":
                pluginlist.append(str(plugin))

        return pluginlist
class settings:
    def __init__(self):
        self.s = QSettings()

        # database settings
        self.conName = self.s.value("vmm/conname", "")
        self.dbhost = self.s.value("vmm/dbhost", "")
        self.dbport = self.s.value("vmm/dbport", "5432")
        self.database = self.s.value("vmm/database", "")
        self.dbuser = self.s.value("vmm/dbuser", "")
        self.dbpassw = self.s.value("vmm/dbpassw", r"")
        # layer
        self.schema = self.s.value("vmm/schema", r"")
        self.polyLayer = self.s.value("vmm/polyLayer", r"")
        self.polyLayerGeom = self.s.value("vmm/polyLayerGeom", r"")
        self.polyLayerName = self.s.value("vmm/polyLayerName", r"")

        self._pgSettings()

    def _getProxySettings(self):
        self.proxyEnabled = self.proxyHost = self.proxyPort = self.proxyUser = self.proxyPassword = None
        self.proxyUrl = ""
        proxyEnabled = self.s.value("proxy/proxyEnabled", "")
        if proxyEnabled == 1 or self.proxyEnabled == "true":
            self.proxyEnabled = True
            self.proxyHost = self.s.value("proxy/proxyHost", "")
            self.proxyPort = self.s.value("proxy/proxyPort", "")
            self.proxyUser = self.s.value("proxy/proxyUser", "")
            self.proxyPassword = self.s.value("proxy/proxyPassword", "")

            self.proxyUrl = "http://"
            if self.proxyUser and self.proxyPassword:
                self.proxyUrl += self.proxyUser + ":" + self.proxyPassword + "@"
            self.proxyUrl += self.proxyHost + ":" + self.proxyPort

    def _pgSettings(self):
        self.s.beginGroup("PostgreSQL/connections")
        cons = self.s.childGroups()
        self.connections = {}
        for con in cons:
            self.s.beginGroup(con)
            self.connections[con] = {n: self.s.value(n) for n in self.s.childKeys()}
            self.s.endGroup()
        self.s.endGroup()

    def saveSettings(self):
        # database settings
        self.s.setValue("vmm/conname", self.conName)
        self.s.setValue("vmm/dbhost", self.dbhost)
        self.s.setValue("vmm/database", self.database)
        self.s.setValue("vmm/dbport", self.dbport)
        self.s.setValue("vmm/dbuser", self.dbuser)
        self.s.setValue("vmm/dbpassw", self.dbpassw)

        # layer
        self.s.setValue("vmm/schema", self.schema)
        self.s.setValue("vmm/polyLayer", self.polyLayer)
        self.s.setValue("vmm/polyLayerGeom", self.polyLayerGeom)
        self.s.setValue("vmm/polyLayerName", self.polyLayerName)
Example #13
0
 def dbConnectionList(self):
     connection_names = []
     settings = QSettings()
     settings.beginGroup(u"/PostgreSQL/connections")
     for name in settings.childGroups():
         connection_names.append(name)
     settings.endGroup()
     return connection_names
Example #14
0
 def dbConnectionList(self):
     connection_names = []
     settings = QSettings()
     settings.beginGroup(u"/PostgreSQL/connections")
     for name in settings.childGroups():
         connection_names.append(name)
     settings.endGroup()
     return connection_names
 def getServers(self):
     """
     Gets server from QSettings
     """
     settings = QSettings()
     settings.beginGroup('PostgreSQL/servers')
     currentConnections = settings.childGroups()
     settings.endGroup()
     return currentConnections
Example #16
0
 def connections(self):
     # get the list of connections
     conn_list = []
     settings = QSettings()
     settings.beginGroup(self.connectionSettingsKey())
     for name in settings.childGroups():
         conn_list.append(createDbPlugin(self.typeName(), name))
     settings.endGroup()
     return conn_list
Example #17
0
 def connections(self):
     # get the list of connections
     conn_list = []
     settings = QSettings()
     settings.beginGroup(self.connectionSettingsKey())
     for name in settings.childGroups():
         conn_list.append(createDbPlugin(self.typeName(), name))
     settings.endGroup()
     return conn_list
Example #18
0
 def getServers(self):
     """
     Gets server from QSettings
     """
     settings = QSettings()
     settings.beginGroup('PostgreSQL/servers')
     currentConnections = settings.childGroups()
     settings.endGroup()
     return currentConnections
Example #19
0
def shortcutsFromSettings():
    shortcuts = []
    
    settings = QSettings()
    settings.beginGroup('/NextGIS/ShortcutManager/shortcuts')
    shortcuts_names = settings.childGroups()
    for shortcut_name in shortcuts_names:
        shortcuts.append(Shorcut(shortcut_name))
        
    return shortcuts
Example #20
0
 def getDbSettings(self):
     settings = QSettings()
     settings.beginGroup('PostgreSQL/connections')
     self.cmbServer.addItem('------------')
     self.cmbServer.addItems(settings.childGroups())
     settings.endGroup()
     selectedDb = settings.value(
         pystring('PostgreSQL/connections/selected'))
     cmbIndex = self.cmbServer.findText(selectedDb, Qt.MatchExactly)
     self.cmbServer.setCurrentIndex(cmbIndex)
Example #21
0
def get_connections():
    
    # Get the list of current connections
    conn_list = []
    conn_list.append('')
    qgisSettings = QSettings()
    root = '/PostgreSQL/connections'
    qgisSettings.beginGroup(root)
    for name in qgisSettings.childGroups():
        conn_list.append(name)
        qgisSettings.endGroup()
    return conn_list
Example #22
0
 def readSettings(self):
     if os.name == 'nt':
         settings = QSettings()
     else:
         settings = QSettings(os.path.expanduser("~/.iosshy.ini"), QSettings.IniFormat)
     for name in settings.childGroups():
         tunnel = Tunnel(self)
         tunnel.name = name
         tunnel.readSettings(settings)
         self._tunnels.append(tunnel)
         self.tray.menu.insertAction(self.actionLastSep, tunnel.action)
         self.tray.menu.removeAction(self.actionNoTun)
Example #23
0
 def readSettings(self):
     if os.name == 'nt':
         settings = QSettings()
     else:
         settings = QSettings(os.path.expanduser("~/.iosshy.ini"), QSettings.IniFormat)
     for name in settings.childGroups():
         tunnel = Tunnel(self)
         tunnel.name = name
         tunnel.readSettings(settings)
         self._tunnels.append(tunnel)
         self.tray.menu.insertAction(self.actionLastSep, tunnel.action)
         self.tray.menu.removeAction(self.actionNoTun)
Example #24
0
    def group_children(self):
        """
        :return: Names of the child groups in the path specified in the constructor.
        :rtype: list
        """
        settings = QSettings()
        settings.beginGroup(self.groupPath)

        group_children = settings.childGroups()

        settings.endGroup()

        return group_children
    def load(self):
        """Load repositories registered in settings."""
        self._repositories = {}
        settings = QSettings()
        settings.beginGroup(repo_settings_group())

        # Write Official Repository first to QSettings if needed
        official_repo_present = False
        for repo_name in settings.childGroups():
            url = settings.value(repo_name + '/url', '', type=unicode)
            if url == self.OFFICIAL_REPO[1]:
                official_repo_present = True
                break
        if not official_repo_present:
            settings.setValue(
                self.OFFICIAL_REPO[0] + '/url', self.OFFICIAL_REPO[1])

        for repo_name in settings.childGroups():
            self._repositories[repo_name] = {}
            self._repositories[repo_name]['url'] = settings.value(
                repo_name + '/url', '', type=unicode)
        settings.endGroup()
Example #26
0
    def group_children(self):
        """
        :return: Names of the child groups in the path specified in the constructor.
        :rtype: list
        """
        settings = QSettings()
        settings.beginGroup(self.groupPath)

        group_children = settings.childGroups()

        settings.endGroup()

        return group_children
Example #27
0
 def _loadFilterSets(self):
     self._filterSets = {}
     settings = QSettings()
     settings.beginGroup('ARK.filterset')
     groups = settings.childGroups()
     settings.endGroup()
     for group in groups:
         filterSet = FilterSet.fromSettings(self._plugin, 'ARK.filterset', group)
         self._filterSets[filterSet.key] = filterSet
     if 'Default' not in self._filterSets:
         filterSet = FilterSet.fromName(self._plugin, 'ARK.filterset', 'Default', 'Default')
         self._filterSets[filterSet.key] = filterSet
     self._dock.initFilterSets(self._filterSets, self._arkFilterSets)
Example #28
0
def setRepositoryAuth(authConfigId):
    """Add auth to the repository
    """
    settings = QSettings('Boundless', 'BoundlessConnect')
    repoUrl = settings.value('repoUrl', '', unicode)

    settings = QSettings()
    settings.beginGroup(reposGroup)
    for repo in settings.childGroups():
        url = settings.value(repo + '/url', '', unicode)
        if url == repoUrl:
            settings.setValue(repo + '/authcfg', authConfigId)
    settings.endGroup()
 def update_database_list(self):
     """update list of defined postgres connections"""
     settings = QSettings()
     settings.beginGroup("/PostgreSQL/connections")
     keys = settings.childGroups()
     self.combo_database.clear()
     self.combo_schema.clear()
     self.menumodel.clear()
     self.combo_database.addItems(keys)
     self.combo_database.setCurrentIndex(-1)
     settings.endGroup()
     # clear profile list
     self.combo_profile.clear()
     self.combo_profile.setCurrentIndex(-1)
Example #30
0
def getTagGroupsConfiguration(filename):
    """
    Configure in an external file the attributes that belong together to a
    taggroup. This configuration file is also used to decide which attributes
    are considered. The file needs to be in the ini format in the following
    form:
    [taggroup1]
    attribute1_name=key1
    attribute2_name=key2

    [taggroup2]
    attribute3_name=key1
    attribute4_name=key2

    etc.

    The ini format has been chosen because Qt has the in-built class QSettings
    to read that format (unlike e.g. YAML)

    """
    settings = QSettings("%s/%s" % (os.path.dirname(__file__), filename), QSettings.IniFormat)

    # Two-dimensional array with taggroups
    groups = []
    # Dictionary that maps the attribute names to LO keys
    transformMap = {}
    # The attribute column in the GIS layer that holds the Uuid
    identifierColumn = None

    # Loop all groups
    for i in settings.childGroups():
        settings.beginGroup(i)
        keys = []
        # Check if the current group defines a taggroup
        if QString(i).contains(QRegExp('taggroup[0-9+]')):
            for j in settings.allKeys():
                keys.append(str(j))
                transformMap[str(j)] = str(settings.value(j).toString())
            # Append the keys to the taggroup
            groups.append(keys)
        # Check if the current group defines general settings
        elif QString(i).contains(QRegExp('settings')):
            for j in settings.allKeys():
                if settings.value(j) == QString('activity_identifier'):
                    identifierColumn = str(j)

        # End this group
        settings.endGroup()

    return identifierColumn, transformMap, groups
    def testBoundlessRepoAdded(self):
        """Test that Boundless repository added to QGIS"""
        settings = QSettings('Boundless', 'BoundlessConnect')
        repoUrl = settings.value('repoUrl', '', unicode)

        settings = QSettings()
        settings.beginGroup(reposGroup)
        self.assertTrue(boundlessRepoName in settings.childGroups())
        settings.endGroup()

        settings.beginGroup(reposGroup + '/' + boundlessRepoName)
        url = settings.value('url', '', unicode)
        self.assertEqual(url, repoUrl)
        settings.endGroup()
Example #32
0
 def update_database_list(self):
     """update list of defined postgres connections"""
     settings = QSettings()
     settings.beginGroup("/PostgreSQL/connections")
     keys = settings.childGroups()
     self.combo_database.clear()
     self.combo_schema.clear()
     self.menumodel.clear()
     self.combo_database.addItems(keys)
     self.combo_database.setCurrentIndex(-1)
     settings.endGroup()
     # clear profile list
     self.combo_profile.clear()
     self.combo_profile.setCurrentIndex(-1)
    def set_database_connection(self):
        ''' Ser database connection '''

        # Initialize variables
        self.dao = None
        self.last_error = None
        self.connection_name = self.settings.value('db/connection_name',
                                                   self.plugin_name)
        self.schema_name = self.plugin_settings_value('schema_name')
        self.log_codes = {}

        # Look for connection data in QGIS configuration (if exists)
        connection_settings = QSettings()
        root_conn = "/PostgreSQL/connections/"
        connection_settings.beginGroup(root_conn)
        groups = connection_settings.childGroups()
        if self.connection_name in groups:

            root = self.connection_name + "/"
            host = connection_settings.value(root + "host", '')
            port = connection_settings.value(root + "port", '')
            db = connection_settings.value(root + "database", '')
            self.user = connection_settings.value(root + "username", '')
            pwd = connection_settings.value(root + "password", '')

            # We need to create this connections for Table Views
            self.db = QSqlDatabase.addDatabase("QPSQL")
            self.db.setHostName(host)
            self.db.setPort(int(port))
            self.db.setDatabaseName(db)
            self.db.setUserName(self.user)
            self.db.setPassword(pwd)
            status = self.db.open()

            # Connect to Database
            self.dao = PgDao()
            self.dao.set_params(host, port, db, self.user, pwd)
            status = self.dao.init_db()
            if not status:
                msg = "Database connection error. Please check connection parameters"
                self.last_error = self.tr(msg)
                return False
        else:
            msg = "Database connection name not found. Please check configuration file 'giswater.config'"
            self.last_error = self.tr(msg)
            return False

        return status
Example #34
0
 def getCustomSizesDict(self):
     #get custom sizes from qsettings
     settings = QSettings()
     settings.beginGroup('DSGTools/CustomSizes/')
     currentSettings = settings.childGroups()
     settings.endGroup()
     customSizesDict = dict()
     #get each parameter
     for settingName in currentSettings:
         customSizesDict[settingName] = dict()
         settings = QSettings()
         settings.beginGroup('DSGTools/CustomSizes/' + settingName)
         customSizesDict[settingName]['shape'] = settings.value('shape')
         customSizesDict[settingName]['value'] = settings.value('value')
         settings.endGroup()
     return customSizesDict
Example #35
0
 def getCustomSizesDict(self):
     #get custom sizes from qsettings
     settings = QSettings()
     settings.beginGroup('DSGTools/CustomSizes/')
     currentSettings = settings.childGroups()
     settings.endGroup()
     customSizesDict = dict()
     #get each parameter
     for settingName in currentSettings:
         customSizesDict[settingName] = dict()
         settings = QSettings()
         settings.beginGroup('DSGTools/CustomSizes/'+settingName)
         customSizesDict[settingName]['shape'] = settings.value('shape')
         customSizesDict[settingName]['value'] = settings.value('value')
         settings.endGroup()
     return customSizesDict
Example #36
0
 def read(self, items):
     '''
     Get the value of the user defined items from the STDM registry tree
     '''
     userKeys = {}
     settings = QSettings()
     settings.beginGroup("/")
     groups = settings.childGroups()
     for group in groups:
         if str(group) == self.groupPath:
             for t in items:
                 tKey = self.groupPath + "/" + t
                 if settings.contains(tKey):
                     tValue = settings.value(tKey)
                     userKeys[t] = tValue
             break
     return userKeys
Example #37
0
    def __init__(self, uri, iface):
        """
        Constructor
        :param uri: database uri
        :param iface: interface
        """
        self.__dbName = uri.database()
        self.__host = uri.host()
        self.__username = uri.username()
        self.__pwd = uri.password()
        self.__port = uri.port()
        self.__iface = iface

        s = QSettings()
        s.beginGroup("PostgreSQL/connections")
        connections = s.childGroups()
        s.endGroup()
        for connection in connections:
            s.beginGroup("PostgreSQL/connections/" + connection)
            if s.value("database", "") == self.__dbName:
                if self.__host is None:
                    if s.value("host", "") != "":
                        self.__host = s.value("host", "")
                if self.__username is None:
                    if s.value("username", "") != "":
                        self.__username = s.value("username", "")
                if self.__pwd is None:
                    if s.value("password", "") != "":
                        self.__pwd = s.value("password", "")
                if self.__port is None:
                    if s.value("port", "") != "":
                        self.__port = s.value("port", "")
                s.endGroup()
                break
            s.endGroup()

        if self.__username == "" or self.__pwd == "":
            (success, user,
             passwd) = QgsCredentials.instance().get(uri.connectionInfo(),
                                                     self.__username,
                                                     self.__pwd)
            if success:
                QgsCredentials.instance().put(uri.connectionInfo(), user,
                                              passwd)
                self.__username = user
                self.__pwd = passwd
Example #38
0
 def read(self,items):
     '''
     Get the value of the user defined items from the STDM registry tree
     '''
     userKeys = {}
     settings = QSettings()        
     settings.beginGroup("/")
     groups = settings.childGroups()
     for group in groups:
         if str(group) == self.groupPath:
             for t in items:
                 tKey = self.groupPath + "/" + t
                 if settings.contains(tKey):                        
                     tValue = settings.value(tKey)
                     userKeys[t] = tValue
             break        
     return userKeys
 def set_database_connection(self):
     
     # Initialize variables
     self.dao = None 
     self.last_error = None      
     self.connection_name = self.settings.value('db/connection_name', self.plugin_name)
     self.schema_name = self.settings.value('db/schema_name')
     self.log_codes = {}
     
     # Look for connection data in QGIS configuration (if exists)    
     qgis_settings = QSettings()     
     root_conn = "/PostgreSQL/connections/"          
     qgis_settings.beginGroup(root_conn);           
     groups = qgis_settings.childGroups();                                
     if self.connection_name in groups:      
         root = self.connection_name+"/"  
         host = qgis_settings.value(root+"host", '')
         port = qgis_settings.value(root+"port", '')            
         db = qgis_settings.value(root+"database", '')
         user = qgis_settings.value(root+"username", '')
         pwd = qgis_settings.value(root+"password", '') 
         # We need to create this connections for Table Views
         self.db = QSqlDatabase.addDatabase("QPSQL")
         self.db.setHostName(host)
         self.db.setPort(int(port))
         self.db.setDatabaseName(db)
         self.db.setUserName(user)
         self.db.setPassword(pwd)
         self.status = self.db.open()    
         if not self.status:
             msg = "Database connection error"
             self.last_error = self.tr(msg)           
     else:
         msg = "Database connection name '"+self.connection_name+"' not set in QGIS. Please define it or check parameter 'configuration_name' in file 'giswater.config'"
         self.last_error = self.tr(msg)
         return False
 
     # Connect to Database 
     self.dao = PgDao()     
     self.dao.set_params(host, port, db, user, pwd)
     status = self.dao.init_db()
     
     # TODO: Get postgresql data folder
    
     return status    
Example #40
0
 def __init__(self, iface, parent=None):
     '''
     Constructor
     '''
     super(LayerDialog, self).__init__(parent)
     self.setupUi(self)
     self.bar = QgsMessageBar(self)
     self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
     self.layout().addWidget(self.bar)
     self.iface = iface
     settings = QSettings()
     settings.beginGroup('/SpatiaLite/connections')
     self.mDatabaseComboBox.clear()
     for k in settings.childGroups():
         text = settings.value(k + '/sqlitepath', '###unknown###')
         self.mDatabaseComboBox.addItem(text)
     settings.endGroup()
     self.mOkButton = self.buttonBox.button(QDialogButtonBox.Ok)
Example #41
0
    def read(self,items):
        """
        Get the value of the user defined items from the STDM registry tree
        """
        userKeys = {}
        settings = QSettings()        
        settings.beginGroup("/")
        groups = settings.childGroups()
        for group in groups:
            #QMessageBox.information(None, "Info", group)
            if str(group) == self._base_group():
                for t in items:
                    tKey = self.groupPath + "/" + t
                    if settings.contains(tKey):                        
                        tValue = settings.value(tKey)
                        userKeys[t] = tValue
                break

        return userKeys
 def loadParameterSet(self):
     s = QSettings()
     name = self.configCombo.currentText()
     s.beginGroup('ParameterSets')
     if not name in s.childGroups():
         dlg = QErrorMessage(self)
         dlg.setWindowTitle('Error')
         dlg.showMessage('No saved parameters available for %s' % name)
         return
     s.beginGroup(name)
     for item in self.parameterItems:
         item.setValue(s.value(item.objectName(), item.value(), type=float))
     self.adjustExcitationCb.setChecked(s.value('adjustExcitation', False, type=bool))
     self.sensorNameLe.setText(s.value('sensorName', '', type=QString))
     self.visaCombo.setCurrentIndex(self.visaCombo.findText(s.value('sr830Visa', 'GPIB0::12', type=QString)))
     self.autoRangingCb.setChecked(s.value('autoRanging', True, type=bool))
     self.minSensitivityCombo.setCurrentCodeSilently(s.value('minSensitivity', 0, type=int))
     s.endGroup()
     s.endGroup()
Example #43
0
 def on_removeMissingPushButton_clicked(self):
     servers = self.serverWidget.getServers()
     settings = QSettings()
     settings.beginGroup('PostgreSQL/connections')
     candidates = settings.childGroups()
     settings.endGroup()
     removedConn = []
     dbList = self.serverWidget.abstractDb.getDbsFromServer()
     for candidate in candidates:
         candidateSettings = QSettings()
         candidateSettings.beginGroup('PostgreSQL/connections/'+candidate)
         candidateDb = candidateSettings.value('database')
         if candidateDb not in dbList:
             self.removeConnections(candidate, removedConn)
     if len(removedConn)>0:
         msg = self.tr('\nThe following databases connections were removed successfully:\n')+', '.join(removedConn)
     else:
         msg = self.tr('No connections were removed.')
     QMessageBox.warning(self, self.tr("Warning!"), msg)
Example #44
0
 def setConnection(dbName, iface):
     """
     To set a connection to a PstgreSQL database
     :param dbName: the name of the database
     :param iface: the qgs interface
     :return: a QsqlDatabase object, or none
     """
     s = QSettings()
     s.beginGroup("PostgreSQL/connections")
     connections = s.childGroups()
     s.endGroup()
     for connection in connections:
         s.beginGroup("PostgreSQL/connections/" + connection)
         if s.value("database", "") == dbName:
             db = QSqlDatabase.addDatabase('QPSQL')
             db.setHostName(s.value("host", ""))
             db.setDatabaseName(s.value("database", ""))
             username = s.value("username", "")
             db.setUserName(username)
             password = s.value("password", "")
             db.setPassword(password)
             s.endGroup()
             if username == "" or password == "":
                 iface.messageBar().pushMessage(
                     QCoreApplication.translate(
                         "VDLTools", "Need user and password for db"),
                     level=QgsMessageBar.CRITICAL)
                 return None
             ok = db.open()
             if not ok:
                 iface.messageBar().pushMessage(
                     QCoreApplication.translate("VDLTools",
                                                "Database Error: ") +
                     db.lastError().text(),
                     level=QgsMessageBar.CRITICAL)
                 return None
             return db
         s.endGroup()
     iface.messageBar().pushMessage(QCoreApplication.translate(
         "VDLTools", "No connection for this db"),
                                    level=QgsMessageBar.CRITICAL)
     return None
Example #45
0
    def read(self, items):
        """
        Get the value of the user defined items from the STDM registry tree
        param items: List of registry keys to fetch.
        type items: list
        """
        userKeys = {}
        settings = QSettings()        
        settings.beginGroup("/")
        groups = settings.childGroups()
        for group in groups:
            if str(group) == self._base_group():
                for t in items:
                    tKey = self.groupPath + "/" + t
                    if settings.contains(tKey):                        
                        tValue = settings.value(tKey)
                        userKeys[t] = tValue
                break

        return userKeys
Example #46
0
    def read(self, items):
        """
        Get the value of the user defined items from the STDM registry tree
        param items: List of registry keys to fetch.
        type items: list
        """
        userKeys = {}
        settings = QSettings()
        settings.beginGroup("/")
        groups = settings.childGroups()
        for group in groups:
            if str(group) == self._base_group():
                for t in items:
                    tKey = self.groupPath + "/" + t
                    if settings.contains(tKey):
                        tValue = settings.value(tKey)
                        userKeys[t] = tValue
                break

        return userKeys
Example #47
0
 def getQGISDbs(self):
     """Return all PostGIS connection settings stored in QGIS
     :return: connection dict() with name and other settings
     """
     con_settings = []
     settings = QSettings()
     settings.beginGroup('/PostgreSQL/connections')
     for item in settings.childGroups():
         con = dict()
         con['name'] = unicode(item)
         con['host'] = unicode(settings.value(u'%s/host' % unicode(item)))
         con['port'] = unicode(settings.value(u'%s/port' % unicode(item)))
         con['database'] = unicode(settings.value(u'%s/database' % unicode(item)))
         con['username'] = unicode(settings.value(u'%s/username' % unicode(item)))
         con['password'] = unicode(settings.value(u'%s/password' % unicode(item)))
         con_settings.append(con)
     settings.endGroup()
     dbs = {}
     if len(con_settings) > 0:
         for conn in con_settings:
             dbs[conn['name']]= conn
     return dbs
Example #48
0
    def setDbConnName(self, inLayer):
        # get db details by
        # searching qgis db connections for a match to the layer
        if not inLayer.providerType() == u"postgres":
            raise GeoAlgorithmExecutionException("Layer %s is not stored in a PostGIS database" % inLayer.name())

        # get the name of the db from the uri
        uri = QgsDataSourceURI(inLayer.source())
        dbName = uri.database()

        settings = QSettings()
        settings.beginGroup("/PostgreSQL/connections/")
        try:
            for conn in settings.childGroups():
                if settings.value(conn + "/database") == dbName:
                    self.dbConnName = conn
        except Exception, e:
            raise GeoAlgorithmExecutionException(
                "Unspecified error reading \
                    database connections: %s"
                % e
            )
Example #49
0
    def selectOutput(self):
        if isinstance(self.output, OutputDirectory):
            self.selectDirectory()
        else:
            popupMenu = QMenu()

            actionSaveToTempFile = QAction(self.tr('Save to a temporary file'),
                                           self.btnSelect)
            actionSaveToTempFile.triggered.connect(self.saveToTemporaryFile)
            popupMenu.addAction(actionSaveToTempFile)

            actionSaveToFile = QAction(self.tr('Save to file...'),
                                       self.btnSelect)
            actionSaveToFile.triggered.connect(self.selectFile)
            popupMenu.addAction(actionSaveToFile)

            if isinstance(self.output, OutputVector) \
                    and self.alg.provider.supportsNonFileBasedOutput():
                actionSaveToMemory = QAction(self.tr('Save to memory layer'),
                                             self.btnSelect)
                actionSaveToMemory.triggered.connect(self.saveToMemory)
                popupMenu.addAction(actionSaveToMemory)
                actionSaveToSpatialite = QAction(
                    self.tr('Save to Spatialite table...'), self.btnSelect)
                actionSaveToSpatialite.triggered.connect(self.saveToSpatialite)
                popupMenu.addAction(actionSaveToSpatialite)
                actionSaveToPostGIS = QAction(
                    self.tr('Save to PostGIS table...'), self.btnSelect)
                actionSaveToPostGIS.triggered.connect(self.saveToPostGIS)
                settings = QSettings()
                settings.beginGroup('/PostgreSQL/connections/')
                names = settings.childGroups()
                settings.endGroup()
                actionSaveToPostGIS.setEnabled(bool(names))
                popupMenu.addAction(actionSaveToPostGIS)

            popupMenu.exec_(QCursor.pos())
Example #50
0
class ManageConnectionsDialog(QDialog, BASE_CLASS):

    """manage connections"""

    def __init__(self, mode):
        """init dialog"""

        QDialog.__init__(self)
        self.setupUi(self)
        self.settings = QSettings()
        self.filename = None
        self.mode = mode  # 0 - save, 1 - load
        self.btnBrowse.clicked.connect(self.select_file)
        self.manage_gui()

    def manage_gui(self):
        """manage interface"""

        if self.mode == 1:
            self.label.setText(self.tr('Load from file'))
            self.buttonBox.button(QDialogButtonBox.Ok).setText(self.tr('Load'))
        else:
            self.label.setText(self.tr('Save to file'))
            self.buttonBox.button(QDialogButtonBox.Ok).setText(self.tr('Save'))
            self.populate()

        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)

    def select_file(self):
        """select file ops"""

        label = self.tr('eXtensible Markup Language (*.xml *.XML)')

        if self.mode == 0:
            slabel = self.tr('Save connections')
            self.filename = QFileDialog.getSaveFileName(self, slabel,
                                                        '.', label)
        else:
            slabel = self.tr('Load connections')
            self.filename = QFileDialog.getOpenFileName(self, slabel,
                                                        '.', label)

        if not self.filename:
            return

        # ensure the user never omitted the extension from the file name
        if not self.filename.lower().endswith('.xml'):
            self.filename = '%s.xml' % self.filename

        self.leFileName.setText(self.filename)

        if self.mode == 1:
            self.populate()

        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True)

    def populate(self):
        """populate connections list from settings"""

        if self.mode == 0:
            self.settings.beginGroup('/MetaSearch/')
            keys = self.settings.childGroups()
            for key in keys:
                item = QListWidgetItem(self.listConnections)
                item.setText(key)
            self.settings.endGroup()

        else:  # populate connections list from file
            doc = get_connections_from_file(self, self.filename)
            if doc is None:
                self.filename = None
                self.leFileName.clear()
                self.listConnections.clear()
                return

            for csw in doc.findall('csw'):
                item = QListWidgetItem(self.listConnections)
                item.setText(csw.attrib.get('name'))

    def save(self, connections):
        """save connections ops"""

        doc = etree.Element('qgsCSWConnections')
        doc.attrib['version'] = '1.0'

        for conn in connections:
            url = self.settings.value('/MetaSearch/%s/url' % conn)
            if url is not None:
                connection = etree.SubElement(doc, 'csw')
                connection.attrib['name'] = conn
                connection.attrib['url'] = url

        # write to disk
        with open(self.filename, 'w') as fileobj:
            fileobj.write(prettify_xml(etree.tostring(doc)))
        QMessageBox.information(self, self.tr('Save Connections'),
                                self.tr('Saved to %s') % self.filename)
        self.reject()

    def load(self, items):
        """load connections"""

        self.settings.beginGroup('/MetaSearch/')
        keys = self.settings.childGroups()
        self.settings.endGroup()

        exml = etree.parse(self.filename).getroot()

        for csw in exml.findall('csw'):
            conn_name = csw.attrib.get('name')

            # process only selected connections
            if conn_name not in items:
                continue

            # check for duplicates
            if conn_name in keys:
                label = self.tr('File %s exists. Overwrite?') % conn_name
                res = QMessageBox.warning(self, self.tr('Loading Connections'),
                                          label,
                                          QMessageBox.Yes | QMessageBox.No)
                if res != QMessageBox.Yes:
                    continue

            # no dups detected or overwrite is allowed
            url = '/MetaSearch/%s/url' % conn_name
            self.settings.setValue(url, csw.attrib.get('url'))

    def accept(self):
        """accept connections"""

        selection = self.listConnections.selectedItems()
        if len(selection) == 0:
            return

        items = []
        for sel in selection:
            items.append(sel.text())

        if self.mode == 0:  # save
            self.save(items)
        else:  # load
            self.load(items)

        self.filename = None
        self.leFileName.clear()
        self.listConnections.clear()
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)

    def reject(self):
        """back out of manage connections dialogue"""

        QDialog.reject(self)
Example #51
0
from PyQt4.QtCore import QSettings, QStringList
from PyQt4.QtSql import *

config = QSettings("__settings.ini", QSettings.IniFormat)
groups = [str(k) for k in QStringList(config.childGroups())]


def setGroup(text):
    config.beginGroup(text)


def getProperties():
    #fields = [{str(field),config.value(field).toString()} for field in QStringList(config.allKeys())]
    fields = {}
    for name in config.allKeys():
        value = str(config.value(name).toString())
        fields[str(name)] = value

    config.endGroup()
    return fields
Example #52
0
 def loadParameterSets(self):
     s = QSettings()
     s.beginGroup('ParameterSets')
     names = s.childGroups()
     self.configCombo.addItems(names)
 def getConnections(self):
     s = QSettings()
     s.beginGroup("PostgreSQL/connections")
     currentConnections = s.childGroups()
     s.endGroup()
     return currentConnections
Example #54
0
class Setting():

    def __init__(self, notebooks):

        # Index directory of whoosh, located in notebookPath.
        self.schema = fields.Schema(
            path = fields.TEXT(stored=True),
            title = fields.TEXT(stored=True),
            content = fields.TEXT(stored=True),
            tags = fields.KEYWORD(commas=True))

        self.notebookName = notebooks[0][0]
        self.notebookPath = notebooks[0][1]
        self.notePath = os.path.join(self.notebookPath, "notes").replace(os.sep, '/')
        self.htmlPath = os.path.join(self.notebookPath, "html", "notes").replace(os.sep, '/')
        self.indexdir = os.path.join(self.notePath, ".indexdir").replace(os.sep, '/')
        self.attachmentPath = os.path.join(self.notebookPath, "attachments").replace(os.sep, '/')
        self.configfile = os.path.join(self.notebookPath, "notebook.conf").replace(os.sep, '/')
        cssPath = os.path.join(self.notebookPath, "css").replace(os.sep, '/')
        self.cssfile = os.path.join(cssPath, "notebook.css").replace(os.sep, '/')
        self.searchcssfile = os.path.join(cssPath, "search-window.css").replace(os.sep, '/')
        self.qsettings = QSettings(self.configfile, QSettings.IniFormat)

        if os.path.exists(self.configfile):
            self.extensions = readListFromSettings(self.qsettings,
                                                   "extensions")
            self.fileExt = self.qsettings.value("fileExt")
            self.attachmentImage = self.qsettings.value("attachmentImage")
            self.attachmentDocument = self.qsettings.value("attachmentDocument")
            self.version = self.qsettings.value("version")
            self.geometry = self.qsettings.value("geometry")
            self.windowstate = self.qsettings.value("windowstate")
            self.mathjax = self.qsettings.value('mathJax')
            if 'extensionsConfig' not in set(self.qsettings.childGroups()):
                self.extcfg = self.qsettings.value('extensionsConfig',  defaultValue={})
                writeDictToSettings(self.qsettings, 'extensionsConfig', self.extcfg)
            else:
                self.extcfg = readDictFromSettings(self.qsettings, 'extensionsConfig')
        else:
            self.extensions = []
            self.fileExt = ""
            self.attachmentImage = []
            self.attachmentDocument = []
            self.version = None
            self.geometry = None
            self.windowstate = None
            self.mathjax = ''
            self.extcfg = {}

        self.faulty_exts=[]

        # Default enabled python-markdown extensions.
        # http://pythonhosted.org/Markdown/extensions/index.html
        if not self.extensions:
            self.extensions = [
                   'nl2br'           # newline to break
                 , 'strkundr'        # bold-italics-underline-delete style
                 , 'codehilite'      # code syntax highlight
                 , 'fenced_code'     # code block
                 , 'headerid'        # add id to headers
                 , 'headerlink'      # add anchor to headers
                 , 'footnotes'
                 , 'asciimathml'
                 ]
            writeListToSettings(self.qsettings, "extensions", self.extensions)

        while True:
             print(self.extensions)
             try:
                 markdown.markdown("",extensions=self.extensions)
             except AttributeError as e:
                 remove_this = NOT_EXT.findall(e.args[0])[0]
                 if remove_this in self.extensions:
                     print("Found invalid markdown extension",remove_this,". Please consider removing it.")
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(remove_this)
                     self.faulty_exts.append(remove_this)
             except ImportError as e:
                 if e.name.startswith('mdx_') and e.name[4:] in self.extensions:
                     print('Found missing markdown extension', e.name[4:], ', temporarily disabling.')
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(e.name[4:])
                     self.faulty_exts.append(e.name[4:])
                 elif e.name in self.extensions:
                     print('Found missing markdown extension', e.name, ', temporarily disabling.')
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(e.name)
                     self.faulty_exts.append(e.name)
             else:
                 self.md = markdown.Markdown(self.extensions, extension_configs=self.extcfg)
                 break

        # Default file extension name
        if not self.fileExt:
            self.fileExt = ".md"
            self.qsettings.setValue("fileExt", self.fileExt)

        # Image file types that will be copied to attachmentDir
        # Inserted as image link
        if not self.attachmentImage:
            self.attachmentImage = [".jpg", ".jpeg", ".png", ".gif", ".svg"]
            self.qsettings.setValue("attachmentImage", self.attachmentImage)

        # Document file types that will be copied to attachmentDir
        # Inserted as link
        if not self.attachmentDocument:
            self.attachmentDocument = [".pdf", ".doc", ".odt"]
            self.qsettings.setValue("attachmentDocument", self.attachmentDocument)

        # Migrate notebookPath to v0.3.0 folder structure
        if not self.version:
            notebookDir = QDir(self.notebookPath)

            # move all markdown files to notes/
            dirList = notebookDir.entryList(QDir.Dirs | QDir.NoDotAndDotDot)
            if 'css' in dirList:
                dirList.remove('css')
            fileList = notebookDir.entryList(['*.md', '*.mkd', '*.markdown'])
            notebookDir.mkdir('notes')
            for d in dirList + fileList:
                notebookDir.rename(d, os.path.join('notes', d).replace(os.sep, '/'))

            # remove .indexdir folder
            oldIndexDir = QDir(os.path.join(self.notebookPath, '.indexdir'.replace(os.sep, '/')))
            indexFileList = oldIndexDir.entryList()
            for f in indexFileList:
                oldIndexDir.remove(f)
            notebookDir.rmdir('.indexdir')

            # rename notes.css to css/notebook.css
            oldCssFile = os.path.join(self.notebookPath, 'notes.css').replace(os.sep, '/')
            QDir().mkpath(cssPath)
            if os.path.exists(oldCssFile):
                QFile.rename(oldCssFile, self.cssfile)

            self.version = '0'

        if not self.mathjax:
            self.mathjax = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
            self.qsettings.setValue('mathJax', self.mathjax)

    def saveGeometry(self, geometry):
        self.qsettings.setValue("geometry", geometry)

    def saveWindowState(self, state):
        self.qsettings.setValue("windowstate", state)

    def recentViewedNotes(self):
        return readListFromSettings(self.qsettings, "recentViewedNoteList")

    def updateRecentViewedNotes(self, notesList):
        writeListToSettings(self.qsettings, "recentViewedNoteList", notesList)
Example #55
0
class TagWrapper():

    GROUP_FILES_NAME = "files"
    
    KEY_TAGS = "tags"
    KEY_TIMESTAMP = "timestamp"
    KEY_CATEGORIES = "category"
    KEY_HASHSUM = "hashsum"

    TAG_SEPARATOR = ","
    
    def __init__(self, file_path):#, store_id=None):
        """
        constructor
        """
        path_list = str(file_path).split("/")[:-2]
        logging_path = unicode("/").join(path_list)
        self.__log = LogHelper.get_store_logger(logging_path, logging.INFO)
        
        #if store_id is not None:
        #    self.__create_file_structure(file_path, store_id)
        self.__settings = QSettings(file_path, QSettings.IniFormat)
        self.__settings.setIniCodec(TsConstants.DEFAULT_ENCODING)
        if self.__settings.iniCodec() is None:
            self.__log.info("no such encoding: " + TsConstants.DEFAULT_ENCODING)
        self.__path = file_path
#    def __create_file_structure(self, file_path, store_id):
#        """
#        creates the default file structure in a given file
#        """
#        file = open(file_path, "w")
#        file.write("[store]\n")
#        file.write("store_id=%s\n" % store_id)
#        file.write("\n")
#        file.write("[files]\n")
#        file.write("\n")
#        file.write("[categories]\n")
#        file.close()
    
    @staticmethod
    def create_tags_file(file_path):
        """
        create a new tags file structure in the given file
        this  method has to be used in a static way
        the file must be opened with write permission
        """
        ## copy the config template to the new store config dir
        shutil.copyfile(TsConstants.STORE_TAGFILE_TEMPLATE_PATH, file_path)
          
    def __get_file_list(self):
        """
        returns a list of file dictionaries sorted by timestamp descending
        """
        file_list = []
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        for file in self.__settings.childGroups():
            self.__settings.beginGroup(file)            
            tags = unicode(self.__settings.value(self.KEY_TAGS, "").toString()).split(self.TAG_SEPARATOR)
            categories = unicode(self.__settings.value(self.KEY_CATEGORIES, "").toString()).split(self.TAG_SEPARATOR)
            timestamp = unicode(self.__settings.value(self.KEY_TIMESTAMP, "").toString())
            hashsum = unicode(self.__settings.value(self.KEY_HASHSUM, "").toString())            
            file_list.append(dict(filename=file, tags=tags, category=categories, timestamp=timestamp, hashsum=hashsum))
            self.__settings.endGroup()
        self.__settings.endGroup()
        return sorted(file_list, key=lambda k:k[self.KEY_TIMESTAMP], reverse=True)
        
    def get_files_with_tag(self, tag):
        """
        returns a list of file dictionaries including filename, tags, categories, timestamp
        """
        file_list = self.__get_file_list()
        filtered_list = []
        for file in file_list:
            if tag in file[self.KEY_TAGS] or tag in file[self.KEY_CATEGORIES]:
                filtered_list.append(file)
        return filtered_list
    
    def get_files_with_expiry_tags(self, prefix):
        """
        returns all file with a valid expiry timestamp
        """
        file_list = self.__get_file_list()
        filtered_list = []
        for file in file_list:
            file_added = False
            for tag in file[self.KEY_TAGS]:
                match = re.match("^("+prefix+")([0-9]{4})(-)([0-9]{2})", tag)
                if match: 
                    filtered_list.append(dict(filename=file["filename"], tags=file["tags"], category=file["category"], exp_year=match.groups()[1], exp_month=match.groups()[3]))
                    file_added = True
                    break
            
            if not file_added:
                for tag in file[self.KEY_CATEGORIES]:
                    match = re.match("^("+prefix+")([0-9]{4})(-)([0-9]{2})", tag)
                    if match:
                        filtered_list.append(dict(filename=file["filename"], tags=file["tags"], category=file["category"], exp_year=match.groups()[1], exp_month=match.groups()[3]))
                        break

        return filtered_list
        
    def __get_tag_dictionary(self, attribute=KEY_TAGS):
        """
        iterates through all files and creates a dictionary with tags + counter
        attribute is used to define if it returns describing tags or categorizing tags (categories)
        attribute can be KEY_TAGS|KEY_CATEGORIES
        """
        dictionary = dict()
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        for file in self.__settings.childGroups():
            self.__settings.beginGroup(file)
            tags = unicode(self.__settings.value(attribute, "").toString()).split(self.TAG_SEPARATOR)
            self.__settings.endGroup()
            for tag in tags:
                if tag in dictionary:
                    dictionary[unicode(tag)] += 1
                else:
                    dictionary[unicode(tag)] = 1
        self.__settings.endGroup()
        return dictionary

    def get_file_timestamp(self, file_name):
        """
        returns the timestamp value of a given file
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.beginGroup(file_name)
        timestamp = unicode(self.__settings.value(self.KEY_TIMESTAMP, "").toString())
        self.__settings.endGroup()        
        self.__settings.endGroup()
        return timestamp

    def get_file_hashsum(self, file_name):
        """
        returns the hashsum value of a given file
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.beginGroup(file_name)
        timestamp = unicode(self.__settings.value(self.KEY_TIMESTAMP, "").toString())
        self.__settings.endGroup()        
        self.__settings.endGroup()
        return timestamp

    def get_file_tags(self, file_name):
        """
        returns the describing tag list of a given file
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.beginGroup(file_name)
        tags = unicode(self.__settings.value(self.KEY_TAGS, "").toString()).split(self.TAG_SEPARATOR)
        self.__settings.endGroup()        
        self.__settings.endGroup()    
        return tags    
    
    def get_file_categories(self, file_name):
        """
        returns the categorizing tag list of a given file
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.beginGroup(file_name)
        categories = unicode(self.__settings.value(self.KEY_CATEGORIES, "").toString()).split(self.TAG_SEPARATOR)
        self.__settings.endGroup()        
        self.__settings.endGroup()    
        return categories
    
    def get_files(self):
        """
        returns a list of all files stored in the config file
        """
        
        files = []
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        for file in self.__settings.childGroups():
            files.append(unicode(file))
        self.__settings.endGroup()
        return files

    def set_file_path(self, file_path):
        """
        sets the internal path to the source file
        """
        self.__settings = QSettings(file_path, QSettings.IniFormat)

#    def set_store_id(self, id):
#        """
#        writes the stores id to the configuration file
#        """
#        self.__settings.beginGroup(self.GROUP_STORE_NAME)
#        self.__settings.setValue("store_id", id)
#        self.__settings.endGroup()
    
#    def get_store_id(self):
#        """
#        returns the store id of the current file to identify the store during rename
#        """
#        self.__settings.beginGroup(self.GROUP_STORE_NAME)
#        id = unicode(self.__settings.value(self.KEY_STORE_ID, "").toString())
#        self.__settings.endGroup()
#        return id
    
    def get_all_tags(self):
        """
        returns a list of all tags of the current store sorted by name asc
        """
        dictionary = self.__get_tag_dictionary()
        return sorted(dictionary.keys())
        
    def get_all_categorizing_tags(self):
        """
        returns a list of all categorizing tags of the current store sorted by name asc
        """
        dictionary = self.__get_tag_dictionary(self.KEY_CATEGORIES)
        return sorted(dictionary.keys())

    def get_recent_tags(self, no_of_tags):
        """
        returns a defined number of recently entered tags
        """
        tags = set()
        files = self.__get_file_list()
        position = 0
        while len(tags) < no_of_tags and position < len(files) and files[position] is not None:
            tags = tags.union(set(files[position]["tags"]))
            position +=1
        return sorted(tags)[:no_of_tags]

    def get_recent_files_tags(self, no_of_files):
        """
        returns all tags of a number of recently entered files
        """
        tags = set()
        files = self.__get_file_list()
        position = 0
        while position < no_of_files and position < len(files) and files[position] is not None:
            tags = tags.union(set(files[position]["tags"]))
            position +=1
        return sorted(tags)

    def get_popular_tags(self, no_of_tags):
        """
        returns a defined number of the most popular tags
        """
        dictionary = self.__get_tag_dictionary()
        list = sorted(dictionary.iteritems(), key=lambda (k,v): (v,k), reverse=True)
        return_list = []
        for item in list[:no_of_tags]:
            return_list.append(item[0])
        return return_list

    def get_popular_categories(self, no_of_tags):
        """
        returns a defined number of the most popular categories
        """
        dictionary = self.__get_tag_dictionary(self.KEY_CATEGORIES)
        list = sorted(dictionary.iteritems(), key=lambda (k,v): (v,k), reverse=True)
        return_list = []
        for item in list[:no_of_tags]:
            return_list.append(item[0])
        return return_list

    def get_recent_categories(self, no_of_tags):
        """
        returns a defined number of recently entered tags
        """
        tags = set()
        files = self.__get_file_list()
        position = 0
        while len(tags) < no_of_tags and position < len(files) and files[position] is not None:
            tags = tags.union(set(files[position]["category"]))
            position +=1
        return sorted(tags)[:no_of_tags]

    def set_file(self, file_name, tag_list, category_list=None, timestamp=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")):
        """
        adds a file and its tags to the config file
        or overrides the tags of an existing file
        assumption: file_name and tag_list do not have to be checked
        for lower case upper case inconsistencies 
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.beginGroup(unicode(file_name))
        self.__settings.setValue(self.KEY_TAGS, unicode(self.TAG_SEPARATOR.join(tag_list)))
        self.__settings.setValue(self.KEY_TIMESTAMP, timestamp)
        if category_list is not None:
            self.__settings.setValue(self.KEY_CATEGORIES, unicode(self.TAG_SEPARATOR.join(category_list)))
        self.__settings.endGroup()
        self.__settings.endGroup()
        self.__settings.sync()

    def __set_tags(self, file_name, tag_list, category_list=None):
        """
        resets the files tag list without updating its timestamp
        this method is called during renaming/deleting tags to avoid changing the recent tags
        """
        if not self.file_exists(file_name):
            self.set_file(file_name, tag_list, category_list)
        else:
            self.__settings.beginGroup(self.GROUP_FILES_NAME)
            self.__settings.beginGroup(file_name)
            self.__settings.setValue(self.KEY_TAGS, self.TAG_SEPARATOR.join(tag_list))
            if category_list is not None:
                self.__settings.setValue(self.KEY_CATEGORIES, self.TAG_SEPARATOR.join(category_list))
            self.__settings.endGroup()
            self.__settings.endGroup()

#    def rename_file(self, old_file_name, new_file_name):
#        """
#        renames an existing file
#        """
#        self.__settings.beginGroup(self .GROUP_FILES_NAME)
#        self.__settings.beginGroup(old_file_name)            
#        tags = unicode(self.__settings.value(self.KEY_TAGS, "").toString()).split(self.TAG_SEPARATOR)
#        timestamp = unicode(self.__settings.value(self.KEY_TIMESTAMP, "").toString())
#        self.__settings.endGroup()
#        self.__settings.remove(old_file_name)
#        self.__settings.endGroup()
#        self.set_file(new_file_name, tags, timestamp)
        
    def remove_file(self, file_name):
        """
        removes a given file from the config file
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        self.__settings.remove(file_name)
        self.__settings.endGroup()
        self.__settings.sync()
    
    def file_exists(self, file_name):
        """ 
        checks if a file is already existing in the section "files"
        """
        self.__settings.beginGroup(self.GROUP_FILES_NAME)
        files = self.__settings.childGroups()
        self.__settings.endGroup()
        for name in files:
            name = unicode(name)
            if name == file_name:
                return True
        return False
    
#    def rename_tag(self, old_tag_name, new_tag_name):
#        """
#        renames all tags in the store
#        """
#        files = self.__get_file_list()
#        for file in files:
#            tags = file["tags"]
#            if old_tag_name in tags:
#                tags.remove(old_tag_name)
#                tags.append(new_tag_name)
#                self.__set_tags(file["filename"], tags)

    def sync_settings(self):
        """
        flushes all pending changes to disk and reloads the file in case it has been changed by another application, i.e. sync app
        """
        self.__settings.sync()

    def remove_tag(self, tag_name):
        """
        removes the given tag from all file entries
        """
        files = self.__get_file_list()
        for file in files:
            describing_tags = file["tags"]
            categorising_tags = file["category"]
            if tag_name in describing_tags or tag_name in categorising_tags:
                if tag_name in describing_tags:
                    describing_tags.remove(tag_name)
                if tag_name in categorising_tags:
                    categorising_tags.remove(tag_name)
                if len(describing_tags) >= 1 or len(categorising_tags) >= 1:
                    self.__set_tags(file["filename"], describing_tags, categorising_tags)
                else:
                    #delete file entry if there is no tag left
                    self.remove_file(file["filename"])

    def get_tag_dict(self, attribute=KEY_TAGS):
        """
        ADD from Georg
        TODO: Change function-name
        """
        return self.__get_tag_dictionary(attribute)
## end
Example #56
0
 def dbConnectionNames(self):
     settings = QSettings()
     settings.beginGroup('/PostgreSQL/connections/')
     return settings.childGroups()
Example #57
0
class MetaSearchDialog(QDialog, BASE_CLASS):
    """main dialogue"""
    def __init__(self, iface):
        """init window"""

        QDialog.__init__(self)
        self.setupUi(self)

        self.iface = iface
        self.map = iface.mapCanvas()
        self.settings = QSettings()
        self.catalog = None
        self.catalog_url = None
        self.context = StaticContext()

        version = self.context.metadata.get('general', 'version')
        self.setWindowTitle('MetaSearch %s' % version)

        self.rubber_band = QgsRubberBand(self.map, True)  # True = a polygon
        self.rubber_band.setColor(QColor(255, 0, 0, 75))
        self.rubber_band.setWidth(5)

        # form inputs
        self.startfrom = 0
        self.maxrecords = 10
        self.timeout = 10
        self.constraints = []

        # Servers tab
        self.cmbConnectionsServices.activated.connect(self.save_connection)
        self.cmbConnectionsSearch.activated.connect(self.save_connection)
        self.btnServerInfo.clicked.connect(self.connection_info)
        self.btnAddDefault.clicked.connect(self.add_default_connections)
        self.btnCapabilities.clicked.connect(self.show_xml)
        self.tabWidget.currentChanged.connect(self.populate_connection_list)

        # server management buttons
        self.btnNew.clicked.connect(self.add_connection)
        self.btnEdit.clicked.connect(self.edit_connection)
        self.btnDelete.clicked.connect(self.delete_connection)
        self.btnLoad.clicked.connect(self.load_connections)
        self.btnSave.clicked.connect(save_connections)

        # Search tab
        self.treeRecords.itemSelectionChanged.connect(self.record_clicked)
        self.treeRecords.itemDoubleClicked.connect(self.show_metadata)
        self.btnSearch.clicked.connect(self.search)
        self.leKeywords.returnPressed.connect(self.search)
        # prevent dialog from closing upon pressing enter
        self.buttonBox.button(QDialogButtonBox.Close).setAutoDefault(False)
        # launch help from button
        self.buttonBox.helpRequested.connect(self.help)
        self.btnCanvasBbox.setAutoDefault(False)
        self.btnCanvasBbox.clicked.connect(self.set_bbox_from_map)
        self.btnGlobalBbox.clicked.connect(self.set_bbox_global)

        # navigation buttons
        self.btnFirst.clicked.connect(self.navigate)
        self.btnPrev.clicked.connect(self.navigate)
        self.btnNext.clicked.connect(self.navigate)
        self.btnLast.clicked.connect(self.navigate)

        self.btnAddToWms.clicked.connect(self.add_to_ows)
        self.btnAddToWfs.clicked.connect(self.add_to_ows)
        self.btnAddToWcs.clicked.connect(self.add_to_ows)
        self.btnShowXml.clicked.connect(self.show_xml)

        # settings
        self.radioTitleAsk.clicked.connect(self.set_ows_save_title_ask)
        self.radioTitleNoAsk.clicked.connect(self.set_ows_save_title_no_ask)
        self.radioTempName.clicked.connect(self.set_ows_save_temp_name)

        self.manageGui()

    def manageGui(self):
        """open window"""

        self.tabWidget.setCurrentIndex(0)
        self.populate_connection_list()
        self.btnCapabilities.setEnabled(False)
        self.spnRecords.setValue(
            self.settings.value('/MetaSearch/returnRecords', 10, int))

        key = '/MetaSearch/%s' % self.cmbConnectionsSearch.currentText()
        self.catalog_url = self.settings.value('%s/url' % key)

        self.set_bbox_global()

        self.reset_buttons()

        # get preferred connection save strategy from settings and set it
        save_strat = self.settings.value('/MetaSearch/ows_save_strategy',
                                         'title_ask')
        if save_strat == 'temp_name':
            self.radioTempName.setChecked(True)
        elif save_strat == 'title_no_ask':
            self.radioTitleNoAsk.setChecked(True)
        else:
            self.radioTitleAsk.setChecked(True)

        # install proxy handler if specified in QGIS settings
        self.install_proxy()

    # Servers tab

    def populate_connection_list(self):
        """populate select box with connections"""

        self.settings.beginGroup('/MetaSearch/')
        self.cmbConnectionsServices.clear()
        self.cmbConnectionsServices.addItems(self.settings.childGroups())
        self.cmbConnectionsSearch.clear()
        self.cmbConnectionsSearch.addItems(self.settings.childGroups())
        self.settings.endGroup()

        self.set_connection_list_position()

        if self.cmbConnectionsServices.count() == 0:
            # no connections - disable various buttons
            state_disabled = False
            self.btnSave.setEnabled(state_disabled)
            # and start with connection tab open
            self.tabWidget.setCurrentIndex(1)
            # tell the user to add services
            msg = self.tr('No services/connections defined. To get '
                          'started with MetaSearch, create a new '
                          'connection by clicking \'New\' or click '
                          '\'Add default services\'.')
            self.textMetadata.setHtml('<p><h3>%s</h3></p>' % msg)
        else:
            # connections - enable various buttons
            state_disabled = True

        self.btnServerInfo.setEnabled(state_disabled)
        self.btnEdit.setEnabled(state_disabled)
        self.btnDelete.setEnabled(state_disabled)

    def set_connection_list_position(self):
        """set the current index to the selected connection"""
        to_select = self.settings.value('/MetaSearch/selected')
        conn_count = self.cmbConnectionsServices.count()

        if conn_count == 0:
            self.btnDelete.setEnabled(False)
            self.btnServerInfo.setEnabled(False)
            self.btnEdit.setEnabled(False)

        # does to_select exist in cmbConnectionsServices?
        exists = False
        for i in range(conn_count):
            if self.cmbConnectionsServices.itemText(i) == to_select:
                self.cmbConnectionsServices.setCurrentIndex(i)
                self.cmbConnectionsSearch.setCurrentIndex(i)
                exists = True
                break

        # If we couldn't find the stored item, but there are some, default
        # to the last item (this makes some sense when deleting items as it
        # allows the user to repeatidly click on delete to remove a whole
        # lot of items)
        if not exists and conn_count > 0:
            # If to_select is null, then the selected connection wasn't found
            # by QSettings, which probably means that this is the first time
            # the user has used CSWClient, so default to the first in the list
            # of connetions. Otherwise default to the last.
            if not to_select:
                current_index = 0
            else:
                current_index = conn_count - 1

            self.cmbConnectionsServices.setCurrentIndex(current_index)
            self.cmbConnectionsSearch.setCurrentIndex(current_index)

    def save_connection(self):
        """save connection"""

        caller = self.sender().objectName()

        if caller == 'cmbConnectionsServices':  # servers tab
            current_text = self.cmbConnectionsServices.currentText()
        elif caller == 'cmbConnectionsSearch':  # search tab
            current_text = self.cmbConnectionsSearch.currentText()

        self.settings.setValue('/MetaSearch/selected', current_text)
        key = '/MetaSearch/%s' % current_text

        if caller == 'cmbConnectionsSearch':  # bind to service in search tab
            self.catalog_url = self.settings.value('%s/url' % key)

        if caller == 'cmbConnectionsServices':  # clear server metadata
            self.textMetadata.clear()

        self.btnCapabilities.setEnabled(False)

    def connection_info(self):
        """show connection info"""

        current_text = self.cmbConnectionsServices.currentText()
        key = '/MetaSearch/%s' % current_text
        self.catalog_url = self.settings.value('%s/url' % key)

        # connect to the server
        if not self._get_csw():
            return

        QApplication.restoreOverrideCursor()

        if self.catalog:  # display service metadata
            self.btnCapabilities.setEnabled(True)
            metadata = render_template('en', self.context, self.catalog,
                                       'service_metadata.html')
            style = QgsApplication.reportStyleSheet()
            self.textMetadata.clear()
            self.textMetadata.document().setDefaultStyleSheet(style)
            self.textMetadata.setHtml(metadata)

    def add_connection(self):
        """add new service"""

        conn_new = NewConnectionDialog()
        conn_new.setWindowTitle(self.tr('New Catalogue service'))
        if conn_new.exec_() == QDialog.Accepted:  # add to service list
            self.populate_connection_list()
        self.textMetadata.clear()

    def edit_connection(self):
        """modify existing connection"""

        current_text = self.cmbConnectionsServices.currentText()

        url = self.settings.value('/MetaSearch/%s/url' % current_text)

        conn_edit = NewConnectionDialog(current_text)
        conn_edit.setWindowTitle(self.tr('Edit Catalogue service'))
        conn_edit.leName.setText(current_text)
        conn_edit.leURL.setText(url)
        if conn_edit.exec_() == QDialog.Accepted:  # update service list
            self.populate_connection_list()

    def delete_connection(self):
        """delete connection"""

        current_text = self.cmbConnectionsServices.currentText()

        key = '/MetaSearch/%s' % current_text

        msg = self.tr('Remove service %s?') % current_text

        result = QMessageBox.information(self, self.tr('Confirm delete'), msg,
                                         QMessageBox.Ok | QMessageBox.Cancel)
        if result == QMessageBox.Ok:  # remove service from list
            self.settings.remove(key)
            index_to_delete = self.cmbConnectionsServices.currentIndex()
            self.cmbConnectionsServices.removeItem(index_to_delete)
            self.cmbConnectionsSearch.removeItem(index_to_delete)
            self.set_connection_list_position()

    def load_connections(self):
        """load services from list"""

        ManageConnectionsDialog(1).exec_()
        self.populate_connection_list()

    def add_default_connections(self):
        """add default connections"""

        filename = os.path.join(self.context.ppath, 'resources',
                                'connections-default.xml')
        doc = get_connections_from_file(self, filename)
        if doc is None:
            return

        self.settings.beginGroup('/MetaSearch/')
        keys = self.settings.childGroups()
        self.settings.endGroup()

        for server in doc.findall('csw'):
            name = server.attrib.get('name')
            # check for duplicates
            if name in keys:
                msg = self.tr('%s exists.  Overwrite?') % name
                res = QMessageBox.warning(self, self.tr('Loading connections'),
                                          msg,
                                          QMessageBox.Yes | QMessageBox.No)
                if res != QMessageBox.Yes:
                    continue

            # no dups detected or overwrite is allowed
            key = '/MetaSearch/%s' % name
            self.settings.setValue('%s/url' % key, server.attrib.get('url'))

        self.populate_connection_list()

    # Settings tab

    def set_ows_save_title_ask(self):
        """save ows save strategy as save ows title, ask if duplicate"""

        self.settings.setValue('/MetaSearch/ows_save_strategy', 'title_ask')

    def set_ows_save_title_no_ask(self):
        """save ows save strategy as save ows title, do NOT ask if duplicate"""

        self.settings.setValue('/MetaSearch/ows_save_strategy', 'title_no_ask')

    def set_ows_save_temp_name(self):
        """save ows save strategy as save with a temporary name"""

        self.settings.setValue('/MetaSearch/ows_save_strategy', 'temp_name')

    # Search tab

    def set_bbox_from_map(self):
        """set bounding box from map extent"""

        crs = self.map.mapRenderer().destinationCrs()
        crsid = int(crs.authid().split(':')[1])

        extent = self.map.extent()

        if crsid != 4326:  # reproject to EPSG:4326
            src = QgsCoordinateReferenceSystem(crsid)
            dest = QgsCoordinateReferenceSystem(4326)
            xform = QgsCoordinateTransform(src, dest)
            minxy = xform.transform(
                QgsPoint(extent.xMinimum(), extent.yMinimum()))
            maxxy = xform.transform(
                QgsPoint(extent.xMaximum(), extent.yMaximum()))
            minx, miny = minxy
            maxx, maxy = maxxy
        else:  # 4326
            minx = extent.xMinimum()
            miny = extent.yMinimum()
            maxx = extent.xMaximum()
            maxy = extent.yMaximum()

        self.leNorth.setText(unicode(maxy)[0:9])
        self.leSouth.setText(unicode(miny)[0:9])
        self.leWest.setText(unicode(minx)[0:9])
        self.leEast.setText(unicode(maxx)[0:9])

    def set_bbox_global(self):
        """set global bounding box"""
        self.leNorth.setText('90')
        self.leSouth.setText('-90')
        self.leWest.setText('-180')
        self.leEast.setText('180')

    def search(self):
        """execute search"""

        self.catalog = None
        self.constraints = []

        # clear all fields and disable buttons
        self.lblResults.clear()
        self.treeRecords.clear()

        self.reset_buttons()

        # save some settings
        self.settings.setValue('/MetaSearch/returnRecords',
                               self.spnRecords.cleanText())

        # set current catalogue
        current_text = self.cmbConnectionsSearch.currentText()
        key = '/MetaSearch/%s' % current_text
        self.catalog_url = self.settings.value('%s/url' % key)

        # start position and number of records to return
        self.startfrom = 0
        self.maxrecords = self.spnRecords.value()

        # set timeout
        self.timeout = self.spnTimeout.value()

        # bbox
        minx = self.leWest.text()
        miny = self.leSouth.text()
        maxx = self.leEast.text()
        maxy = self.leNorth.text()
        bbox = [minx, miny, maxx, maxy]

        # only apply spatial filter if bbox is not global
        # even for a global bbox, if a spatial filter is applied, then
        # the CSW server will skip records without a bbox
        if bbox != ['-180', '-90', '180', '90']:
            self.constraints.append(BBox(bbox))

        # keywords
        if self.leKeywords.text():
            # TODO: handle multiple word searches
            keywords = self.leKeywords.text()
            self.constraints.append(PropertyIsLike('csw:AnyText', keywords))

        if len(self.constraints) > 1:  # exclusive search (a && b)
            self.constraints = [self.constraints]

        # build request
        if not self._get_csw():
            return

        # TODO: allow users to select resources types
        # to find ('service', 'dataset', etc.)
        try:
            self.catalog.getrecords2(constraints=self.constraints,
                                     maxrecords=self.maxrecords,
                                     esn='full')
        except ExceptionReport as err:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(self, self.tr('Search error'),
                                self.tr('Search error: %s') % err)
            return
        except Exception as err:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(self, self.tr('Connection error'),
                                self.tr('Connection error: %s') % err)
            return

        if self.catalog.results['matches'] == 0:
            QApplication.restoreOverrideCursor()
            self.lblResults.setText(self.tr('0 results'))
            return

        QApplication.restoreOverrideCursor()
        self.display_results()

    def display_results(self):
        """display search results"""

        self.treeRecords.clear()

        position = self.catalog.results['returned'] + self.startfrom

        msg = self.tr(
            'Showing %d - %d of %n result(s)', 'number of results',
            self.catalog.results['matches']) % (self.startfrom + 1, position)

        self.lblResults.setText(msg)

        for rec in self.catalog.records:
            item = QTreeWidgetItem(self.treeRecords)
            if self.catalog.records[rec].type:
                item.setText(0, normalize_text(self.catalog.records[rec].type))
            else:
                item.setText(0, 'unknown')
            if self.catalog.records[rec].title:
                item.setText(1,
                             normalize_text(self.catalog.records[rec].title))
            if self.catalog.records[rec].identifier:
                set_item_data(item, 'identifier',
                              self.catalog.records[rec].identifier)

        self.btnShowXml.setEnabled(True)

        if self.catalog.results["matches"] < self.maxrecords:
            disabled = False
        else:
            disabled = True

        self.btnFirst.setEnabled(disabled)
        self.btnPrev.setEnabled(disabled)
        self.btnNext.setEnabled(disabled)
        self.btnLast.setEnabled(disabled)

    def record_clicked(self):
        """record clicked signal"""

        # disable only service buttons
        self.reset_buttons(True, False, False)

        if not self.treeRecords.selectedItems():
            return

        item = self.treeRecords.currentItem()
        if not item:
            return

        identifier = get_item_data(item, 'identifier')
        try:
            record = self.catalog.records[identifier]
        except KeyError as err:
            QMessageBox.warning(self, self.tr('Record parsing error'),
                                'Unable to locate record identifier')
            return

        # if the record has a bbox, show a footprint on the map
        if record.bbox is not None:
            points = bbox_to_polygon(record.bbox)
            if points is not None:
                src = QgsCoordinateReferenceSystem(4326)
                dst = self.map.mapRenderer().destinationCrs()
                geom = QgsGeometry.fromPolygon(points)
                if src.postgisSrid() != dst.postgisSrid():
                    ctr = QgsCoordinateTransform(src, dst)
                    try:
                        geom.transform(ctr)
                    except Exception as err:
                        QMessageBox.warning(
                            self, self.tr('Coordinate Transformation Error'),
                            unicode(err))
                self.rubber_band.setToGeometry(geom, None)

        # figure out if the data is interactive and can be operated on
        self.find_services(record, item)

    def find_services(self, record, item):
        """scan record for WMS/WMTS|WFS|WCS endpoints"""

        links = record.uris + record.references

        services = {}
        for link in links:

            if 'scheme' in link:
                link_type = link['scheme']
            elif 'protocol' in link:
                link_type = link['protocol']
            else:
                link_type = None

            if link_type is not None:
                link_type = link_type.upper()

            wmswmst_link_types = map(str.upper, link_types.WMSWMST_LINK_TYPES)
            wfs_link_types = map(str.upper, link_types.WFS_LINK_TYPES)
            wcs_link_types = map(str.upper, link_types.WCS_LINK_TYPES)

            # if the link type exists, and it is one of the acceptable
            # interactive link types, then set
            if all([
                    link_type is not None, link_type
                    in wmswmst_link_types + wfs_link_types + wcs_link_types
            ]):
                if link_type in wmswmst_link_types:
                    services['wms'] = link['url']
                    self.btnAddToWms.setEnabled(True)
                if link_type in wfs_link_types:
                    services['wfs'] = link['url']
                    self.btnAddToWfs.setEnabled(True)
                if link_type in wcs_link_types:
                    services['wcs'] = link['url']
                    self.btnAddToWcs.setEnabled(True)

            set_item_data(item, 'link', json.dumps(services))

    def navigate(self):
        """manage navigation / paging"""

        caller = self.sender().objectName()

        if caller == 'btnFirst':
            self.startfrom = 0
        elif caller == 'btnLast':
            self.startfrom = self.catalog.results['matches'] - self.maxrecords
        elif caller == 'btnNext':
            self.startfrom += self.maxrecords
            if self.startfrom >= self.catalog.results["matches"]:
                msg = self.tr('End of results. Go to start?')
                res = QMessageBox.information(
                    self, self.tr('Navigation'), msg,
                    (QMessageBox.Ok | QMessageBox.Cancel))
                if res == QMessageBox.Ok:
                    self.startfrom = 0
                else:
                    return
        elif caller == "btnPrev":
            self.startfrom -= self.maxrecords
            if self.startfrom <= 0:
                msg = self.tr('Start of results. Go to end?')
                res = QMessageBox.information(
                    self, self.tr('Navigation'), msg,
                    (QMessageBox.Ok | QMessageBox.Cancel))
                if res == QMessageBox.Ok:
                    self.startfrom = (self.catalog.results['matches'] -
                                      self.maxrecords)
                else:
                    return

        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        try:
            self.catalog.getrecords2(constraints=self.constraints,
                                     maxrecords=self.maxrecords,
                                     startposition=self.startfrom,
                                     esn='full')
        except ExceptionReport as err:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(self, self.tr('Search error'),
                                self.tr('Search error: %s') % err)
            return
        except Exception as err:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(self, self.tr('Connection error'),
                                self.tr('Connection error: %s') % err)
            return

        QApplication.restoreOverrideCursor()

        self.display_results()

    def add_to_ows(self):
        """add to OWS provider connection list"""

        conn_name_matches = []

        item = self.treeRecords.currentItem()

        if not item:
            return

        item_data = json.loads(get_item_data(item, 'link'))

        caller = self.sender().objectName()

        # stype = human name,/Qgis/connections-%s,providername
        if caller == 'btnAddToWms':
            stype = ['OGC:WMS/OGC:WMTS', 'wms', 'wms']
            data_url = item_data['wms']
        elif caller == 'btnAddToWfs':
            stype = ['OGC:WFS', 'wfs', 'WFS']
            data_url = item_data['wfs']
        elif caller == 'btnAddToWcs':
            stype = ['OGC:WCS', 'wcs', 'wcs']
            data_url = item_data['wcs']

        QApplication.restoreOverrideCursor()

        sname = '%s from MetaSearch' % stype[1]

        # store connection
        # check if there is a connection with same name
        self.settings.beginGroup('/Qgis/connections-%s' % stype[1])
        keys = self.settings.childGroups()
        self.settings.endGroup()

        for key in keys:
            if key.startswith(sname):
                conn_name_matches.append(key)
        if conn_name_matches:
            sname = conn_name_matches[-1]

        # check for duplicates
        if sname in keys:  # duplicate found
            if self.radioTitleAsk.isChecked():  # ask to overwrite
                msg = self.tr('Connection %s exists. Overwrite?') % sname
                res = QMessageBox.warning(self, self.tr('Saving server'), msg,
                                          QMessageBox.Yes | QMessageBox.No)
                if res != QMessageBox.Yes:  # assign new name with serial
                    sname = serialize_string(sname)
            elif self.radioTitleNoAsk.isChecked():  # don't ask to overwrite
                pass
            elif self.radioTempName.isChecked():  # use temp name
                sname = serialize_string(sname)

        # no dups detected or overwrite is allowed
        self.settings.beginGroup('/Qgis/connections-%s' % stype[1])
        self.settings.setValue('/%s/url' % sname, data_url)
        self.settings.endGroup()

        # open provider window
        ows_provider = QgsProviderRegistry.instance().selectWidget(
            stype[2], self)

        service_type = stype[0]

        # connect dialog signals to iface slots
        if service_type == 'OGC:WMS/OGC:WMTS':
            ows_provider.connect(
                ows_provider,
                SIGNAL('addRasterLayer(QString, QString, QString)'),
                self.iface, SLOT('addRasterLayer(QString, QString, QString)'))
            conn_cmb = ows_provider.findChild(QWidget, 'cmbConnections')
            connect = 'on_btnConnect_clicked'
        elif service_type == 'OGC:WFS':
            ows_provider.connect(ows_provider,
                                 SIGNAL('addWfsLayer(QString, QString)'),
                                 self.iface.mainWindow(),
                                 SLOT('addWfsLayer(QString, QString)'))
            conn_cmb = ows_provider.findChild(QWidget, 'cmbConnections')
            connect = 'connectToServer'
        elif service_type == 'OGC:WCS':
            ows_provider.connect(
                ows_provider,
                SIGNAL('addRasterLayer(QString, QString, QString)'),
                self.iface, SLOT('addRasterLayer(QString, QString, QString)'))
            conn_cmb = ows_provider.findChild(QWidget, 'mConnectionsComboBox')
            connect = 'on_mConnectButton_clicked'
        ows_provider.setModal(False)
        ows_provider.show()

        # open provider dialogue against added OWS
        index = conn_cmb.findText(sname)
        if index > -1:
            conn_cmb.setCurrentIndex(index)
            # only for wfs
            if service_type == 'OGC:WFS':
                ows_provider.on_cmbConnections_activated(index)
        getattr(ows_provider, connect)()

    def show_metadata(self):
        """show record metadata"""

        if not self.treeRecords.selectedItems():
            return

        item = self.treeRecords.currentItem()
        if not item:
            return

        identifier = get_item_data(item, 'identifier')

        try:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout)
            cat.getrecordbyid([self.catalog.records[identifier].identifier])
        except ExceptionReport as err:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(self, self.tr('GetRecords error'),
                                self.tr('Error getting response: %s') % err)
            return
        except KeyError as err:
            QMessageBox.warning(self, self.tr('Record parsing error'),
                                'Unable to locate record identifier')
            QApplication.restoreOverrideCursor()
            return

        QApplication.restoreOverrideCursor()

        record = cat.records[identifier]
        record.xml_url = cat.request

        crd = RecordDialog()
        metadata = render_template('en', self.context, record,
                                   'record_metadata_dc.html')

        style = QgsApplication.reportStyleSheet()
        crd.textMetadata.document().setDefaultStyleSheet(style)
        crd.textMetadata.setHtml(metadata)
        crd.exec_()

    def show_xml(self):
        """show XML request / response"""

        crd = XMLDialog()
        request_html = highlight_xml(self.context, self.catalog.request)
        response_html = highlight_xml(self.context, self.catalog.response)
        style = QgsApplication.reportStyleSheet()
        crd.txtbrXMLRequest.clear()
        crd.txtbrXMLResponse.clear()
        crd.txtbrXMLRequest.document().setDefaultStyleSheet(style)
        crd.txtbrXMLResponse.document().setDefaultStyleSheet(style)
        crd.txtbrXMLRequest.setHtml(request_html)
        crd.txtbrXMLResponse.setHtml(response_html)
        crd.exec_()

    def reset_buttons(self, services=True, xml=True, navigation=True):
        """Convenience function to disable WMS/WMTS|WFS|WCS buttons"""

        if services:
            self.btnAddToWms.setEnabled(False)
            self.btnAddToWfs.setEnabled(False)
            self.btnAddToWcs.setEnabled(False)

        if xml:
            self.btnShowXml.setEnabled(False)

        if navigation:
            self.btnFirst.setEnabled(False)
            self.btnPrev.setEnabled(False)
            self.btnNext.setEnabled(False)
            self.btnLast.setEnabled(False)

    def help(self):
        """launch help"""

        open_url(get_help_url())

    def reject(self):
        """back out of dialogue"""

        QDialog.reject(self)
        self.rubber_band.reset()

    def _get_csw(self):
        """convenience function to init owslib.csw.CatalogueServiceWeb"""

        # connect to the server
        try:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
            self.catalog = CatalogueServiceWeb(self.catalog_url,
                                               timeout=self.timeout)
            return True
        except ExceptionReport as err:
            msg = self.tr('Error connecting to service: %s') % err
        except ValueError as err:
            msg = self.tr('Value Error: %s') % err
        except Exception as err:
            msg = self.tr('Unknown Error: %s') % err

        QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
        QApplication.restoreOverrideCursor()
        return False

    def install_proxy(self):
        """set proxy if one is set in QGIS network settings"""

        # initially support HTTP for now
        if self.settings.value('/proxy/proxyEnabled') == 'true':
            if self.settings.value('/proxy/proxyType') == 'HttpProxy':
                ptype = 'http'
            else:
                return

            user = self.settings.value('/proxy/proxyUser')
            password = self.settings.value('/proxy/proxyPassword')
            host = self.settings.value('/proxy/proxyHost')
            port = self.settings.value('/proxy/proxyPort')

            proxy_up = ''
            proxy_port = ''

            if all([user != '', password != '']):
                proxy_up = '%s:%s@' % (user, password)

            if port != '':
                proxy_port = ':%s' % port

            conn = '%s://%s%s%s' % (ptype, proxy_up, host, proxy_port)
            install_opener(build_opener(ProxyHandler({ptype: conn})))