123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317 |
- ########################################################################
- # 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 PyQt5.QtWidgets import (
- QWidget,
- QVBoxLayout,
- QHBoxLayout,
- QLineEdit,
- QTextBrowser,
- QCheckBox,
- QLabel,
- QComboBox,
- QFrame,
- QMenu,
- QWidgetAction,
- QSpacerItem,
- QSizePolicy,
- QDialog,
- QListWidget,
- QTableView,
- QAbstractItemView,
- QHeaderView,
- QFormLayout,
- QMessageBox
- )
- from PyQt5.QtCore import pyqtSignal, Qt, QVariant
- from searxqt.models.search import (
- SearchStatus,
- UserCategoriesModel,
- CategoriesModel
- )
- from searxqt.models.instances import EnginesTableModel
- from searxqt.widgets.buttons import Button, CheckboxOptionsButton
- from searxqt.thread import Thread
- from searxqt.translations import _
- class SearchNavigation(QWidget):
- requestPage = pyqtSignal(int) # pageno
- def __init__(self, parent=None):
- QWidget.__init__(self, parent=parent)
- layout = QHBoxLayout(self)
- self.prevPageButton = Button("<<", self)
- self.pageNoLabel = QLabel("1", self)
- self.nextPageButton = Button(">>", self)
- layout.addWidget(self.prevPageButton, 0, Qt.AlignLeft)
- layout.addWidget(self.pageNoLabel, 0, Qt.AlignCenter)
- layout.addWidget(self.nextPageButton, 0, Qt.AlignRight)
- self.prevPageButton.setEnabled(False)
- self.nextPageButton.clicked.connect(self._nextPage)
- self.prevPageButton.clicked.connect(self._prevPage)
- self.reset()
- def _updateLabel(self):
- self.pageNoLabel.setText(str(self._pageno))
- def _nextPage(self):
- self._pageno += 1
- if self._pageno > 1 and not self.prevPageButton.isEnabled():
- self.prevPageButton.setEnabled(True)
- self._updateLabel()
- self.requestPage.emit(self._pageno)
- def _prevPage(self):
- self._pageno -= 1
- if self._pageno == 1:
- self.prevPageButton.setEnabled(False)
- self.setNextEnabled(True)
- self._updateLabel()
- self.requestPage.emit(self._pageno)
- def reset(self):
- self._pageno = 1
- self.prevPageButton.setEnabled(False)
- self._updateLabel()
- def setNextEnabled(self, state):
- self.nextPageButton.setEnabled(state)
- class SearchEngines(CheckboxOptionsButton):
- def __init__(self, searchModel, instancesModel, parent=None):
- """
- @param searchModel: needed for getting and setting current
- enabled/disabled engines.
- @type searchModel: SearchModel
- @param instancesModel: needed for listing current available
- engines and update it's current filter
- to filter out instances without atleast
- one of the required engine(s).
- @type instancesModel: InstanceModelFilter
- """
- self._instancesModel = instancesModel
- self._searchModel = searchModel
- CheckboxOptionsButton.__init__(
- self,
- labelName=_("Engines"),
- parent=parent
- )
- instancesModel.parentModel().changed.connect(self.reGenerate)
- def updateFilter(self):
- """ Filter out instances that don't support atleast one of the
- enabled engines.
- """
- self._instancesModel.updateKwargs(
- {'engines': self.getCheckedOptionNames()}
- )
- """ Below are re-implementations.
- """
- def getCheckedOptionNames(self):
- """ Should return a list with checked option names. This will
- be used to generate the label.
- @return: should return a list with strings.
- @rtype: list
- """
- return self._searchModel.engines
- def getOptions(self):
- """ Should return a list with options tuple(key, name, state)
- This will be used to generate the options.
- """
- list_ = []
- tmp = []
- for url, instance in self._instancesModel.items():
- for engine in instance.engines:
- if engine.name not in tmp:
- state = bool(engine.name in self._searchModel.engines)
- list_.append((engine.name, engine.name, state))
- tmp.append(engine.name)
- return sorted(list_)
- def optionToggled(self, key, state):
- if state:
- self._searchModel.engines.append(key)
- else:
- self._searchModel.engines.remove(key)
- self.updateFilter()
- class CategoryEditor(QDialog):
- def __init__(self, enginesModel, categoriesModel,
- userCategoriesModel, parent=None):
- QDialog.__init__(self, parent=parent)
- self._categoriesModel = categoriesModel
- self._userCategoriesModel = userCategoriesModel
- self.setWindowTitle(_("Category manager"))
- layout = QHBoxLayout(self)
- # Categories
- catLayout = QVBoxLayout()
- label = QLabel("<h2>{0}</h2>".format(_("Categories")), self)
- # Categories toolbuttons
- catToolLayout = QHBoxLayout()
- catAddButton = Button("+", self)
- self._catDelButton = Button("-", self)
- catToolLayout.addWidget(catAddButton, 0, Qt.AlignLeft)
- catToolLayout.addWidget(self._catDelButton, 1, Qt.AlignLeft)
- self._categoryListWidget = QListWidget(self)
- catLayout.addWidget(label)
- catLayout.addLayout(catToolLayout)
- catLayout.addWidget(self._categoryListWidget)
- # Engines
- engLayout = QVBoxLayout()
- label = QLabel("<h2>{0}</h2>".format(_("Engines")), self)
- # Engines filter
- filterLayout = QHBoxLayout()
- self._enginesCategoryFilterBox = QComboBox(self)
- self._enginesCategoryFilterBox.addItem(_("All"))
- for key, cat in categoriesModel.items():
- self._enginesCategoryFilterBox.addItem(cat.name)
- filterLayout.addWidget(
- QLabel(_("Category") + ":", self), 1, Qt.AlignRight
- )
- filterLayout.addWidget(
- self._enginesCategoryFilterBox, 0, Qt.AlignRight
- )
- # Engines table
- self._enginesTableView = QTableView(self)
- self._enginesTableView.setAlternatingRowColors(True)
- self._enginesTableView.setSelectionBehavior(
- QAbstractItemView.SelectRows
- )
- self._enginesTableView.setEditTriggers(
- QAbstractItemView.NoEditTriggers
- )
- self._enginesTableView.setSortingEnabled(True)
- self._enginesTableView.setHorizontalScrollMode(
- QAbstractItemView.ScrollPerPixel
- )
- header = self._enginesTableView.horizontalHeader()
- header.setSectionResizeMode(QHeaderView.ResizeToContents)
- header.setSectionsMovable(True)
- self._enginesTableModel = EnginesTableModel(enginesModel, self)
- self._enginesTableView.setModel(self._enginesTableModel)
- engLayout.addWidget(label)
- engLayout.addLayout(filterLayout)
- engLayout.addWidget(self._enginesTableView)
- layout.addLayout(catLayout)
- layout.addLayout(engLayout)
- # Connections
- catAddButton.clicked.connect(self.__addCategoryClicked)
- self._catDelButton.clicked.connect(self.__delCategoryClicked)
- self._categoryListWidget.currentRowChanged.connect(
- self.__currentUserCategoryChanged
- )
- self._enginesCategoryFilterBox.currentIndexChanged.connect(
- self.__enginesCategoryFilterChanged
- )
- self._enginesTableView.setEnabled(False)
- self._catDelButton.setEnabled(False)
- self.__addUserCategories()
- self.__selectFirst()
- def __currentUserCategoryChanged(self, index):
- if index < 0:
- self._enginesTableView.setEnabled(False)
- self._catDelButton.setEnabled(False)
- else:
- self._enginesTableView.setEnabled(True)
- self._catDelButton.setEnabled(True)
- if self._userCategoriesModel:
- key = list(self._userCategoriesModel.keys())[index]
- self._enginesTableModel.setUserModel(
- self._userCategoriesModel[key]
- )
- def __addUserCategories(self):
- for catKey, cat in self._userCategoriesModel.items():
- self._categoryListWidget.addItem(cat.name)
- def __selectFirst(self):
- if self._categoryListWidget.count():
- self._categoryListWidget.setCurrentRow(0)
- def __selectLast(self):
- self._categoryListWidget.setCurrentRow(
- self._categoryListWidget.count() - 1
- )
- def __reloadUserCategories(self):
- self._categoryListWidget.currentRowChanged.disconnect(
- self.__currentUserCategoryChanged
- )
- self._enginesTableModel.setUserModel(None)
- self._categoryListWidget.clear()
- self.__addUserCategories()
- self._categoryListWidget.currentRowChanged.connect(
- self.__currentUserCategoryChanged
- )
- def __addCategoryClicked(self, state):
- dialog = AddUserCategoryDialog(
- self._userCategoriesModel.keys()
- )
- if dialog.exec():
- self._userCategoriesModel.addCategory(
- dialog.name.lower(),
- dialog.name
- )
- self.__reloadUserCategories()
- self.__selectLast()
- def __delCategoryClicked(self, state):
- index = self._categoryListWidget.currentRow()
- key = list(self._userCategoriesModel.keys())[index]
- confirmDialog = QMessageBox()
- confirmDialog.setWindowTitle(
- _("Delete category")
- )
- confirmDialog.setText(
- _("Are you sure you want to delete the category `{0}`?")
- .format(self._userCategoriesModel[key].name)
- )
- confirmDialog.setStandardButtons(
- QMessageBox.Yes | QMessageBox.No
- )
- confirmDialog.button(QMessageBox.Yes).setText(_("Yes"))
- confirmDialog.button(QMessageBox.No).setText(_("No"))
- if confirmDialog.exec() != QMessageBox.Yes:
- return
- self._userCategoriesModel.removeCategory(key)
- self.__reloadUserCategories()
- self.__selectLast()
- self.__currentUserCategoryChanged(
- self._categoryListWidget.count() - 1
- )
- def __enginesCategoryFilterChanged(self, index):
- if not index: # All
- self._enginesTableModel.setCatFilter()
- else:
- key = list(self._categoriesModel.keys())[index-1]
- self._enginesTableModel.setCatFilter(key)
- class AddUserCategoryDialog(QDialog):
- def __init__(self, existingNames=[], text="", parent=None):
- QDialog.__init__(self, parent=parent)
- self._existingNames = existingNames
- layout = QFormLayout(self)
- label = QLabel(_("Name") + ":")
- self._nameEdit = QLineEdit(self)
- if text:
- self._nameEdit.setText(text)
- self._nameEdit.setPlaceholderText(text)
- else:
- self._nameEdit.setPlaceholderText(_("My category"))
- self._cancelButton = Button(_("Cancel"), self)
- self._addButton = Button(_("Add"), self)
- self._addButton.setEnabled(False)
- # Add stuff to layout
- layout.addRow(label, self._nameEdit)
- layout.addRow(self._cancelButton, self._addButton)
- # Connections
- self._nameEdit.textChanged.connect(self.__inputChanged)
- self._addButton.clicked.connect(self.accept)
- self._cancelButton.clicked.connect(self.reject)
- def __inputChanged(self, text):
- if self.isValid():
- self._addButton.setEnabled(True)
- else:
- self._addButton.setEnabled(False)
- def isValid(self):
- name = self._nameEdit.text().lower()
- if not name:
- return False
- for existingName in self._existingNames:
- if name == existingName.lower():
- return False
- return True
- @property
- def name(self):
- return self._nameEdit.text()
- class SearchCategories(CheckboxOptionsButton):
- def __init__(self, categoriesModel, enginesModel,
- userCategoriesModel, parent=None):
- """
- @param categoriesModel:
- @type categoriesModel: searxqt.models.search.CategoriesModel
- @param enginesModel:
- @type enginesModel: searxqt.models.instances.EnginesModel
- @param userCategoriesModel:
- @type userCategoriesModel: searxqt.models.search.UserCategoriesModel
- @param parent:
- @type parent: QObject or None
- """
- self._categoriesModel = categoriesModel
- self._enginesModel = enginesModel
- self._userCategoriesModel = userCategoriesModel
- CheckboxOptionsButton.__init__(
- self,
- labelName=_("Categories"),
- parent=parent
- )
- self._categoriesModel.dataChanged.connect(self.__categoryDataChanged)
- def __categoryDataChanged(self):
- # This happends after CategoriesModel.setData
- self.reGenerate()
- def __openUserCategoryEditor(self):
- window = CategoryEditor(
- self._enginesModel,
- self._categoriesModel,
- self._userCategoriesModel, self)
- window.exec()
- def __userCategoryToggled(self, key, state):
- if state:
- self._userCategoriesModel[key].check()
- else:
- self._userCategoriesModel[key].uncheck()
- self.reGenerate()
- """ Below are re-implementations.
- """
- def addCustomWidgetsTop(self, menu):
- menu.addSection(_("Custom"))
- action = QWidgetAction(menu)
- manageCustomButton = Button(_("Manage"), menu)
- action.setDefaultWidget(manageCustomButton)
- menu.addAction(action)
- for catKey, cat in self._userCategoriesModel.items():
- action = QWidgetAction(menu)
- widget = QCheckBox(cat.name, menu)
- widget.setTristate(False)
- widget.setChecked(
- cat.isChecked()
- )
- action.setDefaultWidget(widget)
- widget.stateChanged.connect(
- lambda state, key=catKey:
- self.__userCategoryToggled(key, state)
- )
- menu.addAction(action)
- menu.addSection(_("Default"))
- manageCustomButton.clicked.connect(self.__openUserCategoryEditor)
- def getCheckedOptionNames(self):
- """ Should return a list with checked option names. This will
- be used to generate the label.
- @return: should return a list with strings.
- @rtype: list
- """
- return(
- [
- self._categoriesModel[catKey].name
- for catKey in self._categoriesModel.checkedCategories()
- ] +
- [
- self._userCategoriesModel[catKey].name
- for catKey in self._userCategoriesModel.checkedCategories()
- ]
- )
- def getOptions(self):
- """ Should return a list with options tuple(key, name, state)
- This will be used to generate the options.
- """
- list_ = []
- for catKey in self._categoriesModel:
- list_.append(
- (
- catKey,
- self._categoriesModel[catKey].name,
- self._categoriesModel[catKey].isChecked()
- )
- )
- return list_
- def optionToggled(self, key, state):
- if state:
- self._categoriesModel[key].check()
- else:
- self._categoriesModel[key].uncheck()
- class SearchPeriod(QComboBox):
- def __init__(self, model, parent=None):
- QComboBox.__init__(self, parent=parent)
- self._model = model
- self.setMinimumContentsLength(2)
- for period in model.Periods:
- self.addItem(model.Periods[period], QVariant(period))
- self.currentIndexChanged.connect(self.__indexChanged)
- def __indexChanged(self, index):
- self._model.timeRange = self.currentData()
- class SearchLanguage(QComboBox):
- def __init__(self, model, parent=None):
- QComboBox.__init__(self, parent=parent)
- self._model = model
- self.setMinimumContentsLength(2)
- for lang in model.Languages:
- self.addItem(model.Languages[lang], QVariant(lang))
- self.currentIndexChanged.connect(self.__indexChanged)
- def __indexChanged(self, index):
- self._model.lang = self.currentData()
- class SearchOptionsContainer(QFrame):
- """ Custom QFrame to be able to show or hide certain widgets.
- """
- def __init__(self, searchModel, instancesModel, enginesModel, parent=None):
- """
- @param searchModel:
- @type searchModel: searxqt.models.search.SearchModel
- @param instancesModel:
- @type instancesModel: searxqt.models.instances.InstanceModelFilter
- @param enginesModel:
- @type enginesModel: searxqt.models.instances.EnginesModel
- @param parent:
- @type parent: QObject or None
- """
- QFrame.__init__(self, parent=parent)
- self._enginesModel = enginesModel
- self._searchModel = searchModel
- self._categoriesModel = CategoriesModel()
- self._userCategoriesModel = UserCategoriesModel()
- # Backup user checked engines.
- self.__userCheckedBackup = []
- # Keep track of disabled engines (these engines are disabled
- # because they are part of one or more checked categories).
- #
- # @key: engine name (str)
- # @value : list with category keys (str)
- self.__disabledByCat = {}
- layout = QHBoxLayout(self)
- self._widgets = {
- 'categories': SearchCategories(
- self._categoriesModel,
- enginesModel,
- self._userCategoriesModel,
- self
- ),
- 'engines': SearchEngines(searchModel, instancesModel, self),
- 'period': SearchPeriod(searchModel, self),
- 'lang': SearchLanguage(searchModel, self)
- }
- for widget in self._widgets.values():
- layout.addWidget(widget, 0, Qt.AlignTop)
- # Keep widgets left aligned.
- spacer = QSpacerItem(
- 40, 20, QSizePolicy.MinimumExpanding, QSizePolicy.Minimum
- )
- layout.addItem(spacer)
- # Connections
- self._categoriesModel.stateChanged.connect(
- self.__categoriesStateChanged
- )
- self._userCategoriesModel.stateChanged.connect(
- self.__userCategoriesStateChanged
- )
- self._userCategoriesModel.changed.connect(self.__userCategoriesChanged)
- self._userCategoriesModel.removed.connect(self.__userCategoryRemoved)
- self._enginesModel.changed.connect(self.__enginesModelChanged)
- def __enginesModelChanged(self):
- """Settings loaded or data updated
- """
- # Remove deleted engines from __disabledByCat
- for engine in list(self.__disabledByCat.keys()):
- if engine not in self._enginesModel:
- del self.__disabledByCat[engine]
- self._widgets['engines'].setKeyEnabled(engine)
- # Add new categories
- for catKey in self._enginesModel.categories():
- if catKey not in self._categoriesModel:
- name = ""
- if catKey in self._searchModel.categories.types:
- # Default pre-defined categories are translatable
- name = self._searchModel.categories.types[catKey][0]
- else:
- name = catKey.capitalize()
- print("[DEBUG] Found non default category `{0}`"
- .format(name))
- self._categoriesModel.addCategory(catKey, name)
- # Remove old categories
- for catKey in self._categoriesModel.copy():
- if catKey not in self._enginesModel.categories():
- self._categoriesModel.removeCategory(catKey)
- # Release potentialy checked engines
- self.__processCategoriesStateChange(
- [engine.name for engine in
- self._enginesModel.getByCategory(catKey)],
- catKey,
- False
- )
- self._widgets['categories'].reGenerate()
- self.__finalizeCategoriesStateChange()
- def __userCategoryRemoved(self, catKey):
- for engineKey, catList in self.__disabledByCat.copy().items():
- if catKey in catList:
- self.__uncheckEngineByCat(catKey, engineKey)
- self._widgets['categories'].reGenerate()
- self.__finalizeCategoriesStateChange()
- def __userCategoriesChanged(self, catKey):
- """ When the user edited a existing user-category this should
- check freshly added engines to this category and uncheck engines
- that have been removed from this category.
- """
- if self._userCategoriesModel[catKey].isChecked():
- engines = self._userCategoriesModel[catKey].engines
- # Uncheck removed engines
- for engineKey, categories in self.__disabledByCat.copy().items():
- if catKey in categories:
- if engineKey not in engines:
- self.__uncheckEngineByCat(catKey, engineKey)
- # Check newly added engines
- for engineKey in engines:
- if engineKey not in self.__disabledByCat:
- self.__checkEngineByCat(catKey, engineKey)
- self.__finalizeCategoriesStateChange()
- def __checkEngineByCat(self, catKey, engineKey):
- """ This method handles checking of a engine by a category.
- @param catKey: Category key
- @type catKey: str
- @param engineKey: Engine key
- @type engineKey: str
- """
- if engineKey not in self._searchModel.engines:
- # User did not check this engine so we are going to.
- self._searchModel.engines.append(
- engineKey
- )
- elif(engineKey not in self.__userCheckedBackup and
- not self._widgets['engines'].keyDisabled(engineKey)):
- # User did check this engine, so we backup that.
- self.__userCheckedBackup.append(engineKey)
- if not self._widgets['engines'].keyDisabled(engineKey):
- # Disable the engine from being toggled by the user.
- self._widgets['engines'].setKeyDisabled(engineKey)
- if engineKey not in self.__disabledByCat:
- self.__disabledByCat.update({engineKey: []})
- # Backup that this category is blocking this engine from
- # being toggled by the user.
- self.__disabledByCat[engineKey].append(catKey)
- def __uncheckEngineByCat(self, catKey, engineKey):
- """ This method handles the unchecking of a engine by a category.
- @param catKey: Category key
- @type catKey: str
- @param engineKey: Engine key
- @type engineKey: str
- """
- if engineKey in self.__disabledByCat:
- if catKey in self.__disabledByCat[engineKey]:
- # This category no longer blocks this engine from
- # being edited by the user.
- self.__disabledByCat[engineKey].remove(catKey)
- if not self.__disabledByCat[engineKey]:
- # No category left that blocks this engine from
- # user-toggleing.
- self._widgets['engines'].setKeyEnabled(engineKey)
- self.__disabledByCat.pop(engineKey)
- if engineKey not in self.__userCheckedBackup:
- # User didn't check this engine, so we can
- # uncheck it.
- self._searchModel.engines.remove(
- engineKey
- )
- else:
- # User did check this engine before checking
- # this category so we won't uncheck it.
- self.__userCheckedBackup.remove(engineKey)
- def __userCategoriesStateChanged(self, catKey, state):
- """ The user checked or unchecked a user-category.
- @param catKey: Category key
- @type catKey: str
- @param state:Category enabled or disabled (checked or unchecked)
- @type state: bool
- """
- self.__processCategoriesStateChange(
- self._userCategoriesModel[catKey].engines,
- catKey,
- state
- )
- self._widgets['categories'].reGenerate()
- self.__finalizeCategoriesStateChange()
- def __categoriesStateChanged(self, catKey, state):
- """ The user checked or unchecked a default-category.
- @param catKey: Category key
- @type catKey: str
- @param state: Category enabled or disabled (checked or unchecked)
- @type state: bool
- """
- self.__processCategoriesStateChange(
- [engine.name for engine in
- self._enginesModel.getByCategory(catKey)],
- catKey,
- state
- )
- self._widgets['categories'].reGenerate()
- self.__finalizeCategoriesStateChange()
- def __processCategoriesStateChange(self, engines, catKey, state):
- """ The user checked or unchecked a category, depending on the
- `state` variable.
- When a category gets checked all the engines in that category
- will be checked and disabled so that the user can't toggle the
- engine.
- On uncheck of a category all engines in that category should be
- re-enabled. And those engines should be unchecked if they weren't
- checked by the user before checking this category.
- @param engines: A list with engineKeys (str) that are part of the
- category (catKey).
- @type engines: list
- @param catKey: Category key
- @type catKey: str
- @param state: Category enabled or disabled (checked or unchecked)
- @type state: bool
- """
- if state:
- # Category checked.
- for engine in engines:
- self.__checkEngineByCat(catKey, engine)
- else:
- # Category unchecked.
- for engine in engines:
- self.__uncheckEngineByCat(catKey, engine)
- def __finalizeCategoriesStateChange(self):
- # Re-generate the engines label
- self._widgets['engines'].reGenerate()
- # Update the instances filter.
- self._widgets['engines'].updateFilter()
- def saveSettings(self):
- data = {}
- for key, widget in self._widgets.items():
- data.update(
- {
- '{0}Visible'.format(key): not widget.isHidden(),
- 'userCatModel': self._userCategoriesModel.data(),
- 'defaultCatModel': self._categoriesModel.data()
- }
- )
- return data
- def loadSettings(self, data):
- for key, widget in self._widgets.items():
- if data.get('{0}Visible'.format(key), True):
- widget.show()
- else:
- widget.hide()
- self._userCategoriesModel.setData(data.get('userCatModel', {}))
- self._categoriesModel.setData(data.get('defaultCatModel', {}))
- def __checkBoxStateChanged(self, key, state):
- if state:
- self._widgets[key].show()
- else:
- self._widgets[key].hide()
- """ QFrame re-implementations
- """
- def contextMenuEvent(self, event):
- menu = QMenu(self)
- menu.addSection(_("Show / Hide"))
- for key, widget in self._widgets.items():
- action = QWidgetAction(menu)
- checkbox = QCheckBox(key, menu)
- checkbox.setTristate(False)
- checkbox.setChecked(not widget.isHidden())
- action.setDefaultWidget(checkbox)
- checkbox.stateChanged.connect(
- lambda state, key=key:
- self.__checkBoxStateChanged(key, state)
- )
- menu.addAction(action)
- menu.exec_(self.mapToGlobal(event.pos()))
- class SearchContainer(QWidget):
- def __init__(self, searchModel, instancesModel,
- instanceSelecter, enginesModel, parent=None):
- """
- @type searchModel: models.search.SearchModel
- @type instancesModel: models.instances.InstancesModelFilter
- @type instanceSelecter: models.instances.InstanceSelecterModel
- @type enginesModel: models.instances.EnginesModel
- """
- QWidget.__init__(self, parent=parent)
- layout = QVBoxLayout(self)
- self._model = searchModel
- self._instancesModel = instancesModel
- self._instanceSelecter = instanceSelecter
- self._searchThread = None
- # Maximum other instances to try on fail.
- self._maxSearchFailCount = 10
- # Set `_useFallback` to True to try another instance when the
- # search failed somehow or set to False to try same instance or
- # for pagination which also should use the same instance.
- self._useFallback = True
- # `_fallbackActive` should be False when a fresh list of fallback
- # instances should be picked on failed search and should be True
- # when search(es) fail and `_fallbackInstancesQueue` is beeing
- # used.
- self._fallbackActive = False
- # Every first request that has `_useFallback` set to True will
- # use this list as a resource for next instance(s) to try until
- # it is out of instance url's. Also on the first request this
- # list will be cleared and filled again with `_maxSearchFailCount`
- # of random instances.
- self._fallbackInstancesQueue = []
- # Set to True to break out of the fallback loop.
- # This is used for the Stop action.
- self._breakFallback = False
- searchLayout = QHBoxLayout()
- layout.addLayout(searchLayout)
- # -- Start search bar
- self.queryEdit = QLineEdit(self)
- self.queryEdit.setPlaceholderText(_("Search for .."))
- searchLayout.addWidget(self.queryEdit)
- self.searchButton = Button(_("Searx"), self)
- self.searchButton.setToolTip(_("Preform search."))
- searchLayout.addWidget(self.searchButton)
- self.reloadButton = Button("♻", self)
- self.reloadButton.setToolTip(_("Reload"))
- searchLayout.addWidget(self.reloadButton)
- self.randomButton = Button("⤳", self)
- self.randomButton.setToolTip(_(
- "Search with random instance.\n"
- "(Obsolete when 'Random Every is checked')"
- ))
- searchLayout.addWidget(self.randomButton)
- rightLayout = QVBoxLayout()
- rightLayout.setSpacing(0)
- searchLayout.addLayout(rightLayout)
- self._fallbackCheck = QCheckBox(_("Fallback"), self)
- self._fallbackCheck.setToolTip(_("Try random other instance on fail."))
- rightLayout.addWidget(self._fallbackCheck)
- self._randomCheckEvery = QCheckBox(_("Random every"), self)
- self._randomCheckEvery.setToolTip(_("Pick a random instance for "
- "every request."))
- rightLayout.addWidget(self._randomCheckEvery)
- # -- End search bar
- # -- Start search options toolbar
- self._optionsContainer = SearchOptionsContainer(
- searchModel, instancesModel, enginesModel, self
- )
- layout.addWidget(self._optionsContainer)
- # -- End search options toolbar
- self.resultsContainer = QTextBrowser(self)
- self.resultsContainer.setOpenExternalLinks(True)
- self.resultsContainer.setLineWrapMode(1)
- layout.addWidget(self.resultsContainer)
- self.navBar = SearchNavigation(self)
- self.navBar.setEnabled(False)
- layout.addWidget(self.navBar)
- self.queryEdit.textChanged.connect(self.__queryChanged)
- self._model.statusChanged.connect(self.__searchStatusChanged)
- self._model.optionsChanged.connect(self.__searchOptionsChanged)
- self._fallbackCheck.stateChanged.connect(self.__useFallbackChanged)
- self._randomCheckEvery.stateChanged.connect(
- self.__randomEveryRequestChanged)
- self._instanceSelecter.instanceChanged.connect(
- self.__instanceChanged)
- self.queryEdit.returnPressed.connect(self.__searchButtonClicked)
- self.searchButton.clicked.connect(self.__searchButtonClicked)
- self.reloadButton.clicked.connect(self.__reloadButtonClicked)
- self.randomButton.clicked.connect(self.__randomSearchButtonClicked)
- self.navBar.requestPage.connect(self.__navBarRequest)
- self.__queryChanged("")
- def isBusy(self):
- return bool(self._searchThread is not None)
- def cancelAll(self):
- self._breakFallback = True
- self._searchThread.wait()
- def __searchButtonClicked(self, checked=0):
- # Set to use fallback
- self._useFallback = self._model.useFallback
- if (self._model.randomEvery or
- (self._useFallback and
- not self._instanceSelecter.currentUrl)):
- self._instanceSelecter.randomInstance()
- self._resetPagination()
- self._newSearch(self._instanceSelecter.currentUrl)
- def __stopButtonClicked(self):
- self._breakFallback = True
- self.searchButton.setEnabled(False)
- def __reloadButtonClicked(self):
- self._useFallback = False
- self._newSearch(self._instanceSelecter.currentUrl)
- def __randomSearchButtonClicked(self):
- self._useFallback = self._model.useFallback
- self._instanceSelecter.randomInstance()
- self._newSearch(self._instanceSelecter.currentUrl)
- def __navBarRequest(self, pageNo):
- self._useFallback = False
- self._model.pageno = pageNo
- self._newSearch(self._instanceSelecter.currentUrl)
- def _resetPagination(self):
- self.navBar.reset()
- self._model.pageno = 1
- def __instanceChanged(self):
- self._resetPagination()
- def __searchOptionsChanged(self):
- """ From the model (on load settings)
- """
- self._randomCheckEvery.stateChanged.disconnect(
- self.__randomEveryRequestChanged)
- self._fallbackCheck.stateChanged.disconnect(
- self.__useFallbackChanged)
- self._randomCheckEvery.setChecked(self._model.randomEvery)
- self._fallbackCheck.setChecked(self._model.useFallback)
- self.__handleRandomButtonState()
- self._randomCheckEvery.stateChanged.connect(
- self.__randomEveryRequestChanged)
- self._fallbackCheck.stateChanged.connect(self.__useFallbackChanged)
- def __handleRandomButtonState(self):
- """ Hides or shows the 'Random search button'.
- We don't need the button when the model it's randomEvery is True.
- """
- if self._model.randomEvery:
- self.randomButton.hide()
- else:
- self.randomButton.show()
- def __randomEveryRequestChanged(self, state):
- """ From the checkbox
- """
- self._model.optionsChanged.disconnect(self.__searchOptionsChanged)
- self._model.randomEvery = bool(state)
- self.__handleRandomButtonState()
- self._model.optionsChanged.connect(self.__searchOptionsChanged)
- def __useFallbackChanged(self, state):
- """ From the checkbox
- """
- self._model.optionsChanged.disconnect(self.__searchOptionsChanged)
- self._model.useFallback = bool(state)
- self._model.optionsChanged.connect(self.__searchOptionsChanged)
- def __queryChanged(self, q):
- if self._model.status() == SearchStatus.Busy:
- return
- if q:
- self.searchButton.setEnabled(True)
- self.reloadButton.setEnabled(True)
- self.randomButton.setEnabled(True)
- else:
- self.searchButton.setEnabled(False)
- self.reloadButton.setEnabled(False)
- self.randomButton.setEnabled(False)
- def _setOptionsState(self, state=True):
- if self._useFallback:
- if state:
- # Search stopped
- self.searchButton.setText(_("Search"))
- self.searchButton.clicked.disconnect(
- self.__stopButtonClicked
- )
- self.searchButton.clicked.connect(self.__searchButtonClicked)
- self.searchButton.setEnabled(True)
- else:
- # Searching
- self.searchButton.setText(_("Stop"))
- self.searchButton.clicked.disconnect(
- self.__searchButtonClicked
- )
- self.searchButton.clicked.connect(self.__stopButtonClicked)
- else:
- self.searchButton.setEnabled(state)
- self.reloadButton.setEnabled(state)
- self.randomButton.setEnabled(state)
- self._randomCheckEvery.setEnabled(state)
- self._fallbackCheck.setEnabled(state)
- self.queryEdit.setEnabled(state)
- self._optionsContainer.setEnabled(state)
- def __searchStatusChanged(self, status):
- if status == SearchStatus.Busy:
- self._setOptionsState(False)
- elif status == SearchStatus.Done:
- self._setOptionsState()
- @property
- def query(self): return self.queryEdit.text()
- def _newSearch(self, url, query=''):
- self.resultsContainer.clear()
- self._search(url, query)
- def _search(self, url, query=''):
- if self._searchThread:
- return
- if not query:
- query = self.query
- if not query:
- self.resultsContainer.setHtml(_("Please enter a search query."))
- return
- if not url:
- self.resultsContainer.setHtml(_("Please select a instance first."))
- return
- self.navBar.setEnabled(False)
- self._model.url = url
- self._model.query = query
- self._searchThread = Thread(
- self._model.search,
- args=[self._model],
- parent=self
- )
- self._searchThread.finished.connect(self._searchFinished)
- self._searchThread.start()
- def _searchFailed(self, result):
- if self._useFallback: # Re-try another instance
- if self._breakFallback: # Stop button pressed
- self._breakFallback = False
- return
- if not self._fallbackActive:
- # Get new list with instances to try same request.
- self._fallbackActive = True
- self._fallbackInstancesQueue.clear()
- self._fallbackInstancesQueue = (
- self._instanceSelecter.getRandomInstances(
- amount=self._maxSearchFailCount))
- if not self._fallbackInstancesQueue:
- self.resultsContainer.setHtml(
- "{0} ({1})".format(
- _("Max fail count reached!"),
- self._maxSearchFailCount))
- self._fallbackActive = False
- return
- # Set next instance url to try.
- self._instanceSelecter.currentUrl = (
- self._fallbackInstancesQueue.pop(0))
- self._search(self._instanceSelecter.currentUrl)
- return
- if self._model.pageno > 1:
- self.navBar.setEnabled(True)
- self.navBar.setNextEnabled(False)
- self.resultsContainer.setHtml("{0}: {1} {2}"
- .format(_("Search failed"),
- result.errorType(),
- result.error()))
- def _searchFinished(self):
- result = self._searchThread.result()
- self._clearSearchThread()
- if not bool(result): # Failed
- self._searchFailed(result)
- return
- self._fallbackActive = False
- # Create HTML from results
- elemStr = ""
- for item in result.json().get('results', {}):
- elemStr += self.createResultElement(item)
- self.resultsContainer.setHtml(self.compileHtml(elemStr))
- self.navBar.setEnabled(True)
- self.navBar.setNextEnabled(True)
- def _clearSearchThread(self):
- self._searchThread.finished.disconnect(self._searchFinished)
- # Wait before deleting because the `finished` signal is emited
- # from the thread itself, so this method could be called before the
- # thread is actually finished and result in a crash.
- self._searchThread.wait()
- self._searchThread.deleteLater()
- self._searchThread = None
- def createResultElement(self, data):
- # Create general elements
- elem = """<div class="result" id="result-{id}">
- <h4 class="result-title"><i>{engine}: </i><a href="{url}">{title}</a></h4>
- <div style="margin-left: 10px;">
- <p class="result-description">{content}</p>
- <p class="result-url">{url}</p>
- """.format(id="TODO-id", title=data.get('title', ''),
- url=data.get('url', ''), content=data.get('content', ''),
- engine=data.get('engine', '?'))
- # Add file data elements
- elem += self.htmlFileSection(data)
- elem += "</div></div>"
- return elem
- def compileHtml(self, elemStr=""):
- return """<html>
- <head>
- <title>Yeah</title>
- </head>
- <body>
- {0}
- </body>
- </html>""".format(elemStr)
- def formatFileSize(self, fileSize):
- sizeStr = ""
- _TiB = 1024 ** 4
- _GiB = 1024 ** 3
- _MiB = 1024 ** 2
- _KiB = 1024
- if fileSize > _TiB: # TiB
- sizeStr = "{0:.2f} TB".format(fileSize / _TiB)
- elif fileSize > _GiB: # GiB
- sizeStr = "{0:.2f} GB".format(fileSize / _GiB)
- elif fileSize > _MiB: # MiB
- sizeStr = "{0:.2f} MB".format(fileSize / _MiB)
- elif fileSize > _KiB: # KiB
- sizeStr = "{0:.2f} KB".format(fileSize / _KiB)
- else:
- sizeStr = "{0} Bytes".format(fileSize)
- return sizeStr
- def formatFileCount(self, fileCount):
- if fileCount == 1:
- return "1" + _("file")
- return "{0} {1}".format(fileCount, _("files"))
- def htmlFileSection(self, data):
- elem = ""
- if data.get('magnetlink', ''):
- elem += "<a href=\"{0}\">Magnet</a> ".format(data['magnetlink'])
- if data.get('torrentfile', ''):
- elem += "<a href=\"{0}\">Torrent</a> ".format(data['torrentfile'])
- if data.get('filesize', 0):
- elem += self.formatFileSize(data['filesize']) + " "
- if data.get('files', 0):
- elem += self.formatFileCount(data['files']) + " "
- if data.get('seed', None) is not None:
- elem += "seeders: {0} ".format(data['seed'])
- if data.get('leech', None) is not None:
- elem += "leechers: {0} ".format(data['leech'])
- if elem:
- elem = "<p class=\"result-file\">" + elem + "</p>"
- return elem
- def saveSettings(self):
- return {
- 'searchOptions': self._optionsContainer.saveSettings()
- }
- def loadSettings(self, data):
- self._optionsContainer.loadSettings(
- data.get('searchOptions', {})
- )
|