~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/kspread/dbase/dbaseimport.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <kmessagebox.h>
35
35
 
36
36
typedef KGenericFactory<DBaseImport> DBaseImportFactory;
37
 
K_EXPORT_COMPONENT_FACTORY( libdbaseimport, DBaseImportFactory( "kofficefilters" ) )
38
 
 
39
 
 
40
 
DBaseImport::DBaseImport ( QObject* parent, const QStringList& )
41
 
    : KoFilter( parent )
 
37
K_EXPORT_COMPONENT_FACTORY(libdbaseimport, DBaseImportFactory("kofficefilters"))
 
38
 
 
39
 
 
40
DBaseImport::DBaseImport(QObject* parent, const QStringList&)
 
41
        : KoFilter(parent)
42
42
{
43
43
}
44
44
 
45
 
KoFilter::ConversionStatus DBaseImport::convert( const QByteArray& from, const QByteArray& to )
 
45
KoFilter::ConversionStatus DBaseImport::convert(const QByteArray& from, const QByteArray& to)
46
46
{
47
 
  if (to != "application/x-kspread" || from != "application/x-dbf")
48
 
    return KoFilter::NotImplemented;
49
 
 
50
 
  QString inputFile = m_chain->inputFile();
51
 
 
52
 
  DBase dbase;
53
 
  bool result = dbase.load( inputFile );
54
 
 
55
 
  if( dbase.version() !=3 )
56
 
  {
57
 
    KMessageBox::sorry( 0, i18n("File format is not supported.") );
58
 
    return KoFilter::NotImplemented;
59
 
  }
60
 
 
61
 
  if( !result )
62
 
  {
63
 
    KMessageBox::sorry( 0, i18n("Could not read from file." ) );
64
 
    return KoFilter::StupidError;
65
 
  }
66
 
 
67
 
  QString root, documentInfo;
68
 
 
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";
73
 
  root += "<head/>\n";
74
 
  root += "<foot/>\n";
75
 
  root += "</paper>\n";
76
 
  root += "<map activeTable=\"Table1\" >\n";
77
 
 
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";
87
 
 
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";
91
 
 
92
 
  // KOffice default font
93
 
  QFont font = KoGlobal::defaultFont();
94
 
 
95
 
  // define columns
96
 
  QFontMetrics fm( font );
97
 
  for( unsigned i=0; i<dbase.fields.count(); i++ )
98
 
  {
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";
103
 
  }
104
 
 
105
 
  // define rows
106
 
  double h = POINT_TO_MM( 5 + fm.height() + fm.leading() );
107
 
  for( unsigned j=0; j<dbase.recordCount(); j++ )
108
 
  {
109
 
    root += "<row row=\"" + QString::number(j+1) + "\"";
110
 
    root += " height=\"" + QString::number( h ) + "\" ><format/></row>\n";
111
 
  }
112
 
 
113
 
  // field names come as first row
114
 
  for( unsigned i=0; i<dbase.fields.count(); i++ )
115
 
  {
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()) + "\"" +
120
 
      " weight=\"50\" />";
121
 
    root += "</format>\n";
122
 
    root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
123
 
  }
124
 
 
125
 
  // process all records
126
 
  unsigned row = 1;
127
 
  for( unsigned j=0; j<dbase.recordCount(); j++ )
128
 
  {
129
 
    QStringList rec = dbase.readRecord( j );
130
 
    if( rec.count() )
131
 
    {
132
 
      row++;
133
 
      for( int i=0; i<rec.count(); i++ )
134
 
      {
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;
 
49
 
 
50
    QString inputFile = m_chain->inputFile();
 
51
 
 
52
    DBase dbase;
 
53
    bool result = dbase.load(inputFile);
 
54
 
 
55
    if (dbase.version() != 3) {
 
56
        KMessageBox::sorry(0, i18n("File format is not supported."));
 
57
        return KoFilter::NotImplemented;
 
58
    }
 
59
 
 
60
    if (!result) {
 
61
        KMessageBox::sorry(0, i18n("Could not read from file."));
 
62
        return KoFilter::StupidError;
 
63
    }
 
64
 
 
65
    QString root, documentInfo;
 
66
 
 
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";
 
71
    root += "<head/>\n";
 
72
    root += "<foot/>\n";
 
73
    root += "</paper>\n";
 
74
    root += "<map activeTable=\"Table1\" >\n";
 
75
 
 
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";
 
85
 
 
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";
 
89
 
 
90
    // KOffice default font
 
91
    QFont font = KoGlobal::defaultFont();
 
92
 
 
93
    // define columns
 
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";
 
100
    }
 
101
 
 
102
    // define rows
 
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";
 
107
    }
 
108
 
 
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()) + "\"" +
140
 
          " weight=\"50\" />";
 
114
                " size=\"" + QString::number(font.pointSizeF()) + "\"" +
 
115
                " weight=\"50\" />";
141
116
        root += "</format>\n";
142
 
        root += "<text>" + rec[i] + "</text></cell>\n";
143
 
      }
144
 
    }
145
 
  }
146
 
 
147
 
  dbase.close();
148
 
 
149
 
  root += "</table>\n";
150
 
  root += "</map>\n";
151
 
  root += "</spreadsheet>";
152
 
 
153
 
  // prepare storage
154
 
  KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
155
 
 
156
 
  // store output document
157
 
  if( out )
158
 
    {
159
 
      QByteArray cstring = root.toUtf8();
160
 
      cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
161
 
      out->write( (const char*) cstring, cstring.length() );
162
 
    }
163
 
 
164
 
  // store document info
165
 
  out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
166
 
  if ( out )
167
 
    {
168
 
       QByteArray cstring = documentInfo.toUtf8();
169
 
       cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
170
 
 
171
 
       out->write( (const char*) cstring, cstring.length() );
172
 
     }
173
 
 
174
 
  return KoFilter::OK;
 
117
        root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
 
118
    }
 
119
 
 
120
    // process all records
 
121
    unsigned row = 1;
 
122
    for (unsigned j = 0; j < dbase.recordCount(); j++) {
 
123
        QStringList rec = dbase.readRecord(j);
 
124
        if (rec.count()) {
 
125
            row++;
 
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()) + "\"" +
 
132
                        " weight=\"50\" />";
 
133
                root += "</format>\n";
 
134
                root += "<text>" + rec[i] + "</text></cell>\n";
 
135
            }
 
136
        }
 
137
    }
 
138
 
 
139
    dbase.close();
 
140
 
 
141
    root += "</table>\n";
 
142
    root += "</map>\n";
 
143
    root += "</spreadsheet>";
 
144
 
 
145
    // prepare storage
 
146
    KoStoreDevice* out = m_chain->storageFile("root", KoStore::Write);
 
147
 
 
148
    // store output document
 
149
    if (out) {
 
150
        QByteArray cstring = root.toUtf8();
 
151
        cstring.prepend("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 
152
        out->write((const char*) cstring, cstring.length());
 
153
    }
 
154
 
 
155
    // store document info
 
156
    out = m_chain->storageFile("documentinfo.xml", KoStore::Write);
 
157
    if (out) {
 
158
        QByteArray cstring = documentInfo.toUtf8();
 
159
        cstring.prepend("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 
160
 
 
161
        out->write((const char*) cstring, cstring.length());
 
162
    }
 
163
 
 
164
    return KoFilter::OK;
175
165
}