fix:Rewrite configuration files to static functions

This commit is contained in:
sunfengsheng 2021-01-07 20:07:29 +08:00
parent f8f0a8d232
commit 736c22b1b2
2 changed files with 32 additions and 39 deletions

View File

@ -1,19 +1,7 @@
#include "config-file.h" #include "config-file.h"
ConfigFile::ConfigFile(QObject *parent) : QObject(parent)
{
QFile file(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf");
if(!file.exists()){
file.open( QIODevice::ReadWrite | QIODevice::Text );
file.close();
}
m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
receiveMessage("ukui-panel");//测试使用
receiveMessage("ukui-panel.desktop");//测试使用
readConfig();//页面调用
}
void ConfigFile::writeCommonly(QString message){ void ConfigFile::writeCommonly(QString message){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
QStringList messagelist=message.split("/"); QStringList messagelist=message.split("/");
QString appname=messagelist.last(); QString appname=messagelist.last();
if(!appname.contains("desktop")) if(!appname.contains("desktop"))
@ -29,6 +17,7 @@ void ConfigFile::writeCommonly(QString message){
} }
QStringList ConfigFile::readCommonly(){ QStringList ConfigFile::readCommonly(){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
QStringList returnlist; QStringList returnlist;
QMap<QString,int> quicklycount; QMap<QString,int> quicklycount;
m_qSettings->beginGroup("Commonly"); m_qSettings->beginGroup("Commonly");
@ -50,42 +39,57 @@ QStringList ConfigFile::readCommonly(){
for(int j=0;j<vec.size();j++){ for(int j=0;j<vec.size();j++){
returnlist.append(vec.at(j).first); returnlist.append(vec.at(j).first);
} }
// qDebug()<<returnlist; if(m_qSettings)
delete m_qSettings;
return returnlist; return returnlist;
} }
void ConfigFile::writeRecently(QString message){ void ConfigFile::writeRecently(QString message){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
m_qSettings->beginGroup("Recently"); m_qSettings->beginGroup("Recently");
QStringList recently=m_qSettings->value("Recently").toStringList(); QStringList recently=m_qSettings->value("Recently").toStringList();
m_qSettings->endGroup(); m_qSettings->endGroup();
if(recently.contains(message)){
recently.removeOne(message);
}
recently.insert(0,message); recently.insert(0,message);
m_qSettings->beginGroup("Recently"); m_qSettings->beginGroup("Recently");
m_qSettings->setValue("Recently",recently); m_qSettings->setValue("Recently",recently);
m_qSettings->endGroup(); m_qSettings->endGroup();
if(m_qSettings)
delete m_qSettings;
} }
QStringList ConfigFile::readRecently(){ QStringList ConfigFile::readRecently(){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
m_qSettings->beginGroup("Recently"); m_qSettings->beginGroup("Recently");
QStringList recently=m_qSettings->value("Recently").toStringList(); QStringList recently=m_qSettings->value("Recently").toStringList();
m_qSettings->endGroup(); m_qSettings->endGroup();
// qDebug()<<recently; if(m_qSettings)
delete m_qSettings;
return recently; return recently;
} }
void ConfigFile::writeConfig(){ void ConfigFile::writeConfig(QString message){
writeCommonly(m_message); writeCommonly(message);
writeRecently(m_message); writeRecently(message);
} }
QMap<QString,QStringList> ConfigFile::readConfig(){ QMap<QString,QStringList> ConfigFile::readConfig(){
QMap<QString,QStringList> returnresult; QMap<QString,QStringList> returnresult;
returnresult.insert("Commonly",readCommonly()); returnresult.insert("Commonly",readCommonly());
returnresult.insert("Recently",readRecently()); returnresult.insert("Recently",readRecently());
// qDebug()<<returnresult;
return returnresult; return returnresult;
} }
void ConfigFile::receiveMessage(QString message){ void ConfigFile::receiveMessage(QString message){
m_message=message; QFile file(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf");
writeConfig(); if(!file.exists()){
file.open( QIODevice::ReadWrite | QIODevice::Text );
file.close();
}
readConfig();//页面调用
writeConfig(message);
} }

View File

@ -10,25 +10,14 @@ class ConfigFile : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ConfigFile(QObject *parent = nullptr); static void writeConfig(QString message);
void writeConfig(); static QMap<QString,QStringList> readConfig();
QMap<QString,QStringList> readConfig(); static void receiveMessage(QString message);
private: private:
QSettings *m_qSettings; static void writeCommonly(QString message);
QString m_message; static QStringList readCommonly();
static void writeRecently(QString message);
private: static QStringList readRecently();
void writeCommonly(QString message);
QStringList readCommonly();
void writeRecently(QString message);
QStringList readRecently();
public Q_SLOTS:
void receiveMessage(QString message);
Q_SIGNALS:
}; };