instances.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 PyQt5.QtWidgets import (
  22. QWidget,
  23. QVBoxLayout,
  24. QLabel,
  25. QTableView,
  26. QHeaderView,
  27. QAbstractItemView,
  28. QHBoxLayout,
  29. QFormLayout,
  30. QCheckBox,
  31. QMessageBox,
  32. QMenu,
  33. QComboBox,
  34. QSizePolicy,
  35. QScrollArea,
  36. QSplitter,
  37. QFrame,
  38. QWidgetAction
  39. )
  40. from PyQt5.QtCore import pyqtSignal, Qt, pyqtProperty
  41. from PyQt5.QtGui import QGuiApplication, QPainter, QFontMetrics, QColor
  42. from searxqt.core.handler import NetworkTypes # for network type filtering.
  43. from searxqt.core.requests import ErrorType
  44. from searxqt.widgets.buttons import Button
  45. from searxqt.widgets.dialogs import UrlDialog, RequestsErrorMessage
  46. from searxqt.models.instances import (
  47. InstanceTableModel,
  48. InstancesModelTypes
  49. )
  50. from searxqt.translations import _, timeToString
  51. class AddUserInstanceDialog(UrlDialog):
  52. def __init__(self, url='', parent=None):
  53. UrlDialog.__init__(self, url=url, acceptTxt=_("Add"), parent=parent)
  54. layout = self.layout()
  55. label = QLabel("Update data on add:")
  56. self._updateCheckBox = QCheckBox("", self)
  57. layout.insertRow(2, label, self._updateCheckBox)
  58. def updateOnAdd(self):
  59. return bool(self._updateCheckBox.isChecked())
  60. class InstancesView(QWidget):
  61. def __init__(self, filterModel, instanceSelecter, parent=None):
  62. """
  63. @type model: searxqt.models.instances.InstanceModelFilter
  64. @type instanceSelecter: searxqt.models.instances.InstanceSelecterModel
  65. @type parent: QObject
  66. """
  67. QWidget.__init__(self, parent=parent)
  68. layout = QVBoxLayout(self)
  69. layout.setContentsMargins(0, 0, 0, 0)
  70. layout.setSpacing(0)
  71. self._filterModel = filterModel
  72. self._instanceSelecter = instanceSelecter
  73. self._tableModel = InstanceTableModel(filterModel, self)
  74. # Diffrent types can have diffrent attibutes.
  75. self._currentInstancesType = filterModel.parentModel().Type
  76. headLabel = QLabel("<h2>{0}</h2>".format(_("Instances")), self)
  77. layout.addWidget(headLabel)
  78. # Splitter
  79. self._splitter = QSplitter(self)
  80. self._splitter.setOrientation(Qt.Vertical)
  81. layout.addWidget(self._splitter)
  82. # Filter scroll area
  83. self._scrollArea = QScrollArea(self._splitter)
  84. self._scrollContentWidget = QWidget(self._scrollArea)
  85. scrollLayout = QVBoxLayout(self._scrollContentWidget)
  86. self.filterWidget = FilterWidget(
  87. filterModel,
  88. self._scrollContentWidget
  89. )
  90. self._scrollArea.setWidget(self._scrollContentWidget)
  91. self._scrollArea.setFrameShape(QFrame.NoFrame)
  92. self._scrollArea.setWidgetResizable(True)
  93. self._scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  94. self._scrollArea.setAlignment(
  95. Qt.AlignLeading | Qt.AlignLeft | Qt.AlignTop
  96. )
  97. scrollLayout.addWidget(self.filterWidget, 0, Qt.AlignTop)
  98. # Bottom widgets
  99. bottomWidget = QWidget(self._splitter)
  100. bottomLayout = QVBoxLayout(bottomWidget)
  101. self.statsWidget = InstancesStatsWidget(filterModel, bottomWidget)
  102. bottomLayout.addWidget(self.statsWidget)
  103. self._typeDependantButton = Button("", bottomWidget)
  104. bottomLayout.addWidget(self._typeDependantButton)
  105. self._tableView = InstanceTableView(
  106. filterModel,
  107. self._tableModel,
  108. self
  109. )
  110. bottomLayout.addWidget(self._tableView)
  111. # Connections
  112. self._tableView.selectionModel().selectionChanged.connect(
  113. self.__selectionChanged)
  114. self._tableModel.layoutChanged.connect(self.__tableModelLayoutChanged)
  115. self._instanceSelecter.instanceChanged.connect(self.__instanceChanged)
  116. filterModel.parentModel().typeChanged.connect(
  117. self.__instancesModelTypeChanged
  118. )
  119. """ InstancesType dependant methods below
  120. """
  121. def __instancesModelTypeChanged(self, type_):
  122. """ PersistentInstancesModel emitted changed.
  123. """
  124. previousType = self._currentInstancesType
  125. # Disconnect signals
  126. if previousType == InstancesModelTypes.Stats2:
  127. self._typeDependantButton.clicked.disconnect(self.__updateClicked)
  128. elif previousType == InstancesModelTypes.User:
  129. self._typeDependantButton.clicked.disconnect(self.__addClicked)
  130. if type_ == InstancesModelTypes.Stats2:
  131. self._typeDependantButton.setText(_("Update"))
  132. self._typeDependantButton.clicked.connect(self.__updateClicked)
  133. elif type_ == InstancesModelTypes.User:
  134. self._typeDependantButton.setText(_("Add Instance"))
  135. self._typeDependantButton.clicked.connect(self.__addClicked)
  136. self._currentInstancesType = type_
  137. # InstancesModelTypes.Stats2
  138. def __updateClicked(self, checked):
  139. self._typeDependantButton.setEnabled(False)
  140. handler = self._filterModel.parentModel().handler()
  141. if handler.updateInstances(): # True when thread has started.
  142. handler.updateFinished.connect(self.__updateFinished)
  143. def __updateFinished(self, errorType, errorMsg):
  144. handler = self._filterModel.parentModel().handler()
  145. handler.updateFinished.disconnect(self.__updateFinished)
  146. if errorType != ErrorType.Success:
  147. dialog = RequestsErrorMessage(errorType, errorMsg)
  148. dialog.exec()
  149. self._typeDependantButton.setEnabled(True)
  150. # InstancesModelTypes.User
  151. def __addClicked(self, checked):
  152. dialog = AddUserInstanceDialog()
  153. if dialog.exec():
  154. handler = self._filterModel.parentModel().handler()
  155. handler.addInstance(dialog.url)
  156. if dialog.updateOnAdd():
  157. handler.updateInstance(dialog.url)
  158. """ -----------------------------------------------------------
  159. """
  160. def selectUrl(self, url):
  161. self._tableView.selectionModel().selectionChanged.disconnect(
  162. self.__selectionChanged)
  163. # Backup horizontal scroll value
  164. horScrollVal = self._tableView.horizontalScrollBar().value()
  165. if url in self._filterModel:
  166. rowIndex = self._tableModel.getByUrl(url)
  167. modelIndex = self._tableModel.index(rowIndex, 0)
  168. self._tableView.setCurrentIndex(modelIndex)
  169. self._tableView.scrollTo(
  170. modelIndex,
  171. QAbstractItemView.PositionAtCenter
  172. )
  173. else:
  174. # No URL set so clear the selection.
  175. self._tableView.clearSelection()
  176. # Restore horizontal scroll value
  177. self._tableView.horizontalScrollBar().setValue(horScrollVal)
  178. self._tableView.selectionModel().selectionChanged.connect(
  179. self.__selectionChanged)
  180. def __instanceChanged(self, url):
  181. """ Select and scroll to table item with passed url.
  182. @param url: Instance url to select and scroll to.
  183. @type url: str
  184. """
  185. if url in self._filterModel:
  186. rowIndex = self._tableModel.getByUrl(url)
  187. modelIndex = self._tableModel.index(rowIndex, 0)
  188. self._tableView.setCurrentIndex(modelIndex)
  189. self._tableView.scrollTo(
  190. modelIndex,
  191. QAbstractItemView.PositionAtCenter
  192. )
  193. def __tableModelLayoutChanged(self):
  194. """ Re-select the current URL when the layout changed.
  195. """
  196. self.selectUrl(self._instanceSelecter.currentUrl)
  197. def __selectionChanged(self):
  198. """ Let the InstanceSelecter know that the current instance has
  199. changed. (Selected by the user.)
  200. """
  201. self._instanceSelecter.instanceChanged.disconnect(
  202. self.__instanceChanged)
  203. selectionModel = self._tableView.selectionModel()
  204. if selectionModel.hasSelection():
  205. rows = selectionModel.selectedRows()
  206. selectedIndex = rows[0].row()
  207. self._instanceSelecter.currentUrl = (
  208. self._tableModel.getByIndex(selectedIndex)
  209. )
  210. else:
  211. self._instanceSelecter.currentUrl = ""
  212. self._instanceSelecter.instanceChanged.connect(self.__instanceChanged)
  213. def saveSettings(self):
  214. header = self._tableView.horizontalHeader()
  215. data = {
  216. 'hiddenColumnIndexes': self._tableView.getHiddenColumnIndexes(),
  217. 'headerState': header.saveState(),
  218. 'splitterState': self._splitter.saveState()
  219. }
  220. return data
  221. def loadSettings(self, data):
  222. self._tableView.setHiddenColumnIndexes(
  223. data.get('hiddenColumnIndexes', [])
  224. )
  225. if 'headerState' in data:
  226. header = self._tableView.horizontalHeader()
  227. header.restoreState(data['headerState'])
  228. if 'splitterState' in data:
  229. self._splitter.restoreState(data['splitterState'])
  230. class InstanceTableView(QTableView):
  231. def __init__(self, filterModel, tableModel=None, parent=None):
  232. """
  233. @type filterModel: searxqt.models.instances.InstanceModelFilter
  234. @type tableModel: searxqt.models.instances.InstanceTableModel
  235. """
  236. QTableView.__init__(self, parent=parent)
  237. self._filterModel = filterModel
  238. if tableModel:
  239. self.setModel(tableModel)
  240. self.setAlternatingRowColors(True)
  241. self.setSelectionBehavior(QAbstractItemView.SelectRows)
  242. self.setEditTriggers(QAbstractItemView.NoEditTriggers)
  243. self.setSortingEnabled(True)
  244. self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
  245. # Horizontal header
  246. header = self.horizontalHeader()
  247. header.setSectionResizeMode(QHeaderView.ResizeToContents)
  248. header.setSectionsMovable(True)
  249. def _hideColumn(self, index, state):
  250. state = state == 0
  251. self.setColumnHidden(index, state)
  252. def getHiddenColumnIndexes(self):
  253. columnIndex = 0
  254. indexes = []
  255. for columnData in self.model().getColumns():
  256. # Set current value
  257. if self.isColumnHidden(columnIndex):
  258. indexes.append(columnIndex)
  259. columnIndex += 1
  260. return indexes
  261. def setHiddenColumnIndexes(self, indexes):
  262. for index in indexes:
  263. self._hideColumn(index, 0)
  264. """ Re-implementations
  265. """
  266. def contextMenuEvent(self, event):
  267. def __removeFromWhitelist(self, url):
  268. # Remove instance url from whitelist without applying the filter.
  269. self._filterModel.delInstanceFromWhitelist(url)
  270. selectionModel = self.selectionModel()
  271. if selectionModel.hasSelection():
  272. menu = QMenu(self)
  273. # Add to blacklist
  274. addToBlacklistAction = menu.addAction(_("Add to blacklist"))
  275. # Add to temporary blacklist
  276. addToTmpBlacklistAction = menu.addAction(_("Temporary blacklist"))
  277. # Add to whitelist
  278. addToWhitelistAction = menu.addAction(_("Add to whitelist"))
  279. # Copy URL
  280. copyUrlAction = menu.addAction(_("Copy URL"))
  281. menu.addSeparator()
  282. # Select all
  283. selectAllAction = menu.addAction(_("Select All"))
  284. menu.addSeparator()
  285. # Column options
  286. columnsMenu = QMenu(_("Columns"), self)
  287. menu.addMenu(columnsMenu)
  288. # User instances specific
  289. userInstances = False
  290. handler = self._filterModel.parentModel().handler()
  291. if (
  292. self._filterModel.parentModel().Type
  293. == InstancesModelTypes.User
  294. ):
  295. userInstances = True
  296. menu.addSeparator()
  297. delUserInstanceAction = menu.addAction(_("Remove selected"))
  298. updateUserInstanceAction = menu.addAction(_("Update selected"))
  299. if handler.hasActiveJobs():
  300. delUserInstanceAction.setEnabled(False)
  301. columnIndex = 0
  302. for columnData in self.model().getColumns():
  303. action = QWidgetAction(columnsMenu)
  304. widget = QCheckBox(columnData.name, columnsMenu)
  305. widget.setTristate(False)
  306. action.setDefaultWidget(widget)
  307. # Set current value
  308. if not self.isColumnHidden(columnIndex):
  309. widget.setChecked(True)
  310. columnsMenu.addAction(action)
  311. widget.stateChanged.connect(
  312. lambda state, index=columnIndex:
  313. self._hideColumn(index, state)
  314. )
  315. columnIndex += 1
  316. action = menu.exec_(self.mapToGlobal(event.pos()))
  317. if action == addToBlacklistAction:
  318. # Blacklist
  319. indexes = self.selectionModel().selectedRows()
  320. forAll = None
  321. for index in indexes:
  322. url = self.model().getByIndex(index.row())
  323. if url in self._filterModel.whitelist:
  324. # Url is whitelisted, what to do?
  325. if forAll == QMessageBox.YesRole:
  326. __removeFromWhitelist(self, url)
  327. elif forAll == QMessageBox.NoRole:
  328. continue
  329. else:
  330. questionBox = QMessageBox(self)
  331. questionBox.addButton(
  332. _("Yes"), QMessageBox.YesRole
  333. )
  334. questionBox.addButton(
  335. _("No"), QMessageBox.NoRole
  336. )
  337. questionBox.addButton(
  338. _("Cancel"), QMessageBox.RejectRole
  339. )
  340. questionBox.setWindowTitle("Url whitelisted")
  341. questionBox.setText(_(
  342. "{0} found in the <b>whitelist</b>. Would you"
  343. " like to <b>remove</b> it from the"
  344. " <b>whitelist</b> and add it to the"
  345. " <b>blacklist</b>?").format(url)
  346. )
  347. checkBox = QCheckBox(
  348. _("Remember for all"), questionBox
  349. )
  350. questionBox.setCheckBox(checkBox)
  351. questionBox.exec()
  352. answer = questionBox.result()
  353. if answer == 0: # Yes
  354. # Remove url from whitelist and add it to
  355. # the blacklist
  356. __removeFromWhitelist(self, url)
  357. if checkBox.isChecked():
  358. forAll = QMessageBox.YesRole
  359. elif answer == 1 and checkBox.isChecked():
  360. forAll = QMessageBox.NoRole
  361. continue
  362. else:
  363. continue
  364. self._filterModel.putInstanceOnBlacklist(
  365. url, reason=_("Manual")
  366. )
  367. self._filterModel.apply()
  368. elif action == addToTmpBlacklistAction:
  369. index = selectionModel.selectedRows()[0]
  370. url = self.model().getByIndex(index.row())
  371. self._filterModel.putInstanceOnTimeout(url, reason=_("Manual"))
  372. self._filterModel.apply()
  373. elif action == addToWhitelistAction:
  374. # Whitelist
  375. indexes = self.selectionModel().selectedRows()
  376. for index in indexes:
  377. url = self.model().getByIndex(index.row())
  378. if url not in self._filterModel.whitelist:
  379. self._filterModel.putInstanceOnWhitelist(url)
  380. self._filterModel.apply()
  381. elif action == copyUrlAction:
  382. index = selectionModel.selectedRows()[0]
  383. url = self.model().getByIndex(index.row())
  384. clipboard = QGuiApplication.clipboard()
  385. clipboard.setText(url)
  386. elif action == selectAllAction:
  387. self.selectAll()
  388. # User instances actions
  389. if userInstances:
  390. if action == delUserInstanceAction:
  391. indexes = self.selectionModel().selectedRows()
  392. # Confirmation
  393. instancesStr = ""
  394. for index in indexes:
  395. url = self.model().getByIndex(index.row())
  396. instancesStr = "{0}\n{1}".format(instancesStr, url)
  397. confirmDialog = QMessageBox()
  398. confirmDialog.setWindowTitle(
  399. _("Delete instances")
  400. )
  401. confirmDialog.setText(
  402. _("Are you sure you want to delete the following"
  403. " instances?\n") + instancesStr
  404. )
  405. confirmDialog.setStandardButtons(
  406. QMessageBox.Yes | QMessageBox.No
  407. )
  408. confirmDialog.button(QMessageBox.Yes).setText(_("Yes"))
  409. confirmDialog.button(QMessageBox.No).setText(_("No"))
  410. if confirmDialog.exec() != QMessageBox.Yes:
  411. return
  412. toRemove = []
  413. for index in indexes:
  414. toRemove.append(self.model().getByIndex(index.row()))
  415. handler.removeMultiInstances(toRemove)
  416. elif action == updateUserInstanceAction:
  417. indexes = self.selectionModel().selectedRows()
  418. handler = self._filterModel.parentModel().handler()
  419. for index in indexes:
  420. handler.updateInstance(
  421. self.model().getByIndex(index.row())
  422. )
  423. class UrlFilterLabel(QLabel):
  424. """ Fancy QLabel that eludes text and has a hover color property.
  425. """
  426. def __init__(self, text, parent):
  427. QLabel.__init__(self, text, parent=parent)
  428. self.__hover = False
  429. self.__hoverColor = QColor()
  430. self.setMinimumWidth(100)
  431. @pyqtProperty(QColor)
  432. def hoverColor(self):
  433. return self.__hoverColor
  434. @hoverColor.setter
  435. def hoverColor(self, color):
  436. self.__hoverColor = color
  437. def enterEvent(self, event):
  438. self.__hover = True
  439. self.update()
  440. def leaveEvent(self, event):
  441. self.__hover = False
  442. self.update()
  443. def paintEvent(self, event):
  444. painter = QPainter(self)
  445. if self.__hover and self.hoverColor.isValid():
  446. pen = painter.pen()
  447. pen.setColor(self.hoverColor)
  448. painter.setPen(pen)
  449. metrics = QFontMetrics(self.font())
  450. elided = metrics.elidedText(self.text(), Qt.ElideRight, self.width())
  451. painter.drawText(self.rect(), self.alignment(), elided)
  452. class UrlFilterItem(QWidget):
  453. deleted = pyqtSignal(str) # url
  454. def __init__(self, url, parent=None):
  455. QWidget.__init__(self, parent=parent)
  456. layout = QHBoxLayout(self)
  457. layout.setContentsMargins(0, 0, 0, 0)
  458. self._urlStr = url
  459. label = UrlFilterLabel(url, self)
  460. delButton = Button("X", self)
  461. delButton.setSizePolicy(
  462. QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed)
  463. )
  464. layout.addWidget(label, 0, Qt.AlignTop)
  465. layout.addWidget(delButton, 0, Qt.AlignTop)
  466. delButton.clicked.connect(self.__delMe)
  467. def str(self): return self._urlStr
  468. def __delMe(self):
  469. self.deleted.emit(self.str())
  470. self.deleteLater()
  471. class UrlFilterWidget(QWidget):
  472. def __init__(self, model, parent=None):
  473. QWidget.__init__(self, parent=parent)
  474. self._model = model
  475. self._items = {}
  476. QVBoxLayout(self)
  477. self.layout().setContentsMargins(0, 0, 0, 0)
  478. self.layout().setSpacing(0)
  479. self._model.changed.connect(self.__modelChanged)
  480. # -- Override below in subclass.
  481. def filterItems(self):
  482. return []
  483. def delete(self, url):
  484. self._delete(url)
  485. def itemAdded(self, url):
  486. """ Callback from when a item has been added, usefull for subclasses
  487. to do something; for example set a tooltip.
  488. """
  489. pass
  490. # ---
  491. def __modelChanged(self):
  492. # Remove old
  493. for url in list(self._items.keys()):
  494. if url not in self.filterItems():
  495. self._items[url].deleteLater()
  496. del self._items[url]
  497. # Add new
  498. for url in self.filterItems():
  499. if url not in self._items:
  500. self._add(url)
  501. def _add(self, url):
  502. item = UrlFilterItem(url, self)
  503. self._items.update({url: item})
  504. item.deleted.connect(self.delete)
  505. self.layout().addWidget(item)
  506. self.itemAdded(url)
  507. def _delete(self, url):
  508. del self._items[url]
  509. class WhitelistFilterWidget(UrlFilterWidget):
  510. def filterItems(self):
  511. return self._model.whitelist
  512. def delete(self, url):
  513. UrlFilterWidget.delete(self, url)
  514. self._model.delInstanceFromWhitelist(url)
  515. self._model.apply()
  516. class BlacklistFilterWidget(UrlFilterWidget):
  517. def filterItems(self):
  518. return self._model.blacklist
  519. def itemAdded(self, url):
  520. filterItems = self.filterItems()
  521. dateStr = timeToString(filterItems[url][0] * 60)
  522. reasonStr = filterItems[url][1]
  523. tooltipStr = ("<b>{dateLabel}</b>: {dateStr}\n"
  524. "<b>{reasonLabel}</b>: {reasonStr}"
  525. ).format(
  526. dateLabel=_("Date"),
  527. dateStr=dateStr,
  528. reasonLabel=_("Reason"),
  529. reasonStr=reasonStr
  530. )
  531. self._items[url].setToolTip(tooltipStr)
  532. def delete(self, url):
  533. UrlFilterWidget.delete(self, url)
  534. self._model.delInstanceFromBlacklist(url)
  535. self._model.apply()
  536. class TimeoutFilterWidget(UrlFilterWidget):
  537. def filterItems(self):
  538. return self._model.timeoutList
  539. def itemAdded(self, url):
  540. # Add reason and duration tooltip
  541. durationStr = _("Until restart or manual removal.")
  542. filterItems = self.filterItems()
  543. timer = filterItems[url][0]
  544. if timer:
  545. durationStr = str(timer.interval() / 60000) + "min"
  546. tooltipStr = ("<b>{reasonLabel}</b>: \n{reason}\n<b>{durationLabel}"
  547. "</b>: {duration}".format(
  548. reasonLabel = _("Reason"),
  549. durationLabel = _("Duration"),
  550. reason = filterItems[url][1],
  551. duration = durationStr
  552. ))
  553. self._items[url].setToolTip(tooltipStr)
  554. def delete(self, url):
  555. UrlFilterWidget.delete(self, url)
  556. self._model.delInstanceFromTimeout(url)
  557. self._model.apply()
  558. class NetworkFilterWidget(QWidget):
  559. def __init__(self, model, parent=None):
  560. """
  561. @type model: InstancesModel
  562. """
  563. QWidget.__init__(self, parent=parent)
  564. self._model = model
  565. layout = QHBoxLayout(self)
  566. layout.setContentsMargins(0, 0, 0, 0)
  567. layout.setSpacing(0)
  568. self._webBox = QCheckBox("Web", self)
  569. self._torBox = QCheckBox("Tor", self)
  570. self._i2pBox = QCheckBox("i2p", self)
  571. self._webBox.setSizePolicy(
  572. QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed)
  573. )
  574. self._torBox.setSizePolicy(
  575. QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed)
  576. )
  577. self._i2pBox.setSizePolicy(
  578. QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed)
  579. )
  580. layout.addWidget(self._webBox, 0, Qt.AlignLeft)
  581. layout.addWidget(self._torBox, 0, Qt.AlignLeft)
  582. layout.addWidget(self._i2pBox, 1, Qt.AlignLeft)
  583. self._model.changed.connect(self._modelChanged)
  584. self._webBox.stateChanged.connect(self.__changed)
  585. self._torBox.stateChanged.connect(self.__changed)
  586. self._i2pBox.stateChanged.connect(self.__changed)
  587. def __changed(self, state):
  588. self._model.changed.disconnect(self._modelChanged)
  589. self._model.updateKwargs(self.data())
  590. self._model.changed.connect(self._modelChanged)
  591. def _modelChanged(self):
  592. self._webBox.stateChanged.disconnect(self.__changed)
  593. self._torBox.stateChanged.disconnect(self.__changed)
  594. self._i2pBox.stateChanged.disconnect(self.__changed)
  595. self._webBox.setChecked(False)
  596. self._torBox.setChecked(False)
  597. self._i2pBox.setChecked(False)
  598. filter = self._model.filter()
  599. if NetworkTypes.Web in filter['networkTypes']:
  600. self._webBox.setChecked(True)
  601. if NetworkTypes.Tor in filter['networkTypes']:
  602. self._torBox.setChecked(True)
  603. if NetworkTypes.I2P in filter['networkTypes']:
  604. self._i2pBox.setChecked(True)
  605. self._webBox.stateChanged.connect(self.__changed)
  606. self._torBox.stateChanged.connect(self.__changed)
  607. self._i2pBox.stateChanged.connect(self.__changed)
  608. def data(self):
  609. result = {
  610. 'networkTypes': [] # Network Types to pass
  611. }
  612. if self._webBox.isChecked():
  613. result['networkTypes'].append(NetworkTypes.Web)
  614. if self._torBox.isChecked():
  615. result['networkTypes'].append(NetworkTypes.Tor)
  616. if self._i2pBox.isChecked():
  617. result['networkTypes'].append(NetworkTypes.I2P)
  618. return result
  619. class VersionFilter(QWidget):
  620. def __init__(self, model, parent=None):
  621. """
  622. @type model: InstanceModelFilter
  623. """
  624. QWidget.__init__(self, parent=parent)
  625. self._model = model
  626. self._items = {}
  627. layout = QVBoxLayout(self)
  628. layout.setContentsMargins(0, 0, 0, 0)
  629. layout.setSpacing(0)
  630. self.minVersionCombo = QComboBox(self)
  631. self.gitVersionCheck = QCheckBox(_("Development"), self)
  632. self.gitVersionCheck.setToolTip(
  633. _("Include development versions of searx (Git versions) when "
  634. "checked.")
  635. )
  636. # Disable by default since there isn't a version selected by default.
  637. self.gitVersionCheck.setEnabled(False)
  638. self.unknownSuffixVersionCheck = QCheckBox(_("Unknown git"), self)
  639. self.unknownSuffixVersionCheck.setToolTip(
  640. _("Include git versions with a unknown commit hash when checked.")
  641. )
  642. # Disable by default since there isn't a version selected by default.
  643. self.unknownSuffixVersionCheck.setEnabled(False)
  644. # Add widgets to layout
  645. layout.addWidget(self.minVersionCombo)
  646. layout.addWidget(self.gitVersionCheck)
  647. layout.addWidget(self.unknownSuffixVersionCheck)
  648. # Signal connections
  649. self._model.changed.connect(self.__modelChanged)
  650. self.minVersionCombo.currentTextChanged.connect(
  651. self._minVersionComboChanged
  652. )
  653. self.gitVersionCheck.stateChanged.connect(self._optionCheckBoxChanged)
  654. self.unknownSuffixVersionCheck.stateChanged.connect(
  655. self._optionCheckBoxChanged
  656. )
  657. def _minVersionComboChanged(self, versionStr):
  658. """ From the ComboBox
  659. """
  660. self._model.changed.disconnect(self.__modelChanged)
  661. self._model.updateKwargs(self.data()) # update the model.
  662. self._minVersionChanged()
  663. self._model.changed.connect(self.__modelChanged)
  664. def _minVersionChanged(self):
  665. """ Minimum version has changed, it doesn't matter if it comes from the
  666. ComboBox or the Model.
  667. """
  668. # Enable/Disable the checkboxes based on wheter a minimum version is
  669. # selected or not.
  670. enabled = bool(self.minVersionCombo.currentIndex() != 0)
  671. self.gitVersionCheck.setEnabled(enabled)
  672. self.unknownSuffixVersionCheck.setEnabled(enabled)
  673. def _optionCheckBoxChanged(self, checkState):
  674. """ 'Development' or 'Unknown suffix' CheckBox has changed.
  675. """
  676. self._model.updateKwargs(self.data()) # update the model.
  677. def _compileVersions(self):
  678. """ Fill the combobox with available version strings
  679. """
  680. # Backup selected text so we can find the new index if it still exists.
  681. versionFilter = self._model.filter().get('version', {})
  682. selectedText = versionFilter.get('min', "")
  683. # Clear the combo
  684. self.minVersionCombo.clear()
  685. # Add the default None item
  686. self.minVersionCombo.addItem(_("Off"))
  687. # Unique
  688. uniqueVersions = []
  689. for url, instance in self._model.parentModel().items():
  690. if (
  691. instance.version.type and
  692. str(instance.version) not in uniqueVersions
  693. ):
  694. uniqueVersions.append(str(instance.version))
  695. # Sort
  696. uniqueVersions.sort()
  697. # Add versions to combobox
  698. selectedIndex = 0
  699. for versionStr in uniqueVersions:
  700. if versionStr == selectedText:
  701. # The selected item is still there, store it's index so we can
  702. # set it back later.
  703. selectedIndex = self.minVersionCombo.count()
  704. self.minVersionCombo.addItem(versionStr)
  705. # Restore the selected index
  706. self.minVersionCombo.setCurrentIndex(selectedIndex)
  707. # Enable/Disable the checkboxes based on wheter a minimum version is
  708. # selected or not.
  709. self._minVersionChanged()
  710. def __modelChanged(self):
  711. # Instances model has changed
  712. # Update versions combo
  713. self.minVersionCombo.currentTextChanged.disconnect(
  714. self._minVersionComboChanged
  715. )
  716. self._compileVersions() # re-generate the combobox items
  717. self.minVersionCombo.currentTextChanged.connect(
  718. self._minVersionComboChanged
  719. )
  720. versionFilter = self._model.filter().get('version', {})
  721. self.gitVersionCheck.setChecked(versionFilter.get('git', True))
  722. self.unknownSuffixVersionCheck.setChecked(
  723. versionFilter.get('unknown', True)
  724. )
  725. def data(self):
  726. result = {
  727. 'version': {
  728. 'min': self.minVersionCombo.currentText(),
  729. 'git': bool(self.gitVersionCheck.checkState()),
  730. 'unknown': bool(self.unknownSuffixVersionCheck.checkState())
  731. }
  732. }
  733. return result
  734. class CheckboxFilter(QCheckBox):
  735. def __init__(self, model, key, parent=None):
  736. """
  737. @type model: InstancesModel
  738. @param key: key to use (from model.filter())
  739. @type key: str
  740. """
  741. QCheckBox.__init__(self, parent=parent)
  742. self._model = model
  743. self._key = key
  744. self._model.changed.connect(self.__modelChanged)
  745. self.stateChanged.connect(self.__stateChanged)
  746. def __modelChanged(self):
  747. """ Model changed, update the checkbox.
  748. """
  749. filter_ = self._model.filter()
  750. state = filter_.get(self._key, True)
  751. self.stateChanged.disconnect(self.__stateChanged)
  752. self.setChecked(state)
  753. self.stateChanged.connect(self.__stateChanged)
  754. def __stateChanged(self):
  755. """ Checkbox state changed, update the model.
  756. """
  757. state = self.isChecked()
  758. self._model.updateKwargs({self._key: state})
  759. class FilterWidget(QWidget):
  760. def __init__(self, model, parent=None):
  761. """
  762. @type model: searxqt.models.instances.InstanceModelFilter
  763. """
  764. QWidget.__init__(self, parent=parent)
  765. self._model = model
  766. layout = QFormLayout(self)
  767. layout.setLabelAlignment(
  768. Qt.AlignLeading | Qt.AlignLeft | Qt.AlignTop
  769. )
  770. layout.addRow(QLabel(_("<h3>Filter</h3>"), self))
  771. self._networkFilter = NetworkFilterWidget(model, self)
  772. layout.addRow(QLabel(_("Network") + ":"), self._networkFilter)
  773. self._asnPrivacyFilter = None
  774. self._ipv6Filter = None
  775. self._analyticsFilter = None
  776. self._versionFilter = VersionFilter(model, self)
  777. layout.addRow(QLabel(_("Min version") + ":"), self._versionFilter)
  778. self._urlFilterOut = BlacklistFilterWidget(model, self)
  779. layout.addRow(QLabel(_("Blacklist") + ":"), self._urlFilterOut)
  780. self._urlFilter = WhitelistFilterWidget(model, self)
  781. layout.addRow(QLabel(_("Whitelist") + ":"), self._urlFilter)
  782. self._tmpBlacklist = TimeoutFilterWidget(model, self)
  783. layout.addRow(QLabel(_("Timeout") + ":"), self._tmpBlacklist)
  784. model.parentModel().typeChanged.connect(self.__modelTypeChanged)
  785. def __modelTypeChanged(self, type_):
  786. layout = self.layout()
  787. if type_ == InstancesModelTypes.Stats2:
  788. if not self._asnPrivacyFilter:
  789. self._asnPrivacyFilter = CheckboxFilter(
  790. self._model,
  791. 'asnPrivacy',
  792. self
  793. )
  794. label = QLabel(_("Require ASN privacy") + ":")
  795. label.setWordWrap(True)
  796. label.setToolTip(_(
  797. "Filter out instances that run their server at a known\n"
  798. "malicious network like Google, CloudFlare, Akamai etc..\n"
  799. "\n"
  800. "This does not give any guarantee, it only filters known\n"
  801. "privacy violators!"
  802. ))
  803. layout.insertRow(2, label, self._asnPrivacyFilter)
  804. if not self._ipv6Filter:
  805. self._ipv6Filter = CheckboxFilter(self._model, 'ipv6', self)
  806. label = QLabel(_("Require IPv6") + ":")
  807. label.setWordWrap(True)
  808. label.setToolTip(_(
  809. "Only instances with at least one ipv6-address remain"
  810. " if checked."
  811. ))
  812. layout.insertRow(3, label, self._ipv6Filter)
  813. if not self._analyticsFilter:
  814. self._analyticsFilter = CheckboxFilter(
  815. self._model,
  816. 'analytics',
  817. self
  818. )
  819. label = QLabel(_("Excl. analytics") + ":")
  820. label.setWordWrap(True)
  821. label.setToolTip(_(
  822. "Exclude instances running tracking software."
  823. ))
  824. layout.insertRow(4, label, self._analyticsFilter)
  825. else:
  826. if self._asnPrivacyFilter:
  827. self._asnPrivacyFilter.deleteLater()
  828. layout.removeRow(2)
  829. self._asnPrivacyFilter = None
  830. if self._ipv6Filter:
  831. self._ipv6Filter.deleteLater()
  832. layout.removeRow(2)
  833. self._ipv6Filter = None
  834. if self._analyticsFilter:
  835. self._analyticsFilter.deleteLater()
  836. layout.removeRow(2)
  837. self._analyticsFilter = None
  838. class InstancesStatsWidget(QWidget):
  839. def __init__(self, filterModel, parent=None):
  840. """
  841. @param filterModel:
  842. @type filterModel: searxqt.models.instances.InstanceModelFilter
  843. """
  844. QWidget.__init__(self, parent=parent)
  845. self._filterModel = filterModel
  846. self._currentInstancesType = filterModel.parentModel().Type
  847. self._countLabel = None
  848. self._filterCountLabel = None
  849. self._lastUpdateLabel = None
  850. layout = QFormLayout(self)
  851. layout.setLabelAlignment(
  852. Qt.AlignLeading | Qt.AlignLeft | Qt.AlignTop
  853. )
  854. self._toggleButton = Button("", self)
  855. layout.addRow(QLabel(_("<h3>Stats</h3>"), self), self._toggleButton)
  856. self.__show()
  857. self._toggleButton.clicked.connect(self.__toggle)
  858. self._filterModel.parentModel().typeChanged.connect(
  859. self.__instancesModelTypeChanged
  860. )
  861. def collapsed(self):
  862. return bool(self._countLabel is None)
  863. def __toggle(self):
  864. if self.collapsed():
  865. self.__show()
  866. else:
  867. self.__hide()
  868. def __show(self):
  869. self._toggleButton.setText(_("Hide"))
  870. layout = self.layout()
  871. self._countLabel = QLabel("0", self)
  872. layout.addRow(QLabel(_("Total count") + ":"), self._countLabel)
  873. self._filterCountLabel = QLabel("0", self)
  874. layout.addRow(
  875. QLabel(_("After filter count") + ":"),
  876. self._filterCountLabel
  877. )
  878. self._lastUpdateLabel = QLabel("0", self)
  879. self._lastUpdateLabel.setWordWrap(True)
  880. layout.addRow(QLabel(_("Last update") + ":"), self._lastUpdateLabel)
  881. self.__modelChanged() # Update count and after filter count
  882. self.__instancesModelTypeChanged(self._filterModel.parentModel().Type)
  883. self._filterModel.changed.connect(self.__modelChanged)
  884. def __hide(self):
  885. self._filterModel.changed.disconnect(self.__modelChanged)
  886. self.__instancesModelTypeChanged(InstancesModelTypes.NotDefined)
  887. self._toggleButton.setText(_("Show"))
  888. layout = self.layout()
  889. while layout.rowCount() > 1:
  890. layout.removeRow(1)
  891. self._countLabel = None
  892. self._filterCountLabel = None
  893. self._lastUpdateLabel = None
  894. """ InstancesType dependant methods below
  895. """
  896. def __instancesModelTypeChanged(self, type_):
  897. """ PersistentInstancesModel emitted changed.
  898. """
  899. if self.collapsed():
  900. return
  901. previousType = self._currentInstancesType
  902. self._currentInstancesType = type_
  903. # Disconnect signals
  904. if previousType == InstancesModelTypes.Stats2:
  905. self._filterModel.changed.disconnect(self.__updateLastUpdated)
  906. elif previousType == InstancesModelTypes.User:
  907. pass
  908. if type_ == InstancesModelTypes.Stats2:
  909. self._filterModel.changed.connect(self.__updateLastUpdated)
  910. self.__updateLastUpdated()
  911. else:
  912. self._lastUpdateLabel.setText("-")
  913. # InstancesModelTypes.Stats2
  914. def __updateLastUpdated(self):
  915. self._lastUpdateLabel.setText(
  916. timeToString(
  917. self._filterModel.parentModel().handler().lastUpdated()
  918. )
  919. )
  920. """ -------------------------------------------
  921. """
  922. def __modelChanged(self):
  923. self._countLabel.setText(str(len(self._filterModel.parentModel())))
  924. self._filterCountLabel.setText(str(len(self._filterModel)))