Hi,
I want to temporarily apply badge on selected files and folders.
though i am successful in it,there are some hiccups.
1] If a badge is applied and then removed on a folder. Again try to apply badge on the same folder,it does not get applied.
I want the addition and removal of badges to be really smooth and the finder should update without relaunching it.
Can anyone please see the code below and tell me what wrong am i doing?
Any kind of help is appreciated.
Boolean AddBadgeToItem(char *path, char *icon )
{
FSCatalogInfo info;
FSRef pToFSRef;
FSRef pToFSRef2;
Boolean isDirectory = false;
OSErr osErrResult = noErr;
if (nil == path)
return false;
osErrResult = FSPathMakeRef(path,&pToFSRef,&isDirectory) ;
if (icon && (osErrResult==noErr))
{
HFSUniStr255 fork;
SInt16 refnum = kResFileNotOpened;
osErrResult = FSGetResourceForkName(&fork);
if(osErrResult != noErr)
return false;
if (isDirectory)
{
memset(&info,0,sizeof(info));
((FileInfo*)(&info.finderInfo))->finderFlags = kIsInvisible;
osErrResult = FSCreateResourceFile(&pToFSRef,8,(UniChar*)"\000I\000c\000o\000n\000\r",kFSCatInfoFinderXInfo,&info,fork.length,fork.unicode,&pToFSRef2,NULL);
}
else
{
BlockMoveData(&pToFSRef,&pToFSRef2,sizeof(FSRef));
osErrResult = FSCreateResourceFork(&pToFSRef2,fork.length,fork.unicode,0);
//here take care of condition !=noErr &
//errFSForkExists -1421 An attempt to create a fork, but that fork already exists.
//even with this function failing,FSOpenResourceFile succeeds!!,naturally:-))
//so let it pass
through.
}
osErrResult = FSOpenResourceFile(&pToFSRef2,fork.length,fork.unicode,fsRdWrPerm,&refnum);
if (osErrResult!=noErr)
return false;
if (refnum!=kResFileNotOpened)
{
CustomBadgeResource* cbr;
struct stat fs ;
stat ( icon, &fs ) ;
int len = fs.st_size;
char *bytes = malloc ( len ) ;
FILE *fp = fopen ( icon, "rb" ) ;
fread ( bytes, sizeof (char), len, fp ) ;
fclose ( fp ) ;
Handle h = NewHandle(len);
if (h)
{
BlockMoveData(bytes,*h,len);
AddResource(h,kIconFamilyType,128,"\p");
WriteResource(h);
ReleaseResource(h);
}
h = NewHandle(sizeof(CustomBadgeResource));
if (h)
{
cbr = (CustomBadgeResource*)*h;
memset(cbr,0,sizeof(CustomBadgeResource));
cbr->version = kCustomBadgeResourceVersion;
cbr->customBadgeResourceID = 128;
AddResource(h,kCustomBadgeResourceType,kCustomBadgeResourceID,"\p");
WriteResource(h);
ReleaseResource(h);
}
UpdateResFile(refnum);
CloseResFile(refnum);
!
if
(FSGetCatalogInfo(&pToFSRef,kFSCatInfoFinderXInfo,&info,NULL,NULL,NULL)==noErr)
{
((ExtendedFileInfo*)(&info.extFinderInfo))->extendedFinderFlags = kExtendedFlagHasCustomBadge;
FSSetCatalogInfo(&pToFSRef,kFSCatInfoFinderXInfo,&info);
}
MoreFEUpdateItemFSRef(&pToFSRef);
return true;
}
}
return false;
}
FSCreateResourceFile - returns -48 and FSOpenResourceFile - returns
-35. Why is this? Am i not removing the badge correctly that it is not allwoing the re - applying of badge?
The Removal Part of the code is in Part II of the mail.