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 <string_view>
37 #include <vector>
38 
39 namespace com::sun::star::awt { class XCheckBox; }
40 namespace com::sun::star::awt { class XListBox; }
41 namespace com::sun::star::awt { class XTextComponent; }
42 namespace com::sun::star::sdb { class XColumn; }
43 
44 enum class TransliterationFlags;
45 
51 {
53  // (move to new record; progress during counting of records; cancelled; record found; nothing found;
54  // any non-processable error)
56 
57  // current record - always valid (e.g. of interest for continuing search in case of cancellation)
58  sal_uInt32 nCurrentRecord;
59  // Overflow - only valid in case of STATE_PROGRESS
60  bool bOverflow;
61 
62  // the position of the search cursor - valid in case of STATE_SUCCESSFULL, STATE_CANCELED and STATE_NOTHING_FOUND
63  css::uno::Any aBookmark;
64  // the field, in which the text was found - valid in case of STATE_SUCCESSFULL
65  sal_Int32 nFieldIndex;
66 };
67 
72 // workaround for incremental linking bugs in MSVC2019
73 class SAL_DLLPUBLIC_TEMPLATE FmRecordCountListener_Base : public cppu::WeakImplHelper< css::beans::XPropertyChangeListener > {};
75 {
76 // attribute
78  css::uno::Reference< css::beans::XPropertySet > m_xListening;
79 
80 // attribute access
81 public:
82  void SetPropChangeHandler(const Link<sal_Int32,void>& lnk);
83 
84 // methods
85 public:
86  FmRecordCountListener(const css::uno::Reference< css::sdbc::XResultSet >& dbcCursor);
87  // the set has to support the sdb::ResultSet service
88  virtual ~FmRecordCountListener() override;
89 
90  // DECLARE_UNO3_AGG_DEFAULTS(FmPropertyListener, UsrObject)
91  // virtual sal_Bool queryInterface(css::uno::Uik aUik, css::uno::Reference< css::uno::XInterface >& rOut);
92 
93  // css::lang::XEventListener
94  virtual void SAL_CALL disposing(const css::lang::EventObject& Source) override;
95 
96  // css::beans::XPropertyChangeListener
97  virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent& evt) override;
98 
99  void DisConnect();
100 
101 private:
102  void NotifyCurrentCount();
103 
104 };
105 
109 namespace svxform {
110  // We have three possible control types we may search in, determined by the supported interfaces : css::awt::XTextComponent,
111  // css::awt::XListBox, css::awt::XCheckBox.
112  // While searching we don't want to do this distinction for every control in every round. So we need some helpers.
114  {
115  // attributes
116  css::uno::Reference< css::uno::XInterface > m_xControl;
117  // attribute access
118  public:
119  const css::uno::Reference< css::uno::XInterface >& getControl() const{ return m_xControl; }
120  public:
121  ControlTextWrapper(const css::uno::Reference< css::uno::XInterface >& _xControl) { m_xControl = _xControl; }
122  virtual ~ControlTextWrapper() { }
123 
124  virtual OUString getCurrentText() const = 0;
125  };
126 
128  {
129  css::uno::Reference< css::awt::XTextComponent > m_xText;
130  public:
131  SimpleTextWrapper(const css::uno::Reference< css::awt::XTextComponent >& _xText);
132  virtual OUString getCurrentText() const override;
133  };
134 
136  {
137  css::uno::Reference< css::awt::XListBox > m_xBox;
138  public:
139  ListBoxWrapper(const css::uno::Reference< css::awt::XListBox >& _xBox);
140  virtual OUString getCurrentText() const override;
141  };
142 
144  {
145  css::uno::Reference< css::awt::XCheckBox > m_xBox;
146  public:
147  CheckBoxWrapper(const css::uno::Reference< css::awt::XCheckBox >& _xBox);
148  virtual OUString getCurrentText() const override;
149  };
150 }
151 
152 typedef std::vector< css::uno::Reference< css::uno::XInterface> > InterfaceArray;
153 
155 {
156  friend class FmSearchThread;
157 
158  enum class SearchResult { Found, NotFound, Error, Cancelled };
159  enum class SearchFor { String, Null, NotNull };
160 
162  std::deque<sal_Int32> m_arrFieldMapping;
163  // Since the iterator could have more columns, as managed here (in this field listbox),
164  // a mapping of this css::form keys on the indices of the respective columns is kept in the iterator
165 
168 
169  // the collection of all interesting fields (or their css::data::XDatabaseVariant interfaces and FormatKeys)
170  struct FieldInfo
171  {
172  css::uno::Reference< css::sdb::XColumn > xContents;
173  };
174 
175  typedef std::vector<FieldInfo> FieldCollection;
176  FieldCollection m_arrUsedFields;
177  sal_Int32 m_nCurrentFieldIndex; // the last parameter of RebuildUsedFields, it allows checks in FormatField
178 
179  std::vector<std::unique_ptr<svxform::ControlTextWrapper>>
181 
184 
185  // Data for the decision in which field a "Found" is accepted
186  css::uno::Any m_aPreviousLocBookmark; // position of the last finding
187  FieldCollection::iterator m_iterPreviousLocField; // field of the last finding
188 
189  // Communication with the thread that does the actual searching
190  OUString m_strSearchExpression; // forward direction
192  SearchResult m_srResult; // backward direction
193 
194  // The link we broadcast the progress and the result to
196  bool m_bSearchingCurrently : 1; // is an (asynchronous) search running?
197  bool m_bCancelAsynchRequest : 1; // should be cancelled?
198  ::osl::Mutex m_aCancelAsynchAccess; // access to_bCancelAsynchRequest (technically only
199  // relevant for m_eMode == SM_USETHREAD)
200 
201  // parameters for the search
202  bool m_bFormatter : 1; // use field formatting
203  bool m_bForward : 1; // direction
204  bool m_bWildcard : 1; // wildcard search
205  bool m_bRegular : 1; // regular expression
206  bool m_bLevenshtein : 1; // Levenshtein search
207  bool m_bTransliteration : 1; // Levenshtein search
208 
209  bool m_bLevRelaxed : 1; // parameters for Levenshtein search
210  sal_uInt16 m_nLevOther;
211  sal_uInt16 m_nLevShorter;
212  sal_uInt16 m_nLevLonger;
213 
214  sal_uInt16 m_nPosition; // if not regular or levenshtein, then one of the MATCHING_... values
215 
217 
218 
219 // member access
220 private:
221  SVX_DLLPRIVATE bool CancelRequested(); // provides a through m_aCancelAsynchAccess backed interpretation of m_bCancelAsynchRequest
222 
223 public:
224  void SetCaseSensitive(bool bSet);
225  bool GetCaseSensitive() const;
226 
227  void SetFormatterUsing(bool bSet); // this is somewhat more extensive, so no inline ... here
228  bool GetFormatterUsing() const { return m_bFormatter; }
229 
230  void SetDirection(bool bForward) { m_bForward = bForward; }
231  bool GetDirection() const { return m_bForward; }
232 
233  void SetWildcard(bool bSet) { m_bWildcard = bSet; }
234  bool GetWildcard() const { return m_bWildcard; }
235 
236  void SetRegular(bool bSet) { m_bRegular = bSet; }
237  bool GetRegular() const { return m_bRegular; }
238 
239  void SetLevenshtein(bool bSet) { m_bLevenshtein = bSet; }
240  bool GetLevenshtein() const { return m_bLevenshtein; }
241 
242  void SetIgnoreWidthCJK(bool bSet);
243  bool GetIgnoreWidthCJK() const;
244 
245  void SetTransliteration(bool bSet) { m_bTransliteration = bSet; }
246  bool GetTransliteration() const { return m_bTransliteration; }
247 
248  void SetLevRelaxed(bool bSet) { m_bLevRelaxed = bSet; }
249  bool GetLevRelaxed() const { return m_bLevRelaxed; }
250  void SetLevOther(sal_uInt16 nHowMuch) { m_nLevOther = nHowMuch; }
251  sal_uInt16 GetLevOther() const { return m_nLevOther; }
252  void SetLevShorter(sal_uInt16 nHowMuch) { m_nLevShorter = nHowMuch; }
253  sal_uInt16 GetLevShorter() const { return m_nLevShorter; }
254  void SetLevLonger(sal_uInt16 nHowMuch) { m_nLevLonger = nHowMuch; }
255  sal_uInt16 GetLevLonger() const { return m_nLevLonger; }
256  // all Lev. values will only be considered in case of m_bLevenshtein==sal_True
257 
258  void SetTransliterationFlags(TransliterationFlags _nFlags) { m_nTransliterationFlags = _nFlags; }
260  GetTransliterationFlags() const { return m_nTransliterationFlags; }
261 
262  void SetPosition(sal_uInt16 nValue) { m_nPosition = nValue; }
263  sal_uInt16 GetPosition() const { return m_nPosition; }
264  // position will be ignored in case of m_bWildCard==sal_True
265 
266 public:
276  const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
277  const css::uno::Reference< css::sdbc::XResultSet >& xCursor,
278  const OUString& strVisibleFields,
279  const InterfaceArray& arrFields);
280 
285  void SetProgressHandler(Link<const FmSearchProgress*,void> aHdl) { m_aProgressHandler = aHdl; }
286 
288  void SearchNext(const OUString& strExpression);
290  void SearchNextSpecial(bool _bSearchForNull);
292  void StartOver(const OUString& strExpression);
294  void StartOverSpecial(bool _bSearchForNull);
296  void InvalidatePreviousLoc();
297 
302  void RebuildUsedFields(sal_Int32 nFieldIndex, bool bForce = false);
303  OUString FormatField(sal_Int32 nWhich);
304 
306  void CancelSearch();
307 
311  void SwitchToContext(const css::uno::Reference< css::sdbc::XResultSet >& xCursor, const OUString& strVisibleFields, const InterfaceArray& arrFields,
312  sal_Int32 nFieldIndex);
313 
314 private:
315  void Init(const OUString& strVisibleFields);
316 
317  void SearchNextImpl();
318  // this Impl method is running in SearchThread
319 
320  // start a thread-search (or call SearchNextImpl directly, depending on the search mode)
321  void ImplStartNextSearch();
322 
323  SVX_DLLPRIVATE void fillControlTexts(const InterfaceArray& arrFields);
324 
325  // three methods implementing a complete search loop (null/not null, wildcard, SearchText)
326  // (they all have some code in common, but with this solution we have to do a distinction only once per search (before
327  // starting the loop), not in every loop step
328  SVX_DLLPRIVATE SearchResult SearchSpecial(bool _bSearchForNull, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
329  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
330  SVX_DLLPRIVATE SearchResult SearchWildcard(std::u16string_view strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
331  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
332  SVX_DLLPRIVATE SearchResult SearchRegularApprox(const OUString& strExpression, sal_Int32& nFieldPos, FieldCollection::iterator& iterFieldLoop,
333  const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
334 
335  SVX_DLLPRIVATE void PropagateProgress(bool _bDontPropagateOverflow);
336  // call the ProgressHandler with STATE_PROGRESS and the current position of the search iterator
337 
338  // helpers, that are needed several times
339  SVX_DLLPRIVATE bool MoveCursor();
340  // moves m_xSearchIterator with respect to direction/overflow cursor
341  SVX_DLLPRIVATE bool MoveField(sal_Int32& nPos, FieldCollection::iterator& iter, const FieldCollection::iterator& iterBegin, const FieldCollection::iterator& iterEnd);
342  // moves the iterator with respect to the direction/overflow iterator/overflow cursor
343  SVX_DLLPRIVATE void BuildAndInsertFieldInfo(const css::uno::Reference< css::container::XIndexAccess >& xAllFields, sal_Int32 nField);
344 
345  void OnSearchTerminated();
346  // is used by SearchThread, after the return from this handler the thread removes itself
347  DECL_LINK(OnNewRecordCount, sal_Int32, void);
348 };
349 
350 #endif // INCLUDED_SVX_FMSRCIMP_HXX
351 
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
NotNull
TransliterationFlags m_nTransliterationFlags
Definition: fmsrcimp.hxx:216
std::vector< FieldInfo > FieldCollection
Definition: fmsrcimp.hxx:175
void Init()
sal_Int32 nFieldIndex
Definition: fmsrcimp.hxx:65
Link< const FmSearchProgress *, void > m_aProgressHandler
Definition: fmsrcimp.hxx:195
Reference< XControl > m_xControl
FieldCollection::iterator m_iterPreviousLocField
Definition: fmsrcimp.hxx:187
css::uno::Any aBookmark
Definition: fmsrcimp.hxx:63
sal_uInt16 m_nLevLonger
Definition: fmsrcimp.hxx:212
void SetLevOther(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:250
CursorWrapper m_xOriginalIterator
Definition: fmsrcimp.hxx:182
void SetLevShorter(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:252
sal_uInt16 GetPosition() const
Definition: fmsrcimp.hxx:263
sal_uInt16 m_nLevShorter
Definition: fmsrcimp.hxx:211
bool GetRegular() const
Definition: fmsrcimp.hxx:237
#define SVX_DLLPUBLIC
Definition: svxdllapi.h:28
css::uno::Any m_aPreviousLocBookmark
Definition: fmsrcimp.hxx:186
void SetTransliteration(bool bSet)
Definition: fmsrcimp.hxx:245
css::uno::Reference< css::uno::XInterface > m_xControl
Definition: fmsrcimp.hxx:116
bool GetTransliteration() const
Definition: fmsrcimp.hxx:246
std::vector< css::uno::Reference< css::uno::XInterface > > InterfaceArray
Definition: fmsrcimp.hxx:152
CollatorWrapper m_aStringCompare
Definition: fmsrcimp.hxx:167
sal_uInt32 nCurrentRecord
Definition: fmsrcimp.hxx:58
State aSearchState
Definition: fmsrcimp.hxx:55
FieldCollection m_arrUsedFields
Definition: fmsrcimp.hxx:176
css::uno::Reference< css::awt::XCheckBox > m_xBox
Definition: fmsrcimp.hxx:145
void SetWildcard(bool bSet)
Definition: fmsrcimp.hxx:233
bool GetWildcard() const
Definition: fmsrcimp.hxx:234
TransliterationFlags
css::uno::Reference< css::beans::XPropertySet > m_xListening
Definition: fmsrcimp.hxx:78
css::uno::Reference< css::awt::XListBox > m_xBox
Definition: fmsrcimp.hxx:137
std::deque< sal_Int32 > m_arrFieldMapping
Definition: fmsrcimp.hxx:162
std::vector< std::unique_ptr< svxform::ControlTextWrapper > > m_aControlTexts
Definition: fmsrcimp.hxx:180
sal_uInt16 GetLevOther() const
Definition: fmsrcimp.hxx:251
Error
class FmRecordCountListener - utility class for FmSearchEngine, listens at a certain cursor and provi...
Definition: fmsrcimp.hxx:73
bool GetDirection() const
Definition: fmsrcimp.hxx:231
struct FmSearchProgress - the owner of SearchEngine receives this structure for status updates (at th...
Definition: fmsrcimp.hxx:50
sal_Int32 m_nCurrentFieldIndex
Definition: fmsrcimp.hxx:177
ControlTextWrapper(const css::uno::Reference< css::uno::XInterface > &_xControl)
Definition: fmsrcimp.hxx:121
void SetDirection(bool bForward)
Definition: fmsrcimp.hxx:230
SearchFor m_eSearchForType
Definition: fmsrcimp.hxx:191
sal_uInt16 m_nLevOther
Definition: fmsrcimp.hxx:210
OUString m_strSearchExpression
Definition: fmsrcimp.hxx:190
#define SAL_WARN_UNUSED
CharClass m_aCharacterClassficator
Definition: fmsrcimp.hxx:166
void SetLevRelaxed(bool bSet)
Definition: fmsrcimp.hxx:248
const css::uno::Reference< css::uno::XInterface > & getControl() const
Definition: fmsrcimp.hxx:119
sal_uInt16 m_nPosition
Definition: fmsrcimp.hxx:214
void SetRegular(bool bSet)
Definition: fmsrcimp.hxx:236
::osl::Mutex m_aCancelAsynchAccess
Definition: fmsrcimp.hxx:198
TransliterationFlags GetTransliterationFlags() const
Definition: fmsrcimp.hxx:260
CursorWrapper m_xSearchCursor
Definition: fmsrcimp.hxx:161
bool GetFormatterUsing() const
Definition: fmsrcimp.hxx:228
css::uno::Reference< css::sdb::XColumn > xContents
Definition: fmsrcimp.hxx:172
void SetLevLonger(sal_uInt16 nHowMuch)
Definition: fmsrcimp.hxx:254
void SetTransliterationFlags(TransliterationFlags _nFlags)
Definition: fmsrcimp.hxx:258
class FmSearchEngine - Impl class for FmSearchDialog
Link< sal_Int32, void > m_lnkWhoWantsToKnow
Definition: fmsrcimp.hxx:77
void SetPosition(sal_uInt16 nValue)
Definition: fmsrcimp.hxx:262
void SetLevenshtein(bool bSet)
Definition: fmsrcimp.hxx:239
SearchResult m_srResult
Definition: fmsrcimp.hxx:192
bool GetLevenshtein() const
Definition: fmsrcimp.hxx:240
#define SVX_DLLPRIVATE
Definition: svxdllapi.h:30
sal_uInt16 GetLevShorter() const
Definition: fmsrcimp.hxx:253
bool GetLevRelaxed() const
Definition: fmsrcimp.hxx:249
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:285
CursorWrapper m_xClonedIterator
Definition: fmsrcimp.hxx:183
Found
sal_uInt16 GetLevLonger() const
Definition: fmsrcimp.hxx:255
sal_Int16 nValue
Definition: fmsrccfg.cxx:81
css::uno::Reference< css::awt::XTextComponent > m_xText
Definition: fmsrcimp.hxx:129