home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libpref / src / mac / macpref.cp next >
Encoding:
Text File  |  1998-04-08  |  4.0 KB  |  164 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "xp_core.h"
  20. #include "prefapi.h"
  21. #include "jsapi.h"
  22. #include "prlink.h"
  23. #include "ufilemgr.h"
  24. #include "uprefd.h"
  25.  
  26. #include <Types.h>
  27. #include <Resources.h>
  28. #include <Memory.h>
  29.  
  30. /*
  31.  * Mac-specific libpref routines
  32.  */
  33.  
  34. extern "C" {
  35. JSBool pref_InitInitialObjects();
  36. }
  37. Boolean pref_FindAutoAdminLib(FSSpec& spec);
  38.  
  39. static JSBool pref_ReadResource(short id)
  40. {
  41.     JSBool ok = JS_FALSE;
  42.     Handle data;
  43.     UInt32 datasize;
  44.     data = GetResource('TEXT', id);
  45.     
  46.     if (data) {
  47.         DetachResource( data );
  48.         HNoPurge( data );
  49.         MoveHHi( data );
  50.         datasize = GetHandleSize(data);
  51.  
  52.         HLock(data);
  53.         ok = (JSBool) PREF_QuietEvaluateJSBuffer((char*) *data, datasize);
  54.         HUnlock(data);
  55.         DisposeHandle(data);
  56.     }
  57.  
  58.     return ok;
  59. }
  60.  
  61. /*
  62.  * Initialize default preference JavaScript buffers from
  63.  * appropriate TEXT resources
  64.  */
  65. JSBool pref_InitInitialObjects()
  66. {
  67.     JSBool ok = pref_ReadResource(3000);        // initprefs
  68.     if (ok)
  69.         ok = pref_ReadResource(3010);            // all.js
  70.     if (ok)
  71.         ok = pref_ReadResource(3016);            // mailnews.js
  72.     if (ok)
  73.         ok = pref_ReadResource(3017);            // editor.js
  74.     if (ok)
  75.         ok = pref_ReadResource(3018);            // security.js
  76.     if (ok)
  77.         ok = pref_ReadResource(3011);            // config.js
  78.     if (ok)
  79.         ok = pref_ReadResource(3015);            // macprefs.js
  80.     
  81.     return ok;
  82. }
  83.  
  84. /*
  85.  * Convert between cross-platform file/folder pathname strings
  86.  * and Mac aliases flattened into binary strings
  87.  */
  88. PR_IMPLEMENT(int)
  89. PREF_CopyPathPref(const char *pref_name, char ** return_buffer)
  90. {
  91.     int dirSize, result;
  92.     AliasHandle aliasH = NULL;
  93.     char *dirAliasBuf = NULL;
  94.     OSErr err;
  95.     FSSpec fileSpec;
  96.     Boolean changed;
  97.  
  98.     result = PREF_CopyBinaryPref(pref_name, &dirAliasBuf, &dirSize);
  99.     if (result != PREF_NOERROR)
  100.         return result;
  101.  
  102.     // Cast to an alias record and resolve.
  103.     err = PtrToHand(dirAliasBuf, &(Handle) aliasH, dirSize);
  104.     free(dirAliasBuf);
  105.     if (err != noErr)
  106.         return PREF_ERROR; // not enough memory?
  107.  
  108.     err = ::ResolveAlias(NULL, aliasH, &fileSpec, &changed);
  109.     DisposeHandle((Handle) aliasH);
  110.     if (err != noErr)
  111.         return PREF_ERROR; // bad alias
  112.         
  113.     *return_buffer = CFileMgr::EncodedPathNameFromFSSpec(fileSpec, TRUE);
  114.     
  115.     return PREF_NOERROR;
  116. }
  117.  
  118. PR_IMPLEMENT(int)
  119. PREF_SetPathPref(const char *pref_name, const char *path, XP_Bool set_default)
  120. {
  121.     FSSpec fileSpec;
  122.     AliasHandle    aliasH;
  123.     OSErr err = CFileMgr::FSSpecFromLocalUnixPath(path, &fileSpec);
  124.     if (err != noErr)
  125.         return PREF_ERROR;
  126.  
  127.     err = NewAlias(nil, &fileSpec, &aliasH);
  128.     if (err != noErr)
  129.         return PREF_ERROR;
  130.  
  131.     int result;
  132.     Size bytes = GetHandleSize((Handle) aliasH);
  133.     HLock((Handle) aliasH);
  134.     
  135.     if (set_default)
  136.         result = PREF_SetDefaultBinaryPref(pref_name, *aliasH, bytes);
  137.     else
  138.         result = PREF_SetBinaryPref(pref_name, *aliasH, bytes);
  139.     DisposeHandle((Handle) aliasH);
  140.  
  141.     return result;
  142. }
  143.  
  144. /* Looks for AutoAdminLib in Essential Files and returns FSSpec */
  145. Boolean
  146. pref_FindAutoAdminLib(FSSpec& spec)
  147. {
  148.     spec = CPrefs::GetFilePrototype(CPrefs::RequiredGutsFolder);
  149.     LString::CopyPStr("\pAutoAdminLib", spec.name, 32);
  150.     
  151.     if (!CFileMgr::FileExists(spec))
  152.         LString::CopyPStr("\pAutoAdminPPCLib", spec.name, 32);
  153.     
  154.     return CFileMgr::FileExists(spec);
  155. }
  156.  
  157. PR_IMPLEMENT(XP_Bool)
  158. PREF_IsAutoAdminEnabled()
  159. {
  160.     FSSpec spec;
  161.     return (XP_Bool) pref_FindAutoAdminLib(spec);
  162. }
  163.  
  164.