summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/dbgui/scuiimoptdlg.cxx
blob: 2ddbd56c7afefe91be49de81857f78fea9fc229b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#undef SC_DLLIMPLEMENTATION

#include <scuiimoptdlg.hxx>
#include <scresid.hxx>
#include <strings.hrc>
#include <strings.hxx>
#include <officecfg/Office/Calc.hxx>
#include <osl/thread.h>
#include <rtl/tencinfo.h>
#include <imoptdlg.hxx>
#include <svx/txencbox.hxx>

// ScDelimiterTable

class ScDelimiterTable
{
public:
    explicit ScDelimiterTable( const OUString& rDelTab )
            :   theDelTab ( rDelTab ),
                nDelIdx   ( 0 )
            {}

    sal_uInt16  GetCode( const OUString& rDelimiter ) const;
    OUString  GetDelimiter( sal_Unicode nCode ) const;

    OUString  FirstDel()  { nDelIdx = 0; return theDelTab.getToken( 0, cSep, nDelIdx ); }
    OUString  NextDel()   { return theDelTab.getToken( 1, cSep, nDelIdx ); }

private:
    const OUString      theDelTab;
    static constexpr sal_Unicode cSep {'\t'};
    sal_Int32           nDelIdx;
};

sal_uInt16 ScDelimiterTable::GetCode( const OUString& rDel ) const
{
    if (!theDelTab.isEmpty())
    {
        sal_Int32 nIdx {0};

        // Check even tokens: start from 0 and then skip 1 token at each iteration
        if (rDel != theDelTab.getToken( 0, cSep, nIdx ))
            while (nIdx>0 && rDel != theDelTab.getToken( 1, cSep, nIdx ));

        if (nIdx>0)
            return static_cast<sal_Unicode>(theDelTab.getToken( 0, cSep, nIdx ).toInt32());
    }

    return 0;
}

OUString ScDelimiterTable::GetDelimiter( sal_Unicode nCode ) const
{
    if (!theDelTab.isEmpty())
    {
        sal_Int32 nIdx {0};
        // Check odd tokens: start from 1 and then skip 1 token at each iteration
        do
        {
            sal_Int32 nPrevIdx {nIdx};
            if (nCode == static_cast<sal_Unicode>(theDelTab.getToken( 1, cSep, nIdx ).toInt32()))
                return theDelTab.getToken( 0, cSep, nPrevIdx );
        }
        while (nIdx>0);
    }

    return OUString();
}

void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
{
    if (m_bIsAsciiImport)
        m_xLbCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
    else
        m_xTvCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
}

void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
{
    if (m_bIsAsciiImport)
        m_xLbCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
    else
        m_xTvCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
}

// ScImportOptionsDlg
ScImportOptionsDlg::ScImportOptionsDlg(weld::Window* pParent, bool bAscii,
                                       const ScImportOptions*  pOptions,
                                       const OUString* pStrTitle,
                                       bool bMultiByte, bool bOnlyDbtoolsEncodings,
                                       bool bImport)
    : GenericDialogController(pParent, "modules/scalc/ui/imoptdialog.ui", "ImOptDialog")
    , m_bIsAsciiImport(bAscii)
    , m_xFieldFrame(m_xBuilder->weld_frame("fieldframe"))
    , m_xFtCharset(m_xBuilder->weld_label("charsetft"))
    , m_xEncGrid(m_xBuilder->weld_widget("grid2"))
    , m_xFtFieldSep(m_xBuilder->weld_label("fieldft"))
    , m_xEdFieldSep(m_xBuilder->weld_combo_box("field"))
    , m_xFtTextSep(m_xBuilder->weld_label("textft"))
    , m_xEdTextSep(m_xBuilder->weld_combo_box("text"))
    , m_xCbShown(m_xBuilder->weld_check_button("asshown"))
    , m_xCbFormulas(m_xBuilder->weld_check_button("formulas"))
    , m_xCbQuoteAll(m_xBuilder->weld_check_button("quoteall"))
    , m_xCbFixed(m_xBuilder->weld_check_button("fixedwidth"))
    , m_xBtnOk(m_xBuilder->weld_button("ok"))
    , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charsetdropdown")))
    , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder->weld_tree_view("charsetlist")))
{
    if (bAscii)
    {
        m_xDialog->set_help_id(m_xDialog->get_help_id() + "?config=NonTextImport");
        m_xLbCharset->show();
        m_xTvCharset->hide();
    }
    else
    {
        m_xTvCharset->set_size_request(-1, m_xTvCharset->get_height_rows(6));
        m_xEncGrid->set_vexpand(true);
        m_xLbCharset->hide();
        m_xTvCharset->show();
    }

    OUString sFieldSep(SCSTR_FIELDSEP);
    sFieldSep = sFieldSep.replaceFirst( "%TAB",   ScResId(SCSTR_FIELDSEP_TAB) );
    sFieldSep = sFieldSep.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE) );

    // not possible in the Ctor initializer (MSC cannot do that):
    pFieldSepTab.reset( new ScDelimiterTable(sFieldSep) );
    pTextSepTab.reset( new ScDelimiterTable(SCSTR_TEXTSEP) );

    OUString aStr = pFieldSepTab->FirstDel();
    sal_Unicode nCode;

    while (!aStr.isEmpty())
    {
        m_xEdFieldSep->append_text(aStr);
        aStr = pFieldSepTab->NextDel();
    }

    aStr = pTextSepTab->FirstDel();

    while (!aStr.isEmpty())
    {
        m_xEdTextSep->append_text(aStr);
        aStr = pTextSepTab->NextDel();
    }

    m_xEdFieldSep->set_active(0);
    m_xEdTextSep->set_active(0);

    if ( bOnlyDbtoolsEncodings )
    {
        // Even dBase export allows multibyte now
        if ( bMultiByte )
            FillFromDbTextEncodingMap( bImport );
        else
            FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
    }
    else if ( !bAscii )
    {   //!TODO: Unicode would need work in each filter
        if ( bMultiByte )
            FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
        else
            FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
                RTL_TEXTENCODING_INFO_MULTIBYTE );
    }
    else
    {
        if ( pOptions )
        {
            nCode = pOptions->nFieldSepCode;
            aStr  = pFieldSepTab->GetDelimiter( nCode );

            if ( aStr.isEmpty() )
                m_xEdFieldSep->set_entry_text(OUString(nCode));
            else
                m_xEdFieldSep->set_entry_text(aStr);

            nCode = pOptions->nTextSepCode;
            aStr  = pTextSepTab->GetDelimiter( nCode );

            if ( aStr.isEmpty() )
                m_xEdTextSep->set_entry_text(OUString(nCode));
            else
                m_xEdTextSep->set_entry_text(aStr);
        }
        // all encodings allowed, even Unicode
        FillFromTextEncodingTable( bImport );
    }

    if( bAscii )
    {
        sal_Int32 nCharSet = officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
        OUString strFieldSeparator = officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
        OUString strTextSeparator = officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
        bool bSaveTrueCellContent = officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
        bool bSaveCellFormulas = officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
        bool bQuoteAllTextCells = officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
        bool bFixedWidth = officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();

        m_xCbFixed->show();
        m_xCbFixed->connect_toggled(LINK(this, ScImportOptionsDlg, FixedWidthHdl));
        m_xCbFixed->set_active( bFixedWidth );
        FixedWidthHdl(*m_xCbFixed);
        m_xCbShown->show();
        m_xCbShown->set_active( bSaveTrueCellContent );
        m_xCbQuoteAll->show();
        m_xCbQuoteAll->set_active( bQuoteAllTextCells );
        m_xCbFormulas->show();
        // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
        m_xCbFormulas->set_active( bSaveCellFormulas );
        // if no charset, text separator or field separator exist, keep the values from dialog initialization
        if (strFieldSeparator.getLength() > 0)
            m_xEdFieldSep->set_entry_text(strFieldSeparator);
        if (strTextSeparator.getLength() > 0)
            m_xEdTextSep->set_entry_text(strTextSeparator);
        if (nCharSet < 0 || nCharSet == RTL_TEXTENCODING_DONTKNOW )
            m_xLbCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
        else
            m_xLbCharset->SelectTextEncoding(nCharSet);
    }
    else
    {
        m_xFieldFrame->set_label(m_xFtCharset->get_label());
        m_xFtFieldSep->hide();
        m_xFtTextSep->hide();
        m_xFtCharset->hide();
        m_xEdFieldSep->hide();
        m_xEdTextSep->hide();
        m_xCbFixed->hide();
        m_xCbShown->hide();
        m_xCbQuoteAll->hide();
        m_xCbFormulas->hide();
        m_xTvCharset->grab_focus();
        m_xTvCharset->connect_row_activated(LINK(this, ScImportOptionsDlg, DoubleClickHdl));
        m_xTvCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
    }

    // optional title:
    if (pStrTitle)
        m_xDialog->set_title(*pStrTitle);
}

ScImportOptionsDlg::~ScImportOptionsDlg()
{
}

void ScImportOptionsDlg::GetImportOptions( ScImportOptions& rOptions ) const
{
    auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
    rOptions.SetTextEncoding(nEncoding);

    if (m_xCbFixed->get_visible())
    {
        rOptions.nFieldSepCode = GetCodeFromCombo( *m_xEdFieldSep );
        rOptions.nTextSepCode  = GetCodeFromCombo( *m_xEdTextSep );
        rOptions.bFixedWidth = m_xCbFixed->get_active();
        rOptions.bSaveAsShown = m_xCbShown->get_active();
        rOptions.bQuoteAllText = m_xCbQuoteAll->get_active();
        rOptions.bSaveFormulas = m_xCbFormulas->get_active();
    }
}

sal_uInt16 ScImportOptionsDlg::GetCodeFromCombo(const weld::ComboBox& rEd) const
{
    ScDelimiterTable* pTab;
    OUString  aStr( rEd.get_active_text() );
    sal_uInt16  nCode;

    if (&rEd == m_xEdTextSep.get())
        pTab = pTextSepTab.get();
    else
        pTab = pFieldSepTab.get();

    if ( aStr.isEmpty() )
    {
        nCode = 0;          // no separator
    }
    else
    {
        nCode = pTab->GetCode( aStr );

        if ( nCode == 0 )
            nCode = static_cast<sal_uInt16>(aStr[0]);
    }

    return nCode;
}

IMPL_LINK_NOARG(ScImportOptionsDlg, FixedWidthHdl, weld::ToggleButton&, void)
{
    bool bEnable = !m_xCbFixed->get_active();
    m_xFtFieldSep->set_sensitive( bEnable );
    m_xEdFieldSep->set_sensitive( bEnable );
    m_xFtTextSep->set_sensitive( bEnable );
    m_xEdTextSep->set_sensitive( bEnable );
    m_xCbShown->set_sensitive( bEnable );
    m_xCbQuoteAll->set_sensitive( bEnable );
}

IMPL_LINK_NOARG(ScImportOptionsDlg, DoubleClickHdl, weld::TreeView&, bool)
{
    m_xDialog->response(RET_OK);
    return true;
}

void ScImportOptionsDlg::SaveImportOptions() const
{
    std::shared_ptr < comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
    auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
    officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding, batch);
    officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep->get_active_text(), batch);
    officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep->get_active_text(), batch);
    officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed->get_active(), batch);
    officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas->get_active(), batch);
    officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown->get_active(), batch);
    officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll->get_active(), batch);
    batch->commit();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */