dialogs.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 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. QTextBrowser,
  29. QVBoxLayout
  30. )
  31. from searxqt.core.requests import ErrorTypeStr
  32. from searxqt.widgets.buttons import Button
  33. from searxqt.translations import _
  34. class ErrorMessage(QDialog):
  35. def __init__(self, error, parent=None):
  36. QDialog.__init__(self, parent=parent)
  37. self.setWindowTitle(_("Error"))
  38. layout = QVBoxLayout(self)
  39. label = QLabel(_("A error has occured!"), self)
  40. errorMessage = QTextBrowser(self)
  41. okButton = Button(_("Ok"), self)
  42. layout.addWidget(label)
  43. layout.addWidget(errorMessage)
  44. layout.addWidget(okButton)
  45. errorMessage.setPlainText(error)
  46. okButton.clicked.connect(self.accept)
  47. class RequestsErrorMessage(ErrorMessage):
  48. def __init__(self, errorType, errorMsg, parent=None):
  49. errorStr = f"Error type: {ErrorTypeStr[errorType]}\n\n{errorMsg}"
  50. ErrorMessage.__init__(self, errorStr, parent=parent)
  51. class UrlDialog(QDialog):
  52. def __init__(self, url='', acceptTxt=_("Save"), parent=None):
  53. QDialog.__init__(self, parent=parent)
  54. layout = QFormLayout(self)
  55. label = QLabel("Scheme:")
  56. self._schemeSelect = QComboBox(self)
  57. self._schemeSelect.addItems(["http", "https"])
  58. layout.addRow(label, self._schemeSelect)
  59. label = QLabel("URL:")
  60. self._urlEdit = QLineEdit(self)
  61. layout.addRow(label, self._urlEdit)
  62. self._cancelButton = Button(_("Cancel"), self)
  63. self._saveButton = Button(acceptTxt, self)
  64. layout.addRow(self._cancelButton, self._saveButton)
  65. self._urlEdit.textEdited.connect(self.__inputChanged)
  66. self._saveButton.clicked.connect(self.accept)
  67. self._cancelButton.clicked.connect(self.reject)
  68. parsedUrl = urlparse(url)
  69. if parsedUrl.scheme == 'http':
  70. self._schemeSelect.setCurrentIndex(0)
  71. else:
  72. # Default to https
  73. self._schemeSelect.setCurrentIndex(1)
  74. self._urlEdit.setText(
  75. "{0}{1}".format(parsedUrl.netloc, parsedUrl.path)
  76. )
  77. def __inputChanged(self, text):
  78. newText = text.replace("https://", "").replace("http://", "")
  79. if text != newText:
  80. self._urlEdit.setText(newText)
  81. if self.isValid():
  82. self._saveButton.setEnabled(True)
  83. else:
  84. self._saveButton.setEnabled(False)
  85. def isValid(self):
  86. if urlparse(self.__currentUrl()).netloc:
  87. return True
  88. return False
  89. def __currentUrl(self):
  90. return "{0}://{1}".format(
  91. self._schemeSelect.currentText(),
  92. self._urlEdit.text()
  93. )
  94. @property
  95. def url(self):
  96. if self.isValid():
  97. return self.__currentUrl()
  98. return ''