001/*
002 *  $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/ReduceJPEG.java,v $
003 *  ReduceJPEG.java - A widget to grab URLs through a proxy.
004 *
005 *  $Revision: 563 $
006 *  $Date: 2012-11-03 19:28:37 -0700 (Sat, 03 Nov 2012) $
007 *  $Locker:  $
008 *
009 *
010 *  Written by Tom Gutwin - WebARTS Design.
011 *  Copyright (C) 2003 WebARTS Design, North Vancouver Canada
012 *  http://www.webarts.bc.ca
013 *
014 *  This program is free software; you can redistribute it and/or modify
015 *  it under the terms of the GNU General Public License as published by
016 *  the Free Software Foundation; either version 2 of the License, or
017 *  (at your option) any later version.
018 *
019 *  This program is distributed in the hope that it will be useful,
020 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
021 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022 *  GNU General Public License for more details.
023 *
024 *  You should have received a copy of the GNU General Public License
025 *  along with this program; if not, write to the Free Software
026 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
027 */
028package ca.bc.webarts.tools;
029
030import java.io.File;
031
032import ca.bc.webarts.widgets.Util;
033import ca.bc.webarts.tools.NativeAppLauncher;
034
035/**
036 * A tool to downsize/grade all jpg files in a dir and save the results in a
037 * subdir. It does the processing by calling the native ImageMagik 'convert'
038 * command.
039 *
040 * <pre>
041 *    Usage:
042 *      java ca.bc.webarts.widgets.PrintPhotoDir [directory]
043 *
044 *      The directory is not required... if NOT spec'd the current dir will be processed.
045 *</pre>
046 *
047 * @author     Tom Gutwin P.Eng
048 **/
049public class PrintPhotoDir
050{
051
052  /** The file extension to search for. **/
053  protected String imageFileExtension_ = "jpg";
054
055  /**
056   * The default empty constructor for this class.  It initsbut does no grabbing.
057   **/
058  public PrintPhotoDir()
059  {
060    this(System.getProperty("user.dir") );
061  }
062
063
064  /**
065   * The main constructor for this class.  It inits and grabs the spec'd URL
066   * into the current directory.
067   *
068   * @param urlStr the string representation of the URL to grab.
069   *
070   **/
071  public PrintPhotoDir(String dirStr)
072  {
073    File tmpFile = new File(dirStr);
074    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
075      printPhotosFromDir(dirStr);
076    else
077      System.out.println("Cannot find/read a directory named "+dirStr);
078  }
079
080
081  /**
082   * The main entry for this app. It takes url strings on the commandline.
083   *
084   * @param args is a dir to do the deed on (NOT REQUIRED - default is current dir).
085   **/
086  public static void main(String [] args)
087  {
088    if (args.length > 0)
089    {
090      PrintPhotoDir instance = new PrintPhotoDir(args[0]);
091    }
092    else
093    {
094      /*PrintPhotoDir instance =
095        new PrintPhotoDir(System.getProperty("user.dir"));*/
096      PrintPhotoDir instance =
097        new PrintPhotoDir(Util.chooseAFilename(null, System.getProperty("user.dir") ));
098
099    }
100    System.exit(0);
101  }
102
103
104  /**
105   * Does the grunt work to print the files in a dir.
106   *
107   * @param fileName is the name of the dir to convert.
108   * @param recurseDir flag to recurse the dir.
109   * @return the conversion apps result code.
110   **/
111  public int printPhotosFromDir(String fileName)
112  {
113    int retVal = 0;
114    File dirToConvert = new File(fileName);
115    if (dirToConvert!=null && dirToConvert.exists())
116    {
117      //System.out.println("Printing Images in dir "+fileName);
118      String[] files = dirToConvert.list();
119      int counter = 0;
120      File tmpFile = null;
121      int tmpRet = 0;
122      boolean done = false;
123      StringBuffer command = new StringBuffer();
124      String [] parms;
125      System.out.println("Printing "+files.length+" photos from dir: "+fileName);
126      //loop through all the files in the dir
127      for (int i = 0; i < files.length && !done; i++)
128      {
129        tmpFile = new File(files[i]);
130        tmpRet = 0;
131          if (tmpFile != null && !tmpFile.isDirectory() &&
132              files[i].trim().toLowerCase().endsWith(this.getFileExtension()))
133          {
134            // copy the content file that has the tokens
135            command = new StringBuffer("cp");
136            parms = new String [2];
137            parms[0] = "/home/tgutwin/docs/pictures/2x2LandLayout/contentWithTokens.xml";
138            parms[1] = "/home/tgutwin/docs/pictures/2x2LandLayout/content.xml";
139            //System.out.println(""+i+" Copying base content file");
140            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
141
142            // S&R Token 1
143            command = new StringBuffer("/home/tgutwin/bin/SandR.sh");
144            parms = new String [3];
145            parms[0] = "PICTURE_FILE_ONE";
146            System.out.println("   ... "+fileName+"/"+files[i]);
147            parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i++];
148            parms[2] = "/home/tgutwin/docs/pictures/2x2LandLayout/content.xml";
149            //System.out.println(""+i+" S&R1");
150            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
151            Util.sleep(1000);
152
153            // S&R Token 2
154            command = new StringBuffer("/home/tgutwin/bin/SandR.sh");
155            parms = new String [3];
156            parms[0] = "PICTURE_FILE_TWO";
157            if (i+1 < files.length)
158            {
159              tmpFile = new File(files[i+1]);
160              if (tmpFile != null & !tmpFile.isDirectory()&&
161                 files[i+1].trim().toLowerCase().endsWith(this.getFileExtension()))
162              {
163                System.out.println(""+i+"   ... "+fileName+"/"+files[i]);
164                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i++];
165              }
166              else
167                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i++];
168            }
169            else
170            {
171              parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i];
172              done = true;
173            }
174            parms[2] = "/home/tgutwin/docs/pictures/2x2LandLayout/content.xml";
175            //System.out.println(""+i+" S&R2");
176            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
177            Util.sleep(1000);
178
179            // S&R Token 3
180            command = new StringBuffer("/home/tgutwin/bin/SandR.sh");
181            parms = new String [3];
182            parms[0] = "PICTURE_FILE_THREE";
183            if (i+1 < files.length)
184            {
185              tmpFile = new File(files[i+1]);
186              if (tmpFile != null & !tmpFile.isDirectory()&&
187                 files[i+1].trim().toLowerCase().endsWith(this.getFileExtension()))
188              {
189                System.out.println("   ... "+fileName+"/"+files[i]);
190                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i++];
191              }
192              else
193                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i++];
194            }
195            else
196            {
197              parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i];
198              done = true;
199            }
200            parms[2] = "/home/tgutwin/docs/pictures/2x2LandLayout/content.xml";
201            //System.out.println(""+i+" S&R3");
202            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
203            Util.sleep(1000);
204
205            // S&R Token 4
206            command = new StringBuffer("/home/tgutwin/bin/SandR.sh");
207            parms = new String [3];
208            parms[0] = "PICTURE_FILE_FOUR";
209            if (i+1 < files.length)
210            {
211              tmpFile = new File(files[i+1]);
212              if (tmpFile != null & !tmpFile.isDirectory()&&
213                 files[i+1].trim().toLowerCase().endsWith(this.getFileExtension()))
214              {
215                System.out.println("   ... "+files[i]);
216                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i];
217              }
218              else
219                parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i];
220            }
221            else
222            {
223              parms[1] = Util.tokenReplace(fileName,"/","\\/")+"\\/"+files[i];
224              done = true;
225            }
226            parms[2] = "/home/tgutwin/docs/pictures/2x2LandLayout/content.xml";
227            //System.out.println(""+i+" S&R4");
228            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
229            Util.sleep(1000);
230
231
232            // zip the /home/tgutwin/docs/pictures/2x2LandLayout.sxd file
233            command = new StringBuffer("/usr/bin/zip");
234            parms = new String [5];
235            parms[0] = "-r";
236            parms[1] = "/home/tgutwin/docs/pictures/2x2LandLayout.sxd";
237            parms[2] = ".";
238            parms[3] = "-i";
239            parms[4] = "*";
240            Util.removeFile(parms[1]);
241            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
242            Util.sleep(1000);
243
244            // execute the print command /home/tgutwin/bin/print2x2Land.sh
245            command = new StringBuffer("/home/tgutwin/docs/pictures/print2x2Land.sh");
246            parms = new String [1];
247            parms[0] = "";
248            retVal = Util.executeNativeApp(command.toString(), parms, null, true);
249            retVal = tmpRet;
250            System.out.println();
251          }
252      }
253    }
254    return retVal;
255
256  }
257
258  public  String getFileExtension() {return this.imageFileExtension_;}
259}
260