File: newconnectiondialog.py

package info (click to toggle)
qgis 3.40.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,181,336 kB
  • sloc: cpp: 1,593,302; python: 370,494; xml: 23,474; perl: 3,664; sh: 3,482; ansic: 2,257; sql: 2,133; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 157
file content (116 lines) | stat: -rw-r--r-- 4,212 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
###############################################################################
#
# CSW Client
# ---------------------------------------------------------
# QGIS Catalog Service client.
#
# Copyright (C) 2010 NextGIS (http://nextgis.org),
#                    Alexander Bruy (alexander.bruy@gmail.com),
#                    Maxim Dubinin (sim@gis-lab.info)
#
# Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com)
#
# This source is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
###############################################################################

from qgis.core import QgsSettings
from qgis.PyQt.QtWidgets import QDialog, QMessageBox

from MetaSearch.util import get_ui_class
from MetaSearch.search_backend import CATALOG_TYPES

BASE_CLASS = get_ui_class("newconnectiondialog.ui")


class NewConnectionDialog(QDialog, BASE_CLASS):
    """Dialogue to add a new CSW entry"""

    def __init__(self, conn_name=None):
        """init"""

        QDialog.__init__(self)
        self.setupUi(self)
        self.settings = QgsSettings()
        self.conn_name = None
        self.conn_name_orig = conn_name
        self.username = None
        self.password = None

        self.cmbCatalogType.addItems(CATALOG_TYPES)

    def accept(self):
        """add CSW entry"""

        conn_name = self.leName.text().strip()
        conn_url = self.leURL.text().strip()
        conn_username = self.leUsername.text().strip()
        conn_password = self.lePassword.text().strip()
        conn_catalog_type = self.cmbCatalogType.currentText()

        if any([conn_name == "", conn_url == ""]):
            QMessageBox.warning(
                self,
                self.tr("Save Connection"),
                self.tr("Both Name and URL must be provided."),
            )
            return

        if "/" in conn_name:
            QMessageBox.warning(
                self, self.tr("Save Connection"), self.tr("Name cannot contain '/'.")
            )
            return

        if conn_name is not None:
            key = "/MetaSearch/%s" % conn_name
            keyurl = "%s/url" % key
            key_orig = "/MetaSearch/%s" % self.conn_name_orig

            # warn if entry was renamed to an existing connection
            if all([self.conn_name_orig != conn_name, self.settings.contains(keyurl)]):
                res = QMessageBox.warning(
                    self,
                    self.tr("Save Connection"),
                    self.tr("Overwrite {0}?").format(conn_name),
                    QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel,
                )
                if res == QMessageBox.StandardButton.Cancel:
                    return

            # on rename delete original entry first
            if all([self.conn_name_orig is not None, self.conn_name_orig != conn_name]):
                self.settings.remove(key_orig)

            self.settings.setValue(keyurl, conn_url)
            self.settings.setValue("/MetaSearch/selected", conn_name)

            if conn_username != "":
                self.settings.setValue("%s/username" % key, conn_username)
            else:
                self.settings.remove("%s/username" % key)
            if conn_password != "":
                self.settings.setValue("%s/password" % key, conn_password)
            else:
                self.settings.remove("%s/password" % key)

            self.settings.setValue("%s/catalog-type" % key, conn_catalog_type)

            QDialog.accept(self)

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

        QDialog.reject(self)