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