OSDN Git Service

Correct java cast exceptions when doing a getUserInformation.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / ContainsAttributeFilterFactory.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009,2010 Randy Baumgarte\r
4  * Copyright 2010 Hiroshi Miura\r
5  * \r
6  * This file may be licensed under the terms of of the\r
7  * GNU General Public License Version 2 (the ``GPL'').\r
8  *\r
9  * Software distributed under the License is distributed\r
10  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
11  * express or implied. See the GPL for the specific language\r
12  * governing rights and limitations.\r
13  *\r
14  * You should have received a copy of the GPL along with this\r
15  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
16  * or write to the Free Software Foundation, Inc.,\r
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
18  *\r
19 */\r
20 \r
21 package cx.fbn.nevernote.filters;\r
22 \r
23 import com.trolltech.qt.core.QCoreApplication;\r
24 import com.evernote.edam.type.Note;\r
25 import cx.fbn.nevernote.filters.ContainsAttributeFilter;\r
26 \r
27 public class ContainsAttributeFilterFactory {\r
28         private ContainsAttributeFilterFactory() {\r
29         }\r
30         public static enum Contains {Images,Audio,Ink,EncryptedText,Todo,UnfinishedTodo,FinishedTodo,Attachment,PDF};\r
31 \r
32         public static ContainsAttributeFilter create(Contains fType){\r
33         switch (fType) {\r
34         case Images:\r
35             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
36                                                    "Images"),"image/");\r
37         case Audio:\r
38             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
39                                                    "Audio"),"audio/");\r
40         case Ink:\r
41             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
42                                                    "Ink"),"application/vnd.evernote.ink");\r
43         case EncryptedText:\r
44             return new ContainsAttributeFilterContent(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
45                                                    "Encrypted Text"), "<en-crypt");\r
46         case Todo:\r
47             return new ContainsAttributeFilterContent(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
48                                                    "ToDo Items"),"<en-todo");\r
49         case UnfinishedTodo:\r
50             return new ContainsAttributeFilterTodo(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
51                                                    "Unfinished to-do items"),false);\r
52         case FinishedTodo:\r
53             return new ContainsAttributeFilterTodo(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
54                                                    "Finished to-do items"), true);\r
55         case Attachment:\r
56             return new ContainsAttributeFilterAttachment(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
57                                                    "Attachment"));\r
58         case PDF:\r
59             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
60                                                    "PDF"),"application/pdf");\r
61                 }\r
62         throw new IllegalArgumentException("The filter type " + fType + " is not recognized.");\r
63 \r
64         }\r
65 }\r
66 \r
67 // Contains filter strategies\r
68 class ContainsAttributeFilterMime extends ContainsAttributeFilter {\r
69         private String _mime;\r
70         public ContainsAttributeFilterMime(String n, String m) {\r
71                 super(n);\r
72                 _mime = m; \r
73         }\r
74         public boolean attributeCheck(Note n) {\r
75         for (int i=0; i<n.getResourcesSize(); i++) {\r
76             if (n.getResources().get(i).getMime().startsWith(_mime))\r
77                     return true;\r
78         }\r
79         return false;\r
80         }\r
81 }\r
82 \r
83 class ContainsAttributeFilterContent extends ContainsAttributeFilter {\r
84         private String _text;\r
85         public ContainsAttributeFilterContent(String n, String text) {\r
86                 super(n);\r
87                 _text = text;\r
88         }\r
89         public boolean attributeCheck(Note n) {\r
90         if (n.getContent().indexOf(_text) > -1)\r
91             return true;\r
92         else\r
93             return false;\r
94     }\r
95 }\r
96 \r
97 class ContainsAttributeFilterTodo extends ContainsAttributeFilter {\r
98         private boolean _checked;\r
99         public ContainsAttributeFilterTodo(String n, boolean checked) {\r
100                 super(n);\r
101                 _checked = checked;\r
102         }\r
103                 public boolean attributeCheck(Note n) {\r
104                         String content = n.getContent();\r
105                         int pos = content.indexOf("<en-todo");\r
106                         for (; pos >=0 ; pos=content.indexOf("<en-todo", pos+1)) {\r
107                                 int endPos = content.indexOf("/>", pos);\r
108                                 String segment = content.substring(pos, endPos);\r
109                                 boolean currentState = false;\r
110                                 if (segment.indexOf("checked=\"true\"") > -1)\r
111                                         currentState = true;\r
112                                 if (currentState == _checked)\r
113                                         return true;\r
114                         }\r
115                         return false;\r
116                 }\r
117 }\r
118 \r
119 \r
120 class ContainsAttributeFilterAttachment extends ContainsAttributeFilter {\r
121                 public ContainsAttributeFilterAttachment(String n) {\r
122                         super(n);\r
123                 }\r
124                 public boolean attributeCheck(Note n) {\r
125                         for (int i=0; i<n.getResourcesSize(); i++) {\r
126                                 if (n.getResources().get(i).getAttributes() != null \r
127                                         && n.getResources().get(i).getAttributes().isAttachment())\r
128                                         return true;\r
129                         }\r
130                         return false;\r
131                 }\r
132         }\r
133 \r