0022573: Extend the range of status values returned by the method TDocStd_Application...
[occt.git] / src / TDocStd / TDocStd_Application.cxx
CommitLineData
7fd59977 1// File: TDocStd_Application.cxx
2// Created: Wed Jun 30 12:40:45 1999
3// Author: Denis PASCAL
4// <dp@dingox.paris1.matra-dtv.fr>
5
6
7#include <TDocStd_Application.ixx>
8
9#include <CDF_Session.hxx>
10#include <CDF_DirectoryIterator.hxx>
11#include <CDF_Directory.hxx>
12
13#include <Standard_ErrorHandler.hxx>
14#include <Standard_NotImplemented.hxx>
15#include <Standard_DomainError.hxx>
16#include <Plugin_Failure.hxx>
17#include <TDF_Label.hxx>
18#include <CDF_Store.hxx>
19#include <TDocStd_PathParser.hxx>
20
21#include <CDM_MessageDriver.hxx>
22
23// The improvement concerns returning value of the methods Open(), Save() and SaveAs():
24#define BUC60867
25
26// TDocStd_Owner attribute have pointer of closed TDocStd_Document
27#define OCC159
28
29#ifdef OCC159
30#include <TDocStd_Owner.hxx>
31#endif
32
33//=======================================================================
34//function : TDocStd_Application
35//purpose :
36//=======================================================================
37
38TDocStd_Application::TDocStd_Application()
39 : myIsDriverLoaded (Standard_True)
40{
41 Handle(CDF_Session) S;
42 if (!CDF_Session::Exists()) S = new CDF_Session();
43 else S = CDF_Session::CurrentSession();
44 S->SetCurrentApplication(this);
45 try
46 {
47 OCC_CATCH_SIGNALS
48 S->LoadDriver();
49 }
50 catch (Plugin_Failure)
51 {
52 myIsDriverLoaded = Standard_False;
53 }
54}
55
56
57//=======================================================================
58//function : IsDriverLoaded
59//purpose :
60//=======================================================================
61Standard_Boolean TDocStd_Application::IsDriverLoaded() const
62{
63 return myIsDriverLoaded;
64}
65
66//=======================================================================
67//function : Resources
68//purpose :
69//=======================================================================
70
71Handle(Resource_Manager) TDocStd_Application::Resources() {
72 if (myResources.IsNull()) {
73 myResources = new Resource_Manager(ResourcesName());
74 }
75 return myResources;
76}
77
78
79//=======================================================================
80//function : NbDocuments
81//purpose :
82//=======================================================================
83
84Standard_Integer TDocStd_Application::NbDocuments() const
85{
86 if (!CDF_Session::Exists())
87 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
88 Handle(CDF_Session) S = CDF_Session::CurrentSession();
89 return S->Directory()->Length();
90}
91
92//=======================================================================
93//function : GetDocument
94//purpose :
95//=======================================================================
96
97void TDocStd_Application::GetDocument(const Standard_Integer index,Handle(TDocStd_Document)& aDoc) const
98{
99 if (!CDF_Session::Exists())
100 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
101 Handle(CDF_Session) S = CDF_Session::CurrentSession();
102 CDF_DirectoryIterator it (S->Directory());
103 Standard_Integer current = 0;
104 for (;it.MoreDocument();it.NextDocument()) {
105 current++;
106 if (index == current) {
107 Handle(TDocStd_Document) D =
108 Handle(TDocStd_Document)::DownCast(it.Document());
109 aDoc = D;
110 return;
111 }
112 }
113}
114
115//=======================================================================
116//function : NewDocument
117//purpose :
118//=======================================================================
119
120void TDocStd_Application::NewDocument(const TCollection_ExtendedString& format,Handle(TDocStd_Document)& aDoc)
121{
122 Handle(TDocStd_Document) D = new TDocStd_Document(format);
123 InitDocument (D);
124 CDF_Application::Open(D); // add the document in the session
125 aDoc = D;
126}
127
128//=======================================================================
129//function : InitDocument
130//purpose : do nothing
131//=======================================================================
132
133void TDocStd_Application::InitDocument(const Handle(TDocStd_Document)& /*aDoc*/) const
134{
135}
136
137#ifdef OCC159
138//=======================================================================
139//function : Close
140//purpose :
141//=======================================================================
142
143void TDocStd_Application::Close(const Handle(TDocStd_Document)& aDoc)
144{
145 Handle(TDocStd_Owner) Owner;
146 if (aDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) {
147 Handle(TDocStd_Document) emptyDoc;
148 Owner->SetDocument(emptyDoc);
149 }
150 CDF_Application::Close(aDoc);
151}
152#endif
153
154//=======================================================================
155//function : IsInSession
156//purpose :
157//=======================================================================
158
159Standard_Integer TDocStd_Application::IsInSession (const TCollection_ExtendedString& path) const
160{
161 Standard_Integer nbdoc = NbDocuments();
162 Handle(TDocStd_Document) D;
163 for (Standard_Integer i = 1; i <= nbdoc; i++) {
164 GetDocument(i,D);
165 if (D->IsSaved()) {
166 if (path == D->GetPath()) return i;
167 }
168 }
169 return 0;
170}
171
172//=======================================================================
173//function : Open
174//purpose :
175//=======================================================================
176
15e8b082
M
177PCDM_ReaderStatus TDocStd_Application::Open(const TCollection_ExtendedString& path,Handle(TDocStd_Document)& aDoc) {
178 PCDM_ReaderStatus status = PCDM_RS_DriverFailure;
7fd59977 179 TDocStd_PathParser tool (path);
180 TCollection_ExtendedString directory = tool.Trek();
181 TCollection_ExtendedString file = tool.Name();
182 file+=".";
183 file+=tool.Extension();
184#ifdef BUC60867
185 status = CanRetrieve(directory,file);
15e8b082 186 if (status != PCDM_RS_OK) return status;
7fd59977 187#endif
188 try {
189 OCC_CATCH_SIGNALS
190 Handle(TDocStd_Document) D =
191 Handle(TDocStd_Document)::DownCast(Retrieve(directory,file));
192 CDF_Application::Open(D);
193 aDoc = D;
194 }
195 catch (Standard_Failure) {
196// status = GetRetrieveStatus();
197 Handle(Standard_Failure) F = Standard_Failure::Caught();
198 if (!F.IsNull() && !MessageDriver().IsNull()) {
199// Standard_SStream aMsg;
200// aMsg << Standard_Failure::Caught() << endl;
201// cout << "TDocStd_Application::Open(): " << aMsg.rdbuf()->str() << endl;
202 TCollection_ExtendedString aString (F->GetMessageString());
203 MessageDriver()->Write(aString.ToExtString());
204 }
205 }
206 status = GetRetrieveStatus();
207#ifdef DEB
208 cout<<"TDocStd_Application::Open(): The status = "<<status<<endl;
209#endif
210 return status;
211}
212
213//=======================================================================
214//function : SaveAs
215//purpose :
216//=======================================================================
217
15e8b082 218PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,const TCollection_ExtendedString& path) {
7fd59977 219 TDocStd_PathParser tool (path);
220 TCollection_ExtendedString directory = tool.Trek();
221 TCollection_ExtendedString file = tool.Name();
222 file+=".";
223 file+=tool.Extension();
224 D->Open(this);
225 CDF_Store storer (D);
226#ifdef BUC60867
227 if (!storer.SetFolder(directory))
228 {
229 TCollection_ExtendedString aMsg ("TDocStd_Application::SaveAs() - folder ");
230 aMsg += directory;
231 aMsg += " does not exist";
232 if(!MessageDriver().IsNull())
233 MessageDriver()->Write(aMsg.ToExtString());
234 return storer.StoreStatus(); //CDF_SS_Failure;
235 }
236#endif
237 storer.SetName (file);
238 try {
239 OCC_CATCH_SIGNALS
240 storer.Realize();
241 }
242 catch (Standard_Failure) {
243 Handle(Standard_Failure) F = Standard_Failure::Caught();
244 if (!F.IsNull() && !MessageDriver().IsNull()) {
245 TCollection_ExtendedString aString (F->GetMessageString());
246 MessageDriver()->Write(aString.ToExtString());
247 }
248 }
15e8b082 249 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 250 D->SetSaved();
251#ifdef BUC60867
252#ifdef DEB
253 cout<<"TDocStd_Application::SaveAs(): The status = "<<storer.StoreStatus()<<endl;
254#endif
255 return storer.StoreStatus();
256#endif
257}
258
259//=======================================================================
260//function : Save
261//purpose :
262//=======================================================================
263
15e8b082 264PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D) {
7fd59977 265#ifdef BUC60867
15e8b082 266 PCDM_StoreStatus status = PCDM_SS_OK;
7fd59977 267#endif
268 if (D->IsSaved()) {
269 CDF_Store storer (D);
270 try{
271 OCC_CATCH_SIGNALS
272 storer.Realize();
273 }
274 catch (Standard_Failure) {
275 Handle(Standard_Failure) F = Standard_Failure::Caught();
276 if (!F.IsNull() && !MessageDriver().IsNull()) {
277 TCollection_ExtendedString aString (F->GetMessageString());
278 MessageDriver()->Write(aString.ToExtString());
279 }
280 }
15e8b082 281 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 282 D->SetSaved();
283#ifdef BUC60867
284 status = storer.StoreStatus();
285#endif
286 } else {
7fd59977 287 if(!MessageDriver().IsNull()) {
15e8b082 288 TCollection_ExtendedString aMsg("Document has not been saved yet");
7fd59977 289 MessageDriver()->Write(aMsg.ToExtString());
290 }
15e8b082 291 status = PCDM_SS_Failure;
7fd59977 292 }
293#ifdef BUC60867
294#ifdef DEB
295 cout<<"TDocStd_Application::Save(): The status = "<<status<<endl;
296#endif
297 return status;
298#endif
299}
300
301//=======================================================================
302//function : SetViewer
303//purpose :
304//=======================================================================
305
306// void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
307// const Handle(V3d_Viewer)& viewer)
308// {
309// TPrsStd_AISViewer::New (D->Main(),viewer);
310// InitViewer(D);
311// }
312
313
314//=======================================================================
315//function : SetViewer
316//purpose :
317//=======================================================================
318
319// void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
320// const Handle(AIS_InteractiveContext)& IC)
321// {
322// TPrsStd_AISViewer::New (D->Main(),IC);
323// InitViewer(D);
324// }
325
326
327//=======================================================================
328//function : SaveAs
329//purpose :
330//=======================================================================
331
15e8b082 332PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,
7fd59977 333 const TCollection_ExtendedString& path,
334 TCollection_ExtendedString& theStatusMessage)
335{
336 TDocStd_PathParser tool (path);
15e8b082 337 PCDM_StoreStatus aStatus = PCDM_SS_Failure;
7fd59977 338 TCollection_ExtendedString directory = tool.Trek();
339 TCollection_ExtendedString file = tool.Name();
340 file+=".";
341 file+=tool.Extension();
342 D->Open(this);
343 CDF_Store storer (D);
344 if (storer.SetFolder(directory)) {
345 storer.SetName (file);
346 try {
347 OCC_CATCH_SIGNALS
348 storer.Realize();
349 }
350 catch (Standard_Failure) {
351 Handle(Standard_Failure) F = Standard_Failure::Caught();
352 if (!F.IsNull() && !MessageDriver().IsNull()) {
353 TCollection_ExtendedString aString (F->GetMessageString());
354 MessageDriver()->Write(aString.ToExtString());
355 }
356 }
15e8b082 357 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 358 D->SetSaved();
359 theStatusMessage = storer.AssociatedStatusText();
360 aStatus = storer.StoreStatus();
361 } else {
362 theStatusMessage =
363 TCollection_ExtendedString("TDocStd_Application::SaveAs"
364 ": No such directory ") + directory;
15e8b082 365 aStatus = PCDM_SS_Failure;
7fd59977 366 }
367 return aStatus;
368}
369
370//=======================================================================
371//function : Save
372//purpose :
373//=======================================================================
374
15e8b082 375PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D,
7fd59977 376 TCollection_ExtendedString& theStatusMessage)
377{
15e8b082 378 PCDM_StoreStatus status = PCDM_SS_OK;
7fd59977 379 if (D->IsSaved()) {
380 CDF_Store storer (D);
381 try {
382 OCC_CATCH_SIGNALS
383 storer.Realize();
384 }
385 catch (Standard_Failure) {
386 Handle(Standard_Failure) F = Standard_Failure::Caught();
387 if (!F.IsNull() && !MessageDriver().IsNull()) {
388 TCollection_ExtendedString aString (F->GetMessageString());
389 MessageDriver()->Write(aString.ToExtString());
390 }
391 }
15e8b082 392 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 393 D->SetSaved();
394 status = storer.StoreStatus();
395 theStatusMessage = storer.AssociatedStatusText();
396 } else {
15e8b082
M
397 theStatusMessage = "TDocStd_Application::the document has not been saved yet";
398 status = PCDM_SS_Failure;
7fd59977 399 }
400 return status;
401}
402
403
404//=======================================================================
405//function : OnOpenTransaction
406//purpose :
407//=======================================================================
408
409void TDocStd_Application::OnOpenTransaction (const Handle(TDocStd_Document)&)
410{
411 // nothing to do on this level
412}
413
414//=======================================================================
415//function : OnAbortTransaction
416//purpose :
417//=======================================================================
418
419void TDocStd_Application::OnAbortTransaction (const Handle(TDocStd_Document)&)
420{
421 // nothing to do on this level
422}
423
424//=======================================================================
425//function : OnCommitTransaction
426//purpose :
427//=======================================================================
428
429void TDocStd_Application::OnCommitTransaction (const Handle(TDocStd_Document)&)
430{
431 // nothing to do on this level
432}