LibreOffice Module svx (master)  1
fmsrcimp.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SVX_FMSRCIMP_HXX
21 #define INCLUDED_SVX_FMSRCIMP_HXX
22 
23 #include <svx/fmtools.hxx>
24 #include <svx/svxdllapi.h>
25 
26 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
27 
28 #include <cppuhelper/implbase.hxx>
29 #include <osl/mutex.hxx>
30 #include <unotools/charclass.hxx>
32 #include <tools/link.hxx>
33 
34 #include <deque>
35 #include <memory>
36 #include <vector>
37 
38 namespace com::sun::star::awt { class XCheckBox; }
39 namespace com::sun::star::awt { class XListBox; }
40 namespace com::sun::star::awt { class XTextComponent; }
41 namespace com::sun::star::sdb { class XColumn; }
42 
43 enum class TransliterationFlags;
44 
50 {
52  // (move to new record; progress during counting of records; cancelled; record found; nothing found;
53  // any non-processable error)
55 
56  // current record - always valid (e.g. of interest for continuing search in case of cancellation)
57  sal_uInt32 nCurrentRecord;
58  // Overflow - only valid in case of STATE_PROGRESS
59  bool bOverflow;
60 
61  // the position of the search cursor - valid in case of STATE_SUCCESSFULL, STATE_CANCELED and STATE_NOTHING_FOUND
62  css::uno::Any aBookmark;
63  // the field, in which the text was found - valid in case of STATE_SUCCESSFULL
64  sal_Int32 nFieldIndex;
65 };
66 
71 class SAL_WARN_UNUSED FmRecordCountListener final : public cppu::WeakImplHelper< css::beans::XPropertyChangeListener >
72 {
73 // attribute
75  css::uno::Reference< css::beans::XPropertySet > m_xListening;
76 
77 // attribute access
78 public:
79  void SetPropChangeHandler(const Link<sal_Int32,void>& lnk);
80 
81 // methods
82 public:
83  FmRecordCountListener(const css::uno::Reference< css::sdbc::XResultSet >& dbcCursor);
84  // the set has to support the sdb::ResultSet service
85  virtual ~FmRecordCountListener() override;
86 
87  // DECLARE_UNO3_AGG_DEFAULTS(FmPropertyListener, UsrObject)
88  // virtual sal_Bool queryInterface(css::uno::Uik aUik, css::uno::Reference< css::uno::XInterface >& rOut);
89 
90  // css::lang::XEventListener
91  virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
92 
93  // css::beans::XPropertyChangeListener
94  virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent& evt) override;
95 
96  void DisConnect();
97 
98 private:
99  void NotifyCurrentCount();
100 
101 };
102 
106 namespace svxform {
107  // We have three possible control types we may search in, determined by the supported interfaces : css::awt::XTextComponent,
108  // css::awt::XListBox, css::awt::XCheckBox.
109  // While searching we don't want to do this distinction for every control in every round. So we need some helpers.
111  {
112  // attributes
113  css::uno::Reference< css::uno::XInterface > m_xControl;
114  // attribute access
115  public:
116  const css::uno::Reference< css::uno::XInterface >& getControl() const{ return m_xControl; }
117  public:
118  ControlTextWrapper(const css::uno::Reference< css::uno::XInterface >& _xControl) { m_xControl = _xControl; }
119  virtual ~ControlTextWrapper() { }
120 
121  virtual OUString getCurrentText() const = 0;
122  };
123 
125  {
126  css::uno::Reference< css::awt::XTextComponent > m_xText;
127  public:
128  SimpleTextWrapper(const css::uno::Reference< css::awt::XTextComponent >& _xText);
129  virtual OUString getCurrentText() const override;
130  };
131 
133  {
134  css::uno::Reference< css::awt::XListBox > m_xBox;
135  public:
136  ListBoxWrapper(const css::uno::Reference< css::awt::XListBox >& _xBox);
137  virtual OUString getCurrentText() const override;
138  };
139 
141  {
142  css::uno::Reference< css::awt::XCheckBox > m_xBox;
143  public:
144  CheckBoxWrapper(const css::uno::Reference< css::awt::XCheckBox >& _xBox);
145  virtual OUString getCurrentText() const override;
146  };
147 }
148 
149 typedef std::vector< css::uno::Reference< css::uno::XInterface> > InterfaceArray;
150 
152 {
153  friend class FmSearchThread;
154 
155  enum class SearchResult { Found, NotFound, Error, Cancelled };
156  enum class SearchFor { String, Null, NotNull };
157 
159  std::deque<sal_Int32> m_arrFieldMapping;
160  // Since the iterator could have more columns, as managed here (in this field listbox),
161  // a mapping of this css::form keys on the indices of the respective columns is kept in the iterator
162 
165 
166  // the collection of all interesting fields (or their css::data::XDatabaseVariant interfaces and FormatKeys)
167  struct FieldInfo
168  {
169  css::uno::Reference< css::sdb::XColumn > xContents;
170  };
171 
172  typedef std::vector<FieldInfo> FieldCollection;
173  FieldCollection m_arrUsedFields;
174  sal_Int32 m_nCurrentFieldIndex; // the last parameter of RebuildUsedFields, it allows checks in FormatField
175 
176  std::vector<std::unique_ptr<svxform::ControlTextWrapper>>
178 
181 
182  // Data for the decision in which field a "Found" is accepted
183  css::uno::Any m_aPreviousLocBookmark; // position of the last finding
184  FieldCollection::iterator m_iterPreviousLocField; // field of the last finding
185 
186  // Communication with the thread that does the actual searching
187  OUString m_strSearchExpression; // forward direction
189  SearchResult m_srResult; // backward direction
190 
191  // The link we broadcast the progress and the result to
193  bool m_bSearchingCurrently : 1; // is an (asynchronous) search running?
194  bool m_bCancelAsynchRequest : 1; // should be cancelled?
195  ::osl::Mutex m_aCancelAsynchAccess; // access to_bCancelAsynchRequest (technically only
196  // relevant for m_eMode == SM_USETHREAD)
197 
198  // parameters for the search
199  bool m_bFormatter : 1; // use field formatting
200  bool m_bForward : 1; // direction
201  bool m_bWildcard : 1; // wildcard search
202  bool m_bRegular : 1; // regular expression
203  bool m_bLevenshtein : 1; // Levenshtein search
204  bool m_bTransliteration : 1; // Levenshtein search
205 
206  bool m_bLevRelaxed : 1; // parameters for Levenshtein search
207  sal_uInt16 m_nLevOther;
208  sal_uInt16 m_nLevShorter;
209  sal_uInt16 m_nLevLonger;
210 
211  sal_uInt16 m_nPosition; // if not regular or levenshtein, then one of the MATCHING_... values
212 
214 
215 
216 // member access
217 private:
218  SVX_DLLPRIVATE bool CancelRequested(); // provides a through m_aCancelAsynchAccess backed interpretation of m_bCancelAsynchRequest
219 
220 public:
221  void SetCaseSensitive(bool bSet);
222  bool GetCaseSensitive() const;
223 
224  void SetFormatterUsing(bool bSet); // this is somewhat more extensive, so no inline ... here
225  bool GetFormatterUsing() const { return m_bFormatter; }
226 
227  void SetDirection(bool bForward) { m_bForward = bForward; }
228  bool GetDirection() const { return m_bForward; }
229 
230  void SetWildcard(bool bSet) { m_bWildcard = bSet; }
231  bool GetWildcard() const { return m_bWildcard; }
232 
233  void SetRegular(bool bSet) { m_bRegular = bSet; }
234  bool GetRegular() const { return m_bRegular; }
235 
236  void SetLevenshtein(bool bSet) { m_bLevenshtein = bSet; }
237  bool GetLevenshtein() const { return m_bLevenshtein; }
238 
239  void SetIgnoreWidthCJK(bool bSet);
240  bool GetIgnoreWidthCJK() const;
241 
242  void SetTransliteration(bool bSet) { m_bTransliteration = bSet; }
243  bool GetTransliteration() const { return m_bTransliteration; }
244 
245  void SetLevRelaxed(bool bSet) { m_bLevRelaxed = bSet; }
246  bool GetLevRelaxed() const { return m_bLevRelaxed; }
247  void SetLevOther(sal_uInt16 nHowMuch) { m_nLevOther = nHowMuch; }
248  sal_uInt16 GetLevOther() const { return m_nLevOther; }
249  void SetLevShorter(sal_uInt16 nHowMuch) { m_nLevShorter = nHowMuch; }
250  sal_uInt16 GetLevShorter() const { return m_nLevShorter; }
251  void SetLevLonger(sal_uInt16 nHowMuch) { m_nLevLonger = nHowMuch; }
252  sal_uInt16 GetLevLonger() const { return m_nLevLonger; }
253  // all Lev. values will only be considered in case of m_bLevenshtein==sal_True
254 
255  void SetTransliterationFlags(TransliterationFlags _nFlags) { m_nTransliterationFlags = _nFlags; }
257  GetTransliterationFlags() const { return m_nTransliterationFlags; }
258 
259  void SetPosition(sal_uInt16 nValue) { m_nPosition = nValue; }
260  sal_uInt16 GetPosition() const { return m_nPosition; }
261  // position will be ignored in case of m_bWildCard==sal_True
262 
263 public:
273  const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
274  const css::uno::Reference< css::sdbc::XResultSet >& xCursor,
275  const OUString& strVisibleFields,
276  const InterfaceArray& arrFields);
277 
282  void SetProgressHandler(Link<const FmSearchProgress*,void> aHdl) { m_aProgressHandler = aHdl; }
283 
285  void SearchNext(const OUString& strExpression);
287  void SearchNextSpecial(bool _bSearchForNull);
289  void StartOver(const OUString& strExpression);
291  void StartOverSpecial(bool _bSearchForNull);
293  void InvalidatePreviousLoc();
294 
299  void RebuildUsedFields(sal_Int32 nFieldIndex, bool bForce = false);
300  OUString FormatField(sal_Int32 nWhich);
301 
303  void CancelSearch();
304 
308  void SwitchToContext(const css::uno::Reference< css::sdbc::XResultSet >& xCursor, const OUString& strVisibleFields, const InterfaceArray& arrFields,
309  sal_Int32 nFieldIndex);
310 
311 private:
312  void Init(const OUString& strVisibleFields);
313 
314  void SearchNextImpl();
315  // this Impl method is running in SearchThread
316 
317  // start a thread-search (or call SearchNextImpl directly, depending on the search mode)
318  void ImplStartNextSearch();
319 
320  SVX_DLLPRIVATE void clearControlTexts();
321  SVX_DLLPRIVATE void fillControlTexts(const InterfaceArray& arrFields);
322 
323  // three methods implementing a complete search loop (null/not null, wildcard, SearchText)
324  // (they all have some code in common, but with this solution we have to do a distinction only once per search (before
325  // starting the loop), not in every loop step
326  SVX_DLLPRIVATE SearchResult SearchSpecial(bool _bSearchForNull, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
327  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
328  SVX_DLLPRIVATE SearchResult SearchWildcard(const OUString& strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
329  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
330  SVX_DLLPRIVATE SearchResult SearchRegularApprox(const OUString& strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
331  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
332 
333  SVX_DLLPRIVATE void PropagateProgress(bool _bDontPropagateOverflow);
334  // call the ProgressHandler with STATE_PROGRESS and the current position of the search iterator
335 
336  // helpers, that are needed several times
337  SVX_DLLPRIVATE bool MoveCursor();
338  // moves m_xSearchIterator with respect to direction/overflow cursor
339  SVX_DLLPRIVATE bool MoveField(sal_Int32& nPos, FieldCollection::iterator& iter, const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
340  // moves the iterator with respect to the direction/overflow iterator/overflow cursor
341  SVX_DLLPRIVATE void BuildAndInsertFieldInfo(const css::uno::Reference< css::container::XIndexAccess >& xAllFields, sal_Int32 nField);
342 
343  void OnSearchTerminated();
344  // is used by SearchThread, after the return from this handler the thread removes itself
345  DECL_LINK(OnNewRecordCount, sal_Int32, void);
346 };
347 
348 #endif // INCLUDED_SVX_FMSRCIMP_HXX
349 
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NotNull
TransliterationFlags m_nTransliterationFlags
Definition: fmsrcimp.hxx:213
std::vector< FieldInfo > FieldCollection
Definition: fmsrcimp.hxx:172
void Init()
sal_Int32 nFieldIndex
Definition: fmsrcimp.hxx:64
Link< const FmSearchProgress *, void > m_aProgressHandler
Definition: fmsrcimp.hxx:192
Reference< XControl > m_xControl
FieldCollection::iterator m_iterPreviousLocField
Definition: fmsrcimp.hxx:184
css::uno::Any aBookmark
Definition: fmsrcimp.hxx:62
sal_uInt16 m_nLevLonger
Definition: fmsrcimp.hxx:209
void SetLevOther(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:247
CursorWrapper m_xOriginalIterator
Definition: fmsrcimp.hxx:179
void SetLevShorter(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:249
sal_uInt16 GetPosition() const
Definition: fmsrcimp.hxx:260
sal_uInt16 m_nLevShorter
Definition: fmsrcimp.hxx:208
bool GetRegular() const
Definition: fmsrcimp.hxx:234
#define SVX_DLLPUBLIC
Definition: svxdllapi.h:28
class FmRecordCountListener - utility class for FmSearchEngine, listens at a certain cursor and provi...
Definition: fmsrcimp.hxx:71
css::uno::Any m_aPreviousLocBookmark
Definition: fmsrcimp.hxx:183
void SetTransliteration(bool bSet)
Definition: fmsrcimp.hxx:242
css::uno::Reference< css::uno::XInterface > m_xControl
Definition: fmsrcimp.hxx:113
bool GetTransliteration() const
Definition: fmsrcimp.hxx:243
std::vector< css::uno::Reference< css::uno::XInterface > > InterfaceArray
Definition: fmsrcimp.hxx:149
CollatorWrapper m_aStringCompare
Definition: fmsrcimp.hxx:164
sal_uInt32 nCurrentRecord
Definition: fmsrcimp.hxx:57
State aSearchState
Definition: fmsrcimp.hxx:54
FieldCollection m_arrUsedFields
Definition: fmsrcimp.hxx:173
css::uno::Reference< css::awt::XCheckBox > m_xBox
Definition: fmsrcimp.hxx:142
sal_Int16 const nValue
Definition: fmsrccfg.cxx:82
void SetWildcard(bool bSet)
Definition: fmsrcimp.hxx:230
bool GetWildcard() const
Definition: fmsrcimp.hxx:231
TransliterationFlags
css::uno::Reference< css::beans::XPropertySet > m_xListening
Definition: fmsrcimp.hxx:75
css::uno::Reference< css::awt::XListBox > m_xBox
Definition: fmsrcimp.hxx:134
std::deque< sal_Int32 > m_arrFieldMapping
Definition: fmsrcimp.hxx:159
std::vector< std::unique_ptr< svxform::ControlTextWrapper > > m_aControlTexts
Definition: fmsrcimp.hxx:177
sal_uInt16 GetLevOther() const
Definition: fmsrcimp.hxx:248
Error
bool GetDirection() const
Definition: fmsrcimp.hxx:228
struct FmSearchProgress - the owner of SearchEngine receives this structure for status updates (at th...
Definition: fmsrcimp.hxx:49
sal_Int32 m_nCurrentFieldIndex
Definition: fmsrcimp.hxx:174
ControlTextWrapper(const css::uno::Reference< css::uno::XInterface > &_xControl)
Definition: fmsrcimp.hxx:118
void SetDirection(bool bForward)
Definition: fmsrcimp.hxx:227
SearchFor m_eSearchForType
Definition: fmsrcimp.hxx:188
sal_uInt16 m_nLevOther
Definition: fmsrcimp.hxx:207
OUString m_strSearchExpression
Definition: fmsrcimp.hxx:187
#define SAL_WARN_UNUSED
void SetLevRelaxed(bool bSet)
Definition: fmsrcimp.hxx:245
const css::uno::Reference< css::uno::XInterface > & getControl() const
Definition: fmsrcimp.hxx:116
sal_uInt16 m_nPosition
Definition: fmsrcimp.hxx:211
void SetRegular(bool bSet)
Definition: fmsrcimp.hxx:233
::osl::Mutex m_aCancelAsynchAccess
Definition: fmsrcimp.hxx:195
TransliterationFlags GetTransliterationFlags() const
Definition: fmsrcimp.hxx:257
CursorWrapper m_xSearchCursor
Definition: fmsrcimp.hxx:158
bool GetFormatterUsing() const
Definition: fmsrcimp.hxx:225
css::uno::Reference< css::sdb::XColumn > xContents
Definition: fmsrcimp.hxx:169
void SetLevLonger(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:251
void SetTransliterationFlags(TransliterationFlags _nFlags)
Definition: fmsrcimp.hxx:255
class FmSearchEngine - Impl class for FmSearchDialog
Link< sal_Int32, void > m_lnkWhoWantsToKnow
Definition: fmsrcimp.hxx:74
void SetPosition(sal_uInt16 nValue)
Definition: fmsrcimp.hxx:259
void SetLevenshtein(bool bSet)
Definition: fmsrcimp.hxx:236
SearchResult m_srResult
Definition: fmsrcimp.hxx:189
bool GetLevenshtein() const
Definition: fmsrcimp.hxx:237
#define SVX_DLLPRIVATE
Definition: svxdllapi.h:30
sal_uInt16 GetLevShorter() const
Definition: fmsrcimp.hxx:250
bool GetLevRelaxed() const
Definition: fmsrcimp.hxx:246
void SetProgressHandler(Link< const FmSearchProgress *, void > aHdl)
the link will be called on every record and after the completion of the search, the parameter is a po...
Definition: fmsrcimp.hxx:282
CursorWrapper m_xClonedIterator
Definition: fmsrcimp.hxx:180
Found
sal_uInt16 GetLevLonger() const
Definition: fmsrcimp.hxx:252
CharClass const m_aCharacterClassficator
Definition: fmsrcimp.hxx:163
css::uno::Reference< css::awt::XTextComponent > m_xText
Definition: fmsrcimp.hxx:126