~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to pgadmin/frm/frmExport.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2011-06-07 23:03:54 UTC
  • mfrom: (1.3.1 upstream) (13 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110607230354-3td4j9y71u4ahcvj
Tags: 1.14.0~beta1-1
* New upstream development release, adding Build-Depends on
  postgresql-server-dev-all >= 117~.
* Add Build-Depends on quilt, (un)patch to debian/rules and patch for fixing
  the include for kwlist.h in pgadmin/db/keywords.c.
* Add pg_config --includedir-server output to CPPFLAGS.
* Remove unrecognized configure options: --with-wx-config,
  --with-pgsql-include, --enable-gtk2, --enable-unicode.
* Clean up manually the files that are left behind after the broken
  distclean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//////////////////////////////////////////////////////////////////////////
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
 
// 
5
 
// Copyright (C) 2002 - 2010, The pgAdmin Development Team
 
4
//
 
5
// Copyright (C) 2002 - 2011, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// frmExport.cpp - The export file dialogue
34
34
 
35
35
 
36
36
BEGIN_EVENT_TABLE(frmExport, pgDialog)
37
 
    EVT_TEXT(XRCID("txtFilename"),          frmExport::OnChange)
38
 
    EVT_RADIOBUTTON(XRCID("rbQuoteNone"),   frmExport::OnChange)
39
 
    EVT_RADIOBUTTON(XRCID("rbQuoteStrings"),frmExport::OnChange)
40
 
    EVT_RADIOBUTTON(XRCID("rbQuoteAll"),    frmExport::OnChange)
41
 
    EVT_BUTTON(XRCID("btnFilename"),        frmExport::OnBrowseFile)
42
 
    EVT_BUTTON(wxID_HELP,                   frmExport::OnHelp)
43
 
    EVT_BUTTON(wxID_OK,                     frmExport::OnOK)
44
 
    EVT_BUTTON(wxID_CANCEL,                 frmExport::OnCancel)
 
37
        EVT_TEXT(XRCID("txtFilename"),          frmExport::OnChange)
 
38
        EVT_RADIOBUTTON(XRCID("rbQuoteNone"),   frmExport::OnChange)
 
39
        EVT_RADIOBUTTON(XRCID("rbQuoteStrings"), frmExport::OnChange)
 
40
        EVT_RADIOBUTTON(XRCID("rbQuoteAll"),    frmExport::OnChange)
 
41
        EVT_BUTTON(XRCID("btnFilename"),        frmExport::OnBrowseFile)
 
42
        EVT_BUTTON(wxID_HELP,                   frmExport::OnHelp)
 
43
        EVT_BUTTON(wxID_OK,                     frmExport::OnOK)
 
44
        EVT_BUTTON(wxID_CANCEL,                 frmExport::OnCancel)
45
45
END_EVENT_TABLE()
46
46
 
47
47
 
48
48
 
49
49
frmExport::frmExport(wxWindow *p)
50
50
{
51
 
    parent=p;
52
 
 
53
 
    wxWindowBase::SetFont(settings->GetSystemFont());
54
 
    LoadResource(p, wxT("frmExport"));
55
 
    RestorePosition();
56
 
 
57
 
    // Icon
58
 
    appearanceFactory->SetIcons(this);
59
 
    cbQuoteChar->Disable();
60
 
    btnOK->Disable();
61
 
 
62
 
 
63
 
    bool uc=settings->GetExportUnicode();
64
 
    rbUnicode->SetValue(uc);
65
 
    rbLocal->SetValue(!uc);
66
 
 
67
 
    bool isCrLf=settings->GetExportRowSeparator() == wxT("\r\n");
68
 
    rbCRLF->SetValue(isCrLf);
69
 
    rbLF->SetValue(!isCrLf);
70
 
 
71
 
    int qt=settings->GetExportQuoting();
72
 
    
73
 
    rbQuoteNone->SetValue(qt == 0);
74
 
    rbQuoteStrings->SetValue(qt == 1);
75
 
    rbQuoteAll->SetValue(qt == 2);
76
 
 
77
 
    cbColSeparator->SetValue(settings->GetExportColSeparator());
78
 
 
79
 
 
80
 
    cbQuoteChar->SetValue(settings->GetExportQuoteChar());
81
 
 
82
 
    wxString val;
83
 
    settings->Read(wxT("Export/LastFile"), &val, wxEmptyString);
84
 
    txtFilename->SetValue(val);
85
 
 
86
 
    wxCommandEvent ev;
87
 
    OnChange(ev);
 
51
        parent = p;
 
52
 
 
53
        wxWindowBase::SetFont(settings->GetSystemFont());
 
54
        LoadResource(p, wxT("frmExport"));
 
55
        RestorePosition();
 
56
 
 
57
        // Icon
 
58
        appearanceFactory->SetIcons(this);
 
59
        cbQuoteChar->Disable();
 
60
        btnOK->Disable();
 
61
 
 
62
 
 
63
        bool uc = settings->GetExportUnicode();
 
64
        rbUnicode->SetValue(uc);
 
65
        rbLocal->SetValue(!uc);
 
66
 
 
67
        bool isCrLf = settings->GetExportRowSeparator() == wxT("\r\n");
 
68
        rbCRLF->SetValue(isCrLf);
 
69
        rbLF->SetValue(!isCrLf);
 
70
 
 
71
        int qt = settings->GetExportQuoting();
 
72
 
 
73
        rbQuoteNone->SetValue(qt == 0);
 
74
        rbQuoteStrings->SetValue(qt == 1);
 
75
        rbQuoteAll->SetValue(qt == 2);
 
76
 
 
77
        cbColSeparator->SetValue(settings->GetExportColSeparator());
 
78
 
 
79
 
 
80
        cbQuoteChar->SetValue(settings->GetExportQuoteChar());
 
81
 
 
82
        wxString val;
 
83
        settings->Read(wxT("Export/LastFile"), &val, wxEmptyString);
 
84
        txtFilename->SetValue(val);
 
85
 
 
86
        wxCommandEvent ev;
 
87
        OnChange(ev);
88
88
}
89
89
 
90
90
 
91
91
frmExport::~frmExport()
92
92
{
93
 
    SavePosition();
 
93
        SavePosition();
94
94
}
95
95
 
96
96
 
97
97
void frmExport::OnHelp(wxCommandEvent &ev)
98
98
{
99
 
    DisplayHelp(wxT("export"), HELP_PGADMIN);
 
99
        DisplayHelp(wxT("export"), HELP_PGADMIN);
100
100
}
101
101
 
102
102
 
103
103
void frmExport::OnChange(wxCommandEvent &ev)
104
104
{
105
 
    cbQuoteChar->Enable(rbQuoteStrings->GetValue() || rbQuoteAll->GetValue());
106
 
    btnOK->Enable(!txtFilename->GetValue().IsEmpty() && !cbColSeparator->GetValue().IsEmpty());
 
105
        cbQuoteChar->Enable(rbQuoteStrings->GetValue() || rbQuoteAll->GetValue());
 
106
        btnOK->Enable(!txtFilename->GetValue().IsEmpty() && !cbColSeparator->GetValue().IsEmpty());
107
107
}
108
108
 
109
109
 
110
110
void frmExport::OnOK(wxCommandEvent &ev)
111
111
{
112
 
    settings->SetExportUnicode(rbUnicode->GetValue());
113
 
    settings->SetExportRowSeparator(rbCRLF->GetValue() ? wxT("\r\n") : wxT("\n"));
114
 
    settings->SetExportColSeparator(cbColSeparator->GetValue());
115
 
 
116
 
    if (rbQuoteAll->GetValue())
117
 
        settings->SetExportQuoting(2);
118
 
    else if (rbQuoteStrings->GetValue())
119
 
        settings->SetExportQuoting(1);
120
 
    else
121
 
        settings->SetExportQuoting(0);
122
 
 
123
 
    settings->SetExportQuoteChar(cbQuoteChar->GetValue());
124
 
 
125
 
    settings->Write(wxT("Export/LastFile"), txtFilename->GetValue());
126
 
 
127
 
 
128
 
    if (IsModal())
129
 
        EndModal(wxID_OK);
130
 
    else
131
 
        Destroy();
 
112
        settings->SetExportUnicode(rbUnicode->GetValue());
 
113
        settings->SetExportRowSeparator(rbCRLF->GetValue() ? wxT("\r\n") : wxT("\n"));
 
114
        settings->SetExportColSeparator(cbColSeparator->GetValue());
 
115
 
 
116
        if (rbQuoteAll->GetValue())
 
117
                settings->SetExportQuoting(2);
 
118
        else if (rbQuoteStrings->GetValue())
 
119
                settings->SetExportQuoting(1);
 
120
        else
 
121
                settings->SetExportQuoting(0);
 
122
 
 
123
        settings->SetExportQuoteChar(cbQuoteChar->GetValue());
 
124
 
 
125
        settings->Write(wxT("Export/LastFile"), txtFilename->GetValue());
 
126
 
 
127
 
 
128
        if (IsModal())
 
129
                EndModal(wxID_OK);
 
130
        else
 
131
                Destroy();
132
132
}
133
133
 
134
 
    
 
134
 
135
135
 
136
136
bool frmExport::Export(pgSet *set)
137
137
{
138
 
        ctlSQLResult *grid=0;
 
138
        ctlSQLResult *grid = 0;
139
139
        if (!set)
140
140
        {
141
141
                wxLogInfo(wxT("Exporting data from the grid"));
144
144
        else
145
145
                wxLogInfo(wxT("Exporting data from a resultset"));
146
146
 
147
 
    wxFile file(txtFilename->GetValue(), wxFile::write);
148
 
    if (!file.IsOpened())
149
 
    {
150
 
        wxLogError(__("Failed to open file %s."), txtFilename->GetValue().c_str());
151
 
        return false;
152
 
    }
153
 
 
154
 
    wxString line;
155
 
    long skipped = 0;
156
 
    wxWX2MBbuf buf;
157
 
 
158
 
    int colCount, rowCount;
 
147
        wxFile file(txtFilename->GetValue(), wxFile::write);
 
148
        if (!file.IsOpened())
 
149
        {
 
150
                wxLogError(__("Failed to open file %s."), txtFilename->GetValue().c_str());
 
151
                return false;
 
152
        }
 
153
 
 
154
        wxString line;
 
155
        long skipped = 0;
 
156
        wxWX2MBbuf buf;
 
157
 
 
158
        int colCount, rowCount;
159
159
 
160
160
        if (set)
161
161
        {
168
168
                rowCount = grid->NumRows();
169
169
        }
170
170
 
171
 
    int col;
172
 
    if (chkColnames->GetValue())
173
 
    {
174
 
        for (col=0 ; col < colCount ; col++)
175
 
        {
176
 
            if (!col)
177
 
                line=wxEmptyString;
178
 
            else
179
 
                line += cbColSeparator->GetValue();
180
 
            
181
 
            if (rbQuoteStrings->GetValue() || rbQuoteAll->GetValue())
182
 
            {
183
 
                wxString qc = cbQuoteChar->GetValue();
184
 
                                
 
171
        int col;
 
172
        if (chkColnames->GetValue())
 
173
        {
 
174
                for (col = 0 ; col < colCount ; col++)
 
175
                {
 
176
                        if (!col)
 
177
                                line = wxEmptyString;
 
178
                        else
 
179
                                line += cbColSeparator->GetValue();
 
180
 
 
181
                        if (rbQuoteStrings->GetValue() || rbQuoteAll->GetValue())
 
182
                        {
 
183
                                wxString qc = cbQuoteChar->GetValue();
 
184
 
185
185
                                wxString hdr;
186
186
                                if (set)
187
 
                    hdr = set->ColName(col);
 
187
                                        hdr = set->ColName(col);
188
188
                                else
189
 
                                        hdr = grid->OnGetItemText(-1, col+1).BeforeFirst('\n');
 
189
                                        hdr = grid->OnGetItemText(-1, col + 1).BeforeFirst('\n');
190
190
 
191
 
                hdr.Replace(qc, qc+qc);
192
 
                line += qc + hdr + qc;
193
 
            }
194
 
            else
 
191
                                hdr.Replace(qc, qc + qc);
 
192
                                line += qc + hdr + qc;
 
193
                        }
 
194
                        else
195
195
                        {
196
196
                                if (set)
197
 
                    line += set->ColName(col);
 
197
                                        line += set->ColName(col);
198
198
                                else
199
 
                                        line += grid->OnGetItemText(-1, col+1).BeforeFirst('\n');
 
199
                                        line += grid->OnGetItemText(-1, col + 1).BeforeFirst('\n');
200
200
                        }
201
 
        }
202
 
        if (rbCRLF->GetValue())
203
 
            line += wxT("\r\n");
204
 
        else
205
 
            line += wxT("\n");
206
 
 
207
 
        if (rbUnicode->GetValue())
208
 
            file.Write(line, wxConvUTF8);
209
 
        else
210
 
        {
211
 
            buf = line.mb_str(wxConvLibc);
212
 
            if (!buf)
213
 
                skipped++;
214
 
            else
215
 
                file.Write(line, wxConvLibc);
216
 
        }
217
 
    }
218
 
 
219
 
 
220
 
    wxString text;
221
 
    OID typOid;
222
 
 
223
 
    int row;
224
 
    for (row=0 ; row < rowCount ; row++)
225
 
    {
226
 
        for (col=0 ; col < colCount ; col++)
227
 
        {
228
 
            if (!col)
229
 
                line=wxEmptyString;
230
 
            else
231
 
                line += cbColSeparator->GetValue();
232
 
 
233
 
            bool needQuote=rbQuoteAll->GetValue();
 
201
                }
 
202
                if (rbCRLF->GetValue())
 
203
                        line += wxT("\r\n");
 
204
                else
 
205
                        line += wxT("\n");
 
206
 
 
207
                if (rbUnicode->GetValue())
 
208
                        file.Write(line, wxConvUTF8);
 
209
                else
 
210
                {
 
211
                        buf = line.mb_str(wxConvLibc);
 
212
                        if (!buf)
 
213
                                skipped++;
 
214
                        else
 
215
                                file.Write(line, wxConvLibc);
 
216
                }
 
217
        }
 
218
 
 
219
 
 
220
        wxString text;
 
221
        OID typOid;
 
222
 
 
223
        int row;
 
224
        for (row = 0 ; row < rowCount ; row++)
 
225
        {
 
226
                for (col = 0 ; col < colCount ; col++)
 
227
                {
 
228
                        if (!col)
 
229
                                line = wxEmptyString;
 
230
                        else
 
231
                                line += cbColSeparator->GetValue();
 
232
 
 
233
                        bool needQuote = rbQuoteAll->GetValue();
234
234
 
235
235
                        if (set)
236
236
                        {
237
 
                text = set->GetVal(col);
238
 
                typOid = set->ColTypClass(col);
 
237
                                text = set->GetVal(col);
 
238
                                typOid = set->ColTypClass(col);
239
239
                        }
240
240
                        else
241
241
                        {
242
 
                                text = grid->OnGetItemText(row, col+1);
 
242
                                text = grid->OnGetItemText(row, col + 1);
243
243
                                typOid = grid->colTypClasses[col];
244
244
                        }
245
245
 
246
 
            if (!needQuote && rbQuoteStrings->GetValue())
247
 
            {
248
 
                // find out if string
249
 
                switch (typOid)
250
 
                {
251
 
                    case PGTYPCLASS_NUMERIC:
252
 
                    case PGTYPCLASS_BOOL:
253
 
                        break;
254
 
                    default:
255
 
                        needQuote=true;
256
 
                        break;
257
 
                }
258
 
            }
259
 
            if (needQuote)
260
 
            {
261
 
                wxString qc = cbQuoteChar->GetValue();
262
 
                text.Replace(qc, qc+qc);
263
 
                line += qc + text + qc;
264
 
            }
265
 
            else
266
 
                line += text;
267
 
        }
268
 
        if (rbCRLF->GetValue())
269
 
            line += wxT("\r\n");
270
 
        else
271
 
            line += wxT("\n");
 
246
                        if (!needQuote && rbQuoteStrings->GetValue())
 
247
                        {
 
248
                                // find out if string
 
249
                                switch (typOid)
 
250
                                {
 
251
                                        case PGTYPCLASS_NUMERIC:
 
252
                                        case PGTYPCLASS_BOOL:
 
253
                                                break;
 
254
                                        default:
 
255
                                                needQuote = true;
 
256
                                                break;
 
257
                                }
 
258
                        }
 
259
                        if (needQuote)
 
260
                        {
 
261
                                wxString qc = cbQuoteChar->GetValue();
 
262
                                text.Replace(qc, qc + qc);
 
263
                                line += qc + text + qc;
 
264
                        }
 
265
                        else
 
266
                                line += text;
 
267
                }
 
268
                if (rbCRLF->GetValue())
 
269
                        line += wxT("\r\n");
 
270
                else
 
271
                        line += wxT("\n");
272
272
 
273
 
        if (rbUnicode->GetValue())
274
 
            file.Write(line, wxConvUTF8);
275
 
        else
276
 
        {
277
 
            buf = line.mb_str(wxConvLibc);
278
 
            if (!buf)
279
 
                skipped++;
280
 
            else
281
 
                file.Write(line, wxConvLibc);
282
 
        }
 
273
                if (rbUnicode->GetValue())
 
274
                        file.Write(line, wxConvUTF8);
 
275
                else
 
276
                {
 
277
                        buf = line.mb_str(wxConvLibc);
 
278
                        if (!buf)
 
279
                                skipped++;
 
280
                        else
 
281
                                file.Write(line, wxConvLibc);
 
282
                }
283
283
 
284
284
                if (set)
285
285
                        set->MoveNext();
286
 
    }
287
 
    file.Close();
288
 
 
289
 
    if (skipped)
290
 
        wxLogError(wxPLURAL(
291
 
            "Data export incomplete.\n\n%d row contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.", 
292
 
            "Data export incomplete.\n\n%d rows contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.", 
293
 
            skipped), skipped);
294
 
    else
295
 
        wxMessageBox(_("Data export completed successfully."), _("Export data"), wxICON_INFORMATION | wxOK);
296
 
 
297
 
    return true;
 
286
        }
 
287
        file.Close();
 
288
 
 
289
        if (skipped)
 
290
                wxLogError(wxPLURAL(
 
291
                               "Data export incomplete.\n\n%d row contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.",
 
292
                               "Data export incomplete.\n\n%d rows contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.",
 
293
                               skipped), skipped);
 
294
        else
 
295
                wxMessageBox(_("Data export completed successfully."), _("Export data"), wxICON_INFORMATION | wxOK);
 
296
 
 
297
        return true;
298
298
}
299
299
 
300
300
 
301
301
void frmExport::OnCancel(wxCommandEvent &ev)
302
302
{
303
 
    if (IsModal())
304
 
        EndModal(wxID_CANCEL);
305
 
    else
306
 
        Destroy();
 
303
        if (IsModal())
 
304
                EndModal(wxID_CANCEL);
 
305
        else
 
306
                Destroy();
307
307
}
308
308
 
309
309
void frmExport::OnBrowseFile(wxCommandEvent &ev)
310
310
{
311
 
    wxString directory;
312
 
    wxString filename;
 
311
        wxString directory;
 
312
        wxString filename;
313
313
 
314
 
    if (txtFilename->GetValue().IsEmpty())
315
 
        directory = wxGetHomeDir();
316
 
    else
317
 
    {
318
 
        directory = wxFileName(txtFilename->GetValue()).GetPath();
319
 
        filename = wxFileName(txtFilename->GetValue()).GetFullName();
320
 
    }
 
314
        if (txtFilename->GetValue().IsEmpty())
 
315
                directory = wxGetHomeDir();
 
316
        else
 
317
        {
 
318
                directory = wxFileName(txtFilename->GetValue()).GetPath();
 
319
                filename = wxFileName(txtFilename->GetValue()).GetFullName();
 
320
        }
321
321
 
322
322
#ifdef __WXMSW__
323
 
    wxFileDialog file(this, _("Select export filename"), directory, filename, 
324
 
        _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*.*)|*.*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
 
323
        wxFileDialog file(this, _("Select export filename"), directory, filename,
 
324
                          _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*.*)|*.*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
325
325
#else
326
 
    wxFileDialog file(this, _("Select export filename"), directory, filename, 
327
 
        _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*)|*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
 
326
        wxFileDialog file(this, _("Select export filename"), directory, filename,
 
327
                          _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*)|*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
328
328
#endif
329
329
 
330
 
    if (file.ShowModal() == wxID_OK)
331
 
    {
332
 
        txtFilename->SetValue(file.GetPath());
333
 
        OnChange(ev);
334
 
    }
 
330
        if (file.ShowModal() == wxID_OK)
 
331
        {
 
332
                txtFilename->SetValue(file.GetPath());
 
333
                OnChange(ev);
 
334
        }
335
335
}