34
34
#include <kmessagebox.h>
36
36
typedef KGenericFactory<DBaseImport> DBaseImportFactory;
37
K_EXPORT_COMPONENT_FACTORY( libdbaseimport, DBaseImportFactory( "kofficefilters" ) )
40
DBaseImport::DBaseImport ( QObject* parent, const QStringList& )
37
K_EXPORT_COMPONENT_FACTORY(libdbaseimport, DBaseImportFactory("kofficefilters"))
40
DBaseImport::DBaseImport(QObject* parent, const QStringList&)
45
KoFilter::ConversionStatus DBaseImport::convert( const QByteArray& from, const QByteArray& to )
45
KoFilter::ConversionStatus DBaseImport::convert(const QByteArray& from, const QByteArray& to)
47
if (to != "application/x-kspread" || from != "application/x-dbf")
48
return KoFilter::NotImplemented;
50
QString inputFile = m_chain->inputFile();
53
bool result = dbase.load( inputFile );
55
if( dbase.version() !=3 )
57
KMessageBox::sorry( 0, i18n("File format is not supported.") );
58
return KoFilter::NotImplemented;
63
KMessageBox::sorry( 0, i18n("Could not read from file." ) );
64
return KoFilter::StupidError;
67
QString root, documentInfo;
69
root = "<!DOCTYPE spreadsheet >\n";
70
root += "<spreadsheet mime=\"application/x-kspread\" editor=\"KSpread\" >\n";
71
root += "<paper format=\"A4\" orientation=\"Portrait\" >\n";
72
root += "<borders right=\"20\" left=\"20\" bottom=\"20\" top=\"20\" />\n";
76
root += "<map activeTable=\"Table1\" >\n";
78
root += "<locale positivePrefixCurrencySymbol=\"True\"";
79
root += " negativeMonetarySignPosition=\"0\"";
80
root += " negativePrefixCurrencySymbol=\"True\" fracDigits=\"2\"";
81
root += " thousandsSeparator=\",\" dateFormat=\"%A %d %B %Y\"";
82
root += " timeFormat=\"%H:%M:%S\" monetaryDecimalSymbol=\".\"";
83
root += " weekStartsMonday=\"True\" currencySymbol=\"$\"";
84
root += " negativeSign=\"-\" positiveSign=\"\"";
85
root += " positiveMonetarySignPosition=\"1\" decimalSymbol=\".\"";
86
root += " monetaryThousandsSeparator=\",\" dateFormatShort=\"%Y-%m-%d\" />\n";
88
root += "<table name=\"Table1\" columnnumber=\"0\" borders=\"0\"";
89
root += " hide=\"0\" hidezero=\"0\" firstletterupper=\"0\" grid=\"1\"";
90
root += " formular=\"0\" lcmode=\"0\" >\n";
92
// KOffice default font
93
QFont font = KoGlobal::defaultFont();
96
QFontMetrics fm( font );
97
for( unsigned i=0; i<dbase.fields.count(); i++ )
99
int mw = qMax( (int)dbase.fields.at(i)->length, dbase.fields.at(i)->name.length());
100
double w = POINT_TO_MM( fm.maxWidth() * mw );
101
root += "<column column=\"" + QString::number(i+1) + "\"";
102
root += " width=\"" + QString::number( w ) + "\"><format/></column>\n";
106
double h = POINT_TO_MM( 5 + fm.height() + fm.leading() );
107
for( unsigned j=0; j<dbase.recordCount(); j++ )
109
root += "<row row=\"" + QString::number(j+1) + "\"";
110
root += " height=\"" + QString::number( h ) + "\" ><format/></row>\n";
113
// field names come as first row
114
for( unsigned i=0; i<dbase.fields.count(); i++ )
116
root += "<cell row=\"1\" column=\"" + QString::number(i+1) + "\" >\n";
117
root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
118
root += "<font family=\"" + font.family() + "\"" +
119
" size=\"" + QString::number(font.pointSizeF()) + "\"" +
121
root += "</format>\n";
122
root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
125
// process all records
127
for( unsigned j=0; j<dbase.recordCount(); j++ )
129
QStringList rec = dbase.readRecord( j );
133
for( int i=0; i<rec.count(); i++ )
135
root += "<cell row=\"" + QString::number(row) + "\"" +
136
"column=\"" + QString::number(i+1) + "\" >\n";
47
if (to != "application/x-kspread" || from != "application/x-dbf")
48
return KoFilter::NotImplemented;
50
QString inputFile = m_chain->inputFile();
53
bool result = dbase.load(inputFile);
55
if (dbase.version() != 3) {
56
KMessageBox::sorry(0, i18n("File format is not supported."));
57
return KoFilter::NotImplemented;
61
KMessageBox::sorry(0, i18n("Could not read from file."));
62
return KoFilter::StupidError;
65
QString root, documentInfo;
67
root = "<!DOCTYPE spreadsheet >\n";
68
root += "<spreadsheet mime=\"application/x-kspread\" editor=\"KSpread\" >\n";
69
root += "<paper format=\"A4\" orientation=\"Portrait\" >\n";
70
root += "<borders right=\"20\" left=\"20\" bottom=\"20\" top=\"20\" />\n";
74
root += "<map activeTable=\"Table1\" >\n";
76
root += "<locale positivePrefixCurrencySymbol=\"True\"";
77
root += " negativeMonetarySignPosition=\"0\"";
78
root += " negativePrefixCurrencySymbol=\"True\" fracDigits=\"2\"";
79
root += " thousandsSeparator=\",\" dateFormat=\"%A %d %B %Y\"";
80
root += " timeFormat=\"%H:%M:%S\" monetaryDecimalSymbol=\".\"";
81
root += " weekStartsMonday=\"True\" currencySymbol=\"$\"";
82
root += " negativeSign=\"-\" positiveSign=\"\"";
83
root += " positiveMonetarySignPosition=\"1\" decimalSymbol=\".\"";
84
root += " monetaryThousandsSeparator=\",\" dateFormatShort=\"%Y-%m-%d\" />\n";
86
root += "<table name=\"Table1\" columnnumber=\"0\" borders=\"0\"";
87
root += " hide=\"0\" hidezero=\"0\" firstletterupper=\"0\" grid=\"1\"";
88
root += " formular=\"0\" lcmode=\"0\" >\n";
90
// KOffice default font
91
QFont font = KoGlobal::defaultFont();
94
QFontMetrics fm(font);
95
for (int i = 0; i < dbase.fields.count(); i++) {
96
int mw = qMax((int)dbase.fields.at(i)->length, dbase.fields.at(i)->name.length());
97
double w = POINT_TO_MM(fm.maxWidth() * mw);
98
root += "<column column=\"" + QString::number(i + 1) + "\"";
99
root += " width=\"" + QString::number(w) + "\"><format/></column>\n";
103
double h = POINT_TO_MM(5 + fm.height() + fm.leading());
104
for (unsigned j = 0; j < dbase.recordCount(); j++) {
105
root += "<row row=\"" + QString::number(j + 1) + "\"";
106
root += " height=\"" + QString::number(h) + "\" ><format/></row>\n";
109
// field names come as first row
110
for (int i = 0; i < dbase.fields.count(); i++) {
111
root += "<cell row=\"1\" column=\"" + QString::number(i + 1) + "\" >\n";
137
112
root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
138
113
root += "<font family=\"" + font.family() + "\"" +
139
" size=\"" + QString::number(font.pointSizeF()) + "\"" +
114
" size=\"" + QString::number(font.pointSizeF()) + "\"" +
141
116
root += "</format>\n";
142
root += "<text>" + rec[i] + "</text></cell>\n";
149
root += "</table>\n";
151
root += "</spreadsheet>";
154
KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
156
// store output document
159
QByteArray cstring = root.toUtf8();
160
cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
161
out->write( (const char*) cstring, cstring.length() );
164
// store document info
165
out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
168
QByteArray cstring = documentInfo.toUtf8();
169
cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
171
out->write( (const char*) cstring, cstring.length() );
117
root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
120
// process all records
122
for (unsigned j = 0; j < dbase.recordCount(); j++) {
123
QStringList rec = dbase.readRecord(j);
126
for (int i = 0; i < rec.count(); i++) {
127
root += "<cell row=\"" + QString::number(row) + "\"" +
128
"column=\"" + QString::number(i + 1) + "\" >\n";
129
root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
130
root += "<font family=\"" + font.family() + "\"" +
131
" size=\"" + QString::number(font.pointSizeF()) + "\"" +
133
root += "</format>\n";
134
root += "<text>" + rec[i] + "</text></cell>\n";
141
root += "</table>\n";
143
root += "</spreadsheet>";
146
KoStoreDevice* out = m_chain->storageFile("root", KoStore::Write);
148
// store output document
150
QByteArray cstring = root.toUtf8();
151
cstring.prepend("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
152
out->write((const char*) cstring, cstring.length());
155
// store document info
156
out = m_chain->storageFile("documentinfo.xml", KoStore::Write);
158
QByteArray cstring = documentInfo.toUtf8();
159
cstring.prepend("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
161
out->write((const char*) cstring, cstring.length());