1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is Fireinput Inc.
15 *
16 * Portions created by the Initial Developer are Copyright (C) 2007
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 * Olly Ja <ollyja@gmail.com>
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK *****
35 */
36
37 var FireinputDocSaver =
38 {
39 docData: '',
40
41 getFile: function(path)
42 {
43 var ios = FireinputXPC.getIOService();
44 var fileHandler = ios.getProtocolHandler("file")
45 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
46 return fileHandler.getFileFromURLSpec(path);
47 },
48
49 initAutoSave: function()
50 {
51 var path = FireinputUtils.getAppRootPath() + "/userdocument.fireinput";
52 return this.getFile(path);
53 },
54
55 loadFileDone: function(cb)
56 {
57 this.loadFileDoneCallback(this.docData);
58 },
59
60 loadFileLine: function(l)
61 {
62 this.docData += l;
63 },
64
65 read: function(file, cb)
66 {
67 this.docData = '';
68 var options = {
69 caller: this,
70 oncomplete: this.loadFileDone,
71 onavailable: this.loadFileLine
72 };
73 this.loadFileDoneCallback = cb;
74
75 var datafile = this.getFile('file://' + file);
76 FireinputStream.loadXHTMLDataAsync(datafile, options);
77 },
78
79 open: function(title)
80 {
81 var nsIFilePicker = Components.interfaces.nsIFilePicker;
82 var fileChooser = Components.classes["@mozilla.org/filepicker;1"].
83 createInstance(nsIFilePicker);
84 fileChooser.init(window, title, nsIFilePicker.modeOpen);
85 fileChooser.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText);
86 fileChooser.appendFilter('XHTML files', "*.xhtml");
87 fileChooser.appendFilters(nsIFilePicker.filterAll);
88 fileChooser.defaultExtension = 'html';
89 var ret = fileChooser.show();
90 if(ret == fileChooser.returnOK)
91 {
92 return fileChooser.file.path;
93 }
94
95 return null;
96
97 },
98
99 write : function(file, data, mode)
100 {
101 try
102 {
103 var writefile = this.getFile('file://' + file);
104
105 var fos = Components.classes["@mozilla.org/network/file-output-stream;1"]
106 .createInstance(Components.interfaces.nsIFileOutputStream);
107 if(mode && mode == 'overwrite')
108 fos.init(writefile, 0x02 | 0x08 | 0x20, 0664, 0);
109 else
110 fos.init(writefile, 0x02 | 0x08 | 0x10, 0664, 0);
111
112 //FIXME: here we always save as UTF-8. Are there any other charsets that should be used ?
113 var charset = "UTF-8";
114 data = FireinputUnicode.convertFromUnicode(data);
115 fos.write(data, data.length);
116 fos.close();
117 }
118 catch(e)
119 {
120 alert(e.message);
121 return false;
122 }
123
124 return true;
125 },
126
127 save: function(doc, title, skipPrompt, path)
128 {
129 if (!doc)
130 throw "Must have a document when calling save";
131
132 if(!skipPrompt || !path)
133 {
134 var nsIFilePicker = Components.interfaces.nsIFilePicker;
135 var fileChooser = Components.classes["@mozilla.org/filepicker;1"].
136 createInstance(nsIFilePicker);
137 fileChooser.init(window, title, nsIFilePicker.modeSave);
138 fileChooser.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText);
139 fileChooser.appendFilter('XHTML files', "*.xhtml");
140 fileChooser.defaultExtension = 'html';
141 fileChooser.defaultString = path ? path : '文件名.html' ;
142
143 var ret = fileChooser.show();
144 if (ret != nsIFilePicker.returnCancel)
145 {
146 ret = this.write(fileChooser.file.path, doc, (ret == nsIFilePicker.returnReplace) ? 'overwrite': '');
147 return ret ? fileChooser.file.path : null;
148 }
149 }
150 else
151 {
152 var ret = this.write(path, doc, 'overwrite');
153 return path;
154 }
155
156 return null;
157 },
158
159 savetoserver: function()
160 {
161
162
163 },
164
165 autosave: function(doc, path)
166 {
167 var file = path;
168 if(!file)
169 {
170 file = this.initAutoSave();
171
172 var savetime = new Date();
173 var xmlDoc = new XML();
174 xmlDoc = <autosave><lastsave>{savetime}</lastsave><doc>{doc}</doc></autosave>;
175 this.write(file.path, xmlDoc.toString(), 'overwrite');
176 }
177 else
178 this.write(file.path, doc, 'overwrite');
179
180 return true;
181 },
182
183 autoload: function(cb)
184 {
185 var file = this.initAutoSave();
186
187 if(!file.exists())
188 return;
189
190 var loadDone = function(lines)
191 {
192 var xmlDoc = new XML(lines);
193 return cb(xmlDoc.doc);
194 };
195
196 this.read(file.path, loadDone);
197 }
198
199 };
syntax highlighted by Code2HTML, v. 0.9.1