0029748: Samples - Inspector tool - use recently opened files in TInspectorEXE
[occt.git] / tools / TInspector / TInspector_Preferences.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/TInspector_Preferences.hxx>
17
18 #include <Standard_WarningsDisable.hxx>
19 #include <QDir>
20 #include <QDomDocument>
21 #include <QDomElement>
22 #include <QDomNode>
23 #include <QFile>
24 #include <QTextStream>
25 #include <Standard_WarningsRestore.hxx>
26
27 // =======================================================================
28 // function : GetPreferences
29 // purpose :
30 // =======================================================================
31 void TInspector_Preferences::GetPreferences (const TCollection_AsciiString& thePluginName,
32                                              TInspectorAPI_PreferencesDataMap& theItem)
33 {
34   if (!myIsLoadedPreferences)
35   {
36     loadPreferences();
37     myIsLoadedPreferences = Standard_True;
38   }
39   myLoadedPreferences.Find (thePluginName, theItem);
40 }
41
42 // =======================================================================
43 // function : StorePreferences
44 // purpose :
45 // =======================================================================
46 void TInspector_Preferences::StorePreferences()
47 {
48   if (myLoadedPreferences.IsEmpty())
49     return;
50
51   QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
52   QFile aFile(aFileName);
53   if (!aFile.open (QFile::WriteOnly))
54     return;
55
56   QDomDocument aDomDocument (documentKey());
57   QDomComment aComment = aDomDocument.createComment("\nThis file is automatically created by TInspector application.\nChanges made in this file can be lost.\n");
58   aDomDocument.appendChild (aComment);
59   QDomElement aRootElement = aDomDocument.createElement (documentKey());
60   aDomDocument.appendChild (aRootElement);
61
62   for (NCollection_DataMap<TCollection_AsciiString, TInspectorAPI_PreferencesDataMap>::Iterator aPrefsIt (myLoadedPreferences);
63     aPrefsIt.More(); aPrefsIt.Next())
64   {
65     QDomElement aPluginElement = aDomDocument.createElement (pluginKey());
66     aPluginElement.setAttribute (nameKey(), aPrefsIt.Key().ToCString());
67     aRootElement.appendChild (aPluginElement);
68
69     const TInspectorAPI_PreferencesDataMap& aPluginMap = aPrefsIt.Value();
70     for (TInspectorAPI_IteratorOfPreferencesDataMap aPluginPrefsIt (aPluginMap); aPluginPrefsIt.More(); aPluginPrefsIt.Next())
71     {
72       QDomElement aParameterElement = aDomDocument.createElement (parameterKey());
73       aParameterElement.setAttribute (nameKey(), aPluginPrefsIt.Key().ToCString());
74       aParameterElement.setAttribute (valueKey(), aPluginPrefsIt.Value().ToCString());
75       aPluginElement.appendChild (aParameterElement);
76     }
77   }
78
79   QTextStream aTextStream (&aFile);
80   QStringList aDocumentStr = aDomDocument.toString().split ("\n");
81   for (QStringList::ConstIterator aContentIt = aDocumentStr.begin(); aContentIt != aDocumentStr.end(); ++aContentIt)
82     aTextStream << *aContentIt << endl;
83   aFile.close();
84 }
85
86 // =======================================================================
87 // function : RemovePreferences
88 // purpose :
89 // =======================================================================
90 void TInspector_Preferences::RemovePreferences()
91 {
92   QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
93   QDir aDir (GetDirectory().ToCString());
94   if (aDir.exists (aFileName))
95     aDir.remove (aFileName);
96   reset();
97 }
98
99 // =======================================================================
100 // function : loadPreferences
101 // purpose :
102 // =======================================================================
103 void TInspector_Preferences::loadPreferences()
104 {
105   QString aFileName = QString (GetDirectory().ToCString()) + QDir::separator() + PreferencesFileName();
106   QFile aFile (aFileName);
107   if (!aFile.open (QFile::ReadOnly))
108     return;
109
110   QDomDocument aDomDocument;
111   bool aResult = aDomDocument.setContent (&aFile);
112   aFile.close();
113   if (!aResult)
114     return;
115
116   QDomElement aRootElement = aDomDocument.documentElement();
117   if (aRootElement.isNull() || aRootElement.tagName() != documentKey())
118     return;
119
120   QDomNode aPluginNode = aRootElement.firstChild();
121   while (!aPluginNode.isNull())
122   {
123     if (aPluginNode.isElement())
124     {
125       QDomElement aPluginElement = aPluginNode.toElement();
126       if (aPluginElement.tagName() == pluginKey() && aPluginElement.hasAttribute (nameKey()))
127       {
128         TInspectorAPI_PreferencesDataMap anItem;
129         readPluginItem (aPluginElement, anItem);
130         myLoadedPreferences.Bind (aPluginElement.attribute (nameKey()).toStdString().c_str(), anItem);
131       }
132     }
133     aPluginNode = aPluginNode.nextSibling();
134   }
135 }
136
137 // =======================================================================
138 // function : readPluginItem
139 // purpose :
140 // =======================================================================
141 void TInspector_Preferences::readPluginItem (const QDomElement thePluginElement, TInspectorAPI_PreferencesDataMap& theItem)
142 {
143   QDomNode aParameterNode = thePluginElement.firstChild();
144   while (!aParameterNode.isNull())
145   {
146     if (aParameterNode.isElement())
147     {
148       QDomElement aParameterElement = aParameterNode.toElement();
149       if (aParameterElement.tagName() == parameterKey() &&
150           aParameterElement.hasAttribute (nameKey()) && aParameterElement.hasAttribute (valueKey()))
151       {
152         theItem.Bind (aParameterElement.attribute (nameKey()).toStdString().c_str(),
153                       aParameterElement.attribute (valueKey()).toStdString().c_str());
154       }
155     }
156     aParameterNode = aParameterNode.nextSibling();
157   }
158 }