123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- ########################################################################
- # 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.QtCore import QSettings, QStandardPaths, QFile
- from searxqt.models.instances import InstancesModelTypes
- from searxqt import PROFILES_PATH, SETTINGS_PATH
- class ProfileItem(tuple):
- def __init__(self, data=None):
- """
- @param data:
- @type data: tuple (id, name, type)
- """
- tuple.__init__(self)
- self.id = ''
- self.name = ''
- self.type = InstancesModelTypes.NotDefined
- if data:
- self.id = data[0]
- self.name = data[1]
- self.type = data[2]
- def __new__(cls, data=('', '')):
- return super(ProfileItem, cls).__new__(cls, tuple(data))
- class Profiles:
- def __init__(self):
- self._currentProfile = ProfileItem()
- self._profiles = {
- # list with searxqt.models.profiles.ProfileItem's
- 'profiles': [],
- # str: profile id
- 'default': ProfileItem()
- }
- def __contains__(self, profile):
- return bool(profile in self._profiles['profiles'])
- def __len__(self):
- return len(self._profiles['profiles'])
- def __getitem__(self, index):
- return self._profiles['profiles'][index]
- def add(self, profile):
- """ Add profile to this object. This will not save profiles.conf!
- @param profile: ProfileItem to add.
- @type profile: ProfileItem
- """
- self._profiles['profiles'].append(profile)
- def remove(self, profile):
- """ Remove profile from this object. This will not save profiles.conf!
- @param profile: ProfileItem to remove.
- @type profile: ProfileItem
- """
- self._profiles['profiles'].remove(profile)
- def current(self):
- """ Returns current profile
- @rtype: ProfileItem
- """
- return self._currentProfile
- def default(self):
- """ Returns the default profile
- @rtype: ProfileItem
- """
- return self._profiles['default']
- def setDefault(self, profile):
- """
- @param profile: ProfileItem to set as default.
- @type profile: ProfileItem
- """
- self._profiles['default'] = profile
- def loadProfiles(self, settings):
- """ Load profiles.conf
- """
- self._profiles = {
- # list with searxqt.models.profiles.ProfileItem's
- 'profiles': settings.value('profiles', list(), list),
- # str: profile id
- 'default': settings.value(
- 'default', ProfileItem(), ProfileItem
- )
- }
- def getActiveProfiles(self, settings):
- """ Returns a list with active profile id's
- """
- return settings.value('active', list(), list)
- def profileActive(self, profile):
- """ Check if profile is active or not. This will re-read the
- profiles.conf file.
- @rtype: bool
- """
- settings = self.settings()
- return bool(profile.id in self.getActiveProfiles(settings))
- def profileExists(self, profile):
- """ Check if profile is still present in profiles.conf. This will
- re-read the profiles.conf file. It however won't store the read
- data in this object.
- @rtype: bool
- """
- settings = self.settings()
- profiles = settings.value('profiles', list(), list)
- for p in profiles:
- if p.id == profile.id:
- return True
- return False
- def settings(self):
- return QSettings(SETTINGS_PATH, 'profiles')
- def releaseAll(self):
- """ Release active profiles. This may be wanted after a crash of
- searx-qt or the system. User must make sure to close all other
- instances of searx-qt first.
- """
- settings = self.settings()
- settings.remove('active')
- def setProfile(self, settings, profile):
- """ Sets current profile active (also stores it to profiles.conf).
- @type settings: QSettings
- @type profile: ProfileItem
- """
- # Read a list of active profile id's
- activeProfiles = self.getActiveProfiles(settings)
- if self._currentProfile.id in activeProfiles:
- # Remove current profile id from active profiles
- activeProfiles.remove(self._currentProfile.id)
- # Append current profile id to the active profiles list
- if profile.id:
- activeProfiles.append(profile.id)
- # Store the changes
- settings.setValue('active', activeProfiles)
- self._currentProfile = profile
- def saveProfiles(self):
- settings = self.settings()
- settings.setValue('profiles', self._profiles['profiles'])
- settings.setValue('default', self._profiles['default'])
- def removeProfileFiles(self, profileIds):
- """ Removes all profile files for given profile ID's.
- @param profileIds: List with unique profile ID's
- @type profileIds: list
- """
- for profileId in profileIds:
- self.removeProfileFile(profileId)
- def removeProfileFile(self, profileId):
- """ Remove profile file for given profile ID.
- @param profileId: Unique profile id
- @type profileId: str
- """
- # Locate full file-path
- confFilePath = QStandardPaths.locate(
- QStandardPaths.ConfigLocation,
- f"{PROFILES_PATH}{profileId}.conf",
- QStandardPaths.LocateFile
- )
- # confFilePath may be a empty string, this means the file wasn't
- # found. This can happen when the profile has never loaded and saved.
- if confFilePath:
- print(f"Trying to remove: {confFilePath}")
- confFile = QFile(confFilePath)
- if confFile.remove():
- print(f"Removed {confFilePath}")
- return True
- else:
- print(f"Could not remove {confFilePath}")
- return False
- def names(self):
- """ Returns all profile names in a list.
- @rtype: list
- """
- return [profile.name for profile in self._profiles['profiles']]
|