3
* Copyright (C) 2001-2004 Murray Cumming
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public
16
* License along with this program; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
21
#ifndef GLOM_DIALOG_IMPORT_CSV_H
22
#define GLOM_DIALOG_IMPORT_CSV_H
27
#include <giomm/asyncresult.h>
28
#include <giomm/file.h>
29
#include <giomm/inputstream.h>
30
#include <gtkmm/dialog.h>
31
#include <gtkmm/treeview.h>
32
#include <gtkmm/liststore.h>
33
#include <gtkmm/combobox.h>
34
#include <gtkmm/spinbutton.h>
35
#include <libgdamm/datamodelimport.h>
36
#include <libglademm/xml.h>
41
class Dialog_Import_CSV
46
typedef sigc::signal<void> SignalStateChanged;
55
Dialog_Import_CSV(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade);
57
void import(const Glib::ustring& uri, const Glib::ustring& into_table);
59
State get_state() const { return m_state; }
60
Glib::ustring get_target_table_name() const { return m_target_table->get_text(); }
61
const Glib::ustring& get_filename() const { return m_filename; }
63
unsigned int get_row_count() const;
64
unsigned int get_column_count() const;
65
const sharedptr<Field>& get_field_for_column(unsigned int col);
66
const Glib::ustring& get_data(unsigned int row, unsigned int col);
68
SignalStateChanged signal_state_changed() const { return m_signal_state_changed; }
72
void show_error_dialog(const Glib::ustring& primary, const Glib::ustring& secondary);
74
void encoding_data_func(const Gtk::TreeModel::iterator& iter, Gtk::CellRendererText& renderer);
75
bool row_separator_func(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::iterator& iter) const;
77
void on_query_info(const Glib::RefPtr<Gio::AsyncResult>& result);
78
void on_file_read(const Glib::RefPtr<Gio::AsyncResult>& result);
79
void on_stream_read(const Glib::RefPtr<Gio::AsyncResult>& result);
81
void on_encoding_changed();
82
void on_first_line_as_title_toggled();
83
void on_sample_rows_changed();
85
Glib::ustring get_current_encoding() const;
87
void encoding_error();
90
void handle_line(const Glib::ustring& line, unsigned int line_number);
92
void line_data_func(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter);
93
void field_data_func(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter, unsigned int column_number);
94
void on_field_edited(const Glib::ustring& path, const Glib::ustring& new_text, unsigned int column_number);
96
void set_state(State state);
97
void validate_primary_key();
99
class EncodingColumns: public Gtk::TreeModelColumnRecord
102
EncodingColumns() { add(m_col_name); add(m_col_charset); }
104
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
105
Gtk::TreeModelColumn<Glib::ustring> m_col_charset;
108
class FieldColumns: public Gtk::TreeModelColumnRecord
111
FieldColumns() { add(m_col_field_name); add(m_col_field); }
113
Gtk::TreeModelColumn<Glib::ustring> m_col_field_name;
114
Gtk::TreeModelColumn<sharedptr<Field> > m_col_field;
117
class SampleColumns: public Gtk::TreeModelColumnRecord
120
SampleColumns() { add(m_col_row); }
122
Gtk::TreeModelColumn<int> m_col_row;
125
EncodingColumns m_encoding_columns;
126
Glib::RefPtr<Gtk::ListStore> m_encoding_model;
128
FieldColumns m_field_columns;
129
Glib::RefPtr<Gtk::ListStore> m_field_model;
130
Glib::RefPtr<Gtk::TreeModelSort> m_field_model_sorted;
132
SampleColumns m_sample_columns;
133
Glib::RefPtr<Gtk::ListStore> m_sample_model;
134
Gtk::TreeView* m_sample_view;
135
Gtk::Label* m_target_table;
136
Gtk::ComboBox* m_encoding_combo;
137
Gtk::Label* m_encoding_info;
138
Gtk::CheckButton* m_first_line_as_title;
139
Gtk::SpinButton* m_sample_rows;
140
Gtk::Label* m_advice_label;
141
Gtk::Label* m_error_label;
143
Glib::RefPtr<Gio::File> m_file;
144
Glib::RefPtr<Gio::FileInputStream> m_stream;
145
Glib::ustring m_filename;
147
// The raw data in the original encoding. We keep this so we can convert
148
// from the user-selected encoding to UTF-8 every time the user changes
150
std::vector<char> m_raw;
155
std::auto_ptr<Buffer> m_buffer;
157
// Index into the ENCODINGS array (see dialog_import_csv.cc) for the
158
// encoding that we currently try to read the data with, or -1 if
159
// auto-detection is disabled.
160
int m_auto_detect_encoding;
162
// We use the low-level Glib::IConv routines to progressively convert the
163
// input data in an idle handler.
166
std::vector<char>::size_type input_position;
167
std::string current_line;
168
sigc::connection idle_connection;
169
unsigned int line_number;
171
Parser(const char* encoding): conv("UTF-8", encoding), input_position(0), line_number(0) {}
172
~Parser() { idle_connection.disconnect(); }
175
std::auto_ptr<Parser> m_parser;
179
std::vector<std::vector<Glib::ustring> > m_rows;
181
// The fields into which to import the data:
182
typedef std::vector< sharedptr<Field> > type_vec_fields;
183
type_vec_fields m_fields;
185
SignalStateChanged m_signal_state_changed;
190
#endif //GLOM_DIALOG_IMPORT_CSV_H