CFDataRef copyIconDataForURL(CFURLRef URL) {
	CFDataRef data = NULL;

	if (URL) {
		FSRef ref;
		if (CFURLGetFSRef(URL, &ref)) {
			IconRef icon = NULL;
			SInt16 label_noOneCares;
			OSStatus err = GetIconRefFromFileInfo(&ref,
												  /*inFileNameLength*/ 0U, /*inFileName*/ NULL,
												  kFSCatInfoNone, /*inCatalogInfo*/ NULL,
												  kIconServicesNoBadgeFlag | kIconServicesUpdateIfNeededFlag,
												  &icon,
												  &label_noOneCares);
			if (err != noErr) {
				NSLog(CFSTR("in copyIconDataForURL in CFGrowlAdditions: could not get icon for %@: GetIconRefFromFileInfo returned %li\n"), URL, (long)err);
			} else {
				IconFamilyHandle fam = NULL;
				err = IconRefToIconFamily(icon, kSelectorAllAvailableData, &fam);
				if (err != noErr) {
					NSLog(CFSTR("in copyIconDataForURL in CFGrowlAdditions: could not get icon for %@: IconRefToIconFamily returned %li\n"), URL, (long)err);
				} else {
					HLock((Handle)fam);
					data = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)*(Handle)fam, GetHandleSize((Handle)fam));
					HUnlock((Handle)fam);
					DisposeHandle((Handle)fam);
				}
				ReleaseIconRef(icon);
			}
		}
	}

	return data;
}
Beispiel #2
0
/* get IPIcon from file icon */
OSErr GetFileIPIcon(const FSSpec *theFile,IPIconRec *ipIcon,Boolean isFolder,Boolean *is32Exist,short *alertMode)
{
	#if TARGET_API_MAC_CARBON
	#pragma unused(isFolder)
	#endif
	OSErr		err=noErr;
	
	if (isFolder)
		err=FolderIconCheck(theFile,alertMode);
	else
		err=FileIconCheck(theFile,alertMode);
	
	#if !TARGET_API_MAC_CARBON
	if (isIconServicesAvailable)
	#endif
	{
		/* use IconServices when runinng on 8.5 or later */
		IconRef		iconRef;
		short		label;
		IconFamilyHandle	iconFamily;
		
		err=GetIconRefFromFile(theFile,&iconRef,&label);
		if (err!=noErr) return err;
		
		//err=IconRefToIconFamily(iconRef,GetMySelector(),&iconFamily);
		err=IconRefToIconFamily(iconRef,kSelectorAllAvailableData,&iconFamily); // 1.20b14
		ReleaseIconRef(iconRef);
		if (err!=noErr) return err;
		
		err=IconFamilyToIPIcon(iconFamily,ipIcon);
		DisposeHandle((Handle)iconFamily);
		
		if (err==noErr)
			err=IPIconHas32Icons(ipIcon,is32Exist);
		return err;
	}
	#if !TARGET_API_MAC_CARBON
	else
	{
		/* use Find_icon when running on older than 8.5 */
		err=Find_icon(theFile,NULL,kSelectorMyData,&ipIcon->iconSuite);
		ipIcon->it32Data=NULL;
		ipIcon->t8mkData=NULL;
		
		return err;
	}
	#endif
}
Beispiel #3
0
static PyObject *Icn_IconRefToIconFamily(PyObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSErr _err;
	IconRef theIconRef;
	IconSelectorValue whichIcons;
	IconFamilyHandle iconFamily;
#ifndef IconRefToIconFamily
	PyMac_PRECHECK(IconRefToIconFamily);
#endif
	if (!PyArg_ParseTuple(_args, "O&l",
	                      ResObj_Convert, &theIconRef,
	                      &whichIcons))
		return NULL;
	_err = IconRefToIconFamily(theIconRef,
	                           whichIcons,
	                           &iconFamily);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     ResObj_New, iconFamily);
	return _res;
}
Beispiel #4
0
static void AddFolderIcon (FSRef *fref, const char *folderName)
{
	OSStatus			err;
	FSCatalogInfo		fcat, icat;
	FSRef				bref, iref;
	CFStringRef			str;
	CFURLRef			url;
	IconFamilyHandle	family;
	IconRef				icon;
	HFSUniStr255		fork;
	Boolean				r;
	SInt16				resf;
	char				name[64];
	UniChar				iconName[5] = { 'I', 'c', 'o', 'n', '\r' };

	strcpy(name, "folder_");
	strcat(name, folderName);

	str = CFStringCreateWithCString(kCFAllocatorDefault, name, CFStringGetSystemEncoding());
	if (str)
	{
		url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), str, CFSTR("icns"), NULL);
		if (url)
		{
			r = CFURLGetFSRef(url, &bref);
			if (r)
			{
				err = RegisterIconRefFromFSRef('~9X~', 'TEMP', &bref, &icon);
				if (err == noErr)
				{
					err = FSGetResourceForkName(&fork);
					if (err == noErr)
					{
						err = FSCreateResourceFile(fref, 5, iconName, kFSCatInfoNone, NULL, fork.length, fork.unicode, &iref, NULL);
						if (err == noErr)
						{
							err = FSOpenResourceFile(&iref, fork.length, fork.unicode, fsWrPerm, &resf);
							if (err == noErr)
							{
								err = IconRefToIconFamily(icon, kSelectorAllAvailableData, &family);
								if (err == noErr)
								{
									AddResource((Handle) family, 'icns', -16455, "\p");
									WriteResource((Handle) family);
									ReleaseResource((Handle) family);

									err = FSGetCatalogInfo(&iref, kFSCatInfoFinderInfo, &icat, NULL, NULL, NULL);
									((FileInfo *) &icat.finderInfo)->finderFlags |= kIsInvisible;
									((FileInfo *) &icat.finderInfo)->fileCreator = 'MACS';
									((FileInfo *) &icat.finderInfo)->fileType    = 'icon';
									err = FSSetCatalogInfo(&iref, kFSCatInfoFinderInfo, &icat);

									err = FSGetCatalogInfo(fref, kFSCatInfoFinderInfo, &fcat, NULL, NULL, NULL);
									((FolderInfo *) &fcat.finderInfo)->finderFlags |=  kHasCustomIcon;
									((FolderInfo *) &fcat.finderInfo)->finderFlags &= ~kHasBeenInited;
									err = FSSetCatalogInfo(fref, kFSCatInfoFinderInfo, &fcat);
								}

								CloseResFile(resf);
							}
						}
					}
Beispiel #5
0
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Given two path strings, srcPath and destPath, creates a MacOS Finder alias from file in srcPath
//  in the destPath, complete with custom icon and all.  Pretty neat.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
static void CreateAlias (char *srcPath, char *destPath)
{
  OSErr err;

  FSSpec sourceFSSpec;
  FSRef srcRef, destRef;
  OSType srcFileType = (OSType)NULL;
  OSType srcCreatorType = (OSType)NULL;
  FInfo srcFinderInfo, destFinderInfo;

  int fd;
  SInt16 rsrcRefNum;

  IconRef srcIconRef;
  IconFamilyHandle srcIconFamily;
  SInt16 theLabel;

  AliasHandle alias;
  short isSrcFolder;

  //find out if we're dealing with a folder alias
  isSrcFolder = UnixIsFolder(srcPath);
  if (isSrcFolder == -1)//error
  {
    fprintf(stderr, "UnixIsFolder(): Error doing a stat on %s\n", srcPath);
    exit(EX_IOERR);
  }

  ///////////////////// Get the FSRef's and FSSpec's for source and dest ///////////////////

  //get file ref to src
  err = FSPathMakeRef(srcPath, &srcRef, NULL);
  if (err != noErr)
  {
    fprintf(stderr, "FSPathMakeRef: Error %d getting file ref for source \"%s\"\n", err, srcPath);
    exit(EX_IOERR);
  }

  //retrieve source filespec from source file ref
  err = FSGetCatalogInfo (&srcRef, NULL, NULL, NULL, &sourceFSSpec, NULL);
  if (err != noErr)
  {
    fprintf(stderr, "FSGetCatalogInfo(): Error %d getting file spec from source FSRef\n", err);
    exit(EX_IOERR);
  }

  //get the finder info for the source if it's a folder
  if (!isSrcFolder)
  {
    err = FSGetFInfo (&srcRef, &srcFinderInfo);
    if (err != noErr)
    {
      fprintf(stderr, "FSpGetFInfo(): Error %d getting Finder info for source \"%s\"\n", err, srcPath);
      exit(EX_IOERR);
    }
    srcFileType = srcFinderInfo.fdType;
    srcCreatorType = srcFinderInfo.fdCreator;
  }

  //////////////// Get the source file's icon ///////////////////////

  if (!noCustomIconCopy)
  {
    err = GetIconRefFromFileInfo(
      &srcRef, 0, NULL, 0, NULL,
      kIconServicesNormalUsageFlag, &srcIconRef, &theLabel
    );

    if (err != noErr)
      fprintf(stderr, "GetIconRefFromFile(): Error getting source file's icon.\n");

    IconRefToIconFamily (srcIconRef, kSelectorAllAvailableData, &srcIconFamily);
  }

  ///////////////////// Create the relevant alias record ///////////////////

  if (makeRelativeAlias)
  {
    // The following code for making relative aliases was borrowed from Apple. See the following technote:
    //
    //  http://developer.apple.com/technotes/tn/tn1188.html
    //

    // create the new file
    err = myFSCreateResFile(destPath, 'TEMP', 'TEMP', &destRef);
    if (err != noErr)
    {
      fprintf(stderr, "FSpCreateResFile(): Error %d while creating file\n", err);
      exit(EX_CANTCREAT);
    }

    //create the alias record, relative to the new alias file
    err = FSNewAlias(&destRef, &srcRef, &alias);
    if (err != noErr)
    {
      fprintf(stderr, "NewAlias(): Error %d while creating relative alias\n", err);
      exit(EX_CANTCREAT);
    }

    // save the resource
    rsrcRefNum = FSOpenResFile(&destRef, fsRdWrPerm);
    if (rsrcRefNum == -1)
    {
      err = ResError();
      fprintf(stderr, "Error %d while opening resource fork for %s\n", err, (char *)&destPath);
      exit(EX_IOERR);
    }

    UseResFile(rsrcRefNum);
    Str255 rname;
    AddResource((Handle) alias, rAliasType, 0, NULL);

    if ((err = ResError()) != noErr)
    {
      fprintf(stderr, "Error %d while adding alias resource for %s", err, (char *)&destPath);
      exit(EX_IOERR);
    }
    if (!noCustomIconCopy)
    {
      //write the custom icon data
      AddResource( (Handle)srcIconFamily, kIconFamilyType, kCustomIconResource, "\p");
    }

    CloseResFile(rsrcRefNum);
  }
  else
  {