manageconnectionsdialog.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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) 2014 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. import xml.etree.ElementTree as etree
  29. from qgis.core import QgsSettings
  30. from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QFileDialog, QListWidgetItem, QMessageBox # noqa
  31. from MetaSearch.util import (get_connections_from_file, get_ui_class,
  32. prettify_xml)
  33. BASE_CLASS = get_ui_class('manageconnectionsdialog.ui')
  34. class ManageConnectionsDialog(QDialog, BASE_CLASS):
  35. """manage connections"""
  36. def __init__(self, mode):
  37. """init dialog"""
  38. QDialog.__init__(self)
  39. self.setupUi(self)
  40. self.settings = QgsSettings()
  41. self.filename = None
  42. self.mode = mode # 0 - save, 1 - load
  43. self.btnBrowse.clicked.connect(self.select_file)
  44. self.manage_gui()
  45. def manage_gui(self):
  46. """manage interface"""
  47. if self.mode == 1:
  48. self.label.setText(self.tr('Load from file'))
  49. self.buttonBox.button(QDialogButtonBox.Ok).setText(self.tr('Load'))
  50. else:
  51. self.label.setText(self.tr('Save to file'))
  52. self.buttonBox.button(QDialogButtonBox.Ok).setText(self.tr('Save'))
  53. self.populate()
  54. self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
  55. def select_file(self):
  56. """select file ops"""
  57. label = self.tr('eXtensible Markup Language (*.xml *.XML)')
  58. if self.mode == 0:
  59. slabel = self.tr('Save Connections')
  60. self.filename, filter = QFileDialog.getSaveFileName(self, slabel,
  61. '.', label)
  62. else:
  63. slabel = self.tr('Load Connections')
  64. self.filename, selected_filter = QFileDialog.getOpenFileName(
  65. self, slabel, '.', label)
  66. if not self.filename:
  67. return
  68. # ensure the user never omitted the extension from the file name
  69. if not self.filename.lower().endswith('.xml'):
  70. self.filename = '%s.xml' % self.filename
  71. self.leFileName.setText(self.filename)
  72. if self.mode == 1:
  73. self.populate()
  74. self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True)
  75. def populate(self):
  76. """populate connections list from settings"""
  77. if self.mode == 0:
  78. self.settings.beginGroup('/MetaSearch/')
  79. keys = self.settings.childGroups()
  80. for key in keys:
  81. item = QListWidgetItem(self.listConnections)
  82. item.setText(key)
  83. self.settings.endGroup()
  84. else: # populate connections list from file
  85. doc = get_connections_from_file(self, self.filename)
  86. if doc is None:
  87. self.filename = None
  88. self.leFileName.clear()
  89. self.listConnections.clear()
  90. return
  91. for catalog in doc.findall('csw'):
  92. item = QListWidgetItem(self.listConnections)
  93. item.setText(catalog.attrib.get('name'))
  94. def save(self, connections):
  95. """save connections ops"""
  96. doc = etree.Element('qgsCSWConnections')
  97. doc.attrib['version'] = '1.0'
  98. for conn in connections:
  99. url = self.settings.value('/MetaSearch/%s/url' % conn)
  100. type_ = self.settings.value('/MetaSearch/%s/catalog-type' % conn)
  101. if url is not None:
  102. connection = etree.SubElement(doc, 'csw')
  103. connection.attrib['name'] = conn
  104. connection.attrib['type'] = type_ or 'OGC CSW 2.0.2'
  105. connection.attrib['url'] = url
  106. # write to disk
  107. with open(self.filename, 'w') as fileobj:
  108. fileobj.write(prettify_xml(etree.tostring(doc)))
  109. QMessageBox.information(self, self.tr('Save Connections'),
  110. self.tr('Saved to {0}.').format(self.filename))
  111. self.reject()
  112. def load(self, items):
  113. """load connections"""
  114. self.settings.beginGroup('/MetaSearch/')
  115. keys = self.settings.childGroups()
  116. self.settings.endGroup()
  117. exml = etree.parse(self.filename).getroot()
  118. for catalog in exml.findall('csw'):
  119. conn_name = catalog.attrib.get('name')
  120. # process only selected connections
  121. if conn_name not in items:
  122. continue
  123. # check for duplicates
  124. if conn_name in keys:
  125. label = self.tr('File {0} exists. Overwrite?').format(
  126. conn_name)
  127. res = QMessageBox.warning(self, self.tr('Loading Connections'),
  128. label,
  129. QMessageBox.Yes | QMessageBox.No)
  130. if res != QMessageBox.Yes:
  131. continue
  132. # no dups detected or overwrite is allowed
  133. url = '/MetaSearch/%s/url' % conn_name
  134. self.settings.setValue(url, catalog.attrib.get('url'))
  135. self.settings.setValue(url, catalog.attrib.get('catalog-type', 'OGC CSW 2.0.2'))
  136. def accept(self):
  137. """accept connections"""
  138. selection = self.listConnections.selectedItems()
  139. if len(selection) == 0:
  140. return
  141. items = []
  142. for sel in selection:
  143. items.append(sel.text())
  144. if self.mode == 0: # save
  145. self.save(items)
  146. else: # load
  147. self.load(items)
  148. self.filename = None
  149. self.leFileName.clear()
  150. self.listConnections.clear()
  151. self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
  152. def reject(self):
  153. """back out of manage connections dialogue"""
  154. QDialog.reject(self)