1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 
27 #include <unotools/searchopt.hxx>
28 #include <tools/solar.h>
29 #include <tools/debug.hxx>
30 #include <unotools/configitem.hxx>
31 #include <com/sun/star/i18n/TransliterationModules.hpp>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/uno/Any.h>
34 #include <rtl/logfile.hxx>
35 
36 
37 using namespace rtl;
38 using namespace utl;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::i18n;
41 
42 #define MAX_FLAGS_OFFSET	25
43 
44 //////////////////////////////////////////////////////////////////////
45 
46 
47 class SvtSearchOptions_Impl : public ConfigItem
48 {
49 	sal_Int32	nFlags;
50 	sal_Bool	bModified;
51 
52 	// disallow copy-constructor and assignment-operator for now
53 	SvtSearchOptions_Impl( const SvtSearchOptions_Impl & );
54 	SvtSearchOptions_Impl & operator = ( const SvtSearchOptions_Impl & );
55 
56 protected:
IsModified() const57 	sal_Bool			IsModified() const { return bModified; }
58     using ConfigItem::SetModified;
59 	void			SetModified( sal_Bool bVal );
60 	sal_Bool			Load();
61 	sal_Bool			Save();
62 
63 	Sequence< OUString >	GetPropertyNames() const;
64 
65 public:
66 	SvtSearchOptions_Impl();
67 	virtual ~SvtSearchOptions_Impl();
68 
69 	// ConfigItem
70 	virtual void	Commit();
71     virtual void    Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
72 
73 	sal_Bool			GetFlag( sal_uInt16 nOffset ) const;
74 	void			SetFlag( sal_uInt16 nOffset, sal_Bool bVal );
75 };
76 
77 
78 
SvtSearchOptions_Impl()79 SvtSearchOptions_Impl::SvtSearchOptions_Impl() :
80 	ConfigItem( OUString::createFromAscii( "Office.Common/SearchOptions" ) )
81 {
82     RTL_LOGFILE_CONTEXT(aLog, "unotools SvtSearchOptions_Impl::SvtSearchOptions_Impl()");
83 	nFlags = 0x0003FFFF;	// set all options values to 'true'
84 	Load();
85 	SetModified( sal_False );
86 }
87 
88 
~SvtSearchOptions_Impl()89 SvtSearchOptions_Impl::~SvtSearchOptions_Impl()
90 {
91 	Commit();
92 }
93 
94 
Commit()95 void SvtSearchOptions_Impl::Commit()
96 {
97 	if (IsModified())
98 		Save();
99 }
100 
Notify(const Sequence<rtl::OUString> &)101 void SvtSearchOptions_Impl::Notify( const Sequence< rtl::OUString >&  )
102 {
103 }
104 
105 
GetFlag(sal_uInt16 nOffset) const106 sal_Bool SvtSearchOptions_Impl::GetFlag( sal_uInt16 nOffset ) const
107 {
108 	DBG_ASSERT( nOffset <= MAX_FLAGS_OFFSET, "offset out of range");
109 	return ((nFlags >> nOffset) & 0x01) ? sal_True : sal_False;
110 }
111 
112 
SetFlag(sal_uInt16 nOffset,sal_Bool bVal)113 void SvtSearchOptions_Impl::SetFlag( sal_uInt16 nOffset, sal_Bool bVal )
114 {
115 	DBG_ASSERT( nOffset <= MAX_FLAGS_OFFSET, "offset out of range");
116 	sal_Int32 nOldFlags = nFlags;
117 	sal_Int32 nMask = ((sal_Int32) 1)  << nOffset;
118 	if (bVal)
119 		nFlags |= nMask;
120 	else
121 		nFlags &= ~nMask;
122 	if (nFlags != nOldFlags)
123 		SetModified( sal_True );
124 }
125 
126 
SetModified(sal_Bool bVal)127 void SvtSearchOptions_Impl::SetModified( sal_Bool bVal )
128 {
129     bModified = bVal;
130 	if (bModified)
131 	{
132 		ConfigItem::SetModified();
133 	}
134 }
135 
136 
GetPropertyNames() const137 Sequence< OUString > SvtSearchOptions_Impl::GetPropertyNames() const
138 {
139 	static const char* aPropNames[ MAX_FLAGS_OFFSET + 1 ] =
140 	{
141 		"IsWholeWordsOnly",						//  0
142 		"IsBackwards",							//  1
143 		"IsUseRegularExpression",				//  2
144 		//"IsCurrentSelectionOnly",				// interactively set or not...
145 		"IsSearchForStyles",					//  3
146 		"IsSimilaritySearch",					//  4
147 		"IsUseAsianOptions",					//  5
148 		"IsMatchCase",							//  6
149 		"Japanese/IsMatchFullHalfWidthForms",	//  7
150 		"Japanese/IsMatchHiraganaKatakana",		//  8
151 		"Japanese/IsMatchContractions",			//  9
152 		"Japanese/IsMatchMinusDashCho-on",		// 10
153 		"Japanese/IsMatchRepeatCharMarks",		// 11
154 		"Japanese/IsMatchVariantFormKanji",		// 12
155 		"Japanese/IsMatchOldKanaForms",			// 13
156 		"Japanese/IsMatch_DiZi_DuZu",			// 14
157 		"Japanese/IsMatch_BaVa_HaFa",			// 15
158 		"Japanese/IsMatch_TsiThiChi_DhiZi",		// 16
159 		"Japanese/IsMatch_HyuIyu_ByuVyu",		// 17
160 		"Japanese/IsMatch_SeShe_ZeJe",			// 18
161 		"Japanese/IsMatch_IaIya",				// 19
162 		"Japanese/IsMatch_KiKu",				// 20
163 		"Japanese/IsIgnorePunctuation",			// 21
164 		"Japanese/IsIgnoreWhitespace",			// 22
165 		"Japanese/IsIgnoreProlongedSoundMark",		// 23
166 		"Japanese/IsIgnoreMiddleDot",			// 24
167 		"IsNotes"					// 25
168 	};
169 
170     const int nCount = sizeof( aPropNames ) / sizeof( aPropNames[0] );
171 	Sequence< OUString > aNames( nCount );
172 	OUString* pNames = aNames.getArray();
173 	for (sal_Int32 i = 0;  i < nCount;  ++i)
174 		pNames[i] = OUString::createFromAscii( aPropNames[i] );
175 
176 	return aNames;
177 }
178 
179 
Load()180 sal_Bool SvtSearchOptions_Impl::Load()
181 {
182 	sal_Bool bSucc = sal_False;
183 
184 	Sequence< OUString > aNames = GetPropertyNames();
185 	sal_Int32 nProps = aNames.getLength();
186 
187 	const Sequence< Any > aValues = GetProperties( aNames );
188 	DBG_ASSERT( aValues.getLength() == aNames.getLength(),
189 			"GetProperties failed" );
190 	//EnableNotification( aNames );
191 
192 	if (nProps  &&  aValues.getLength() == nProps)
193 	{
194 		bSucc = sal_True;
195 
196 		const Any* pValues = aValues.getConstArray();
197 		for (sal_uInt16 i = 0;  i < nProps;  ++i)
198 		{
199 			const Any &rVal = pValues[i];
200 			DBG_ASSERT( rVal.hasValue(), "property value missing" );
201 			if (rVal.hasValue())
202 			{
203 				sal_Bool bVal = sal_Bool();
204 				if (rVal >>= bVal)
205 				{
206 					if (i <= MAX_FLAGS_OFFSET)
207 					{
208 						// use index in sequence as flag index
209 						SetFlag( i, bVal );
210 					}
211 					else {
212 						DBG_ERROR( "unexpected index" );
213                     }
214 				}
215 				else
216 				{
217 					DBG_ERROR( "unexpected type" );
218 					bSucc = sal_False;
219 				}
220 			}
221 			else
222 			{
223 				DBG_ERROR( "value missing" );
224 				bSucc = sal_False;
225 			}
226 		}
227 	}
228 	DBG_ASSERT( bSucc, "LoadConfig failed" );
229 
230 	return bSucc;
231 }
232 
233 
Save()234 sal_Bool SvtSearchOptions_Impl::Save()
235 {
236 	sal_Bool bSucc = sal_False;
237 
238 	const Sequence< OUString > aNames = GetPropertyNames();
239 	sal_Int32 nProps = aNames.getLength();
240 
241 	Sequence< Any > aValues( nProps );
242 	Any *pValue = aValues.getArray();
243 
244 	DBG_ASSERT( nProps == MAX_FLAGS_OFFSET + 1,
245 			"unexpected size of index" );
246 	if (nProps  &&  nProps == MAX_FLAGS_OFFSET + 1)
247 	{
248 		for (sal_uInt16 i = 0;  i < nProps;  ++i)
249 			pValue[i] <<= (sal_Bool) GetFlag(i);
250 		bSucc |= PutProperties( aNames, aValues );
251 	}
252 
253 	if (bSucc)
254 		SetModified( sal_False );
255 
256 	return bSucc;
257 }
258 
259 
260 //////////////////////////////////////////////////////////////////////
261 
SvtSearchOptions()262 SvtSearchOptions::SvtSearchOptions()
263 {
264 	pImpl = new SvtSearchOptions_Impl;
265 }
266 
267 
~SvtSearchOptions()268 SvtSearchOptions::~SvtSearchOptions()
269 {
270 	delete pImpl;
271 }
272 
273 
GetTransliterationFlags() const274 sal_Int32 SvtSearchOptions::GetTransliterationFlags() const
275 {
276 	sal_Int32 nRes = 0;
277 
278     if (!IsMatchCase()) // 'IsMatchCase' means act case sensitive
279 		nRes |= TransliterationModules_IGNORE_CASE;
280     if ( IsMatchFullHalfWidthForms())
281 		nRes |= TransliterationModules_IGNORE_WIDTH;
282     if ( IsMatchHiraganaKatakana())
283 		nRes |= TransliterationModules_IGNORE_KANA;
284     if ( IsMatchContractions())
285 		nRes |= TransliterationModules_ignoreSize_ja_JP;
286     if ( IsMatchMinusDashChoon())
287 		nRes |= TransliterationModules_ignoreMinusSign_ja_JP;
288     if ( IsMatchRepeatCharMarks())
289 		nRes |= TransliterationModules_ignoreIterationMark_ja_JP;
290     if ( IsMatchVariantFormKanji())
291 		nRes |= TransliterationModules_ignoreTraditionalKanji_ja_JP;
292     if ( IsMatchOldKanaForms())
293 		nRes |= TransliterationModules_ignoreTraditionalKana_ja_JP;
294     if ( IsMatchDiziDuzu())
295 		nRes |= TransliterationModules_ignoreZiZu_ja_JP;
296     if ( IsMatchBavaHafa())
297 		nRes |= TransliterationModules_ignoreBaFa_ja_JP;
298     if ( IsMatchTsithichiDhizi())
299 		nRes |= TransliterationModules_ignoreTiJi_ja_JP;
300     if ( IsMatchHyuiyuByuvyu())
301 		nRes |= TransliterationModules_ignoreHyuByu_ja_JP;
302     if ( IsMatchSesheZeje())
303 		nRes |= TransliterationModules_ignoreSeZe_ja_JP;
304     if ( IsMatchIaiya())
305 		nRes |= TransliterationModules_ignoreIandEfollowedByYa_ja_JP;
306     if ( IsMatchKiku())
307 		nRes |= TransliterationModules_ignoreKiKuFollowedBySa_ja_JP;
308 	if ( IsIgnorePunctuation())
309 		nRes |= TransliterationModules_ignoreSeparator_ja_JP;
310 	if ( IsIgnoreWhitespace())
311 		nRes |= TransliterationModules_ignoreSpace_ja_JP;
312 	if ( IsIgnoreProlongedSoundMark())
313 		nRes |= TransliterationModules_ignoreProlongedSoundMark_ja_JP;
314 	if ( IsIgnoreMiddleDot())
315 		nRes |= TransliterationModules_ignoreMiddleDot_ja_JP;
316 
317 	return nRes;
318 }
319 
320 
IsWholeWordsOnly() const321 sal_Bool SvtSearchOptions::IsWholeWordsOnly() const
322 {
323 	return pImpl->GetFlag( 0 );
324 }
325 
326 
SetWholeWordsOnly(sal_Bool bVal)327 void SvtSearchOptions::SetWholeWordsOnly( sal_Bool bVal )
328 {
329 	pImpl->SetFlag( 0, bVal );
330 }
331 
332 
IsBackwards() const333 sal_Bool SvtSearchOptions::IsBackwards() const
334 {
335 	return pImpl->GetFlag( 1 );
336 }
337 
338 
SetBackwards(sal_Bool bVal)339 void SvtSearchOptions::SetBackwards( sal_Bool bVal )
340 {
341 	pImpl->SetFlag( 1, bVal );
342 }
343 
344 
IsUseRegularExpression() const345 sal_Bool SvtSearchOptions::IsUseRegularExpression() const
346 {
347 	return pImpl->GetFlag( 2 );
348 }
349 
350 
SetUseRegularExpression(sal_Bool bVal)351 void SvtSearchOptions::SetUseRegularExpression( sal_Bool bVal )
352 {
353 	pImpl->SetFlag( 2, bVal );
354 }
355 
356 
IsSearchForStyles() const357 sal_Bool SvtSearchOptions::IsSearchForStyles() const
358 {
359 	return pImpl->GetFlag( 3 );
360 }
361 
362 
SetSearchForStyles(sal_Bool bVal)363 void SvtSearchOptions::SetSearchForStyles( sal_Bool bVal )
364 {
365 	pImpl->SetFlag( 3, bVal );
366 }
367 
368 
IsSimilaritySearch() const369 sal_Bool SvtSearchOptions::IsSimilaritySearch() const
370 {
371 	return pImpl->GetFlag( 4 );
372 }
373 
374 
SetSimilaritySearch(sal_Bool bVal)375 void SvtSearchOptions::SetSimilaritySearch( sal_Bool bVal )
376 {
377 	pImpl->SetFlag( 4, bVal );
378 }
379 
380 
IsUseAsianOptions() const381 sal_Bool SvtSearchOptions::IsUseAsianOptions() const
382 {
383 	return pImpl->GetFlag( 5 );
384 }
385 
386 
SetUseAsianOptions(sal_Bool bVal)387 void SvtSearchOptions::SetUseAsianOptions( sal_Bool bVal )
388 {
389 	pImpl->SetFlag( 5, bVal );
390 }
391 
392 
IsMatchCase() const393 sal_Bool SvtSearchOptions::IsMatchCase() const
394 {
395 	return pImpl->GetFlag( 6 );
396 }
397 
398 
SetMatchCase(sal_Bool bVal)399 void SvtSearchOptions::SetMatchCase( sal_Bool bVal )
400 {
401 	pImpl->SetFlag( 6, bVal );
402 }
403 
404 
IsMatchFullHalfWidthForms() const405 sal_Bool SvtSearchOptions::IsMatchFullHalfWidthForms() const
406 {
407 	return pImpl->GetFlag( 7 );
408 }
409 
410 
SetMatchFullHalfWidthForms(sal_Bool bVal)411 void SvtSearchOptions::SetMatchFullHalfWidthForms( sal_Bool bVal )
412 {
413 	pImpl->SetFlag( 7, bVal );
414 }
415 
416 
IsMatchHiraganaKatakana() const417 sal_Bool SvtSearchOptions::IsMatchHiraganaKatakana() const
418 {
419 	return pImpl->GetFlag( 8 );
420 }
421 
422 
SetMatchHiraganaKatakana(sal_Bool bVal)423 void SvtSearchOptions::SetMatchHiraganaKatakana( sal_Bool bVal )
424 {
425 	pImpl->SetFlag( 8, bVal );
426 }
427 
428 
IsMatchContractions() const429 sal_Bool SvtSearchOptions::IsMatchContractions() const
430 {
431 	return pImpl->GetFlag( 9 );
432 }
433 
434 
SetMatchContractions(sal_Bool bVal)435 void SvtSearchOptions::SetMatchContractions( sal_Bool bVal )
436 {
437 	pImpl->SetFlag( 9, bVal );
438 }
439 
440 
IsMatchMinusDashChoon() const441 sal_Bool SvtSearchOptions::IsMatchMinusDashChoon() const
442 {
443 	return pImpl->GetFlag( 10 );
444 }
445 
446 
SetMatchMinusDashChoon(sal_Bool bVal)447 void SvtSearchOptions::SetMatchMinusDashChoon( sal_Bool bVal )
448 {
449 	pImpl->SetFlag( 10, bVal );
450 }
451 
452 
IsMatchRepeatCharMarks() const453 sal_Bool SvtSearchOptions::IsMatchRepeatCharMarks() const
454 {
455 	return pImpl->GetFlag( 11 );
456 }
457 
458 
SetMatchRepeatCharMarks(sal_Bool bVal)459 void SvtSearchOptions::SetMatchRepeatCharMarks( sal_Bool bVal )
460 {
461 	pImpl->SetFlag( 11, bVal );
462 }
463 
464 
IsMatchVariantFormKanji() const465 sal_Bool SvtSearchOptions::IsMatchVariantFormKanji() const
466 {
467 	return pImpl->GetFlag( 12 );
468 }
469 
470 
SetMatchVariantFormKanji(sal_Bool bVal)471 void SvtSearchOptions::SetMatchVariantFormKanji( sal_Bool bVal )
472 {
473 	pImpl->SetFlag( 12, bVal );
474 }
475 
476 
IsMatchOldKanaForms() const477 sal_Bool SvtSearchOptions::IsMatchOldKanaForms() const
478 {
479 	return pImpl->GetFlag( 13 );
480 }
481 
482 
SetMatchOldKanaForms(sal_Bool bVal)483 void SvtSearchOptions::SetMatchOldKanaForms( sal_Bool bVal )
484 {
485 	pImpl->SetFlag( 13, bVal );
486 }
487 
488 
IsMatchDiziDuzu() const489 sal_Bool SvtSearchOptions::IsMatchDiziDuzu() const
490 {
491 	return pImpl->GetFlag( 14 );
492 }
493 
494 
SetMatchDiziDuzu(sal_Bool bVal)495 void SvtSearchOptions::SetMatchDiziDuzu( sal_Bool bVal )
496 {
497 	pImpl->SetFlag( 14, bVal );
498 }
499 
500 
IsMatchBavaHafa() const501 sal_Bool SvtSearchOptions::IsMatchBavaHafa() const
502 {
503 	return pImpl->GetFlag( 15 );
504 }
505 
506 
SetMatchBavaHafa(sal_Bool bVal)507 void SvtSearchOptions::SetMatchBavaHafa( sal_Bool bVal )
508 {
509 	pImpl->SetFlag( 15, bVal );
510 }
511 
512 
IsMatchTsithichiDhizi() const513 sal_Bool SvtSearchOptions::IsMatchTsithichiDhizi() const
514 {
515 	return pImpl->GetFlag( 16 );
516 }
517 
518 
SetMatchTsithichiDhizi(sal_Bool bVal)519 void SvtSearchOptions::SetMatchTsithichiDhizi( sal_Bool bVal )
520 {
521 	pImpl->SetFlag( 16, bVal );
522 }
523 
524 
IsMatchHyuiyuByuvyu() const525 sal_Bool SvtSearchOptions::IsMatchHyuiyuByuvyu() const
526 {
527 	return pImpl->GetFlag( 17 );
528 }
529 
530 
SetMatchHyuiyuByuvyu(sal_Bool bVal)531 void SvtSearchOptions::SetMatchHyuiyuByuvyu( sal_Bool bVal )
532 {
533 	pImpl->SetFlag( 17, bVal );
534 }
535 
536 
IsMatchSesheZeje() const537 sal_Bool SvtSearchOptions::IsMatchSesheZeje() const
538 {
539 	return pImpl->GetFlag( 18 );
540 }
541 
542 
SetMatchSesheZeje(sal_Bool bVal)543 void SvtSearchOptions::SetMatchSesheZeje( sal_Bool bVal )
544 {
545 	pImpl->SetFlag( 18, bVal );
546 }
547 
548 
IsMatchIaiya() const549 sal_Bool SvtSearchOptions::IsMatchIaiya() const
550 {
551 	return pImpl->GetFlag( 19 );
552 }
553 
554 
SetMatchIaiya(sal_Bool bVal)555 void SvtSearchOptions::SetMatchIaiya( sal_Bool bVal )
556 {
557 	pImpl->SetFlag( 19, bVal );
558 }
559 
560 
IsMatchKiku() const561 sal_Bool SvtSearchOptions::IsMatchKiku() const
562 {
563 	return pImpl->GetFlag( 20 );
564 }
565 
566 
SetMatchKiku(sal_Bool bVal)567 void SvtSearchOptions::SetMatchKiku( sal_Bool bVal )
568 {
569 	pImpl->SetFlag( 20, bVal );
570 }
571 
572 
IsIgnorePunctuation() const573 sal_Bool SvtSearchOptions::IsIgnorePunctuation() const
574 {
575 	return pImpl->GetFlag( 21 );
576 }
577 
578 
SetIgnorePunctuation(sal_Bool bVal)579 void SvtSearchOptions::SetIgnorePunctuation( sal_Bool bVal )
580 {
581 	pImpl->SetFlag( 21, bVal );
582 }
583 
584 
IsIgnoreWhitespace() const585 sal_Bool SvtSearchOptions::IsIgnoreWhitespace() const
586 {
587 	return pImpl->GetFlag( 22 );
588 }
589 
590 
SetIgnoreWhitespace(sal_Bool bVal)591 void SvtSearchOptions::SetIgnoreWhitespace( sal_Bool bVal )
592 {
593 	pImpl->SetFlag( 22, bVal );
594 }
595 
596 
IsIgnoreProlongedSoundMark() const597 sal_Bool SvtSearchOptions::IsIgnoreProlongedSoundMark() const
598 {
599 	return pImpl->GetFlag( 23 );
600 }
601 
602 
SetIgnoreProlongedSoundMark(sal_Bool bVal)603 void SvtSearchOptions::SetIgnoreProlongedSoundMark( sal_Bool bVal )
604 {
605 	pImpl->SetFlag( 23, bVal );
606 }
607 
608 
IsIgnoreMiddleDot() const609 sal_Bool SvtSearchOptions::IsIgnoreMiddleDot() const
610 {
611 	return pImpl->GetFlag( 24 );
612 }
613 
614 
SetIgnoreMiddleDot(sal_Bool bVal)615 void SvtSearchOptions::SetIgnoreMiddleDot( sal_Bool bVal )
616 {
617 	pImpl->SetFlag( 24, bVal );
618 }
619 
IsNotes() const620 sal_Bool SvtSearchOptions::IsNotes() const
621 {
622         return pImpl->GetFlag( 25 );
623 }
624 
625 
SetNotes(sal_Bool bVal)626 void SvtSearchOptions::SetNotes( sal_Bool bVal )
627 {
628         pImpl->SetFlag( 25, bVal );
629 }
630 
631 //////////////////////////////////////////////////////////////////////
632 
633