home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / LinkMgr.cpp < prev    next >
Text File  |  1997-03-21  |  88KB  |  2,154 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: oddataxfer
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.7 com/src/storage/LinkMgr.cpp, oddataxfer, od96os2, odos29712d 8/22/96 15:37:17 [ 3/21/97 17:18:54 ]
  28. /*
  29.         File:           LinkMgr.cpp
  30.  
  31.         Contains:       Implementation for ODLinkManager class.
  32.  
  33.         Owned by:       Craig Carper
  34.  
  35.         Copyright:      ⌐ 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  36.  
  37.         Change History (most recent first):
  38.  
  39.                 <44>     10/8/95        TJ              Fixes Recomended by Refball
  40.                 <43>     10/4/95        jpa             Added missing ODVolatiles [1285799]
  41.                 <42>     10/3/95        TJ              Changes done by RefBall Team
  42.                 <41>     9/27/95        eeh             1284465: use ClipStringToBytes
  43.                 <40>      9/1/95        CC              1278590: Added RequestLinkAEHandler.
  44.                                                                         Call SetStatus and UpdateDependents if link
  45.                                                                         cannot be established.
  46.                                                                         1190727: GetLinkAEHandler: Update Edition
  47.                                                                         file if waiting for save.
  48.                 <39>     8/30/95        EL              1279582: Storage ODVolatile problems.
  49.                                                                         1279832: Storage THROW_IF_NULL problems.
  50.                 <38>     8/29/95        DM              1221778, 1276165:  add SuspendResume(),
  51.                                                                         ShowLinkUpdateAlert(), and _fSuspended-
  52.                                                                         UpdateLinks to let LinkMgr do update
  53.                                                                         alerts.
  54.                 <37>     8/26/95        Té              1274606 FB2: Patching Remarks
  55.                 <36>     8/22/95        eeh             1276821:
  56.                                                                         kOpenDocShellSignature->kODShellSignature
  57.                 <35>     8/15/95        CC              1275241: Moved declaration of DraftInfo
  58.                                                                         struct into LinkMgr.cpp.
  59.                 <34>     8/12/95        Té              1276812 Need to use TempObjs and TempRefs
  60.                                                                         for exception safety and to avoid TRY
  61.                                                                         blocks, 1276807 Opt./Bug: use StdTypIO
  62.                                                                         routines for portable streaming & smaller
  63.                                                                         footprint
  64.                 <33>      8/3/95        RR              #1257260: Collapse B classes. Remove
  65.                                                                         somInit methods. Don't call IsInitialized
  66.                                                                         or SubclassResponsibility
  67.                 <32>     7/28/95        VL              1270320: Do not create draft info in
  68.                                                                         DraftSaved if the draft has not been opened
  69.                                                                         yet.
  70.                 <31>     7/25/95        CC              1212338: Created class ODObjectCollection;
  71.                                                                         inherits from OrderedCollection but
  72.                                                                         overrides ElementsMatch to use
  73.                                                                         ODObjectsAreEqual macro.
  74.                 <30>     6/26/95        Té              1242642 BB:Fix refcounting bugs
  75.                 <29>      6/7/95        CC              1242442: CreateLink: Don't add link to
  76.                                                                         kODPropImportedLinkSources draft property.
  77.                                                                         GetLinkAEHandler: Don' t add linkSource to
  78.                                                                         kODPropExportedLinks draft property.
  79.                                                                         Removed local function
  80.                                                                         AppendStrongReferenceToDraftProperty.
  81.                 <28>      6/2/95        CC              1255474: AcquireLinkAEHandler restored to
  82.                                                                         GetLinkAEHandler
  83.                                                                         1255474: kODAcquireLinkEventID restored to
  84.                                                                         kODGetLinkEventID
  85.                 <27>     5/26/95        VL              1251403: Multithreading naming support.
  86.                 <26>     5/22/95        CC              1251802: Added TruncateFileName.
  87.                 <25>     4/25/95        CC              1242555: DeleteEditionFiles: $5 entered as
  88.                                                                         bug 1242448.
  89.                                                                         1242555: CreateLink,AcquireLinkAEHandler: $5
  90.                                                                         entered as bug 1242442.
  91.                                                                         1242625: GetDraftInfo: Use ODDeleteObject
  92.                                                                         instead of delete operator.
  93.                 <24>     4/14/95        Té              #1235279 BB: InfoUtil & StdTypIO functions
  94.                                                                         should take Environment* and SU* when
  95.                                                                         possible
  96.                 <23>      3/7/95        CC              1224121 ODPart::CreateLink() and
  97.                                                                         ODLinkManager::CreateLink() return
  98.                                                                         ODLinkSource object.
  99.                 <22>      3/3/95        CC              1223667 ExportOnSave() and DeleteOnSave
  100.                                                                         return previous setting as boolean result.
  101.                 <21>     2/24/95        CC              1153783: Made edition name localizable by
  102.                                                                         using ReplaceIntoString.
  103.                 <20>     2/15/95        CC              1220327 InitLinkManager: Call
  104.                                                                         InitLinkDialogs to set session global.
  105.                                                                         1196831 Use PStrToText; remove static
  106.                                                                         function NumToText.
  107.                                                                         1220318 BuildErrorString calls
  108.                                                                         ReplaceIntoString.
  109.                 <19>      2/1/95        CC              1153347, 1154916, 1213004:
  110.                                                                         GetSectionFromEvent returns error rather
  111.                                                                         than throwing.
  112.                                                                         SectionScrollAEHandler: Alert the user via
  113.                                                                         the notification manager.
  114.                                                                         Added BuildErrorString() and NumToText().
  115.                                                                         Support fNMResponseUPP and fNMRecPtr
  116.                                                                         fields.
  117.                 <18>     1/27/95        TJ              VL: some stuff.
  118.                 <17>     1/26/95        VL              #???: Use updated Storage Unit Ref API.
  119.                 <16>     1/19/95        CC              1210935, 1207937 Add DeleteOnSave() method.
  120.                                                                         1212315 Use shell's signature for Apple
  121.                                                                         event class.
  122.                                                                         1191890 - Code review changes.
  123.                 <15>    12/22/94        CC              1154916  SectionScrollAEHandler: Let part
  124.                                                                         make process frontmost.
  125.                                                                         1153780 Implemented ReserveSectionID; fixed
  126.                                                                         bug in
  127.                                                                         AppendStrongReferenceToDraftProperty.
  128.                 <14>    12/20/94        VL              1195012: Make Storage calls be
  129.                                                                         marshallable.
  130.                 <13>    12/15/94        CC              1197221 Changed call to
  131.                                                                         ODPart::CreateLink().
  132.                 <12>     12/8/94        CC              1201906 somUninit: fixed parameters to
  133.                                                                         AEGetEventHandler.
  134.                 <11>    11/28/94        RR              Fix for Eric - check AE errors
  135.                 <10>     10/4/94        CC              1190068 - Add ODLink publishers and
  136.                                                                         ODLinkSource subscribers to draft
  137.                                                                         properties.
  138.                  <9>     9/29/94        RA              1189812: Mods for 68K build.
  139.                  <8>     9/23/94        VL              1184272: ContainerID is now a sequence of
  140.                                                                         octets.
  141.                  <7>     9/19/94        CC              1187316 - somUninit() must call parent
  142.                                                                         method last.
  143.                  <6>     8/19/94        Té              #1159233 Remove references to j3di
  144.                  <5>     8/11/94        CC              Call ODDraft::CreateLinkIterator() and
  145.                                                                         ODDraft::CreateLinkSourceIterator() rather
  146.                                                                         than ugly cast from ODDraft to CMDraft.
  147.                  <4>     7/31/94        CC              Changed ODDocument parameters to ODDraft
  148.                                                                         since document properties have been
  149.                                                                         eliminated.
  150.                  <3>     7/27/94        CC              Added SOM_CATCH; use
  151.                                                                         GetOriginatingProcessID() method of
  152.                                                                         ODLinkSpec class.
  153.                  <2>      7/6/94        CC              Temporary work-around for nil result from
  154.                                                                         GetODLinkIterator(),
  155.                                                                         GetODLinkSourceIterator(), and
  156.                                                                         AcquireDocumentProperties().
  157.                  <1>     6/21/94        CC              first checked in
  158.  
  159.                 ------------------- CC ---- Converted to SOM
  160.  
  161.                  <4>      5/9/94        MB              #1162181: Changes necessary to install MMM.
  162.                  <3>      4/4/94        CC              SectionScrollAEHandler: Call link source's
  163.                                                                         ShowSourceContent() method for registered
  164.                                                                         sections. (1153347)
  165.                  <2>     3/28/94        CG              1153547: XMPSessn.h renamed to XMPSessM.h
  166.                  <1>     3/25/94        CC              first checked in
  167.  
  168.                 ------------------- CC ---- Moved from Core to Storage subsystem and
  169.                                                                         renamed from LinkMgr.cpp
  170.  
  171.                  <8>     3/25/94        eeh             bug #1153053: Changes for PPC nativity.
  172.                  <7>     3/25/94        VL              #1147173: Draft.h to CMDraft.
  173.                  <6>     3/17/94        CG              #1151392: Moved kOpenDocShellSignature from
  174.                                                                         ContsDef.h to StdDefs.h
  175.                  <5>     3/15/94        MB              Changes to support SCpp/ASLM builds,
  176.                                                                         #1150864.
  177.                  <4>      3/2/94        CC              CreateLink: Increment ref count of returned
  178.                                                                         object (1144936).  Removed DebugStrs
  179.                                                                         (1144942).
  180.                  <3>      3/1/94        CC              Declared local functions static.
  181.                  <2>     2/15/94        CC              Bug #1142928 - Added document parameter to
  182.                                                                         ReserveSectionID.
  183.                 <12>      2/8/94        JA              Minor tweaks for CodeWarrior.
  184.                 <11>      2/8/94        Té              Throw -> THROW & some code clean up
  185.                 <10>      2/7/94        Té              more fixes to compile with PPC Headers
  186.                  <9>      2/4/94        Té              fixes to compile with PPC Universal headers
  187.                  <8>     1/21/94        CC              Moved InitEditionPack() call into shell;
  188.                                                                         ExportOnSave takes doExport parameter; made
  189.                                                                         RemoveAllLinksInCollection and
  190.                                                                         DeleteEditionFiles non-class functions.
  191.                  <7>     1/18/94        CC              Added AnyLinkImported(); removed 'j3di'
  192.                                                                         literal.
  193.                  <6>     1/14/94        CC              Added methods UnsavedExportedLinks(),
  194.                                                                         DraftOpened(), DraftSaved(), DraftClosed(),
  195.                                                                         and private support routines; added
  196.                                                                         DraftInfo type and fDraftInfos field.
  197.                  <5>    12/22/93        CC              CreateLink - dispose targetAddress
  198.                                                                         descriptor.
  199.                  <4>    12/22/93        CC              Added DraftOpened(); Dispose Apple event
  200.                                                                         descriptors in CreateLink().
  201.                  <3>    12/22/93        CC              NextGlobalID() and callers uses XMPDocument
  202.                                                                         instead of XMPDraft.  Handle section write
  203.                                                                         events by calling links WriteEditionFile()
  204.                                                                         method.  Improved error handling in several
  205.                                                                         methods.
  206.                  <2>    12/16/93        CC              Install handlers for Edition Manager
  207.                                                                         events.  Moved code from
  208.                                                                         XMPDraft::AcquireLink() into CreateLink().
  209.                                                                         Calls link objects on receiving AcquireLink and
  210.                                                                         Edition Manager events.
  211.                  <1>     12/8/93        CC              first checked in
  212.                  <2>     12/3/93        Té              Stop including XMPError.h, it is included
  213.                                                                         as ErrorDef.h inside Except.h
  214.                  <1>    11/15/93        CC              first checked in
  215.         To Do:
  216.         In Progress:
  217. */
  218.  
  219. #define VARIABLE_MACROS
  220.  
  221. // Commented out for now as OSA support is yet to be defined.
  222. /*
  223. #ifdef _PLATFORM_OS2_
  224.  #ifndef OSA_INCLUDED
  225.  #define INCL_OSA
  226.  #include <osa.h>
  227.  #endif
  228.  
  229.  #ifndef SOM_ODOSAEvent_xh
  230.  #include "ODOSAEvt.xh"
  231.  #endif
  232. #endif
  233. */
  234.  
  235.  
  236. #define ODLinkManager_Class_Source
  237. #include <LinkMgr.xih>
  238.  
  239. #ifndef _EXCEPT_
  240. #include <Except.h>
  241. #endif
  242.  
  243. #ifdef _PLATFORM_OS2_
  244. #include <ODPagtun.h>
  245. #endif
  246.  
  247. #ifndef _ODMEMORY_
  248. #include <ODMemory.h>
  249. #endif
  250.  
  251. #ifndef _ISOSTR_
  252. #include <ISOStr.h>
  253. #endif
  254.  
  255. #ifndef _PASCLSTR_
  256. #include <PasclStr.h>
  257. #endif
  258.  
  259. #ifndef SOM_Module_OpenDoc_StdProps_defined
  260. #include <StdProps.xh>
  261. #endif
  262.  
  263. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  264. #include <StdTypes.xh>
  265. #endif
  266.  
  267. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  268. #include <StdDefs.xh>
  269. #endif
  270.  
  271. #ifndef SOM_ODPart_xh
  272. #include <Part.xh>
  273. #endif
  274.  
  275. #ifndef SOM_ODSession_xh
  276. #include <ODSessn.xh>
  277. #endif
  278.  
  279. #ifndef SOM_ODContainer_xh
  280. #include <ODCtr.xh>
  281. #endif
  282.  
  283. #ifndef SOM_ODDocument_xh
  284. #include <Document.xh>
  285. #endif
  286.  
  287. #ifndef SOM_ODDraft_xh
  288. #include <Draft.xh>
  289. #endif
  290.  
  291. #ifndef SOM_ODStorageUnit_xh
  292. #include <StorageU.xh>
  293. #endif
  294.  
  295. #ifndef SOM_ODLink_xh
  296. #include <Link.xh>
  297. #endif
  298.  
  299. #ifndef SOM_ODLinkSource_xh
  300. #include <LinkSrc.xh>
  301. #endif
  302.  
  303. #ifndef SOM_ODLinkSpec_xh
  304. #include <LinkSpec.xh>
  305. #endif
  306.  
  307. #ifndef SOM_ODLinkIterator_xh
  308. #include <LinkItr.xh>
  309. #endif
  310.  
  311. #ifndef SOM_ODLinkSourceIterator_xh
  312. #include <LkSrcItr.xh>
  313. #endif
  314.  
  315. #ifndef _LINKDEFS_
  316. #include <LinkDefs.h>
  317. #endif
  318.  
  319. #ifndef _LINKDLGS_
  320. #include <LinkDlgs.h>
  321. #endif
  322.  
  323. #ifndef _ORDCOLL_
  324. #include <OrdColl.h>
  325. #endif
  326.  
  327. #ifndef _PLFMFILE_
  328. #include <PlfmFile.h>
  329. #endif
  330.  
  331. #ifndef SOM_Module_OpenDoc_Errors_defined
  332. #include <ErrorDef.xh>
  333. #endif
  334.  
  335. #ifdef _PLATFORM_OS2_ // CED
  336. #include <builtin.h>
  337. #else
  338. #ifndef __ERRORS__
  339. #include <Errors.h>
  340. #endif
  341. #endif
  342.  
  343. #ifndef _BARRAY_
  344. #include <BArray.h>
  345. #endif
  346.  
  347. #ifndef _REFCTCOL_
  348. #include "RefCtCol.h"
  349. #endif
  350.  
  351. #ifndef _TEMPOBJ_
  352. #include <TempObj.h>
  353. #endif
  354.  
  355. #ifndef _STORUTIL_
  356. #include <StorUtil.h>
  357. #endif
  358.  
  359. #ifndef _USERSRCM_
  360. #include <UseRsrcM.h>
  361. #endif
  362.  
  363. #ifdef _PLATFORM_MACINTOSH_
  364. // For smSystemScript
  365. #ifndef __SCRIPT__
  366. #include <Script.h>
  367. #endif
  368.  
  369. #ifndef __TEXTUTILS__
  370. #include <TextUtils.h>
  371. #endif
  372.  
  373. #ifndef _DLOGUTIL_
  374. #include <DlogUtil.h>
  375. #endif
  376.  
  377. #endif //
  378.  
  379. #ifndef _STDTYPIO_
  380. #include <StdTypIO.h>
  381. #endif
  382.  
  383. #ifdef _PLATFORM_MACINTOSH_
  384. #ifndef __GESTALT__
  385. #include <Gestalt.h>
  386. #endif
  387.  
  388. #ifndef __TOOLUTILS__
  389. #include <ToolUtils.h>
  390. #endif
  391.  
  392. #ifndef __NOTIFICATION__
  393. #include <Notification.h>
  394. #endif
  395.  
  396. #pragma segment ODLinkManager
  397. #endif
  398.  
  399. #ifdef _PLATFORM_OS2_
  400. #ifndef _ODUTILS_
  401. #include <odutils.h>
  402. #endif
  403. #endif
  404.  
  405. //==============================================================================
  406. // Constants
  407. //==============================================================================
  408.  
  409. #ifdef _PLATFORM_MACINTOSH_
  410. const Boolean kNotSysHandler = false;
  411. #else
  412. const Boolean kNotSysHandler = kODFalse;
  413. #endif
  414.  
  415. // Apple event constants
  416. #define kODGetLinkEventID               'gtln'
  417. #define kODRequestLinkEventID   'rqln'
  418. #define keyEditionAlias                 'edas'
  419. #define kODLinkSourceKey                'lksr'
  420.  
  421. // Value type
  422. const  ODValueType  kODULongSequence            = "OpenDoc:Type:ULongSequence";
  423.  
  424. const   ODUShort        kODStorageUnitRefLen    = 4;
  425. const   ODUShort        kODULongSize                    = 4;
  426.  
  427. //==============================================================================
  428. // Local Function Prototypes
  429. //==============================================================================
  430. ODStatic StringPtr BuildErrorString(ODSShort strResourceID, ODError error);
  431.  
  432. ODStatic EditionContainerSpec NewEditionFile(Environment* ev,
  433.                         PlatformFile* documentFile,
  434.                         ODDraft* draft);
  435.  
  436. ODStatic void EditionFileName(Str255 fileName, ODUShort numb);
  437. ODStatic void TruncateFileName(Str255 fileName, Str63 numberString);
  438.  
  439. ODStatic ODULong NewEditionID(Environment *ev, ODDraft* draft);
  440.  
  441. ODStatic void RegisterPublishingLinks(Environment *ev, ODDraft* draft);
  442. ODStatic void RegisterSubscribingLinks(Environment *ev, ODDraft* draft);
  443. ODStatic void UpdateEditionFiles(Environment *ev, ODRefCntCollection* collection);
  444. ODStatic void DeleteEditionFiles(Environment *ev, ODRefCntCollection* collection);
  445. // ODStatic void RemoveAllLinksInCollection(Environment *ev, OrderedCollection* collection);
  446. #ifdef _PLATFORM_MACINTOSH_
  447. ODStatic OSErr GetSectionFromEvent(const AppleEvent* theAppleEvent,
  448.         SectionHandle* section);
  449. #else
  450. // Commented out for now as OSA support is yet to be defined.
  451. // ODStatic OSErr GetSectionFromEvent(const ODOSAEvent* theOSAEvent);
  452. #endif
  453. ODStatic void AppendULongToDraftProperty(Environment *ev,
  454.         ODDraft* draft, ODPropertyName prop, ODULong value);
  455. ODStatic ODBoolean SectionIDReserved(Environment *ev,
  456.         ODDraft* draft, ODULong requestedID);
  457. ODStatic ODULong PeekNextGlobalID(Environment *ev,
  458.         ODDraft* draft,
  459.         ODPropertyName globalProp);
  460. ODStatic ODULong ReserveGlobalID(Environment *ev,
  461.         ODDraft* draft,
  462.         ODPropertyName globalProp);
  463. #ifdef _PLATFORM_MACINTOSH_
  464. ODStatic OSErr ODPascal GetLinkAEHandler(
  465.                                                         const AppleEvent* theAppleEvent,
  466.                                                         AppleEvent* reply,
  467.                                                         long refCon);
  468. ODStatic OSErr ODPascal RequestLinkAEHandler(
  469.                                                         const AppleEvent* theAppleEvent,
  470.                                                         AppleEvent* reply,
  471.                                                         long refCon);
  472. ODStatic OSErr ODPascal SectionReadAEHandler(
  473.                                                         const AppleEvent* theAppleEvent,
  474.                                                         const AppleEvent* reply,
  475.                                                         long refCon);
  476. ODStatic OSErr ODPascal SectionWriteAEHandler(
  477.                                                         const AppleEvent* theAppleEvent,
  478.                                                         const AppleEvent* reply,
  479.                                                         long refCon);
  480. ODStatic OSErr ODPascal SectionScrollAEHandler(
  481.                                                         const AppleEvent* theAppleEvent,
  482.                                                         const AppleEvent* reply,
  483.                                                         long refCon);
  484. #endif //
  485.  
  486. //==============================================================================
  487. // struct DraftInfo
  488. //==============================================================================
  489.  
  490. struct DraftInfo
  491. {
  492.         ODDraft*                draft;
  493.         ODRefCntCollection*     linksToExport;
  494.         ODRefCntCollection*     linksToDelete;
  495.         ODRefCntCollection*     newExportedLinks;
  496.         ODBoolean                       linksImported;
  497. };
  498.  
  499. // ODObjectCollection is replaced by ODObjectOrdColl in RefCtCol.h in Utilities
  500.  
  501. //==============================================================================
  502. // class ODObjectCollection
  503. //==============================================================================
  504. //
  505. // class ODObjectCollection : public OrderedCollection
  506. // {
  507. //      public:
  508. //              ODObjectCollection() : OrderedCollection()      {}
  509. //              ODVMethod ~ODObjectCollection()                         {}
  510. //
  511. //              ODMethod ODBoolean ElementsMatch(ElementType v1,ElementType v2) const;
  512. // };
  513.  
  514. //------------------------------------------------------------------------------
  515. // ODObjectCollection::ElementsMatch (OVERRIDE)
  516. //------------------------------------------------------------------------------
  517.  
  518. // ODBoolean ODObjectCollection::ElementsMatch(ElementType v1,ElementType v2) const
  519. // {
  520. //      Environment* ev = somGetGlobalEnvironment();
  521. //      return ODObjectsAreEqual(ev, (ODObject*) v1, (ODObject*) v2);
  522. // }
  523.  
  524. //==============================================================================
  525. // Static functions
  526. //==============================================================================
  527.  
  528. #ifdef _PLATFORM_MACINTOSH_
  529. //------------------------------------------------------------------------------
  530. // LinkMgrNMResponse
  531. //------------------------------------------------------------------------------
  532.  
  533. ODStatic void ODPascal LinkMgrNMResponse(NMRecPtr theNMRecPtr)
  534. {
  535.         OSErr error = NMRemove(theNMRecPtr);
  536. }
  537. #endif
  538.  
  539. //==============================================================================
  540. // class ODLinkManager
  541. //==============================================================================
  542.  
  543. //------------------------------------------------------------------------------
  544. // ODLinkManager::InitLinkManager
  545. //------------------------------------------------------------------------------
  546.  
  547. SOM_Scope void  SOMLINK ODLinkManagerInitLinkManager(ODLinkManager *somSelf, Environment *ev,
  548.                 ODSession* session)
  549. {
  550.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  551.     ODLinkManagerMethodDebug("ODLinkManager","InitLinkManager");
  552.  
  553. #ifdef _PLATFORM_MACINTOSH_
  554.         SOM_CATCH return;
  555. #else
  556.         SOM_TRY
  557. #endif
  558.  
  559. #ifdef _PLATFORM_MACINTOSH_ // CED
  560.         /* Moved from somInit. SOM itself sets fields to zero
  561.         _fSuspendedUpdateLinks = (ODRefCntCollection*) kODNULL;
  562.         _fDraftInfos = (OrderedCollection*) kODNULL;
  563.         _fNMResponseUPP = (NMUPP) kODNULL;
  564.         _fNMRecPtr = (NMRecPtr) kODNULL;
  565.         */
  566.         somSelf->InitObject(ev);
  567.  
  568.         long  gestaltResult;
  569.         OSErr result;
  570.  
  571.         // Check if the Edition manager is present
  572.         result = Gestalt(gestaltEditionMgrAttr, &gestaltResult);
  573.         THROW_IF_ERROR (result, "Error from Gestalt");
  574.  
  575.         // The mysterious 31 is the bit position as BitTst uses a convention opposite to MC68000
  576.         if ( BitTst(&gestaltResult, (31 - gestaltEditionMgrPresent)) == false )
  577.                 THROW(kODErrNoEditionManager);
  578.  
  579.         InitLinkDialogs(session);
  580.  
  581.         _fSuspendedUpdateLinks = new ODRefCntCollection(ev);
  582.         _fDraftInfos = new OrderedCollection;
  583.         _fNMResponseUPP = NewNMProc(LinkMgrNMResponse);
  584.         _fNMRecPtr = (NMRecPtr) ODNewPtr(sizeof(NMRec));
  585.         THROW_IF_NULL(_fNMRecPtr);
  586.         _fNMRecPtr->nmStr = kODNULL;
  587.  
  588.         AEEventHandlerUPP theHandlerUPP ;
  589.         theHandlerUPP = NewAEEventHandlerProc( GetLinkAEHandler ) ;
  590.         THROW_IF_ERROR(AEInstallEventHandler(kODShellSignature,
  591.                         kODGetLinkEventID, theHandlerUPP, (long)somSelf, kNotSysHandler));
  592.  
  593.         theHandlerUPP = NewAEEventHandlerProc( RequestLinkAEHandler ) ;
  594.         THROW_IF_ERROR(AEInstallEventHandler(kODShellSignature,
  595.                         kODRequestLinkEventID, theHandlerUPP, (long)somSelf, kNotSysHandler));
  596.  
  597.         theHandlerUPP = NewAEEventHandlerProc( SectionReadAEHandler ) ;
  598.         THROW_IF_ERROR(AEInstallEventHandler(sectionEventMsgClass, sectionReadMsgID,
  599.                         theHandlerUPP, (long)somSelf, kNotSysHandler));
  600.  
  601.         theHandlerUPP = NewAEEventHandlerProc( SectionWriteAEHandler );
  602.         THROW_IF_ERROR(AEInstallEventHandler(sectionEventMsgClass, sectionWriteMsgID,
  603.                         theHandlerUPP, (long)somSelf, kNotSysHandler));
  604.  
  605.         theHandlerUPP = NewAEEventHandlerProc( SectionScrollAEHandler );
  606.         THROW_IF_ERROR(AEInstallEventHandler(sectionEventMsgClass, sectionScrollMsgID,
  607.                         theHandlerUPP, (long)somSelf, kNotSysHandler));
  608.  
  609. #endif //
  610.  
  611. #ifdef _PLATFORM_OS2_
  612.         SOM_CATCH_ALL
  613.         SOM_ENDTRY
  614.                 return;
  615. #endif
  616. }
  617.  
  618. //------------------------------------------------------------------------------
  619. // ODLinkManager::NewSectionID
  620. //------------------------------------------------------------------------------
  621.  
  622. SOM_Scope ODULong  SOMLINK ODLinkManagerNewSectionID(ODLinkManager *somSelf, Environment *ev,
  623.                 ODDraft* draft)
  624. {
  625.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  626.     ODLinkManagerMethodDebug("ODLinkManager","NewSectionID");
  627.  
  628.         ODULong sectionID;
  629.  
  630. #ifdef _PLATFORM_MACINTOSH_
  631.         SOM_CATCH return 0;
  632. #else
  633.         SOM_TRY
  634. #endif
  635.  
  636.         do
  637.                 sectionID = ReserveGlobalID(ev, draft, kODPropSectionID);
  638.         while
  639.                 ( SectionIDReserved(ev, draft, sectionID) );
  640.  
  641.         return sectionID;
  642.  
  643. #ifdef _PLATFORM_OS2_
  644.         SOM_CATCH_ALL
  645.         SOM_ENDTRY
  646.                 return 0;
  647. #endif
  648. }
  649.  
  650. //------------------------------------------------------------------------------
  651. // ODLinkManager::ReserveSectionID
  652. //------------------------------------------------------------------------------
  653.  
  654. SOM_Scope ODBoolean  SOMLINK ODLinkManagerReserveSectionID(ODLinkManager *somSelf, Environment *ev,
  655.                 ODULong sectionID,
  656.                 ODDraft* draft)
  657. {
  658.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  659.     ODLinkManagerMethodDebug("ODLinkManager","ReserveSectionID");
  660.  
  661. #ifdef _PLATFORM_MACINTOSH_
  662.         SOM_CATCH return kODFalse;
  663. #else
  664.         SOM_TRY
  665. #endif
  666.  
  667.         ODBoolean result = kODFalse;
  668.  
  669.         if ( sectionID >= PeekNextGlobalID(ev, draft, kODPropSectionID) )
  670.                 if ( !SectionIDReserved(ev, draft, sectionID) )
  671.                 {
  672.                         AppendULongToDraftProperty(ev, draft, kODPropReservedSectionIDs, sectionID);
  673.                         result = kODTrue;
  674.                 }
  675.  
  676.         return result;
  677.  
  678. #ifdef _PLATFORM_OS2_
  679.         SOM_CATCH_ALL
  680.         SOM_ENDTRY
  681.                 return kODFalse;
  682. #endif
  683. }
  684.  
  685. #ifdef _PLATFORM_OS2_
  686. //-------------------------------------------------------------------------------------
  687. // ODLinkManager: ShowLinkUpdateAlert
  688. //-------------------------------------------------------------------------------------
  689.  
  690. SOM_Scope ODLinkUpdateResult  SOMLINK ODLinkManagerShowLinkUpdateAlert(ODLinkManager *somSelf, Environment *ev,
  691.                 ODBaseLinkSource* link)
  692. {
  693.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  694.     ODLinkManagerMethodDebug("ODLinkManager","ShowLinkUpdateAlert");
  695.  
  696.     ODLinkUpdateResult result = kODLinkUpdateContinue; ODVolatile(result);
  697.  
  698.     SOM_TRY
  699.     result = ( ::ShowLinkUpdateAlert() )?
  700.            kODLinkUpdateContinue : kODLinkUpdateStop;
  701.  
  702.     SOM_CATCH_ALL
  703.  
  704.     SOM_ENDTRY
  705.  
  706.     return result;
  707. }
  708. #endif
  709.  
  710. #ifdef _PLATFORM_MACINTOSH_
  711. //-------------------------------------------------------------------------------------
  712. // ODLinkManager: ShowLinkUpdateAlert
  713. //-------------------------------------------------------------------------------------
  714.  
  715. SOM_Scope ODLinkUpdateResult  SOMLINK ODLinkManagerShowLinkUpdateAlert(ODLinkManager *somSelf, Environment *ev,
  716.                 ODBaseLinkSource* link)
  717. {
  718.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  719.     ODLinkManagerMethodDebug("ODLinkManager","ShowLinkUpdateAlert");
  720.  
  721.     ODLinkUpdateResult result = kODLinkUpdateContinue; ODVolatile(result);
  722.  
  723.     SOM_TRY
  724.  
  725.                 // When we leave the background, iterate over all the link sources
  726.                 // that attempted to call ShowLinkUpdateAlert() in the background,
  727.                 // showing the alert now and calling the links back with the
  728.                 // resulting information.
  729.  
  730.                 if ( _fInBackground )
  731.                 {
  732.                         result = kODLinkUpdateStop;
  733.                         if ( !_fSuspendedUpdateLinks->Contains(link) )
  734.                                 _fSuspendedUpdateLinks->AddLastAndAcquire(link);
  735.                         result = kODLinkUpdateSuspend;
  736.                 }
  737.                 else
  738.                 {
  739.                         result = ( ::ShowLinkUpdateAlert() )?
  740.                                 kODLinkUpdateContinue : kODLinkUpdateStop;
  741.                 }
  742.  
  743.         SOM_CATCH_ALL
  744.  
  745.         SOM_ENDTRY
  746.  
  747.         return result;
  748. }
  749. #endif
  750.  
  751. //-------------------------------------------------------------------------------------
  752. // ODLinkManager: SuspendResume
  753. //-------------------------------------------------------------------------------------
  754.  
  755. #if ODDebug
  756. extern int gODButtonKeyNoEnterOkay;
  757. #endif
  758. #ifdef _PLATFORM_MACINTOSH_
  759. SOM_Scope void  SOMLINK ODLinkManagerSuspendResume(ODLinkManager *somSelf, Environment *ev,
  760.                 ODEventData* event)
  761. {
  762.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  763.     ODLinkManagerMethodDebug("ODLinkManager","SuspendResume");
  764.  
  765.         const short kResumeMask = 0x01; // High byte suspend/resume event
  766.  
  767.         ODBoolean inBackground = (event->message & kResumeMask) == 0;
  768.         _fInBackground = inBackground;
  769.  
  770.     SOM_TRY
  771.  
  772.                 // When we leave the background, show the alert and iterate over all the
  773.                 // link sources that attempted to call ShowLinkUpdateAlert() in the
  774.                 // background, calling the links back with the resulting alert information.
  775.  
  776.                 if ( !inBackground )
  777.                 {
  778.                         ODBaseLinkSource* link = (ODBaseLinkSource*) _fSuspendedUpdateLinks->RemoveFirst();
  779.                         if (link)
  780.                         {
  781.  
  782.                                 ODLinkUpdateResult result = ( ::ShowLinkUpdateAlert() )?
  783.                                         kODLinkUpdateContinue : kODLinkUpdateStop;
  784.  
  785.                                 while ( link )
  786.                                 {
  787.                                         TempODRefCntObject tempLink = link; // ensure it is released
  788.  
  789.                                         // Ignore errors returned by LinkUpdateAlertCompleted
  790.                                         TRY
  791.                                                 link->LinkUpdateAlertCompleted(ev, result);
  792.                                         CATCH_ALL
  793.                                         ENDTRY
  794.  
  795.                                         link = (ODBaseLinkSource*) _fSuspendedUpdateLinks->RemoveFirst();
  796.                                 }
  797.                         }
  798.                 }
  799.  
  800.         SOM_CATCH_ALL
  801.  
  802.         SOM_ENDTRY
  803. }
  804. #endif
  805.  
  806. //------------------------------------------------------------------------------
  807. // ODLinkManager::UnsavedExportedLinks
  808. //------------------------------------------------------------------------------
  809.  
  810. SOM_Scope ODBoolean  SOMLINK ODLinkManagerUnsavedExportedLinks(ODLinkManager *somSelf, Environment *ev,
  811.                 ODDraft* draft)
  812. {
  813.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  814.     ODLinkManagerMethodDebug("ODLinkManager","UnsavedExportedLinks");
  815.  
  816. #ifdef _PLATFORM_MACINTOSH_
  817.         SOM_CATCH return kODFalse;
  818. #else
  819.         SOM_TRY
  820. #endif
  821.  
  822.         DraftInfo* info = somSelf->GetDraftInfo(ev, draft);
  823.  
  824.         if ( info != (DraftInfo*) kODNULL )
  825.         {
  826.                 return info->newExportedLinks->Count() > 0;
  827.         }
  828.         else
  829.                 return kODFalse;
  830.  
  831. #ifdef _PLATFORM_OS2_
  832.         SOM_CATCH_ALL
  833.         SOM_ENDTRY
  834.                 return kODFalse;
  835. #endif
  836. }
  837.  
  838. //------------------------------------------------------------------------------
  839. // ODLinkManager::AnyLinkImported
  840. //------------------------------------------------------------------------------
  841.  
  842. SOM_Scope ODBoolean  SOMLINK ODLinkManagerAnyLinkImported(ODLinkManager *somSelf, Environment *ev,
  843.                 ODDraft* draft)
  844. {
  845.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  846.     ODLinkManagerMethodDebug("ODLinkManager","AnyLinkImported");
  847.  
  848. #ifdef _PLATFORM_MACINTOSH_
  849.         SOM_CATCH return kODFalse;
  850. #else
  851.         SOM_TRY
  852. #endif
  853.         DraftInfo* info = somSelf->GetDraftInfo(ev, draft);
  854.  
  855.         if ( info != (DraftInfo*) kODNULL )
  856.         {
  857.                 return info->linksImported;
  858.         }
  859.         else
  860.                 return kODFalse;
  861.  
  862. #ifdef _PLATFORM_OS2_
  863.         SOM_CATCH_ALL
  864.         SOM_ENDTRY
  865.                 return kODFalse;
  866. #endif
  867. }
  868.  
  869. //------------------------------------------------------------------------------
  870. // ODLinkManager::DraftOpened
  871. //------------------------------------------------------------------------------
  872.  
  873. SOM_Scope void  SOMLINK ODLinkManagerDraftOpened(ODLinkManager *somSelf, Environment *ev,
  874.                 ODDraft* draft)
  875. {
  876.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  877.     ODLinkManagerMethodDebug("ODLinkManager","DraftOpened");
  878.  
  879. #ifdef _PLATFORM_MACINTOSH_
  880.         SOM_CATCH return;
  881. #else
  882.         SOM_TRY
  883. #endif
  884.  
  885.         DraftInfo* info = somSelf->GetDraftInfo(ev, draft);
  886.  
  887.         if ( info == (DraftInfo*) kODNULL )
  888.         {
  889.                 somSelf->NewDraftInfo(ev, draft);
  890.                 RegisterSubscribingLinks(ev, draft);
  891.                 RegisterPublishingLinks(ev, draft);
  892.         }
  893.  
  894.  
  895. #ifdef _PLATFORM_OS2_
  896.         SOM_CATCH_ALL
  897.         SOM_ENDTRY
  898.                 return;
  899. #endif
  900. }
  901.  
  902. //------------------------------------------------------------------------------
  903. // ODLinkManager::DraftSaved
  904. //------------------------------------------------------------------------------
  905.  
  906. SOM_Scope void  SOMLINK ODLinkManagerDraftSaved(ODLinkManager *somSelf, Environment *ev,
  907.                 ODDraft* draft)
  908. {
  909.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  910.     ODLinkManagerMethodDebug("ODLinkManager","DraftSaved");
  911.  
  912.  
  913. #ifdef _PLATFORM_MACINTOSH_
  914.         SOM_CATCH return;
  915. #else
  916.         SOM_TRY
  917. #endif
  918.         DraftInfo* info = somSelf->GetDraftInfo(ev, draft);
  919.  
  920.         if ( info != (DraftInfo*) kODNULL )
  921.         {
  922.                 UpdateEditionFiles(ev, info->linksToExport);
  923.                 // old: RemoveAllLinksInCollection(ev, info->linksToExport);
  924.                 info->linksToExport->RemoveAndReleaseAll();
  925.  
  926.                 DeleteEditionFiles(ev, info->linksToDelete);
  927.                 // old: RemoveAllLinksInCollection(ev, info->linksToDelete);
  928.                 info->linksToDelete->RemoveAndReleaseAll();
  929.  
  930.                 // old: RemoveAllLinksInCollection(ev, info->newExportedLinks);
  931.                 info->newExportedLinks->RemoveAndReleaseAll();
  932.  
  933.                 info->linksImported = kODFalse;
  934.         }
  935.         else
  936.         {
  937. //                WARN("Draft is not opened yet.");
  938.         }
  939.  
  940. #ifdef _PLATFORM_OS2_
  941.         SOM_CATCH_ALL
  942.         SOM_ENDTRY
  943.                 return;
  944. #endif
  945. }
  946.  
  947. //------------------------------------------------------------------------------
  948. // ODLinkManager::DraftClosing
  949. //------------------------------------------------------------------------------
  950.  
  951. SOM_Scope void  SOMLINK ODLinkManagerDraftClosing(ODLinkManager *somSelf, Environment *ev,
  952.                 ODDraft* draft)
  953. {
  954.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  955.     ODLinkManagerMethodDebug("ODLinkManager","DraftClosing");
  956.  
  957. #ifdef _PLATFORM_MACINTOSH_
  958.         SOM_CATCH return;
  959. #else
  960.         SOM_TRY
  961. #endif
  962.  
  963.         DraftInfo* info = somSelf->GetDraftInfo(ev, draft);
  964.  
  965.         if ( info != (DraftInfo*) kODNULL )
  966.         {
  967.                 // old: RemoveAllLinksInCollection(ev, info->linksToExport);
  968.                 // new: info->linksToExport->RemoveAndReleaseAll();
  969.                 ODDeleteObject(info->linksToExport); // calls RemoveAndReleaseAll()
  970.  
  971.                 // old: RemoveAllLinksInCollection(ev, info->linksToDelete);
  972.                 // new: info->linksToDelete->RemoveAndReleaseAll();
  973.                 ODDeleteObject(info->linksToDelete); // calls RemoveAndReleaseAll()
  974.  
  975.                 DeleteEditionFiles(ev, info->newExportedLinks);
  976.                 // old: RemoveAllLinksInCollection(ev, info->newExportedLinks);
  977.                 // new: info->newExportedLinks->RemoveAndReleaseAll();
  978.                 ODDeleteObject(info->newExportedLinks); // calls RemoveAndReleaseAll()
  979.  
  980.                 _fDraftInfos->Remove(info);
  981.                 delete info;
  982.         }
  983.  
  984.  
  985. #ifdef _PLATFORM_OS2_
  986.         SOM_CATCH_ALL
  987.         SOM_ENDTRY
  988.                 return;
  989. #endif
  990. }
  991.  
  992. //------------------------------------------------------------------------------
  993. // ODLinkManager::ExportOnSave
  994. //------------------------------------------------------------------------------
  995.  
  996. SOM_Scope ODBoolean  SOMLINK ODLinkManagerExportOnSave(ODLinkManager *somSelf, Environment *ev,
  997.                 ODLink* link,
  998.                 ODBoolean doExport)
  999. {
  1000.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1001.     ODLinkManagerMethodDebug("ODLinkManager","ExportOnSave");
  1002.  
  1003.         ODBoolean wereToExport = kODFalse;
  1004.  
  1005. #ifdef _PLATFORM_MACINTOSH_
  1006.         SOM_CATCH return kODFalse;
  1007. #else
  1008.         SOM_TRY
  1009. #endif
  1010.  
  1011.         DraftInfo* info = somSelf->GetDraftInfo(ev, link->GetStorageUnit(ev)->GetDraft(ev));
  1012.         if ( info != (DraftInfo*) kODNULL )
  1013.         {
  1014.                 wereToExport = info->linksToExport->Contains(link);
  1015.                 if ( wereToExport && !doExport )
  1016.                 {
  1017.                         info->linksToExport->RemoveAndRelease(link);
  1018.                         // link->Release(ev);
  1019.                 }
  1020.                 else if ( !wereToExport && doExport )
  1021.                 {
  1022.                         info->linksToExport->AddLastAndAcquire(link);
  1023.                         // link->Acquire(ev);
  1024.                 }
  1025.         }
  1026.  
  1027.         return wereToExport;
  1028.  
  1029. #ifdef _PLATFORM_OS2_
  1030.         SOM_CATCH_ALL
  1031.         SOM_ENDTRY
  1032.                 return kODFalse;
  1033. #endif
  1034. }
  1035.  
  1036. //------------------------------------------------------------------------------
  1037. // ODLinkManager::DeleteOnSave
  1038. //------------------------------------------------------------------------------
  1039.  
  1040. SOM_Scope ODBoolean  SOMLINK ODLinkManagerDeleteOnSave(ODLinkManager *somSelf, Environment *ev,
  1041.                 ODLink* link,
  1042.                 ODBoolean doDelete)
  1043. {
  1044.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1045.     ODLinkManagerMethodDebug("ODLinkManager","DeleteOnSave");
  1046.  
  1047.         ODBoolean wereToDelete = kODFalse;
  1048.  
  1049.  
  1050. #ifdef _PLATFORM_MACINTOSH_
  1051.         SOM_CATCH return kODFalse;
  1052. #else
  1053.         SOM_TRY
  1054. #endif
  1055.  
  1056.         DraftInfo* info = somSelf->GetDraftInfo(ev, link->GetStorageUnit(ev)->GetDraft(ev));
  1057.         if ( info != (DraftInfo*) kODNULL )
  1058.         {
  1059.                 wereToDelete = info->linksToDelete->Contains(link);
  1060.                 if ( wereToDelete && !doDelete )
  1061.                 {
  1062.                         info->linksToDelete->RemoveAndRelease(link);
  1063.                         // link->Release(ev);
  1064.                 }
  1065.                 else if ( !wereToDelete && doDelete )
  1066.                 {
  1067.                         info->linksToDelete->AddLastAndAcquire(link);
  1068.                         // link->Acquire(ev);
  1069.                 }
  1070.         }
  1071.  
  1072.         return wereToDelete;
  1073.  
  1074. #ifdef _PLATFORM_OS2_
  1075.         SOM_CATCH_ALL
  1076.         SOM_ENDTRY
  1077.                 return kODFalse;
  1078. #endif
  1079. }
  1080.  
  1081. //------------------------------------------------------------------------------
  1082. // ODLinkManager::CreateLink
  1083. //------------------------------------------------------------------------------
  1084.  
  1085. SOM_Scope ODLinkSource*  SOMLINK ODLinkManagerCreateLink(ODLinkManager *somSelf, Environment *ev,
  1086.                 ODDraft* draft,
  1087.                 ODLinkSpec* linkSpec)
  1088. {
  1089.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1090.     ODLinkManagerMethodDebug("ODLinkManager","CreateLink");
  1091.  
  1092.  
  1093. #ifdef _PLATFORM_OS2_ // CED
  1094.   _interrupt(3);
  1095.   return 0;
  1096. #else
  1097.         ODLinkSource*                   linkSource = kODNULL;   ODVolatile(linkSource);
  1098.         AppleEvent                              theAppleEvent, replyAppleEvent;
  1099.         AEAddressDesc                   targetAddress;
  1100.         OSErr                                   error;
  1101.         ProcessSerialNumber     psn;
  1102.  
  1103.         SOM_TRY
  1104.  
  1105.                 // Create a link source to return
  1106.                 linkSource = draft->CreateLinkSource(ev, (ODPart*) kODNULL);
  1107.  
  1108.                 // Send an Apple Event to the this process using GetCurrentProcess
  1109.                 // so event is delivered as a high-level event
  1110.                 GetCurrentProcess(&psn);
  1111.                 error = AECreateDesc(typeProcessSerialNumber, &psn,
  1112.                                                         sizeof(ProcessSerialNumber), &targetAddress);
  1113.                 THROW_IF_ERROR(error);
  1114.                 TempAEDesc tmpTargetAddress(&targetAddress);
  1115.  
  1116.                 error = AECreateAppleEvent(kODShellSignature, kODRequestLinkEventID,
  1117.                                                         &targetAddress, kAutoGenerateReturnID,
  1118.                                                         kAnyTransactionID, &theAppleEvent);
  1119.                 THROW_IF_ERROR(error);
  1120.                 TempAEDesc tmpAppleEvent(&theAppleEvent);
  1121.  
  1122.                 // Add the link spec to the Apple event
  1123.                 linkSpec->WriteToAppleEvent(ev, &theAppleEvent);
  1124.  
  1125.                 // Add the source link to the Apple event
  1126.                 error = AEPutParamPtr(&theAppleEvent, kODLinkSourceKey, typeLongInteger,
  1127.                                                         &linkSource, sizeof(linkSource));
  1128.                 THROW_IF_ERROR(error);
  1129.  
  1130.                 error = AESend(&theAppleEvent, &replyAppleEvent, kAENoReply | kAENeverInteract,
  1131.                                                         kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  1132.                 THROW_IF_ERROR(error);
  1133.  
  1134.         SOM_CATCH_ALL
  1135.  
  1136.                 ODSafeReleaseObject(linkSource);
  1137.                 linkSource = kODNULL;
  1138.  
  1139.         SOM_ENDTRY
  1140.  
  1141.         return linkSource;
  1142.  
  1143. #endif
  1144. }
  1145.  
  1146. #ifdef _PLATFORM_MACINTOSH_ // CED
  1147. //------------------------------------------------------------------------------
  1148. // GetLinkAEHandler
  1149. //------------------------------------------------------------------------------
  1150.  
  1151. ODStatic OSErr ODPascal GetLinkAEHandler(
  1152.                 const AppleEvent* theAppleEvent,
  1153.                 AppleEvent* reply,
  1154.                 long refCon)
  1155. {
  1156.         ODLinkManager*          somSelf = (ODLinkManager*)refCon;
  1157.     Environment*                ev = somGetGlobalEnvironment ();
  1158.         ODLinkSpec*                     linkSpec = (ODLinkSpec*) kODNULL; ODVolatile(linkSpec);
  1159.         ODPart*                         part;
  1160.         ODLinkKey                       key;
  1161.         AliasHandle                     editionAlias;
  1162.         OSErr                           error = noErr;
  1163.         PlatformFile*           documentFile = kODNULL; ODVolatile(documentFile);
  1164.  
  1165. #if ODDebug
  1166.         somPrintf("ODLinkManager: Get Link event received\n");
  1167. #endif
  1168.  
  1169.         TRY
  1170.                 linkSpec = new ODLinkSpec;      // Can't use factory method; no draft object!
  1171.                 THROW_IF_NULL(linkSpec, kODErrOutOfMemory);
  1172.                 linkSpec->ReadFromAppleEvent(ev, (AppleEvent*) theAppleEvent);
  1173.  
  1174.                 part = linkSpec->GetPart(ev);
  1175.  
  1176.                 DraftInfo* info = somSelf->GetDraftInfo(ev, part->GetStorageUnit(ev)->GetDraft(ev));
  1177.                 if ( info == (DraftInfo*) kODNULL )
  1178.                 {
  1179.                         // Document has never been saved, so refuse to create a link
  1180.                         error = kODErrDocNotSaved;
  1181.                 }
  1182.                 else
  1183.                 {
  1184.                         TempODLink link = kODNULL;
  1185.                         ODByteArray partData = linkSpec->GetPartData(ev);
  1186.                         ODVolatile(partData);
  1187.                         {
  1188.                                 TempODLinkSource linkSource = kODNULL;
  1189.                                 ODVolatile(linkSource);
  1190.                                 TRY
  1191.                                         linkSource = part->CreateLink(ev, &partData);
  1192.                                         THROW_IF_NULL(linkSource,kODErrCannotCreateLink);
  1193.                                         link = linkSource->GetLink(ev);
  1194.                                         THROW_IF_NULL(link,kODErrCannotCreateLink);
  1195.                                         link->Acquire(ev);
  1196.                                 CATCH_ALL
  1197.                                         DisposeByteArrayStruct(partData);
  1198.                                         RERAISE;
  1199.                                 ENDTRY
  1200.                                 DisposeByteArrayStruct(partData);
  1201.                         }
  1202.  
  1203.                         if ( link->Lock(ev,0,&key) )
  1204.                         {
  1205.                                 TRY
  1206.                                         editionAlias = link->GetLinkEdition(ev, key);
  1207.                                         if ( editionAlias == (AliasHandle) kODNULL )
  1208.                                         {
  1209.                                                 ODDraft* draft = part->GetStorageUnit(ev)->GetDraft(ev);
  1210.                                                 ODContainer* container = draft->GetDocument(ev)->GetContainer(ev);;
  1211.  
  1212.                                                 documentFile = GetPlatformFileFromContainer(ev, container);
  1213.                                                 ODFileSpec documentSpec = documentFile->GetFileSpec();
  1214.  
  1215.                                                 EditionContainerSpec editionContainer = NewEditionFile(ev, documentFile, draft);
  1216.  
  1217.                                                 editionAlias = link->CreateLinkEdition(ev,
  1218.                                                                                                 editionContainer,
  1219.                                                                                                 &documentSpec,
  1220.                                                                                                 somSelf->NewSectionID(ev, draft),
  1221.                                                                                                 key);
  1222.  
  1223.                                                 info->newExportedLinks->AddLastAndAcquire(link);
  1224.                                                 // link->Acquire(ev);
  1225.                                         }
  1226.                                         else
  1227.                                         {
  1228.                                                 // If a new cross-document destination is being created, and the edition
  1229.                                                 // file is waiting for a save to update, force the edition file to update
  1230.                                                 // now so the link content is the same as the content that would be pasted.
  1231.                                                 // [cc; #1190727]
  1232.                                                 DraftInfo* info = somSelf->GetDraftInfo(ev, link->GetStorageUnit(ev)->GetDraft(ev));
  1233.                                                 if ( info && info->linksToExport->Contains(link) )
  1234.                                                 {
  1235.                                                         TRY
  1236.                                                                 link->UpdateLinkEdition(ev, key);
  1237.                                                                 info->linksToExport->RemoveAndRelease(link);
  1238.                                                         CATCH_ALL
  1239.                                                         ENDTRY
  1240.                                                 }
  1241.                                         }
  1242.                                 CATCH_ALL
  1243.                                         link->Unlock(ev, key);
  1244.                                         ODDeleteObject(documentFile);
  1245.                                         RERAISE;
  1246.                                 ENDTRY
  1247.                                 link->Unlock(ev, key);
  1248.                                 ODDeleteObject(documentFile);
  1249.                         }
  1250.  
  1251.                         error = AEPutParamPtr(reply, keyEditionAlias, typeAlias,
  1252.                                                                 &editionAlias, sizeof(editionAlias));
  1253.                 }
  1254.         CATCH_ALL
  1255.  
  1256.                 // Must catch any exceptions thrown!
  1257.                 error = ErrorCode();
  1258.         ENDTRY
  1259.  
  1260.         delete linkSpec;
  1261.  
  1262.         return error;
  1263. }
  1264.  
  1265. //------------------------------------------------------------------------------
  1266. // RequestLinkAEHandler
  1267. //------------------------------------------------------------------------------
  1268.  
  1269. ODStatic OSErr ODPascal RequestLinkAEHandler(
  1270.                 const AppleEvent* requestLinkAppleEvent,
  1271.                 AppleEvent* reply,
  1272.                 long refCon)
  1273. {
  1274.         ODLinkManager*                  somSelf = (ODLinkManager*) refCon;
  1275.     Environment*                        ev = somGetGlobalEnvironment();
  1276.         AppleEvent                              getLinkAppleEvent;
  1277.         AppleEvent                              replyAppleEvent;
  1278.         AEAddressDesc                   targetAddress;
  1279.         ProcessSerialNumber     psn;
  1280.         DescType                                returnedType;
  1281.         Size                                    actualSize;
  1282.         AliasHandle                             editionAlias;
  1283.         EditionContainerSpec    editionContainer;
  1284.         OSErr                                   error;
  1285.         ODBoolean                               wasChanged;
  1286.         const long                              kGetLinkReplyTimeout = 600;     // Ten seconds
  1287.  
  1288.         ODLinkSource*   linkSource = kODNULL;   ODVolatile(linkSource);
  1289.         ODLinkSpec*             linkSpec = kODNULL;             ODVolatile(linkSpec);
  1290.  
  1291.         TRY
  1292.  
  1293.                 linkSpec = new ODLinkSpec;      // Can't use factory method; no draft object!
  1294.                 THROW_IF_NULL(linkSpec, kODErrOutOfMemory);
  1295.  
  1296.                 linkSpec->ReadFromAppleEvent(ev, (AppleEvent*) requestLinkAppleEvent);
  1297.  
  1298.                 error = AEGetParamPtr(requestLinkAppleEvent, kODLinkSourceKey,
  1299.                                                                 typeLongInteger, &returnedType,
  1300.                                                                 &linkSource, sizeof(linkSource), &actualSize);
  1301.                 THROW_IF_ERROR(error);
  1302.  
  1303.                 // Create the GetLink event
  1304.  
  1305.                 linkSpec->GetOriginatingProcessID(ev, &psn);
  1306.  
  1307.                 error = AECreateDesc(typeProcessSerialNumber, &psn,
  1308.                                                         sizeof(ProcessSerialNumber), &targetAddress);
  1309.                 THROW_IF_ERROR(error);
  1310.                 TempAEDesc tmpTargetAddress(&targetAddress);
  1311.  
  1312.                 error = AECreateAppleEvent(kODShellSignature, kODGetLinkEventID,
  1313.                                                         &targetAddress, kAutoGenerateReturnID,
  1314.                                                         kAnyTransactionID, &getLinkAppleEvent);
  1315.                 THROW_IF_ERROR(error);
  1316.                 TempAEDesc tmpGetLinkAppleEvent(&getLinkAppleEvent);
  1317.  
  1318.                 linkSpec->WriteToAppleEvent(ev, &getLinkAppleEvent);
  1319.  
  1320.                 error = AESend(&getLinkAppleEvent, &replyAppleEvent,
  1321.                                                         kAEWaitReply | kAENeverInteract, kAENormalPriority,
  1322.                                                         kGetLinkReplyTimeout, NULL, NULL);
  1323.                 TempAEDesc tmpReplyAppleEvent(&replyAppleEvent);
  1324.                 THROW_IF_ERROR(error);
  1325.  
  1326.                 // Process the reply
  1327.  
  1328.                 error = AEGetParamPtr(&replyAppleEvent, keyEditionAlias, typeAlias, &returnedType,
  1329.                                                                 &editionAlias, sizeof(editionAlias), &actualSize);
  1330.                 THROW_IF_ERROR(error);
  1331.  
  1332.                 error = ResolveAlias(nil, editionAlias, &editionContainer.theFile, &wasChanged);
  1333.                 THROW_IF_ERROR(error);
  1334.  
  1335.                 editionContainer.theFileScript = smSystemScript;
  1336.                 editionContainer.thePart = kPartsNotUsed;
  1337.  
  1338.                 ODDraft* draft = linkSource->GetStorageUnit(ev)->GetDraft(ev);
  1339.  
  1340.                 ODContainer* container = draft->GetDocument(ev)->GetContainer(ev);;
  1341.                 ODFileSpec documentSpec = GetODFileSpecFromContainer(ev, container);
  1342.  
  1343.                 ODLinkKey key;  ODVolatile(key);
  1344.                 if ( linkSource->Lock(ev,0,&key) )
  1345.                 {
  1346.                         TRY
  1347.                                 linkSource->UseLinkEdition(ev,
  1348.                                                                         editionContainer,
  1349.                                                                         &documentSpec,
  1350.                                                                         somSelf->NewSectionID(ev, draft),
  1351.                                                                         key);
  1352.                         CATCH_ALL
  1353.                                 linkSource->Unlock(ev, key);
  1354.                                 RERAISE;
  1355.                         ENDTRY
  1356.                         linkSource->Unlock(ev, key);
  1357.                 }
  1358.  
  1359.         CATCH_ALL
  1360.  
  1361.                 if ( linkSource )
  1362.                 {
  1363.                         linkSource->SetStatus(ev, kODErrCannotEstablishLink);
  1364.  
  1365.                         // Ignore errors returned by UpdateDependents
  1366.                         TRY
  1367.                                 linkSource->GetLink(ev)->UpdateDependents(ev);
  1368.                         CATCH_ALL
  1369.                         ENDTRY
  1370.                 }
  1371.  
  1372.         ENDTRY
  1373.  
  1374.         ODDeleteObject(linkSpec);
  1375.  
  1376.         return noErr;
  1377. }
  1378.  
  1379. //------------------------------------------------------------------------------
  1380. // SectionReadAEHandler
  1381. //------------------------------------------------------------------------------
  1382.  
  1383. ODStatic OSErr ODPascal SectionReadAEHandler(
  1384.                 const AppleEvent* theAppleEvent,
  1385.                 const AppleEvent* reply,
  1386.                 long refCon)
  1387. {
  1388.         ODUnused(reply);
  1389.  
  1390.         ODLinkManager*          somSelf = (ODLinkManager*)refCon;
  1391.     Environment*                ev = somGetGlobalEnvironment ();
  1392.         SectionHandle           section = kODNULL;
  1393.         OSErr                           error;
  1394.  
  1395.         error = GetSectionFromEvent(theAppleEvent, §ion);
  1396.  
  1397. #if ODDebug
  1398.                 somPrintf("ODLinkManager: Section Read event received; section = %x, linkSource = %x\n",
  1399.                                         section, (**section).refCon);
  1400. #endif
  1401.  
  1402.         if ( error == noErr )
  1403.         {
  1404.                 if ( IsRegisteredSection(section) == noErr )
  1405.                 {
  1406.                         ODLinkSource* linkSource = (ODLinkSource*) (*section)->refCon;
  1407.                         ODLinkKey key;
  1408.  
  1409.                         if ( linkSource->Lock(ev,0,&key) )
  1410.                         {
  1411.                                 TRY
  1412.                                         linkSource->ReadLinkEdition(ev,key);
  1413.                                         DraftInfo* info = somSelf->GetDraftInfo(ev, linkSource->GetStorageUnit(ev)->GetDraft(ev));
  1414.                                         if ( info != (DraftInfo*) kODNULL )
  1415.                                         {
  1416.                                                 info->linksImported = kODTrue;
  1417.                                         }
  1418.                                 CATCH_ALL
  1419.                                         error = ErrorCode();
  1420.                                 ENDTRY
  1421.                                 linkSource->Unlock(ev, key);
  1422.                         }
  1423.                 }
  1424.         }
  1425.  
  1426.         return error;
  1427. }
  1428.  
  1429. //------------------------------------------------------------------------------
  1430. // SectionWriteAEHandler
  1431. //------------------------------------------------------------------------------
  1432.  
  1433. ODStatic OSErr ODPascal SectionWriteAEHandler(
  1434.                 const AppleEvent* theAppleEvent,
  1435.                 const AppleEvent* reply,
  1436.                 long refCon)
  1437. {
  1438.         ODUnused(reply);
  1439.  
  1440.         ODLinkManager*          somSelf = (ODLinkManager*)refCon;
  1441.     Environment*                ev = somGetGlobalEnvironment ();
  1442.         ODLinkKey                       key;
  1443.         SectionHandle           section = kODNULL;
  1444.         OSErr                           error;
  1445.  
  1446.         error = GetSectionFromEvent(theAppleEvent, §ion);
  1447.  
  1448. #if ODDebug
  1449.                 somPrintf("ODLinkManager: Section Write event received; section = %x, link = %x\n",
  1450.                                         section, (**section).refCon);
  1451. #endif
  1452.  
  1453.         if ( error == noErr )
  1454.         {
  1455.                 if ( IsRegisteredSection(section) == noErr )
  1456.                 {
  1457.                         ODLink* link = (ODLink*) (**section).refCon;
  1458.  
  1459.                         // Force ExportLinkEdition to write the edition file
  1460.                         (**section).mdDate = (TimeStamp) 0;
  1461.  
  1462.                         if ( link->Lock(ev,0,&key) )
  1463.                         {
  1464.                                 TRY
  1465.                                         link->UpdateLinkEdition(ev, key);
  1466.                                 CATCH_ALL
  1467.                                         error = ErrorCode();
  1468.                                 ENDTRY
  1469.                                 link->Unlock(ev, key);
  1470.                         }
  1471.                 }
  1472.         }
  1473.  
  1474.         return error;
  1475. }
  1476.  
  1477. //------------------------------------------------------------------------------
  1478. // BuildErrorString
  1479. //------------------------------------------------------------------------------
  1480.  
  1481. ODStatic StringPtr BuildErrorString(ODSShort strResourceID, ODError error)
  1482. {
  1483.         Str63 numberString;
  1484.         Str255 msgStr;
  1485.  
  1486.         StringPtr result = kODNULL;
  1487.  
  1488.         ODSLong savedRefNum;
  1489.         BeginUsingLibraryResources(savedRefNum);
  1490.  
  1491.         NumToString(error, numberString);
  1492.         ReplaceIntoString(strResourceID, numberString, kODNULL, msgStr);
  1493.  
  1494.         if ( msgStr[0] > 0 )
  1495.         {
  1496.                 ODULong ptrSize = msgStr[0]+1;
  1497.                 result = (StringPtr) ODNewPtr(ptrSize);
  1498.                 if ( result )
  1499.                         ODBlockMove((ODPtr) &msgStr, (ODPtr) result, ptrSize);
  1500.         }
  1501.  
  1502.         EndUsingLibraryResources(savedRefNum);
  1503.  
  1504.         return result;
  1505. }
  1506.  
  1507. //------------------------------------------------------------------------------
  1508. // SectionScrollAEHandler
  1509. //------------------------------------------------------------------------------
  1510.  
  1511. ODStatic OSErr ODPascal SectionScrollAEHandler(
  1512.                 const AppleEvent* theAppleEvent,
  1513.                 const AppleEvent* reply,
  1514.                 long refCon)
  1515. {
  1516.         ODUnused(reply);
  1517.  
  1518.         ODLinkManager*          somSelf = (ODLinkManager*)refCon;
  1519.     ODLinkManagerData*  somThis = ODLinkManagerGetData(somSelf);
  1520.     Environment*                ev = somGetGlobalEnvironment ();
  1521.         SectionHandle           section = kODNULL;
  1522.         OSErr                           error;
  1523.  
  1524.         error = GetSectionFromEvent(theAppleEvent, §ion);
  1525.  
  1526. #if ODDebug
  1527.         somPrintf("ODLinkManager: Section Scroll event received; section = %x, link = %x\n",
  1528.                                         section, (section ? (**section).refCon : 0));
  1529. #endif
  1530.  
  1531.         if ( error == noErr )
  1532.         {
  1533.                 if ( IsRegisteredSection(section) == noErr )
  1534.                 {
  1535.                         TRY
  1536.                         ODLink* link = (ODLink*) (**section).refCon;
  1537.  
  1538.                         if ( link != kODNULL )
  1539.                         {
  1540.                                 DraftInfo* info = somSelf->GetDraftInfo(ev, link->GetStorageUnit(ev)->GetDraft(ev));
  1541.                                 if ( info && info->linksToDelete && info->linksToDelete->Contains(link) )
  1542.                                         THROW(kODErrCannotRevealLink);
  1543.                                 else
  1544.                                         link->ShowSourceContent(ev);
  1545.                         }
  1546.                         CATCH_ALL
  1547.                                 error = ErrorCode();
  1548.  
  1549.                                 NMRemove(_fNMRecPtr);   // ensure not already queued
  1550.                                 _fNMRecPtr->qType = nmType;
  1551.                                 _fNMRecPtr->nmMark = 0;
  1552.                                 _fNMRecPtr->nmIcon = nil;
  1553.                                 _fNMRecPtr->nmSound = nil;
  1554.                                 ODDisposePtr((ODPtr) _fNMRecPtr->nmStr);
  1555.                                 _fNMRecPtr->nmStr = BuildErrorString(kODLinkMgrCannotShowSourceID, error);
  1556.                                 _fNMRecPtr->nmResp = _fNMResponseUPP;
  1557.                                 error = NMInstall(_fNMRecPtr);
  1558.                         ENDTRY
  1559.                 }
  1560.         }
  1561.  
  1562.         return error;
  1563. }
  1564.  
  1565. #endif // _PLATFORM_OS2_
  1566.  
  1567. //------------------------------------------------------------------------------
  1568. // NewEditionFile
  1569. //------------------------------------------------------------------------------
  1570.  
  1571. ODStatic EditionContainerSpec NewEditionFile(Environment* ev,
  1572.                         PlatformFile* documentFile,
  1573.                         ODDraft* draft)
  1574. {
  1575.         EditionContainerSpec    editionContainer;
  1576.         OSErr                                   error;
  1577.         Str255                                  documentName;
  1578.         ODFileSpec                              documentSpec = documentFile->GetFileSpec();
  1579.  
  1580. #ifdef _PLATFORM_OS2_
  1581. _interrupt( 3 );
  1582. #else
  1583.         do {
  1584.                 documentFile->GetAsciiName((char *) documentName, 254);
  1585.                 CToPascalString((char *) documentName);
  1586.                 EditionFileName(documentName, (ODUShort) NewEditionID(ev, draft));
  1587.                 error = FSMakeFSSpec(documentSpec.vRefNum,
  1588.                                                          documentSpec.parID,
  1589.                                                          documentName,
  1590.                                                          &(editionContainer.theFile));
  1591.                 }
  1592.         while (error == noErr);
  1593.  
  1594.         // Expect a file not found error
  1595.         if ( error != fnfErr )
  1596.                 THROW_IF_ERROR(error);
  1597.  
  1598.         editionContainer.theFileScript = smSystemScript;
  1599.         editionContainer.thePart = kPartsNotUsed;
  1600.  
  1601. #endif
  1602.         return editionContainer;
  1603. }
  1604.  
  1605. //------------------------------------------------------------------------------
  1606. // GetSectionFromEvent
  1607. //------------------------------------------------------------------------------
  1608.  
  1609. #ifdef _OSA_SUPPORT_
  1610.  
  1611. #ifdef _PLATFORM_MACINTOSH_
  1612. ODStatic OSErr GetSectionFromEvent(const AppleEvent* theAppleEvent,
  1613.                                 SectionHandle* section)
  1614. #else
  1615. ODStatic OSErr GetSectionFromEvent(const ODOSAEvent* theAppleEvent,
  1616.                                 SectionHandle* section)
  1617. #endif
  1618. {
  1619.         DescType                returnedType;
  1620.         Size                    actualSize;
  1621.         OSErr                   error;
  1622.  
  1623. #ifdef _PLATFORM_OS2_ // CED
  1624. #ifdef _OSA_SUPPORT_
  1625.   _interrupt(3);
  1626. #endif
  1627. #else
  1628.  
  1629.  
  1630.         error = AEGetParamPtr(theAppleEvent, keyDirectObject, typeSectionH, &returnedType,
  1631.                                                         section, sizeof(*section), &actualSize);
  1632. #if ODDebug
  1633.         if ( error != noErr )
  1634.                 somPrintf("ODLinkManager: Error %d getting section from event\n", error);
  1635. #endif
  1636.  
  1637. #endif
  1638.         return error;
  1639. }
  1640. #endif
  1641.  
  1642. //------------------------------------------------------------------------------
  1643. // EditionFileName
  1644. //------------------------------------------------------------------------------
  1645.  
  1646. ODStatic void EditionFileName(Str255 fileName, ODUShort numb)
  1647. {
  1648.         Str63 numberString;
  1649.  
  1650. #ifdef _PLATFORM_OS2_
  1651. _interrupt( 3 );
  1652. #else
  1653.         NumToString(numb, numberString);
  1654.  
  1655.         TruncateFileName(fileName, numberString);
  1656.  
  1657.         ODSLong savedRefNum;
  1658.         BeginUsingLibraryResources(savedRefNum);
  1659.  
  1660.         ReplaceIntoString(kODLinkMgrEditionTemplateID, fileName, numberString, fileName);
  1661.  
  1662.         EndUsingLibraryResources(savedRefNum);
  1663.  
  1664. #endif
  1665. }
  1666.  
  1667. //------------------------------------------------------------------------------
  1668. // TruncateFileName
  1669. //------------------------------------------------------------------------------
  1670.  
  1671. ODStatic void TruncateFileName(Str255 fileName, Str63 numberString)
  1672. {
  1673.         const maxFileNameLength = 31;
  1674.  
  1675. #ifdef _PLATFORM_OS2_
  1676. _interrupt( 3 );
  1677. #else
  1678.  
  1679.         Str255 emptyString;
  1680.         emptyString[0] = (unsigned char) 0;
  1681.  
  1682.         ODSLong savedRefNum;
  1683.         BeginUsingLibraryResources(savedRefNum);
  1684.  
  1685.         ReplaceIntoString(kODLinkMgrEditionTemplateID, emptyString, numberString, emptyString);
  1686.  
  1687.         EndUsingLibraryResources(savedRefNum);
  1688.  
  1689.         if ( (StrLength(fileName)+StrLength(emptyString)) > maxFileNameLength )
  1690.                 ClipStringToBytes( fileName,
  1691.                                 maxFileNameLength-StrLength(emptyString), smCurrentScript );
  1692.  
  1693. #endif
  1694. }
  1695.  
  1696. //------------------------------------------------------------------------------
  1697. // NewEditionID
  1698. //------------------------------------------------------------------------------
  1699.  
  1700. ODStatic ODULong NewEditionID(Environment *ev, ODDraft* draft)
  1701. {
  1702.         return ReserveGlobalID(ev, draft, kODPropEditionID);
  1703. }
  1704.  
  1705. //------------------------------------------------------------------------------
  1706. // PeekNextGlobalID
  1707. //------------------------------------------------------------------------------
  1708.  
  1709. ODStatic ODULong PeekNextGlobalID(Environment *ev,
  1710.                 ODDraft* draft,
  1711.                 ODPropertyName globalProp)
  1712. {
  1713.         TempODStorageUnit       draftPropertiesSU = draft->AcquireDraftProperties(ev);
  1714.         ODULong                 id = 0;
  1715.  
  1716.         if ( ODSUExistsThenFocus(ev, draftPropertiesSU, globalProp, kODULong) )
  1717.                 StorageUnitGetValue(draftPropertiesSU, ev, sizeof(ODULong), (ODValue) &id);
  1718.  
  1719.         return ++id;
  1720. }
  1721.  
  1722. //------------------------------------------------------------------------------
  1723. // ReserveGlobalID
  1724. //------------------------------------------------------------------------------
  1725.  
  1726. ODStatic ODULong ReserveGlobalID(Environment *ev,
  1727.                 ODDraft* draft,
  1728.                 ODPropertyName globalProp)
  1729. {
  1730.         TempODStorageUnit       draftPropertiesSU = draft->AcquireDraftProperties(ev);
  1731.         ODULong                 id = 0;
  1732.  
  1733.         ODSUForceFocus(ev, draftPropertiesSU, globalProp, kODULong);
  1734.         if ( draftPropertiesSU->GetSize(ev) > 0 )
  1735.                 id = ODGetULongProp(ev, draftPropertiesSU, globalProp, kODULong);
  1736.         ++id;
  1737.         ODSetULongProp(ev, draftPropertiesSU, globalProp, kODULong, id);
  1738.  
  1739.         return id;
  1740. }
  1741.  
  1742. //------------------------------------------------------------------------------
  1743. // ODLinkManager::NewDraftInfo
  1744. //------------------------------------------------------------------------------
  1745.  
  1746. SOM_Scope void  SOMLINK ODLinkManagerNewDraftInfo(ODLinkManager *somSelf, Environment *ev,
  1747.                 ODDraft* draft)
  1748. {
  1749.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1750.     ODLinkManagerMethodDebug("ODLinkManager","NewDraftInfo");
  1751.  
  1752. #ifdef _PLATFORM_MACINTOSH_
  1753.         SOM_CATCH return;
  1754. #else
  1755.         SOM_TRY
  1756. #endif
  1757.  
  1758.         // Create a new info structure for this draft
  1759.         DraftInfo* info = new DraftInfo;
  1760.  
  1761.         info->draft = draft;
  1762.         info->linksToExport = new ODRefCntCollection(ev);
  1763.         info->linksToDelete = new ODRefCntCollection(ev);
  1764.         info->newExportedLinks = new ODRefCntCollection(ev);
  1765.         info->linksImported = kODFalse;
  1766.         _fDraftInfos->AddLast(info);
  1767.  
  1768. #ifdef _PLATFORM_OS2_
  1769.         SOM_CATCH_ALL
  1770.         SOM_ENDTRY
  1771.                 return;
  1772. #endif
  1773. }
  1774.  
  1775. //------------------------------------------------------------------------------
  1776. // ODLinkManager::GetDraftInfo
  1777. //------------------------------------------------------------------------------
  1778.  
  1779. SOM_Scope DraftInfo*  SOMLINK ODLinkManagerGetDraftInfo(ODLinkManager *somSelf, Environment *ev,
  1780.                 ODDraft* draft)
  1781. {
  1782.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1783.     ODLinkManagerMethodDebug("ODLinkManager","GetDraftInfo");
  1784.  
  1785.         OrderedCollectionIterator* iter = kODNULL; ODVolatile(iter);
  1786.  
  1787.         DraftInfo* result = (DraftInfo*) kODNULL;
  1788.  
  1789.         if ( draft && _fDraftInfos )
  1790.         {
  1791.                 TRY
  1792.                         iter = _fDraftInfos->CreateIterator();
  1793.  
  1794.                         for (DraftInfo* info = (DraftInfo*) iter->First();
  1795.                                         iter->IsNotComplete();
  1796.                                         info = (DraftInfo*) iter->Next() )
  1797.                         {
  1798.                                 if ( info && draft->IsEqualTo(ev, info->draft) )
  1799.                                 {
  1800.                                         result = info;
  1801.                                         break;
  1802.                                 }
  1803.                         }
  1804.                 CATCH_ALL
  1805.                 ENDTRY
  1806.  
  1807.                 ODDeleteObject(iter);
  1808.         }
  1809.  
  1810.         return result;
  1811. }
  1812.  
  1813. //------------------------------------------------------------------------------
  1814. // ODLinkManager::DeleteDraftInfos
  1815. //------------------------------------------------------------------------------
  1816.  
  1817. // SOM_Scope void  SOMLINK ODLinkManagerDeleteDraftInfos(ODLinkManager *somSelf, Environment *ev)
  1818. // {
  1819. //     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1820. //     ODLinkManagerMethodDebug("ODLinkManager","DeleteDraftInfos");
  1821. //
  1822. //      SOM_CATCH return;
  1823. //
  1824. //      if (_fDraftInfos != (OrderedCollection*) kODNULL)
  1825. //      {
  1826. //              DraftInfo* info;
  1827. //              while ((info = (DraftInfo*) _fDraftInfos->First()) != (DraftInfo*) kODNULL)
  1828. //              {
  1829. //                      ODDeleteObject(info->linksToExport);
  1830. //                      ODDeleteObject(info->linksToDelete);
  1831. //                      ODDeleteObject(info->newExportedLinks);
  1832. //                      _fDraftInfos->RemoveFirst();
  1833. //                      delete info;
  1834. //              }
  1835. //              ODDeleteObject(_fDraftInfos);
  1836. //      }
  1837. // }
  1838.  
  1839. //------------------------------------------------------------------------------
  1840. // ODLinkManager::somUninit
  1841. //------------------------------------------------------------------------------
  1842.  
  1843. SOM_Scope void  SOMLINK ODLinkManagersomUninit(ODLinkManager *somSelf)
  1844. {
  1845.     ODLinkManagerData *somThis = ODLinkManagerGetData(somSelf);
  1846.     ODLinkManagerMethodDebug("ODLinkManager","somUninit");
  1847.  
  1848. #ifdef _PLATFORM_MACINTOSH_
  1849.         AEEventHandlerUPP       toBeDisposedUPP;
  1850.         long                            toBeIgnoredRefCon;
  1851.         OSErr                           error;
  1852.  
  1853.         // somSelf->DeleteDraftInfos(ev);
  1854.         // It is incorrect to call methods on somSelf inside somUninit,
  1855.         // a subclass may have already been somUninited.  See OpenDoc Building Code for details.
  1856.         // Instead the implementation of DeleteDraftInfos has been copied here.
  1857.  
  1858.         // BEGIN copy of DeleteDraftInfos implementation
  1859.         if (_fDraftInfos != (OrderedCollection*) kODNULL )
  1860.         {
  1861.                 Environment* ev = somGetGlobalEnvironment();
  1862.                 DraftInfo* info;
  1863.                 while ((info = (DraftInfo*) _fDraftInfos->First()) != (DraftInfo*) kODNULL)
  1864.                 {
  1865.                         ODDeleteObject(info->linksToExport);
  1866.                         ODDeleteObject(info->linksToDelete);
  1867.                         ODDeleteObject(info->newExportedLinks);
  1868.                         _fDraftInfos->RemoveFirst();
  1869.                         delete info;
  1870.                 }
  1871.                 ODDeleteObject(_fDraftInfos);
  1872.         }
  1873.         // END copy of DeleteDraftInfos implementation
  1874.  
  1875.         if ( _fSuspendedUpdateLinks != (ODRefCntCollection*) kODNULL )
  1876.         {
  1877.                 // _fSuspendedUpdateLinks->RemoveAndReleaseAll();
  1878.                 ODDeleteObject(_fSuspendedUpdateLinks);
  1879.         }
  1880.  
  1881.         error = AEGetEventHandler(kODShellSignature, kODGetLinkEventID,
  1882.                         &toBeDisposedUPP, &toBeIgnoredRefCon, kNotSysHandler);
  1883.         if ( error == noErr )
  1884.         {
  1885.                 DisposeRoutineDescriptor(toBeDisposedUPP);
  1886.                 error = AERemoveEventHandler(kODShellSignature, kODGetLinkEventID,
  1887.                                         kODNULL,                // a UPP wouldn't work: it might not be ==
  1888.                                         kNotSysHandler);
  1889.         }
  1890.  
  1891.         error = AEGetEventHandler(kODShellSignature, kODRequestLinkEventID,
  1892.                         &toBeDisposedUPP, &toBeIgnoredRefCon, kNotSysHandler);
  1893.         if ( error == noErr )
  1894.         {
  1895.                 DisposeRoutineDescriptor(toBeDisposedUPP);
  1896.                 error = AERemoveEventHandler(kODShellSignature, kODRequestLinkEventID,
  1897.                                         kODNULL,                // a UPP wouldn't work: it might not be ==
  1898.                                         kNotSysHandler);
  1899.         }
  1900.  
  1901.         error = AEGetEventHandler(sectionEventMsgClass, sectionReadMsgID,
  1902.                         &toBeDisposedUPP, &toBeIgnoredRefCon, kNotSysHandler);
  1903.         if ( error == noErr )
  1904.         {
  1905.                 DisposeRoutineDescriptor(toBeDisposedUPP);
  1906.                 error = AERemoveEventHandler(sectionEventMsgClass, sectionReadMsgID,
  1907.                                         kODNULL,
  1908.                                         kNotSysHandler);
  1909.         }
  1910.  
  1911.         error = AEGetEventHandler(sectionEventMsgClass, sectionWriteMsgID,
  1912.                         &toBeDisposedUPP, &toBeIgnoredRefCon, kNotSysHandler);
  1913.         if ( error == noErr )
  1914.         {
  1915.                 DisposeRoutineDescriptor(toBeDisposedUPP) ;
  1916.                 error = AERemoveEventHandler(sectionEventMsgClass, sectionWriteMsgID,
  1917.                                         kODNULL,
  1918.                                         kNotSysHandler);
  1919.         }
  1920.  
  1921.         error = AEGetEventHandler(sectionEventMsgClass, sectionScrollMsgID,
  1922.                         &toBeDisposedUPP, &toBeIgnoredRefCon, kNotSysHandler);
  1923.         if ( error == noErr )
  1924.         {
  1925.                 DisposeRoutineDescriptor(toBeDisposedUPP) ;
  1926.                 error = AERemoveEventHandler(sectionEventMsgClass, sectionScrollMsgID,
  1927.                                         kODNULL,
  1928.                                         kNotSysHandler);
  1929.         }
  1930.  
  1931.         if ( _fNMResponseUPP != kODNULL )
  1932.                 DisposeRoutineDescriptor(_fNMResponseUPP);
  1933.  
  1934.         if ( _fNMRecPtr )
  1935.                 ODDisposePtr((ODPtr) _fNMRecPtr->nmStr);
  1936.         ODDisposePtr(_fNMRecPtr);
  1937.  
  1938.     parent_somUninit(somSelf);
  1939.  
  1940. #endif
  1941. }
  1942.  
  1943. //------------------------------------------------------------------------------
  1944. // RegisterSubscribingLinks
  1945. //------------------------------------------------------------------------------
  1946.  
  1947. ODStatic void RegisterSubscribingLinks(Environment *ev, ODDraft* draft)
  1948. {
  1949.         // Internalize all link source objects; each link subscribing to an edition
  1950.         // file will register with the edition manager.
  1951.  
  1952.         ODLinkSourceIterator* iter = kODNULL;
  1953.  
  1954. #ifdef _PLATFORM_MACINTOSH_
  1955.         FN_CATCH
  1956.         {
  1957.                 ODDeleteObject(iter);
  1958.                 return;
  1959.         }
  1960. #else
  1961.         TRY
  1962. #endif
  1963.  
  1964.  
  1965.         iter = draft->CreateLinkSourceIterator(ev);
  1966.  
  1967.         for (ODLinkSource* linkSource = iter->First(ev);
  1968.                         iter->IsNotComplete(ev);
  1969.                         linkSource = iter->Next(ev) )
  1970.         {
  1971.                 linkSource->Release(ev);
  1972.         }
  1973.  
  1974.         delete iter;
  1975. #ifdef _PLATFORM_OS2_
  1976.         CATCH_ALL
  1977.         {
  1978.                 ODDeleteObject(iter);
  1979.                 return;
  1980.         }
  1981.         ENDTRY
  1982. #endif
  1983. }
  1984.  
  1985. //------------------------------------------------------------------------------
  1986. // RegisterPublishingLinks
  1987. //------------------------------------------------------------------------------
  1988. ODStatic void RegisterPublishingLinks(Environment *ev, ODDraft* draft)
  1989. {
  1990.         // Internalize all link objects; each link publishing an edition file will
  1991.         // register with the edition manager.
  1992.  
  1993.         ODLinkIterator* iter = kODNULL;
  1994.  
  1995. #ifdef _PLATFORM_MACINTOSH_
  1996.         FN_CATCH
  1997.         {
  1998.                 ODDeleteObject(iter);
  1999.                 return;
  2000.         }
  2001.  
  2002. #else
  2003.         TRY
  2004. #endif
  2005.  
  2006.         iter = draft->CreateLinkIterator(ev);
  2007.  
  2008.         for (ODLink* link = iter->First(ev);
  2009.                         iter->IsNotComplete(ev);
  2010.                         link = iter->Next(ev) )
  2011.         {
  2012.                 link->Release(ev);
  2013.         }
  2014.  
  2015.         delete iter;
  2016. #ifdef _PLATFORM_OS2_
  2017.         CATCH_ALL
  2018.         {
  2019.                 ODDeleteObject(iter);
  2020.                 return;
  2021.         }
  2022.         ENDTRY
  2023. #endif
  2024. }
  2025.  
  2026. //------------------------------------------------------------------------------
  2027. // UpdateEditionFiles
  2028. //------------------------------------------------------------------------------
  2029.  
  2030. ODStatic void UpdateEditionFiles(Environment *ev, ODRefCntCollection* collection)
  2031. {
  2032. #ifdef _PLATFORM_OS2_ // CED
  2033.   _interrupt(3);
  2034. #else
  2035.         if ( collection != kODNULL )
  2036.         {
  2037.                 ODRefCntCollectionIterator iter(collection);
  2038.                 ODLinkKey key;
  2039.  
  2040.                 for (ODLink* link = (ODLink*) iter.First();
  2041.                                 iter.IsNotComplete();
  2042.                                 link = (ODLink*) iter.Next() )
  2043.                 {
  2044.                         if ( link->Lock(ev,0,&key) )
  2045.                         {
  2046.                                 TRY
  2047.                                         link->UpdateLinkEdition(ev, key);
  2048.                                 CATCH_ALL
  2049.                                 ENDTRY
  2050.                                 link->Unlock(ev, key);
  2051.                         }
  2052.                 }
  2053.         }
  2054. #endif
  2055. }
  2056.  
  2057. //------------------------------------------------------------------------------
  2058. // DeleteEditionFiles
  2059. //------------------------------------------------------------------------------
  2060.  
  2061. ODStatic void DeleteEditionFiles(Environment *ev, ODRefCntCollection* collection)
  2062. {
  2063. #ifdef _PLATFORM_OS2_ // CED
  2064.   _interrupt(3);
  2065. #else
  2066.         if ( collection != kODNULL )
  2067.         {
  2068.                 ODRefCntCollectionIterator iter(collection);
  2069.                 ODLinkKey key;
  2070.  
  2071.                 for (ODLink* link = (ODLink*) iter.First();
  2072.                                 iter.IsNotComplete();
  2073.                                 link = (ODLink*) iter.Next() )
  2074.                 {
  2075.                         if ( link->Lock(ev,0,&key) )
  2076.                         {
  2077.                                 TRY
  2078.                                         link->DeleteLinkEdition(ev, key);
  2079.                                 CATCH_ALL
  2080.                                 ENDTRY
  2081.                                 link->Unlock(ev, key);
  2082.                         }
  2083.                 }
  2084.         }
  2085. #endif
  2086. }
  2087.  
  2088. //------------------------------------------------------------------------------
  2089. // RemoveAllLinksInCollection
  2090. //------------------------------------------------------------------------------
  2091.  
  2092. // ODStatic void RemoveAllLinksInCollection(Environment *ev, ODRefCntCollection* collection)
  2093. //      // collection must contain only ODLink subclass instances
  2094. // {
  2095. //      if ( collection != kODNULL )
  2096. //      {
  2097. //              ODRefCntCollectionIterator iter(collection);
  2098. //
  2099. //              TRY
  2100. //                      for (ODLink* link = (ODLink*) iter->First();
  2101. //                                      iter->IsNotComplete();
  2102. //                                      link = (ODLink*) iter->Next() )
  2103. //                      {
  2104. //                              ODReleaseObject(ev, link);
  2105. //                      }
  2106. //              CATCH_ALL
  2107. //              ENDTRY
  2108. //
  2109. //              collection->RemoveAll();
  2110. //      }
  2111. // }
  2112.  
  2113. //------------------------------------------------------------------------------
  2114. // AppendULongToDraftProperty
  2115. //------------------------------------------------------------------------------
  2116.  
  2117. ODStatic void AppendULongToDraftProperty(Environment *ev,
  2118.         ODDraft* draft, ODPropertyName prop, ODULong value)
  2119. {
  2120.         TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  2121.         ODSUForceFocus(ev, draftProperties, prop, kODULongSequence);
  2122.         draftProperties->SetOffset(ev, draftProperties->GetSize(ev));
  2123.         ODSetULongProp(ev, draftProperties, kODNULL, kODNULL, value);
  2124.  
  2125.         draft->SetChangedFromPrev(ev);
  2126. }
  2127.  
  2128. //------------------------------------------------------------------------------
  2129. // SectionIDReserved
  2130. //------------------------------------------------------------------------------
  2131.  
  2132. ODStatic ODBoolean SectionIDReserved(Environment *ev,
  2133.         ODDraft* draft, ODULong requestedID)
  2134. {
  2135.         ODBoolean result = kODFalse;
  2136.  
  2137.         TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  2138.  
  2139.         if ( ODSUExistsThenFocus(ev, draftProperties, kODPropReservedSectionIDs, kODULongSequence) )
  2140.         {
  2141.                 ODULong size = draftProperties->GetSize(ev);
  2142.                 while ( draftProperties->GetOffset(ev) < size )
  2143.                 {
  2144.                         if ( ODGetULongProp(ev, draftProperties, kODNULL, kODNULL) == requestedID )
  2145.                         {
  2146.                                 result = kODTrue;
  2147.                                 break;
  2148.                         }
  2149.                 }
  2150.         }
  2151.  
  2152.         return result;
  2153. }
  2154.