OSDN Git Service

Alter sync logic to examine new notes as they arrive rather than doing a full reload...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NoteTableModel.java
1 package cx.fbn.nevernote.gui;\r
2 \r
3 import java.text.SimpleDateFormat;\r
4 import java.util.ArrayList;\r
5 import java.util.HashMap;\r
6 import java.util.List;\r
7 \r
8 import com.evernote.edam.type.Note;\r
9 import com.trolltech.qt.core.QDateTime;\r
10 import com.trolltech.qt.core.QModelIndex;\r
11 import com.trolltech.qt.core.Qt;\r
12 import com.trolltech.qt.gui.QAbstractTableModel;\r
13 import com.trolltech.qt.gui.QColor;\r
14 \r
15 import cx.fbn.nevernote.Global;\r
16 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;\r
17 import cx.fbn.nevernote.utilities.ListManager;\r
18 \r
19 public class NoteTableModel extends QAbstractTableModel {\r
20         private final ListManager               listManager;\r
21         private final Object[]                  headers;\r
22         private List<Note>                              noteIndex;\r
23         private List<Note>                              masterNoteIndex;\r
24         private List<String>                    unsynchronizedNotes;\r
25         public HashMap<String,Integer>  titleColors;\r
26         public NoteSortFilterProxyModel proxyModel;             // note sort model\r
27         \r
28         public NoteTableModel(ListManager m) {\r
29                 headers = new Object[Global.noteTableColumnCount];\r
30                 listManager = m;\r
31                 masterNoteIndex = null;\r
32                 unsynchronizedNotes = new ArrayList<String>();\r
33         }\r
34         \r
35         public List<Note> getNoteIndex() {\r
36                 return noteIndex;\r
37         }\r
38         public void setNoteIndex(List<Note> list) {\r
39                 noteIndex = list;\r
40         }\r
41         public List<Note> getMasterNoteIndex() {\r
42                 return masterNoteIndex;\r
43         }\r
44         public void setMasterNoteIndex(List<Note> list) {\r
45                 masterNoteIndex = list;\r
46         }\r
47         public void setSortProxyModel(NoteSortFilterProxyModel m) {\r
48                 proxyModel = m;\r
49         }\r
50         \r
51         public List<String> getUnsynchronizedNotes() {\r
52                 return unsynchronizedNotes;\r
53         }\r
54         public void setUnsynchronizedNotes(List<String> list) {\r
55                 unsynchronizedNotes = list;\r
56         }\r
57         \r
58         public HashMap<String, Integer> getTitleColors() {\r
59                 return titleColors;\r
60         }\r
61         public void setTitleColors(HashMap<String, Integer> map) {\r
62                 titleColors = map;\r
63         }\r
64         \r
65         @Override\r
66         public int columnCount(QModelIndex arg0) {\r
67                 return Global.noteTableColumnCount;\r
68         }\r
69 \r
70         @Override\r
71         public Object data(QModelIndex index, int role) {\r
72                 if (index == null)\r
73                         return null;\r
74         switch (role) {\r
75         case Qt.ItemDataRole.DisplayRole: {\r
76             return valueAt(index.row(), index.column());\r
77         }\r
78         case Qt.ItemDataRole.BackgroundRole: {\r
79                 String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
80                 QColor backgroundColor = new QColor(QColor.white);\r
81                 if (titleColors != null && titleColors.containsKey(guid)) {\r
82                         int color = titleColors.get(guid);\r
83                         backgroundColor.setRgb(color);\r
84                 }\r
85                 return backgroundColor;\r
86         }\r
87         case Qt.ItemDataRole.ForegroundRole: {\r
88                 String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
89                 QColor backgroundColor = new QColor(QColor.white);\r
90                 QColor foregroundColor = new QColor(QColor.black);\r
91                 if (titleColors != null && titleColors.containsKey(guid)) {\r
92                         int color = titleColors.get(guid);\r
93                         backgroundColor.setRgb(color);\r
94                         if (backgroundColor.rgb() == QColor.black.rgb() || backgroundColor.rgb() == QColor.blue.rgb()) \r
95                                 foregroundColor.setRgb(QColor.white.rgb());\r
96                 } \r
97                 return foregroundColor;\r
98         }\r
99         default:\r
100             break;\r
101         }\r
102                 return null;\r
103         }\r
104 \r
105         @Override\r
106         public int rowCount(QModelIndex arg0) {\r
107                 if (listManager != null && listManager.getMasterNoteIndex() != null) \r
108                         return listManager.getMasterNoteIndex().size();\r
109                 else\r
110                         return 0;\r
111         }\r
112 \r
113         \r
114         private String valueAt(int row, int col) {\r
115                 Note note = listManager.getMasterNoteIndex().get(row);\r
116                 \r
117                 if (col == Global.noteTableGuidPosition)\r
118                         return note.getGuid();\r
119                 if (col == Global.noteTableCreationPosition) \r
120                         return formattedDate(note.getCreated());\r
121                 if (col == Global.noteTableChangedPosition) \r
122                         return formattedDate(note.getUpdated());\r
123                 if (col == Global.noteTableSubjectDatePosition) {\r
124                         if (note.getAttributes().getSubjectDate() > 0)\r
125                                 return formattedDate(note.getAttributes().getSubjectDate());\r
126                         else\r
127                                 return formattedDate(note.getCreated());                                \r
128                 }\r
129                 if (col == Global.noteTableTitlePosition)\r
130                         return note.getTitle();\r
131                 if (col == Global.noteTableAuthorPosition)\r
132                         return note.getAttributes().getAuthor();\r
133                 if (col == Global.noteTableSourceUrlPosition)\r
134                         return note.getAttributes().getSourceURL();\r
135                 if (col == Global.noteTableSynchronizedPosition) {\r
136                         String guid = note.getGuid();\r
137                         for (int i=0; i<unsynchronizedNotes.size(); i++) {\r
138                                 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid)) \r
139                                         return "false";\r
140                         }\r
141                         return "true";\r
142                 }\r
143                 if (col == Global.noteTableTagPosition) {\r
144                         String tags = new String();\r
145                         for (int i=0; i<note.getTagNamesSize(); i++) {\r
146                                 tags = tags + note.getTagNames().get(i);\r
147                                 if (i!=note.getTagNamesSize()-1)\r
148                                         tags = tags + ", ";\r
149                         }\r
150                         return tags;\r
151                 }\r
152                 if (col == Global.noteTableNotebookPosition) {\r
153                         for (int i=0; i<listManager.getNotebookIndex().size(); i++) {\r
154                                 if (listManager.getNotebookIndex().get(i).getGuid().equals(note.getNotebookGuid()))\r
155                                         return listManager.getNotebookIndex().get(i).getName();\r
156                         }\r
157                 }\r
158                 return "";\r
159         }\r
160         \r
161         private String formattedDate(long d) {\r
162                 String fmt = Global.getDateFormat() + " " + Global.getTimeFormat();\r
163                 String dateTimeFormat = new String(fmt);\r
164                 SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
165                 \r
166                 StringBuilder date = new StringBuilder(simple.format(d));\r
167                 QDateTime created = QDateTime.fromString(date.toString(), fmt);\r
168                 return created.toString(fmt);\r
169 \r
170         }\r
171 \r
172         /*\r
173         @Override\r
174         public void sort(int column, Qt.SortOrder order) {\r
175                 layoutAboutToBeChanged.emit();\r
176                 boolean finished = false;\r
177                 while (!finished) {\r
178                         finished = true;\r
179                         for (int i=1; i<rowCount(); i++) {\r
180                                 String val1 = valueAt(i-1,column-1);\r
181                                 Note n1 = getMasterNoteIndex().get(i-1);\r
182                                 String val2 = valueAt(i,column);\r
183                                 Note n2 = getMasterNoteIndex().get(i);\r
184                                 \r
185                                 if (val1.compareTo(val2) > 0) {\r
186                                         finished = false;\r
187                                         getMasterNoteIndex().set(i-1, n2);\r
188                                         getMasterNoteIndex().set(i, n1);\r
189                                 }\r
190                         }\r
191                 }\r
192                 layoutChanged.emit();\r
193         }\r
194         */\r
195         \r
196         @Override\r
197         public java.lang.Object headerData(int section, Qt.Orientation orientation, int role) {\r
198                 if (role != Qt.ItemDataRole.DisplayRole)\r
199                         return null;\r
200                 \r
201                 if (orientation == Qt.Orientation.Horizontal && section < headers.length) { \r
202                         return headers[section];\r
203                 }\r
204                 return null;\r
205                 \r
206         }\r
207         \r
208         \r
209         @Override\r
210         public boolean setHeaderData(int section, Qt.Orientation orientation, Object value, int role) {\r
211                 if (orientation == Qt.Orientation.Horizontal && section < headers.length && role == Qt.ItemDataRole.DisplayRole) {\r
212                         headers[section] = value;\r
213                         headerDataChanged.emit(orientation, section, section);\r
214                         return true;\r
215                 }\r
216                 return false;\r
217         }\r
218 \r
219         \r
220         // Update a note title\r
221         public void updateNoteTitle(String guid, String title) {\r
222                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
223                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
224                                 getMasterNoteIndex().get(i).setTitle(title);\r
225                                 QModelIndex idx = createIndex(i, Global.noteTableTitlePosition, nativePointer());\r
226                                 setData(idx, title, Qt.ItemDataRole.EditRole); \r
227                                 i = getMasterNoteIndex().size();\r
228                         }       \r
229                 }\r
230                 // Update the list tables \r
231                 for (int i=0; i<getNoteIndex().size(); i++) {\r
232                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
233                                 getNoteIndex().get(i).setTitle(title);\r
234                                 i = getNoteIndex().size();\r
235                         }\r
236                 }\r
237         }\r
238         \r
239         public void updateNoteCreatedDate(String guid, QDateTime date) {\r
240                 \r
241                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
242                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
243                                 getMasterNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);\r
244                                 QModelIndex idx = createIndex(i, Global.noteTableCreationPosition, nativePointer());\r
245                                 setData(idx, new Long(getMasterNoteIndex().get(i).getCreated()), Qt.ItemDataRole.EditRole); \r
246                                 i = getMasterNoteIndex().size();\r
247                         }       \r
248                 }\r
249                 \r
250                 // Update the list tables \r
251                 for (int i=0; i<getNoteIndex().size(); i++) {\r
252                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
253                                 getNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);\r
254                                 i = getNoteIndex().size();\r
255                         }\r
256                 }\r
257         }\r
258         \r
259         public void updateNoteSubjectDate(String guid, QDateTime date) {\r
260                 \r
261                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
262                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
263                                 getMasterNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);\r
264                                 QModelIndex idx = createIndex(i, Global.noteTableSubjectDatePosition, nativePointer());\r
265                                 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole); \r
266                                 i = getMasterNoteIndex().size();\r
267                         }       \r
268                 }\r
269                 \r
270                 // Update the list tables \r
271                 for (int i=0; i<getNoteIndex().size(); i++) {\r
272                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
273                                 getNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);\r
274                                 i = getNoteIndex().size();\r
275                         }\r
276                 }\r
277         }\r
278         \r
279         public void updateNoteChangedDate(String guid, QDateTime date) {\r
280                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
281                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
282                                 getMasterNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);\r
283                                 QModelIndex idx = createIndex(i, Global.noteTableChangedPosition, nativePointer());\r
284                                 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole); \r
285                                 i = getMasterNoteIndex().size();\r
286                         }       \r
287                 }\r
288                 // Update the list tables \r
289                 for (int i=0; i<getNoteIndex().size(); i++) {\r
290                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
291                                 getNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);\r
292                                 i = getNoteIndex().size();\r
293                         }\r
294                 }\r
295         }\r
296 \r
297         public void updateNoteGuid(String oldGuid, String newGuid) {\r
298                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
299                         if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
300                                 getMasterNoteIndex().get(i).setGuid(newGuid);\r
301                                 QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());\r
302                                 setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole); \r
303                                 i=getMasterNoteIndex().size()+1;\r
304                         }\r
305                 }\r
306                 for (int i=0; i<getNoteIndex().size(); i++) {\r
307                         if (getNoteIndex().get(i).getGuid() != null && getNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
308                                 getNoteIndex().get(i).setGuid(newGuid);; \r
309                                 i=getNoteIndex().size()+1;\r
310                         }\r
311                 }\r
312         }\r
313         \r
314         public void updateNoteNotebook(String guid, String notebookGuid) {\r
315                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
316                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
317                                 getMasterNoteIndex().get(i).setNotebookGuid(notebookGuid);\r
318                                 for (int j=0; j<listManager.getNotebookIndex().size(); j++) {\r
319                                         if (listManager.getNotebookIndex().get(j).getGuid().equals(notebookGuid)) {\r
320                                                 String name = listManager.getNotebookIndex().get(j).getName();\r
321                                                 QModelIndex idx = createIndex(i, Global.noteTableNotebookPosition, nativePointer());\r
322                                                 setData(idx, name, Qt.ItemDataRole.EditRole); \r
323                                                 j=listManager.getNotebookIndex().size();\r
324                                         }\r
325                                 }\r
326                                 i=getMasterNoteIndex().size();\r
327                         }\r
328                 }\r
329                 for (int i=0; i<getNoteIndex().size(); i++) {\r
330                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
331                                 getNoteIndex().get(i).setNotebookGuid(notebookGuid);\r
332                                 i=getNoteIndex().size();\r
333                         }\r
334                 }\r
335         }\r
336 \r
337         public void updateNoteAuthor(String guid, String author) {\r
338                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
339                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
340                                 getMasterNoteIndex().get(i).getAttributes().setAuthor(author);\r
341                                 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);\r
342                                 QModelIndex idx = createIndex(i, Global.noteTableAuthorPosition, nativePointer());\r
343                                 setData(idx, author, Qt.ItemDataRole.EditRole); \r
344                                 i = getMasterNoteIndex().size();\r
345                         }       \r
346                 }\r
347                 // Update the list tables \r
348                 for (int i=0; i<getNoteIndex().size(); i++) {\r
349                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
350                                 getNoteIndex().get(i).getAttributes().setAuthor(author);\r
351                                 getNoteIndex().get(i).getAttributes().setAuthorIsSet(true);\r
352                                 i = getNoteIndex().size();\r
353                         }\r
354                 }\r
355         }\r
356         \r
357         public void updateNoteSourceUrl(String guid, String url) {\r
358                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
359                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
360                                 getMasterNoteIndex().get(i).getAttributes().setSourceURL(url);\r
361                                 getMasterNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);\r
362                                 QModelIndex idx = createIndex(i, Global.noteTableSourceUrlPosition, nativePointer());\r
363                                 setData(idx, url, Qt.ItemDataRole.EditRole); \r
364                                 i = getMasterNoteIndex().size();\r
365                         }       \r
366                 }\r
367                 // Update the list tables \r
368                 for (int i=0; i<getNoteIndex().size(); i++) {\r
369                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
370                                 getNoteIndex().get(i).getAttributes().setSourceURL(url);\r
371                                 getNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);\r
372                                 i = getNoteIndex().size();\r
373                         }\r
374                 }\r
375         }\r
376 \r
377         public void updateNoteSyncStatus(String guid, boolean sync) {\r
378                 \r
379                 boolean found = false;\r
380                 for (int i=0; i<unsynchronizedNotes.size(); i++) {                      \r
381                         // If the note is now synchronized, but it is in the unsynchronized list, remove it\r
382                         if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
383                                 unsynchronizedNotes.remove(i);\r
384                                 found = true;\r
385                                 i=unsynchronizedNotes.size();\r
386                         }\r
387                         \r
388                         // If the note is not synchronized, but it is already in the unsynchronized list, do nothing\r
389                         if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
390                                 found = true;\r
391                                 i=unsynchronizedNotes.size();\r
392                         }\r
393                 }\r
394                 \r
395                 // If we've gotten through the entire list, then we consider it synchronized.  If this is \r
396                 // wrong, add it to the list.\r
397                 if (!sync && !found) \r
398                         unsynchronizedNotes.add(guid);\r
399                 \r
400                 // Now we need to go through the table & update it\r
401                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
402                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
403                                 QModelIndex idx = createIndex(i, Global.noteTableSynchronizedPosition, nativePointer());\r
404                                 String value;\r
405                                 if (!sync)\r
406                                         value = tr("false");\r
407                                 else\r
408                                         value = tr("true");\r
409                                 setData(idx, value, Qt.ItemDataRole.EditRole); \r
410                                 return;\r
411                         }       \r
412                 }\r
413         }\r
414         \r
415         public void addNote(Note n) {\r
416                 getNoteIndex().add(n);\r
417                 getMasterNoteIndex().add(n);\r
418                 proxyModel.addGuid(n.getGuid());\r
419                 proxyModel.invalidate();\r
420 //              proxyModel.filter();\r
421         }\r
422         \r
423         \r
424         @Override\r
425         public boolean setData(QModelIndex index, Object value,int role) {\r
426                 if (role == Qt.ItemDataRole.EditRole) {\r
427                         dataChanged.emit(index, index);\r
428                         return true;\r
429                 } else {\r
430                         return super.setData(index, value, role);\r
431                 }\r
432         }\r
433         \r
434         public void updateNoteTitleColor(String guid, Integer color) {\r
435                 getTitleColors().remove(guid);\r
436                 getTitleColors().put(guid, color);\r
437                 layoutChanged.emit();\r
438         }\r
439         \r
440 }\r