1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: SwordBookDriver.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.jsword.book.sword;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URI;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.crosswire.common.util.FileUtil;
31  import org.crosswire.common.util.Logger;
32  import org.crosswire.common.util.NetUtil;
33  import org.crosswire.jsword.JSMsg;
34  import org.crosswire.jsword.JSOtherMsg;
35  import org.crosswire.jsword.book.Book;
36  import org.crosswire.jsword.book.BookDriver;
37  import org.crosswire.jsword.book.BookException;
38  import org.crosswire.jsword.book.Books;
39  import org.crosswire.jsword.book.basic.AbstractBookDriver;
40  import org.crosswire.jsword.index.IndexManager;
41  import org.crosswire.jsword.index.IndexManagerFactory;
42  import org.crosswire.jsword.index.IndexStatus;
43  
44  /**
45   * This represents all of the Sword Books (aka modules).
46   * 
47   * @see gnu.lgpl.License for license details.<br>
48   *      The copyright to this program is held by it's authors.
49   * @author Joe Walker [joe at eireneh dot com]
50   * @author DM Smith [dmsmith555 at yahoo dot com]
51   */
52  public class SwordBookDriver extends AbstractBookDriver {
53      /**
54       * Some basic name initialization
55       */
56      public SwordBookDriver() {
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see org.crosswire.jsword.book.BookDriver#getName()
63       */
64      public String getDriverName() {
65          return "Sword";
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see org.crosswire.jsword.book.BookDriver#getBooks()
72       */
73      public Book[] getBooks() {
74          ConfigEntry.resetStatistics();
75  
76          List<Book> valid = new ArrayList<Book>();
77  
78          File[] dirs = SwordBookPath.getSwordPath();
79          for (int j = 0; j < dirs.length; j++) {
80              getBooks(valid, dirs[j]);
81          }
82  
83          ConfigEntry.dumpStatistics();
84  
85          return valid.toArray(new Book[valid.size()]);
86      }
87  
88      private void getBooks(List<Book> valid, File bookDir) {
89          File mods = new File(bookDir, SwordConstants.DIR_CONF);
90          if (mods.isDirectory()) {
91              String[] bookConfs = SwordBookPath.getBookList(mods);
92  
93              // Loop through the entries in this mods.d directory
94              for (int i = 0; i < bookConfs.length; i++) {
95                  String bookConf = bookConfs[i];
96                  try {
97                      File configfile = new File(mods, bookConf);
98                      String internal = bookConf;
99                      if (internal.endsWith(SwordConstants.EXTENSION_CONF)) {
100                         internal = internal.substring(0, internal.length() - 5);
101                     }
102                     SwordBookMetaData sbmd = new SwordBookMetaData(configfile, internal, NetUtil.getURI(bookDir));
103 
104                     // skip any book that is not supported.
105                     if (!sbmd.isSupported()) {
106                         continue;
107                     }
108 
109                     sbmd.setDriver(this);
110 
111                     // Only take the first "installation" of the Book
112                     Book book = createBook(sbmd);
113                     if (!valid.contains(book)) {
114                         valid.add(book);
115 
116                         IndexManager imanager = IndexManagerFactory.getIndexManager();
117                         if (imanager.isIndexed(book)) {
118                             sbmd.setIndexStatus(IndexStatus.DONE);
119                         } else {
120                             sbmd.setIndexStatus(IndexStatus.UNDONE);
121                         }
122                     }
123                 } catch (IOException e) {
124                     log.warn("Couldn't create SwordBookMetaData", e);
125                 } catch (BookException e) {
126                     log.warn("Couldn't create SwordBookMetaData", e);
127                 }
128             }
129         } else {
130             log.debug("mods.d directory at " + mods + " does not exist");
131         }
132     }
133 
134     /*
135      * (non-Javadoc)
136      * 
137      * @see
138      * org.crosswire.jsword.book.BookDriver#isDeletable(org.crosswire.jsword
139      * .book.BookMetaData)
140      */
141     @Override
142     public boolean isDeletable(Book dead) {
143         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
144         File dlDir = SwordBookPath.getSwordDownloadDir();
145         File confFile = new File(dlDir, sbmd.getConfPath());
146 
147         // We can only uninstall what we download into our download dir.
148         return confFile.exists();
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see
155      * org.crosswire.jsword.book.BookDriver#delete(org.crosswire.jsword.book
156      * .BookMetaData)
157      */
158     @Override
159     public void delete(Book dead) throws BookException {
160         SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData();
161         File dlDir = SwordBookPath.getSwordDownloadDir();
162         File confFile = new File(dlDir, sbmd.getConfPath());
163 
164         // We can only uninstall what we download into our download dir.
165         if (!confFile.exists()) {
166             // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons.
167             // {0} is a placeholder for the file.
168             throw new BookException(JSMsg.gettext("Unable to delete: {0}", confFile));
169         }
170 
171         // Delete the conf
172         List<File> failures = FileUtil.delete(confFile);
173         if (failures.isEmpty()) {
174             URI loc = sbmd.getLocation();
175             if (loc != null) {
176                 File bookDir = new File(loc.getPath());
177                 failures = FileUtil.delete(bookDir);
178                 Books.installed().removeBook(dead);
179             }
180 
181         }
182 
183         // TODO(DM): list all that failed
184         if (!failures.isEmpty()) {
185             // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons.
186             // {0} is a placeholder for the file.
187             throw new BookException(JSMsg.gettext("Unable to delete: {0}", failures.get(0)));
188         }
189     }
190 
191     /**
192      * Get the singleton instance of this driver.
193      * 
194      * @return this driver instance
195      */
196     public static BookDriver instance() {
197         return INSTANCE;
198     }
199 
200     /**
201      * A helper class for the SwordInstaller to tell us that it has copied a new
202      * Book into our install directory
203      * 
204      * @param sbmd
205      *            The SwordBookMetaData object for the new Book
206      * @throws BookException
207      */
208     public static void registerNewBook(SwordBookMetaData sbmd) throws BookException {
209         BookDriver[] drivers = Books.installed().getDriversByClass(SwordBookDriver.class);
210         for (int i = 0; i < drivers.length; i++) {
211             SwordBookDriver sdriver = (SwordBookDriver) drivers[i];
212             Book book = sdriver.createBook(sbmd);
213             Books.installed().addBook(book);
214         }
215     }
216 
217     /**
218      * Create a Book appropriate for the BookMetaData
219      */
220     private Book createBook(SwordBookMetaData sbmd) throws BookException {
221         BookType modtype = sbmd.getBookType();
222         if (modtype == null || modtype.getBookCategory() == null) {
223             // FIXME(DMS): missing parameter
224             throw new BookException(JSOtherMsg.lookupText("Unsupported type: {0} when reading {1}"));
225         }
226 
227         return modtype.createBook(sbmd);
228     }
229 
230     /**
231      * A shared instance of this driver.
232      */
233     private static final BookDriver INSTANCE = new SwordBookDriver();
234 
235     /**
236      * The log stream
237      */
238     private static final Logger log = Logger.getLogger(SwordBookDriver.class);
239 
240 }
241