From 9eb9e9c01ace6e004a7db324ed6814e1c407cb6a Mon Sep 17 00:00:00 2001 From: mossroy Date: Sat, 1 Jun 2013 20:45:26 +0200 Subject: [PATCH] Implement scanning of sdcard content, in order to find the archives on it --- www/index.html | 4 ++-- www/js/app.js | 18 +++++++++++++----- www/js/lib/evopedia.js | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/www/index.html b/www/index.html index 19fefe12..93b379cd 100644 --- a/www/index.html +++ b/www/index.html @@ -55,8 +55,8 @@ License:

It's only a proof of concept so far : there are many many ways this could be enhanced (suggestions and patches are welcome : the source code is on github). In particular : diff --git a/www/js/app.js b/www/js/app.js index 8d383ebb..e5848f44 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -49,13 +49,12 @@ define(function(require) { } if (storage != null) { - //var directory = 'evopedia/wikipedia_small_2010-08-14'; - var directory = 'evopedia/wikipedia_fr_2013-02-16'; - localArchive = new evopedia.LocalArchive(); - localArchive.readTitleFile(storage, directory); - localArchive.readDataFiles(storage, directory, 0); + // If DeviceStorage is available, we look for archives in it + var directory = 'evopedia'; + evopedia.LocalArchive.scanForArchives(storage,directory,selectFirstArchive); } else { + // If DeviceStorage is not available, we display the file select components displayFileSelect(); setLocalArchiveFromFileSelect(); } @@ -65,6 +64,15 @@ define(function(require) { var titleName = event.state.titleName; goToArticle(titleName); }; + + /** + * Selects the first archive found on device storage + */ + function selectFirstArchive(archiveDirectories) { + localArchive = new evopedia.LocalArchive(); + localArchive.readTitleFile(storage, archiveDirectories[0]); + localArchive.readDataFiles(storage, archiveDirectories[0], 0); + } /** * Displays the zone to select files from the dump diff --git a/www/js/lib/evopedia.js b/www/js/lib/evopedia.js index c59a1793..4b133cfb 100644 --- a/www/js/lib/evopedia.js +++ b/www/js/lib/evopedia.js @@ -419,6 +419,47 @@ define(function(require) { reader.readAsArrayBuffer(blob); }; + /** + *  Scans the DeviceStorage for archives + * + * @param storage DeviceStorage instance + * @param rootDirectory Directory from which to start scanning + * @param callbackFunction Function to call with the list of directories where archives are found + */ + LocalArchive.scanForArchives = function(storage, rootDirectory, callbackFunction) { + var directories = []; + var cursor = storage.enumerate(); + cursor.onerror = function() { + alert("Error scanning directory " + rootDirectory + " : " + cursor.error); + } + cursor.onsuccess = function() { + if (cursor.result) { + var file = cursor.result; + var fileName = file.name; + + // We look for files "titles.idx" + if (!endsWith(fileName,"titles.idx")) { + cursor.continue(); + return; + } + + // We want to return the directory where titles.idx is stored + var directory = fileName.substring(0, fileName.lastIndexOf('/')); + console.log(directory); + directories.push(directory); + cursor.continue(); + } + else { + callbackFunction(directories); + } + }; + }; + + function endsWith(str, suffix) { + return str.indexOf(suffix, str.length - suffix.length) !== -1; + } + + /** * Title class : defines the title of an article and some methods to manipulate it