OSDN Git Service

Add notebook specific sorting and alter some NeverNote labels to say NixNote.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / DateAttributeFilterFactory.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 import com.trolltech.qt.core.QDateTime;\r
26 \r
27 public class DateAttributeFilterFactory {\r
28     private DateAttributeFilterFactory () {};\r
29     public enum FilterType {Today,Yesterday,ThisWeek,LastWeek,Month,LastMonth,Year,LastYear};\r
30 \r
31     public static DateAttributeFilter getFilter (FilterType fType, boolean since, boolean created) {\r
32         switch(fType) {\r
33             case Today:\r
34                 return new checkToday(since,created);\r
35             case Yesterday:\r
36                 return new checkYesterday(since,created);\r
37             case ThisWeek:\r
38                 return new checkThisWeek(since,created);\r
39             case LastWeek:\r
40                 return new checkLastWeek(since,created);\r
41             case Month:\r
42                 return new checkMonth(since,created);\r
43             case LastMonth:\r
44                 return new checkLastMonth(since,created);\r
45             case Year:\r
46                 return new checkYear(since,created);\r
47             case LastYear:\r
48                 return new checkLastYear(since,created);\r
49                 }\r
50                 throw new IllegalArgumentException("The filter type " + fType + " is not recognized.");\r
51         }\r
52 }\r
53 \r
54 class checkToday extends DateAttributeFilter {\r
55     public checkToday(boolean since,boolean created) {\r
56         super(since, created);\r
57     }\r
58         // Check if it was within the last day\r
59         @Override\r
60         public boolean attributeCheck(Note n) {\r
61                 QDateTime noteDate, current;\r
62                 noteDate = noteTime(n);\r
63                 current = currentTime();\r
64                 if (checkSince)\r
65                         return noteDate.daysTo(current) == 0;\r
66                 else \r
67                         return noteDate.daysTo(current) > 0;\r
68         }\r
69         @Override\r
70         public String getLabel(){\r
71                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "Today");\r
72         }\r
73 }\r
74 \r
75 class checkYesterday extends DateAttributeFilter {\r
76         public checkYesterday(boolean since,boolean created) {\r
77         super(since,created);\r
78     }\r
79 \r
80         // Check if it was within the last two days\r
81     @Override\r
82         public boolean attributeCheck(Note n) {\r
83         QDateTime noteDate, current;\r
84         noteDate = noteTime(n);\r
85         current = currentTime();\r
86                 if (checkSince) \r
87                         return noteDate.daysTo(current) <= 1;\r
88                 else\r
89                         return noteDate.daysTo(current) > 1;\r
90         }\r
91         @Override\r
92         public String getLabel(){\r
93                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "Yesterday");\r
94         }\r
95 }\r
96 \r
97 class checkThisWeek extends DateAttributeFilter {\r
98     public checkThisWeek(boolean since,boolean created) {\r
99         super(since,created);\r
100     }\r
101 \r
102         // Check if it was within the last two days\r
103     @Override\r
104         public boolean attributeCheck(Note n) {\r
105         QDateTime noteDate, current;\r
106         noteDate = noteTime(n);\r
107         current = currentTime();\r
108 \r
109                 if (checkSince) \r
110                         return noteDate.daysTo(current) <= 7;\r
111                 else\r
112                         return noteDate.daysTo(current) > 7;\r
113         }\r
114         @Override\r
115         public String getLabel(){\r
116                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "This Week");\r
117         }\r
118 }\r
119 \r
120 class checkLastWeek extends DateAttributeFilter {\r
121     public checkLastWeek(boolean since,boolean created) {\r
122         super(since,created);\r
123     }\r
124 \r
125         // Check if it was within the last two weeks\r
126     @Override\r
127         public boolean attributeCheck(Note n) {\r
128         QDateTime noteDate, current;\r
129         noteDate = noteTime(n);\r
130         current = currentTime();\r
131 \r
132                 if (checkSince) \r
133                         return noteDate.daysTo(current) <= 14;\r
134                 else\r
135                         return noteDate.daysTo(current) > 14;\r
136         }\r
137         @Override\r
138         public String getLabel(){\r
139                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "Last Week");\r
140         }\r
141 }\r
142 \r
143 class checkMonth extends DateAttributeFilter {\r
144     public checkMonth(boolean since,boolean created) {\r
145         super(since,created);\r
146     }\r
147 \r
148         // Check if it was within the last month\r
149     @Override\r
150         public boolean attributeCheck(Note n) {\r
151         QDateTime noteDate, current;\r
152         noteDate = noteTime(n);\r
153         current = currentTime();\r
154 \r
155                 if (checkSince) {\r
156                         if (noteDate.date().year() == current.date().year())\r
157                                 return noteDate.date().month() - current.date().month() == 0;\r
158                         else\r
159                                 return false;\r
160                 } else {\r
161                         if (noteDate.date().year() < current.date().year())\r
162                         return true;\r
163                         else\r
164                                 return noteDate.date().month() - current.date().month() != 0;\r
165                 }\r
166         }\r
167         @Override\r
168         public String getLabel(){\r
169                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "This Month");\r
170         }\r
171 }\r
172 \r
173 class checkLastMonth extends DateAttributeFilter {\r
174     public checkLastMonth(boolean since,boolean created) {\r
175         super(since,created);\r
176     }\r
177 \r
178         // Check if it was within the last two months\r
179     @Override\r
180         public boolean attributeCheck(Note n) {\r
181         QDateTime noteDate, current;\r
182         noteDate = noteTime(n);\r
183         current = currentTime();\r
184 \r
185                 int ny = noteDate.date().year();\r
186                 int cy = current.date().year();\r
187                 int nm = noteDate.date().month();\r
188                 int cm = current.date().month();\r
189 \r
190                 if (cy-ny >= 0)  {\r
191                         cm = cm+12*(cy-ny);\r
192                 } else {\r
193                         return false;\r
194                 }\r
195                 if (checkSince) {\r
196                         return cm-nm <=1;\r
197                 } else {\r
198                         return cm-nm > 1;\r
199                 }\r
200         }\r
201         @Override\r
202         public String getLabel(){\r
203                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "Last Month");\r
204         }\r
205 }\r
206 \r
207 class checkYear extends DateAttributeFilter {\r
208         public checkYear(boolean since,boolean created) {\r
209                 super(since,created);\r
210         }\r
211         // Check if it was within this year\r
212     @Override\r
213         public boolean attributeCheck(Note n) {\r
214         QDateTime noteDate, current;\r
215         noteDate = noteTime(n);\r
216         current = currentTime();\r
217 \r
218                 int ny = noteDate.date().year();\r
219                 int cy = current.date().year();\r
220                 if (checkSince)\r
221                         return cy-ny == 0;\r
222                 else\r
223                         return cy-ny > 0;\r
224         }       \r
225 \r
226         @Override\r
227         public String getLabel(){\r
228                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "This Year");\r
229         }\r
230 }\r
231 \r
232 class checkLastYear extends DateAttributeFilter {\r
233     public checkLastYear(boolean since,boolean created) {\r
234         super(since,created);\r
235     }\r
236 \r
237         // Check if it was within the last year\r
238     @Override\r
239         public boolean attributeCheck(Note n) {\r
240         QDateTime noteDate, current;\r
241         noteDate = noteTime(n);\r
242         current = currentTime();\r
243 \r
244                 int ny = noteDate.date().year();\r
245                 int cy = current.date().year();\r
246                 if (checkSince) \r
247                         return cy-ny <=1;\r
248                 else\r
249                         return cy-ny > 1;\r
250         }\r
251 \r
252         @Override\r
253         public String getLabel(){\r
254                 return QCoreApplication.translate("cx.fbn.nevernote.filters.DateAttributeFilter", "Last Year");\r
255         }\r
256 }\r