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