Пример #1
0
 public FileInfo getOPDSRoot() {
   for (int i = 0; i < mRoot.dirCount(); i++) {
     if (mRoot.getDir(i).isOPDSRoot()) return mRoot.getDir(i);
   }
   L.w("OPDS root directory not found!");
   return null;
 }
Пример #2
0
 public FileInfo getRecentDir() {
   for (int i = 0; i < mRoot.dirCount(); i++) {
     if (mRoot.getDir(i).isRecentDir()) return mRoot.getDir(i);
   }
   L.w("Recent books directory not found!");
   return null;
 }
 protected Collection<FileInfo> loadFileInfos(long fileId) {
   final ArrayList<FileInfo> infos = new ArrayList<FileInfo>();
   while (fileId != -1) {
     final Cursor cursor =
         myDatabase.rawQuery(
             "SELECT name,size,parent_id FROM Files WHERE file_id = " + fileId, null);
     if (cursor.moveToNext()) {
       FileInfo info = createFileInfo(fileId, cursor.getString(0), null);
       if (!cursor.isNull(1)) {
         info.FileSize = cursor.getLong(1);
       }
       infos.add(0, info);
       fileId = cursor.isNull(2) ? -1 : cursor.getLong(2);
     } else {
       fileId = -1;
     }
     cursor.close();
   }
   for (int i = 1; i < infos.size(); ++i) {
     final FileInfo oldInfo = infos.get(i);
     final FileInfo newInfo = createFileInfo(oldInfo.Id, oldInfo.Name, infos.get(i - 1));
     newInfo.FileSize = oldInfo.FileSize;
     infos.set(i, newInfo);
   }
   return infos;
 }
  protected Collection<FileInfo> loadFileInfos(ZLFile file) {
    final LinkedList<ZLFile> fileStack = new LinkedList<ZLFile>();
    for (; file != null; file = file.getParent()) {
      fileStack.addFirst(file);
    }

    final ArrayList<FileInfo> infos = new ArrayList<FileInfo>(fileStack.size());
    final String[] parameters = {null};
    FileInfo current = null;
    for (ZLFile f : fileStack) {
      parameters[0] = f.getLongName();
      final Cursor cursor =
          myDatabase.rawQuery(
              (current == null)
                  ? "SELECT file_id,size FROM Files WHERE name = ?"
                  : "SELECT file_id,size FROM Files WHERE parent_id = "
                      + current.Id
                      + " AND name = ?",
              parameters);
      if (cursor.moveToNext()) {
        current = createFileInfo(cursor.getLong(0), parameters[0], current);
        if (!cursor.isNull(1)) {
          current.FileSize = cursor.getLong(1);
        }
        infos.add(current);
        cursor.close();
      } else {
        cursor.close();
        break;
      }
    }

    return infos;
  }
  /**
   * Returns true iff all the files listed in this tracker's dependency list exist and are at the
   * same version as when they were recorded.
   *
   * @return a boolean
   */
  boolean isUpToDate(Properties properties) {
    Iterator e;

    // fixes bug 962
    {
      if (mProperties.size() != properties.size()) {
        mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="my size " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                DependencyTracker.class.getName(),
                "051018-181",
                new Object[] {new Integer(mProperties.size())}));
        mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="new size " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(
                DependencyTracker.class.getName(),
                "051018-189",
                new Object[] {new Integer(properties.size())}));
        return false;
      }

      for (e = mProperties.keySet().iterator(); e.hasNext(); ) {
        String key = (String) e.next();
        String val0 = mProperties.getProperty(key);
        String val1 = properties.getProperty(key);

        // val0 can't be null; properties don't allow that
        if (val1 == null || !val0.equals(val1)) {
          mLogger.debug(
              /* (non-Javadoc)
               * @i18n.test
               * @org-mes="Missing or changed property: " + p[0]
               */
              org.openlaszlo.i18n.LaszloMessages.getMessage(
                  DependencyTracker.class.getName(), "051018-207", new Object[] {val0}));
          return false;
        }
      }
    }

    for (e = mDependencies.iterator(); e.hasNext(); ) {
      FileInfo saved = (FileInfo) e.next();
      FileInfo current = new FileInfo(saved.mPathname);
      if (!saved.isUpToDate(current)) {
        mLogger.debug(saved.mPathname + " has changed");
        mLogger.debug("was " + saved.mLastMod);
        mLogger.debug(" is " + current.mLastMod);
        return false;
      }
    }
    return true;
  }
Пример #6
0
 private FileInfo findRoot(String pathname) {
   String normalized = engine.getPathCorrector().normalizeIfPossible(pathname);
   for (int i = 0; i < mRoot.dirCount(); i++) {
     FileInfo dir = mRoot.getDir(i);
     if (normalized.equals(engine.getPathCorrector().normalizeIfPossible(dir.getPathName())))
       return dir;
   }
   return null;
 }
 /**
  * Add the specified file to the list of file dependencies.
  *
  * @param file a file
  */
 void addFile(File file) {
   mLogger.debug("addFile Path is " + file.getPath());
   FileInfo fi = new FileInfo(file.getPath());
   try {
     fi.mPathname = file.getCanonicalPath();
   } catch (java.io.IOException e) {
     throw new ChainedException(e);
   }
   mDependencies.add(fi);
 }
Пример #8
0
 private boolean addRoot(String pathname, String filename, boolean listIt) {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = pathname;
   dir.filename = filename;
   if (findRoot(pathname) != null) {
     log.w("skipping duplicate root " + pathname);
     return false; // exclude duplicates
   }
   if (listIt) {
     log.i("Checking FS root " + pathname);
     if (!dir.isReadableDirectory()) { // isWritableDirectory
       log.w("Skipping " + pathname + " - it's not a readable directory");
       return false;
     }
     if (!listDirectory(dir)) {
       log.w("Skipping " + pathname + " - listing failed");
       return false;
     }
     log.i("Adding FS root: " + pathname + "  " + filename);
   }
   mRoot.addDir(dir);
   dir.parent = mRoot;
   if (!listIt) {
     dir.isListed = true;
     dir.isScanned = true;
   }
   return true;
 }
Пример #9
0
 /**
  * List directories in subtree, limited by runtime and depth; remove empty branches (w/o books).
  *
  * @param root is directory to start with
  * @param maxDepth is maximum depth
  * @param limitTs is limit for android.os.SystemClock.uptimeMillis()
  * @return true if completed, false if stopped by limit.
  */
 private boolean listSubtree(FileInfo root, int maxDepth, long limitTs) {
   long ts = android.os.SystemClock.uptimeMillis();
   if (ts > limitTs || maxDepth <= 0) return false;
   listDirectory(root);
   for (int i = root.dirCount() - 1; i >= -0; i--) {
     boolean res = listSubtree(root.getDir(i), maxDepth - 1, limitTs);
     if (!res) return false;
   }
   if (mHideEmptyDirs) root.removeEmptyDirs();
   return true;
 }
 protected Collection<FileInfo> loadFileInfos() {
   Cursor cursor = myDatabase.rawQuery("SELECT file_id,name,parent_id,size FROM Files", null);
   HashMap<Long, FileInfo> infosById = new HashMap<Long, FileInfo>();
   while (cursor.moveToNext()) {
     final long id = cursor.getLong(0);
     final FileInfo info =
         createFileInfo(
             id, cursor.getString(1), cursor.isNull(2) ? null : infosById.get(cursor.getLong(2)));
     if (!cursor.isNull(3)) {
       info.FileSize = cursor.getLong(3);
     }
     infosById.put(id, info);
   }
   cursor.close();
   return infosById.values();
 }
Пример #11
0
 /** Returns an InputStream for the image described by this FileInfo. */
 public InputStream createInputStream(FileInfo fi) throws IOException, MalformedURLException {
   InputStream is = null;
   boolean gzip =
       fi.fileName != null && (fi.fileName.endsWith(".gz") || fi.fileName.endsWith(".GZ"));
   if (fi.inputStream != null) is = fi.inputStream;
   else if (fi.url != null && !fi.url.equals("")) is = new URL(fi.url + fi.fileName).openStream();
   else {
     if (fi.directory.length() > 0 && !fi.directory.endsWith(Prefs.separator))
       fi.directory += Prefs.separator;
     File f = new File(fi.directory + fi.fileName);
     if (gzip) fi.compression = FileInfo.COMPRESSION_UNKNOWN;
     if (f == null || f.isDirectory() || !validateFileInfo(f, fi)) is = null;
     else is = new FileInputStream(f);
   }
   if (is != null) {
     if (fi.compression >= FileInfo.LZW) is = new RandomAccessStream(is);
     else if (gzip) is = new GZIPInputStream(is, 50000);
   }
   return is;
 }
Пример #12
0
 public static FileInfo createOnlineLibraryPluginItem(String packageName, String label) {
   final FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   if (packageName.startsWith(FileInfo.ONLINE_CATALOG_PLUGIN_PREFIX)) dir.pathname = packageName;
   else dir.pathname = FileInfo.ONLINE_CATALOG_PLUGIN_PREFIX + packageName;
   dir.filename = label;
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #13
0
  private static FileInfo parseFile(Node parent) throws XmlParserException {
    String label = "";
    String name = "";
    String location = "";
    Vector<Annotation> annotations = null;

    NodeList nodes = parent.getChildNodes();
    for (int nodeid = 0; nodeid < nodes.getLength(); ++nodeid) {
      Node node = nodes.item(nodeid);
      if (node.getNodeType() != Node.ELEMENT_NODE) continue;

      Element element = (Element) node;
      if (element.getTagName().equals("label")) label = element.getTextContent();
      else if (element.getTagName().equals("name")) name = element.getTextContent();
      else if (element.getTagName().equals("location")) location = element.getTextContent();
      else if (element.getTagName().equals("annotations")) annotations = parseAnnotations(node);
    }

    FileInfo file = new FileInfo(label, name, location);
    if (annotations != null) file.addAnnotations(annotations);
    return file;
  }
Пример #14
0
 /**
  * This will update the FileInfo object chain to use the (possibly new) webappPath once the
  * DependencyTracker object has been reconstitutded from ondisk cache.
  */
 void updateWebappPath() {
   String webappPath = LPS.HOME(); // get it from global
   if (webappPath.equals(mWebappPath)) return;
   mLogger.debug(
       /* (non-Javadoc)
        * @i18n.test
        * @org-mes="updating webappPath from: " + p[0]
        */
       org.openlaszlo.i18n.LaszloMessages.getMessage(
           DependencyTracker.class.getName(), "051018-128", new Object[] {mWebappPath}));
   mLogger.debug(
       /* (non-Javadoc)
        * @i18n.test
        * @org-mes="updating webappPath to:   " + p[0]
        */
       org.openlaszlo.i18n.LaszloMessages.getMessage(
           DependencyTracker.class.getName(), "051018-136", new Object[] {webappPath}));
   for (Iterator e = mDependencies.iterator(); e.hasNext(); ) {
     FileInfo saved = (FileInfo) e.next();
     if (saved.mPathname.startsWith(mWebappPath)) {
       mLogger.debug(
           /* (non-Javadoc)
            * @i18n.test
            * @org-mes="updating dependencies from: " + p[0]
            */
           org.openlaszlo.i18n.LaszloMessages.getMessage(
               DependencyTracker.class.getName(), "051018-147", new Object[] {saved.mPathname}));
       saved.mPathname = webappPath + saved.mPathname.substring(mWebappPath.length());
       mLogger.debug(
           /* (non-Javadoc)
            * @i18n.test
            * @org-mes="updating dependencies to  : " + p[0]
            */
           org.openlaszlo.i18n.LaszloMessages.getMessage(
               DependencyTracker.class.getName(), "051018-157", new Object[] {saved.mPathname}));
     }
   }
   mWebappPath = webappPath;
 }
Пример #15
0
 static boolean validateFileInfo(File f, FileInfo fi) {
   long offset = fi.getOffset();
   long length = 0;
   if (fi.width <= 0 || fi.height < 0) {
     error("Width or height <= 0.", fi, offset, length);
     return false;
   }
   if (offset >= 0 && offset < 1000L) return true;
   if (offset < 0L) {
     error("Offset is negative.", fi, offset, length);
     return false;
   }
   if (fi.fileType == FileInfo.BITMAP || fi.compression != FileInfo.COMPRESSION_NONE) return true;
   length = f.length();
   long size = fi.width * fi.height * fi.getBytesPerPixel();
   size = fi.nImages > 1 ? size : size / 4;
   if (fi.height == 1) size = 0; // allows plugins to read info of unknown length at end of file
   if (offset + size > length) {
     error("Offset + image size > file length.", fi, offset, length);
     return false;
   }
   return true;
 }
Пример #16
0
 public FileInfo createAuthorsRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.AUTHORS_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_author);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #17
0
 public FileInfo createOPDSRoot() {
   final FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.OPDS_LIST_TAG;
   dir.filename = mActivity.getString(R.string.mi_book_opds_root);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #18
0
 public FileInfo createRecentRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.RECENT_DIR_TAG;
   dir.filename = mActivity.getString(R.string.dir_recent_books);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #19
0
 public FileInfo createTitleRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.TITLE_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_title);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #20
0
 public FileInfo createSearchRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.SEARCH_SHORTCUT_TAG;
   dir.filename = mActivity.getString(R.string.mi_book_search);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #21
0
 public FileInfo createBooksByStateReadingRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.STATE_READING_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_state_reading);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #22
0
 public FileInfo createSeriesRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.SERIES_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_series);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #23
0
 public FileInfo createBooksByRatingRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.RATING_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_rating);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #24
0
 public FileInfo createBooksByStateFinishedRoot() {
   FileInfo dir = new FileInfo();
   dir.isDirectory = true;
   dir.pathname = FileInfo.STATE_FINISHED_TAG;
   dir.filename = mActivity.getString(R.string.folder_name_books_by_state_finished);
   dir.isListed = true;
   dir.isScanned = true;
   return dir;
 }
Пример #25
0
 /** Opens a stack of images. */
 ImagePlus openStack(ColorModel cm, boolean show) {
   ImageStack stack = new ImageStack(fi.width, fi.height, cm);
   long skip = fi.getOffset();
   Object pixels;
   try {
     ImageReader reader = new ImageReader(fi);
     InputStream is = createInputStream(fi);
     if (is == null) return null;
     IJ.resetEscape();
     for (int i = 1; i <= fi.nImages; i++) {
       if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages);
       if (IJ.escapePressed()) {
         IJ.beep();
         IJ.showProgress(1.0);
         silentMode = false;
         return null;
       }
       pixels = reader.readPixels(is, skip);
       if (pixels == null) break;
       stack.addSlice(null, pixels);
       skip = fi.gapBetweenImages;
       if (!silentMode) IJ.showProgress(i, fi.nImages);
     }
     is.close();
   } catch (Exception e) {
     IJ.log("" + e);
   } catch (OutOfMemoryError e) {
     IJ.outOfMemory(fi.fileName);
     stack.trim();
   }
   if (!silentMode) IJ.showProgress(1.0);
   if (stack.getSize() == 0) return null;
   if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) {
     for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1);
   }
   ImagePlus imp = new ImagePlus(fi.fileName, stack);
   if (fi.info != null) imp.setProperty("Info", fi.info);
   if (show) imp.show();
   imp.setFileInfo(fi);
   setCalibration(imp);
   ImageProcessor ip = imp.getProcessor();
   if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank
   setStackDisplayRange(imp);
   if (!silentMode) IJ.showProgress(1.0);
   silentMode = false;
   return imp;
 }
Пример #26
0
 public Scanner(BaseActivity coolReader, Engine engine) {
   this.engine = engine;
   this.mActivity = coolReader;
   mRoot = new FileInfo();
   mRoot.path = FileInfo.ROOT_DIR_TAG;
   mRoot.filename = "File Manager";
   mRoot.pathname = FileInfo.ROOT_DIR_TAG;
   mRoot.isListed = true;
   mRoot.isScanned = true;
   mRoot.isDirectory = true;
 }
Пример #27
0
 static void error(String msg, FileInfo fi, long offset, long length) {
   IJ.error(
       "FileOpener",
       "FileInfo parameter error. \n"
           + msg
           + "\n \n"
           + "  Width: "
           + fi.width
           + "\n"
           + "  Height: "
           + fi.height
           + "\n"
           + "  Offset: "
           + offset
           + "\n"
           + "  Bytes/pixel: "
           + fi.getBytesPerPixel()
           + "\n"
           + (length > 0 ? "  File length: " + length + "\n" : ""));
 }
 protected void saveFileInfo(FileInfo fileInfo) {
   final long id = fileInfo.Id;
   SQLiteStatement statement;
   if (id == -1) {
     if (myInsertFileInfoStatement == null) {
       myInsertFileInfoStatement =
           myDatabase.compileStatement(
               "INSERT OR IGNORE INTO Files (name,parent_id,size) VALUES (?,?,?)");
     }
     statement = myInsertFileInfoStatement;
   } else {
     if (myUpdateFileInfoStatement == null) {
       myUpdateFileInfoStatement =
           myDatabase.compileStatement(
               "UPDATE Files SET name = ?, parent_id = ?, size = ? WHERE file_id = ?");
     }
     statement = myUpdateFileInfoStatement;
   }
   statement.bindString(1, fileInfo.Name);
   final FileInfo parent = fileInfo.Parent;
   if (parent != null) {
     statement.bindLong(2, parent.Id);
   } else {
     statement.bindNull(2);
   }
   final long size = fileInfo.FileSize;
   if (size != -1) {
     statement.bindLong(3, size);
   } else {
     statement.bindNull(3);
   }
   if (id == -1) {
     fileInfo.Id = statement.executeInsert();
   } else {
     statement.bindLong(4, id);
     statement.execute();
   }
 }
Пример #29
0
  public void initRoots(Map<String, String> fsRoots) {
    Log.d("cr3", "Scanner.initRoots(" + fsRoots + ")");
    mRoot.clear();
    // create recent books dir
    addRoot(FileInfo.RECENT_DIR_TAG, R.string.dir_recent_books, false);

    // create system dirs
    for (Map.Entry<String, String> entry : fsRoots.entrySet())
      addRoot(entry.getKey(), entry.getValue(), true);

    // create OPDS dir
    addOPDSRoot();

    // create search dir
    addSearchRoot();

    // create books by author root
    addAuthorsRoot();
    // create books by series root
    addSeriesRoot();
    // create books by title root
    addTitleRoot();
  }
  public static void processFiles(final List<FileInfo> fileInfos, @Nullable String changelist) {
    FileInfo[] copy = fileInfos.toArray(new FileInfo[fileInfos.size()]);
    MultiValuesMap<HandleType, VirtualFile> handleTypeToFile =
        new MultiValuesMap<HandleType, VirtualFile>();
    for (FileInfo fileInfo : copy) {
      handleTypeToFile.put(fileInfo.getSelectedHandleType(), fileInfo.getFile());
    }

    for (HandleType handleType : handleTypeToFile.keySet()) {
      handleType.processFiles(
          handleTypeToFile.get(handleType),
          changelist == null ? handleType.getDefaultChangelist() : changelist);
    }

    for (FileInfo fileInfo : copy) {
      if (!fileInfo.getFile().exists() || fileInfo.getFile().isWritable()) {
        fileInfos.remove(fileInfo);
      }
    }
  }