Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/unodialogs/textconversiondlgs/chinese_dictionarydialog.cxx
Line
Count
Source
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
21
#include "chinese_dictionarydialog.hxx"
22
#include <com/sun/star/i18n/TextConversionOption.hpp>
23
#include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
24
#include <com/sun/star/linguistic2/ConversionPropertyType.hpp>
25
#include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
26
#include <com/sun/star/linguistic2/XConversionPropertyType.hpp>
27
#include <com/sun/star/util/XFlushable.hpp>
28
#include <com/sun/star/lang/Locale.hpp>
29
#include <comphelper/processfactory.hxx>
30
#include <o3tl/safeint.hxx>
31
#include <unotools/lingucfg.hxx>
32
#include <unotools/linguprops.hxx>
33
#include <vcl/vclenum.hxx>
34
#include <vcl/weld/Builder.hxx>
35
#include <vcl/weld/weldutils.hxx>
36
#include <utility>
37
#include <osl/diagnose.h>
38
39
namespace textconversiondlgs
40
{
41
42
using namespace ::com::sun::star;
43
using namespace ::com::sun::star::uno;
44
45
DictionaryList::DictionaryList(std::unique_ptr<weld::TreeView> xControl)
46
0
    : m_xControl(std::move(xControl))
47
0
    , m_xIter(m_xControl->make_iterator())
48
0
    , m_pED_Term(nullptr)
49
0
    , m_pED_Mapping(nullptr)
50
0
    , m_pLB_Property(nullptr)
51
0
{
52
0
    m_xControl->make_sorted();
53
0
}
54
55
OUString DictionaryList::getPropertyTypeName( sal_Int16 nConversionPropertyType ) const
56
0
{
57
0
    if (!m_pLB_Property || !m_pLB_Property->get_count())
58
0
        return OUString();
59
60
0
    sal_uInt16 nPos = static_cast<sal_uInt16>( nConversionPropertyType )-1;
61
0
    if (nPos < m_pLB_Property->get_count())
62
0
        return m_pLB_Property->get_text(nPos);
63
0
    return m_pLB_Property->get_text(0);
64
0
}
65
66
void DictionaryList::save()
67
0
{
68
0
    if (!m_xDictionary.is())
69
0
        return;
70
71
0
    Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
72
73
0
    sal_Int32 nN;
74
0
    DictionaryEntry* pE;
75
76
0
    for( nN = m_aToBeDeleted.size(); nN--; )
77
0
    {
78
0
        pE = m_aToBeDeleted[nN];
79
0
        m_xDictionary->removeEntry( pE->m_aTerm, pE->m_aMapping );
80
0
    }
81
0
    int nRowCount = m_xControl->n_children();
82
0
    for( nN = nRowCount; nN--; )
83
0
    {
84
0
        pE = getEntryOnPos( nN );
85
0
        if(pE->m_bNewEntry)
86
0
        {
87
0
            try
88
0
            {
89
0
                m_xDictionary->addEntry( pE->m_aTerm, pE->m_aMapping );
90
0
                xPropertyType->setPropertyType( pE->m_aTerm, pE->m_aMapping, pE->m_nConversionPropertyType );
91
0
            }
92
0
            catch( uno::Exception& )
93
0
            {
94
95
0
            }
96
0
        }
97
0
    }
98
0
    Reference< util::XFlushable > xFlush( m_xDictionary, uno::UNO_QUERY );
99
0
    if( xFlush.is() )
100
0
        xFlush->flush();
101
0
}
102
103
void DictionaryList::deleteAll()
104
0
{
105
0
    sal_Int32 nN;
106
0
    int nRowCount = m_xControl->n_children();
107
0
    for( nN = nRowCount; nN--; )
108
0
        deleteEntryOnPos( nN  );
109
0
    for( nN = m_aToBeDeleted.size(); nN--; )
110
0
    {
111
0
        DictionaryEntry* pE = m_aToBeDeleted[nN];
112
0
        delete pE;
113
0
    }
114
0
    m_aToBeDeleted.clear();
115
0
}
116
117
void DictionaryList::refillFromDictionary( sal_Int32 nTextConversionOptions )
118
0
{
119
0
    deleteAll();
120
121
0
    if(!m_xDictionary.is())
122
0
        return;
123
124
0
    const Sequence< OUString > aLeftList(  m_xDictionary->getConversionEntries( linguistic2::ConversionDirection_FROM_LEFT ) );
125
126
0
    Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
127
128
0
    OUString aRight;
129
0
    sal_Int16 nConversionPropertyType;
130
131
0
    for(const OUString& aLeft : aLeftList)
132
0
    {
133
0
        Sequence< OUString > aRightList( m_xDictionary->getConversions(
134
0
            aLeft, 0, aLeft.getLength()
135
0
            , linguistic2::ConversionDirection_FROM_LEFT, nTextConversionOptions ) );
136
137
0
        if(aRightList.getLength()!=1)
138
0
        {
139
0
            OSL_FAIL("The Chinese Translation Dictionary should have exactly one Mapping for each term.");
140
0
            continue;
141
0
        }
142
143
0
        aRight = aRightList[0];
144
0
        nConversionPropertyType = linguistic2::ConversionPropertyType::OTHER;
145
0
        if(xPropertyType.is())
146
0
            nConversionPropertyType = xPropertyType->getPropertyType(aLeft, aRight);
147
148
0
        DictionaryEntry* pEntry = new DictionaryEntry( aLeft, aRight, nConversionPropertyType );
149
150
0
        m_xControl->append(m_xIter.get());
151
0
        m_xControl->set_text(*m_xIter, pEntry->m_aTerm, 0);
152
0
        m_xControl->set_text(*m_xIter, pEntry->m_aMapping, 1);
153
0
        m_xControl->set_text(*m_xIter, getPropertyTypeName(pEntry->m_nConversionPropertyType), 2);
154
0
        m_xControl->set_id(*m_xIter, weld::toId(pEntry));
155
0
    }
156
0
}
157
158
DictionaryEntry* DictionaryList::getFirstSelectedEntry() const
159
0
{
160
0
    DictionaryEntry* pRet=nullptr;
161
0
    int nN = m_xControl->get_selected_index();
162
0
    if (nN != -1)
163
0
        pRet = getEntryOnPos( nN );
164
0
    return pRet;
165
0
}
166
167
DictionaryEntry* DictionaryList::getEntryOnPos(sal_Int32 nPos) const
168
0
{
169
0
    OUString sLBEntry = m_xControl->get_id(nPos);
170
0
    return weld::fromId<DictionaryEntry*>(sLBEntry);
171
0
}
172
173
DictionaryEntry* DictionaryList::getTermEntry( std::u16string_view rTerm ) const
174
0
{
175
0
    int nRowCount = m_xControl->n_children();
176
0
    for( sal_Int32 nN = nRowCount; nN--; )
177
0
    {
178
0
        DictionaryEntry* pE = getEntryOnPos( nN );
179
0
        if( pE && rTerm == pE->m_aTerm )
180
0
            return pE;
181
0
    }
182
0
    return nullptr;
183
0
}
184
185
bool DictionaryList::hasTerm( std::u16string_view rTerm ) const
186
0
{
187
0
    return getTermEntry(rTerm) !=nullptr ;
188
0
}
189
190
void DictionaryList::addEntry(const OUString& rTerm, const OUString& rMapping,
191
                              sal_Int16 nConversionPropertyType, int nPos)
192
0
{
193
0
    if( hasTerm( rTerm ) )
194
0
        return;
195
196
0
    DictionaryEntry* pEntry = new DictionaryEntry( rTerm, rMapping, nConversionPropertyType, true );
197
0
    m_xControl->insert(nPos, m_xIter.get());
198
0
    m_xControl->set_text(*m_xIter, pEntry->m_aTerm, 0);
199
0
    m_xControl->set_text(*m_xIter, pEntry->m_aMapping, 1);
200
0
    m_xControl->set_text(*m_xIter, getPropertyTypeName(pEntry->m_nConversionPropertyType), 2);
201
0
    m_xControl->set_id(*m_xIter, weld::toId(pEntry));
202
0
    m_xControl->select(*m_xIter);
203
0
}
204
205
void DictionaryList::deleteEntryOnPos( sal_Int32 nPos  )
206
0
{
207
0
    DictionaryEntry* pEntry = getEntryOnPos( nPos );
208
0
    m_xControl->remove(nPos);
209
0
    if (pEntry)
210
0
    {
211
0
        if( pEntry->m_bNewEntry )
212
0
            delete pEntry;
213
0
        else
214
0
            m_aToBeDeleted.push_back( pEntry );
215
0
    }
216
0
}
217
218
int DictionaryList::deleteEntries( std::u16string_view rTerm )
219
0
{
220
0
    int nPos = -1;
221
0
    int nRowCount = m_xControl->n_children();
222
0
    for (sal_Int32 nN = nRowCount; nN--;)
223
0
    {
224
0
        DictionaryEntry* pCurEntry = getEntryOnPos( nN );
225
0
        if( rTerm == pCurEntry->m_aTerm )
226
0
        {
227
0
            nPos = nN;
228
0
            m_xControl->remove(nN);
229
0
            if( pCurEntry->m_bNewEntry )
230
0
                delete pCurEntry;
231
0
            else
232
0
                m_aToBeDeleted.push_back( pCurEntry );
233
0
        }
234
0
    }
235
0
    return nPos;
236
0
}
237
238
DictionaryEntry::DictionaryEntry( OUString aTerm, OUString aMapping
239
                    , sal_Int16 nConversionPropertyType
240
                    , bool bNewEntry )
241
0
        : m_aTerm(std::move( aTerm ))
242
0
        , m_aMapping(std::move( aMapping ))
243
0
        , m_nConversionPropertyType( nConversionPropertyType )
244
0
        , m_bNewEntry( bNewEntry )
245
0
{
246
0
    if( m_nConversionPropertyType == 0 )
247
0
        m_nConversionPropertyType = 1;
248
0
}
249
250
DictionaryEntry::~DictionaryEntry()
251
0
{
252
0
}
253
254
IMPL_LINK_NOARG(ChineseDictionaryDialog, SizeAllocHdl, const Size&, void)
255
0
{
256
0
    DictionaryList* pControl = m_xCT_DictionaryToTraditional->get_visible() ?
257
0
                                m_xCT_DictionaryToTraditional.get() :
258
0
                                m_xCT_DictionaryToSimplified.get();
259
0
    std::vector<int> aWidths;
260
0
    int x1, x2, y, width, height;
261
0
    if (!m_xED_Mapping->get_extents_relative_to(pControl->get_widget(), x1, y, width, height))
262
0
        return;
263
0
    aWidths.push_back(x1);
264
0
    if (!m_xLB_Property->get_extents_relative_to(pControl->get_widget(), x2, y, width, height))
265
0
        return;
266
0
    aWidths.push_back(x2 - x1);
267
0
    m_xCT_DictionaryToTraditional->get_widget().set_column_fixed_widths(aWidths);
268
0
    m_xCT_DictionaryToSimplified->get_widget().set_column_fixed_widths(aWidths);
269
0
}
270
271
void DictionaryList::init(const Reference< linguistic2::XConversionDictionary>& xDictionary,
272
    weld::Entry *pED_Term, weld::Entry *pED_Mapping, weld::ComboBox *pLB_Property)
273
0
{
274
0
    if (m_xDictionary.is())
275
0
        return;
276
277
0
    m_xDictionary = xDictionary;
278
279
0
    m_pED_Term = pED_Term;
280
0
    m_pED_Mapping = pED_Mapping;
281
0
    m_pLB_Property = pLB_Property;
282
283
0
    m_xControl->set_sort_column(0);
284
0
    m_xControl->set_sort_indicator(TRISTATE_TRUE, 0);
285
286
0
    std::vector<int> aWidths
287
0
    {
288
0
        o3tl::narrowing<int>(m_pED_Term->get_preferred_size().Width()),
289
0
        o3tl::narrowing<int>(m_pED_Mapping->get_preferred_size().Width())
290
0
    };
291
0
    m_xControl->set_column_fixed_widths(aWidths);
292
0
}
293
294
void ChineseDictionaryDialog::initDictionaryControl(DictionaryList *pList,
295
    const Reference< linguistic2::XConversionDictionary>& xDictionary)
296
0
{
297
    //set widgets to track the width of for columns
298
0
    pList->init(xDictionary,
299
0
        m_xED_Term.get(), m_xED_Mapping.get(), m_xLB_Property.get());
300
0
}
301
302
ChineseDictionaryDialog::ChineseDictionaryDialog(weld::Window* pParent)
303
0
    : GenericDialogController(pParent, u"svx/ui/chinesedictionary.ui"_ustr, u"ChineseDictionaryDialog"_ustr)
304
0
    , m_nTextConversionOptions(i18n::TextConversionOption::NONE)
305
0
    , m_xRB_To_Simplified(m_xBuilder->weld_radio_button(u"tradtosimple"_ustr))
306
0
    , m_xRB_To_Traditional(m_xBuilder->weld_radio_button(u"simpletotrad"_ustr))
307
0
    , m_xCB_Reverse(m_xBuilder->weld_check_button(u"reverse"_ustr))
308
0
    , m_xED_Term(m_xBuilder->weld_entry(u"term"_ustr))
309
0
    , m_xED_Mapping(m_xBuilder->weld_entry(u"mapping"_ustr))
310
0
    , m_xLB_Property(m_xBuilder->weld_combo_box(u"property"_ustr))
311
0
    , m_xCT_DictionaryToSimplified(new DictionaryList(m_xBuilder->weld_tree_view(u"tradtosimpleview"_ustr)))
312
0
    , m_xCT_DictionaryToTraditional(new DictionaryList(m_xBuilder->weld_tree_view(u"simpletotradview"_ustr)))
313
0
    , m_xPB_Add(m_xBuilder->weld_button(u"add"_ustr))
314
0
    , m_xPB_Modify(m_xBuilder->weld_button(u"modify"_ustr))
315
0
    , m_xPB_Delete(m_xBuilder->weld_button(u"delete"_ustr))
316
0
{
317
0
    m_xCT_DictionaryToSimplified->set_size_request(-1, m_xCT_DictionaryToSimplified->get_height_rows(8));
318
0
    m_xCT_DictionaryToTraditional->set_size_request(-1, m_xCT_DictionaryToTraditional->get_height_rows(8));
319
320
0
    SvtLinguConfig  aLngCfg;
321
0
    bool bValue;
322
0
    Any aAny( aLngCfg.GetProperty( UPN_IS_REVERSE_MAPPING ) );
323
0
    if( aAny >>= bValue )
324
0
        m_xCB_Reverse->set_active( bValue );
325
326
0
    m_xLB_Property->set_active(0);
327
328
0
    Reference< linguistic2::XConversionDictionary > xDictionary_To_Simplified;
329
0
    Reference< linguistic2::XConversionDictionary > xDictionary_To_Traditional;
330
    //get dictionaries
331
0
    {
332
0
        css::uno::Reference<css::uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
333
0
        if (xContext.is())
334
0
        {
335
0
            Reference< linguistic2::XConversionDictionaryList > xDictionaryList = linguistic2::ConversionDictionaryList::create(xContext);
336
0
            Reference< container::XNameContainer > xContainer( xDictionaryList->getDictionaryContainer() );
337
0
            if(xContainer.is())
338
0
            {
339
0
                try
340
0
                {
341
0
                    OUString aNameTo_Simplified(u"ChineseT2S"_ustr);
342
0
                    OUString aNameTo_Traditional(u"ChineseS2T"_ustr);
343
0
                    lang::Locale aLocale;
344
0
                    aLocale.Language = "zh";
345
346
0
                    if( xContainer->hasByName( aNameTo_Simplified ) )
347
0
                        xDictionary_To_Simplified.set(
348
0
                                xContainer->getByName( aNameTo_Simplified ), UNO_QUERY );
349
0
                    else
350
0
                    {
351
0
                        aLocale.Country = "TW";
352
0
                        xDictionary_To_Simplified =
353
0
                                xDictionaryList->addNewDictionary( aNameTo_Simplified
354
0
                                    , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
355
0
                                );
356
0
                    }
357
0
                    if (xDictionary_To_Simplified.is())
358
0
                        xDictionary_To_Simplified->setActive( true );
359
360
361
0
                    if( xContainer->hasByName( aNameTo_Traditional ) )
362
0
                        xDictionary_To_Traditional.set(
363
0
                                xContainer->getByName( aNameTo_Traditional ), UNO_QUERY );
364
0
                    else
365
0
                    {
366
0
                        aLocale.Country = "CN";
367
0
                        xDictionary_To_Traditional =
368
0
                                xDictionaryList->addNewDictionary( aNameTo_Traditional
369
0
                                    ,aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE);
370
0
                    }
371
0
                    if (xDictionary_To_Traditional.is())
372
0
                        xDictionary_To_Traditional->setActive( true );
373
374
0
                }
375
0
                catch(const uno::Exception& )
376
0
                {
377
0
                }
378
0
            }
379
0
        }
380
0
    }
381
382
    //init dictionary controls
383
0
    initDictionaryControl(m_xCT_DictionaryToSimplified.get(), xDictionary_To_Simplified);
384
0
    initDictionaryControl(m_xCT_DictionaryToTraditional.get(), xDictionary_To_Traditional);
385
386
    //set hdl
387
0
    m_xCT_DictionaryToSimplified->connect_column_header_clicked(
388
0
        LINK(this, ChineseDictionaryDialog, ToSimplifiedHeaderBarClick));
389
0
    m_xCT_DictionaryToTraditional->connect_column_header_clicked(
390
0
        LINK(this, ChineseDictionaryDialog, ToTraditionalHeaderBarClick));
391
392
0
    updateAfterDirectionChange();
393
394
0
    m_xED_Term->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
395
0
    m_xED_Mapping->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
396
0
    m_xLB_Property->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsListBoxHdl ) );
397
398
0
    m_xRB_To_Simplified->connect_toggled( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
399
400
0
    m_xCT_DictionaryToSimplified->connect_changed( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
401
0
    m_xCT_DictionaryToTraditional->connect_changed( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
402
403
0
    m_xPB_Add->connect_clicked( LINK( this, ChineseDictionaryDialog, AddHdl ) );
404
0
    m_xPB_Modify->connect_clicked( LINK( this, ChineseDictionaryDialog, ModifyHdl ) );
405
0
    m_xPB_Delete->connect_clicked( LINK( this, ChineseDictionaryDialog, DeleteHdl ) );
406
407
0
    m_xED_Mapping->connect_size_allocate(LINK(this, ChineseDictionaryDialog, SizeAllocHdl));
408
0
    m_xLB_Property->connect_size_allocate(LINK(this, ChineseDictionaryDialog, SizeAllocHdl));
409
0
}
410
411
ChineseDictionaryDialog::~ChineseDictionaryDialog()
412
0
{
413
0
}
414
415
void ChineseDictionaryDialog::setDirectionAndTextConversionOptions( bool bDirectionToSimplified, sal_Int32 nTextConversionOptions /*i18n::TextConversionOption*/ )
416
0
{
417
0
    if( bDirectionToSimplified == m_xRB_To_Simplified->get_active()
418
0
        && nTextConversionOptions == m_nTextConversionOptions )
419
0
        return;
420
421
0
    m_nTextConversionOptions = nTextConversionOptions;
422
423
0
    if (bDirectionToSimplified)
424
0
        m_xRB_To_Simplified->set_active(true);
425
0
    else
426
0
        m_xRB_To_Traditional->set_active(true);
427
0
    updateAfterDirectionChange();
428
0
}
429
430
IMPL_LINK_NOARG(ChineseDictionaryDialog, DirectionHdl, weld::Toggleable&, void)
431
0
{
432
0
    updateAfterDirectionChange();
433
0
}
434
435
void ChineseDictionaryDialog::updateAfterDirectionChange()
436
0
{
437
0
    Reference< linguistic2::XConversionDictionary > xDictionary;
438
439
0
    if (m_xRB_To_Simplified->get_active())
440
0
    {
441
0
        m_xCT_DictionaryToTraditional->hide();
442
0
        m_xCT_DictionaryToSimplified->show();
443
0
        xDictionary = m_xCT_DictionaryToSimplified->m_xDictionary;
444
0
    }
445
0
    else
446
0
    {
447
0
        m_xCT_DictionaryToSimplified->hide();
448
0
        m_xCT_DictionaryToTraditional->show();
449
0
        xDictionary = m_xCT_DictionaryToTraditional->m_xDictionary;
450
0
    }
451
452
0
    updateButtons();
453
0
}
454
455
IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsListBoxHdl, weld::ComboBox&, void)
456
0
{
457
0
    updateButtons();
458
0
}
459
460
IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsHdl, weld::TextWidget&, void)
461
0
{
462
0
    updateButtons();
463
0
}
464
465
IMPL_LINK_NOARG(ChineseDictionaryDialog, MappingSelectHdl, weld::ItemView&, void)
466
0
{
467
0
    DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
468
0
    if (pE)
469
0
    {
470
0
        m_xED_Term->set_text( pE->m_aTerm );
471
0
        m_xED_Mapping->set_text( pE->m_aMapping );
472
0
        sal_Int16 nPos = pE->m_nConversionPropertyType-1;
473
0
        if (nPos<0 || nPos>=m_xLB_Property->get_count())
474
0
            nPos=0;
475
0
        if (m_xLB_Property->get_count())
476
0
            m_xLB_Property->set_active(nPos);
477
0
    }
478
479
0
    updateButtons();
480
0
}
481
482
bool ChineseDictionaryDialog::isEditFieldsHaveContent() const
483
0
{
484
0
    return !m_xED_Term->get_text().isEmpty() && !m_xED_Mapping->get_text().isEmpty();
485
0
}
486
487
bool ChineseDictionaryDialog::isEditFieldsContentEqualsSelectedListContent() const
488
0
{
489
0
    DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
490
0
    if( pE )
491
0
    {
492
0
        if (pE->m_aTerm != m_xED_Term->get_text())
493
0
            return false;
494
0
        if (pE->m_aMapping != m_xED_Mapping->get_text())
495
0
            return false;
496
0
        if (pE->m_nConversionPropertyType != m_xLB_Property->get_active() + 1)
497
0
            return false;
498
0
        return true;
499
0
    }
500
0
    return false;
501
0
}
502
503
const DictionaryList& ChineseDictionaryDialog::getActiveDictionary() const
504
0
{
505
0
    if( m_xRB_To_Traditional->get_active() )
506
0
        return *m_xCT_DictionaryToTraditional;
507
0
    return *m_xCT_DictionaryToSimplified;
508
0
}
509
510
DictionaryList& ChineseDictionaryDialog::getActiveDictionary()
511
0
{
512
0
    if( m_xRB_To_Traditional->get_active() )
513
0
        return *m_xCT_DictionaryToTraditional;
514
0
    return *m_xCT_DictionaryToSimplified;
515
0
}
516
517
const DictionaryList& ChineseDictionaryDialog::getReverseDictionary() const
518
0
{
519
0
    if( m_xRB_To_Traditional->get_active() )
520
0
        return *m_xCT_DictionaryToSimplified;
521
0
    return *m_xCT_DictionaryToTraditional;
522
0
}
523
524
DictionaryList& ChineseDictionaryDialog::getReverseDictionary()
525
0
{
526
0
    if( m_xRB_To_Traditional->get_active() )
527
0
        return *m_xCT_DictionaryToSimplified;
528
0
    return *m_xCT_DictionaryToTraditional;
529
0
}
530
531
void ChineseDictionaryDialog::updateButtons()
532
0
{
533
0
    bool bAdd = isEditFieldsHaveContent() && !getActiveDictionary().hasTerm(m_xED_Term->get_text());
534
0
    m_xPB_Add->set_sensitive( bAdd );
535
536
0
    m_xPB_Delete->set_sensitive(!bAdd && getActiveDictionary().get_selected_index() != -1);
537
538
0
    bool bModify = false;
539
0
    {
540
0
        DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
541
0
        bModify = !bAdd && pFirstSelectedEntry && pFirstSelectedEntry->m_aTerm == m_xED_Term->get_text();
542
0
        if( bModify && isEditFieldsContentEqualsSelectedListContent() )
543
0
            bModify = false;
544
0
    }
545
0
    m_xPB_Modify->set_sensitive( bModify );
546
0
}
547
548
IMPL_LINK_NOARG(ChineseDictionaryDialog, AddHdl, weld::Button&, void)
549
0
{
550
0
    if( !isEditFieldsHaveContent() )
551
0
        return;
552
553
0
    sal_Int16 nConversionPropertyType = m_xLB_Property->get_active() + 1;
554
555
0
    getActiveDictionary().addEntry( m_xED_Term->get_text(), m_xED_Mapping->get_text(), nConversionPropertyType );
556
557
0
    if( m_xCB_Reverse->get_active() )
558
0
    {
559
0
        getReverseDictionary().deleteEntries( m_xED_Mapping->get_text() );
560
0
        getReverseDictionary().addEntry( m_xED_Mapping->get_text(), m_xED_Term->get_text(), nConversionPropertyType );
561
0
    }
562
563
0
    updateButtons();
564
0
}
565
566
IMPL_LINK_NOARG(ChineseDictionaryDialog, ModifyHdl, weld::Button&, void)
567
0
{
568
0
    OUString aTerm( m_xED_Term->get_text() );
569
0
    OUString aMapping( m_xED_Mapping->get_text() );
570
0
    sal_Int16 nConversionPropertyType = m_xLB_Property->get_active() + 1;
571
572
0
    DictionaryList& rActive  = getActiveDictionary();
573
0
    DictionaryList& rReverse = getReverseDictionary();
574
575
0
    DictionaryEntry* pE = rActive.getFirstSelectedEntry();
576
0
    if( pE && pE->m_aTerm != aTerm )
577
0
        return;
578
579
0
    if( pE )
580
0
    {
581
0
        if( pE->m_aMapping != aMapping || pE->m_nConversionPropertyType != nConversionPropertyType )
582
0
        {
583
0
            if( m_xCB_Reverse->get_active() )
584
0
            {
585
0
                rReverse.deleteEntries( pE->m_aMapping );
586
0
                int nPos = rReverse.deleteEntries( aMapping );
587
0
                rReverse.addEntry( aMapping, aTerm, nConversionPropertyType, nPos );
588
0
            }
589
590
0
            int nPos = rActive.deleteEntries( aTerm );
591
0
            rActive.addEntry( aTerm, aMapping, nConversionPropertyType, nPos );
592
0
        }
593
0
    }
594
595
0
    updateButtons();
596
0
}
597
598
IMPL_LINK_NOARG(ChineseDictionaryDialog, DeleteHdl, weld::Button&, void)
599
0
{
600
0
    DictionaryList& rActive  = getActiveDictionary();
601
0
    DictionaryList& rReverse = getReverseDictionary();
602
603
0
    int nEntry = rActive.get_selected_index();
604
0
    if (nEntry != -1)
605
0
    {
606
0
        DictionaryEntry* pEntry = rActive.getEntryOnPos(nEntry);
607
0
        if (pEntry)
608
0
        {
609
0
            OUString aMapping = pEntry->m_aMapping;
610
0
            rActive.deleteEntryOnPos(nEntry);
611
0
            if (m_xCB_Reverse->get_active())
612
0
                rReverse.deleteEntries(aMapping);
613
0
        }
614
0
    }
615
616
0
    updateButtons();
617
0
}
618
619
short ChineseDictionaryDialog::run()
620
0
{
621
0
    sal_Int32 nTextConversionOptions = m_nTextConversionOptions;
622
0
    if(m_nTextConversionOptions & i18n::TextConversionOption::USE_CHARACTER_VARIANTS )
623
0
        nTextConversionOptions = nTextConversionOptions^i18n::TextConversionOption::USE_CHARACTER_VARIANTS ;
624
625
0
    m_xCT_DictionaryToSimplified->refillFromDictionary( nTextConversionOptions );
626
0
    m_xCT_DictionaryToTraditional->refillFromDictionary( m_nTextConversionOptions );
627
628
0
    short nRet = GenericDialogController::run();
629
630
0
    if( nRet == RET_OK )
631
0
    {
632
        //save settings to configuration
633
0
        SvtLinguConfig  aLngCfg;
634
0
        aLngCfg.SetProperty( UPN_IS_REVERSE_MAPPING, uno::Any(m_xCB_Reverse->get_active()) );
635
636
0
        m_xCT_DictionaryToSimplified->save();
637
0
        m_xCT_DictionaryToTraditional->save();
638
0
    }
639
640
0
    m_xCT_DictionaryToSimplified->deleteAll();
641
0
    m_xCT_DictionaryToTraditional->deleteAll();
642
643
0
    return nRet;
644
0
}
645
646
void ChineseDictionaryDialog::HeaderBarClick(DictionaryList& rList, int nColumn)
647
0
{
648
0
    bool bSortAtoZ = rList.get_sort_order();
649
650
    //set new arrow positions in headerbar
651
0
    if (nColumn == rList.get_sort_column())
652
0
    {
653
0
        bSortAtoZ = !bSortAtoZ;
654
0
        rList.set_sort_order(bSortAtoZ);
655
0
    }
656
0
    else
657
0
    {
658
0
        rList.set_sort_indicator(TRISTATE_INDET, rList.get_sort_column());
659
0
        rList.set_sort_column(nColumn);
660
0
    }
661
662
    //sort lists
663
0
    rList.set_sort_indicator(bSortAtoZ ? TRISTATE_TRUE : TRISTATE_FALSE, nColumn);
664
0
}
665
666
IMPL_LINK(ChineseDictionaryDialog, ToSimplifiedHeaderBarClick, int, nColumn, void)
667
0
{
668
0
    HeaderBarClick(*m_xCT_DictionaryToSimplified, nColumn);
669
0
}
670
671
IMPL_LINK(ChineseDictionaryDialog, ToTraditionalHeaderBarClick, int, nColumn, void)
672
0
{
673
0
    HeaderBarClick(*m_xCT_DictionaryToTraditional, nColumn);
674
0
}
675
676
} //end namespace
677
678
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */