12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- ########################################################################
- # Searx-qt - Lightweight desktop application for SearX.
- # Copyright (C) 2020 CYBERDEViL
- #
- # This file is part of Searx-qt.
- #
- # Searx-qt 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 3 of the License, or
- # (at your option) any later version.
- #
- # Searx-qt 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, see <https://www.gnu.org/licenses/>.
- #
- ########################################################################
- from urllib.parse import urlparse
- from PyQt5.QtWidgets import (
- QFormLayout,
- QLabel,
- QLineEdit,
- QComboBox,
- QDialog
- )
- from searxqt.widgets.buttons import Button
- from searxqt.translations import _
- class UrlDialog(QDialog):
- def __init__(self, url='', acceptTxt=_("Save"), parent=None):
- QDialog.__init__(self, parent=parent)
- layout = QFormLayout(self)
- label = QLabel("Scheme:")
- self._schemeSelect = QComboBox(self)
- self._schemeSelect.addItems(["http", "https"])
- layout.addRow(label, self._schemeSelect)
- label = QLabel("URL:")
- self._urlEdit = QLineEdit(self)
- layout.addRow(label, self._urlEdit)
- self._cancelButton = Button(_("Cancel"), self)
- self._saveButton = Button(acceptTxt, self)
- layout.addRow(self._cancelButton, self._saveButton)
- self._urlEdit.textEdited.connect(self.__inputChanged)
- self._saveButton.clicked.connect(self.accept)
- self._cancelButton.clicked.connect(self.reject)
- parsedUrl = urlparse(url)
- if parsedUrl.scheme == 'http':
- self._schemeSelect.setCurrentIndex(0)
- else:
- # Default to https
- self._schemeSelect.setCurrentIndex(1)
- self._urlEdit.setText(
- "{0}{1}".format(parsedUrl.netloc, parsedUrl.path)
- )
- def __inputChanged(self, text):
- newText = text.replace("https://", "").replace("http://", "")
- if text != newText:
- self._urlEdit.setText(newText)
- if self.isValid():
- self._saveButton.setEnabled(True)
- else:
- self._saveButton.setEnabled(False)
- def isValid(self):
- if urlparse(self.__currentUrl()).netloc:
- return True
- return False
- def __currentUrl(self):
- return "{0}://{1}".format(
- self._schemeSelect.currentText(),
- self._urlEdit.text()
- )
- @property
- def url(self):
- if self.isValid():
- return self.__currentUrl()
- return ''
|