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