]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filters/datesearchfilterwidget.cpp
Unify the search interface for non-indexed and indexed folders
[dolphin.git] / src / search / filters / datesearchfilterwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 * **************************************************************************/
19
20 #include "datesearchfilterwidget.h"
21
22 #define DISABLE_NEPOMUK_LEGACY
23
24 #include <klocale.h>
25 #include <nepomuk/comparisonterm.h>
26 #include <nepomuk/literalterm.h>
27 #include <nepomuk/orterm.h>
28 #include <nepomuk/property.h>
29 #include <nepomuk/query.h>
30 #include "nie.h"
31 #include <QDate>
32 #include <QDateTime>
33 #include <QLabel>
34 #include <QPushButton>
35 #include <QHBoxLayout>
36
37 DateSearchFilterWidget::DateSearchFilterWidget(QWidget* parent) :
38 AbstractSearchFilterWidget(parent),
39 m_dateButtons()
40 {
41 QHBoxLayout* layout = new QHBoxLayout(this);
42 layout->setSpacing(0);
43
44 for (int i = Today; i <= ThisYear; ++i) {
45 QPushButton* button = createButton();
46 switch (i) {
47 case Today: button->setText(i18nc("@action:button", "Today")); break;
48 case Yesterday: button->setText(i18nc("@action:button", "Yesterday")); break;
49 case ThisWeek: button->setText(i18nc("@action:button", "This Week")); break;
50 case ThisMonth: button->setText(i18nc("@action:button", "This Month")); break;
51 case ThisYear: button->setText(i18nc("@action:button", "This Year")); break;
52 default: Q_ASSERT(false);
53 }
54
55 layout->addWidget(button);
56 m_dateButtons.append(button);
57 }
58 layout->addStretch(1);
59 }
60
61 DateSearchFilterWidget::~DateSearchFilterWidget()
62 {
63 }
64
65
66 QString DateSearchFilterWidget::filterLabel() const
67 {
68 return i18nc("@title:group", "Date");
69 }
70
71 Nepomuk::Query::Term DateSearchFilterWidget::queryTerm() const
72 {
73 Nepomuk::Query::OrTerm orTerm;
74
75 int index = 0;
76 foreach (const QPushButton* button, m_dateButtons) {
77 if (button->isChecked()) {
78 QDate today = QDate::currentDate();
79 QDate date;
80 switch (index) {
81 case Today:
82 // Current date is already set
83 break;
84 case Yesterday:
85 date.addDays(-1);
86 break;
87 case ThisWeek:
88 date.addDays(-today.dayOfWeek());
89 break;
90 case ThisMonth:
91 date = QDate(today.year(), today.month(), 1);
92 break;
93 case ThisYear:
94 date = QDate(today.year(), 1, 1);
95 break;
96 default:
97 Q_ASSERT(false);
98 }
99
100 const QDateTime dateTime(date);
101 const Nepomuk::Query::LiteralTerm term(dateTime);
102 const Nepomuk::Query::ComparisonTerm compTerm(Nepomuk::Vocabulary::NIE::lastModified(),
103 term,
104 Nepomuk::Query::ComparisonTerm::GreaterOrEqual);
105 orTerm.addSubTerm(compTerm);
106 }
107 ++index;
108 }
109
110 return orTerm;
111 }
112
113 #include "datesearchfilterwidget.moc"