1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2009, 2010, 2011 Olivier Bilodeau <olivier@bottomlesspit.org> |
7 | * Copyright 2009, 2010 Benoit Garret <benoit.garret_launchpad@gadz.org> |
8 | * Copyright 2011 Stefan Hammer <j.4@gmx.at> |
9 | * |
10 | * This file is part of Tomdroid. |
11 | * |
12 | * Tomdroid is free software: you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation, either version 3 of the License, or |
15 | * (at your option) any later version. |
16 | * |
17 | * Tomdroid is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * You should have received a copy of the GNU General Public License |
23 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
24 | */ |
25 | package org.tomdroid; |
26 | |
27 | import java.util.regex.Matcher; |
28 | import java.util.regex.Pattern; |
29 | |
30 | import org.tomdroid.ui.Tomdroid; |
31 | import org.tomdroid.util.NoteListCursorAdapter; |
32 | import org.tomdroid.util.XmlUtils; |
33 | |
34 | import android.app.Activity; |
35 | import android.content.ContentResolver; |
36 | import android.content.ContentValues; |
37 | import android.database.Cursor; |
38 | import android.net.Uri; |
39 | import android.util.Log; |
40 | import android.widget.ListAdapter; |
41 | |
42 | public class NoteManager { |
43 | |
44 | public static final String[] FULL_PROJECTION = { Note.ID, Note.TITLE, Note.FILE, Note.NOTE_CONTENT, Note.MODIFIED_DATE }; |
45 | public static final String[] LIST_PROJECTION = { Note.ID, Note.TITLE, Note.MODIFIED_DATE }; |
46 | public static final String[] TITLE_PROJECTION = { Note.TITLE }; |
47 | public static final String[] GUID_PROJECTION = { Note.ID, Note.GUID }; |
48 | public static final String[] ID_PROJECTION = { Note.ID }; |
49 | public static final String[] EMPTY_PROJECTION = {}; |
50 | |
51 | // static properties |
52 | private static final String TAG = "NoteManager"; |
53 | |
54 | // gets a note from the content provider |
55 | public static Note getNote(Activity activity, Uri uri) { |
56 | |
57 | Note note = null; |
58 | |
59 | // can we find a matching note? |
60 | Cursor cursor = activity.managedQuery(uri, FULL_PROJECTION, null, null, null); |
61 | // cursor must not be null and must return more than 0 entry |
62 | if (!(cursor == null || cursor.getCount() == 0)) { |
63 | |
64 | // create the note from the cursor |
65 | cursor.moveToFirst(); |
66 | String noteContent = cursor.getString(cursor.getColumnIndexOrThrow(Note.NOTE_CONTENT)); |
67 | String noteTitle = cursor.getString(cursor.getColumnIndexOrThrow(Note.TITLE)); |
68 | |
69 | note = new Note(); |
70 | note.setTitle(noteTitle); |
71 | note.setXmlContent(stripTitleFromContent(noteContent, noteTitle)); |
72 | } |
73 | |
74 | return note; |
75 | } |
76 | |
77 | // puts a note in the content provider |
78 | public static void putNote(Activity activity, Note note) { |
79 | |
80 | // verify if the note is already in the content provider |
81 | |
82 | // TODO make the query prettier (use querybuilder) |
83 | Uri notes = Tomdroid.CONTENT_URI; |
84 | String[] whereArgs = new String[1]; |
85 | whereArgs[0] = note.getGuid().toString(); |
86 | |
87 | // The note identifier is the guid |
88 | ContentResolver cr = activity.getContentResolver(); |
89 | Cursor managedCursor = cr.query(notes, |
90 | EMPTY_PROJECTION, |
91 | Note.GUID + "= ?", |
92 | whereArgs, |
93 | null); |
94 | activity.startManagingCursor(managedCursor); |
95 | |
96 | // Preparing the values to be either inserted or updated |
97 | // depending on the result of the previous query |
98 | ContentValues values = new ContentValues(); |
99 | values.put(Note.TITLE, note.getTitle()); |
100 | values.put(Note.FILE, note.getFileName()); |
101 | values.put(Note.GUID, note.getGuid().toString()); |
102 | // Notice that we store the date in UTC because sqlite doesn't handle RFC3339 timezone information |
103 | values.put(Note.MODIFIED_DATE, note.getLastChangeDate().format3339(false)); |
104 | values.put(Note.NOTE_CONTENT, note.getXmlContent()); |
105 | values.put(Note.TAGS, note.getTags()); |
106 | |
107 | if (managedCursor.getCount() == 0) { |
108 | |
109 | // This note is not in the database yet we need to insert it |
110 | if (Tomdroid.LOGGING_ENABLED) Log.v(TAG,"A new note has been detected (not yet in db)"); |
111 | |
112 | Uri uri = cr.insert(Tomdroid.CONTENT_URI, values); |
113 | |
114 | if (Tomdroid.LOGGING_ENABLED) Log.v(TAG,"Note inserted in content provider. ID: "+uri+" TITLE:"+note.getTitle()+" GUID:"+note.getGuid()); |
115 | } else { |
116 | |
117 | // Overwrite the previous note if it exists |
118 | cr.update(Tomdroid.CONTENT_URI, values, Note.GUID+" = ?", whereArgs); |
119 | |
120 | if (Tomdroid.LOGGING_ENABLED) Log.v(TAG,"Note updated in content provider. TITLE:"+note.getTitle()+" GUID:"+note.getGuid()); |
121 | } |
122 | } |
123 | |
124 | public static boolean deleteNote(Activity activity, int id) |
125 | { |
126 | Uri uri = Uri.parse(Tomdroid.CONTENT_URI+"/"+id); |
127 | |
128 | ContentResolver cr = activity.getContentResolver(); |
129 | int result = cr.delete(uri, null, null); |
130 | |
131 | if(result > 0) |
132 | return true; |
133 | else |
134 | return false; |
135 | } |
136 | |
137 | public static Cursor getAllNotes(Activity activity, Boolean includeNotebookTemplates) { |
138 | // get a cursor representing all notes from the NoteProvider |
139 | Uri notes = Tomdroid.CONTENT_URI; |
140 | String where = null; |
141 | String orderBy; |
142 | if (!includeNotebookTemplates) { |
143 | where = Note.TAGS + " NOT LIKE '%" + "system:template" + "%'"; |
144 | } |
145 | orderBy = Note.MODIFIED_DATE + " DESC"; |
146 | return activity.managedQuery(notes, LIST_PROJECTION, where, null, orderBy); |
147 | } |
148 | |
149 | |
150 | public static ListAdapter getListAdapter(Activity activity, String querys) { |
151 | |
152 | String where; |
153 | if (querys==null) { |
154 | where=null; |
155 | } else { |
156 | // sql statements to search notes |
157 | String[] query = querys.split(" "); |
158 | where=""; |
159 | int count=0; |
160 | for (String string : query) { |
161 | if (count>0) where = where + " AND "; |
162 | where = where + "("+Note.TITLE+" LIKE '%"+string+"%' OR "+Note.NOTE_CONTENT+" LIKE '%"+string+"%')"; |
163 | count++; |
164 | } |
165 | } |
166 | |
167 | // get a cursor representing all notes from the NoteProvider |
168 | Uri notes = Tomdroid.CONTENT_URI; |
169 | Cursor notesCursor = activity.managedQuery(notes, LIST_PROJECTION, where, null, null); |
170 | |
171 | // set up an adapter binding the TITLE field of the cursor to the list item |
172 | String[] from = new String[] { Note.TITLE }; |
173 | int[] to = new int[] { R.id.note_title }; |
174 | return new NoteListCursorAdapter(activity, R.layout.main_list_item, notesCursor, from, to); |
175 | } |
176 | |
177 | public static ListAdapter getListAdapter(Activity activity) { |
178 | |
179 | return getListAdapter(activity, null); |
180 | } |
181 | |
182 | // gets the titles of the notes present in the db, used in ViewNote.buildLinkifyPattern() |
183 | public static Cursor getTitles(Activity activity) { |
184 | |
185 | // get a cursor containing the notes titles |
186 | return activity.managedQuery(Tomdroid.CONTENT_URI, TITLE_PROJECTION, null, null, null); |
187 | } |
188 | |
189 | // gets the ids of the notes present in the db, used in SyncService.deleteNotes() |
190 | public static Cursor getGuids(Activity activity) { |
191 | |
192 | // get a cursor containing the notes guids |
193 | return activity.managedQuery(Tomdroid.CONTENT_URI, GUID_PROJECTION, null, null, null); |
194 | } |
195 | |
196 | public static int getNoteId(Activity activity, String title) { |
197 | |
198 | int id = 0; |
199 | |
200 | // get the notes ids |
201 | String[] whereArgs = { title }; |
202 | Cursor cursor = activity.managedQuery(Tomdroid.CONTENT_URI, ID_PROJECTION, Note.TITLE+"=?", whereArgs, null); |
203 | |
204 | // cursor must not be null and must return more than 0 entry |
205 | if (!(cursor == null || cursor.getCount() == 0)) { |
206 | |
207 | cursor.moveToFirst(); |
208 | id = cursor.getInt(cursor.getColumnIndexOrThrow(Note.ID)); |
209 | } |
210 | else { |
211 | // TODO send an error to the user |
212 | if (Tomdroid.LOGGING_ENABLED) Log.d(TAG, "Cursor returned null or 0 notes"); |
213 | } |
214 | |
215 | return id; |
216 | } |
217 | |
218 | /** |
219 | * stripTitleFromContent |
220 | * Because of an historic oddity in Tomboy's note format, a note's title is in a <title> tag but is also repeated |
221 | * in the <note-content> tag. This method strips it from <note-content>. |
222 | * @param noteContent |
223 | */ |
224 | private static String stripTitleFromContent(String xmlContent, String title) { |
225 | // get rid of the title that is doubled in the note's content |
226 | // using quote to escape potential regexp chars in pattern |
227 | |
228 | Pattern stripTitle = Pattern.compile("^\\s*"+Pattern.quote(XmlUtils.escape(title))+"\\n\\n"); |
229 | Matcher m = stripTitle.matcher(xmlContent); |
230 | if (m.find()) { |
231 | xmlContent = xmlContent.substring(m.end(), xmlContent.length()); |
232 | if (Tomdroid.LOGGING_ENABLED) Log.d(TAG, "stripped the title from note-content"); |
233 | } |
234 | |
235 | return xmlContent; |
236 | } |
237 | } |