~ubuntu-branches/ubuntu/quantal/glom/quantal

« back to all changes in this revision

Viewing changes to glom/dialog_import_csv.h

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-01-11 17:12:01 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090111171201-0ov9zh1fxfueshxc
Tags: 1.8.5-0ubuntu1
* New upstream release (LP: #256701), fixes LP bugs: 
  + Clear the text in property dialogs for text items (LP: #309147)
  + Don't crash when showing and then hiding the grid (LP: #303453)
  + Temporarily remember the sort order so it is the same when navigating 
    away and back (LP: #303422)
  + Use the list's sort order for the top-level records in the report 
    (LP: #303425)
  + Users/Groups: Disable drag-and-drop for the treeview, because it is
    useless and confusing (LP: #299573)
  + Import: Sort the fields list alphabetically (LP: #306593)
  + delete primary key make unusuable the database (LP: #299549)
  + Spanish translate incomplete (LP: #299556)
  + import date format error (LP: #299591)
  + can't delete data from table list view (LP: #299853)
  + Field definition: default value not saved (LP: #299896)
  + reports crashing (LP: #300054)
  + Year error with date fields (LP: #300057)
  + list view: 2 records added instead of 1 (LP: #300819)
* debian/control: Refreshed dependencies for libglom-dev.
* debian/control: Updated build-deps to match configure checks. goocavnasmm
  build-dep bumped to version without .la files.
* debian/rules: Don't delete the directory containing the templates.
* debian/control, debian/rules, debian/glom-doc.*: Split out
  arch-independent manual and examples into separate package glom-doc, which
  is now recommended by glom (glom will still work if they're not available)
* debian/copyright: Rewritten to new machine-readable format. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Glom
 
2
 *
 
3
 * Copyright (C) 2001-2004 Murray Cumming
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#ifndef GLOM_DIALOG_IMPORT_CSV_H
 
22
#define GLOM_DIALOG_IMPORT_CSV_H
 
23
 
 
24
#include "base_db.h"
 
25
 
 
26
#include <memory>
 
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>
 
37
 
 
38
namespace Glom
 
39
{
 
40
 
 
41
class Dialog_Import_CSV
 
42
  : public Gtk::Dialog,
 
43
    public Base_DB
 
44
{
 
45
public:
 
46
  typedef sigc::signal<void> SignalStateChanged;
 
47
 
 
48
  enum State {
 
49
    NONE,
 
50
    PARSING,
 
51
    ENCODING_ERROR,
 
52
    PARSED
 
53
  };
 
54
 
 
55
  Dialog_Import_CSV(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade);
 
56
 
 
57
  void import(const Glib::ustring& uri, const Glib::ustring& into_table);
 
58
  
 
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; }
 
62
 
 
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);
 
67
 
 
68
  SignalStateChanged signal_state_changed() const { return m_signal_state_changed; }
 
69
 
 
70
protected:
 
71
  void clear();
 
72
  void show_error_dialog(const Glib::ustring& primary, const Glib::ustring& secondary);
 
73
 
 
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;
 
76
 
 
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);
 
80
 
 
81
  void on_encoding_changed();
 
82
  void on_first_line_as_title_toggled();
 
83
  void on_sample_rows_changed();
 
84
 
 
85
  Glib::ustring get_current_encoding() const;
 
86
  void begin_parse();
 
87
  void encoding_error();
 
88
 
 
89
  bool on_idle_parse();
 
90
  void handle_line(const Glib::ustring& line, unsigned int line_number);
 
91
 
 
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);
 
95
 
 
96
  void set_state(State state);
 
97
  void validate_primary_key();
 
98
 
 
99
  class EncodingColumns: public Gtk::TreeModelColumnRecord
 
100
  {
 
101
  public:
 
102
    EncodingColumns() { add(m_col_name); add(m_col_charset); }
 
103
 
 
104
    Gtk::TreeModelColumn<Glib::ustring> m_col_name;
 
105
    Gtk::TreeModelColumn<Glib::ustring> m_col_charset;
 
106
  };
 
107
  
 
108
  class FieldColumns: public Gtk::TreeModelColumnRecord
 
109
  {
 
110
  public:
 
111
    FieldColumns() { add(m_col_field_name); add(m_col_field); }
 
112
 
 
113
    Gtk::TreeModelColumn<Glib::ustring> m_col_field_name;
 
114
    Gtk::TreeModelColumn<sharedptr<Field> > m_col_field;
 
115
  };
 
116
 
 
117
  class SampleColumns: public Gtk::TreeModelColumnRecord
 
118
  {
 
119
  public:
 
120
    SampleColumns() { add(m_col_row); }
 
121
 
 
122
    Gtk::TreeModelColumn<int> m_col_row;
 
123
  };
 
124
 
 
125
  EncodingColumns m_encoding_columns;
 
126
  Glib::RefPtr<Gtk::ListStore> m_encoding_model;
 
127
 
 
128
  FieldColumns m_field_columns;
 
129
  Glib::RefPtr<Gtk::ListStore> m_field_model;
 
130
  Glib::RefPtr<Gtk::TreeModelSort> m_field_model_sorted;
 
131
 
 
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;
 
142
 
 
143
  Glib::RefPtr<Gio::File> m_file;
 
144
  Glib::RefPtr<Gio::FileInputStream> m_stream;
 
145
  Glib::ustring m_filename;
 
146
 
 
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
 
149
  // the encoding.
 
150
  std::vector<char> m_raw;
 
151
 
 
152
  struct Buffer {
 
153
    char buf[1024];
 
154
  };
 
155
  std::auto_ptr<Buffer> m_buffer;
 
156
 
 
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;
 
161
 
 
162
  // We use the low-level Glib::IConv routines to progressively convert the
 
163
  // input data in an idle handler.
 
164
  struct Parser {
 
165
    Glib::IConv conv;
 
166
    std::vector<char>::size_type input_position;
 
167
    std::string current_line;
 
168
    sigc::connection idle_connection;
 
169
    unsigned int line_number;
 
170
 
 
171
    Parser(const char* encoding): conv("UTF-8", encoding), input_position(0), line_number(0) {}
 
172
    ~Parser() { idle_connection.disconnect(); }
 
173
  };
 
174
 
 
175
  std::auto_ptr<Parser> m_parser;
 
176
  State m_state;
 
177
 
 
178
  // Parsed data:
 
179
  std::vector<std::vector<Glib::ustring> > m_rows;
 
180
 
 
181
  // The fields into which to import the data:
 
182
  typedef std::vector< sharedptr<Field> > type_vec_fields;
 
183
  type_vec_fields m_fields;
 
184
 
 
185
  SignalStateChanged m_signal_state_changed;
 
186
};
 
187
 
 
188
} //namespace Glom
 
189
 
 
190
#endif //GLOM_DIALOG_IMPORT_CSV_H
 
191