OSDN Git Service

Correct problem parsing notes where carriage returns did not separate some XML lines...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / DateAttributeFilter.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.filters;\r
21 \r
22 import com.trolltech.qt.core.QCoreApplication;\r
23 import com.trolltech.qt.core.QDateTime;\r
24 \r
25 public abstract class DateAttributeFilter extends AttributeFilter {\r
26         protected boolean checkSince;\r
27         public abstract boolean attributeCheck(QDateTime a, QDateTime b);\r
28 \r
29         public DateAttributeFilter() {\r
30                 super();\r
31         }\r
32         public DateAttributeFilter(String n) {\r
33                 super(n);\r
34         }\r
35         public abstract String getLabel();\r
36         \r
37         public static class checkToday extends DateAttributeFilter {\r
38                 public checkToday(boolean since) {\r
39                     super();\r
40                         checkSince=since; \r
41                 }\r
42                 // Check if it was within the last day\r
43                 @Override\r
44                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
45                         if (checkSince)\r
46                                 return noteDate.daysTo(current) == 0;\r
47                         else \r
48                                 return noteDate.daysTo(current) > 0;\r
49                 }\r
50                 @Override\r
51                 public String getLabel(){\r
52                         return QCoreApplication.translate("DateAttributeFilter", "Today");\r
53                 }\r
54         }\r
55         public static class checkYesterday extends DateAttributeFilter {\r
56                 public checkYesterday(boolean since) {\r
57                         super();\r
58                         checkSince=since; \r
59                 }\r
60                 // Check if it was within the last two days\r
61                 @Override\r
62                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
63                 if (checkSince) \r
64                         return noteDate.daysTo(current) <= 1;\r
65                 else\r
66                         return noteDate.daysTo(current) > 1;\r
67                 }\r
68                 @Override\r
69                 public String getLabel(){\r
70                         return QCoreApplication.translate("DateAttributeFilter", "Yesterday");\r
71                 }\r
72         }\r
73         public static class checkThisWeek extends DateAttributeFilter {\r
74                 public checkThisWeek(boolean since) {\r
75                      super();\r
76                         checkSince=since; \r
77                 }\r
78                 // Check if it was within the last two days\r
79                 @Override\r
80                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
81                         if (checkSince) \r
82                                 return noteDate.daysTo(current) <= 7;\r
83                         else\r
84                                 return noteDate.daysTo(current) > 7;\r
85                 }\r
86                 @Override\r
87                 public String getLabel(){\r
88                         return QCoreApplication.translate("DateAttributeFilter", "This Week");\r
89                 }\r
90         }\r
91         public static class checkLastWeek extends DateAttributeFilter {\r
92                 public checkLastWeek(boolean since) {\r
93                                 super();\r
94                                 checkSince=since; \r
95                 }\r
96                 // Check if it was within the last two weeks\r
97                 @Override\r
98                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
99                         if (checkSince) \r
100                                 return noteDate.daysTo(current) <= 14;\r
101                         else\r
102                                 return noteDate.daysTo(current) > 14;\r
103                 }\r
104                 @Override\r
105                 public String getLabel(){\r
106                         return QCoreApplication.translate("DateAttributeFilter", "Last Week");\r
107                 }\r
108         }\r
109         public static class checkMonth extends DateAttributeFilter {\r
110                 public checkMonth(boolean since) {\r
111                         super();\r
112                         checkSince=since; \r
113                 }\r
114                 // Check if it was within the last month\r
115                 @Override\r
116                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
117                         if (checkSince) {\r
118                                 if (noteDate.date().year() == current.date().year())\r
119                                         return noteDate.date().month() - current.date().month() == 0;\r
120                                 else\r
121                                         return false;\r
122                         } else {\r
123                                 if (noteDate.date().year() < current.date().year())\r
124                                         return true;\r
125                                 else\r
126                                         return noteDate.date().month() - current.date().month() != 0;\r
127                         }\r
128                 }\r
129                 @Override\r
130                 public String getLabel(){\r
131                         return QCoreApplication.translate("DateAttributeFilter", "This Month");\r
132                 }\r
133         }\r
134         public static class checkLastMonth extends DateAttributeFilter {\r
135                 public checkLastMonth(boolean since) {\r
136                         super();\r
137                         checkSince=since; \r
138                 }\r
139                 // Check if it was within the last two months\r
140                 @Override\r
141                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
142                         int ny = noteDate.date().year();\r
143                         int cy = current.date().year();\r
144                         int nm = noteDate.date().month();\r
145                         int cm = current.date().month();\r
146 \r
147                         if (cy-ny >= 0)  {\r
148                                 cm = cm+12*(cy-ny);\r
149                         } else {\r
150                                 return false;\r
151                         }\r
152                         if (checkSince) {\r
153                                 return cm-nm <=1;\r
154                         } else {\r
155                                 return cm-nm > 1;\r
156                         }\r
157                 }\r
158                 @Override\r
159                 public String getLabel(){\r
160                         return QCoreApplication.translate("DateAttributeFilter", "Last Month");\r
161                 }\r
162         }\r
163         public static class checkYear extends DateAttributeFilter {\r
164                 public checkYear(boolean since) {\r
165                         super();\r
166                         checkSince=since; \r
167                 }\r
168                 // Check if it was within this year\r
169                 @Override\r
170                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
171                         int ny = noteDate.date().year();\r
172                         int cy = current.date().year();\r
173                         if (checkSince)\r
174                                 return cy-ny == 0;\r
175                         else\r
176                                 return cy-ny > 0;\r
177                 }       \r
178                 @Override\r
179                 public String getLabel(){\r
180                         return QCoreApplication.translate("DateAttributeFilter", "This Year");\r
181                 }\r
182         }\r
183         public static class checkLastYear extends DateAttributeFilter {\r
184                 public checkLastYear(boolean since) {\r
185                         super();\r
186                         checkSince=since; \r
187                 }\r
188                 // Check if it was within the last year\r
189                 @Override\r
190                 public boolean attributeCheck(QDateTime noteDate, QDateTime current) {\r
191                         int ny = noteDate.date().year();\r
192                         int cy = current.date().year();\r
193                         if (checkSince) \r
194                                 return cy-ny <=1;\r
195                         else\r
196                                 return cy-ny > 1;\r
197                 }\r
198                 @Override\r
199                 public String getLabel(){\r
200                         return QCoreApplication.translate("DateAttributeFilter", "Last Year");\r
201                 }\r
202         }\r
203 }\r