OSDN Git Service

Performance improvements when switching notebooks or selecting tags by removing the...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / ContainsAttributeFilterTable.java
1 /*\r
2  * This file is part of NeverNote \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.gui;\r
21 \r
22 import java.util.ArrayList;\r
23 \r
24 import com.evernote.edam.type.Note;\r
25 \r
26 import cx.fbn.nevernote.filters.AttributeFilter;\r
27 import cx.fbn.nevernote.sql.NoteTable;\r
28 \r
29 public class ContainsAttributeFilterTable {\r
30         ArrayList<AttributeFilter> table;\r
31         \r
32         public ContainsAttributeFilterTable() {\r
33                 table = new ArrayList<AttributeFilter>();\r
34                 table.add(new AttributeFilter("Images"));\r
35                 table.add(new AttributeFilter("Audio"));\r
36                 table.add(new AttributeFilter("Ink"));\r
37                 table.add(new AttributeFilter("Encrypted Text"));\r
38                 table.add(new AttributeFilter("To-Do Items"));\r
39                 table.add(new AttributeFilter("Unfinished to-do items"));\r
40                 table.add(new AttributeFilter("Finished to-do items"));\r
41                 table.add(new AttributeFilter("Attachment"));\r
42                 table.add(new AttributeFilter("PDF"));\r
43 \r
44 \r
45 \r
46 \r
47                 \r
48         }\r
49         \r
50         public void reset() {\r
51                 for (int i=0; i<table.size(); i++) \r
52                         table.get(i).set(false);\r
53         }\r
54         \r
55         public void select(String name) {\r
56                 for (int i=0; i<table.size(); i++) \r
57                         if (table.get(i).getName().equalsIgnoreCase(name))\r
58                                 table.get(i).set(true);\r
59         }\r
60         \r
61         public int size() {\r
62                 return table.size();\r
63         }\r
64         \r
65         public boolean hasSelection() {\r
66                 for (int i=0; i<table.size(); i++) {\r
67                         if (table.get(i).isSet() == true)\r
68                                 return true;\r
69                 }\r
70                 return false;\r
71         }\r
72         \r
73         public boolean check(NoteTable sqlTable, Note n) {\r
74                 boolean result = true;\r
75                 \r
76                 for (int i=0; i<table.size(); i++) {\r
77                         if (table.get(i).isSet()) {\r
78                                 n = sqlTable.getNote(n.getGuid(), true, true, false, false, false);\r
79                                 if (table.get(i).getName().equalsIgnoreCase("images"))\r
80                                         result = checkMime(n, "image/");\r
81                                 if (table.get(i).getName().equalsIgnoreCase("audio"))\r
82                                         result = checkMime(n, "audio/");\r
83                                 if (table.get(i).getName().equalsIgnoreCase("ink"))\r
84                                         result = checkMime(n, "application/vnd.evernote.ink");\r
85                                 if (table.get(i).getName().equalsIgnoreCase("Attachment"))\r
86                                         result = checkAttachment(n);\r
87                                 if (table.get(i).getName().equalsIgnoreCase("pdf"))\r
88                                         result = checkMime(n, "application/pdf");\r
89                                 if (table.get(i).getName().equalsIgnoreCase("Encrypted Text"))\r
90                                         result = checkText(n.getContent(), "<en-crypt");\r
91                                 if (table.get(i).getName().equalsIgnoreCase("To-Do Items"))\r
92                                         result = checkText(n.getContent(), "<en-todo");\r
93                                 if (table.get(i).getName().equalsIgnoreCase("Unfinished to-do items"))\r
94                                         result = checkTodo(n.getContent(), false);\r
95                                 if (table.get(i).getName().equalsIgnoreCase("Finished to-do items"))\r
96                                         result = checkTodo(n.getContent(), true);\r
97 \r
98 \r
99                         }\r
100                 }\r
101                 return result;\r
102         }\r
103         \r
104         private boolean checkMime(Note n, String mime) {\r
105                 for (int i=0; i<n.getResourcesSize(); i++) {\r
106                         if (n.getResources().get(i).getMime().startsWith(mime))\r
107                                 return true;\r
108                 }\r
109                 return false;\r
110         }\r
111 \r
112         private boolean checkAttachment(Note n) {\r
113                 for (int i=0; i<n.getResourcesSize(); i++) {\r
114                         if (n.getResources().get(i).getAttributes() != null && n.getResources().get(i).getAttributes().isAttachment())\r
115                                 return true;\r
116                 }\r
117                 return false;\r
118         }\r
119         \r
120         private boolean checkTodo(String content, boolean checked) {\r
121                 int pos = content.indexOf("<en-todo");\r
122                 \r
123                 for (; pos >=0 ; pos=content.indexOf("<en-todo", pos+1)) {\r
124                         int endPos = content.indexOf("/>", pos);\r
125                         String segment = content.substring(pos, endPos);\r
126                         boolean currentState = false;\r
127                         if (segment.indexOf("checked=\"true\"") > -1)\r
128                                         currentState = true;\r
129                         if (currentState == checked)\r
130                                 return true;\r
131                 }\r
132                 \r
133                         \r
134                 return false;\r
135                 \r
136         }\r
137         \r
138         private boolean checkText(String content, String text) {\r
139                 if (content.indexOf(text) > -1)\r
140                         return true;\r
141                 else\r
142                         return false;\r
143         }\r
144         // Get the name of a particular attribute check\r
145         public String getName(int i) {\r
146                 return table.get(i).getName();\r
147         }\r
148 }\r