123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- import sys
- from PyQt5.QtWidgets import (
- QApplication,
- QAction,
- QSplitter,
- QMenuBar,
- QMenu,
- QMainWindow,
- QHBoxLayout,
- QVBoxLayout,
- QWidget,
- QStatusBar,
- QLabel,
- QMessageBox
- )
- from PyQt5.QtCore import (
- Qt,
- QSettings,
- QByteArray,
- QSize
- )
- from searxqt.models.instances import (
- Stats2InstancesModel,
- UserInstancesModel,
- Stats2EnginesModel,
- UserEnginesModel,
- Stats2Model,
- InstanceModelFilter,
- InstanceSelecterModel,
- PersistentInstancesModel,
- PersistentEnginesModel,
- InstancesModelTypes
- )
- from searxqt.models.settings import SettingsModel
- from searxqt.models.search import SearchModel, UserInstancesHandler
- from searxqt.models.profiles import Profiles, ProfileItem
- from searxqt.views.instances import InstancesView
- from searxqt.views.settings import SettingsWindow
- from searxqt.views.search import SearchContainer
- from searxqt.views import about
- from searxqt.views.profiles import ProfileChooserDialog
- from searxqt.core.requests import RequestsHandler
- from searxqt.core.guard import Guard
- from searxqt.translations import _
- from searxqt.version import __version__
- from searxqt import PROFILES_PATH, SETTINGS_PATH
- class MainWindow(QMainWindow):
- def __init__(self, *args, **kwargs):
- QMainWindow.__init__(self, *args, **kwargs)
- self.setWindowTitle("SearX-Qt")
- self._settingsModel = SettingsModel(self)
- self._handler = None
- self._settingsWindow = None
-
- self._requestHandler = RequestsHandler(self._settingsModel.requests)
-
- self._persistantInstancesModel = PersistentInstancesModel()
- self._persistantEnginesModel = PersistentEnginesModel()
-
- self._profiles = Profiles()
- self.instanceFilter = InstanceModelFilter(
- self._persistantInstancesModel, self
- )
- self.instanceSelecter = InstanceSelecterModel(self.instanceFilter)
- self._searchModel = SearchModel(self._requestHandler, self)
-
- self._guard = Guard()
-
- menubar = QMenuBar(self)
-
- menuFile = QMenu(menubar)
- menuFile.setTitle(_("File"))
- saveAction = QAction(_("Save"), menuFile)
- menuFile.addAction(saveAction)
- saveAction.setShortcut('Ctrl+S')
- saveAction.triggered.connect(self.saveSettings)
- actionExit = QAction(_("Exit"), menuFile)
- menuFile.addAction(actionExit)
- actionExit.setShortcut('Ctrl+Q')
- actionExit.triggered.connect(self.close)
- menubar.addAction(menuFile.menuAction())
-
- settingsAction = QAction(_("Settings"), menubar)
- menubar.addAction(settingsAction)
- settingsAction.triggered.connect(self._openSettingsWindow)
-
- profilesAction = QAction(_("Profiles"), menubar)
- menubar.addAction(profilesAction)
- profilesAction.triggered.connect(self._openProfileChooser)
-
- aboutAction = QAction(_("About"), menubar)
- menubar.addAction(aboutAction)
- aboutAction.triggered.connect(self._openAboutDialog)
- self.setMenuBar(menubar)
-
-
- self.statusBar = QStatusBar(self)
- statusWidget = QWidget(self)
- statusLayout = QHBoxLayout(statusWidget)
- self._handlerThreadStatusLabel = QLabel(self)
- statusLayout.addWidget(self._handlerThreadStatusLabel)
- self._statusInstanceLabel = QLabel(self)
- statusLayout.addWidget(self._statusInstanceLabel)
- self.statusBar.addPermanentWidget(statusWidget)
- self.setStatusBar(self.statusBar)
-
- centralWidget = QWidget(self)
- layout = QVBoxLayout(centralWidget)
- self.setCentralWidget(centralWidget)
- self.splitter = QSplitter(centralWidget)
- self.splitter.setOrientation(Qt.Horizontal)
- layout.addWidget(self.splitter)
- self.searchContainer = SearchContainer(
- self._searchModel,
- self.instanceFilter,
- self.instanceSelecter,
- self._persistantEnginesModel,
- self._guard,
- self.splitter
- )
- self.instancesWidget = InstancesView(
- self.instanceFilter,
- self.instanceSelecter,
- self.splitter
- )
- self.instanceSelecter.instanceChanged.connect(self.__instanceChanged)
- self.__profileChooserInit()
- self.resize(800, 600)
- self.loadSharedSettings()
- self.loadProfile()
- def __instanceChanged(self, url):
- self._statusInstanceLabel.setText(
- "<b>{0}:</b> {1}".format(_("Instance"), url)
- )
- def closeEvent(self, event=None):
-
- self.setEnabled(False)
-
- if self.searchContainer.isBusy():
- print("- Waiting for search thread to finish...")
- self.searchContainer.cancelAll()
- print("- Search thread finished.")
- if self._handler and self._handler.hasActiveJobs():
- print("- Waiting for update instances thread to finish...")
- self._handler.cancelAll()
- print("- Instances update thread finished.")
- self.saveSettings()
- print("- Settings saved.")
-
- self._profiles.setProfile(
- self._profiles.settings(),
- ProfileItem()
- )
- QApplication.closeAllWindows()
- print("Bye!")
- def _openAboutDialog(self):
- about.show(self)
- def __execProfileChooser(self):
- """ This only sets the profile, it does not load it.
- Returns True on success, False when something went wrong.
- """
- profiles = self._profiles
- profilesSettings = profiles.settings()
- profiles.loadProfiles(profilesSettings)
- dialog = ProfileChooserDialog(profiles)
- if dialog.exec():
- currentProfile = profiles.current()
- selectedProfile = dialog.selectedProfile()
-
- if currentProfile.id:
- self.saveProfile()
- profiles.setProfile(
- profilesSettings,
- selectedProfile
- )
- else:
- self.__finalizeProfileChooser(dialog)
- return False
- self.__finalizeProfileChooser(dialog)
- return True
- def __profileChooserInit(self):
- profiles = self._profiles
- profilesSettings = profiles.settings()
- profiles.loadProfiles(profilesSettings)
- activeProfiles = profiles.getActiveProfiles(profilesSettings)
- defaultProfile = profiles.default()
- if not defaultProfile.id or defaultProfile.id in activeProfiles:
- if not self.__execProfileChooser():
- sys.exit()
- else:
-
- profiles.setProfile(
- profilesSettings,
- defaultProfile
- )
- def _openProfileChooser(self):
- if self._handler and self._handler.hasActiveJobs():
- QMessageBox.information(
- self,
- _("Instances update thread active"),
- _("Please wait until instances finished updating before\n"
- "switching profiles.")
- )
- return
- if self.__execProfileChooser():
- self.loadProfile()
- def __finalizeProfileChooser(self, dialog):
- """ Profiles may have been added or removed.
- - Store profiles.conf
- - Remove removed profile conf files
- """
- self._profiles.saveProfiles()
- self._profiles.removeProfileFiles(dialog.removedProfiles())
- def _openSettingsWindow(self):
- if not self._settingsWindow:
- self._settingsWindow = SettingsWindow(
- self._settingsModel,
- self._guard
- )
- self._settingsWindow.resize(self.__lastSettingsWindowSize)
- self._settingsWindow.show()
- self._settingsWindow.activateWindow()
- self._settingsWindow.closed.connect(self._delSettingsWindow)
- def _delSettingsWindow(self):
- self._settingsWindow.deleteLater()
- self._settingsWindow = None
- def __handlerThreadChanged(self):
- self._handlerThreadStatusLabel.setText(
- self._handler.currentJobStr()
- )
- def loadProfile(self):
- profile = self._profiles.current()
- self.setWindowTitle(f"SearX-Qt - {profile.name}")
- profileSettings = QSettings(PROFILES_PATH, profile.id, self)
-
- from searxqt.config import upgradeProfileConfig
- upgradeProfileConfig(profileSettings)
- del upgradeProfileConfig
-
- if self._handler:
- self._handler.threadStarted.disconnect(
- self.__handlerThreadChanged
- )
- self._handler.threadFinished.disconnect(
- self.__handlerThreadChanged
- )
- self._handler.deleteLater()
- self._handler = None
- if self._settingsWindow:
- self._delSettingsWindow()
- self._searchModel.reset()
-
- if profile.type == InstancesModelTypes.Stats2:
- self._handler = Stats2Model(self._requestHandler, self)
- instancesModel = Stats2InstancesModel(self._handler, parent=self)
- enginesModel = Stats2EnginesModel(self._handler, parent=self)
- self._settingsModel.loadSettings(
- profileSettings.value('settings', dict(), dict),
- stats2=True
- )
- elif profile.type == InstancesModelTypes.User:
- self._handler = UserInstancesHandler(self._requestHandler, self)
- instancesModel = UserInstancesModel(self._handler, parent=self)
- enginesModel = UserEnginesModel(self._handler, parent=self)
- self._settingsModel.loadSettings(
- profileSettings.value('settings', dict(), dict),
- stats2=False
- )
- self._persistantInstancesModel.setModel(instancesModel)
- self._persistantEnginesModel.setModel(enginesModel)
- self._handler.setData(
- profileSettings.value('data', dict(), dict)
- )
- self._handler.threadStarted.connect(self.__handlerThreadChanged)
- self._handler.threadFinished.connect(self.__handlerThreadChanged)
-
- self.instanceFilter.loadSettings(
- profileSettings.value('instanceFilter', dict(), dict)
- )
- self.instancesWidget.loadSettings(
- profileSettings.value('instancesView', dict(), dict)
- )
- self.instanceSelecter.loadSettings(
- profileSettings.value('instanceSelecter', dict(), dict)
- )
- self.searchContainer.loadSettings(
- profileSettings.value('searchContainer', dict(), dict)
- )
- self._searchModel.loadSettings(
- profileSettings.value('searchModel', dict(), dict)
- )
-
- self._guard.deserialize(profileSettings.value('Guard', dict(), dict))
-
- self.splitter.restoreState(
- profileSettings.value('splitterState', QByteArray(), QByteArray)
- )
- def saveProfile(self):
- """ Save current profile
- """
- profile = self._profiles.current()
- profileSettings = QSettings(PROFILES_PATH, profile.id, self)
- profileSettings.setValue(
- 'settings', self._settingsModel.saveSettings()
- )
- profileSettings.setValue(
- 'instanceFilter', self.instanceFilter.saveSettings()
- )
- profileSettings.setValue(
- 'instancesView', self.instancesWidget.saveSettings()
- )
- profileSettings.setValue(
- 'instanceSelecter', self.instanceSelecter.saveSettings()
- )
- profileSettings.setValue(
- 'searchContainer', self.searchContainer.saveSettings()
- )
- profileSettings.setValue(
- 'searchModel', self._searchModel.saveSettings()
- )
-
- profileSettings.setValue('Guard', self._guard.serialize())
-
- profileSettings.setValue('splitterState', self.splitter.saveState())
-
- profileSettings.setValue('version', __version__)
- if self._handler:
- profileSettings.setValue('data', self._handler.data())
- def loadSharedSettings(self):
- """ Load shared settings
- """
- settings = QSettings(SETTINGS_PATH, 'shared', self)
- self.resize(
- settings.value('windowSize', QSize(), QSize)
- )
- self.__lastSettingsWindowSize = settings.value(
- 'settingsWindowSize',
- QSize(400, 400),
- QSize
- )
- def saveSettings(self):
-
- if self._profiles.current().id:
- self.saveProfile()
-
- settings = QSettings(SETTINGS_PATH, 'shared', self)
- settings.setValue('windowSize', self.size())
- if self._settingsWindow:
- settings.setValue(
- 'settingsWindowSize',
- self._settingsWindow.size()
- )
|