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-04-30 16:28:50 +08:00
using namespace Zeeker ;
2021-01-11 16:59:50 +08:00
static GlobalSettings * global_instance_of_global_settings = nullptr ;
2021-01-03 16:58:26 +08:00
2021-04-26 15:06:47 +08:00
GlobalSettings * GlobalSettings : : getInstance ( ) {
if ( ! global_instance_of_global_settings ) {
2021-01-11 16:59:50 +08:00
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
}
2021-04-26 15:06:47 +08:00
GlobalSettings : : GlobalSettings ( QObject * parent ) : QObject ( parent ) {
2021-03-30 09:23:57 +08:00
m_settings = new QSettings ( MAIN_SETTINGS , QSettings : : IniFormat , this ) ;
2021-02-25 14:40:44 +08:00
// m_settings->setAtomicSyncRequired(false);
2021-04-26 15:06:47 +08:00
m_block_dirs_settings = new QSettings ( BLOCK_DIRS , QSettings : : IniFormat , this ) ;
2021-01-12 16:07:50 +08:00
m_block_dirs_settings - > setIniCodec ( QTextCodec : : codecForName ( " UTF-8 " ) ) ;
2021-03-30 09:23:57 +08:00
2021-04-26 15:06:47 +08:00
m_search_record_settings = new QSettings ( SEARCH_HISTORY , QSettings : : IniFormat , this ) ;
2021-03-30 09:23:57 +08:00
m_search_record_settings - > setIniCodec ( QTextCodec : : codecForName ( " UTF-8 " ) ) ;
2021-04-26 15:06:47 +08:00
for ( QString i : m_search_record_settings - > allKeys ( ) ) {
2021-03-30 09:23:57 +08:00
m_history . append ( QUrl : : fromPercentEncoding ( i . toLocal8Bit ( ) ) ) ;
}
if ( ! QDBusConnection : : sessionBus ( ) . connect ( " org.kylinssoclient.dbus " ,
2021-04-26 15:06:47 +08:00
" /org/kylinssoclient/path " ,
" org.freedesktop.kylinssoclient.interface " ,
" keyChanged " ,
this , SLOT ( updateSearchHistory ( QString ) ) ) )
2021-03-30 09:23:57 +08:00
2021-04-26 15:06:47 +08:00
qWarning ( ) < < " Kylinssoclient Dbus connect fail! " ;
2021-03-30 09:23:57 +08:00
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 ) ;
2021-04-26 15:06:47 +08:00
if ( QGSettings : : isSchemaInstalled ( CONTROL_CENTER_PERSONALISE_GSETTINGS_ID ) ) {
2021-04-13 15:40:19 +08:00
m_trans_gsettings = new QGSettings ( CONTROL_CENTER_PERSONALISE_GSETTINGS_ID , QByteArray ( ) , this ) ;
2021-04-26 15:06:47 +08:00
connect ( m_trans_gsettings , & QGSettings : : changed , this , [ = ] ( const QString & key ) {
if ( key = = TRANSPARENCY_KEY ) {
2021-01-04 14:21:45 +08:00
m_cache . remove ( TRANSPARENCY_KEY ) ;
2021-04-13 15:40:19 +08:00
m_cache . insert ( TRANSPARENCY_KEY , m_trans_gsettings - > get ( TRANSPARENCY_KEY ) . toDouble ( ) ) ;
2021-01-04 14:21:45 +08:00
qApp - > paletteChanged ( qApp - > palette ( ) ) ;
}
} ) ;
2021-01-20 15:33:49 +08:00
m_cache . remove ( TRANSPARENCY_KEY ) ;
2021-04-13 15:40:19 +08:00
m_cache . insert ( TRANSPARENCY_KEY , m_trans_gsettings - > get ( TRANSPARENCY_KEY ) . toDouble ( ) ) ;
}
2021-04-16 15:57:48 +08:00
m_cache . insert ( STYLE_NAME_KEY , " ukui-light " ) ;
m_cache . insert ( FONT_SIZE_KEY , 11 ) ;
2021-04-26 15:06:47 +08:00
if ( QGSettings : : isSchemaInstalled ( THEME_GSETTINGS_ID ) ) {
2021-04-13 15:40:19 +08:00
m_theme_gsettings = new QGSettings ( THEME_GSETTINGS_ID , QByteArray ( ) , this ) ;
2021-04-26 15:06:47 +08:00
connect ( m_theme_gsettings , & QGSettings : : changed , this , [ = ] ( const QString & key ) {
if ( key = = STYLE_NAME_KEY ) {
2021-04-13 15:40:19 +08:00
//当前主题改变时也发出paletteChanged信号, 通知主界面刷新
qApp - > paletteChanged ( qApp - > palette ( ) ) ;
2021-04-20 16:24:07 +08:00
m_cache . remove ( STYLE_NAME_KEY ) ;
m_cache . insert ( STYLE_NAME_KEY , m_theme_gsettings - > get ( STYLE_NAME_KEY ) . toString ( ) ) ;
2021-04-26 15:06:47 +08:00
} else if ( key = = FONT_SIZE_KEY ) {
2021-04-20 16:24:07 +08:00
qApp - > paletteChanged ( qApp - > palette ( ) ) ;
m_cache . remove ( FONT_SIZE_KEY ) ;
m_cache . insert ( FONT_SIZE_KEY , m_theme_gsettings - > get ( FONT_SIZE_KEY ) . toDouble ( ) ) ;
2021-04-13 15:40:19 +08:00
}
} ) ;
2021-04-20 16:24:07 +08:00
m_cache . remove ( STYLE_NAME_KEY ) ;
m_cache . insert ( STYLE_NAME_KEY , m_theme_gsettings - > get ( STYLE_NAME_KEY ) . toString ( ) ) ;
m_cache . remove ( FONT_SIZE_KEY ) ;
m_cache . insert ( FONT_SIZE_KEY , m_theme_gsettings - > get ( FONT_SIZE_KEY ) . toDouble ( ) ) ;
2021-01-04 14:21:45 +08:00
}
2021-01-03 16:58:26 +08:00
}
2021-04-26 15:06:47 +08:00
const QVariant GlobalSettings : : getValue ( const QString & key ) {
2021-01-03 16:58:26 +08:00
return m_cache . value ( key ) ;
}
2021-04-26 15:06:47 +08:00
bool GlobalSettings : : isExist ( const QString & key ) {
2021-01-03 16:58:26 +08:00
return ! m_cache . value ( key ) . isNull ( ) ;
}
2021-04-26 15:06:47 +08:00
void GlobalSettings : : reset ( const QString & key ) {
2021-01-03 16:58:26 +08:00
m_cache . remove ( key ) ;
2021-04-26 15:06:47 +08:00
QtConcurrent : : run ( [ = ] ( ) {
2021-02-25 14:40:44 +08:00
// if (m_mutex.tryLock(1000)) {
2021-04-26 15:06:47 +08:00
m_settings - > remove ( key ) ;
m_settings - > sync ( ) ;
2021-02-25 14:40:44 +08:00
// m_mutex.unlock();
// }
2021-01-03 16:58:26 +08:00
} ) ;
Q_EMIT this - > valueChanged ( key ) ;
}
2021-04-26 15:06:47 +08:00
void GlobalSettings : : resetAll ( ) {
2021-01-03 16:58:26 +08:00
QStringList tmp = m_cache . keys ( ) ;
m_cache . clear ( ) ;
2021-04-26 15:06:47 +08:00
for ( auto key : tmp ) {
2021-01-03 16:58:26 +08:00
Q_EMIT this - > valueChanged ( key ) ;
}
2021-04-26 15:06:47 +08:00
QtConcurrent : : run ( [ = ] ( ) {
if ( m_mutex . tryLock ( 1000 ) ) {
2021-01-03 16:58:26 +08:00
m_settings - > clear ( ) ;
m_settings - > sync ( ) ;
m_mutex . unlock ( ) ;
}
} ) ;
}
2020-12-28 20:29:13 +08:00
2021-04-26 15:06:47 +08:00
bool GlobalSettings : : setBlockDirs ( const QString & path , int & returnCode , bool remove ) {
if ( remove ) {
if ( path . isEmpty ( ) ) {
2021-02-04 20:01:21 +08:00
returnCode = PATH_EMPTY ;
2021-01-27 10:22:56 +08:00
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-04-26 15:06:47 +08:00
if ( ! path . startsWith ( " /home " ) ) {
2021-02-04 20:01:21 +08:00
// returnCode = 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!"));
returnCode = PATH_NOT_IN_HOME ;
2021-01-27 10:22:56 +08:00
return false ;
}
2021-01-12 16:07:50 +08:00
//why QSetting's key can't start with "/"??
2021-04-26 15:06:47 +08:00
QString pathKey = path . right ( path . length ( ) - 1 ) ;
2021-01-12 16:07:50 +08:00
2021-01-08 19:08:24 +08:00
QStringList blockDirs = m_block_dirs_settings - > allKeys ( ) ;
2021-04-26 15:06:47 +08:00
for ( QString i : blockDirs ) {
if ( pathKey . startsWith ( i ) ) {
2021-02-04 20:01:21 +08:00
// returnCode = QString(tr("My parent folder has been blocked!"));
returnCode = PATH_PARENT_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-04-26 15:06:47 +08:00
m_block_dirs_settings - > setValue ( pathKey , " 0 " ) ;
2021-01-08 19:08:24 +08:00
return true ;
}
2021-04-26 15:06:47 +08:00
QStringList GlobalSettings : : getBlockDirs ( ) {
2021-01-08 19:08:24 +08:00
return m_block_dirs_settings - > allKeys ( ) ;
2021-01-03 16:58:26 +08:00
}
2021-03-30 09:23:57 +08:00
//void GlobalSettings::appendCloudData(const QString &key, const QString &value)
//{
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if (values.contains(value)) {
// values.removeOne(value);
// }
// values.insert(0,value);
2021-03-20 15:37:54 +08:00
2021-03-30 09:23:57 +08:00
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, values);
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
//}
2021-03-20 15:37:54 +08:00
2021-04-26 15:06:47 +08:00
void GlobalSettings : : setSearchRecord ( const QString & word , const QDateTime & time ) {
2021-03-30 09:23:57 +08:00
QStringList keys = m_search_record_settings - > allKeys ( ) ;
if ( keys . contains ( QString ( QUrl : : toPercentEncoding ( word ) ) ) )
m_history . removeOne ( word ) ;
2021-04-26 15:06:47 +08:00
m_search_record_settings - > setValue ( QString ( QUrl : : toPercentEncoding ( word ) ) , time . toString ( " yyyy-MM-dd hh:mm:ss " ) ) ;
2021-03-30 09:23:57 +08:00
if ( keys . size ( ) > = 20 )
2021-04-26 15:06:47 +08:00
m_search_record_settings - > remove ( QString ( QUrl : : toPercentEncoding ( m_history . takeFirst ( ) ) ) ) ;
2021-03-30 09:23:57 +08:00
m_history . append ( word ) ;
2021-03-20 15:37:54 +08:00
}
2021-04-26 15:06:47 +08:00
QStringList GlobalSettings : : getSearchRecord ( ) {
2021-03-30 09:23:57 +08:00
return m_history ;
2021-03-20 15:37:54 +08:00
}
2021-03-30 09:23:57 +08:00
//bool GlobalSettings::removeOneCloudData(const QString &key, const QString &value)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return false;
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if (values.contains(value)) {
// values.removeOne(value);
// } else return false;
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, values);
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
// return true;
//}
//bool GlobalSettings::removeAllCloudData(const QString &key)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return false;
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// m_qSettings->beginGroup(key);
// m_qSettings->setValue(key, QStringList());
// m_qSettings->endGroup();
// if (m_qSettings) {
// delete m_qSettings;
// m_qSettings = NULL;
// }
// return true;
//}
//QStringList GlobalSettings::getCloudData(const QString &key)
//{
// if (!QFileInfo(CLOUD_FILE).isFile()) return QStringList();
// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat);
// m_qSettings->beginGroup(key);
// QStringList values = m_qSettings->value(key).toStringList();
// m_qSettings->endGroup();
// if(m_qSettings)
// delete m_qSettings;
// return values;
//}
2021-03-20 15:37:54 +08:00
2021-01-10 09:01:22 +08:00
//here should be override
//MouseZhangZh
2021-04-26 15:06:47 +08:00
void GlobalSettings : : setValue ( const QString & key , const QVariant & value ) {
2021-02-25 14:40:44 +08:00
// qDebug()<<"setvalue========"<<key<<":"<<value;
2021-01-03 16:58:26 +08:00
m_cache . insert ( key , value ) ;
2021-02-25 14:40:44 +08:00
// m_settings->sync();
2021-04-26 15:06:47 +08:00
QtConcurrent : : run ( [ = ] ( ) {
2021-02-25 14:40:44 +08:00
// qDebug()<<m_settings->status();
2021-04-20 16:24:07 +08:00
// if (m_mutex.tryLock(1000)) {
2021-02-25 14:40:44 +08:00
// m_mutex.lock();
m_settings - > setValue ( key , value ) ;
// qDebug()<<"setvalue========finish!!!"<<key<<":"<<value;
m_settings - > sync ( ) ;
// qDebug()<<"setvalue========sync!!!"<<key<<":"<<value;
2021-04-20 16:24:07 +08:00
// m_mutex.unlock();
// }
2021-01-03 16:58:26 +08:00
} ) ;
}
2021-04-26 15:06:47 +08:00
void GlobalSettings : : forceSync ( const QString & key ) {
2021-01-03 16:58:26 +08:00
m_settings - > sync ( ) ;
2021-04-26 15:06:47 +08:00
if ( key . isNull ( ) ) {
2021-01-03 16:58:26 +08:00
m_cache . clear ( ) ;
2021-04-26 15:06:47 +08:00
for ( auto key : m_settings - > allKeys ( ) ) {
2021-01-03 16:58:26 +08:00
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
}
2021-03-30 09:23:57 +08:00
2021-04-26 15:06:47 +08:00
void GlobalSettings : : updateSearchHistory ( QString key ) {
if ( key = = " search " ) {
2021-03-30 09:23:57 +08:00
m_search_record_settings - > sync ( ) ;
m_history . clear ( ) ;
2021-04-26 15:06:47 +08:00
for ( QString i : m_search_record_settings - > allKeys ( ) ) {
2021-03-30 09:23:57 +08:00
m_history . append ( QUrl : : fromPercentEncoding ( i . toLocal8Bit ( ) ) ) ;
}
}
}