22
33#include < QFile>
44#include < QDir>
5- #include < QDomDocument>
65#include < QUuid>
76#include < QTextStream>
87
@@ -12,10 +11,9 @@ mbCoreFileManager::Strings::Strings() :
1211 dirname_mbtools (QStringLiteral(" .mbtools" )),
1312 dirname_project (QStringLiteral(" project" )),
1413 dirname_temporary(QStringLiteral(" tmp" )),
15- filename_projects(QStringLiteral(" projects.xml " )),
14+ filename_projects(QStringLiteral(" projects.conf " )),
1615 filename_files (QStringLiteral(" files.xml" )),
17- dom_projects (QStringLiteral(" projects" )),
18- dom_project (QStringLiteral(" project" ))
16+ id_path (QStringLiteral(" path" ))
1917{
2018}
2119
@@ -29,6 +27,7 @@ mbCoreFileManager::mbCoreFileManager(mbCore *core, QObject *parent) : QObject{pa
2927 m_core (core)
3028{
3129 m_project = nullptr ;
30+ m_settings = nullptr ;
3231
3332 checkAndCreateHiddenFolder ();
3433 connect (m_core, &mbCore::projectChanged, this , &mbCoreFileManager::setProject);
@@ -54,11 +53,11 @@ bool mbCoreFileManager::getOrCreateHiddenFile(const QString &fileName, QFile &fi
5453{
5554 QString sFileName = m_currentProjectDir.filePath (fileName);
5655 file.setFileName (sFileName );
56+ if (m_project->isModified ())
57+ return false ;
5758 QFileInfo info (file);
58- if (m_project->isModified () || !file.exists () || (info.lastModified () < m_project->fileModified ()))
59- {
59+ if (!file.exists () || (info.lastModified () < m_project->fileModified ()))
6060 return file.open (QIODevice::WriteOnly);
61- }
6261 return file.open (mode);
6362}
6463
@@ -164,83 +163,37 @@ QList<mbCoreFileManager::ProjectInfo*> mbCoreFileManager::parseProjects()
164163{
165164 const Strings &s = Strings::instance ();
166165 QList<ProjectInfo*> projects;
167-
168- QString filePath = m_hiddenProjectsDir.absoluteFilePath (s.filename_projects );
169- QFile file (filePath);
170- if (!file.open (QIODevice::ReadOnly | QIODevice::Text))
166+ QSettings *conf = getQSettings ();
167+ Q_FOREACH (const QString &id, conf->childGroups ())
171168 {
172- // qWarning() << "Failed to open file:" << filePath;
173- return projects;
174- }
175-
176- QDomDocument doc;
177- if (!doc.setContent (&file))
178- {
179- // qWarning() << "Failed to parse XML file.";
180- file.close ();
181- return projects;
182- }
183- file.close ();
184-
185- QDomElement root = doc.documentElement ();
186- if (root.tagName () != s.dom_projects )
187- {
188- // qWarning() << "Invalid root element in XML.";
189- return projects;
190- }
191-
192- QDomNodeList projectNodes = root.elementsByTagName (s.dom_project );
193- for (int i = 0 ; i < projectNodes.size (); ++i)
194- {
195- QDomElement projectElement = projectNodes.at (i).toElement ();
196- if (!projectElement.isNull ())
169+ conf->beginGroup (id);
170+ QString absPath = conf->value (s.id_path ).toString ();
171+ conf->endGroup ();
172+ if (!id.isEmpty () && !absPath.isEmpty ())
197173 {
198- QString id = projectElement.attribute (" dir" );
199- QString absPath = projectElement.text ();
200- if (!id.isEmpty () && !absPath.isEmpty ())
201- {
202- ProjectInfo *info = new ProjectInfo;
203- info->id = id;
204- info->absPath = absPath;
205- projects.append (info);
206- }
174+ ProjectInfo *info = new ProjectInfo;
175+ info->id = id;
176+ info->absPath = absPath;
177+ projects.append (info);
207178 }
208179 }
209-
210180 return projects;
211181}
212182
213183bool mbCoreFileManager::saveProjects (const QList<ProjectInfo*> &projects)
214184{
215185 const Strings &s = Strings::instance ();
216- QString filePath = m_hiddenProjectsDir.absoluteFilePath (s.filename_projects );
217- QDomDocument doc;
218-
219- QDomElement root = doc.createElement (s.dom_projects );
220- doc.appendChild (root);
221-
222- Q_FOREACH (ProjectInfo *info, projects)
223- {
224- QDomElement projectElement = doc.createElement (s.dom_project );
225- projectElement.setAttribute (" dir" , info->id );
226- QDomText pathText = doc.createTextNode (info->absPath );
227- projectElement.appendChild (pathText);
228- root.appendChild (projectElement);
229- }
230-
231- QFile file (filePath);
232- if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
186+ QSettings *conf = getQSettings ();
187+ conf->clear ();
188+ Q_FOREACH (const ProjectInfo *info, projects)
233189 {
234- // qWarning() << "Failed to open file for writing:" << filePath;
235- return false ;
190+ conf->beginGroup (info->id );
191+ conf->setValue (s.id_path , info->absPath );
192+ conf->endGroup ();
236193 }
237194
238- QTextStream stream (&file);
239- stream.setCodec (" UTF-8" );
240- doc.save (stream, 4 );
241- file.close ();
242-
243- return true ;}
195+ return true ;
196+ }
244197
245198void mbCoreFileManager::cleanupProjects (QList<ProjectInfo*> &projects)
246199{
@@ -269,3 +222,15 @@ void mbCoreFileManager::addProjectInfo(ProjectInfo *info)
269222 m_projects.append (info);
270223 m_hashProjects.insert (info->absPath , info);
271224}
225+
226+ QSettings *mbCoreFileManager::getQSettings ()
227+ {
228+ if (!m_settings)
229+ {
230+ const Strings &s = Strings::instance ();
231+ QString filePath = m_hiddenProjectsDir.absoluteFilePath (s.filename_projects );
232+ m_settings = new QSettings (filePath, QSettings::IniFormat, this );
233+ m_settings->setIniCodec (" UTF-8" );
234+ }
235+ return m_settings;
236+ }
0 commit comments