settings.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.QtCore import QObject, pyqtSignal
  22. from searxqt.themes import Themes
  23. from searxqt.core.instances import Stats2
  24. from searxqt.core import log
  25. class General:
  26. def serialize():
  27. return {
  28. 'logLevel': log.LogLevel,
  29. 'themes': Themes.serialize()
  30. }
  31. def deserialize(data):
  32. # Populate / re-populate user and system themes
  33. Themes.populate()
  34. themesData = data.get('themes', {})
  35. if themesData:
  36. Themes.deserialize(themesData)
  37. # Log level
  38. logLevel = data.get('logLevel', None)
  39. if logLevel is not None:
  40. log.LogLevel = logLevel
  41. else:
  42. log.LogLevel = log.LogLevels.QUIET
  43. class SettingsModel(QObject):
  44. changed = pyqtSignal()
  45. def __init__(self, requestSettings, parent=None):
  46. QObject.__init__(self, parent=parent)
  47. self._requests = RequestSettingsModel(requestSettings)
  48. self._stats2 = None
  49. self._requests.changed.connect(self.changed)
  50. def loadSettings(self, data, stats2=True):
  51. self.requests.data = data.get('requests', {})
  52. if self._stats2:
  53. self._stats2.deleteLater()
  54. self._stats2 = None
  55. if stats2:
  56. self._stats2 = SearxStats2Model()
  57. stats2Data = data.get('stats2', {})
  58. self.stats2.loadSettings(stats2Data)
  59. General.deserialize(data.get('general', {}))
  60. def saveSettings(self):
  61. if self._stats2:
  62. return {
  63. 'general': General.serialize(),
  64. 'requests': self.requests.data,
  65. 'stats2': self.stats2.saveSettings()
  66. }
  67. return {
  68. 'general': General.serialize(),
  69. 'requests': self.requests.data,
  70. }
  71. @property
  72. def requests(self): return self._requests
  73. @property
  74. def stats2(self): return self._stats2
  75. class SearxStats2Model(QObject):
  76. changed = pyqtSignal()
  77. def __init__(self, parent=None):
  78. QObject.__init__(self, parent=parent)
  79. self._backupUrl = Stats2.URL
  80. def loadSettings(self, data):
  81. self.url = data.get('url', self._backupUrl)
  82. def saveSettings(self):
  83. return {
  84. 'url': self.url
  85. }
  86. def reset(self):
  87. self.url = self._backupUrl
  88. @property
  89. def url(self):
  90. return Stats2.URL
  91. @url.setter
  92. def url(self, url):
  93. Stats2.URL = url
  94. self.changed.emit()
  95. class RequestSettingsModel(QObject):
  96. changed = pyqtSignal()
  97. # @param requestSettings: TODO
  98. # @type requestSettings: core.requests.RequestSettings
  99. def __init__(self, requestSettings, parent=None):
  100. QObject.__init__(self, parent=parent)
  101. self._settings = requestSettings
  102. def __changed(self):
  103. self._settings.updateRequestKwargs()
  104. self.changed.emit()
  105. @property
  106. def data(self):
  107. return self._settings.getData()
  108. @data.setter
  109. def data(self, data):
  110. self._settings.setData(data)
  111. @property
  112. def extraHeaders(self):
  113. return self._settings.extraHeaders
  114. @property
  115. def verifySSL(self):
  116. return self._settings.verifySSL
  117. @verifySSL.setter
  118. def verifySSL(self, state):
  119. self._settings.verifySSL = state
  120. self.__changed()
  121. @property
  122. def timeout(self):
  123. return self._settings.timeout
  124. @timeout.setter
  125. def timeout(self, seconds):
  126. self._settings.timeout = seconds
  127. self.__changed()
  128. @property
  129. def maxSize(self):
  130. return self._settings.maxSize
  131. @maxSize.setter
  132. def maxSize(self, size):
  133. self._settings.maxSize = size
  134. self.__changed()
  135. @property
  136. def chunkSize(self):
  137. return self._settings.chunkSize
  138. @chunkSize.setter
  139. def chunkSize(self, size):
  140. self._settings.chunkSize = size
  141. self.__changed()
  142. @property
  143. def proxyEnabled(self):
  144. return self._settings.proxyEnabled
  145. @proxyEnabled.setter
  146. def proxyEnabled(self, state):
  147. self._settings.proxyEnabled = state
  148. self.__changed()
  149. @property
  150. def proxyHost(self):
  151. return self._settings.proxyHost
  152. @proxyHost.setter
  153. def proxyHost(self, host):
  154. self._settings.proxyHost = host
  155. self.__changed()
  156. @property
  157. def proxyProtocol(self):
  158. return self._settings.proxyProtocol
  159. @proxyProtocol.setter
  160. def proxyProtocol(self, protocol):
  161. self._settings.proxyProtocol = protocol
  162. self.__changed()
  163. @property
  164. def proxyDNS(self):
  165. return self._settings.proxyDNS
  166. @proxyDNS.setter
  167. def proxyDNS(self, state):
  168. self._settings.proxyDNS = state
  169. self.__changed()
  170. @property
  171. def useragents(self):
  172. return self._settings.useragents
  173. @useragents.setter
  174. def useragents(self, strList):
  175. """
  176. @param strList: A string with user-agents, separated by a new line.
  177. May be empty.
  178. @type strList: str
  179. """
  180. self._settings.useragents.clear()
  181. for ua in strList:
  182. self._settings.useragents.append(ua)
  183. self.__changed()
  184. @property
  185. def randomUserAgent(self):
  186. return self._settings.randomUserAgent
  187. @randomUserAgent.setter
  188. def randomUserAgent(self, state):
  189. """
  190. @param state: True to select random user-agent for each request
  191. If False it will select the first user-agent. When
  192. the user-agents list is empty the send string will
  193. be also empty.
  194. @type state: bool
  195. """
  196. self._settings.randomUserAgent = state
  197. self.__changed()