newconnectiondialog.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ###############################################################################
  2. #
  3. # CSW Client
  4. # ---------------------------------------------------------
  5. # QGIS Catalog Service client.
  6. #
  7. # Copyright (C) 2010 NextGIS (http://nextgis.org),
  8. # Alexander Bruy (alexander.bruy@gmail.com),
  9. # Maxim Dubinin (sim@gis-lab.info)
  10. #
  11. # Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com)
  12. #
  13. # This source is free software; you can redistribute it and/or modify it under
  14. # the terms of the GNU General Public License as published by the Free
  15. # Software Foundation; either version 2 of the License, or (at your option)
  16. # any later version.
  17. #
  18. # This code is distributed in the hope that it will be useful, but WITHOUT ANY
  19. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  21. # details.
  22. #
  23. # You should have received a copy of the GNU General Public License along
  24. # with this program; if not, write to the Free Software Foundation, Inc.,
  25. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. #
  27. ###############################################################################
  28. from qgis.core import QgsSettings
  29. from qgis.PyQt.QtWidgets import QDialog, QMessageBox
  30. from MetaSearch.util import get_ui_class
  31. from MetaSearch.search_backend import CATALOG_TYPES
  32. BASE_CLASS = get_ui_class('newconnectiondialog.ui')
  33. class NewConnectionDialog(QDialog, BASE_CLASS):
  34. """Dialogue to add a new CSW entry"""
  35. def __init__(self, conn_name=None):
  36. """init"""
  37. QDialog.__init__(self)
  38. self.setupUi(self)
  39. self.settings = QgsSettings()
  40. self.conn_name = None
  41. self.conn_name_orig = conn_name
  42. self.username = None
  43. self.password = None
  44. self.cmbCatalogType.addItems(CATALOG_TYPES)
  45. def accept(self):
  46. """add CSW entry"""
  47. conn_name = self.leName.text().strip()
  48. conn_url = self.leURL.text().strip()
  49. conn_username = self.leUsername.text().strip()
  50. conn_password = self.lePassword.text().strip()
  51. conn_catalog_type = self.cmbCatalogType.currentText()
  52. if any([conn_name == '', conn_url == '']):
  53. QMessageBox.warning(self, self.tr('Save Connection'),
  54. self.tr('Both Name and URL must be provided.'))
  55. return
  56. if '/' in conn_name:
  57. QMessageBox.warning(self, self.tr('Save Connection'),
  58. self.tr('Name cannot contain \'/\'.'))
  59. return
  60. if conn_name is not None:
  61. key = '/MetaSearch/%s' % conn_name
  62. keyurl = '%s/url' % key
  63. key_orig = '/MetaSearch/%s' % self.conn_name_orig
  64. # warn if entry was renamed to an existing connection
  65. if all([self.conn_name_orig != conn_name,
  66. self.settings.contains(keyurl)]):
  67. res = QMessageBox.warning(
  68. self, self.tr('Save Connection'),
  69. self.tr('Overwrite {0}?').format(conn_name),
  70. QMessageBox.Ok | QMessageBox.Cancel)
  71. if res == QMessageBox.Cancel:
  72. return
  73. # on rename delete original entry first
  74. if all([self.conn_name_orig is not None,
  75. self.conn_name_orig != conn_name]):
  76. self.settings.remove(key_orig)
  77. self.settings.setValue(keyurl, conn_url)
  78. self.settings.setValue('/MetaSearch/selected', conn_name)
  79. if conn_username != '':
  80. self.settings.setValue('%s/username' % key, conn_username)
  81. else:
  82. self.settings.remove('%s/username' % key)
  83. if conn_password != '':
  84. self.settings.setValue('%s/password' % key, conn_password)
  85. else:
  86. self.settings.remove('%s/password' % key)
  87. self.settings.setValue('%s/catalog-type' % key, conn_catalog_type)
  88. QDialog.accept(self)
  89. def reject(self):
  90. """back out of dialogue"""
  91. QDialog.reject(self)