2021-01-29 11:43:07 +08:00
/*
* Copyright ( C ) 2020 , KylinSoft Co . , Ltd .
*
* This program 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 .
*
* This program 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/>.
*
* Authors : zhangzihao < zhangzihao @ kylinos . cn >
* Modified by : zhangpengfei < zhangpengfei @ kylinos . cn >
*
*/
2021-01-03 16:58:26 +08:00
# include <QtConcurrent>
2021-01-04 14:21:45 +08:00
# include <QApplication>
# include <QPalette>
# include "global-settings.h"
2020-12-28 20:29:13 +08:00
2021-01-11 16:59:50 +08:00
static GlobalSettings * global_instance_of_global_settings = nullptr ;
2021-01-03 16:58:26 +08:00
GlobalSettings * GlobalSettings : : getInstance ( )
{
2021-01-11 16:59:50 +08:00
if ( ! global_instance_of_global_settings ) {
global_instance_of_global_settings = new GlobalSettings ;
2021-01-03 16:58:26 +08:00
}
2021-01-11 16:59:50 +08:00
return global_instance_of_global_settings ;
2021-01-03 16:58:26 +08:00
}
2020-12-28 20:29:13 +08:00
GlobalSettings : : GlobalSettings ( QObject * parent ) : QObject ( parent )
{
2021-01-04 18:55:18 +08:00
m_settings = new QSettings ( " org.ukui " , " ukui-search " , this ) ;
2021-01-08 19:08:24 +08:00
m_block_dirs_settings = new QSettings ( " org.ukui " , " ukui-search-block-dirs " , this ) ;
2021-01-12 16:07:50 +08:00
m_block_dirs_settings - > setIniCodec ( QTextCodec : : codecForName ( " UTF-8 " ) ) ;
2021-01-09 11:25:07 +08:00
this - > forceSync ( ) ;
2021-01-04 14:21:45 +08:00
//the default number of transparency in mainwindow is 0.7
//if someone changes the num in mainwindow, here should be modified too
m_cache . insert ( TRANSPARENCY_KEY , 0.7 ) ;
if ( QGSettings : : isSchemaInstalled ( CONTROL_CENTER_PERSONALISE_GSETTINGS_ID ) ) {
m_gsettings = new QGSettings ( CONTROL_CENTER_PERSONALISE_GSETTINGS_ID , QByteArray ( ) , this ) ;
connect ( m_gsettings , & QGSettings : : changed , this , [ = ] ( const QString & key ) {
if ( key = = TRANSPARENCY_KEY ) {
m_cache . remove ( TRANSPARENCY_KEY ) ;
m_cache . insert ( TRANSPARENCY_KEY , m_gsettings - > get ( TRANSPARENCY_KEY ) . toDouble ( ) ) ;
qApp - > paletteChanged ( qApp - > palette ( ) ) ;
}
} ) ;
2021-01-20 15:33:49 +08:00
m_cache . remove ( TRANSPARENCY_KEY ) ;
m_cache . insert ( TRANSPARENCY_KEY , m_gsettings - > get ( TRANSPARENCY_KEY ) . toDouble ( ) ) ;
2021-01-04 14:21:45 +08:00
}
2021-01-03 16:58:26 +08:00
}
GlobalSettings : : ~ GlobalSettings ( )
{
}
const QVariant GlobalSettings : : getValue ( const QString & key )
{
return m_cache . value ( key ) ;
}
bool GlobalSettings : : isExist ( const QString & key )
{
return ! m_cache . value ( key ) . isNull ( ) ;
}
void GlobalSettings : : reset ( const QString & key )
{
m_cache . remove ( key ) ;
QtConcurrent : : run ( [ = ] ( ) {
if ( m_mutex . tryLock ( 1000 ) ) {
m_settings - > remove ( key ) ;
m_settings - > sync ( ) ;
m_mutex . unlock ( ) ;
}
} ) ;
Q_EMIT this - > valueChanged ( key ) ;
}
void GlobalSettings : : resetAll ( )
{
QStringList tmp = m_cache . keys ( ) ;
m_cache . clear ( ) ;
for ( auto key : tmp ) {
Q_EMIT this - > valueChanged ( key ) ;
}
QtConcurrent : : run ( [ = ] ( ) {
if ( m_mutex . tryLock ( 1000 ) ) {
m_settings - > clear ( ) ;
m_settings - > sync ( ) ;
m_mutex . unlock ( ) ;
}
} ) ;
}
2020-12-28 20:29:13 +08:00
2021-01-10 09:23:02 +08:00
bool GlobalSettings : : setBlockDirs ( const QString & path , QString & returnMessage , bool remove )
2021-01-03 16:58:26 +08:00
{
2021-01-10 09:23:02 +08:00
if ( remove )
{
2021-01-27 10:22:56 +08:00
if ( path . isEmpty ( ) )
{
returnMessage = QString ( tr ( " I can't remove an empty path string! " ) ) ;
return false ;
}
2021-01-12 16:07:50 +08:00
m_block_dirs_settings - > remove ( path ) ;
2021-01-10 09:23:02 +08:00
return true ;
}
2021-01-27 10:22:56 +08:00
if ( ! path . startsWith ( " /home " ) )
{
returnMessage = QString ( tr ( " I can only search your user directory, it doesn't make any sense if you block an directory which is not in user directory! " ) ) ;
return false ;
}
2021-01-12 16:07:50 +08:00
//why QSetting's key can't start with "/"??
QString pathKey = path . right ( path . length ( ) - 1 ) ;
2021-01-08 19:08:24 +08:00
QStringList blockDirs = m_block_dirs_settings - > allKeys ( ) ;
for ( QString i : blockDirs )
{
2021-01-10 09:23:02 +08:00
if ( pathKey . startsWith ( i ) )
2021-01-08 19:08:24 +08:00
{
2021-01-27 10:22:56 +08:00
returnMessage = QString ( tr ( " My parent folder has been blocked! " ) ) ;
2021-01-08 19:08:24 +08:00
return false ;
}
2021-01-10 09:23:02 +08:00
if ( i . startsWith ( pathKey ) )
2021-01-08 19:08:24 +08:00
m_block_dirs_settings - > remove ( i ) ;
}
2021-01-10 09:23:02 +08:00
m_block_dirs_settings - > setValue ( pathKey , " 0 " ) ;
2021-01-08 19:08:24 +08:00
return true ;
}
QStringList GlobalSettings : : getBlockDirs ( )
{
return m_block_dirs_settings - > allKeys ( ) ;
2021-01-03 16:58:26 +08:00
}
2021-01-10 09:01:22 +08:00
//here should be override
//MouseZhangZh
2021-01-03 16:58:26 +08:00
void GlobalSettings : : setValue ( const QString & key , const QVariant & value )
{
m_cache . insert ( key , value ) ;
QtConcurrent : : run ( [ = ] ( ) {
2021-01-10 09:01:22 +08:00
// if (m_mutex.tryLock(1000)) {
m_mutex . lock ( ) ;
2021-01-03 16:58:26 +08:00
m_settings - > setValue ( key , value ) ;
m_settings - > sync ( ) ;
m_mutex . unlock ( ) ;
2021-01-10 09:01:22 +08:00
// }
2021-01-03 16:58:26 +08:00
} ) ;
}
void GlobalSettings : : forceSync ( const QString & key )
{
m_settings - > sync ( ) ;
if ( key . isNull ( ) ) {
m_cache . clear ( ) ;
for ( auto key : m_settings - > allKeys ( ) ) {
m_cache . insert ( key , m_settings - > value ( key ) ) ;
}
} else {
m_cache . remove ( key ) ;
m_cache . insert ( key , m_settings - > value ( key ) ) ;
}
2020-12-28 20:29:13 +08:00
}