ios version
[wl-mobile.git] / www / js / filerepo.js
1 /*
2  * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
3  * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4  */
5
6 var FileRepo = new function() {
7         /* API for files repository */
8         var self = this;
9         this.root = null;
10
11         this.init = function(success, error) {
12                 self.initRoot(success);
13         };
14
15         this.initRoot = function(success) {
16                 // fs size is irrelevant, PERSISTENT is futile (on Android, at least)
17                 window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
18                         debug('local fs found: ' + fs.root.fullPath);
19                         self.root = fs.root;
20                         
21                         success && success();
22                 }, function() {
23                         debug('local fs not found');
24                         success && success();
25                 });
26         };
27
28
29         this.withLocalHtml = function(book_id, success, error) {
30                 debug('info:withLocalHtml: id:' + book_id);
31                 View.spinner('Otwieranie treści utworu');
32                 if (!self.root)
33                         error && error('info:withLocalHtml: no local html: no usable filesystem');
34
35                 var url = "file://" + self.root.fullPath + "/html/" + book_id;
36                 debug('info:withLocalHtml: local ajax: ' + url);
37                 var xhr = new XMLHttpRequest();
38                 xhr.open('GET', url, true);
39                 xhr.onload = function() {
40                         debug('info:withLocalHtml: fetched by local ajax: ' + url);
41                         success && success(xhr.responseText);
42                 }
43                 xhr.onerror = error;
44                 xhr.send();
45         };
46
47
48         this.withLocal = function(win, fail) {
49                 if (self.root) {
50                         self.root.getDirectory('html', {create:true}, function(dir) {
51                                 var reader = dir.createReader();
52                                 reader.readEntries(win, fail);
53                         });
54                 }
55                 else {
56                         win && win([]);
57                 }
58         }
59
60
61         // downloads HTML file from server, saves it in cache and calls success with file contents
62         this.withHtmlFromServer = function(book_id, success, error) {
63                 debug('info:withHtmlFromServer: id:' + book_id);
64                 // read file from WL
65                 Catalogue.withBook(book_id, function(book) {
66                         var url = WL + book.html_file;
67                         debug('info:withHtmlFromServer: fetching url: ' + url);
68
69                         View.spinner("Pobieranie treści utworu z sieci");
70
71                         var xhr = new XMLHttpRequest();
72                         xhr.open("GET", url);
73                         xhr.onload = function() {                       
74                                 data = xhr.responseText;
75                                 if(xhr.status != 200) {
76                                                    alert(xhr.status);
77                                         error && error('Błąd podczas pobierania pliku');
78                                 } else {
79                                   if (self.root) {
80                                         self.root.getDirectory('html', {create: true}, function(dir) {
81                                                         var reader = dir.getFile(''+book_id, {create: true}, function(f) {
82                                                                                                          f.createWriter(function(w) {
83                                                                                                                                         w.write(data);
84                                                                                                                                         self.db.transaction(function(tx) {
85                                                                                                                                                                                 alert('setlocal0');
86                                                                                                                                                                                 tx.executeSql("UPDATE book SET _local=1 WHERE id="+book_id);
87                                                                                                                                                                                 alert('setlocal1');
88                                                                                                                                                                                 });
89                                                                                                                                         });
90                                                                                                          });
91                                                                                    });
92                                   }
93                                   success && success(data);
94                                 }
95                         };
96                         xhr.onerror = function(e) {
97                                 debug('Błąd podczas pobierania pliku')
98                                 error && error("error: " + data);
99                         };
100                         xhr.send();
101                 });             
102         };
103         
104         // calls the callback with contents of the HTML file for a given book,
105         // loaded from the server and cached locally
106         this.withHtml = function(id, success, error) {
107                 debug('info:withHtml: id:' + id);
108                 self.withLocalHtml(id, success, function() {
109                         self.withHtmlFromServer(id, success, error);
110                 });
111         };
112
113
114         this.clear = function() {
115                 FileRepo.withLocal(function(local) {
116                         for (i in local) {
117                                 local[i].remove();
118                         }
119                 });
120         };
121
122
123         this.deleteIfExists = function(id) {
124                 if (self.root) {
125                         self.root.getFile('html/' + id, {create: false}, function(f) {
126                                 f.remove();
127                         });
128                 }
129         }
130 };