1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include "kcondition.hxx"
32 #include "kfields.hxx"
33 #include "connectivity/CommonTools.hxx"
34 
35 using namespace ::connectivity::kab;
36 using namespace ::com::sun::star::sdbc;
37 // -----------------------------------------------------------------------------
38 KabCondition::~KabCondition()
39 {
40 }
41 // -----------------------------------------------------------------------------
42 KabConditionConstant::KabConditionConstant(const sal_Bool bValue)
43     : KabCondition(),
44       m_bValue(bValue)
45 {
46 }
47 // -----------------------------------------------------------------------------
48 sal_Bool KabConditionConstant::isAlwaysTrue() const
49 {
50 	return m_bValue;
51 }
52 // -----------------------------------------------------------------------------
53 sal_Bool KabConditionConstant::isAlwaysFalse() const
54 {
55 	return !m_bValue;
56 }
57 // -----------------------------------------------------------------------------
58 sal_Bool KabConditionConstant::eval(const ::KABC::Addressee &) const
59 {
60 	return m_bValue;
61 }
62 // -----------------------------------------------------------------------------
63 KabConditionColumn::KabConditionColumn(const ::rtl::OUString &sColumnName) throw(SQLException)
64 	: KabCondition(),
65 	  m_nFieldNumber(findKabField(sColumnName))
66 {
67 }
68 // -----------------------------------------------------------------------------
69 sal_Bool KabConditionColumn::isAlwaysTrue() const
70 {
71 	// Sometimes true, sometimes false
72 	return sal_False;
73 }
74 // -----------------------------------------------------------------------------
75 sal_Bool KabConditionColumn::isAlwaysFalse() const
76 {
77 	// Sometimes true, sometimes false
78 	return sal_False;
79 }
80 // -----------------------------------------------------------------------------
81 KabConditionNull::KabConditionNull(const ::rtl::OUString &sColumnName) throw(SQLException)
82 	: KabConditionColumn(sColumnName)
83 {
84 }
85 // -----------------------------------------------------------------------------
86 sal_Bool KabConditionNull::eval(const ::KABC::Addressee &aAddressee) const
87 {
88 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
89 
90 	return aQtName.isNull();
91 // KDE address book currently does not use NULL values.
92 // But it might do it someday
93 }
94 // -----------------------------------------------------------------------------
95 KabConditionNotNull::KabConditionNotNull(const ::rtl::OUString &sColumnName) throw(SQLException)
96 	: KabConditionColumn(sColumnName)
97 {
98 }
99 // -----------------------------------------------------------------------------
100 sal_Bool KabConditionNotNull::eval(const ::KABC::Addressee &aAddressee) const
101 {
102 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
103 
104 	return !aQtName.isNull();
105 // KDE address book currently does not use NULL values.
106 // But it might do it someday
107 }
108 // -----------------------------------------------------------------------------
109 KabConditionCompare::KabConditionCompare(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
110     : KabConditionColumn(sColumnName),
111       m_sMatchString(sMatchString)
112 {
113 }
114 // -----------------------------------------------------------------------------
115 KabConditionEqual::KabConditionEqual(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
116 	: KabConditionCompare(sColumnName, sMatchString)
117 {
118 }
119 // -----------------------------------------------------------------------------
120 sal_Bool KabConditionEqual::eval(const ::KABC::Addressee &aAddressee) const
121 {
122 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
123 // Timestamps should not be compared according to their string value
124 // The syntax for such queries should be like
125 //	{ts '2004-03-29 12:55:00.000000'}
126 // They should also support operators like '<' or '>='
127 
128 	if (aQtName.isNull()) return sal_False;
129 
130 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
131 	return sValue == m_sMatchString;
132 }
133 // -----------------------------------------------------------------------------
134 KabConditionDifferent::KabConditionDifferent(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
135 	: KabConditionCompare(sColumnName, sMatchString)
136 {
137 }
138 // -----------------------------------------------------------------------------
139 sal_Bool KabConditionDifferent::eval(const ::KABC::Addressee &aAddressee) const
140 {
141 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
142 
143 	if (aQtName.isNull()) return sal_False;
144 
145 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
146 	return sValue != m_sMatchString;
147 }
148 // -----------------------------------------------------------------------------
149 KabConditionSimilar::KabConditionSimilar(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
150 	: KabConditionCompare(sColumnName, sMatchString)
151 {
152 }
153 // -----------------------------------------------------------------------------
154 sal_Bool KabConditionSimilar::eval(const ::KABC::Addressee &aAddressee) const
155 {
156 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
157 
158 	if (aQtName.isNull()) return sal_False;
159 
160 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
161 	return match(m_sMatchString, sValue, '\0');
162 }
163 // -----------------------------------------------------------------------------
164 KabConditionBoolean::KabConditionBoolean(KabCondition *pLeft, KabCondition *pRight)
165     : KabCondition(),
166       m_pLeft(pLeft),
167       m_pRight(pRight)
168 {
169 }
170 // -----------------------------------------------------------------------------
171 KabConditionBoolean::~KabConditionBoolean()
172 {
173 	delete m_pLeft;
174 	delete m_pRight;
175 }
176 // -----------------------------------------------------------------------------
177 KabConditionOr::KabConditionOr(KabCondition *pLeft, KabCondition *pRight)
178 	: KabConditionBoolean(pLeft, pRight)
179 {
180 }
181 // -----------------------------------------------------------------------------
182 sal_Bool KabConditionOr::isAlwaysTrue() const
183 {
184 	return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
185 }
186 // -----------------------------------------------------------------------------
187 sal_Bool KabConditionOr::isAlwaysFalse() const
188 {
189 	return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
190 }
191 // -----------------------------------------------------------------------------
192 sal_Bool KabConditionOr::eval(const ::KABC::Addressee &aAddressee) const
193 {
194 	// We avoid evaluating terms as much as we can
195 	if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
196 	if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
197 
198 	if (m_pLeft->eval(aAddressee)) return sal_True;
199 	if (m_pRight->eval(aAddressee)) return sal_True;
200 
201 	return sal_False;
202 }
203 // -----------------------------------------------------------------------------
204 KabConditionAnd::KabConditionAnd(KabCondition *pLeft, KabCondition *pRight)
205 	: KabConditionBoolean(pLeft, pRight)
206 {
207 }
208 // -----------------------------------------------------------------------------
209 sal_Bool KabConditionAnd::isAlwaysTrue() const
210 {
211 	return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
212 }
213 // -----------------------------------------------------------------------------
214 sal_Bool KabConditionAnd::isAlwaysFalse() const
215 {
216 	return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
217 }
218 // -----------------------------------------------------------------------------
219 sal_Bool KabConditionAnd::eval(const ::KABC::Addressee &aAddressee) const
220 {
221 	// We avoid evaluating terms as much as we can
222 	if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
223 	if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
224 
225 	if (!m_pLeft->eval(aAddressee)) return sal_False;
226 	if (!m_pRight->eval(aAddressee)) return sal_False;
227 
228 	return sal_True;
229 }
230