summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/importexport/csv/kexicsvimportoptionsdlg.cpp
blob: 23fee674aa027c6e6fb44fee8d0510c02bd3178a (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
/* This file is part of the KDE project
   Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kexicsvimportoptionsdlg.h"
#include <widget/kexicharencodingcombobox.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtextcodec.h>
#include <tqcheckbox.h>

#include <kapplication.h>
#include <kconfig.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kglobal.h>
#include <kcharsets.h>

KexiCSVImportOptions::KexiCSVImportOptions()
{
	kapp->config()->setGroup("ImportExport");
	encoding = kapp->config()->readEntry("DefaultEncodingForImportingCSVFiles");
	if (encoding.isEmpty()) {
		encoding = TQString::tqfromLatin1(KGlobal::locale()->encoding());
		defaultEncodingExplicitySet = false;
	}
	else
		defaultEncodingExplicitySet = true;

	stripWhiteSpaceInTextValuesChecked 
		= kapp->config()->readBoolEntry("StripBlanksOffOfTextValuesWhenImportingCSVFiles", true);
}

KexiCSVImportOptions::~KexiCSVImportOptions()
{
}

bool KexiCSVImportOptions::operator== ( const KexiCSVImportOptions & opt ) const
{
	return defaultEncodingExplicitySet==opt.defaultEncodingExplicitySet
		&& stripWhiteSpaceInTextValuesChecked==opt.stripWhiteSpaceInTextValuesChecked
		&& encoding==opt.encoding;
}

bool KexiCSVImportOptions::operator!= ( const KexiCSVImportOptions & opt ) const
{
	return !( *this==opt );
}

//----------------------------------

KexiCSVImportOptionsDialog::KexiCSVImportOptionsDialog( 
	const KexiCSVImportOptions& options, TQWidget* tqparent )
 : KDialogBase( 
	KDialogBase::Plain, 
	i18n( "CSV Import Options" ),
	Ok|Cancel, 
	Ok,
	tqparent, 
	"KexiCSVImportOptionsDialog", 
	true, 
	false
 )
{
	TQGridLayout *lyr = new TQGridLayout( plainPage(), 5, 3, 
		KDialogBase::marginHint(), KDialogBase::spacingHint());

	m_encodingComboBox = new KexiCharacterEncodingComboBox(plainPage(), options.encoding);
	lyr->addWidget( m_encodingComboBox, 0, 1 );

	TQLabel* lbl = new TQLabel( m_encodingComboBox, i18n("Text encoding:"), plainPage());
	lyr->addWidget( lbl, 0, 0 );

	lyr->addItem( new TQSpacerItem( 20, KDialogBase::spacingHint(), TQSizePolicy::Fixed, TQSizePolicy::Fixed ), 2, 1 );
	lyr->addItem( new TQSpacerItem( 121, KDialogBase::spacingHint(), TQSizePolicy::Expanding, TQSizePolicy::Minimum ), 0, 2 );

	m_chkAlwaysUseThisEncoding = new TQCheckBox(
		i18n("Always use this encoding when importing CSV data files"), plainPage());
	lyr->addWidget( m_chkAlwaysUseThisEncoding, 1, 1 );

	m_chkStripWhiteSpaceInTextValues = new TQCheckBox(
		i18n("Strip leading and trailing blanks off of text values"), plainPage());
	lyr->addWidget( m_chkStripWhiteSpaceInTextValues, 3, 1 );
	lyr->addItem( new TQSpacerItem( 20, KDialogBase::spacingHint(), TQSizePolicy::Minimum, TQSizePolicy::Expanding ), 4, 1 );

	//update widgets
	if (options.defaultEncodingExplicitySet) {
		m_encodingComboBox->setSelectedEncoding(options.encoding);
		m_chkAlwaysUseThisEncoding->setChecked(true);
	}
	m_chkStripWhiteSpaceInTextValues->setChecked(options.stripWhiteSpaceInTextValuesChecked);

	adjustSize();
	m_encodingComboBox->setFocus();
}

KexiCSVImportOptionsDialog::~KexiCSVImportOptionsDialog()
{
}

KexiCSVImportOptions KexiCSVImportOptionsDialog::options() const
{
	KexiCSVImportOptions opt;
	opt.encoding = m_encodingComboBox->selectedEncoding();
	opt.stripWhiteSpaceInTextValuesChecked = m_chkStripWhiteSpaceInTextValues->isChecked();
	return opt;
}

void KexiCSVImportOptionsDialog::accept()
{
	kapp->config()->setGroup("ImportExport");
	if (m_chkAlwaysUseThisEncoding->isChecked())
		kapp->config()->writeEntry("DefaultEncodingForImportingCSVFiles", 
			m_encodingComboBox->selectedEncoding());
	else
		kapp->config()->deleteEntry("DefaultEncodingForImportingCSVFiles");

	kapp->config()->writeEntry("StripBlanksOffOfTextValuesWhenImportingCSVFiles", 
		m_chkStripWhiteSpaceInTextValues->isChecked());

	KDialogBase::accept();
}

#include "kexicsvimportoptionsdlg.moc"