/*
 * Filename: NamePSLookup.java
 *
 * Author: Joe Wang
 *
 * Copyright (c) 2003-2005 
 * The Trustees of Columbia University in the City of New York
 * Columbia University Information Technology
 *
 * This file is part of NINJa.
 *
 * NINJa is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * NINJa is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NINJa; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Created on May 8, 2002, 11:10 AM
 */

package edu.columbia.print.ninja.prix.service;

import com.millipad.util.*;
import edu.columbia.print.ninja.prix.service.lpr.*;
import edu.columbia.print.ninja.prix.service.win.*;
import edu.columbia.print.ninja.core.service.*;
import net.jini.print.service.*;
import java.io.*;
import java.util.*;
import javax.print.attribute.standard.*;


/**
 * Gets print services based on the name.
 * @author  <a href="mailto:jw168@columbia.edu">Joe Wang</a>
 */
public class NamePSLookup extends Lookup implements Serializable {

	public static final int WINDOWS = 0;
	public static final int LPRNG = 1;

	String[] names;
	RemotePrintService[] printers;
	boolean inited;

	/** Creates a new instance of SimplexLookup */
	public NamePSLookup(String[] names) {
		this.names = names;
		this.printers = null;
		this.inited = false;
	}

	private static void debug(Object o) {Debug.print(NamePSLookup.class, o);}

	public void init() {
		if(inited) return;
		inited = true;
		//find out the os
		String os = System.getProperty("os.name");
		int type = WINDOWS;
		if(os.equals("Windows 2000")) type = WINDOWS;
		if(os.equals("SunOS")) type = LPRNG;
		if(os.equals("Linux")) type = LPRNG;
		//find all the print services
		javax.print.PrintService[] ps = javax.print.PrintServiceLookup.lookupPrintServices(null, null);
		debug("pss "+ps.length);
		Vector valid = new Vector();
		for(int i=names.length-1; i>=0; i--) {
			for(int j=ps.length-1; j>=0; j--) {
				Object name = ps[j].getAttribute(PrinterName.class);
				System.out.println(name);
				if(!names[i].equals(name.toString())) continue;
				try {
					switch(type) {
					case LPRNG: {
						valid.add(new RemotePrintServiceAdapter(new LprPrinter(ps[j], null)));
						break;
					}
					default:
						valid.add(new RemotePrintServiceAdapter(new WinPrinter(ps[j])));
						}
					}
				catch(Exception e) {
					e.printStackTrace();
					}
				}
			}
		if(valid.size()!=0) printers = (RemotePrintService[] )valid.toArray(new RemotePrintService[valid.size()]);
		}

	public RemotePrintService getDefaultPrintService() {
		init();
		return (printers==null) ? null : printers[0];
		}
	
	public MultiDocPrintService[] getMultiDocPrintServices(javax.print.DocFlavor[] docFlavor, javax.print.attribute.AttributeSet attributeSet) {
		init();
		return null;
		}
	
	public RemotePrintService[] getPrintServices() {
		init();
		return printers;
		}
	
	public RemotePrintService[] getPrintServices(javax.print.DocFlavor docFlavor, javax.print.attribute.AttributeSet attributeSet) {
		init();
		return null;
		}
	
	}
