| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2010, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 7 | * |
| 8 | * This file is part of Tomdroid. |
| 9 | * |
| 10 | * Tomdroid is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * Tomdroid is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | package org.tomdroid.util; |
| 24 | |
| 25 | import java.io.File; |
| 26 | import java.io.FileNotFoundException; |
| 27 | import java.io.FileWriter; |
| 28 | import java.io.IOException; |
| 29 | import java.io.PrintWriter; |
| 30 | import java.io.StringWriter; |
| 31 | import java.io.Writer; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.LinkedList; |
| 34 | |
| 35 | import org.tomdroid.Note; |
| 36 | import org.tomdroid.ui.Tomdroid; |
| 37 | |
| 38 | public class ErrorList extends LinkedList<HashMap<String, Object>> { |
| 39 | |
| 40 | // Eclipse wants this, let's grant his wish |
| 41 | private static final long serialVersionUID = 2442181279736146737L; |
| 42 | |
| 43 | private static class Error extends HashMap<String, Object> { |
| 44 | |
| 45 | // Eclipse wants this, let's grant his wish |
| 46 | private static final long serialVersionUID = -8279130686438869537L; |
| 47 | |
| 48 | public Error addError(Exception e ) { |
| 49 | Writer result = new StringWriter(); |
| 50 | PrintWriter printWriter = new PrintWriter(result); |
| 51 | e.printStackTrace(printWriter); |
| 52 | this.put("error", result.toString()); |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | public Error addError(String message) { |
| 57 | this.put("error", message); |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public Error addNote(Note note) { |
| 62 | this.put("label", note.getTitle()); |
| 63 | this.put("filename", new File(note.getFileName()).getName()); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public Error addObject(String key, Object o) { |
| 68 | this.put(key, o); |
| 69 | return this; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public static HashMap<String, Object> createError(Note note, Exception e) { |
| 74 | return new Error() |
| 75 | .addNote(note) |
| 76 | .addError(e); |
| 77 | } |
| 78 | |
| 79 | public static HashMap<String, Object> createError(String label, String filename, Exception e) { |
| 80 | return new Error() |
| 81 | .addError(e) |
| 82 | .addObject("label", label) |
| 83 | .addObject("filename", filename); |
| 84 | } |
| 85 | |
| 86 | public static HashMap<String, Object> createErrorWithContents(Note note, Exception e, String noteContents) { |
| 87 | return new Error() |
| 88 | .addNote(note) |
| 89 | .addError(e) |
| 90 | .addObject("note-content", noteContents); |
| 91 | } |
| 92 | |
| 93 | public static HashMap<String, Object> createErrorWithContents(Note note, String message, String noteContents) { |
| 94 | return new Error() |
| 95 | .addNote(note) |
| 96 | .addError(message) |
| 97 | .addObject("note-content", noteContents); |
| 98 | } |
| 99 | |
| 100 | public static HashMap<String, Object> createErrorWithContents(String label, String filename, Exception e, String noteContents) { |
| 101 | return new Error() |
| 102 | .addObject("label", label) |
| 103 | .addObject("filename", filename) |
| 104 | .addError(e) |
| 105 | .addObject("note-content", noteContents); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Saves the error list in an "errors" directory located in the notes directory. |
| 110 | * Both the exception and the note content are saved. |
| 111 | * @return true if the save was successful, false if it wasn't |
| 112 | */ |
| 113 | public boolean save() { |
| 114 | String path = Tomdroid.NOTES_PATH+"errors/"; |
| 115 | |
| 116 | File fPath = new File(path); |
| 117 | if (!fPath.exists()) { |
| 118 | fPath.mkdirs(); |
| 119 | // Check a second time, if not the most likely cause is the volume doesn't exist |
| 120 | if(!fPath.exists()) return false; |
| 121 | } |
| 122 | |
| 123 | for(int i = 0; i < this.size(); i++) { |
| 124 | HashMap<String, Object> error = this.get(i); |
| 125 | String filename = findFilename(path, (String)error.get("filename"), 0); |
| 126 | |
| 127 | try { |
| 128 | FileWriter fileWriter; |
| 129 | String content = (String)error.get("note-content"); |
| 130 | |
| 131 | if(content != null) { |
| 132 | fileWriter = new FileWriter(path+filename); |
| 133 | fileWriter.write(content); |
| 134 | fileWriter.flush(); |
| 135 | fileWriter.close(); |
| 136 | } |
| 137 | |
| 138 | fileWriter = new FileWriter(path+filename+".exception"); |
| 139 | fileWriter.write((String)error.get("error")); |
| 140 | fileWriter.flush(); |
| 141 | fileWriter.close(); |
| 142 | } catch (FileNotFoundException e) { |
| 143 | // TODO Auto-generated catch block |
| 144 | e.printStackTrace(); |
| 145 | } catch (IOException e) { |
| 146 | // TODO Auto-generated catch block |
| 147 | e.printStackTrace(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Finds a not existing filename to write the error. |
| 156 | * @param path The directory in which to save the error |
| 157 | * @param baseName The base filename of the error |
| 158 | * @param level The number that get appended to the filename |
| 159 | * @return A filename that doesn't exists in the path directory |
| 160 | */ |
| 161 | private String findFilename(String path, String baseName, int level) { |
| 162 | |
| 163 | if(level < 0) level = 0; |
| 164 | |
| 165 | String suffix = ""+(level == 0 ? "" : level); |
| 166 | String filePath = path+baseName+suffix; |
| 167 | File file = new File(filePath); |
| 168 | |
| 169 | return file.exists() ? findFilename(path, baseName, level + 1) : baseName+suffix; |
| 170 | } |
| 171 | } |