/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "appletdefs.h"
#include "appletprocess.h"
#include "appletmemory.h"
#include "appletfiles.h"
#include "appletfilesinternal.h"
#include "appletfiledesktop.h"



boolean hasdesktopmanager (short vnum) {
	
	GetVolParmsInfoBuffer volparms;
	HParamBlockRec pb;
	
	pb.volumeParam.ioVRefNum = vnum;
	
	pb.volumeParam.ioNamePtr = nil;
	
	pb.ioParam.ioBuffer = (Ptr) &volparms;
	
	pb.ioParam.ioReqCount = sizeof (volparms);
	
	if (PBHGetVolParmsSync (&pb) != noErr)
		return (false);
	
	return ((volparms.vMAttrib & (1 << bHasDesktopMgr)) != 0);
	} /*hasdesktopmanager*/


boolean findapplication (short vnum, OSType creator, boolean flappsonly, FSSpec *pfs) {
	
	/*
	dmb 9/21/93: keep looping until an APPL is found, or PBDTGetAPPLSync 
	returns an error. (it will eventually return afpItemNotFound.)
	
	dmb 9/24/93: added flappsonly parameter. a non-app, such as an extension 
	or desk accessory, can own a document type.
	*/
	
	DTPBRec dt;
	bigstring appname;
	tyfileinfo info;
	
	if (!hasdesktopmanager (vnum))
		return (false);
		
	clearbytes (&dt, longsizeof (dt));
		
	dt.ioVRefNum = vnum;
	
	if (PBDTGetPath (&dt) != noErr)
		return (false);
		
	dt.ioNamePtr = (StringPtr) &appname;
	
	dt.ioFileCreator = creator;
	
	for (dt.ioIndex = 0; ; ++dt.ioIndex) {
		
		if (PBDTGetAPPLSync (&dt) != noErr) 
			return (false);
		
		if (!filemakespec (vnum, dt.ioAPPLParID, appname, pfs))
			continue;
		
		if (!fileexists (pfs))
			continue;
		
		if (!filegetinfo (pfs, &info))
			continue;
		
		if (!flappsonly) /*dmb 9/24/93: some callers don't require apps*/
			return (true);
		
		if (info.filetype == 'APPL') /*DW 9/16/93: desktop db can contain references to non-apps*/
			return (true);
		}
	} /*findapplication*/


boolean findapponanydisk (OSType appid, FSSpec *fs) {
	
	/*
	find the app whose creator id is appid. return true if we found it, 
	false otherwise.
	*/
	
	DTPBRec dt;
	ParamBlockRec pb;
	Str255 appfname;
	
	clearbytes (&pb, (long) sizeof (pb));
	
	while (true) {
		
		++pb.volumeParam.ioVolIndex;
		
		if (PBGetVInfoSync (&pb) != noErr)
			return (false);
		
		if (!hasdesktopmanager (pb.volumeParam.ioVRefNum))
			continue;
		
		dt.ioNamePtr = NULL;
		
		dt.ioVRefNum = pb.volumeParam.ioVRefNum;
		
		if (PBDTGetPath (&dt) != noErr)
			return (false);
		
		dt.ioNamePtr = (StringPtr) &appfname;
		
		dt.ioIndex = 0;
		
		dt.ioFileCreator = appid;
		
		if (PBDTGetAPPLSync (&dt) == noErr) {
		
			if (FSMakeFSSpec (pb.volumeParam.ioVRefNum, dt.ioAPPLParID, appfname, fs) == noErr)
				return (true);
			}
		} /*while*/
		
	return (false);
	} /*findapponanydisk*/


boolean assureappisrunning (OSType serverid, boolean flbringtofront) {
	
	/*
	if the application whose creator id is serverid is running, return true.
	
	if not, we look for the application and try to launch it. we wait until it's 
	actually running and ready to receive Apple Events.
	*/
	
	typrocessinfo info;
	FSSpec fs;
	
	if (findrunningapp (serverid, &info)) /*already running, nothing to do*/
		return (true);
		
	if (!findapponanydisk (serverid, &fs))
		return (false);
		
	if (!launchappwithdoc (&fs, nil, flbringtofront))
		return (false);
		
	return (true);
	} /*assureappisrunning*/
	
	
boolean getfilecomment (FSSpec *pfs, bigstring bscomment) {
	
	DTPBRec dt;
	
	clearbytes (&dt, longsizeof (dt));
	
	setemptystring (bscomment); /*default return*/
	
	if (!hasdesktopmanager ((*pfs).vRefNum))
		return (false);
	
	dt.ioVRefNum = (*pfs).vRefNum;
	
	if (PBDTGetPath (&dt) != noErr)
		return (false);
	
	dt.ioNamePtr = (*pfs).name;
	
	dt.ioDirID = (*pfs).parID;
	
	dt.ioDTBuffer = (Ptr) bscomment + 1;
	
	dt.ioDTReqCount = lenbigstring;
	
	if (PBDTGetCommentSync (&dt) != noErr)
		return (false);
	
	setstringlength (bscomment, dt.ioDTActCount);
	
	return (true);
	} /*getfilecomment*/


boolean setfilecomment (FSSpec *pfs, bigstring bscomment) {
	
	DTPBRec dt;
	
	clearbytes (&dt, longsizeof (dt));
	
	if (!hasdesktopmanager ((*pfs).vRefNum))
		return (false);
	
	dt.ioVRefNum = (*pfs).vRefNum;
	
	if (PBDTGetPath (&dt) != noErr)
		return (false);
	
	dt.ioNamePtr = (*pfs).name;
	
	dt.ioDirID = (*pfs).parID;
	
	dt.ioDTBuffer = (Ptr) bscomment + 1;
	
	dt.ioDTReqCount = stringlength (bscomment);
	
	if (PBDTSetCommentSync (&dt) != noErr)
		return (false);
	
	PBDTFlushSync (&dt);
	
	return (true);
	} /*setfilecomment*/


boolean filecopycomment (FSSpec *fssource, FSSpec *fsdest) {

	bigstring bscomment;
	
	if (!getfilecomment (fssource, bscomment))
		return (false);
	
	return (setfilecomment (fsdest, bscomment));
	} /*filecopycomment*/