1 /* Copyright 2013-2019 MultiMC Contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "INISettingsObject.h"
17 #include "Setting.h"
18 
INISettingsObject(const QString & path,QObject * parent)19 INISettingsObject::INISettingsObject(const QString &path, QObject *parent)
20     : SettingsObject(parent)
21 {
22     m_filePath = path;
23     m_ini.loadFile(path);
24 }
25 
setFilePath(const QString & filePath)26 void INISettingsObject::setFilePath(const QString &filePath)
27 {
28     m_filePath = filePath;
29 }
30 
reload()31 bool INISettingsObject::reload()
32 {
33     return m_ini.loadFile(m_filePath) && SettingsObject::reload();
34 }
35 
suspendSave()36 void INISettingsObject::suspendSave()
37 {
38     m_suspendSave = true;
39 }
40 
resumeSave()41 void INISettingsObject::resumeSave()
42 {
43     m_suspendSave = false;
44     if(m_doSave)
45     {
46         m_ini.saveFile(m_filePath);
47     }
48 }
49 
changeSetting(const Setting & setting,QVariant value)50 void INISettingsObject::changeSetting(const Setting &setting, QVariant value)
51 {
52     if (contains(setting.id()))
53     {
54         // valid value -> set the main config, remove all the sysnonyms
55         if (value.isValid())
56         {
57             auto list = setting.configKeys();
58             m_ini.set(list.takeFirst(), value);
59             for(auto iter: list)
60                 m_ini.remove(iter);
61         }
62         // invalid -> remove all (just like resetSetting)
63         else
64         {
65             for(auto iter: setting.configKeys())
66                 m_ini.remove(iter);
67         }
68         doSave();
69     }
70 }
71 
doSave()72 void INISettingsObject::doSave()
73 {
74     if(m_suspendSave)
75     {
76         m_doSave = true;
77     }
78     else
79     {
80         m_ini.saveFile(m_filePath);
81     }
82 }
83 
resetSetting(const Setting & setting)84 void INISettingsObject::resetSetting(const Setting &setting)
85 {
86     // if we have the setting, remove all the synonyms. ALL OF THEM
87     if (contains(setting.id()))
88     {
89         for(auto iter: setting.configKeys())
90             m_ini.remove(iter);
91         doSave();
92     }
93 }
94 
retrieveValue(const Setting & setting)95 QVariant INISettingsObject::retrieveValue(const Setting &setting)
96 {
97     // if we have the setting, return value of the first matching synonym
98     if (contains(setting.id()))
99     {
100         for(auto iter: setting.configKeys())
101         {
102             if(m_ini.contains(iter))
103                 return m_ini[iter];
104         }
105     }
106     return QVariant();
107 }
108