-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocMgr.cpp
2677 lines (2250 loc) · 84.8 KB
/
DocMgr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @(#)Z 1.102 com/src/docshell/DocMgr.cpp, odshell, od96os2, odos29712d 97/03/21 17:37:31 (97/03/05 14:33:48) */
//
//
// COMPONENT_NAME: odshell
//
// CLASSES: none
//
// ORIGINS: 82,27
//
//
// (C) COPYRIGHT International Business Machines Corp. 1995,1996
// All Rights Reserved
// Licensed Materials - Property of IBM
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
// IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
// ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
// USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
// OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
//------------------------------------------------------------------------------
// DocMgr.cpp
//
// This file provides the implementation of the DocumentManager class.
// An instance of this class is created for each document which is opened
// or created.
//
//------------------------------------------------------------------------------
#ifndef SOM_Module_docmgr_Source
#define SOM_Module_docmgr_Source
#endif
#define DocumentManager_Class_Source
#include <time.h>
#include <ODTypes.h>
#include <ODUtils.h>
#include <StorUtil.h>
#include <DocUtils.h>
#include <StdProps.xh>
#include <StdTypes.xh>
#include <StdTypIO.h>
#include <ODSessn.xh>
#include <ODStor.xh>
#include <ODCtr.xh>
#include <Document.xh>
#include <Part.xh>
#include <StorageU.xh>
#include <Undo.xh>
#include <WinIter.xh>
#include <LinkMgr.xh>
#ifdef _PLATFORM_OS2_
#include <IODDefs.xh>
#include <ILnkSrvc.xh>
#endif
#include <Except.h>
#include <Clipbd.xh>
#include <DragDrp.xh>
#include <Disptch.xh>
#include <ODPrtRei.xh>
#include "DraftRec.h"
#include <TempObj.h>
#include <EditrSet.h>
#include <ISOStr.h>
#include <BndNSUtl.h>
#include <BArray.h>
#include "ShPInLst.h"
#include "DocShell.h"
#include "ShellUtl.h"
#ifdef _PLATFORM_WIN32_
#include <olewrap.h>
#include <olemgrw.h>
#endif
#define VARIABLE_MACROS
#include "DocMgr.xih"
#ifdef _PLATFORM_WIN32_
#include <objidl.h>
#endif
#undef LOGGING
#define LOGGING 1
#define UNTITLED_FILENAME "NewDoc.od"
#if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_)
#define PATH_SEPARATOR "\\"
#else
#define PATH_SEPARATOR "/"
#endif
#define FILE_LENGTH 256
SOMEXTERN ODBoolean SOMLINK ReadOnlyDlg(Environment *ev);
//------------------------------------------------------------------------------
// Initialize
//
// Initialize a newly created DocumentManager
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerInitialize(DocumentManager *somSelf,
Environment *ev, ODSession* session)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager", "DocumentManagerInitialize");
try
{
// Cache references to the session and window state
_fSession = session;
if (_fSession)
_fWindowState = session->GetWindowState(ev);
// Set the draft permissions to the default
_fDraftPermissions = kODDPExclusiveWrite;
// Initialize file name and root kind name
_fFileName[0] = kODNULL;
_fPartKindName[0] = kODNULL;
// Initialize to existing document
_fUnsavedDocument = kODFalse;
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::Initialize\n");
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// OpenDocumentFromFile
//
// Opens a document that exists on disk
//------------------------------------------------------------------------------
SOM_Scope char* SOMLINK DocumentManagerOpenDocumentFromFile(
DocumentManager *somSelf,
Environment *ev, char *fileName,
ODUShort actionFlag)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerOpenDocumentFromFile");
char* rootPartKind = kODNULL;
PlatformFile* file = new PlatformFile;
try
{
// Set the file name - need error handling for file existence - RLC
file->Specify((ODFileSpec *)fileName);
#ifdef _PLATFORM_WIN32_
// Setup linking by registering this filename
somSelf->GetOleWrapper(ev)->RegisterFilename(fileName, FALSE);
#endif
// Check if it is a read-only document. If so, display a
// read-only alert dialog to ask user whether to continue
// or not. If continue, then proceed to open the document.
if (somSelf->IsReadOnlyDocument(ev, file))
{
// Get the document from the file
somSelf->OpenFile(ev, file, actionFlag);
// Install Shell Plug-in
InstallShellPlugIn(ev, _fDraft);
#if defined(_PLATFORM_OS2_)
_fSession->GetLinkService(ev)->
DraftOpened(ev, _fDraft, kODFalse, fileName);
#endif
// Open the root part of the document
rootPartKind = somSelf->OpenRootPart(ev);
// Display window title
_fWindowState->SetDefaultWindowTitles(ev, _fDraft);
}
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::OpenDocumentFromFile\n");
ODSetSOMException(ev, _exception);
}
// Cache the kindname and filename
// Note: _fFileName is already set if create a new document off stationery
if (fileName && (_fFileName[0] == kODNULL))
strcpy(_fFileName, fileName);
if (rootPartKind)
strcpy(_fPartKindName, rootPartKind);
delete file;
return rootPartKind;
}
//------------------------------------------------------------------------------
// OpenDocumentFromMemory
//
// Opens a document that exists in memory
//------------------------------------------------------------------------------
SOM_Scope char* SOMLINK DocumentManagerOpenDocumentFromMemory(
DocumentManager *somSelf,
Environment *ev,
ODHandle handle)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerOpenDocumentFromMemory");
char* rootPartKind = kODNULL,
*rootPartNameStr = kODNULL;
ODIText *rootPartName = kODNULL;
try
{
// Get the container from the given memory location
_fContainer = GetMemoryContainer(ev, _fSession, handle,
kODDefaultMemoryContainer);
// Get the newly created document from the memory container
somSelf->OpenContainer(ev, _fContainer);
// Install Shell Plug-in
InstallShellPlugIn(ev, _fDraft);
// Open the root part of the document
rootPartKind = somSelf->OpenRootPart(ev);
// Display root part kind as the window title
// Note: If kODPropName is not set by the part, then use the
// rootPartKind returned from OpenRootPart as the text
// in window title.
TempODStorageUnit rootSU = ODAcquireRootPartSUOfDraft(ev, _fDraft);
rootPartName = ODGetITextProp(ev, rootSU, kODPropName,
kODPlatformIText, kODNULL);
if (rootPartName)
{
ODULong strLength = GetITextStringLength(rootPartName);
rootPartNameStr = new char[strLength+1];
GetITextString(rootPartName, rootPartNameStr);
}
else
{
rootPartNameStr = new char[strlen(rootPartKind)+1];
strcpy(rootPartNameStr, rootPartKind);
}
if (rootPartNameStr)
{
_fWindowState->SetLongName(ev, rootPartNameStr);
_fWindowState->SetDefaultWindowTitles(ev, _fDraft);
}
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::OpenDocumentFromMemory\n");
ODError error = ErrorCode();
if (_fContainer)
SaveAndRestoreEv1(_fContainer);
ODSetSOMException(ev, error);
}
// Cache the kindname
if (rootPartKind)
strcpy(_fPartKindName, rootPartKind);
if (rootPartNameStr)
delete[] rootPartNameStr;
if (rootPartName)
DisposeIText(rootPartName);
return rootPartKind;
}
//------------------------------------------------------------------------------
// CreateDocumentInFile
//
// Create a document in a new file container
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerCreateDocumentInFile(
DocumentManager *somSelf,
Environment *ev, ODType kindName,
char *filename)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateDocumentInFile");
PlatformFile *file = kODNULL;
// Cache the kindname and filename
strcpy(_fFileName, filename);
strcpy(_fPartKindName, kindName);
try
{
// Create a new container in a file - filename may be "uniquified"
file = somSelf->NewFileContainer(ev, &_fContainer, _fFileName);
file->GetAsciiName(_fFileName, FILE_LENGTH - 1);
// Create a new document in the new container and externalize it
somSelf->CreateDocument(ev, _fContainer, kindName);
// Release the new container
// Note: Need to release the container which is acquired in
// CreateFileContainer.
// The call to GetFileContainer in OpenFile will acquire
// the created container so the refcount for the container
// remains 1.
ODReleaseObject(ev, _fContainer);
#ifdef _PLATFORM_WIN32_
// Setup linking by registering this filename
// char fileName[_MAX_PATH];
// file->GetAsciiName(fileName, _MAX_PATH);
somSelf->GetOleWrapper(ev)->RegisterFilename(_fFileName, FALSE);
#endif
// Get the newly created document from the file
somSelf->OpenFile(ev, file, kODNULL);
// Install Shell Plug-in
InstallShellPlugIn(ev, _fDraft);
// Open the root part of the document
somSelf->OpenRootPart(ev);
// Sync up window titles with file name
_fWindowState->SetDefaultWindowTitles(ev, _fDraft);
// Mark document as unsaved document
// Note: Should mark this as unsaved document only if working
// with file container, not for memory container.
_fUnsavedDocument = kODTrue;
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::CreateDocumentInFile\n");
ODError error = ErrorCode();
if (_fContainer)
SaveAndRestoreEv1(_fContainer);
ODSetSOMException(ev, error);
}
if (file)
delete file;
}
//------------------------------------------------------------------------------
// CreateDocumentInMemory
//
// Create a document in a new memory container
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerCreateDocumentInMemory(
DocumentManager *somSelf,
Environment *ev, ODType kindName)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateDocumentInMemory");
char *rootPartNameStr = kODNULL;
ODIText *rootPartName = kODNULL;
// Cache the kindname
strcpy(_fPartKindName, kindName);
try
{
// Create a new memory container
somSelf->NewMemoryContainer(ev, &_fContainer);
// Create a new document in the new container and externalize it
somSelf->CreateDocument(ev, _fContainer, kindName);
// Get the newly created document from the memory container.
//
// Note: For memory container, the container is not acquired
// again in OpenMemoryContainer. So the container created
// in CreatedMemoryContainer is not released.
somSelf->OpenContainer(ev, _fContainer);
// Install Shell Plug-in
InstallShellPlugIn(ev, _fDraft);
// Open the root part of the document
char *rootPartKind = somSelf->OpenRootPart(ev);
// Display root part kind as the window title
// Note: If kODPropName is not set by the part, then use the
// rootPartKind returned from OpenRootPart as the text
// in window title.
TempODStorageUnit rootSU = ODAcquireRootPartSUOfDraft(ev, _fDraft);
rootPartName = ODGetITextProp(ev, rootSU, kODPropName,
kODPlatformIText, kODNULL);
if (rootPartName)
{
ODULong strLength = GetITextStringLength(rootPartName);
rootPartNameStr = new char[strLength+1];
GetITextString(rootPartName, rootPartNameStr);
}
else
// Note: Use rootPartKind returned from OpenRootPart not
// kindName passed in because OpenRootPart may fail
// and open NoPart instead.
rootPartNameStr = rootPartKind;
if (rootPartNameStr)
{
_fWindowState->SetLongName(ev, rootPartNameStr);
_fWindowState->SetDefaultWindowTitles(ev, _fDraft);
}
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::CreateDocumentInMemory\n");
ODError error = ErrorCode();
if (_fContainer)
SaveAndRestoreEv1(_fContainer);
ODSetSOMException(ev, error);
}
if (rootPartNameStr)
delete[] rootPartNameStr;
if (rootPartName)
DisposeIText(rootPartName);
}
//------------------------------------------------------------------------------
// CreateDocumentAsStationery
//
// Create a document in a new file container and mark the document as
// stationery
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerCreateDocumentAsStationery(
DocumentManager *somSelf,
Environment *ev, ODType kindName,
char *filename)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateDocumentAsStationery");
PlatformFile *file = kODNULL;
// Cache the kindname and filename
strcpy(_fFileName, filename);
strcpy(_fPartKindName, kindName);
try
{
// Create a new container in a file - filename may be "uniquified"
file = somSelf->NewFileContainer(ev, &_fContainer, _fFileName);
file->GetAsciiName(_fFileName, FILE_LENGTH - 1);
// Create a new document in the new container and externalize it
somSelf->CreateDocument(ev, _fContainer, kindName);
// Acquire new document and draft. CreateDocument method releases
// the document and draft it acquired before returned to caller.
_fDocument = _fContainer->AcquireDocument(ev, kODDefaultDocument);
_fDraft = _fDocument->AcquireBaseDraft(ev, kODDPExclusiveWrite);
// Mark the document as stationery
somSelf->SetStationery(ev, _fDraft);
// Write out draft
_fDraft->Externalize(ev);
ODReleaseObject(ev, _fDraft);
ODReleaseObject(ev, _fDocument);
ODReleaseObject(ev, _fContainer);
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::CreateDocumentAsStationery\n");
ODError error = ErrorCode();
if (file)
delete file;
if (_fDraft || _fDocument || _fContainer)
SaveAndRestoreEv3(_fDraft, _fDocument, _fContainer);
ODSetSOMException(ev, error);
}
}
//------------------------------------------------------------------------------
// NewFileContainer
//
// Create a new file container with a unique name
//------------------------------------------------------------------------------
SOM_Scope PlatformFile* SOMLINK DocumentManagerNewFileContainer(
DocumentManager *somSelf,
Environment *ev,
ODContainer **container,
char *filename)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerNewFileContainer");
PlatformFile *file = kODNULL;
try
{
// Create a uniquely named file
file = somSelf->CreateUntitledFile(ev, filename);
// If problem creating untitled file in current directory, create
// untitled file in directory set in ODTMP.
if (!file)
file = somSelf->CreateUntitledFile(ev, _fFileName);
// Create a new file container
if (file)
(*container) = CreateFileContainer(ev, _fSession,
&(file->GetFileSpec()));
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::NewFileContainer\n");
if (file)
delete file;
file = kODNULL;
ODSetSOMException(ev, _exception);
}
return file;
}
//------------------------------------------------------------------------------
// NewMemoryContainer
//
// Create a new memory container
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerNewMemoryContainer(
DocumentManager *somSelf,
Environment *ev,
ODContainer** container)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerNewMemoryContainer");
ODHandle containerHandle;
try
{
containerHandle = ODNewHandle(0);
ASSERTMSG((containerHandle != 0), kODErrOpeningMemContainer,
"Error allocating document memory container",AMSG_500);
*container = CreateMemoryContainer(ev, _fSession, containerHandle,
kODDefaultMemoryContainer);
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::NewMemoryContainer\n");
if (containerHandle)
ODDisposeHandle(containerHandle);
*container = kODNULL;
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// CreateDocument
//
// Initialize a document in a new container
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerCreateDocument(DocumentManager *somSelf,
Environment *ev,
ODContainer* newContainer,
ODType partKind)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateDocument");
try
{
// Get the document from the container and base draft from the document
TempODDocument newDocument = newContainer->AcquireDocument(ev,
kODDefaultDocument);
// Set the current document so that when GetDocumentManagerOfDraft
// is called the current document is set.
_fDocument = newDocument;
TempODDraft newDraft = newDocument->AcquireBaseDraft(ev,
kODDPExclusiveWrite);
// Modify timestamp and user properties for draft
TempODStorageUnit draftSU = newDraft->AcquireDraftProperties(ev);
somSelf->ResetDateModByInfo(ev, draftSU);
// Create the root part
TempODPart rootPart = newDraft->CreatePart(ev, partKind, kODNULL);
TempODStorageUnit su = rootPart->GetStorageUnit(ev);
if (su)
{
// Need to acquire the storage unit. The storage unit
// returned by GetStorageUnit does not increment the
// refcount.
su->Acquire(ev);
ODSetRootPartSUOfDraft(ev, newDraft, su);
}
// Modify timestamp and user properties for root part
somSelf->ResetDateModByInfo(ev, su);
// Close the document (can't FinalRelease the part since it may have a
// refcount > 1 upon creation, e.g. some might register for idle time,
// etc.
newDraft->Externalize(ev);
// Reset the current document to null
_fDocument = kODNULL;
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::CreateDocument\n");
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// OpenFile
//
// Get the container, document and top draft from the platform file.
// If we've got stationery then open a copy of the file instead.
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerOpenFile(DocumentManager *somSelf,
Environment *ev,
PlatformFile* file,
ODUShort actionFlag)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager","DocumentManagerOpenFile");
PlatformFile *newFile = kODNULL;
try
{
ODFileAttrFlags fileAttrs;
// If file is readonly, set file draft permission to read only.
file->GetFileAttributes(&fileAttrs);
if (fileAttrs & kODFileAttrReadOnly)
_fDraftPermissions = kODDPReadOnly;
// Get the container, document, and top draft
_fContainer = GetFileContainer(ev, _fSession, &(file->GetFileSpec()));
_fDocument = _fContainer->AcquireDocument(ev, kODDefaultDocument);
// Setting draft permission to read so that a new version won't be
// created.
_fDraft = _fDocument->AcquireDraft(ev, kODDPReadOnly, 0,
kODNULL, kODPosTop, kODFalse);
}
catch (ODException _exception)
{
char fileName[FILE_LENGTH];
file->GetAsciiName(fileName, FILE_LENGTH - 1);
ODError error = ErrorCode();
if (error == kODErrDraftDoesNotExist)
WARNMSG(WARN_INDEX(AMSG_901),
"CPPOD0404 - Docshell: File %s is not a valid OpenDoc file.", fileName);
else
WARNMSG(WARN_INDEX(AMSG_862),
"CPPOD0347 - Docshell: Error opening file %s.", fileName);
if (_fDraft || _fDocument || _fContainer)
SaveAndRestoreEv3(_fDraft, _fDocument, _fContainer);
ODSetSOMException(ev, error);
return;
}
try
{
// If file is a stationery and not a request to open the stationery,
// then save a copy of the stationery to a new file.
if ((somSelf->IsStationery(ev, _fDraft)) &&
(actionFlag != kODEditStationery))
{
newFile = somSelf->CopyStationery(ev);
somSelf->OpenFile(ev, newFile, kODNULL);
char fileName[FILE_LENGTH];
// Cache the new file name
newFile->GetAsciiName(fileName, FILE_LENGTH - 1);
strcpy(_fFileName, fileName);
delete newFile;
newFile = kODNULL;
}
else
// Create working draft
somSelf->CreateWorkingDraft(ev);
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::OpenFile\n");
ODError error = ErrorCode();
if (newFile)
delete newFile;
if (_fDraft || _fDocument || _fContainer)
SaveAndRestoreEv3(_fDraft, _fDocument, _fContainer);
ODSetSOMException(ev, error);
}
}
//------------------------------------------------------------------------------
// CopyStationery
//
// Create a new document file and save the top most draft of the
// stationery to the new document.
//------------------------------------------------------------------------------
SOM_Scope PlatformFile* SOMLINK DocumentManagerCopyStationery(
DocumentManager *somSelf,
Environment *ev)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager","DocumentManagerCopyStationery");
PlatformFile *newFile = kODNULL;
try
{
ODContainer *tmpContainer;
TempODContainer stationeryContainer = _fContainer;
TempODDocument stationeryDocument = _fDocument;
TempODDraft stationeryDraft = _fDraft;
_fContainer = kODNULL;
_fDocument = kODNULL;
_fDraft = kODNULL;
// Reset draft permission to the default value
_fDraftPermissions = kODDPExclusiveWrite;
// Create a new file container
newFile = somSelf->NewFileContainer(ev,
&tmpContainer, UNTITLED_FILENAME);
TempODContainer newContainer = tmpContainer;
// Create a new document in the newly created file container
TempODDocument newDoc = newContainer->AcquireDocument(ev,
kODDefaultDocument);
// Copy the topmost draft of the stationery to the base draft
// of the new document
newDoc->SetBaseDraftFromForeignDraft(ev, stationeryDraft);
// Get a reference of the base draft with write permission
TempODDraft newDraft = newDoc->AcquireBaseDraft(ev, kODDPExclusiveWrite);
// Remove stationery property
somSelf->UnsetStationery(ev, newDraft);
// Modify timestamp and user properties
{
TempODStorageUnit rootsu = ODAcquireRootPartSUOfDraft(ev,
newDraft);
// Note: Need to get the root part kind before calling
// ResetDateModByInfo
char *partKindName = ODGetKindFromPartSU(ev,rootsu);
if (partKindName)
strcpy(_fPartKindName, partKindName);
somSelf->ResetDateModByInfo(ev, rootsu);
TempODStorageUnit su = newDraft->AcquireDraftProperties(ev);
somSelf->ResetDateModByInfo(ev, su);
}
// Save the base draft
newDraft->Externalize(ev);
}
catch (ODException _exception)
{
WARNMSG(WARN_INDEX(AMSG_863),
"CPPOD0348 - Docshell: Error creating file from stationery.");
if (newFile)
delete newFile;
newFile = kODNULL;
ODSetSOMException(ev, _exception);
}
return newFile;
}
//------------------------------------------------------------------------------
// OpenContainer
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerOpenContainer(
DocumentManager *somSelf,
Environment *ev,
ODContainer* container)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerOpenContainer");
try
{
// Get the document and top draft from the memory container
_fContainer = container;
_fDocument = _fContainer->AcquireDocument(ev, kODDefaultDocument);
_fDraft = _fDocument->AcquireDraft(ev, _fDraftPermissions, 0, kODNULL,
kODPosTop, kODFalse);
// Create working draft
somSelf->CreateWorkingDraft(ev);
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::OpenContainer\n");
ODError error = ErrorCode();
if (_fDraft || _fDocument || _fContainer)
SaveAndRestoreEv3(_fDraft, _fDocument, _fContainer);
ODSetSOMException(ev, error);
}
}
//------------------------------------------------------------------------------
// CreateWorkingDraft
//
// Create a working draft on top of current draft
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK DocumentManagerCreateWorkingDraft(
DocumentManager *somSelf,
Environment *ev)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateWorkingDraft");
try
{
// Create working draft on top of current most draft so that
// saving a modified draft which has a draft created on it
// before the changes won't corrupt the draft.
if (somSelf->HasWriteAccess(ev))
{
_fDraft = _fDocument->CreateDraft(ev, _fDraft, kODTrue);
if (_fDraft->GetPermissions(ev) != _fDraftPermissions)
// Release the current draft and get a reference of the
// draft again with the specified draft permission
//
// Note: The new working draft is set to have kODDPExclusiveWrite
// permission by default.
_fDraft = _fDocument->AcquireDraft(ev, _fDraftPermissions,
kODNULLID, _fDraft, kODPosSame, kODTrue);
// Notify link manager that this draft is opened
#if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_UNIX_)
_fSession->GetLinkManager(ev)->DraftOpened(ev, _fDraft);
#elif defined(_PLATFORM_OS2_)
_fSession->GetLinkService(ev)->
DraftOpened(ev, _fDraft, kODFalse, _fFileName);
#endif
}
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::CreateWorkingDraft\n");
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// OpenRootPart
//
// Acquire the root part of the document and invoke its Open method
//------------------------------------------------------------------------------
SOM_Scope char* SOMLINK DocumentManagerOpenRootPart(DocumentManager *somSelf,
Environment *ev)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager","DocumentManagerOpenRootPart");
char *rootPartKind = kODNULL;
try
{
// Open the root part
_fWindowState->Internalize(ev, _fDraft);
_fWindowState->OpenWindows(ev, _fDraft);
if (_fWindowState->GetRootWindowCount(ev, _fDraft) == 0)
{
TempODPart rootPart = ODAcquireRootPartOfDraft(ev, _fDraft);
rootPart->Open(ev, kODNULL);
}
// Get the root part kind to return
{
TempODStorageUnit su = ODAcquireRootPartSUOfDraft(ev, _fDraft);
rootPartKind = ODGetKindFromPartSU(ev,su);
}
#ifdef DEBUG
ODULong refcount = _fDraft->GetRefCount(ev);
#endif
}
catch (ODException _exception)
{
LOG("SOM exception occured in DocumentManager::OpenRootPart\n");
ODSetSOMException(ev, _exception);
}
return rootPartKind;
}
//------------------------------------------------------------------------------
// CreateUntitledFile
//
// Create a new platform file with a unique name
//------------------------------------------------------------------------------
SOM_Scope PlatformFile* SOMLINK DocumentManagerCreateUntitledFile(
DocumentManager *somSelf,
Environment *ev,
char *filename)
{
DocumentManagerData *somThis = DocumentManagerGetData(somSelf);
DocumentManagerMethodDebug("DocumentManager",
"DocumentManagerCreateUntitledFile");
static retry = kODTrue;
PlatformFile* file = kODNULL;
try
{
// Default file name...
if (filename == kODNULL)
filename = UNTITLED_FILENAME;
// Create a new PlatformFile
file = new PlatformFile;
// Set the PlatformFile's name to the default
file->Specify((ODFileSpec *)filename);
// Create a uniquely named file
file->UniquifyAndCreate(
kODShellSignature, // creator
kODShellSignature, // fileType
0, // scriptCode
1, // copyCount
kODTryCurrentName); // forceNewName
}
catch (ODException _exception)
{
delete file;
file = kODNULL;
if (!retry)
{
// If attempted to create file in ODTMP is tried, print out
// a warning.
WARNMSG(WARN_INDEX(AMSG_858),"CPPOD0343 - Docshell: Problem creating file %s. Directory may be full or write-protected.", filename);
ODSetSOMException(ev, _exception);
}