1   package net.sf.jhylafax;
2   
3   import static net.sf.jhylafax.JHylaFAX.i18n;
4   import java.io.File;
5   import java.io.IOException;
6   import java.util.ArrayList;
7   import java.util.List;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.xnap.commons.gui.ErrorDialog;
11  import org.xnap.commons.gui.util.GUIHelper;
12  import org.xnap.commons.util.QuotedStringTokenizer;
13  import org.xnap.commons.util.StringHelper;
14  
15  public class JHylaFAXHelper {
16  
17  	private final static Log logger = LogFactory.getLog(JobHelper.class);
18  	
19  	public static void view(String viewerPath, File[] files) {
20  		for (int i = 0; i < files.length; i++) {
21  			List<String> args = new ArrayList<String>();
22  			QuotedStringTokenizer t = new QuotedStringTokenizer(viewerPath);
23  			boolean filenameAdded = false;
24  			while (t.hasMoreTokens()) {
25  				String token = t.nextToken();
26  				if ("%s".equals(token) || "%f".equals(token) || "$f".equals(token)) {
27  					args.add(files[i].getAbsolutePath());
28  					filenameAdded = true;
29  				}
30  				else {
31  					args.add(token);
32  				}
33  			}
34  			if (!filenameAdded) {
35  				args.add(files[i].getAbsolutePath());
36  			}
37  			if (!execute(args.toArray(new String[0]))) {
38  				return;
39  			}
40  		}
41  	}	
42  	
43  	public static boolean execute(String[] args) {
44  		try {
45  			Runtime.getRuntime().exec(args);
46  			return true;
47  		}
48  		catch (IOException e) {
49  			logger.debug("Error executing viewer: '" + StringHelper.toString(args, " ") + "'", e);
50  			ErrorDialog.showError(JHylaFAX.getInstance(), 
51  					i18n.tr("Could not execute viewer"),
52  					i18n.tr("JHylaFAX Error"),
53  					e);					
54  		}
55  		return false;
56  	}
57  
58  	public static String getViewerPath(String filename) {
59  		// TODO should not hard code queuename here
60  		String viewerPath;
61  		if (filename.startsWith("recvq")) {
62  			viewerPath = Settings.VIEWER_PATH.getValue().trim();
63  		}
64  		else {
65  			viewerPath = Settings.DOC_VIEWER_PATH.getValue().trim();				
66  		}
67  		if (viewerPath.length() == 0) {
68  			JHylaFAX.getInstance().showError(GUIHelper.tt(i18n.tr("Could not open file: Please enter the path of an external viewer in the settings."))); 
69  			return null;
70  		}
71  		return viewerPath;
72  	}
73  	
74  
75  }