-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMDoc.cpp
2024 lines (1657 loc) · 56 KB
/
CMDoc.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.20 com/src/bento/CMDoc.cpp, odstorage, od96os2, odos29712d 97/03/21 17:19:03 (96/12/17 13:46:57) */
//====START_GENERATED_PROLOG======================================
//
//
// COMPONENT_NAME: odstorage
//
// 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========================================
//
/*
File: CMDoc.cpp
Contains: Implementation for CMDoc class.
Owned by: Vincent Lo
Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
Change History (most recent first):
<54> 10/24/95 jpa 1293441: DM/VL: Bento memory reserve &
fatal container err & don't throw in
Release.
<53> 10/20/95 VL 1293256: Changed
kODErrBentoInvalidVersionList to
kODErrDraftDoesNotExist.
<52> 10/13/95 EL 1287340: Use standard ISO prefix
<51> 10/8/95 TJ Fixes Recomended by Refball
<50> 10/3/95 TJ Added Includes so it compiles
<49> 10/3/95 TJ Changes done by RefBall Team
<48> 9/29/95 TJ Made Changes for MAC SOM
<47> 9/8/95 VL 1282012: Replaced kODErrDraftNotExists with
kODErrDraftDoesNotExist.
<46> 9/8/95 T‚ 1281096 FB2:Many constants in ODTypesB
without kOD prefix!
<45> 9/7/95 EL 1281410: Window for draft just above a
deleted draft will not be automatically
closed when that draft is deleted.
<44> 8/30/95 EL 1270290: Do not close and open the draft in
SaveToPrev and Collapse draft if draft is
not top draft and hence read only.
<43> 8/21/95 VL 1278330, 1278315: Error code cleanup.
<42> 8/18/95 NP 1274946: add kODErrInvalidPersistentFormat
<41> 8/16/95 NP 1274946: ErrorDef.idl problems. Add include
file.
<40> 8/12/95 T‚ 1276806 Optimization: use kODFalse instead
of kODTrue in comparisons
<39> 8/8/95 EL #1273589: version list is not collapsed
when document is closed.
<38> 8/3/95 RR #1257260: Collapse B classes. Remove
somInit methods. Don't call IsInitialized
or SubclassResponsibility
<37> 7/21/95 VL 1270320: Removed internal field _fName.
<36> 6/28/95 RR 1242642 BB Mostly ref counting. RemoveDraft
releases draft before deleting it
<35> 6/20/95 VL 1259397: Release Draft after getting its id
in GetDraftGut.
<34> 5/26/95 VL 1251403: Multithreading naming support.
<33> 5/25/95 jpa Fixed usage of ODDebug. [1253321]
<32> 5/18/95 CC 1238898: Add destFrame argument to
BeginClone call.
<31> 5/18/95 EL 1249941: Set container dirty flag when
drafts changed.
<30> 4/25/95 VL 1210982: Removed 5$.
<29> 4/7/95 EL 1225905: Call SetModDate when save to prev.
<28> 3/31/95 EL 1234685: If top draft embedded container
has not been saved don't write it in the
version list. Don't externalize version
list except before flush or close.
<27> 3/29/95 DM make standard format little endian
<26> 3/24/95 EL 1209355: Cut down on version list
externalization. Flush file container in
SaveToAPrevDraft.
<25> 3/23/95 VL 1230357: Implemented Purge. 1228003: Added
debug code for versionlist.
<24> 3/9/95 VL 1220320: Moved public error codes from
Bento to ErrorDef.idl.
<23> 2/17/95 EL 1182275: Collapse version list before
closing embedded container so we know if
there is a new draft.
<22> 2/13/95 T‚ 1219963: Document:AcquireDraft should not bump
refcount if kPosSame and release==true
<21> 1/31/95 EL 1195846: revert file if there is no real
change.
<20> 1/26/95 eeh 1214080: OpenDoc really uses kODMacIText,
not kODIntlText.
<19> 1/25/95 jpa Include StdExts.xh
<18> 1/11/95 VL 1185688: Made storage more robust in terms
of error handling. Also did some code
review cleanup.
<17> 11/14/94 VL 1155887: Changed AcquireDraft to handle new
posCode for top draft.
<16> 10/19/94 VL 1155857: Added ODDebug_Drafts for debugging
"Cannot create more than 10 drafts"
problem.
<15> 9/29/94 RA 1189812: Mods for 68K build.
<14> 9/23/94 VL 1184272: ContainerID is now a sequence of
octets. 1184166: DocumentName is ODIText
now.
<13> 9/6/94 VL 1184154: Include StorageU.xh.
<12> 9/6/94 VL 1184154: Removed include file also.
<11> 9/6/94 VL 1184154: Removed AcquireDocumentProperties.
<10> 9/5/94 VL 1184871: Used Renew to remove dependency on
default heap.
<9> 8/31/94 T‚ #1183129, #1183116, #1183119, #1183111:
Lots of ErrorCode cleanup.
<8> 8/31/94 VL No code change. Fixed comments in <7>.
<7> 8/31/94 VL 1161158, 1106013: Commented out code which
does checking on embedded container's use
mode in AcquireDraft. This should avoid a
crashing bug when the embedded container is
kODNULL. Also, fixed GetName to make it
return kODNULL if the document doesn't have a name.
<6> 8/26/94 VL 1183174: Use updated cloning APIs.
<5> 8/16/94 T‚ #1180922 Remove more obsolete types from
StdTypes.idl. Localized kODVersionList to
this file.
<4> 8/3/94 VL 1153123: Storage to ODStor.
<3> 7/14/94 VL Check return value for kODNULL before
calling GetUseMode in AcquireDraftGut.
<2> 7/11/94 VL Added Exists.
<1> 7/5/94 VL first checked in
To Do:
In Progress:
*/
#define CMDocument_Class_Source
#define VARIABLE_MACROS
#include <CMDoc.xih>
#ifndef SOM_ODContainer_xh
#include <ODCtr.xh>
#endif
#ifndef SOM_ODStorageUnit_xh
#include <StorageU.xh>
#endif
#ifndef _EXCEPT_
#include "Except.h"
#endif
#ifndef _TEMPOBJ_
#include "TempObj.h"
#endif
#ifndef _FLIPEND_
#include "FlipEnd.h"
#endif
#ifndef SOM_ODEmbeddedContainer_xh
#include <EmbedCtr.xh>
#endif
#ifndef SOM_CMDraft_xh
#include <CMDraft.xh>
#endif
#ifndef SOM_ODSession_xh
#include <ODSessn.xh>
#endif
#ifndef __STRING__
#include <string.h> // For strlen, strcpy....
#endif
#ifndef _INDHDR_
#include "IndHdr.h" // For Embedded Property and Type names
#endif
#ifndef _CMCTR_
#include "CMCtr.xh" // Just for getting fContainer
#endif
#ifndef _DOCPRIV_
#include "DocPriv.h"
#endif
#ifndef _ISOSTR_
#include "ISOStr.h"
#endif
#ifndef SOM_Module_OpenDoc_StdProps_defined
#include <StdProps.xh>
#endif
#ifndef SOM_Module_OpenDoc_StdTypes_defined
#include <StdTypes.xh>
#endif
#ifndef _ODMEMORY_
#include "ODMemory.h"
#endif
#ifndef _ODNEW_
#include "ODNew.h"
#endif
#ifndef _BENTODEF_
#include "BentoDef.h"
#endif
#ifndef _ODDEBUG_
#include <ODDebug.h>
#endif
#ifndef _ITEXT_
#include <IText.h>
#endif
#ifndef _UTILERRS_
#include "UtilErrs.h"
#endif
#ifndef __TIME_H__
#include <time.h>
#endif
#ifndef _SESSHDR_
#include "SessHdr.h"
#endif
#if defined(_PLATFORM_WIN32_) || defined (_PLATFORM_OS2_) || defined (_PLATFORM_AIX_)
#ifndef _ODUTILS_
#include "ODUtils.h"
#endif
#endif
#ifndef _BENTOERR_
#include "BentoErr.h"
#endif
#pragma segment CMDocument
//==============================================================================
// Constants
//==============================================================================
// Private ISO Strings
const ODPropertyName kODDocumentProperties = "+//ISO 9070/ANSI::113722::US::CI LABS::OpenDoc:Bento Container Suite:Property:DocumentProperties";
const ODValueType kODVersionList = "+//ISO 9070/ANSI::113722::US::CI LABS::OpenDoc:Bento Container Suite:Type:CMVersionList";
// For debugging
#if ODDebug
// #define ODDebug_Drafts 1
// #define ODDebug_DebugRefCount 1
#endif
//==============================================================================
// Function Prototype
//==============================================================================
static CMDraft* NewCMDraft(ODMemoryHeapID heapID);
static CMObject AcquireDocumentPropertiesObject(CMContainer container);
// The following two functions need to be filled in when we are moving to a
// real multithread system.
#define EnableInterrupt()
#define DisableInterrupt()
// For debugging
#ifdef DebugStorage
#define MyDebugStr(s) do {somPrintf(s);} while (0)
#define MyDebug1Str(f,p1) do {somPrintf(f, p1);} while (0)
#define MyDebug2Str(f,p1,p2) do {somPrintf(f, p1, p2);} while (0)
#define MyDebug3Str(f,p1,p2,p3) do {somPrintf(f, p1, p2,p3);} while (0)
#else
#define MyDebugStr(s)
#define MyDebug1Str(f,p1)
#define MyDebug2Str(f,p1,p2)
#define MyDebug3Str(f,p1,p2,p3)
#endif
#if ODDebug_Drafts
#ifndef _MEMDEBG_
#include <MemDebg.h>
#endif
static void PrintDrafts(Environment* ev, DraftList* drafts, char* string);
static void PrintHeapInfo();
#endif
//==============================================================================
// CMDocument
//==============================================================================
//------------------------------------------------------------------------------
// CMDocument: GetCMVersionList
//------------------------------------------------------------------------------
SOM_Scope CMValue SOMLINK CMDocumentGetCMVersionList(CMDocument *somSelf, Environment *ev)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentGetCMVersionList");
CMValue versionList = kODNULL;
try
{
CMContainer cmContainer = _fContainer->GetCMContainer(ev);
ODSessionMustHaveCMAllocReserve(cmContainer);
CMProperty versionListProp;
CMObject versionListObj;
if ((versionListProp = CMRegisterProperty(cmContainer, kODPropVersionList)) == kODNULL)
THROW(kODErrBentoInvalidProperty);
versionListObj = CMGetNextObjectWithProperty(cmContainer, kODNULL, versionListProp);
if (versionListObj != kODNULL)
versionList = CMGetNextValue(versionListObj, versionListProp, kODNULL);
ODSessionRestoreCMAllocReserve(cmContainer);
}
catch (ODException _exception)
{
versionList = kODNULL;
ODSetSOMException(ev, _exception);
}
return versionList;
}
//------------------------------------------------------------------------------
// CMDocument: AcquireDraftGut
//------------------------------------------------------------------------------
SOM_Scope CMDraft* SOMLINK CMDocumentAcquireDraftGut(CMDocument *somSelf, Environment *ev,
VersionList* versionList,
ODDraftPermissions perms,
ODDraftID id,
CMDraft* draft,
ODPositionCode posCode,
ODBoolean release)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentAcquireDraftGut");
CMDraft* newDraft = kODNULL;
ODBoolean acquired = kODFalse;
ODBoolean CreateNewDraft = kODFalse;
try
{
#if ODDebug_Drafts
somPrintf("**** Entering AcquireDraftGut: id %d draft %x posCode %d release %d\n", id, draft, posCode, release);
PrintDrafts(ev, _fDrafts, "**** Entering AcquireDraftGut");
somSelf->GetVersionList(ev)->Print(">>> Entering AcquireDraftGut");
#endif
CMDraft* fromDraft = (CMDraft*) draft;
ODDraftID latestDraftID;
ODDraftID fromDraftID;
ODDraftID prevDraftID;
ODDraftID nextDraftID;
if (id != 0) {
if ((newDraft = _fDrafts->Get(id)) != kODNULL) {
if ((perms == kODDPReadOnly) ||
(newDraft->GetPermissions(ev) == kODDPExclusiveWrite)) {
newDraft->Acquire(ev);
acquired = kODTrue;
MyDebugStr("**** AcquireDraft: Acquire only.\n");
}
else
THROW(kODErrInvalidPermissions);
}
else if ((newDraft = _fReleasedDrafts->Get(id)) != kODNULL) {
if (perms == kODDPExclusiveWrite) {
if (newDraft->GetPermissions(ev) == kODDPReadOnly) {
latestDraftID = versionList->GetLatestDraftID();
if (versionList->IsAbove(latestDraftID, id) != kODFalse)
THROW(kODErrInvalidPermissions);
MyDebugStr("**** AcquireDraft: From ReadOnly to ExclusiveWrite.\n");
}
}
newDraft->Reinitialize(ev, perms);
_fReleasedDrafts->Remove(id);
_fDrafts->Add(id, newDraft);
newDraft->Acquire(ev);
acquired = kODTrue;
MyDebugStr("**** AcquireDraft: Back from the _fReleasedDrafts pile.\n");
}
else {
newDraft = NewCMDraft(somSelf->GetHeap(ev));
CreateNewDraft = kODTrue;
newDraft->InitDraft(ev, somSelf, id, perms);
_fDrafts->Add(id, newDraft);
MyDebug3Str("**** id %d draft %x perms %d\n", id, newDraft, newDraft->GetPermissions(ev));
}
}
else {
if ((posCode != kODPosTop) && (fromDraft == kODNULL)) {
THROW(kODErrInvalidDraftID);
}
switch (posCode) {
case kODPosTop:
if (fromDraft != kODNULL)
THROW(kODErrInvalidDraftID);
nextDraftID = versionList->GetLatestDraftID();
newDraft = somSelf->AcquireDraftGut(ev, versionList,
perms,
nextDraftID,
kODNULL,
kODPosUndefined,
kODFalse);
break;
case kODPosSame:
if (fromDraft->GetPermissions(ev) == perms) {
if (release == kODFalse)
fromDraft->Acquire(ev);
newDraft = fromDraft;
acquired = kODTrue;
}
else {
if (fromDraft->GetRefCount(ev) != 1)
THROW(kODErrRefCountNotEqualOne);
if (release == kODFalse)
THROW(kODErrCannotChangePermissions);
fromDraftID = fromDraft->GetID(ev);
fromDraft->Release(ev);
newDraft = somSelf->AcquireDraftGut(ev, versionList,
perms,
fromDraftID,
kODNULL,
kODPosUndefined,
kODFalse);
}
break;
case kODPosFirstBelow:
case kODPosLastBelow:
fromDraftID = fromDraft->GetID(ev);
prevDraftID = versionList->GetPreviousDraftID(fromDraftID);
if (release != kODFalse)
fromDraft->Release(ev);
newDraft = somSelf->AcquireDraftGut(ev, versionList,
perms,
prevDraftID,
kODNULL,
kODPosUndefined,
kODFalse);
break;
case kODPosFirstAbove:
case kODPosLastAbove:
fromDraftID = fromDraft->GetID(ev);
nextDraftID = versionList->GetNextDraftID(fromDraftID);
if (release != kODFalse)
fromDraft->Release(ev);
newDraft = somSelf->AcquireDraftGut(ev, versionList,
perms,
nextDraftID,
kODNULL,
kODPosUndefined,
kODFalse);
break;
case kODPosUndefined:
case kODPosAll:
case kODPosFirstSib:
case kODPosLastSib:
case kODPosNextSib:
case kODPosPrevSib:
default:
THROW(kODErrUnsupportedPosCode);
break;
}
}
#if ODDebug_Drafts
PrintDrafts(ev, _fDrafts, "**** Exiting AcquireDraftGut");
somSelf->GetVersionList(ev)->Print(">>> Exiting AcquireDraftGut");
#endif
}
catch (ODException _exception)
{
ODError error = ErrorCode();
SaveEv();
try
{
if (acquired){
newDraft->Release(ev);
newDraft = kODNULL;
}
if (CreateNewDraft)
ODDeleteObject(newDraft);
}
catch(ODException _exception)
{
SetErrorCode(kODNoError);
}
RestoreEv();
ODSetSOMException(ev, error);
}
return newDraft;
}
//------------------------------------------------------------------------------
// CMDocument: ~CMDocument
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK CMDocumentsomUninit(CMDocument *somSelf)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentsomUninit");
Environment* ev = somGetGlobalEnvironment();
DraftListIterator* iter = kODNULL;
DraftListIterator* releasedDraftsIter = kODNULL;
try
{
#ifdef _PLATFORM_MACINTOSH_
iter = new(somSelf->GetHeap(ev)) DraftListIterator(_fDrafts);
#endif
#if defined(_PLATFORM_WIN32_) || defined (_PLATFORM_OS2_) || defined (_PLATFORM_AIX_)
// The use of heap is not implemented in Windows platform
iter = new DraftListIterator(_fDrafts);
#endif
iter->Initialize();
#ifdef _PLATFORM_MACINTOSH_
releasedDraftsIter = new(somSelf->GetHeap(ev)) DraftListIterator(_fReleasedDrafts);
#endif
#if defined(_PLATFORM_WIN32_) || defined (_PLATFORM_OS2_) || defined (_PLATFORM_AIX_)
// The use of heap is not implemented in Windows platform
releasedDraftsIter = new DraftListIterator(_fReleasedDrafts);
#endif
releasedDraftsIter->Initialize();
CMDraft* draft;
#if ODDebug_DebugRefCount
if (somSelf->GetRefCount(ev) != 0)
DebugStr("\pRefCount of Document is not 0 at uninit.");
somPrintf("~CMDocument %x RefCount %d\n", somSelf, somSelf->GetRefCount(ev));
#endif
draft = iter->Last();
while (draft != kODNULL) {
delete draft;
draft = iter->Previous();
}
draft = releasedDraftsIter->Last();
while (draft != kODNULL) {
delete draft;
draft = releasedDraftsIter->Previous();
}
delete iter;
delete releasedDraftsIter;
delete _fVersions;
delete _fDrafts;
delete _fReleasedDrafts;
parent_somUninit(somSelf);
}
catch (ODException _exception)
{
ODDeleteObject(iter);
ODDeleteObject(releasedDraftsIter);
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// CMDocument: Purge
//------------------------------------------------------------------------------
SOM_Scope ODSize SOMLINK CMDocumentPurge(CMDocument *somSelf, Environment *ev,
ODSize size)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentPurge");
ODSize purgedSize = 0;
DraftListIterator* draftList = kODNULL;
try
{
#ifdef _PLATFORM_MACINTOSH_
draftList = new(somSelf->GetHeap(ev)) DraftListIterator(_fDrafts);
#endif
#if defined(_PLATFORM_WIN32_) || defined (_PLATFORM_OS2_) || defined (_PLATFORM_AIX_)
// The use of heap is not implemented in Windows platform
draftList = new DraftListIterator(_fDrafts);
#endif
draftList->Initialize();
CMDraft* draft = draftList->Last();
while (draft != kODNULL) {
purgedSize += draft->Purge(ev, size-purgedSize);
draft = draftList->Previous();
}
delete draftList;
}
catch(ODException _exception)
{
purgedSize = 0;
ODDeleteObject(draftList);
ODError error = ErrorCode();
ODSetSOMException(ev, error);
}
return purgedSize;
}
//------------------------------------------------------------------------------
// CMDocument: Acquire
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK CMDocumentAcquire(CMDocument *somSelf, Environment *ev)
{
// CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentAcquire");
parent_Acquire(somSelf,ev);
}
//------------------------------------------------------------------------------
// CMDocument: Release
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK CMDocumentRelease(CMDocument *somSelf, Environment *ev)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentRelease");
try
{
#if TestFlushContainer
if (_fContainer->GetDirtyFlag(ev) != kODFalse)
somSelf->ExternalizeVersionList(ev, kODFalse);
#endif
parent_Release(somSelf,ev);
if (somSelf->GetRefCount(ev) == 0) {
_fContainer->ReleaseDocument(ev, somSelf);
}
}
catch(ODException _exception)
{
ODError err = ErrorCode();
WARNMSG(WARN_INDEX(AMSG_650),"Error occurred in ODDocument::Release: %d %s",
err, ErrorMessage() ?ErrorMessage() :"");
if (err == kODErrBentoErr)
SetErrorCode(kODErrUndefined);
else if (err != kODErrUndefined)
SetErrorCode(kODNoError);
}
}
//------------------------------------------------------------------------------
// CMDocument: AcquireContainer
//------------------------------------------------------------------------------
SOM_Scope ODContainer* SOMLINK CMDocumentGetContainer(CMDocument *somSelf, Environment *ev)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentGetContainer");
return (ODContainer*) _fContainer;
}
//------------------------------------------------------------------------------
// CMDocument: GetID
//------------------------------------------------------------------------------
SOM_Scope ODDocumentID SOMLINK CMDocumentGetID(CMDocument *somSelf, Environment *ev)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentGetID");
return _fID;
}
//------------------------------------------------------------------------------
// CMDocument: GetName
//------------------------------------------------------------------------------
SOM_Scope ODDocumentName SOMLINK CMDocumentGetName(CMDocument *somSelf, Environment *ev)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentGetName");
ODDocumentName *name = kODNULL;
#if defined(_PLATFORM_WIN32) || defined(_PLATFORM_OS2) || defined(_PLATFORM_AIX)
ODDocumentName dummy;
#endif
try
{
CMContainer cmContainer = _fContainer->GetCMContainer(ev);
ODSessionMustHaveCMAllocReserve(cmContainer);
CMObject cmObject = AcquireDocumentPropertiesObject(cmContainer);
ASSERTMSG(cmObject, kODErrNoDocumentProperties,
"No Document Properties Object.", 0);
CMProperty cmProp = CMRegisterProperty(cmContainer, kODPropDocumentName);
CMType cmType = CMRegisterType(cmContainer, kODPlatformIText);
CMValue cmValue = CMUseValue(cmObject, cmProp, cmType);
if (cmValue != kODNULL) {
ODITextFormat stdFormat;
CMReadValueData(cmValue, &stdFormat, 0, sizeof(ODITextFormat));
ODITextFormat format = ConvertODULongFromStd(stdFormat);
if( format != kODTraditionalMacText ) {
WARNMSG(WARN_INDEX(AMSG_660),"Reading IText in unknown format %ld",format);
THROW(kODErrInvalidPersistentFormat);
}
ODULong size = CMGetValueSize(cmValue) - sizeof(ODITextFormat);
name = CreateIText(size);
name->format = format;
CMReadValueData(cmValue, name->text._buffer, sizeof(ODITextFormat), name->text._length);
} else {
name = SetITextStringLength(kODNULL,0,kODFalse);
somSelf->SetName(ev, name);
}
ODSessionRestoreCMAllocReserve(cmContainer);
}
catch (ODException _exception)
{
#ifdef _PLATFORM_MACINTOSH_
ODDocumentName dummy;
return dummy; // will be ignored since error set
#endif
#if defined(_PLATFORM_WIN32) || defined(_PLATFORM_OS2) || defined(_PLATFORM_AIX)
*name = dummy;
#endif
ODSetSOMException(ev, _exception);
}
return *name;
}
//------------------------------------------------------------------------------
// CMDocument: SetName
//------------------------------------------------------------------------------
SOM_Scope void SOMLINK CMDocumentSetName(CMDocument *somSelf, Environment *ev,
ODDocumentName* name)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentSetName");
try
{
CMContainer cmContainer = _fContainer->GetCMContainer(ev);
ODSessionMustHaveCMAllocReserve(cmContainer);
CMObject cmObject = AcquireDocumentPropertiesObject(cmContainer);
ASSERTMSG(cmObject, kODErrNoDocumentProperties,
"No Document Properties Object.", 0);
CMProperty cmProp = CMRegisterProperty(cmContainer, kODPropDocumentName);
CMType cmType = CMRegisterType(cmContainer, kODPlatformIText);
CMValue cmValue = CMUseValue(cmObject, cmProp, cmType);
if (cmValue == kODNULL) {
cmValue = CMNewValue(cmObject, cmProp, cmType);
}
ODITextFormat stdFormat = ConvertODULongToStd(name->format);
CMWriteValueData(cmValue, &stdFormat, 0, sizeof(ODITextFormat));
CMWriteValueData(cmValue, name->text._buffer, sizeof(ODITextFormat), name->text._length);
ODSessionRestoreCMAllocReserve(cmContainer);
}
catch (ODException _exception)
{
ODSetSOMException(ev, _exception);
}
}
//------------------------------------------------------------------------------
// CMDocument: CollapseDrafts
//------------------------------------------------------------------------------
SOM_Scope ODDocument* SOMLINK CMDocumentCollapseDrafts(CMDocument *somSelf, Environment *ev,
ODDraft* fromDraft,
ODDraft* toDraft)
{
CMDocumentData *somThis = CMDocumentGetData(somSelf);
CMDocumentMethodDebug("CMDocument","CMDocumentCollapseDrafts");
try
{
CMDraft* from = (CMDraft*) fromDraft;
CMDraft* to = (CMDraft*) toDraft; ODVolatile(to);
VersionList* versionList = kODNULL;
ODDraftID fromID;
ODDraftID toID;
ODDraftID baseID;
ODBoolean revertIt = kODFalse;
TempODDraft tempDraft = kODNULL; // DMc - better way to release
versionList = somSelf->TestAndGetVersionList(ev);
ASSERT((versionList != kODNULL), kODErrDraftDoesNotExist);
if (!from) {
// LOG("Illegal NULL fromDraft parameter in ODDraft::CollapseDrafts method");
THROW(kODErrIllegalNullDraftInput);
// ODSetSOMException(ev, kODErrIllegalNullDraftInput);
// return somSelf;
}
fromID = from->GetID(ev);
baseID = versionList->GetBaseDraftID();
if (to == kODNULL) {
toID = versionList->GetPreviousDraftID(fromID);
to = (CMDraft*) somSelf->AcquireDraft(ev, kODDPReadOnly,
toID, kODNULL, kODPosUndefined, kODFalse);
// releaseToDraft = kODTrue;
tempDraft = to; // ensure it's released
}
else {
toID = to->GetID(ev);
}
if ((fromID == baseID) || (fromID == toID))
return somSelf;
if (versionList->IsAbove(fromID, toID) == kODFalse){
THROW(kODErrCannotCollapseDrafts);
// LOG("Draft version list error in ODDraft::CollapseDrafts method");
// ODSetSOMException(ev, kODErrCannotCollapseDrafts);
// return somSelf;
}
// from cannot have a ref count more than 1 if it is the top draft
// if from is not top draft, then we are not going to do anything
// to the draft and we need not worry about the ref count
if (fromID == _fVersions->GetLatestDraftID())
if (from->GetRefCount(ev) > 1) // Check for outstanding draft
{
// LOG("Outstanding Draft error in ODDraft::CollapseDrafts");
THROW(kODErrOutstandingDraft);
}
#ifdef _PLATFORM_MACINTOSH_
DraftListIterator* draftList =
new(somSelf->GetHeap(ev)) DraftListIterator(_fDrafts);
#endif
#if defined(_PLATFORM_WIN32_)||defined(_PLATFORM_OS2_)||defined(_PLATFORM_AIX_)
// The use of heap is not implemented in Windows platform
DraftListIterator* draftList = new DraftListIterator(_fDrafts);
#endif
draftList->Initialize();
CMDraft* draft = draftList->Last();
while (draft != kODNULL) {
ODDraftID draftID = draft->GetID(ev);
if ((versionList->IsBelow(draftID, fromID) != kODFalse) &&
(versionList->IsAbove(draftID, toID) != kODFalse) &&
(versionList->GetCurrentVersion(draftID) != kODTombstonedVersion))
THROW(kODErrOutstandingDraft);
// LOG("Outstanding Draft error in ODDraft::CollapseDrafts");
else
draft = draftList->Previous();
}
delete draftList;
// Flush the from draft
if ((from->GetPermissions(ev) == kODDPExclusiveWrite) &&
(from->NeedExternalizing(ev) != kODFalse)) {
THROW(kODErrNonEmptyDraft);
// LOG("Nonempty Draft in ODDraft::CollapseDrafts method");
// ODSetSOMException(ev, kODErrIllegalNullDraftInput);
// return somSelf;
}
else if (from->IsChangedFromPrev(ev, versionList) != kODFalse){
THROW(kODErrNonEmptyDraft);
// LOG("Nonempty Draft in ODDraft::CollapseDrafts method");
// ODSetSOMException(ev, kODErrIllegalNullDraftInput);
// return somSelf;
}
if (from->IsNewDraft(ev) != kODFalse) {
revertIt = kODTrue;
// _fDrafts->Remove(from->GetID(ev));
ODDraftID id = from->GetID(ev);
from->Release(ev);
_fReleasedDrafts->Remove(id);
from->Abort(ev);
delete from;
}
else {
// so that the collapsed version list will be written out on close
_fContainer->SetDirtyFlag(ev, kODTrue);
// Release the draft
from->Release(ev);
}
// CollapseDrafts on VersionList
versionList->CollapseDrafts(fromID, toID);
#if !TestFlushContainer
// Make the change persistent
somSelf->ExternalizeVersionList(ev, kODFalse);
#endif
// If no change, then clear the dirty flag
if (revertIt != kODFalse)
_fContainer->SetDirtyFlag(ev, kODFalse);
somSelf->ReleaseVersionList(ev);
//if (releaseToDraft != kODFalse)
// to->Release(ev);
#ifdef _NO_TMP_OBJS_
if (tempDraft != kODNULL)
tempDraft->Release(ev);
#endif
}
catch (ODException _exception)
{
ODError error = ErrorCode();
SaveEv();
try
{
somSelf->ReleaseVersionList(ev);
}
catch (ODException _exception)
{
SetErrorCode(kODNoError);
}
RestoreEv();
ODSetSOMException(ev, error);
}
return somSelf;
}
//------------------------------------------------------------------------------
// CMDocument: AcquireDraft
//------------------------------------------------------------------------------
SOM_Scope ODDraft* SOMLINK CMDocumentAcquireDraft(CMDocument *somSelf, Environment *ev,
ODDraftPermissions perms,