OSDN Git Service

Add notebook specific sorting and alter some NeverNote labels to say NixNote.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / ContainsAttributeFilterFactory.java
1 /*\r
2  * This file is part of NixNote \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.evernote.edam.type.Note;\r
24 import com.trolltech.qt.core.QCoreApplication;\r
25 \r
26 public class ContainsAttributeFilterFactory {\r
27         private ContainsAttributeFilterFactory() {\r
28         }\r
29         public static enum Contains {Images,Audio,Ink,EncryptedText,Todo,UnfinishedTodo,FinishedTodo,Attachment,PDF};\r
30 \r
31         public static ContainsAttributeFilter create(Contains fType){\r
32         switch (fType) {\r
33         case Images:\r
34             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
35                                                    "Images"),"image/");\r
36         case Audio:\r
37             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
38                                                    "Audio"),"audio/");\r
39         case Ink:\r
40             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
41                                                    "Ink"),"application/vnd.evernote.ink");\r
42         case EncryptedText:\r
43             return new ContainsAttributeFilterContent(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
44                                                    "Encrypted Text"), "<en-crypt");\r
45         case Todo:\r
46             return new ContainsAttributeFilterContent(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
47                                                    "ToDo Items"),"<en-todo");\r
48         case UnfinishedTodo:\r
49             return new ContainsAttributeFilterTodo(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
50                                                    "Unfinished to-do items"),false);\r
51         case FinishedTodo:\r
52             return new ContainsAttributeFilterTodo(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
53                                                    "Finished to-do items"), true);\r
54         case Attachment:\r
55             return new ContainsAttributeFilterAttachment(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
56                                                    "Attachment"));\r
57         case PDF:\r
58             return new ContainsAttributeFilterMime(QCoreApplication.translate("cx.fbn.nevernote.filters.ContainsAttributeFilter",\r
59                                                    "PDF"),"application/pdf");\r
60                 }\r
61         throw new IllegalArgumentException("The filter type " + fType + " is not recognized.");\r
62 \r
63         }\r
64 }\r
65 \r
66 // Contains filter strategies\r
67 class ContainsAttributeFilterMime extends ContainsAttributeFilter {\r
68         private final String _mime;\r
69         public ContainsAttributeFilterMime(String n, String m) {\r
70                 super(n);\r
71                 _mime = m; \r
72         }\r
73         @Override\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 final String _text;\r
85         public ContainsAttributeFilterContent(String n, String text) {\r
86                 super(n);\r
87                 _text = text;\r
88         }\r
89         @Override\r
90         public boolean attributeCheck(Note n) {\r
91         if (n.getContent().indexOf(_text) > -1)\r
92             return true;\r
93         else\r
94             return false;\r
95     }\r
96 }\r
97 \r
98 class ContainsAttributeFilterTodo extends ContainsAttributeFilter {\r
99         private final boolean _checked;\r
100         public ContainsAttributeFilterTodo(String n, boolean checked) {\r
101                 super(n);\r
102                 _checked = checked;\r
103         }\r
104                 @Override\r
105                 public boolean attributeCheck(Note n) {\r
106                         String content = n.getContent();\r
107                         int pos = content.indexOf("<en-todo");\r
108                         for (; pos >=0 ; pos=content.indexOf("<en-todo", pos+1)) {\r
109                                 int endPos = content.indexOf("/>", pos);\r
110                                 String segment = content.substring(pos, endPos);\r
111                                 boolean currentState = false;\r
112                                 if (segment.indexOf("checked=\"true\"") > -1)\r
113                                         currentState = true;\r
114                                 if (currentState == _checked)\r
115                                         return true;\r
116                         }\r
117                         return false;\r
118                 }\r
119 }\r
120 \r
121 \r
122 class ContainsAttributeFilterAttachment extends ContainsAttributeFilter {\r
123                 public ContainsAttributeFilterAttachment(String n) {\r
124                         super(n);\r
125                 }\r
126                 @Override\r
127                 public boolean attributeCheck(Note n) {\r
128                         for (int i=0; i<n.getResourcesSize(); i++) {\r
129                                 if (n.getResources().get(i).getAttributes() != null \r
130                                         && n.getResources().get(i).getAttributes().isAttachment())\r
131                                         return true;\r
132                         }\r
133                         return false;\r
134                 }\r
135         }\r
136 \r