OSDN Git Service

- Correct issue with importing when not a premium member.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / utilities / FileImporter.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.utilities;\r
21 \r
22 import java.io.File;\r
23 import java.security.MessageDigest;\r
24 import java.security.NoSuchAlgorithmException;\r
25 import java.util.ArrayList;\r
26 import java.util.Calendar;\r
27 import java.util.GregorianCalendar;\r
28 \r
29 import com.evernote.edam.type.Data;\r
30 import com.evernote.edam.type.Note;\r
31 import com.evernote.edam.type.NoteAttributes;\r
32 import com.evernote.edam.type.Resource;\r
33 import com.evernote.edam.type.ResourceAttributes;\r
34 import com.trolltech.qt.core.QByteArray;\r
35 import com.trolltech.qt.core.QFile;\r
36 import com.trolltech.qt.core.QFileInfo;\r
37 import com.trolltech.qt.core.QIODevice;\r
38 import com.trolltech.qt.webkit.QWebPage;\r
39 \r
40 import cx.fbn.nevernote.Global;\r
41 import cx.fbn.nevernote.NeverNote;\r
42 import cx.fbn.nevernote.sql.DatabaseConnection;\r
43 \r
44 \r
45 \r
46 public class FileImporter  {\r
47 \r
48         private final ApplicationLogger logger;\r
49         private String                          fileName;\r
50         private String                          content;\r
51         private QFileInfo                       fileInfo;\r
52         private Note                            newNote;\r
53         private String                          noteString;\r
54         private final DatabaseConnection        conn;\r
55         \r
56         \r
57         public FileImporter(ApplicationLogger l, DatabaseConnection c) {\r
58                 logger = l;\r
59                 conn = c;\r
60         }\r
61         \r
62         \r
63         //********************************************************\r
64         //* Begin to import the file\r
65         //********************************************************\r
66         public boolean importFile() {\r
67                 if (fileInfo.isFile()) {\r
68                         if (fileInfo.completeSuffix().equalsIgnoreCase("txt"))\r
69                                 return importTextFile();\r
70                         return importAttachment();\r
71                 }\r
72                 return false;\r
73         }\r
74         \r
75         //********************************************************\r
76         //* Import text files \r
77         //********************************************************\r
78         public boolean importTextFile() {\r
79                 QFile file = new QFile(fileName);\r
80                 if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly))) {\r
81                         // If we can't get to the file, it is probably locked.  We'll try again later.\r
82                         logger.log(logger.LOW, "Unable to save externally edited file - locked.  Saving for later.");\r
83                         return false;\r
84                 }\r
85                 QByteArray binData = file.readAll();\r
86                 file.close();\r
87                 if (binData.size() == 0) {\r
88                         // If we can't get to the file, it is probably locked.  We'll try again later.\r
89                         logger.log(logger.LOW, "Unable to save externally edited file - zero size.  Saving for later.");\r
90                         return false;\r
91                 }\r
92         \r
93                 String fileData =binData.toString();\r
94                 \r
95                 // Fix \r\n & \n and turn inte HTML\r
96                 QWebPage page = new QWebPage();\r
97                 page.mainFrame().setContent(binData);\r
98                 fileData = page.mainFrame().toHtml();\r
99                 fileData = fileData.replace("\n", "<br>\n");\r
100 \r
101                 noteString = new String("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +\r
102                                 "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n" +\r
103                                 "<en-note>\n<br clear=\"none\" />"+fileData+"</en-note>");\r
104 \r
105                 noteString = fileData;\r
106                 buildEmptyNote();\r
107         newNote.setAttributes(new NoteAttributes());\r
108                         \r
109             return true;\r
110         }\r
111 \r
112         //********************************************************\r
113         //* Import an image\r
114         //********************************************************\r
115         public boolean importAttachment() {\r
116                 QFile file = new QFile(fileName);\r
117                 if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly))) {\r
118                         // If we can't get to the file, it is probably locked.  We'll try again later.\r
119                         logger.log(logger.LOW, "Unable to save externally edited file - locked.  Saving for later.");\r
120                         return false;\r
121                 }\r
122                 QByteArray binData = file.readAll();\r
123                 file.close();\r
124                 if (binData.size() == 0) {\r
125                         // If we can't get to the file, it is probably locked.  We'll try again later.\r
126                         logger.log(logger.LOW, "Unable to save externally edited file - zero size.  Saving for later.");\r
127                         return false;\r
128                 }\r
129                         \r
130                 buildEmptyNote();\r
131         newNote.setAttributes(new NoteAttributes());\r
132     \r
133         // Start building the resource itself\r
134         StringBuffer buffer = new StringBuffer();\r
135         buffer.append("<html><head></head><body>\n");\r
136         \r
137         Resource newRes;\r
138         \r
139         // If we have an image\r
140         if (isImage(fileInfo.completeSuffix())) {\r
141                 String mimeType = "image/" +fileInfo.completeSuffix();\r
142                 if (mimeType.equals("image/jpg"))\r
143                         mimeType = "image/jpeg";\r
144                 newRes = createResource(mimeType, false);\r
145                 \r
146                 buffer.append("<img src=\"" +fileName);\r
147                 buffer.append("\" en-tag=\"en-media\" type=\"" + mimeType +"\""\r
148                                 +" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\""\r
149                                 +" guid=\"" +newRes.getGuid() +"\">");\r
150         } else {\r
151                 // We have an attachment, not an image.\r
152                 \r
153                 \r
154                 String mimeType = "application/" +fileInfo.completeSuffix();\r
155                 newRes = createResource(mimeType, false);\r
156                 newRes.getAttributes().setFileName(fileInfo.fileName());\r
157                 \r
158                 String icon = findIcon(fileInfo.completeSuffix());\r
159                 String imageURL = FileUtils.toFileURLString(Global.getFileManager().getImageDirFile(icon));\r
160                 buffer.append("<a en-tag=\"en-media\" guid=\"" +newRes.getGuid()+"\" ");\r
161                         buffer.append("type=\"" + mimeType + "\" href=\"nnres://" + fileName +"\" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\" >");\r
162                         buffer.append("<img src=\"" + imageURL +"\" title=\"" +newRes.getAttributes().getFileName());\r
163                         buffer.append("\">");\r
164                         buffer.append("</a>");\r
165         }\r
166                 buffer.append("</body></html>");\r
167                 \r
168                 content = buffer.toString();\r
169                 noteString = buffer.toString();\r
170                 newNote.setContent(content);\r
171                 newNote.setResources(new ArrayList<Resource>());\r
172                 newNote.getResources().add(newRes);\r
173                 newNote.setResourcesIsSet(true);\r
174         \r
175         return true;\r
176         }\r
177         \r
178         \r
179         //********************************************************\r
180         //* Check if we found an image\r
181         //********************************************************\r
182         // Check if the account supports this type of attachment\r
183         private boolean isImage(String type) {\r
184                 if (type.equalsIgnoreCase("JPG"))\r
185                         return true;\r
186                 if (type.equalsIgnoreCase("JPEG"))\r
187                         return true;\r
188                 if (type.equalsIgnoreCase("PNG"))\r
189                         return true;\r
190                 if (type.equalsIgnoreCase("GIF"))\r
191                         return true;\r
192                 if (!Global.isPremium()) \r
193                         return false;\r
194                 if (type.equalsIgnoreCase("XPM"))\r
195                         return true;\r
196                 if (type.equalsIgnoreCase("BMP"))\r
197                         return true;\r
198 \r
199                 return false;\r
200         }\r
201 \r
202         //********************************************************\r
203         //* Build an empty note\r
204         //********************************************************\r
205         private void buildEmptyNote() {\r
206                 Calendar currentTime = new GregorianCalendar();\r
207                 String baseNote = new String("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +\r
208                                 "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n" +\r
209                                 "<en-note>\n<br clear=\"none\" /></en-note>");\r
210                 Long l = new Long(currentTime.getTimeInMillis());\r
211                 long prevTime = l;\r
212                 while (prevTime==l) {\r
213                         currentTime = new GregorianCalendar();\r
214                         l=currentTime.getTimeInMillis();\r
215                 }\r
216                 \r
217                 String randint = new String(Long.toString(l));\r
218                 \r
219         newNote = new Note();\r
220         newNote.setUpdateSequenceNum(0);\r
221         newNote.setGuid(randint);\r
222         newNote.setTitle(fileName);\r
223         newNote.setContent(baseNote);\r
224         newNote.setDeleted(0);\r
225         int cdate = fileInfo.created().toTime_t();\r
226         int mdate = fileInfo.lastModified().toTime_t();\r
227         newNote.setCreated((long)cdate*1000);\r
228         newNote.setUpdated((long)mdate*1000);\r
229         newNote.setActive(true);\r
230         NoteAttributes na = new NoteAttributes();\r
231         na.setLatitude(0.0);\r
232         na.setLongitude(0.0);\r
233         na.setAltitude(0.0);\r
234         }\r
235         //********************************************************\r
236         //* Check valid file types\r
237         //********************************************************\r
238         public boolean isValidType() {\r
239                 if (!checkFileAttachmentSize(fileName))\r
240                         return false;\r
241                 return true;\r
242         }\r
243         //**************************************************************\r
244         //* Check the file attachment to be sure it isn't over 25 mb\r
245         //**************************************************************\r
246         private boolean checkFileAttachmentSize(String url) {\r
247                 QFile resourceFile = new QFile(fileName);\r
248                 resourceFile.open(new QIODevice.OpenMode(\r
249                                 QIODevice.OpenModeFlag.ReadOnly));\r
250                 long size = resourceFile.size();\r
251                 resourceFile.close();\r
252                 size = size / 1024 / 1024;\r
253                 if (size < 50 && Global.isPremium())\r
254                         return true;\r
255                 if (size < 25)\r
256                         return true;\r
257                 return false;\r
258         }\r
259 \r
260         //********************************************************\r
261         //* Build a new resource\r
262         //********************************************************\r
263         private Resource createResource(String mime, boolean attachment) {\r
264                 QFile resourceFile;\r
265         resourceFile = new QFile(fileName); \r
266         resourceFile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly));\r
267         byte[] fileData = resourceFile.readAll().toByteArray();\r
268         resourceFile.close();\r
269         if (fileData.length == 0)\r
270                 return null;\r
271         MessageDigest md;\r
272         try {\r
273                 md = MessageDigest.getInstance("MD5");\r
274                 md.update(fileData);\r
275                 byte[] hash = md.digest();\r
276   \r
277                 Resource r = new Resource();\r
278                 Calendar time = new GregorianCalendar();\r
279                 Long l = time.getTimeInMillis();\r
280                 long prevTime = l;\r
281                 while (l==prevTime) {\r
282                         time = new GregorianCalendar();\r
283                         l=time.getTimeInMillis();\r
284                 }\r
285                 r.setGuid(new Long(l).toString());\r
286                 r.setNoteGuid(newNote.getGuid());\r
287                 r.setMime(mime);\r
288                 r.setActive(true);\r
289                 r.setUpdateSequenceNum(0);\r
290                 r.setWidth((short) 0);\r
291                 r.setHeight((short) 0);\r
292                 r.setDuration((short) 0);\r
293                                 \r
294                 Data d = new Data();\r
295                 d.setBody(fileData);\r
296                 d.setBodyIsSet(true);\r
297                 d.setBodyHash(hash);\r
298                 d.setBodyHashIsSet(true);\r
299                 r.setData(d);\r
300                 d.setSize(fileData.length);\r
301                 \r
302                 ResourceAttributes a = new ResourceAttributes();\r
303                 a.setAltitude(0);\r
304                 a.setAltitudeIsSet(false);\r
305                 a.setLongitude(0);\r
306                 a.setLongitudeIsSet(false);\r
307                 a.setLatitude(0);\r
308                 a.setLatitudeIsSet(false);\r
309                 a.setCameraMake("");\r
310                 a.setCameraMakeIsSet(false);\r
311                 a.setCameraModel("");\r
312                 a.setCameraModelIsSet(false);\r
313                 a.setAttachment(attachment);\r
314                 a.setAttachmentIsSet(true);\r
315                 a.setClientWillIndex(false);\r
316                 a.setClientWillIndexIsSet(true);\r
317                 a.setRecoType("");\r
318                 a.setRecoTypeIsSet(false);\r
319                 a.setSourceURLIsSet(false);\r
320                 a.setTimestamp(0);\r
321                 a.setTimestampIsSet(false);\r
322                 a.setFileName(fileInfo.fileName());\r
323                 a.setFileNameIsSet(true);\r
324                 r.setAttributes(a);\r
325                 \r
326                 conn.getNoteTable().noteResourceTable.saveNoteResource(r, true);\r
327                 return r;\r
328         } catch (NoSuchAlgorithmException e1) {\r
329                 e1.printStackTrace();\r
330                 }\r
331         return null;\r
332         }\r
333         \r
334         //********************************************************\r
335         //* set file name\r
336         //********************************************************\r
337         public void setFileInfo(QFileInfo f) {\r
338                 fileInfo = f;           \r
339         }\r
340         //********************************************************\r
341         //* Set file name\r
342         //********************************************************\r
343         public void setFileName(String f) {\r
344                 fileName = f;\r
345         }\r
346         //********************************************************\r
347         //* Get file contents\r
348         //********************************************************\r
349         public String getContent() {\r
350                 return content;\r
351         }\r
352         //********************************************************\r
353         //* Return the new note\r
354         //********************************************************\r
355         public Note getNote() {\r
356                 return newNote;\r
357         }\r
358         //********************************************************\r
359         //* Get just the content of the new note\r
360         //********************************************************\r
361         public String getNoteContent() {\r
362                 return noteString;\r
363         }\r
364 \r
365     /**\r
366      * find the appropriate icon for an attachment\r
367      * NFC TODO: duplicate of {@link NeverNote#findIcon(String)}\r
368      */\r
369     private String findIcon(String appl) {\r
370         appl = appl.toLowerCase();\r
371         String relativePath = appl + ".png";\r
372         File f = Global.getFileManager().getImageDirFile(relativePath);\r
373         if (f.exists()) {\r
374             return relativePath;\r
375         }\r
376         return "attachment.png";\r
377     }\r
378 \r
379 }\r