Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _FMSRCIMP_HXX
30 : : #define _FMSRCIMP_HXX
31 : :
32 : : #include <svx/fmtools.hxx>
33 : : #include "svx/svxdllapi.h"
34 : :
35 : : #include <com/sun/star/awt/XCheckBox.hpp>
36 : : #include <com/sun/star/awt/XListBox.hpp>
37 : : #include <com/sun/star/awt/XTextComponent.hpp>
38 : : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
39 : : #include <com/sun/star/util/XNumberFormatter.hpp>
40 : :
41 : : #include <comphelper/stl_types.hxx>
42 : : #include <cppuhelper/implbase1.hxx>
43 : : #include <osl/mutex.hxx>
44 : : #include <unotools/charclass.hxx>
45 : : #include <unotools/collatorwrapper.hxx>
46 : : #include <osl/thread.hxx>
47 : :
48 : : #include <deque>
49 : :
50 : : // ===================================================================================================
51 : : // = class FmSearchThread
52 : : // ===================================================================================================
53 : :
54 : : class FmSearchEngine;
55 : : class FmSearchThread : public ::osl::Thread
56 : : {
57 : : FmSearchEngine* m_pEngine;
58 : : Link m_aTerminationHdl;
59 : :
60 : : virtual void SAL_CALL run();
61 : : virtual void SAL_CALL onTerminated();
62 : :
63 : : public:
64 : : FmSearchThread(FmSearchEngine* pEngine) : m_pEngine(pEngine) { }
65 : : void setTerminationHandler(Link aHdl) { m_aTerminationHdl = aHdl; }
66 : : };
67 : :
68 : : // ===================================================================================================
69 : : // = struct FmSearchProgress - the owner of SearchEngine receives this structure for status updates
70 : : // = (at the end of the search)
71 : : // ===================================================================================================
72 : :
73 : : struct FmSearchProgress
74 : : {
75 : : enum STATE { STATE_PROGRESS, STATE_PROGRESS_COUNTING, STATE_CANCELED, STATE_SUCCESSFULL, STATE_NOTHINGFOUND, STATE_ERROR };
76 : : // (move to new record; progress during counting of records; cancelled; record found; nothing found;
77 : : // any non-processable error)
78 : : STATE aSearchState;
79 : :
80 : : // current record - always valid (e.g. of interest for continuing search in case of cancellation)
81 : : sal_uInt32 nCurrentRecord;
82 : : // Overflow - only valid in case of STATE_PROGRESS
83 : : sal_Bool bOverflow;
84 : :
85 : : // the position of the search cursor - valid in case of STATE_SUCCESSFULL, STATE_CANCELED and STATE_NOTHING_FOUND
86 : : ::com::sun::star::uno::Any aBookmark;
87 : : // the field, in which the text was found - valid in case of STATE_SUCCESSFULL
88 : : sal_Int32 nFieldIndex;
89 : : };
90 : :
91 : : // ===================================================================================================
92 : : // = class FmRecordCountListener - utility class for FmSearchEngine, listens at a certain cursor and provides
93 : : // = the differences in RecordCount
94 : : // ===================================================================================================
95 : :
96 : : class FmRecordCountListener : public ::cppu::WeakImplHelper1< ::com::sun::star::beans::XPropertyChangeListener>
97 : : {
98 : : // attribute
99 : : Link m_lnkWhoWantsToKnow;
100 : : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > m_xListening;
101 : :
102 : : // attribute access
103 : : public:
104 : : Link SetPropChangeHandler(const Link& lnk);
105 : :
106 : : // methods
107 : : public:
108 : : FmRecordCountListener(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& dbcCursor);
109 : : // the set has to support the sdb::ResultSet service
110 : : virtual ~FmRecordCountListener();
111 : :
112 : : // DECLARE_UNO3_AGG_DEFAULTS(FmPropertyListener, UsrObject);
113 : : // virtual sal_Bool queryInterface(::com::sun::star::uno::Uik aUik, ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rOut);
114 : :
115 : : // ::com::sun::star::lang::XEventListener
116 : : virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw(::com::sun::star::uno::RuntimeException);
117 : :
118 : : // ::com::sun::star::beans::XPropertyChangeListener
119 : : virtual void SAL_CALL propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw(::com::sun::star::uno::RuntimeException);
120 : :
121 : : void DisConnect();
122 : :
123 : : private:
124 : : void NotifyCurrentCount();
125 : :
126 : : };
127 : :
128 : : // ===================================================================================================
129 : : // = class FmSearchEngine - Impl class for FmSearchDialog
130 : : // ===================================================================================================
131 : :
132 : : namespace svxform {
133 : : // We have three possible control types we may search in, determined by the supported interfaces : ::com::sun::star::awt::XTextComponent, ::com::sun::star::awt::XListBox, ::com::sun::star::awt::XCheckBox.
134 : : // While searching we don't want to do this distinction for every control in every round. So we need some helpers.
135 : : class ControlTextWrapper
136 : : {
137 : : // attributes
138 : : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xControl;
139 : : // attribute access
140 : : public:
141 : : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getControl() const{ return m_xControl; }
142 : : public:
143 : : ControlTextWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xControl) { m_xControl = _xControl; }
144 [ # # ]: 0 : virtual ~ControlTextWrapper() { }
145 : :
146 : : virtual ::rtl::OUString getCurrentText() const = 0;
147 : : };
148 : : class SimpleTextWrapper : public ControlTextWrapper
149 : : {
150 : : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextComponent > m_xText;
151 : : public:
152 : : SimpleTextWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextComponent >& _xText);
153 : : virtual ::rtl::OUString getCurrentText() const;
154 : : };
155 : : class ListBoxWrapper : public ControlTextWrapper
156 : : {
157 : : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XListBox > m_xBox;
158 : : public:
159 : : ListBoxWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XListBox >& _xBox);
160 : : virtual ::rtl::OUString getCurrentText() const;
161 : : };
162 : : class CheckBoxWrapper : public ControlTextWrapper
163 : : {
164 : : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XCheckBox > m_xBox;
165 : : public:
166 : : CheckBoxWrapper(const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XCheckBox >& _xBox);
167 : : virtual ::rtl::OUString getCurrentText() const;
168 : : };
169 : : }
170 : :
171 : : enum FMSEARCH_MODE { SM_BRUTE, SM_ALLOWSCHEDULE, SM_USETHREAD };
172 : :
173 : : DECLARE_STL_VECTOR( ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>, InterfaceArray);
174 : :
175 : : class SVX_DLLPUBLIC FmSearchEngine
176 : : {
177 : : friend class FmSearchThread;
178 : :
179 : : enum SEARCH_RESULT { SR_FOUND, SR_NOTFOUND, SR_ERROR, SR_CANCELED };
180 : : enum SEARCHFOR_TYPE { SEARCHFOR_STRING, SEARCHFOR_NULL, SEARCHFOR_NOTNULL };
181 : :
182 : : CursorWrapper m_xSearchCursor;
183 : : std::deque<sal_Int32> m_arrFieldMapping;
184 : : // since the iterator could have more columns, as managed here (in this field listbox),
185 : : // a mapping of this ::com::sun::star::form keys on the indices of the respective columns is kept in the iterator
186 : :
187 : : // the formatter
188 : : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > m_xFormatSupplier;
189 : : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > m_xFormatter;
190 : :
191 : : CharClass m_aCharacterClassficator;
192 : : CollatorWrapper m_aStringCompare;
193 : :
194 : : // the collection of all interesting fields (or their ::com::sun::star::data::XDatabaseVariant interfaces and FormatKeys)
195 : : struct FieldInfo
196 : : {
197 : : ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn > xContents;
198 : : sal_uInt32 nFormatKey;
199 : : sal_Bool bDoubleHandling;
200 : : };
201 : :
202 : : DECLARE_STL_VECTOR(FieldInfo, FieldCollection);
203 : : FieldCollection m_arrUsedFields;
204 : : sal_Int32 m_nCurrentFieldIndex; // the last parameter of RebuildUsedFields, it allows checks in FormatField
205 : :
206 : : DECLARE_STL_VECTOR(svxform::ControlTextWrapper*, ControlTextSuppliers);
207 : : ControlTextSuppliers m_aControlTexts;
208 : :
209 : : sal_Bool m_bUsingTextComponents;
210 : : CursorWrapper m_xOriginalIterator;
211 : : CursorWrapper m_xClonedIterator;
212 : :
213 : : // data for the decision in which field a "Found" is accepted
214 : : ::com::sun::star::uno::Any m_aPreviousLocBookmark; // position of the last finding
215 : : FieldCollectionIterator m_iterPreviousLocField; // field of the last finding
216 : :
217 : : // Kommunikation mit dem Thread, der die eigentliche Suche durchfuehrt
218 : : ::rtl::OUString m_strSearchExpression; // forward direction
219 : : SEARCHFOR_TYPE m_eSearchForType; // ditto
220 : : SEARCH_RESULT m_srResult; // backward direction
221 : :
222 : : // der Link, dem ich Fortschritte und Ergebnisse mitteile
223 : : Link m_aProgressHandler;
224 : : sal_Bool m_bSearchingCurrently : 1; // is an (asynchronous) search running?
225 : : sal_Bool m_bCancelAsynchRequest : 1; // should be cancelled ?
226 : : ::osl::Mutex m_aCancelAsynchAccess; // access to_bCancelAsynchRequest (technically only
227 : : // relevant for m_eMode == SM_USETHREAD)
228 : : FMSEARCH_MODE m_eMode; // current mode
229 : :
230 : : // parameters for the search
231 : : sal_Bool m_bFormatter : 1; // use field formatting
232 : : sal_Bool m_bForward : 1; // direction
233 : : sal_Bool m_bWildcard : 1; // wildcard search
234 : : sal_Bool m_bRegular : 1; // regular expression
235 : : sal_Bool m_bLevenshtein : 1; // Levenshtein search
236 : : sal_Bool m_bTransliteration : 1; // Levenshtein search
237 : :
238 : : sal_Bool m_bLevRelaxed : 1; // parameters for Levenshtein search
239 : : sal_uInt16 m_nLevOther;
240 : : sal_uInt16 m_nLevShorter;
241 : : sal_uInt16 m_nLevLonger;
242 : :
243 : : sal_uInt16 m_nPosition; // if not regular or levenshtein, then one of the MATCHING_... values
244 : :
245 : : sal_Int32 m_nTransliterationFlags;
246 : :
247 : : // -------------
248 : : // member access
249 : : private:
250 : : SVX_DLLPRIVATE sal_Bool CancelRequested(); // provides a through m_aCancelAsynchAccess backed interpretation of m_bCancelAsynchRequest
251 : :
252 : : public:
253 : : void SetCaseSensitive(sal_Bool bSet);
254 : : sal_Bool GetCaseSensitive() const;
255 : :
256 : : void SetFormatterUsing(sal_Bool bSet); // this is somewhat more extensive, so no inline ... here
257 : 0 : sal_Bool GetFormatterUsing() const { return m_bFormatter; }
258 : :
259 : 0 : void SetDirection(sal_Bool bForward) { m_bForward = bForward; }
260 : 0 : sal_Bool GetDirection() const { return m_bForward; }
261 : :
262 : 0 : void SetWildcard(sal_Bool bSet) { m_bWildcard = bSet; }
263 : 0 : sal_Bool GetWildcard() const { return m_bWildcard; }
264 : :
265 : 0 : void SetRegular(sal_Bool bSet) { m_bRegular = bSet; }
266 : 0 : sal_Bool GetRegular() const { return m_bRegular; }
267 : :
268 : 0 : void SetLevenshtein(sal_Bool bSet) { m_bLevenshtein = bSet; }
269 : 0 : sal_Bool GetLevenshtein() const { return m_bLevenshtein; }
270 : :
271 : : void SetIgnoreWidthCJK(sal_Bool bSet);
272 : : sal_Bool GetIgnoreWidthCJK() const;
273 : :
274 : 0 : void SetTransliteration(sal_Bool bSet) { m_bTransliteration = bSet; }
275 : 0 : sal_Bool GetTransliteration() const { return m_bTransliteration; }
276 : :
277 : 0 : void SetLevRelaxed(sal_Bool bSet) { m_bLevRelaxed = bSet; }
278 : 0 : sal_Bool GetLevRelaxed() const { return m_bLevRelaxed; }
279 : 0 : void SetLevOther(sal_uInt16 nHowMuch) { m_nLevOther = nHowMuch; }
280 : 0 : sal_uInt16 GetLevOther() const { return m_nLevOther; }
281 : 0 : void SetLevShorter(sal_uInt16 nHowMuch) { m_nLevShorter = nHowMuch; }
282 : 0 : sal_uInt16 GetLevShorter() const { return m_nLevShorter; }
283 : 0 : void SetLevLonger(sal_uInt16 nHowMuch) { m_nLevLonger = nHowMuch; }
284 : 0 : sal_uInt16 GetLevLonger() const { return m_nLevLonger; }
285 : : // all Lev. values will only be considered in case of m_bLevenshtein==sal_True
286 : :
287 : 0 : void SetTransliterationFlags(sal_Int32 _nFlags) { m_nTransliterationFlags = _nFlags; }
288 : 0 : sal_Int32 GetTransliterationFlags() const { return m_nTransliterationFlags; }
289 : :
290 : 0 : void SetPosition(sal_uInt16 nValue) { m_nPosition = nValue; }
291 : 0 : sal_uInt16 GetPosition() const { return m_nPosition; }
292 : : // position will be ignored in case of m_bWildCard==sal_True
293 : :
294 : 0 : FMSEARCH_MODE GetSearchMode() const { return m_eMode; }
295 : :
296 : : public:
297 : : /** two constructs, both analogical to FmSearchDialog, therefore look this up for explanations ....
298 : : xCursor has to implement ::com::sun::star::data::DatabaseCursor service each time.
299 : : If eMode == SM_USETHREAD, a ProgressHandler should be set, because in this case the result forwarding will be done
300 : : by this handler.
301 : : If eMode != SM_USETHREAD, SearchNext and StarOver won't return, until the search has finished (independently of its
302 : : success), only then the result can be requested. If additionally the ProgressHandler is set, it will be called for
303 : : every record as well as at the end of the search.
304 : : */
305 : : FmSearchEngine(
306 : : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
307 : : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& xCursor,
308 : : const ::rtl::OUString& strVisibleFields,
309 : : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier >& xFormat,
310 : : FMSEARCH_MODE eMode);
311 : : FmSearchEngine(
312 : : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
313 : : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& xCursor,
314 : : const ::rtl::OUString& strVisibleFields,
315 : : const InterfaceArray& arrFields,
316 : : FMSEARCH_MODE eMode);
317 : :
318 : : virtual ~FmSearchEngine();
319 : :
320 : : /** the link will be called on every record and after the completion of the search, the parameter is a pointer to
321 : : a FmSearchProgress structure
322 : : the handler should be in any case thread-safe
323 : : */
324 : 0 : void SetProgressHandler(Link aHdl) { m_aProgressHandler = aHdl; }
325 : :
326 : : /// search for the next appearance (for nDirection values check DIRECTION_*-defines)
327 : : void SearchNext(const ::rtl::OUString& strExpression);
328 : : /// analogous, search for "NULL" (_bSearchForNull==sal_True) or "not NULL"
329 : : void SearchNextSpecial(sal_Bool _bSearchForNull);
330 : : /// search for the next appearance, dependent on nDirection from the start or end
331 : : void StartOver(const ::rtl::OUString& strExpression);
332 : : /// analogous, search for "NULL" (_bSearchForNull==sal_True) or "not NULL"
333 : : void StartOverSpecial(sal_Bool _bSearchForNull);
334 : : /// invalidate previous search reference
335 : : void InvalidatePreviousLoc();
336 : :
337 : : /** rebuilds m_arrUsedFields (nFieldIndex==-1 means all fields, otherwise it specifies the field index)
338 : : if bForce is not set, nothing will happen in case of nFieldIndex == m_nCurrentFieldIndex
339 : : (calls InvalidatePreviousLoc)
340 : : */
341 : : void RebuildUsedFields(sal_Int32 nFieldIndex, sal_Bool bForce = sal_False);
342 : : ::rtl::OUString FormatField(sal_Int32 nWhich);
343 : :
344 : : /// returns directly; once it was really aborted, ProgressHandler is called with STATE_CANCELED
345 : : void CancelSearch();
346 : :
347 : : /** only valid, if not an (asynchronous) search is running, the next search will then be executed
348 : : on top of the new iterator with the new parameter
349 : : */
350 : : sal_Bool SwitchToContext(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& xCursor, const ::rtl::OUString& strVisibleFields, const InterfaceArray& arrFields,
351 : : sal_Int32 nFieldIndex);
352 : :
353 : : protected:
354 : : void Init(const ::rtl::OUString& strVisibleFields);
355 : :
356 : : void SearchNextImpl();
357 : : // this Impl method is running in SearchThread
358 : :
359 : : // start a thread-search (or call SearchNextImpl directly, depending on the search mode)
360 : : void ImplStartNextSearch();
361 : :
362 : : private:
363 : : SVX_DLLPRIVATE void clearControlTexts();
364 : : SVX_DLLPRIVATE void fillControlTexts(const InterfaceArray& arrFields);
365 : :
366 : : // three methods implementing a complete search loop (null/not null, wildcard, SearchText)
367 : : // (they all have some code in common, but with this solution we have do do a distinction only once per search (before
368 : : // starting the loop), not in every loop step
369 : : SVX_DLLPRIVATE SEARCH_RESULT SearchSpecial(sal_Bool _bSearchForNull, sal_Int32& nFieldPos, FieldCollectionIterator& iterFieldLoop,
370 : : const FieldCollectionIterator& iterBegin, const FieldCollectionIterator& iterEnd);
371 : : SVX_DLLPRIVATE SEARCH_RESULT SearchWildcard(const ::rtl::OUString& strExpression, sal_Int32& nFieldPos, FieldCollectionIterator& iterFieldLoop,
372 : : const FieldCollectionIterator& iterBegin, const FieldCollectionIterator& iterEnd);
373 : : SVX_DLLPRIVATE SEARCH_RESULT SearchRegularApprox(const ::rtl::OUString& strExpression, sal_Int32& nFieldPos, FieldCollectionIterator& iterFieldLoop,
374 : : const FieldCollectionIterator& iterBegin, const FieldCollectionIterator& iterEnd);
375 : :
376 : : SVX_DLLPRIVATE void PropagateProgress(sal_Bool _bDontPropagateOverflow);
377 : : // call the ProgressHandler with STATE_PROGRESS and the current position of the search iterator
378 : :
379 : : // helpers, that are needed several times
380 : : SVX_DLLPRIVATE sal_Bool MoveCursor();
381 : : // moves m_xSearchIterator with respect to direction/overflow cursor
382 : : SVX_DLLPRIVATE sal_Bool MoveField(sal_Int32& nPos, FieldCollectionIterator& iter, const FieldCollectionIterator& iterBegin, const FieldCollectionIterator& iterEnd);
383 : : // moves the iterator with respect to the direction/overflow iterator/overflow cursor
384 : : SVX_DLLPRIVATE void BuildAndInsertFieldInfo(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >& xAllFields, sal_Int32 nField);
385 : : // builds a FieldInfo in relation to field number nField (in xAllFields) and adds it to m_arrUsedFields
386 : : // xAllFields needs to support the DatabaseRecord service
387 : : SVX_DLLPRIVATE ::rtl::OUString FormatField(const FieldInfo& rField);
388 : : // formats the field with the NumberFormatter
389 : :
390 : : SVX_DLLPRIVATE sal_Bool HasPreviousLoc() { return m_aPreviousLocBookmark.hasValue(); }
391 : :
392 : : DECL_LINK(OnSearchTerminated, FmSearchThread*);
393 : : // is used by SearchThread, after the return from this handler the thread removes itself
394 : : DECL_LINK(OnNewRecordCount, void*);
395 : : };
396 : :
397 : : #endif // _FMSRCIMP_HXX
398 : :
399 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|