Salome HOME
Update from BR_V5_DEV 13feb09
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOMEDSImpl_AttributeStudyProperties.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
27 #include <string.h>
28
29 using namespace std;
30
31
32 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
33 {
34   static std::string SALOMEDSImpl_AttributeStudyPropertiesID ("128371A2-8F52-11d6-A8A3-0001021E8C7F");
35   return SALOMEDSImpl_AttributeStudyPropertiesID;
36 }
37
38 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_AttributeStudyProperties::Set(const DF_Label& label)
39 {
40   SALOMEDSImpl_AttributeStudyProperties* A = NULL;
41   if (!(A=(SALOMEDSImpl_AttributeStudyProperties*)label.FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID()))) {
42     A = new SALOMEDSImpl_AttributeStudyProperties();
43     label.AddAttribute(A);
44   }
45   return A;
46 }
47
48 SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
49 :SALOMEDSImpl_GenericAttribute("AttributeStudyProperties")
50 {
51   myLocked = false;
52   myLockChanged = false;
53   Init();
54 }
55
56 void SALOMEDSImpl_AttributeStudyProperties::Init()
57 {
58   myUserName.clear();
59   myMinute.clear();
60   myHour.clear();
61   myDay.clear();
62   myMonth.clear();
63   myYear.clear();
64   myMode = 0; // none
65 }
66
67 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
68                                                             const int            theMinute,
69                                                             const int            theHour,
70                                                             const int            theDay,
71                                                             const int            theMonth,
72                                                             const int            theYear)
73 {
74   if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
75       theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
76     return;
77
78   CheckLocked();
79   Backup();
80
81   myUserName.push_back(theUserName);
82   myMinute.push_back(theMinute);
83   myHour.push_back(theHour);
84   myDay.push_back(theDay);
85   myMonth.push_back(theMonth);
86   myYear.push_back(theYear);
87 }
88
89 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
90                   (vector<string>& theUserNames,
91                    vector<int>&    theMinutes,
92                    vector<int>&    theHours,
93                    vector<int>&    theDays,
94                    vector<int>&    theMonths,
95                    vector<int>&    theYears) const
96 {
97   theUserNames = myUserName;
98   theMinutes = myMinute;
99   theHours = myHour;
100   theDays = myDay;
101   theMonths = myMonth;
102   theYears = myYear;
103 }
104
105 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
106 {
107   if (myUserName.size() == 0)
108     return std::string("");
109   return myUserName[0];
110 }
111
112 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
113                               (int&           theMinute,
114                                int&           theHour,
115                                int&           theDay,
116                                int&           theMonth,
117                                int&           theYear) const
118 {
119   if (myMinute.size() != 0) {
120     theMinute = myMinute[0];
121     theHour = myHour[0];
122     theDay = myDay[0];
123     theMonth = myMonth[0];
124     theYear = myYear[0];
125     return true;
126   }
127   return false;
128 }
129
130 void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string& theName)
131 {
132   if (myUserName.size() > 0) {
133     CheckLocked();
134     Backup();
135     myUserName[0] = theName;
136   }
137 }
138
139 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
140 {
141   CheckLocked();
142   Backup();
143   myMode = theMode;
144 }
145
146 int SALOMEDSImpl_AttributeStudyProperties::GetCreationMode() const
147 {
148   return myMode;
149 }
150
151 void SALOMEDSImpl_AttributeStudyProperties::SetModified(const int theModified)
152 {
153   myModified = theModified;
154 }
155
156 bool SALOMEDSImpl_AttributeStudyProperties::IsModified() const
157 {
158   return (myModified != 0);
159 }
160
161 int SALOMEDSImpl_AttributeStudyProperties::GetModified() const
162 {
163   return myModified;
164 }
165
166 void SALOMEDSImpl_AttributeStudyProperties::SetLocked(const bool theLocked)
167 {
168 //  Backup();
169   if (myLocked != theLocked) {
170     myLockChanged = true;
171     myLocked = theLocked;
172   }
173 }
174
175 bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
176 {
177   return myLocked;
178 }
179
180 bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase) {
181   if (!myLockChanged) return false;
182   if (theErase) myLockChanged = false;
183   return true;
184 }
185
186 const std::string& SALOMEDSImpl_AttributeStudyProperties::ID() const
187 {
188   return GetID();
189 }
190
191 void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
192 {
193   SALOMEDSImpl_AttributeStudyProperties* aProp =
194     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
195
196   Init();
197   vector<string> aNames;
198   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
199   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
200   for (int i = 0, len = aNames.size(); i < len; i++) {
201     myUserName.push_back(aNames[i]);
202     myMinute.push_back(aMinutes[i]);
203     myHour.push_back(aHours[i]);
204     myDay.push_back(aDays[i]);
205     myMonth.push_back(aMonths[i]);
206     myYear.push_back(aYears[i]);
207   }
208   myMode = aProp->GetCreationMode();
209 //  myModified = aProp->GetModified();
210 //  myLocked = aProp->IsLocked();
211 }
212
213 DF_Attribute* SALOMEDSImpl_AttributeStudyProperties::NewEmpty() const
214 {
215   return new SALOMEDSImpl_AttributeStudyProperties();
216 }
217
218 void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
219 {
220   SALOMEDSImpl_AttributeStudyProperties* aProp =
221     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(into);
222   aProp->Init();
223
224   int i;
225   for(i = 0; i < myUserName.size(); i++) {
226     aProp->SetModification(myUserName[i],
227                            myMinute[i], myHour[i],
228                            myDay[i], myMonth[i], myYear[i]);
229   }
230
231   aProp->SetCreationMode(myMode);
232 //  aProp->SetModified(myModified);
233 //  aProp->SetLocked(myLocked);
234 }
235
236
237 string SALOMEDSImpl_AttributeStudyProperties::Save()
238 {
239   vector<string> aNames;
240   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
241   GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
242
243   int aLength, anIndex;
244   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
245     aLength += aNames[anIndex].size() + 1;
246
247   char* aProperty = new char[3 + aLength + 12 * aNames.size()];
248
249   char crMode = (char)GetCreationMode();
250
251   sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
252
253   aLength = aNames.size();
254   int a = 2;
255   for (anIndex = 0; anIndex  < aLength; anIndex++) {
256     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
257             (int)(aMinutes[anIndex]),
258             (int)(aHours[anIndex]),
259             (int)(aDays[anIndex]),
260             (int)(aMonths[anIndex]),
261             (int)(aYears[anIndex]),
262             (char*)(aNames[anIndex].c_str()));
263     a = strlen(aProperty);
264     aProperty[a++] = 1;
265   }
266   aProperty[a] = 0;
267   string prop(aProperty);
268   delete aProperty;
269
270   return prop;
271 }
272
273 void SALOMEDSImpl_AttributeStudyProperties::Load(const string& value)
274 {
275   char* aCopy = (char*)value.c_str();
276
277   int crMode = (int)aCopy[0];
278   SetCreationMode(crMode);
279
280   int anIndex;
281   for (anIndex = 2; anIndex + 2 < value.size() ;) {
282     char str[10];
283     int aMinute, aHour, aDay, aMonth, aYear;
284     str[0] = aCopy[anIndex++];
285     str[1] = aCopy[anIndex++];
286     str[2] = 0;
287     aMinute = atoi(str);
288     str[0] = aCopy[anIndex++];
289     str[1] = aCopy[anIndex++];
290     aHour =  atoi(str);
291     str[0] = aCopy[anIndex++];
292     str[1] = aCopy[anIndex++];
293     aDay =  atoi(str);
294     str[0] = aCopy[anIndex++];
295     str[1] = aCopy[anIndex++];
296     aMonth =  atoi(str);
297     str[0] = aCopy[anIndex++];
298     str[1] = aCopy[anIndex++];
299     str[2] = aCopy[anIndex++];
300     str[3] = aCopy[anIndex++];
301     str[4] = 0;
302     aYear = atoi(str);
303
304     int aNameSize;
305     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
306     char *aName = new char[aNameSize+1];
307     strncpy(aName, &(aCopy[anIndex]), aNameSize);
308     aName[aNameSize] = 0;
309     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
310     delete [] (aName);
311     anIndex += aNameSize + 1;
312   }
313   if (aCopy[1] == 'l') {
314     SetLocked(true);
315   }
316   SetModified(0);
317 }