0022995: It is not possible to check presence of a document in OCAF session via IsInS...
[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{
bcb0fd43
V
161 TCollection_ExtendedString unifiedPath(path);
162 unifiedPath.ChangeAll('/', '|');
163 unifiedPath.ChangeAll('\\', '|');
164
165 Standard_Integer nbdoc = NbDocuments();
166 Handle(TDocStd_Document) D;
167 for (Standard_Integer i = 1; i <= nbdoc; i++)
168 {
169 GetDocument(i,D);
170 if (D->IsSaved())
171 {
172 TCollection_ExtendedString unifiedDocPath(D->GetPath());
173 unifiedDocPath.ChangeAll('/', '|');
174 unifiedDocPath.ChangeAll('\\', '|');
175
176 if (unifiedPath == unifiedDocPath)
177 return i;
178 }
7fd59977 179 }
bcb0fd43 180 return 0;
7fd59977 181}
182
183//=======================================================================
184//function : Open
185//purpose :
186//=======================================================================
187
15e8b082
M
188PCDM_ReaderStatus TDocStd_Application::Open(const TCollection_ExtendedString& path,Handle(TDocStd_Document)& aDoc) {
189 PCDM_ReaderStatus status = PCDM_RS_DriverFailure;
7fd59977 190 TDocStd_PathParser tool (path);
191 TCollection_ExtendedString directory = tool.Trek();
192 TCollection_ExtendedString file = tool.Name();
193 file+=".";
194 file+=tool.Extension();
195#ifdef BUC60867
196 status = CanRetrieve(directory,file);
15e8b082 197 if (status != PCDM_RS_OK) return status;
7fd59977 198#endif
199 try {
200 OCC_CATCH_SIGNALS
201 Handle(TDocStd_Document) D =
202 Handle(TDocStd_Document)::DownCast(Retrieve(directory,file));
203 CDF_Application::Open(D);
204 aDoc = D;
205 }
206 catch (Standard_Failure) {
207// status = GetRetrieveStatus();
208 Handle(Standard_Failure) F = Standard_Failure::Caught();
209 if (!F.IsNull() && !MessageDriver().IsNull()) {
210// Standard_SStream aMsg;
211// aMsg << Standard_Failure::Caught() << endl;
212// cout << "TDocStd_Application::Open(): " << aMsg.rdbuf()->str() << endl;
213 TCollection_ExtendedString aString (F->GetMessageString());
214 MessageDriver()->Write(aString.ToExtString());
215 }
216 }
217 status = GetRetrieveStatus();
218#ifdef DEB
219 cout<<"TDocStd_Application::Open(): The status = "<<status<<endl;
220#endif
221 return status;
222}
223
224//=======================================================================
225//function : SaveAs
226//purpose :
227//=======================================================================
228
15e8b082 229PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,const TCollection_ExtendedString& path) {
7fd59977 230 TDocStd_PathParser tool (path);
231 TCollection_ExtendedString directory = tool.Trek();
232 TCollection_ExtendedString file = tool.Name();
233 file+=".";
234 file+=tool.Extension();
235 D->Open(this);
236 CDF_Store storer (D);
237#ifdef BUC60867
238 if (!storer.SetFolder(directory))
239 {
240 TCollection_ExtendedString aMsg ("TDocStd_Application::SaveAs() - folder ");
241 aMsg += directory;
242 aMsg += " does not exist";
243 if(!MessageDriver().IsNull())
244 MessageDriver()->Write(aMsg.ToExtString());
245 return storer.StoreStatus(); //CDF_SS_Failure;
246 }
247#endif
248 storer.SetName (file);
249 try {
250 OCC_CATCH_SIGNALS
251 storer.Realize();
252 }
253 catch (Standard_Failure) {
254 Handle(Standard_Failure) F = Standard_Failure::Caught();
255 if (!F.IsNull() && !MessageDriver().IsNull()) {
256 TCollection_ExtendedString aString (F->GetMessageString());
257 MessageDriver()->Write(aString.ToExtString());
258 }
259 }
15e8b082 260 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 261 D->SetSaved();
262#ifdef BUC60867
263#ifdef DEB
264 cout<<"TDocStd_Application::SaveAs(): The status = "<<storer.StoreStatus()<<endl;
265#endif
266 return storer.StoreStatus();
267#endif
268}
269
270//=======================================================================
271//function : Save
272//purpose :
273//=======================================================================
274
15e8b082 275PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D) {
7fd59977 276#ifdef BUC60867
15e8b082 277 PCDM_StoreStatus status = PCDM_SS_OK;
7fd59977 278#endif
279 if (D->IsSaved()) {
280 CDF_Store storer (D);
281 try{
282 OCC_CATCH_SIGNALS
283 storer.Realize();
284 }
285 catch (Standard_Failure) {
286 Handle(Standard_Failure) F = Standard_Failure::Caught();
287 if (!F.IsNull() && !MessageDriver().IsNull()) {
288 TCollection_ExtendedString aString (F->GetMessageString());
289 MessageDriver()->Write(aString.ToExtString());
290 }
291 }
15e8b082 292 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 293 D->SetSaved();
294#ifdef BUC60867
295 status = storer.StoreStatus();
296#endif
297 } else {
7fd59977 298 if(!MessageDriver().IsNull()) {
15e8b082 299 TCollection_ExtendedString aMsg("Document has not been saved yet");
7fd59977 300 MessageDriver()->Write(aMsg.ToExtString());
301 }
15e8b082 302 status = PCDM_SS_Failure;
7fd59977 303 }
304#ifdef BUC60867
305#ifdef DEB
306 cout<<"TDocStd_Application::Save(): The status = "<<status<<endl;
307#endif
308 return status;
309#endif
310}
311
312//=======================================================================
313//function : SetViewer
314//purpose :
315//=======================================================================
316
317// void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
318// const Handle(V3d_Viewer)& viewer)
319// {
320// TPrsStd_AISViewer::New (D->Main(),viewer);
321// InitViewer(D);
322// }
323
324
325//=======================================================================
326//function : SetViewer
327//purpose :
328//=======================================================================
329
330// void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
331// const Handle(AIS_InteractiveContext)& IC)
332// {
333// TPrsStd_AISViewer::New (D->Main(),IC);
334// InitViewer(D);
335// }
336
337
338//=======================================================================
339//function : SaveAs
340//purpose :
341//=======================================================================
342
15e8b082 343PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,
7fd59977 344 const TCollection_ExtendedString& path,
345 TCollection_ExtendedString& theStatusMessage)
346{
347 TDocStd_PathParser tool (path);
15e8b082 348 PCDM_StoreStatus aStatus = PCDM_SS_Failure;
7fd59977 349 TCollection_ExtendedString directory = tool.Trek();
350 TCollection_ExtendedString file = tool.Name();
351 file+=".";
352 file+=tool.Extension();
353 D->Open(this);
354 CDF_Store storer (D);
355 if (storer.SetFolder(directory)) {
356 storer.SetName (file);
357 try {
358 OCC_CATCH_SIGNALS
359 storer.Realize();
360 }
361 catch (Standard_Failure) {
362 Handle(Standard_Failure) F = Standard_Failure::Caught();
363 if (!F.IsNull() && !MessageDriver().IsNull()) {
364 TCollection_ExtendedString aString (F->GetMessageString());
365 MessageDriver()->Write(aString.ToExtString());
366 }
367 }
15e8b082 368 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 369 D->SetSaved();
370 theStatusMessage = storer.AssociatedStatusText();
371 aStatus = storer.StoreStatus();
372 } else {
373 theStatusMessage =
374 TCollection_ExtendedString("TDocStd_Application::SaveAs"
375 ": No such directory ") + directory;
15e8b082 376 aStatus = PCDM_SS_Failure;
7fd59977 377 }
378 return aStatus;
379}
380
381//=======================================================================
382//function : Save
383//purpose :
384//=======================================================================
385
15e8b082 386PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D,
7fd59977 387 TCollection_ExtendedString& theStatusMessage)
388{
15e8b082 389 PCDM_StoreStatus status = PCDM_SS_OK;
7fd59977 390 if (D->IsSaved()) {
391 CDF_Store storer (D);
392 try {
393 OCC_CATCH_SIGNALS
394 storer.Realize();
395 }
396 catch (Standard_Failure) {
397 Handle(Standard_Failure) F = Standard_Failure::Caught();
398 if (!F.IsNull() && !MessageDriver().IsNull()) {
399 TCollection_ExtendedString aString (F->GetMessageString());
400 MessageDriver()->Write(aString.ToExtString());
401 }
402 }
15e8b082 403 if(storer.StoreStatus() == PCDM_SS_OK)
7fd59977 404 D->SetSaved();
405 status = storer.StoreStatus();
406 theStatusMessage = storer.AssociatedStatusText();
407 } else {
15e8b082
M
408 theStatusMessage = "TDocStd_Application::the document has not been saved yet";
409 status = PCDM_SS_Failure;
7fd59977 410 }
411 return status;
412}
413
414
415//=======================================================================
416//function : OnOpenTransaction
417//purpose :
418//=======================================================================
419
420void TDocStd_Application::OnOpenTransaction (const Handle(TDocStd_Document)&)
421{
422 // nothing to do on this level
423}
424
425//=======================================================================
426//function : OnAbortTransaction
427//purpose :
428//=======================================================================
429
430void TDocStd_Application::OnAbortTransaction (const Handle(TDocStd_Document)&)
431{
432 // nothing to do on this level
433}
434
435//=======================================================================
436//function : OnCommitTransaction
437//purpose :
438//=======================================================================
439
440void TDocStd_Application::OnCommitTransaction (const Handle(TDocStd_Document)&)
441{
442 // nothing to do on this level
443}