Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_AttributeStudyProperties_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEDS_AttributeStudyProperties_i.cxx
8 //  Author : Yves FRICAUD
9 //  Module : SALOME
10 //  $Header$
11
12 #include <TColStd_HSequenceOfExtendedString.hxx>
13 #include <TColStd_HSequenceOfInteger.hxx>
14 #include <TCollection_ExtendedString.hxx>
15 #include <TCollection_AsciiString.hxx>
16
17 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
18
19 #define CREATION_MODE_NOTDEFINED 0
20 #define CREATION_MODE_SCRATCH 1
21 #define CREATION_MODE_COPY 2
22
23 using namespace std;
24
25 void SALOMEDS_AttributeStudyProperties_i::SetUserName(const char* theName) {
26   CheckLocked();
27   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
28   aProp->SetFirstName(const_cast<char*>(theName));
29 }
30
31 char* SALOMEDS_AttributeStudyProperties_i::GetUserName() {
32   TCollection_ExtendedString S = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->GetCreatorName();
33   CORBA::String_var c_s = CORBA::string_dup(TCollection_AsciiString(S).ToCString());
34   return c_s._retn();
35 }
36
37 void SALOMEDS_AttributeStudyProperties_i::SetCreationDate(CORBA::Long theMinute,
38                                                           CORBA::Long theHour,
39                                                           CORBA::Long theDay,
40                                                           CORBA::Long theMonth,
41                                                           CORBA::Long theYear) {
42   CheckLocked();
43   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
44   int aTmp;
45   if (aProp->GetCreationDate(aTmp, aTmp, aTmp, aTmp, aTmp)) return;
46   aProp->SetModificationDate(theMinute, theHour, theDay, theMonth, theYear);
47 }
48
49 CORBA::Boolean SALOMEDS_AttributeStudyProperties_i::GetCreationDate(CORBA::Long& theMinute,
50                                                                     CORBA::Long& theHour,
51                                                                     CORBA::Long& theDay,
52                                                                     CORBA::Long& theMonth,
53                                                                     CORBA::Long& theYear) {
54   Standard_Integer aMinute;
55   Standard_Integer aHour;
56   Standard_Integer aDay;
57   Standard_Integer aMonth;
58   Standard_Integer aYear;
59   if (Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->GetCreationDate(aMinute, aHour, aDay,
60                                                                                     aMonth, aYear)) {
61     theMinute = aMinute;
62     theHour = aHour;
63     theDay = aDay;
64     theMonth = aMonth;
65     theYear = aYear;
66     return Standard_True;
67   }
68   return Standard_False;
69 }
70
71 void SALOMEDS_AttributeStudyProperties_i::SetCreationMode(const char* theMode) {
72   CheckLocked();
73   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
74   if (strcmp(theMode,"from scratch")==0) aProp->SetCreationMode(CREATION_MODE_SCRATCH);
75   else if (strcmp(theMode,"copy from")==0) aProp->SetCreationMode(CREATION_MODE_COPY);
76   else aProp->SetCreationMode(CREATION_MODE_NOTDEFINED);
77 }
78
79 char* SALOMEDS_AttributeStudyProperties_i::GetCreationMode() {
80   CORBA::String_var c_s;
81   switch (Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->GetCreationMode()) {
82   case CREATION_MODE_SCRATCH: c_s = "from scratch"; break;
83   case CREATION_MODE_COPY: c_s = "copy from"; break;
84   default: c_s = "";
85   }
86   return c_s._retn();
87 }
88
89 void SALOMEDS_AttributeStudyProperties_i::SetModified(CORBA::Long theModified) {
90   Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->SetModified(theModified);
91 }
92
93 CORBA::Boolean SALOMEDS_AttributeStudyProperties_i::IsModified() {
94   return Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->IsModified();
95 }
96
97 CORBA::Long SALOMEDS_AttributeStudyProperties_i::GetModified() {
98   return Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->GetModified();
99 }
100
101 void SALOMEDS_AttributeStudyProperties_i::SetLocked(CORBA::Boolean theLocked) {
102   Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->SetLocked(theLocked);
103 }
104
105 CORBA::Boolean SALOMEDS_AttributeStudyProperties_i::IsLocked() {
106   return Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr)->IsLocked();
107 }
108
109 void SALOMEDS_AttributeStudyProperties_i::SetModification(const char* theName,
110                                                           CORBA::Long theMinute,
111                                                           CORBA::Long theHour,
112                                                           CORBA::Long theDay,
113                                                           CORBA::Long theMonth,
114                                                           CORBA::Long theYear) {
115   CheckLocked();
116   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
117   aProp->SetUserName(const_cast<char*>(theName));
118   aProp->SetModificationDate((int)theMinute, (int)theHour, (int)theDay, (int)theMonth, (int)theYear);
119 }
120 void SALOMEDS_AttributeStudyProperties_i::GetModificationsList(SALOMEDS::StringSeq_out theNames,
121                                                                SALOMEDS::LongSeq_out theMinutes,
122                                                                SALOMEDS::LongSeq_out theHours,
123                                                                SALOMEDS::LongSeq_out theDays,
124                                                                SALOMEDS::LongSeq_out theMonths,
125                                                                SALOMEDS::LongSeq_out theYears,
126                                                                CORBA::Boolean theWithCreator) {
127   Handle(TColStd_HSequenceOfExtendedString) aNames;
128   Handle(TColStd_HSequenceOfInteger) aMinutes, aHours, aDays, aMonths, aYears;
129   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
130   aNames = aProp->GetUserNames();
131   aProp->GetModificationDates(aMinutes, aHours, aDays, aMonths, aYears);
132   int aLength = aNames->Length()-((theWithCreator)?0:1);
133   theNames = new SALOMEDS::StringSeq;
134 //   SALOMEDS::LongSeq *maminute = new SALOMEDS::LongSeq;
135 //   maminute->length(5);
136   theMinutes = new SALOMEDS::LongSeq;
137   theHours = new SALOMEDS::LongSeq;
138   theDays = new SALOMEDS::LongSeq;
139   theMonths = new SALOMEDS::LongSeq;
140   theYears = new SALOMEDS::LongSeq;
141   theNames->length(aLength);
142   theMinutes->length(aLength);
143   theHours->length(aLength);
144   theDays->length(aLength);
145   theMonths->length(aLength);
146   theYears->length(aLength);
147   int a;
148   for(a = 0; a < aLength; a++) 
149     {
150       (*theNames)[a] = CORBA::string_dup(TCollection_AsciiString(aNames->Value(a + 1 + ((theWithCreator)?0:1))).ToCString());
151       (*theMinutes)[a] = aMinutes->Value(a + 1 + ((theWithCreator)?0:1));
152       (*theHours)[a] = aHours->Value(a + 1 + ((theWithCreator)?0:1));
153       (*theDays)[a] = aDays->Value(a + 1 + ((theWithCreator)?0:1));
154       (*theMonths)[a] = aMonths->Value(a + 1 + ((theWithCreator)?0:1));
155       (*theYears)[a] = aYears->Value(a + 1 + ((theWithCreator)?0:1));
156     }
157 }
158
159 char* SALOMEDS_AttributeStudyProperties_i::Store() {
160   Handle(TColStd_HSequenceOfExtendedString) aNames;
161   Handle(TColStd_HSequenceOfInteger) aMinutes, aHours, aDays, aMonths, aYears;
162   Handle(SALOMEDS_StudyPropertiesAttribute) aProp = Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(_myAttr);
163   aNames = aProp->GetUserNames();
164   aProp->GetModificationDates(aMinutes, aHours, aDays, aMonths, aYears);
165
166   int aLength, anIndex;
167   for(aLength = 0, anIndex = aNames->Length(); anIndex > 0; anIndex--) aLength += aNames->Value(anIndex).Length() + 1;
168
169   char* aProperty = new char[3 + aLength + 12 * aNames->Length()];
170
171   sprintf(aProperty,"%c%c", strlen(GetCreationMode())?GetCreationMode()[0]:'0', IsLocked()?'l':'u');
172
173   aLength = aNames->Length();
174   int a = 2;
175   for(anIndex = 1; anIndex  <= aLength; anIndex++) {
176     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
177             (int)(aMinutes->Value(anIndex)),
178             (int)(aHours->Value(anIndex)),
179             (int)(aDays->Value(anIndex)),
180             (int)(aMonths->Value(anIndex)),
181             (int)(aYears->Value(anIndex)),
182             (char*)(TCollection_AsciiString(aNames->Value(anIndex)).ToCString()));
183     a = strlen(aProperty);
184     aProperty[a++] = 1;
185   }
186   aProperty[a] = 0;
187   return aProperty;
188 }
189
190 void SALOMEDS_AttributeStudyProperties_i::Restore(const char* value) {
191   char* aCopy = strdup(value);
192   if (aCopy[0] == 'f') SetCreationMode("from scratch");
193   else if (aCopy[0] == 'c') SetCreationMode("copy from");
194   else SetCreationMode("none");
195
196   int anIndex;
197   for(anIndex = 2; anIndex + 2 < strlen(value) ;) {
198     char str[10];
199     Standard_Integer aMinute, aHour, aDay, aMonth, aYear;
200     str[0] = aCopy[anIndex++];
201     str[1] = aCopy[anIndex++];
202     str[2] = 0;
203     aMinute = atoi(str);
204     str[0] = aCopy[anIndex++];
205     str[1] = aCopy[anIndex++];
206     aHour =  atoi(str);
207     str[0] = aCopy[anIndex++];
208     str[1] = aCopy[anIndex++];
209     aDay =  atoi(str);
210     str[0] = aCopy[anIndex++];
211     str[1] = aCopy[anIndex++];
212     aMonth =  atoi(str);
213     str[0] = aCopy[anIndex++];
214     str[1] = aCopy[anIndex++];
215     str[2] = aCopy[anIndex++];
216     str[3] = aCopy[anIndex++];
217     str[4] = 0;
218     aYear = atoi(str);
219     
220     int aNameSize;
221     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
222     char *aName = new char[aNameSize+1];
223     strncpy(aName, &(aCopy[anIndex]), aNameSize);
224     aName[aNameSize] = 0;
225     SetModification(aName, aMinute,aHour,aDay,aMonth,aYear);
226     delete(aName);
227     anIndex += aNameSize + 1;
228   }
229   if (aCopy[1] == 'l') {
230     SetLocked(Standard_True);
231   }
232   SetModified(0);
233   free(aCopy);
234 }