/* 
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 "appletfiles.h"
#include "appletfilesinternal.h"
#include "appletfolder.h"
#include "appletprioritydiskloop.h"


static tyfileloopcallback usercallback = nil;

static short userfavoritevnum;

static short passnum;



static boolean priorityvisit (bigstring fname, tyfileinfo *info, long refcon) {
	
	boolean flslow = (*info).flhardwarelock || (*info).flremotevolume;
	boolean flfave = (*info).vnum == userfavoritevnum;
	
	switch (passnum) {
		
		case 0:
			if (!flfave) /*only interested in the favorite disk on pass 0*/
				return (true);
				
			if (flslow) /*we're going to ignore the favorite, it's a slow disk*/
				return (true);
				
			break;
		
		case 1: 
			if (flfave) /*already visited in pass 0*/
				return (true);
				
			if (flslow) /*save for pass 2*/
				return (true);
				
			break;
			
		case 2: 
			if (flfave) /*already visited in pass 0*/
				return (true);
				
			if (!flslow) /*already visited in pass 1*/
				return (true);
				
			break;
		} /*switch*/
	
	return ((*usercallback) (fname, info, refcon));
	} /*priorityvisit*/
	

boolean prioritydiskloop (tyfileloopcallback callback, long refcon, short vnumfavorite) {
	
	/*
	same as diskloop, except we process the volumes 
	according to a sensible priority scheme.
	
	if vnumfavorite is non-zero, we look on that
	volume first.
	
	then we look on all fast volumes except the
	favorite one.
	
	finally we look at all slow volumes, ones that
	are remote, or hardware locked (probably CD's).
	
	note: these can't be nested because the callback 
	is stored in a static, it's not stacked.
	*/
	
	short firstpassnum;
	
	usercallback = callback;
	
	userfavoritevnum = vnumfavorite;
	
	if (vnumfavorite == 0)
		firstpassnum = 1;
	else
		firstpassnum = 0;
	
	for (passnum = firstpassnum; passnum <= 2; passnum++) {
	
		if (!diskloop (&priorityvisit, refcon))
			return (false);
		} /*for*/
	
	return (true);
	} /*prioritydiskloop*/