dialogs.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Searx-qt.
  6. #
  7. # Searx-qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from urllib.parse import urlparse
  22. from PyQt5.QtWidgets import (
  23. QFormLayout,
  24. QLabel,
  25. QLineEdit,
  26. QComboBox,
  27. QDialog
  28. )
  29. from searxqt.widgets.buttons import Button
  30. from searxqt.translations import _
  31. class UrlDialog(QDialog):
  32. def __init__(self, url='', acceptTxt=_("Save"), parent=None):
  33. QDialog.__init__(self, parent=parent)
  34. layout = QFormLayout(self)
  35. label = QLabel("Scheme:")
  36. self._schemeSelect = QComboBox(self)
  37. self._schemeSelect.addItems(["http", "https"])
  38. layout.addRow(label, self._schemeSelect)
  39. label = QLabel("URL:")
  40. self._urlEdit = QLineEdit(self)
  41. layout.addRow(label, self._urlEdit)
  42. self._cancelButton = Button(_("Cancel"), self)
  43. self._saveButton = Button(acceptTxt, self)
  44. layout.addRow(self._cancelButton, self._saveButton)
  45. self._urlEdit.textEdited.connect(self.__inputChanged)
  46. self._saveButton.clicked.connect(self.accept)
  47. self._cancelButton.clicked.connect(self.reject)
  48. parsedUrl = urlparse(url)
  49. if parsedUrl.scheme == 'http':
  50. self._schemeSelect.setCurrentIndex(0)
  51. else:
  52. # Default to https
  53. self._schemeSelect.setCurrentIndex(1)
  54. self._urlEdit.setText(
  55. "{0}{1}".format(parsedUrl.netloc, parsedUrl.path)
  56. )
  57. def __inputChanged(self, text):
  58. newText = text.replace("https://", "").replace("http://", "")
  59. if text != newText:
  60. self._urlEdit.setText(newText)
  61. if self.isValid():
  62. self._saveButton.setEnabled(True)
  63. else:
  64. self._saveButton.setEnabled(False)
  65. def isValid(self):
  66. if urlparse(self.__currentUrl()).netloc:
  67. return True
  68. return False
  69. def __currentUrl(self):
  70. return "{0}://{1}".format(
  71. self._schemeSelect.currentText(),
  72. self._urlEdit.text()
  73. )
  74. @property
  75. def url(self):
  76. if self.isValid():
  77. return self.__currentUrl()
  78. return ''