home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / InfoUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  43.4 KB  |  1,703 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        InfoUtil.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Doug Hill
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     3/15/96    DH        1.0.2: OpenDoc: Drag of no-part to trash
  13.                                     rejected. Fixed
  14.                                     ODGetIconFilePlatformTypeFromPartSU so that
  15.                                     it has proper exception handling and a
  16.                                     default return value.
  17.  
  18.     To Do:
  19. */
  20.  
  21. /*
  22.     File:        InfoUtil.cpp
  23.  
  24.     Contains:    implementation of Info getters & setters
  25.  
  26.     Owned by:    Tantek Çelik
  27.  
  28.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  29.  
  30.     
  31.     In Progress:
  32.         
  33. */
  34.  
  35. #ifndef _PLFMDEF_
  36. #include "PlfmDef.h"
  37. #endif
  38.  
  39. #ifndef _STDTYPIO_
  40. #include "StdTypIO.h"
  41. #endif
  42.  
  43. #ifndef _USERSRCM_
  44. #include "UseRsrcM.h"
  45. #endif
  46.  
  47. #ifndef SOM_ODSession_xh
  48. #include <ODSessn.xh>
  49. #endif
  50.  
  51. #ifndef SOM_ODDraft_xh
  52. #include <Draft.xh>
  53. #endif
  54.  
  55. #ifndef SOM_ODDocument_xh
  56. #include <Document.xh>
  57. #endif
  58.  
  59. #ifndef SOM_ODContainer_xh
  60. #include <ODCtr.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODWindowState_xh
  64. #include <WinStat.xh>
  65. #endif
  66.  
  67. #ifndef SOM_ODWindow_xh
  68. #include <Window.xh>
  69. #endif
  70.  
  71. #ifndef SOM_ODStorageUnit_xh
  72. #include <StorageU.xh>
  73. #endif
  74.  
  75. #ifndef SOM_Module_OpenDoc_StdProps_defined
  76. #include <StdProps.xh>
  77. #endif
  78.  
  79. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  80. #include <StdTypes.xh>
  81. #endif
  82.  
  83. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  84. #include <StdDefs.xh>
  85. #endif
  86.  
  87. #ifndef _ODUTILS_
  88. #include <ODUtils.h>
  89. #endif
  90.  
  91. #ifndef _DLOGUTIL_
  92. #include <DlogUtil.h>
  93. #endif
  94.  
  95. #ifndef SOM_ODFrame_xh
  96. #include <Frame.xh>
  97. #endif
  98.  
  99. #ifndef SOM_ODFacet_xh
  100. #include <Facet.xh>
  101. #endif
  102.  
  103. #ifndef SOM_ODPart_xh
  104. #include <Part.xh>
  105. #endif
  106.  
  107. #ifndef SOM_ODBinding_xh
  108. #include <ODBindng.xh>
  109. #endif
  110.  
  111. #ifndef SOM_ODStorageUnitView_xh
  112. #include <SUView.xh>
  113. #endif
  114.  
  115. #ifndef _PASCLSTR_
  116. #include "PasclStr.h"
  117. #endif
  118.  
  119. #ifndef _ODMEMORY_
  120. #include "ODMemory.h"
  121. #endif
  122.  
  123. #ifndef _PLFMFILE_
  124. #include <PlfmFile.h>
  125. #endif
  126.  
  127. #ifndef _ITEXT_
  128. #include <IText.h>
  129. #endif
  130.  
  131. #ifndef SOM_ODTypeList_xh
  132. #include <TypeList.xh>
  133. #endif
  134.  
  135. #ifndef SOM_ODTypeListIterator_xh
  136. #include <TypLsItr.xh>
  137. #endif
  138.  
  139. #ifndef _EXCEPT_
  140. #include "Except.h"
  141. #endif
  142.  
  143. #ifndef _BNDNSUTL_
  144. #include "BndNSUtl.h"
  145. #endif
  146.  
  147. #ifdef __SC__
  148. #ifndef __PACKAGES__
  149. #include <Packages.h>
  150. #endif
  151. #else
  152. #ifndef __TEXTUTILS__
  153. #include <TextUtils.h>
  154. #endif
  155. #endif
  156.  
  157. #ifndef __STRING__
  158. #include <String.h>
  159. #endif
  160.  
  161. #ifndef __GESTALTEQU__
  162. #include <GestaltEqu.h>
  163. #endif
  164.  
  165. #ifndef __DIALOGS__
  166. #include <Dialogs.h>
  167. #endif
  168.  
  169. #ifndef __TOOLUTILS__
  170. #include <ToolUtils.h>
  171. #endif
  172.  
  173. #ifndef __FONTS__
  174. #include <Fonts.h>
  175. #endif
  176.  
  177. #ifndef __CONTROLS__
  178. #include <Controls.h>
  179. #endif
  180.  
  181. #ifndef __ICONS__
  182. #include <Icons.h>
  183. #endif
  184.  
  185. #ifndef __FINDER__
  186. #include <Finder.h>
  187. #endif
  188.  
  189. #ifndef _INFOUTIL_
  190. #include "InfoUtil.h"
  191. #endif
  192.  
  193. #ifndef SOM_ODTranslation_xh
  194. #include <Translt.xh>
  195. #endif
  196.  
  197. #ifndef _EDITRSET_
  198. #include "EditrSet.h"
  199. #endif
  200.  
  201. #ifndef _STORUTIL_
  202. #include <StorUtil.h>
  203. #endif
  204.  
  205. #ifndef _ISOSTR_
  206. #include "ISOStr.h"
  207. #endif
  208.  
  209. #ifndef SOM_ODWindowIterator_xh
  210. #include <WinIter.xh>
  211. #endif
  212.  
  213. #ifndef SOM_ODNameSpaceManager_xh
  214. #include <NmSpcMg.xh>
  215. #endif
  216.  
  217. #ifndef SOM_ODValueNameSpace_xh
  218. #include <ValueNS.xh>
  219. #endif
  220.  
  221. #ifndef _NMSPCUTL_
  222. #include <NmSpcUtl.h>
  223. #endif
  224.  
  225. #ifndef _UTILDEFS_
  226. #include "UtilDefs.h"
  227. #endif
  228.  
  229. #ifndef _TEMPOBJ_
  230. #include <TempObj.h>
  231. #endif
  232.  
  233. #ifndef _ORDCOLL_
  234. #include "OrdColl.h"
  235. #endif
  236.  
  237. #pragma segment Info
  238.  
  239. //==============================================================================
  240. // Constants
  241. //==============================================================================
  242.  
  243. const short kControlInactive = 255;
  244. const short kControlActive = 0;
  245. static const char* const         kODIconFamilyBaseString = kODIconFamily ":";
  246. static const ResType            kIconType[6] = {'ICN#','icl4','icl8','ics#','ics4','ics8'};
  247. static const short                kIconSize[6] = {256,   512,   1024,  64,    128,   256   };
  248.  
  249. const ODPlatformType kODFileType_hfs = 0x68667320;    // 'hfs '
  250.  
  251. //==============================================================================
  252. // Static Function declarations
  253. //==============================================================================
  254.  
  255. ODTime    ODGetDateProperty(Environment* ev, 
  256.             ODStorageUnit* su,
  257.             ODPropertyName prop);
  258.  
  259. void    ODSetDateProperty(Environment* ev, 
  260.             ODStorageUnit* su,
  261.             ODPropertyName prop,
  262.             ODTime dateTime);
  263.  
  264. ODStatic ODBoolean         ODUserRenameFile(Environment* ev,
  265.                                 ODSession* session,
  266.                                 PlatformFile*    usersFile, 
  267.                                 ODIText* name,
  268.                                 DescType replaceOption);
  269. // If PlatformFile is made a shared utility, then the above function should probably be
  270. // integrated into the PlatformFile::Rename method.
  271.  
  272. ODStatic ODIconFamily    GetGenericIcon( short iconID );
  273.  
  274. ODStatic OSErr             DetachIconProc( ResType, Handle *theIcon, void */*yourDataPtr*/ );
  275.  
  276. ODStatic ODBoolean        CommentsDontMatch(ODIText* dtComments, ODIText* propComments);
  277.  
  278. //==============================================================================
  279. // Info functions
  280. //==============================================================================
  281.  
  282.  
  283. ODStorageUnit* ODGetSUFromPstObj(Environment* ev, ODPersistentObject* pstobj)
  284. {
  285.     return pstobj->GetStorageUnit(ev);
  286. }
  287.  
  288. //-------------------------------------------------------------------------------------
  289. // Part & Frame Property getters and setters
  290. //-------------------------------------------------------------------------------------
  291.  
  292. ODIText*
  293. ODGetPOComments(Environment* ev, ODPart *part, ODIText* comments)
  294. {
  295.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  296.     ODIText*        propComments;
  297.  
  298.     propComments = ODGetITextProp(ev, su, kODPropComments, kODMacIText, comments);
  299.     return propComments;
  300. }
  301.  
  302. void    ODSetPOComments(Environment* ev, ODPart *part, ODIText* comments)
  303. {
  304.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  305.     
  306.     ODSetITextProp(ev, su, kODPropComments, kODMacIText, comments);
  307. }
  308.  
  309.  
  310. ODIText*
  311. ODGetComments(Environment* ev, ODFrame* frame, ODIText* comments)
  312. {
  313.     TempODPart        part = frame->AcquirePart(ev);
  314.     ODIText*        propComments;
  315.     ODIText*        dtComments;
  316.  
  317.     propComments = ODGetPOComments(ev, part, comments);
  318.     if (comments && !propComments)
  319.         propComments = comments;
  320.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  321.     if (usersFile)
  322.     {
  323.         dtComments = usersFile->GetComments();
  324.         if (dtComments && GetITextStringLength(dtComments) == 0)
  325.         {
  326.             // since there are no desktop comments
  327.             // make the desktop comments match the property
  328.             usersFile->SetComments(propComments);
  329.         }
  330.         else if (CommentsDontMatch(dtComments, propComments) && dtComments)
  331.         {
  332.             // if there are desktop comments and the property comments don't match
  333.             // then the desktop comments take precedence, so return the desktop comments
  334.             DisposeIText(propComments);
  335.             propComments = dtComments;
  336.             dtComments = kODNULL;
  337.         }
  338.         DisposeIText(dtComments);
  339.     }
  340.     return propComments;
  341. }
  342.  
  343. void    ODSetComments(Environment* ev, ODFrame* frame, ODIText* comments)
  344. {
  345.     TempODPart        part = frame->AcquirePart(ev);
  346.     ODSetPOComments(ev, part, comments);
  347.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  348.     if (usersFile)
  349.         usersFile->SetComments(comments);
  350. }
  351.  
  352.  
  353. ODULong    ODGetPOSize(Environment* ev, ODPersistentObject* pstobj)
  354. {
  355.     ODStorageUnit* su = ODGetSUFromPstObj(ev, pstobj);
  356.     su->Focus(ev, kODNULL, kODPosAll, kODNULL,0, kODPosAll);
  357.     return su->GetSize(ev);
  358. }
  359.  
  360. ODID ODGetPOID(Environment* ev, ODPersistentObject* pstobj)
  361. {
  362.     ODStorageUnit* su = ODGetSUFromPstObj(ev, pstobj);
  363.     su->Focus(ev, kODNULL, kODPosAll, kODNULL,0, kODPosAll);
  364.     return su->GetID(ev);
  365. }
  366.  
  367. ODTime    ODGetDateProperty(Environment* ev, 
  368.             ODStorageUnit* su,
  369.             ODPropertyName prop)
  370. {    return ODGetTime_TProp(ev, su, prop, kODTime_T);}
  371.  
  372. void    ODSetDateProperty(Environment* ev, 
  373.             ODStorageUnit* su,
  374.             ODPropertyName prop,
  375.             ODTime dateTime)
  376. {    ODSetTime_TProp(ev, su, prop, kODTime_T, dateTime);}
  377.  
  378.  
  379. ODULong    ODGetCreationDate(Environment* ev, 
  380.                 ODStorageUnit* su)
  381. {    return ODGetDateProperty(ev, su, kODPropCreateDate);}
  382.  
  383. void    ODSetCreationDate(Environment* ev, 
  384.                 ODStorageUnit* su,
  385.                 ODTime dateTime)
  386. {    ODSetDateProperty(ev, su, kODPropCreateDate, dateTime);}
  387.  
  388.  
  389. ODULong    ODGetModificationDate(Environment* ev, 
  390.                 ODStorageUnit* su)
  391. {    return ODGetDateProperty(ev, su, kODPropModDate);}
  392.  
  393. void    ODSetModificationDate(Environment* ev, 
  394.                 ODStorageUnit* su,
  395.                 ODTime dateTime)
  396. {    ODSetDateProperty(ev, su, kODPropModDate, dateTime);}
  397.  
  398.  
  399. ODIText*    ODGetModifiedBy(Environment* ev, 
  400.                 ODStorageUnit* su, ODIText* userName)
  401. {
  402.     return ODGetITextProp(ev, su, kODPropModUser,
  403.             kODMacIText, userName);
  404. }
  405.  
  406. void        ODSetModifiedBy(Environment* ev, 
  407.                 ODStorageUnit* su, ODIText* userName)
  408. {
  409.     ODSetITextProp(ev, su, kODPropModUser,
  410.             kODMacIText, userName);
  411. }
  412.  
  413.  
  414. //-------------------------------------------------------------------------------------
  415. // Part only Property getters and setters
  416. //-------------------------------------------------------------------------------------
  417. ODIText*    ODGetPartName(Environment* ev, 
  418.                 ODFrame* frame, ODIText* name)
  419. {
  420.     TempODPart part = frame->AcquirePart(ev);
  421.     TempODIText partName = ODGetPOName(ev, part, name);
  422.     
  423.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  424.     if (usersFile)
  425.     {
  426.         TempODIText fileName = usersFile->GetName();
  427.         if ( CommentsDontMatch( fileName, partName ) )
  428.             return CopyIText( fileName );
  429.     }
  430.     return CopyIText( partName );
  431. }
  432.  
  433. ODBoolean    ODSetPartName(Environment* ev, 
  434.                 ODFrame* frame, ODIText* name, DescType replaceOption)
  435.         // Renames the part of the frame passed in.
  436.         // Returns whether or not it succeeded.
  437.         // The frame helps more easily determine whether
  438.         // the part is the root part of the document or not.
  439.         // For example, renaming the root part of the document could fail
  440.         // if there was a file with the same name and the user canceled the
  441.         // "Replace?" dialog.
  442. {
  443.     ASSERT(frame != kODNULL, kODErrIllegalNullInput);
  444.     
  445.     TempODPart part = frame->AcquirePart(ev);
  446.     TempODWindow window = frame->AcquireWindow(ev);
  447.                 
  448.     ODFrame*     contFrame = frame->AcquireContainingFrame(ev);
  449.     ODBoolean     isRootPart = (contFrame == kODNULL);
  450.     ODReleaseObject(ev, contFrame);
  451.     
  452.     ODBoolean    isRootPartOfDocument = isRootPart && window->IsRootWindow(ev);
  453.     ODBoolean    succeeded = kODTrue;
  454.     
  455.     TRY
  456.         ODName*         oldName = ODGetPartName(ev, frame, kODNULL); 
  457.         ODStorageUnit*    partSU    = ODGetSUFromPstObj(ev, part);
  458.         ODSession*        session = partSU->GetSession(ev);
  459.  
  460.         if (isRootPartOfDocument)
  461.         {
  462.             ODDraft*        draft = partSU->GetDraft(ev);
  463.             PlatformFile*    usersFile = 
  464.                 GetPlatformFileFromContainer(ev, draft->GetDocument(ev)->GetContainer(ev));
  465.  
  466.             if (ODUserRenameFile(ev, session, usersFile, name, replaceOption))
  467.             {
  468.                 ODWindowState* windowState = session->GetWindowState(ev);
  469.                 windowState->SetDefaultWindowTitles(ev, draft);
  470.                 ODSetPOName(ev, part, name);
  471.                 ODRenamePartWindows(ev,session,part,oldName,name);
  472.                 if (isRootPart)
  473.                 {
  474.                     Str255 pName;
  475.                     GetITextPString(name, pName);
  476.                     SetWTitle(window->GetPlatformWindow(ev), pName);
  477.                 }
  478.             }
  479.             else
  480.                 succeeded = kODFalse;
  481.             ODDeleteObject(usersFile);
  482.         }
  483.         else
  484.         {
  485.             ODSetPOName(ev, part,name);
  486.             ODRenamePartWindows(ev,session,part,oldName,name);
  487.         }
  488.         
  489.         DisposeIText(oldName);
  490.  
  491.     CATCH_ALL
  492.         WARN("ODSetPartName failed, error %ld",ErrorCode());
  493.         succeeded = kODFalse;
  494.     ENDTRY
  495.     
  496.     return succeeded;
  497. }
  498.  
  499. void
  500. ODRenamePartWindows( Environment *ev, ODSession *session,
  501.                      ODPart *part, ODIText *oldName, ODIText *name )
  502. {
  503.     // go through all windows which have this part as their root, and
  504.     // have the current name of the part as a prefix
  505.     // rename their prefix to be the new name
  506.     Str255    pOldName;
  507.     Str255    pNewName;
  508.     Str255    pWinTitle;
  509.     ODUByte pOldNameLen;
  510.     ODUByte pNewNameLen;
  511.     
  512.     GetITextPString(oldName, pOldName);
  513.     pOldNameLen = pOldName[0];
  514.     GetITextPString(name, pNewName);
  515.     pNewNameLen = pNewName[0];
  516.     
  517.     ODWindowIterator* iter = session->GetWindowState(ev)->CreateWindowIterator(ev);
  518.     for (ODWindow* window = iter->First(ev); 
  519.          iter->IsNotComplete(ev);
  520.          window = iter->Next(ev))
  521.     {
  522.         TempODPart winPart = window->GetRootFrame(ev)->AcquirePart(ev);
  523.         if (ODObjectsAreEqual(ev, part, winPart))
  524.         {
  525.             // Check to see if name of window has pOldName as prefix
  526.             ODPlatformWindow pwindow = window->GetPlatformWindow(ev);
  527.             GetWTitle(pwindow, pWinTitle);
  528.             if (!strncmp((char*)&pWinTitle[1], (char*)&pOldName[1], pOldNameLen) &&
  529.                     !EqualPascalStrings(pWinTitle, pNewName))
  530.             {
  531.                 ODUByte suffixLen = pWinTitle[0] - pOldNameLen;
  532.                 if (suffixLen + pNewNameLen > 255) 
  533.                     suffixLen = 255 - pNewNameLen;
  534.                 strncpy((char*)&pNewName[pNewNameLen+1], 
  535.                         (char*)&pWinTitle[pOldNameLen+1],
  536.                         suffixLen);
  537.                 pNewName[0] = pNewNameLen+suffixLen;
  538.                 SetWTitle(pwindow, pNewName);
  539.             }
  540.         }
  541.     }
  542.     ODDeleteObject(iter);
  543. }
  544.  
  545.  
  546. ODIText*    ODGetPOName(Environment* ev, ODPersistentObject* pstobj,ODIText* name)
  547. {
  548.     return ODGetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName, kODMacIText,name);
  549. }
  550.  
  551. void        ODSetPOName(Environment* ev, ODPersistentObject* pstobj,ODIText* name)
  552. {
  553.     ODSetITextProp(ev, ODGetSUFromPstObj(ev, pstobj), kODPropName, kODMacIText, name);
  554. }
  555.  
  556. void        ODSetPONameUsingSU(Environment* ev, ODStorageUnit* su, ODIText* name)
  557. {
  558.     ODSetITextProp(ev, su, kODPropName, kODMacIText, name);
  559. }
  560.  
  561. ODName*    ODGetCategory(Environment* ev, ODPart* part, ODNameSpaceManager* nsm)
  562. {
  563.     return ODGetCatFromPartSU(ev, ODGetSUFromPstObj(ev, part), nsm);
  564. }
  565.  
  566. ODName*    ODGetCatFromPartSU(Environment* ev, ODStorageUnit* su, ODNameSpaceManager* nsm)
  567. {
  568.     ODType kind = ODGetKindFromPartSU(ev, su);
  569.     ODName*    categoryName = ODGetCatFromKind(ev, kind, nsm);
  570.     ODDisposePtr( kind );
  571.  
  572.     return categoryName;
  573. }
  574.  
  575. ODName*    ODGetCatFromKind(Environment* ev, ODType kind, ODNameSpaceManager* nsm)
  576. {
  577.     StringHandle strHandle = 0;
  578.     ODTypeList* categoryList = GetCategoriesOfKind(nsm, kind);
  579.     ODTypeListIterator* catIter = kODNULL;
  580.     ODType category = kODNULL;
  581.     ODName*    categoryName = kODNULL;
  582.     
  583.     if (categoryList)
  584.     {
  585.         catIter = categoryList->CreateTypeListIterator(ev);
  586.         category = catIter->First(ev);
  587.         ODDeleteObject(catIter);
  588.     }
  589.     
  590.     if (category == kODNULL || !GetUserCatFromCat(nsm, category, &categoryName)) {
  591.         CUsingLibraryResources r;
  592.         categoryName = GetODIText(kODPartInfoStrUnknownID);
  593.     }
  594.     //else // make a disposable copy // NO NEED TO! GetUserCatFromCat returns a copy!
  595.     //    categoryName = CopyIText(categoryName);
  596.         
  597.     ODDisposePtr(category);
  598.  
  599.     return categoryName;
  600. }
  601.  
  602. ODType    ODGetKindFromPartSU(Environment* ev, ODStorageUnit* su)
  603. {
  604.     ODULong unused;
  605.     ODType kind = ODGetISOStrProp(ev, su, kODPropPreferredKind, kODISOStr, kODNULL, &unused);
  606.     if ( kind == kODNULL )
  607.         if ( su->Exists(ev, kODPropContents, (ODValueType) kODNULL,1) )
  608.         {
  609.             su->Focus(ev, kODPropContents, kODPosSame, (ODValueType) kODNULL,1, kODPosSame);
  610.             kind = su->GetType(ev);
  611.  
  612.             ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  613.             TempODType hfsType = 
  614.                 translation->GetISOTypeFromPlatformType(ev, kODFileType_hfs, kODPlatformDataType);
  615.  
  616.             if ( ODISOStrEqual(kind, hfsType) )
  617.             {
  618.                 ODDisposePtr(kind);
  619.                 kind = kODNULL;
  620.                 if ( su->Exists(ev, kODPropContents, (ODValueType) kODNULL,2) )
  621.                 {
  622.                     su->Focus(ev, kODPropContents, kODPosSame, (ODValueType) kODNULL,2, kODPosSame);
  623.                     kind = su->GetType(ev);
  624.                 }
  625.             }
  626.         }
  627.     return kind;
  628. }
  629.  
  630. ODType    ODGetKind(Environment* ev, ODPart* part)
  631. {
  632.     return ODGetKindFromPartSU(ev, ODGetSUFromPstObj(ev, part));
  633. }
  634.  
  635. ODPlatformType ODGetIconFilePlatformTypeFromPartSU(Environment* ev, ODStorageUnit* su)
  636. {
  637.     ODType partKind = kODNULL;
  638.     ODPlatformType partOSType;
  639.     ODVolatile(partKind);
  640.     ODVolatile(partOSType);
  641.     
  642.     TRY
  643.         ODSession* session = su->GetSession(ev);
  644.     
  645.         partKind = ODGetKindFromPartSU(ev, su);
  646.         partOSType = session->GetTranslation(ev)->
  647.                                 GetPlatformTypeFromISOType(ev, partKind);
  648.         if (partOSType != kODNULL)
  649.         {
  650.             ((ODUByte*)(&partOSType))[0] = kODPlatformKindFileChar1;
  651.         }
  652.         else
  653.         {
  654.             ODBoolean    partOSTypeFound = kODFalse;
  655.             ODValueNameSpace*    osTypeNameSpace = (ODValueNameSpace*)
  656.                 (session->GetNameSpaceManager(ev)->HasNameSpace(ev, kODKindOldMacOSType));
  657.         
  658.             if (osTypeNameSpace)
  659.             {
  660.                 ODPlatformType* typePtr;
  661.                 ODULong        unusedLength;
  662.                 partOSTypeFound = ValueNameSpaceGetEntry(osTypeNameSpace, 
  663.                                                         ev, partKind, 
  664.                                                         (ODPtr*) &typePtr, 
  665.                                                         &unusedLength);
  666.                 if (partOSTypeFound)
  667.                 {
  668.                     partOSType = *typePtr;
  669.                     ODDeleteObject( typePtr );
  670.                 }
  671.             }
  672.             if (!partOSTypeFound)
  673.                 partOSType = kODShellSignature;
  674.         }
  675.         
  676.         ODDisposePtr(partKind);
  677.     CATCH_ALL
  678.         ODDisposePtr(partKind);
  679.         partOSType = kODShellSignature;
  680.     ENDTRY
  681.  
  682.     return partOSType;
  683. }
  684.  
  685.  
  686. //--------------------------------------------------------------------
  687. // ODGetPOIconFamily
  688. //--------------------------------------------------------------------
  689.  
  690. ODIconFamily
  691. ODGetPOIconFamily(Environment* ev, ODPart* part)
  692. {
  693.     //// FIRST, get the Mac icon value if one exists:
  694.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, part);
  695.     ODIconFamily icons = ODGetIconFamilyProp(ev, su, 
  696.                                 kODPropCustomIcon, kODIconFamily, kAllIconsMask);
  697.     if (icons )
  698.         return icons;
  699.         
  700.     //// OTHERWISE, look for other platform icon values:
  701.     if (ODSUExistsThenFocus(ev,su, kODPropCustomIcon, kODNULL))
  702.     {
  703.         ODULong numValues = su->CountValues(ev);
  704.         for( ODULong i = 1; i <= numValues; i++ )
  705.         {
  706.             su->Focus(ev, kODNULL, kODPosSame, kODNULL, 0, kODPosNextSib);    // Next value
  707.             TempODValueType type = su->GetType(ev);
  708.             if( strncmp(type,kODIconFamilyBaseString,strlen(kODIconFamilyBaseString)) == 0 )
  709.             {
  710.                 // This value is an icon family for some platform. Use the b/w icons
  711.                 // because we don't know what color table the color ones may use.
  712.                 icons = ODGetIconFamilyProp(ev, su, kODPropCustomIcon, type, kBWIconsMask);
  713.                 if( icons )
  714.                     return icons;
  715.             }
  716.         }
  717.         return kODNULL;
  718.     }
  719.     return icons;
  720. }
  721.  
  722.  
  723.  
  724. //--------------------------------------------------------------------
  725. // ODSetPOIconFamily
  726. //--------------------------------------------------------------------
  727.  
  728. void    ODSetPOIconFamily(Environment* ev, ODPart* part, 
  729.             ODIconFamily icons, ODBoolean deleteOtherPlatformIcons)
  730. {
  731.     ODSetIconFamilyProp(ev, ODGetSUFromPstObj(ev, part), 
  732.                                 kODPropCustomIcon, kODIconFamily,
  733.                                 icons, deleteOtherPlatformIcons);
  734. }
  735.  
  736. //--------------------------------------------------------------------
  737. // ODGetIconFamily
  738. //--------------------------------------------------------------------
  739.  
  740. ODIconFamily
  741. ODGetIconFamily(Environment* ev, ODFrame* frame)
  742. {
  743.     TempODPart        part = frame->AcquirePart(ev);
  744.     ODStorageUnit*    su = ODGetSUFromPstObj(ev, (ODPart*)part);
  745.     ODIconFamily    icons = kODNULL;
  746.     
  747.     // FIRST we check for the custom icon resources
  748.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  749.     if (usersFile)
  750.         icons = usersFile->GetCustomIconFamily();
  751.     if (icons)
  752.         return icons;
  753.     
  754.     //// THEN get the property icon value if one exists:
  755.     icons = ODGetPOIconFamily(ev, part);
  756.     if (icons)
  757.         return icons;
  758.  
  759.     //// THEN get the editor's icon from the Mac desktop database:
  760.     ODPlatformType fileType = ODGetIconFilePlatformTypeFromPartSU(ev, su);
  761.     ODPlatformType fileCreator = kODShellSignature;
  762.     
  763.     // Query the desktop database:
  764.     DTPBRec pb;
  765.     pb.ioNamePtr = kODNULL;
  766.     pb.ioVRefNum = -1; //spec.vRefNum;
  767.     THROW_IF_ERROR( PBDTGetPath( &pb ) );        // Sets pb.ioDTRefNum
  768.     
  769.     THROW_IF_ERROR( NewIconSuite(&icons) );
  770.     
  771.     ODBoolean hasIcons = kODFalse;
  772.     TRY{
  773.         char iconBuffer[kLarge8BitIconSize];
  774.         for( pb.ioIconType=1; pb.ioIconType<=6; pb.ioIconType++ ) {
  775.             pb.ioTagInfo = 0;
  776.             pb.ioDTBuffer = (Ptr)&iconBuffer;
  777.             pb.ioDTReqCount = sizeof(iconBuffer);
  778.             pb.ioFileCreator = fileCreator; //info.fdCreator;
  779.             pb.ioFileType = fileType; //info.fdType;
  780.             OSErr err= PBDTGetIconSync(&pb);
  781.             
  782.             if( err == noErr ) {
  783.                 Handle hIcon;
  784.                 WASSERT(pb.ioDTActCount==kIconSize[pb.ioIconType-1]);
  785.                 THROW_IF_ERROR( PtrToHand(iconBuffer,&hIcon, kIconSize[pb.ioIconType-1]) );
  786.                 ASSERT(hIcon!=NULL, kODErrAssertionFailed);
  787.                 ASSERT(*hIcon!=NULL, kODErrAssertionFailed);
  788.                 err= AddIconToSuite(hIcon,icons, kIconType[pb.ioIconType-1] );
  789.                 if( err ) {
  790.                     DisposeHandle(hIcon);
  791.                     THROW_IF_ERROR(err);
  792.                 }
  793.                 hasIcons = kODTrue;
  794.             } else if( err != afpItemNotFound )
  795.                 THROW_IF_ERROR(err);
  796.         }
  797.     
  798.     }CATCH_ALL{
  799.         DisposeIconSuite(icons, kODTrue);
  800.         RERAISE;
  801.     }ENDTRY
  802.     
  803.     // FINALLY use a generic icon:
  804.     if( !hasIcons ) {
  805.         DisposeIconSuite(icons, kODTrue);
  806.  
  807.         short id;
  808.                 if( ODGetIsStationery(ev, frame) )
  809.                     id = genericStationeryIconResource;
  810.                 else
  811.                     id = genericDocumentIconResource;
  812.                 icons= GetGenericIcon(id);
  813.             }
  814.     return icons;
  815. }
  816.  
  817.  
  818. //--------------------------------------------------------------------
  819. // ODSetIconFamily
  820. //--------------------------------------------------------------------
  821.  
  822. void    ODSetIconFamily(Environment* ev, 
  823.                 ODFrame* frame, ODIconFamily icons, ODBoolean deleteOtherPlatformIcons)
  824. {
  825.     TempODPart         part = frame->AcquirePart(ev);
  826.     
  827.     ODSetPOIconFamily(ev, part, icons, deleteOtherPlatformIcons);
  828.     
  829.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  830.     if (usersFile)
  831.         usersFile->SetCustomIconFamily(icons);
  832. }
  833.  
  834. //--------------------------------------------------------------------
  835. // ODGetFileIfRoot
  836. //--------------------------------------------------------------------
  837. PlatformFile*    ODGetFileIfRoot(Environment* ev, ODFrame* frame)
  838. {
  839.     PlatformFile*    usersFile = kODNULL;
  840.     ODBoolean        isRootPartOfDocument;
  841.  
  842.     ASSERT(frame != kODNULL, kODErrIllegalNullInput);
  843.  
  844.     ODFrame*     contFrame = frame->AcquireContainingFrame(ev);
  845.     ODBoolean     isRootPart = (contFrame == kODNULL);
  846.     ODReleaseObject(ev, contFrame);
  847.  
  848.     { TempODWindow window = frame->AcquireWindow(ev);
  849.       isRootPartOfDocument = isRootPart && window->IsRootWindow(ev);
  850.     }
  851.     
  852.     if (isRootPartOfDocument)
  853.     {
  854.         ODDraft* draft;
  855.         { TempODPart part = frame->AcquirePart(ev);
  856.           draft = ODGetSUFromPstObj(ev, part)->GetDraft(ev);
  857.         }
  858.         usersFile = GetPlatformFileFromContainer(ev, 
  859.                                 draft->GetDocument(ev)->GetContainer(ev));
  860.     }
  861.     return usersFile;
  862. }
  863.  
  864.  
  865. //--------------------------------------------------------------------
  866. // DetachIconProc
  867. //--------------------------------------------------------------------
  868.  
  869. static OSErr DetachIconProc( ResType, Handle *theIcon, void */*yourDataPtr*/ )
  870. {
  871.     if( *theIcon ) {
  872.         DetachResource(*theIcon);
  873.         return ResError();
  874.     } else
  875.         return noErr;
  876. }
  877.  
  878. //--------------------------------------------------------------------
  879. // GetGenericIcon
  880. //--------------------------------------------------------------------
  881.  
  882. ODStatic ODIconFamily GetGenericIcon( short iconID )
  883. {
  884.     ODIconFamily icon;
  885.     THROW_IF_ERROR( GetIconSuite( &icon, iconID, svAllAvailableData ) );
  886.     IconActionUPP detachProc = NewIconActionProc(&DetachIconProc);
  887.     (void) ForEachIconDo(icon, svAllAvailableData, detachProc, kODNULL);
  888.     DisposeRoutineDescriptor(detachProc);
  889.     return icon;
  890. }
  891.  
  892.  
  893. //--------------------------------------------------------------------
  894. // ODGetIsStationery
  895. //--------------------------------------------------------------------
  896.  
  897. ODBoolean    ODGetIsStationery(Environment* ev, 
  898.                 ODFrame* frame)
  899. {
  900.     TempODPart        part = frame->AcquirePart(ev);
  901.     ODBoolean        isStationery;
  902.     
  903.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  904.     if (usersFile)
  905.         isStationery = usersFile->IsStationery();
  906.     else
  907.         isStationery = ODGetSUIsStationery(ev, ODGetSUFromPstObj(ev, part));
  908.     return isStationery;
  909. }
  910.  
  911. //--------------------------------------------------------------------
  912. // ODSetIsStationery
  913. //--------------------------------------------------------------------
  914.  
  915. void        ODSetIsStationery(Environment* ev, 
  916.                 ODFrame* frame, ODBoolean isStationery)
  917. {
  918.     { TempODPart part = frame->AcquirePart(ev);
  919.       ODSetBooleanProp( ev, ODGetSUFromPstObj(ev, part),
  920.                             kODPropIsStationery, kODBoolean, isStationery);
  921.     }
  922.     TempPlatformFile usersFile = ODGetFileIfRoot(ev, frame);
  923.     if (usersFile)
  924.         usersFile->SetStationery(isStationery);
  925. }
  926.  
  927. //--------------------------------------------------------------------
  928. // ODSetSUIsStationery
  929. //--------------------------------------------------------------------
  930.  
  931. void        ODSetSUIsStationery(Environment* ev, 
  932.                 ODStorageUnit* su, ODBoolean isStationery)
  933. {
  934.     ODSetBooleanProp( ev, su, kODPropIsStationery, kODBoolean, isStationery);
  935. }
  936.  
  937. //--------------------------------------------------------------------
  938. // ODGetSUIsStationery
  939. //--------------------------------------------------------------------
  940.  
  941. ODBoolean        ODGetSUIsStationery(Environment* ev, 
  942.                 ODStorageUnit* su)
  943. {
  944.     ODBoolean isStationery;
  945.     
  946.     if (su->Exists(ev, kODPropIsStationery, kODBoolean, 0))
  947.         isStationery = ODGetBooleanProp( ev, su, kODPropIsStationery, kODBoolean);
  948.     else
  949.         isStationery = kODFalse;
  950.         
  951.     return isStationery;
  952. }
  953.  
  954. //-------------------------------------------------------------------------------------
  955. // Editor and Kind Menu manipulation functions
  956. //-------------------------------------------------------------------------------------
  957. // copied from LinkDlgs.cpp (with changes by TÇ)
  958. //------------------------------------------------------------------------------
  959. // AddTypesToMenu
  960. //------------------------------------------------------------------------------
  961.  
  962. void AddTypesToMenu(ODTypeList* typeList, MenuHandle kindMenu, ODSession* session)
  963. {
  964.     ODName*                name;
  965.     ODSShort            item = 0;
  966.     Environment*        ev = somGetGlobalEnvironment();
  967.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  968.     
  969.     ODTypeListIterator* typeIter = typeList->CreateTypeListIterator(ev);
  970.     
  971.     EditorSet*            editorList = new EditorSet;
  972.     editorList->InitEditorSet();
  973.  
  974.     for (ODType type = typeIter->First(ev);
  975.         typeIter->IsNotComplete(ev);
  976.         type = typeIter->Next(ev))
  977.     {
  978.         if ( GetUserKindFromKind(nsm, type, &name) )
  979.         {
  980.             Str255 itemString;
  981.             IntlToPStr(name, itemString);
  982.             InsertMenuItem(kindMenu, itemString, item++);
  983.             
  984.             ScriptCode itemScript = GetITextScriptCode(name);
  985.             SetPopupItemScript(kindMenu, item, itemScript);
  986.             
  987.             DisposeIText( name );
  988.         }
  989.         else
  990.         {
  991.             // there's no name for this type
  992.             Str255 pKindStr;
  993.             ODTranslation* translation = session->GetTranslation(ev);
  994.             ODPlatformType platformType = 
  995.                 translation->GetPlatformTypeFromISOType(ev, type);
  996.             if ( platformType == kODNULL )
  997.             {
  998.                 // Special menu item
  999.                 CUsingLibraryResources r;
  1000.                 GetIndString(pKindStr, kODInfoUtilStrsID, kODStrIndNoKinds); 
  1001.             }
  1002.             else
  1003.             {
  1004.                 ODBlockMove(&platformType, &(pKindStr[1]), (ODULong) sizeof(ODPlatformType));
  1005.                 pKindStr[0] = (unsigned char) sizeof(ODPlatformType);
  1006.             }
  1007.             // Allow metacharacters in item text
  1008.             InsertMenuItem(kindMenu, "\p ", item++);
  1009.             SetMenuItemText(kindMenu, item, pKindStr);
  1010.         }
  1011.     
  1012.         if (!GetAllEditorsForKind(nsm, type, editorList))
  1013.             DisableItem(kindMenu, item);
  1014.         editorList->RemoveAllEditors();
  1015.  
  1016.         ODDisposePtr(type);
  1017.     }
  1018.     ODDeleteObject( editorList );
  1019.     ODDeleteObject( typeIter );
  1020. }
  1021.  
  1022. //------------------------------------------------------------------------------
  1023. // EnableTypesInMenu
  1024. //------------------------------------------------------------------------------
  1025.  
  1026. ODUShort EnableTypesInMenu(
  1027.                     ODTypeList*            typeList,
  1028.                     MenuHandle            kindMenu,
  1029.                     ODEditor            editor,
  1030.                     ODNameSpaceManager*    nsm)
  1031. {
  1032.     Environment* ev = somGetGlobalEnvironment();
  1033.     ODUShort itemStart = 0;
  1034.  
  1035.     if ( kindMenu && typeList && (typeList->Count(ev) > 0) )
  1036.     {
  1037.         ODTypeListIterator* typeIter = typeList->CreateTypeListIterator(ev);
  1038.         
  1039.         for (ODType type = typeIter->First(ev);
  1040.             typeIter->IsNotComplete(ev);
  1041.             type = typeIter->Next(ev))
  1042.         {
  1043.             if ( editor == kODNULL )
  1044.             {
  1045.                 // Editor is not fixed; Aassume kind can be embedded even if there is
  1046.                 // no editor installed for it
  1047.                 EnableItem(kindMenu, ++itemStart);
  1048.             }
  1049.             else
  1050.             {
  1051.                 if ( EditorSupportsKind(nsm, editor, type) )
  1052.                     EnableItem(kindMenu, ++itemStart);
  1053.                 else
  1054.                     DisableItem(kindMenu, ++itemStart);
  1055.             }
  1056.             ODDisposePtr(type);
  1057.         }
  1058.     
  1059.         ODDeleteObject( typeIter );
  1060.     }
  1061.     
  1062.     return itemStart;
  1063. }
  1064.  
  1065. //------------------------------------------------------------------------------
  1066. // DeleteAllMenuItems
  1067. //------------------------------------------------------------------------------
  1068. ODStatic void DeleteAllMenuItems(MenuHandle theMenu)
  1069. {
  1070.     ODUShort i;
  1071.  
  1072.     for (i = CountMItems(theMenu);  i > 0;  --i)
  1073.     {
  1074.         DeleteMenuItem(theMenu, i);
  1075.     }
  1076. }
  1077.  
  1078. //------------------------------------------------------------------------------
  1079. // SetupEditorMenu
  1080. //------------------------------------------------------------------------------
  1081. ODBoolean SetupEditorMenu(ODType kind,
  1082.                             EditorSet* editorList,
  1083.                             MenuHandle editorMenu, 
  1084.                             ControlHandle popupCtlHndl, 
  1085.                             ODSession* session)
  1086. {
  1087.     ODBoolean anyEditors = (editorList->GetEditorCount() > 0);
  1088.     
  1089.     Environment* ev = somGetGlobalEnvironment();
  1090.     ODNameSpaceManager* nsm = session->GetNameSpaceManager(ev);
  1091.  
  1092.     // And remove all items from the editors menu
  1093.     DeleteAllMenuItems(editorMenu);
  1094.     
  1095.     // Get all editors supporting that kind; Preferred editor must be first
  1096.     if ( kind && !anyEditors)
  1097.     {
  1098.         ODEditor prefEditor = GetSysPrefEditorForKind(nsm, kind);
  1099.         if ( prefEditor )
  1100.             editorList->AddEditor(prefEditor);
  1101.         ODDisposePtr((ODPtr) prefEditor);
  1102.         anyEditors = GetAllEditorsForKind(nsm, kind, editorList);
  1103.     }
  1104.  
  1105.     if ( anyEditors )
  1106.     {
  1107.         AddEditorsToMenu(editorList, editorMenu, session);
  1108.         if ( popupCtlHndl )
  1109.         {
  1110.             SetControlMaximum(popupCtlHndl, editorList->GetEditorCount());
  1111.             HiliteControl(popupCtlHndl, kControlActive);
  1112.         }
  1113.     }
  1114.     else
  1115.     {
  1116.         // The editor popup should be inactive and contain one blank item
  1117.         AppendMenu(editorMenu, "\p");
  1118.         if ( popupCtlHndl )
  1119.         {
  1120.             SetControlMaximum(popupCtlHndl, 1);
  1121.             HiliteControl(popupCtlHndl, kControlInactive);
  1122.         }
  1123.     }
  1124.  
  1125.     if ( popupCtlHndl )
  1126.     {
  1127.         SetControlMinimum(popupCtlHndl, 1);
  1128.         SetControlValue(popupCtlHndl, 1);
  1129.     }
  1130.     
  1131.     return anyEditors;
  1132. }
  1133.  
  1134. //------------------------------------------------------------------------------
  1135. // AddEditorsToMenu
  1136. //------------------------------------------------------------------------------
  1137.  
  1138. void AddEditorsToMenu(EditorSet* editorList,
  1139.                                 MenuHandle editorMenu, 
  1140.                                 ODSession* session)
  1141. {
  1142.     ODName*        name;
  1143.     Str255         pEditorString;
  1144.     ODSShort        item = 0;
  1145.     Environment*         ev = somGetGlobalEnvironment();
  1146.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  1147.     
  1148.     EditorSetIterator* editorIter = editorList->CreateIterator();
  1149.     
  1150.     for (ODType editor = editorIter->First();
  1151.         editorIter->IsNotComplete();
  1152.         editor = editorIter->Next())
  1153.     {
  1154.         if (GetUserEditorFromEditor(nsm, editor, &name))
  1155.         {
  1156.             TempODName tempName = name; // DMc: ensure it's deleted
  1157.             IntlToPStr(name, pEditorString);
  1158.             InsertMenuItem(editorMenu, pEditorString, item);
  1159.             EnableItem(editorMenu, ++item);
  1160.             // comments for SetPopupItemScript say don't call unless the script
  1161.             // needs setting, but there's no way to test that here.  Sounds like
  1162.             // an optimization to me....
  1163.             SetPopupItemScript( editorMenu, item, GetITextScriptCode(name) );
  1164.         }
  1165.         else
  1166.         {
  1167.             // Special menu item in italics
  1168.             CUsingLibraryResources r;
  1169.             GetIndString(pEditorString, kODInfoUtilStrsID, kODStrIndNoEditors); 
  1170.  
  1171.             InsertMenuItem(editorMenu, pEditorString, item);
  1172.             DisableItem(editorMenu, ++item);
  1173.         }
  1174.     }
  1175.  
  1176.     ODDeleteObject( editorIter );
  1177. }
  1178.  
  1179. //------------------------------------------------------------------------------
  1180. // GetThisEditorFromList
  1181. //------------------------------------------------------------------------------
  1182. ODEditor GetThisEditorFromList(ODSShort editorIndex, EditorSet* editorList)
  1183. {
  1184.     ODEditor result = kODNULL;
  1185.     
  1186.     if ( editorIndex > 0 )
  1187.     {
  1188.         ODEditor editor;
  1189.         EditorSetIterator* iter = editorList->CreateIterator();
  1190.         
  1191.         for ( editor = iter->First(); 
  1192.               (--editorIndex > 0) && iter->IsNotComplete(); 
  1193.               editor = iter->Next() )
  1194.         {
  1195.         }
  1196.     
  1197.         delete iter;
  1198.         
  1199.         if ( editor != kODNULL )
  1200.             result = ODISOStrFromCStr((char *) editor);
  1201.     }
  1202.  
  1203.     return result;
  1204. }
  1205.  
  1206. //------------------------------------------------------------------------------
  1207. // IndexOfEditorInList
  1208. //------------------------------------------------------------------------------
  1209. ODSShort IndexOfEditorInList(EditorSet* editorList, ODEditor editor)
  1210. {
  1211.     ODSShort retVal = 0;
  1212.     
  1213.     if ( editorList->GetEditorCount() )
  1214.     {
  1215.         ODSShort i = 1;
  1216.         EditorSetIterator* editorIter = editorList->CreateIterator();
  1217.         
  1218.         for (ODEditor anEditor = editorIter->First();
  1219.             editorIter->IsNotComplete();
  1220.             anEditor = editorIter->Next())
  1221.         {
  1222.             if ( (anEditor == editor) || ODISOStrEqual(anEditor, editor) )
  1223.             {
  1224.                 retVal = i;
  1225.                 break;
  1226.             }
  1227.             i++;
  1228.         }
  1229.  
  1230.         ODDeleteObject( editorIter );
  1231.     }
  1232.     
  1233.     return retVal;
  1234. }
  1235.  
  1236. //------------------------------------------------------------------------------
  1237. // IndexOfKindInList
  1238. //------------------------------------------------------------------------------
  1239. ODSShort IndexOfKindInList(ODTypeList* kindList, ODType kind)
  1240. {
  1241.     ODSShort retVal = 0;
  1242.     
  1243.     Environment* ev = somGetGlobalEnvironment();
  1244.  
  1245.     if ( kindList->Count(ev) )
  1246.     {
  1247.         ODSShort i = 1;
  1248.         ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1249.         
  1250.         TRY
  1251.         
  1252.             for (ODType type = kindIter->First(ev);
  1253.                 kindIter->IsNotComplete(ev);
  1254.                 type = kindIter->Next(ev), i++)
  1255.             {
  1256.                 if ( ODISOStrEqual(type, kind) )
  1257.                     retVal = i;
  1258.                 ODDisposePtr(type);
  1259.                 if ( retVal != 0 )
  1260.                     break;
  1261.             }
  1262.  
  1263.         CATCH_ALL
  1264.             ODDeleteObject(kindIter);
  1265.             RERAISE;
  1266.         ENDTRY
  1267.         
  1268.         ODDeleteObject(kindIter);
  1269.     }
  1270.     
  1271.     return retVal;
  1272. }
  1273.  
  1274. //------------------------------------------------------------------------------
  1275. // GetThisKindFromList
  1276. //------------------------------------------------------------------------------
  1277. ODType GetThisKindFromList(ODSShort kindItem, ODTypeList* kindList)
  1278. {
  1279.     Environment* ev = somGetGlobalEnvironment();
  1280.     ODType type = kODNULL;
  1281.     ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1282.  
  1283.     for ( type = kindIter->First(ev); 
  1284.           (--kindItem > 0) && kindIter->IsNotComplete(ev); 
  1285.           type = kindIter->Next(ev) )
  1286.     {
  1287.         ODDisposePtr(type);
  1288.         type = kODNULL;
  1289.     }
  1290.  
  1291.     ODDeleteObject(kindIter);
  1292.  
  1293.     return type;
  1294. }
  1295.  
  1296.  
  1297. //------------------------------------------------------------------------------
  1298. // ContentValueTypes
  1299. //------------------------------------------------------------------------------
  1300. void ContentValueTypes(ODStorageUnit* contentSU, ODTypeList* typeList)
  1301. {
  1302.     TempODType hfsType = kODNULL;
  1303.  
  1304.     TRY
  1305.         ODULong        count;
  1306.         ODULong        index;
  1307.         
  1308.         Environment* ev = somGetGlobalEnvironment();
  1309.  
  1310.         ODTranslation* translation = contentSU->GetSession(ev)->GetTranslation(ev);
  1311.         hfsType = translation->GetISOTypeFromPlatformType(ev, kODFileType_hfs, kODPlatformDataType);
  1312.  
  1313.         contentSU->Focus(ev, kODPropContents, kODPosUndefined, 0, 0, kODPosUndefined);
  1314.         count = contentSU->CountValues(ev);
  1315.         for (index = 1; index <= count; ++index)
  1316.         {
  1317.             contentSU->Focus(ev, kODPropContents, kODPosUndefined, 0, index, kODPosUndefined);
  1318.             TempODType type = contentSU->GetType(ev);
  1319.             if ( !ODISOStrEqual(type, hfsType) )
  1320.                 typeList->AddLast(ev, type);
  1321.         }
  1322.     CATCH_ALL
  1323.     ENDTRY
  1324. }
  1325.  
  1326. //------------------------------------------------------------------------------
  1327. // ODUserRenameFile
  1328. //------------------------------------------------------------------------------
  1329.  
  1330.  
  1331. ODStatic ODBoolean ODUserRenameFile(Environment* ev,
  1332.                     ODSession* session, 
  1333.                     PlatformFile*    usersFile, 
  1334.                     ODIText* name,
  1335.                     DescType replaceOption)
  1336. {
  1337.     ODBoolean    fileRenamed = kODFalse;
  1338.     
  1339.     // name to aStr255
  1340.     Str255 aStr255;
  1341.     
  1342.     GetITextPString(name, aStr255);
  1343.  
  1344.     TRY
  1345.         usersFile->Rename(aStr255);
  1346.         fileRenamed = kODTrue;
  1347.     CATCH_ALL
  1348.         if (ErrorCode() == dupFNErr)
  1349.         {
  1350.             switch (replaceOption) {
  1351.             case kAEAsk:
  1352.                 ParamText(aStr255, "\p","\p","\p");
  1353.                 fileRenamed = ODAskUserReplace(ev, session);
  1354.                 break;
  1355.             case kAEYes:
  1356.                 fileRenamed = kODTrue; break;
  1357.             case kAENo:
  1358.                 fileRenamed = kODFalse; break;
  1359.             }
  1360.             
  1361.             if (fileRenamed)
  1362.             {
  1363.                 ODFileSpec fsspec = usersFile->GetFileSpec();
  1364.                 ODError result = HDelete(fsspec.vRefNum,fsspec.parID,aStr255);
  1365.                 if ( result!=wPrErr && result!=fLckdErr && 
  1366.                                     result!=fBsyErr && result!=afpAccessDenied )
  1367.                 {
  1368.                     TRY
  1369.                         usersFile->Rename(aStr255);
  1370.                     CATCH_ALL
  1371.                         WARN("File renaming failed, err %ld",ErrorCode());
  1372.                         fileRenamed = kODFalse;
  1373.                     ENDTRY
  1374.                 }
  1375.                 else
  1376.                 {
  1377.                     fileRenamed = kODFalse;
  1378.                     if (replaceOption == kAEAsk)
  1379.                         switch(result)
  1380.                         {
  1381.                             case wPrErr:
  1382.                             case fLckdErr:
  1383.                             case fBsyErr:
  1384.                             {
  1385.                                 CUsingLibraryResources res;
  1386.                                 StopAlert(kFilelockedAlrtID, GetODDialogFilter());
  1387.                                 break;
  1388.                             }
  1389.                             default:
  1390.                                 SysBeep(2);
  1391.                         }
  1392.                     else
  1393.                         SysBeep(2);
  1394.                 }
  1395.             }
  1396.         }
  1397.         else 
  1398.             RERAISE;
  1399.     ENDTRY
  1400.     return( fileRenamed );
  1401. }
  1402.  
  1403. //------------------------------------------------------------------------------
  1404. // ODAskUserReplace
  1405. //------------------------------------------------------------------------------
  1406.  
  1407. ODBoolean    ODAskUserReplace(Environment* ev,
  1408.                             ODSession* session)
  1409. // Pass in the name in question via ParamText(^0).
  1410. {  
  1411.     CUsingLibraryResources r;
  1412.     ODBoolean    doReplace    = kODFalse;
  1413.     short          itemHit     = 0;
  1414.     DialogPtr     dlg         = ODGetNewDialog(ev, kNameConflictDlgID, 
  1415.                                     session, kODFalse );
  1416.     if (dlg)
  1417.     {
  1418.         Handle        scratchHandle = kODNULL;
  1419.         Rect        scratchRect;
  1420.  
  1421.         SetPort(dlg);
  1422.         
  1423.             // Set the draw routine for the default button outline item
  1424.         GetDialogItem(dlg, kNameConflictDefaultButtonItem, &itemHit, &scratchHandle, &scratchRect);
  1425.         SetDialogItem(dlg, kNameConflictDefaultButtonItem, itemHit, 
  1426.                  (Handle)GetODOutlineDefaultButtonDrawProc(), &scratchRect);
  1427.                 
  1428.         ShowWindow(dlg);
  1429.  
  1430.         do {
  1431.             ModalDialog(GetODDialogFilter(), &itemHit);
  1432.             switch(itemHit)
  1433.             {
  1434.                 case kNameConflictReplaceBtn:
  1435.                     doReplace = kODTrue;
  1436.                     break;
  1437.                 case kNameConflictCancelBtn:
  1438.                     break;
  1439.             }
  1440.         } while ((itemHit != kNameConflictReplaceBtn) && 
  1441.                                         (itemHit != kNameConflictCancelBtn));
  1442.                 
  1443.         ODDisposeDialog(dlg);
  1444.     }
  1445.     return doReplace;
  1446. }
  1447.  
  1448. //------------------------------------------------------------------------------
  1449. //    CommentsDontMatch
  1450. //
  1451. //    This compares 2 ODIText strings to see if they match and returns TRUE if
  1452. //    they do not match.  It take into account the fact that the comments field
  1453. //    of the desktop database clips its string to 200 characters.
  1454. //
  1455. //    Returns false if the strings are identical but the script/language are
  1456. //    not.  This is because these values in dtComments have been set using
  1457. //    system defaults while those in propComments were actually saved, and
  1458. //    should therefore take precedence. <eeh>
  1459. //
  1460. //    propComments can be NULL, dtComments cannot
  1461. //------------------------------------------------------------------------------
  1462.  
  1463. ODBoolean
  1464. CommentsDontMatch(ODIText* dtComments, ODIText* propComments)
  1465. {
  1466.     ODBoolean    result;
  1467.     ODULong        dtLen, propLen;
  1468.     Str255        dtStr, propStr;
  1469.     
  1470.     if (dtComments != kODNULL)
  1471.         dtLen = GetITextStringLength(dtComments);
  1472.     else
  1473.         dtLen = 0;
  1474.     if (propComments == kODNULL)
  1475.         return (dtLen != 0);
  1476.     propLen = GetITextStringLength(propComments);
  1477.     if (dtLen == 0)
  1478.         return (propLen != 0);
  1479.     
  1480.     // if either string is length 0 (or NULL) we won't get here
  1481.     WASSERT(dtComments && propComments);
  1482.     result = kODTrue;
  1483.  
  1484.     // because the Finder does not store script and language with the
  1485.     // comments or file names, we ignore these in comparing strings.  That
  1486.     // is, if the strings are identical byte-for-byte, we consider them
  1487.     // to be equal.  That way, the string *we* stored will be used, as will
  1488.     // the script and language information we stored with it.
  1489.  
  1490.     ODBoolean lengthsMatch =
  1491.             (dtLen == propLen) || (dtLen == 200 && propLen >= 200);
  1492.     if ( lengthsMatch )
  1493.     {
  1494.         GetITextPString(dtComments, (StringPtr) &dtStr);
  1495.         GetITextPString(propComments, (StringPtr) &propStr);
  1496.         propStr[0] = dtStr[0];            // fix the 200 case
  1497.         result = !EqualString( dtStr, propStr, kODTrue, kODTrue );
  1498.     }
  1499.     return result;
  1500.  
  1501. }
  1502.  
  1503.  
  1504. void SetAllWindowShowLinks(Environment* ev, ODWindowState* winState, ODBoolean showLinks)
  1505. {
  1506.     ODWindowIterator* iter = kODNULL; ODVolatile(iter);
  1507.             
  1508.     TRY
  1509.  
  1510.         iter = winState->CreateWindowIterator(ev);
  1511.     
  1512.         for (ODWindow* window = iter->First(ev); iter->IsNotComplete(ev);
  1513.                 window = iter->Next(ev))
  1514.         {
  1515.             if (window)
  1516.             {
  1517.                 window->SetShouldShowLinks(ev, showLinks);
  1518.                 ODFrame* root = window->GetRootFrame(ev);
  1519.                 root->Invalidate(ev, kODNULL, kODNULL);
  1520.             }
  1521.         }
  1522.         ODDeleteObject(iter);
  1523.  
  1524.     
  1525.     CATCH_ALL
  1526.  
  1527.         ODDeleteObject(iter);
  1528.         RERAISE;
  1529.  
  1530.     ENDTRY
  1531. }
  1532.  
  1533. //------------------------------------------------------------------------------
  1534. // TranslateValueTypes
  1535. //------------------------------------------------------------------------------
  1536. void TranslateValueTypes(
  1537.                 ODTypeList*    kindList,
  1538.                 ODTypeList*    translateToList,
  1539.                 OrderedCollection* translateFromList,
  1540.                 ODSession*    session)
  1541. {
  1542.     Environment* ev = somGetGlobalEnvironment();
  1543.  
  1544.     ODTranslation* translation = session->GetTranslation(ev);
  1545.  
  1546.     ODTypeListIterator* kindIter = kindList->CreateTypeListIterator(ev);
  1547.     
  1548.     ODUShort kindIndex = 0;
  1549.  
  1550.     for (ODType kind = kindIter->First(ev);
  1551.         kindIter->IsNotComplete(ev);
  1552.         kind = kindIter->Next(ev))
  1553.     {
  1554.         ++kindIndex;
  1555.  
  1556.         TRY
  1557.             ODTypeList* toList = translation->GetTranslationOf(ev, kind);
  1558.             
  1559.             ODTypeListIterator* toIter = toList->CreateTypeListIterator(ev);
  1560.             
  1561.             for (ODType toKind = toIter->First(ev);
  1562.                 toIter->IsNotComplete(ev);
  1563.                 toKind = toIter->Next(ev))
  1564.             {
  1565.                 TRY
  1566.                 
  1567.                     if ( (kindList->Contains(ev, toKind) == kODFalse)
  1568.                         &&
  1569.                          (translateToList->Contains(ev, toKind) == kODFalse)
  1570.                        )
  1571.                     {
  1572.                         translateToList->AddLast(ev, toKind);
  1573.                         translateFromList->AddLast((void*) kindIndex);
  1574.                     }
  1575.                 
  1576.                 CATCH_ALL
  1577.                 ENDTRY
  1578.                 ODDisposePtr(toKind);
  1579.             }
  1580.             delete toIter;
  1581.             delete toList;
  1582.  
  1583.         CATCH_ALL
  1584.         ENDTRY
  1585.  
  1586.         ODDisposePtr(kind);
  1587.     }
  1588.     delete kindIter;
  1589. }
  1590.  
  1591. //------------------------------------------------------------------------------
  1592. // StringPtrFromStrHandle
  1593. //------------------------------------------------------------------------------
  1594.  
  1595. ODStatic StringPtr StringPtrFromStrHandle(StringHandle strHandle)
  1596. {
  1597.     StringPtr result = kODNULL;
  1598.  
  1599.     if ( strHandle )
  1600.     {
  1601.         ODULong size = GetHandleSize((Handle) strHandle);
  1602.         StringPtr strPtr = (StringPtr) ODLockHandle((ODHandle) strHandle);
  1603.         result = (StringPtr) ODNewPtr(size);
  1604.         ODBlockMove(strPtr, result, size);
  1605.         ODUnlockHandle((ODHandle) strHandle);
  1606.     }
  1607.  
  1608.     return result;
  1609. }
  1610.  
  1611. //------------------------------------------------------------------------------
  1612. // DefaultMenuStringForKind
  1613. //------------------------------------------------------------------------------
  1614.  
  1615. ODStatic StringPtr DefaultMenuStringForKind()
  1616. {
  1617.     CUsingLibraryResources r;
  1618.  
  1619.     StringPtr result = kODNULL;
  1620.  
  1621.     StringHandle strHandle = GetString(kODPartInfoStrUnknownID);
  1622.     if ( strHandle )
  1623.     {
  1624.         result = StringPtrFromStrHandle(strHandle);
  1625.         ReleaseResource((Handle) strHandle);
  1626.     }
  1627.     else
  1628.     {
  1629.         strHandle = NewString("\p");
  1630.         result = StringPtrFromStrHandle(strHandle);
  1631.         ODDisposeHandle((ODHandle) strHandle);
  1632.     }
  1633.     return result;
  1634. }
  1635.  
  1636. //------------------------------------------------------------------------------
  1637. // GetMenuStringForKind
  1638. //------------------------------------------------------------------------------
  1639.  
  1640. ODStatic void GetMenuStringForKind(
  1641.     ODSession*        session,
  1642.     ODType            kind,
  1643.     StringPtr*        itemString,
  1644.     ODScriptCode*    itemScript)
  1645. {
  1646.     ODName*                name;
  1647.     Environment*        ev = somGetGlobalEnvironment();
  1648.     ODNameSpaceManager*    nsm = session->GetNameSpaceManager(ev);
  1649.  
  1650.     if ( GetUserKindFromKind(nsm, kind, &name) )
  1651.     {
  1652.         TempODName tempName = name; // DMc: ensure it's deleted
  1653.         *itemString = (StringPtr) ODNewPtr(GetITextStringLength(name)+1);
  1654.         IntlToPStr(name, *itemString);
  1655.         *itemScript = GetITextScriptCode(name);
  1656.     }
  1657.     else
  1658.     {
  1659.         // No available editors support this kind
  1660.         ODTranslation* translation = session->GetTranslation(ev);
  1661.         ODPlatformType platformType = translation->GetPlatformTypeFromISOType(ev, kind);
  1662.         if ( platformType != kODNULL )
  1663.         {
  1664.             *itemString = (StringPtr) ODNewPtr(sizeof(ODPlatformType) + 1);
  1665.             ODBlockMove(&platformType, &((*itemString)[1]), (ODULong) sizeof(ODPlatformType));
  1666.             (*itemString)[0] = (unsigned char) sizeof(ODPlatformType);
  1667.         }
  1668.         else
  1669.         {
  1670.             *itemString = DefaultMenuStringForKind();
  1671.         }
  1672.         *itemScript = smSystemScript;
  1673.     }
  1674. }
  1675.  
  1676. //------------------------------------------------------------------------------
  1677. // AddTranslationKindToMenu
  1678. //------------------------------------------------------------------------------
  1679.  
  1680. void AddTranslationKindToMenu(
  1681.         ODType        kind, 
  1682.         MenuHandle    kindMenu,
  1683.         short        item,
  1684.         ODBoolean    insert,
  1685.         ODSession*    session)
  1686. {
  1687.     StringPtr itemString;
  1688.     ODScriptCode itemScript;
  1689.     
  1690.     GetMenuStringForKind(session, kind, &itemString, &itemScript);
  1691.     TempODString tempItemString = (char*) itemString; // DMc: ensure it's deleted
  1692.  
  1693.     if ( insert )
  1694.     {
  1695.         InsertMenuItem(kindMenu, itemString, item);
  1696.         item += 1;
  1697.     }
  1698.     else
  1699.         SetMenuItemText(kindMenu, item, itemString);
  1700.     
  1701.     SetPopupItemScript(kindMenu, item, itemScript);
  1702. }
  1703.