Changeset fe534f331f514bb65fb6f20f00cee4a7c22266fc
- Timestamp:
- 01/06/07 23:57:46 (6 years ago)
- Author:
- Philipp Kern <phil@…>
- Parents:
- 3ccecb19e9700eb868504cd7b5927768be794791
- Children:
- 1ae339e56a5d9161629369b17d26f17500ad317e
- git-committer:
- Philipp Kern <phil@0x539.de> / 2007-01-06T22:57:46Z+0000
- Message:
-
[project @ Added encoding selection in file chooser]
Original author: Armin Burgmeier <armin@…>
Date: 2006-03-05 18:59:22+00:00
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
rcf2a884
|
rfe534f33
|
|
| 23 | 23 | noinst_HEADERS += inc/mimemap.hpp |
| 24 | 24 | noinst_HEADERS += inc/regex.hpp |
| | 25 | noinst_HEADERS += inc/encoding_selector.hpp |
| 25 | 26 | noinst_HEADERS += inc/defaultdialog.hpp |
| 26 | 27 | noinst_HEADERS += inc/historyentry.hpp |
| … |
… |
|
| 76 | 77 | gobby_SOURCES += src/mimemap.cpp |
| 77 | 78 | gobby_SOURCES += src/regex.cpp |
| | 79 | gobby_SOURCES += src/encoding_selector.cpp |
| 78 | 80 | gobby_SOURCES += src/defaultdialog.cpp |
| 79 | 81 | gobby_SOURCES += src/historyentry.cpp |
-
|
r8ec5e5b
|
rfe534f33
|
|
| 26 | 26 | { |
| 27 | 27 | |
| 28 | | class Encoding |
| | 28 | namespace Encoding |
| 29 | 29 | { |
| 30 | | public: |
| 31 | | enum Charset { |
| 32 | | UTF_7, |
| 33 | | UTF_8, |
| 34 | | UTF_16, |
| 35 | | ISO_8859_1, |
| 36 | | ISO_8859_15, |
| 37 | | UCS_2, |
| 38 | | UCS_4 |
| 39 | | }; |
| 40 | 30 | |
| 41 | | Encoding(const Glib::ustring& name, Charset charset); |
| 42 | | Encoding(const Encoding& other); |
| 43 | | ~Encoding(); |
| | 31 | const std::vector<std::string>& get_encodings(); |
| | 32 | Glib::ustring convert_to_utf8(const std::string& str, |
| | 33 | std::string& encoding); |
| 44 | 34 | |
| 45 | | Encoding& operator=(const Encoding& other); |
| 46 | | |
| 47 | | const Glib::ustring& get_name() const; |
| 48 | | Charset get_charset() const; |
| 49 | | |
| 50 | | Glib::ustring convert_to_utf8(const std::string& str); |
| 51 | | protected: |
| 52 | | Glib::ustring m_name; |
| 53 | | Charset m_charset; |
| 54 | | }; |
| 55 | | |
| 56 | | Glib::ustring convert_to_utf8(const std::string& str); |
| | 35 | } |
| 57 | 36 | |
| 58 | 37 | } |
| 59 | 38 | |
| 60 | 39 | #endif // _GOBBY_ENCODING_HPP_ |
| 61 | | |
-
|
rcf2a884
|
rfe534f33
|
|
| 60 | 60 | * local filesystem. |
| 61 | 61 | */ |
| 62 | | void open_local_file(const Glib::ustring& file); |
| | 62 | void open_local_file(const Glib::ustring& file, |
| | 63 | const std::string& encoding); |
| 63 | 64 | |
| 64 | 65 | /** Saves an existing document to the given path. |
| 65 | 66 | */ |
| 66 | | void save_local_file(DocWindow& doc, const Glib::ustring& file); |
| | 67 | void save_local_file(DocWindow& doc, |
| | 68 | const Glib::ustring& file, |
| | 69 | const std::string& encoding); |
| 67 | 70 | protected: |
| 68 | 71 | // Gtk::Window overrides |
| … |
… |
|
| 131 | 134 | // Paths |
| 132 | 135 | std::string m_last_path; |
| | 136 | std::string m_prev_session; |
| | 137 | |
| 133 | 138 | std::string m_local_file_path; |
| 134 | | std::string m_prev_session; |
| | 139 | std::string m_local_encoding; |
| 135 | 140 | |
| 136 | 141 | // GUI |
-
|
r7bf5515
|
rfe534f33
|
|
| 1 | 1 | # source files |
| 2 | 2 | |
| 3 | | src/io/buffer_wrapper.cpp |
| 4 | 3 | src/icon.cpp |
| 5 | 4 | src/encoding.cpp |
| | 5 | src/encoding_selector.cpp |
| 6 | 6 | src/header.cpp |
| 7 | 7 | src/statusbar.cpp |
-
|
r49defa1
|
rfe534f33
|
|
| 25 | 25 | #endif |
| 26 | 26 | |
| | 27 | #include "encoding_selector.hpp" |
| 27 | 28 | #include "dragdrop.hpp" |
| 28 | 29 | #include "window.hpp" |
| … |
… |
|
| 177 | 178 | DragQueryFileA(drop, i, buf, size + 1); |
| 178 | 179 | |
| 179 | | m_window.open_local_file(buf); |
| | 180 | m_window.open_local_file( |
| | 181 | buf, |
| | 182 | Gobby::EncodingSelector::AUTO_DETECT |
| | 183 | ); |
| | 184 | |
| 180 | 185 | delete[] buf; |
| 181 | 186 | } |
| … |
… |
|
| 235 | 240 | } |
| 236 | 241 | |
| 237 | | window.open_local_file(filename); |
| | 242 | window.open_local_file( |
| | 243 | filename, |
| | 244 | Gobby::EncodingSelector::AUTO_DETECT |
| | 245 | ); |
| 238 | 246 | } |
| 239 | 247 | } |
-
|
r65216bf
|
rfe534f33
|
|
| 17 | 17 | */ |
| 18 | 18 | |
| | 19 | #include <cstdlib> |
| 19 | 20 | #include "common.hpp" |
| 20 | 21 | #include "encoding.hpp" |
| … |
… |
|
| 22 | 23 | namespace |
| 23 | 24 | { |
| 24 | | // Available encodings |
| 25 | | Gobby::Encoding encodings[] = { |
| 26 | | Gobby::Encoding("UTF-8", Gobby::Encoding::UTF_8), |
| 27 | | Gobby::Encoding("ISO_8859-1", Gobby::Encoding::ISO_8859_1), |
| 28 | | Gobby::Encoding("ISO_8859-15", Gobby::Encoding::ISO_8859_15), |
| 29 | | Gobby::Encoding("UTF-7", Gobby::Encoding::UTF_7), |
| 30 | | Gobby::Encoding("UTF-16", Gobby::Encoding::UTF_16), |
| 31 | | Gobby::Encoding("UCS-2", Gobby::Encoding::UCS_2), |
| 32 | | Gobby::Encoding("UCS-4", Gobby::Encoding::UCS_4) |
| 33 | | }; |
| 34 | 25 | |
| 35 | | // Amount of encodings we have |
| 36 | | unsigned int encoding_count = sizeof(encodings) / sizeof(encodings[0]); |
| 37 | | } |
| 38 | | |
| 39 | | Gobby::Encoding::Encoding(const Glib::ustring& name, Charset charset) |
| 40 | | : m_name(name), m_charset(charset) |
| | 26 | Glib::ustring convert_to_utf8(const std::string& str, const std::string& from) |
| 41 | 27 | { |
| 42 | | } |
| 43 | | |
| 44 | | Gobby::Encoding::Encoding(const Encoding& other) |
| 45 | | : m_name(other.m_name), m_charset(other.m_charset) |
| 46 | | { |
| 47 | | } |
| 48 | | |
| 49 | | Gobby::Encoding::~Encoding() |
| 50 | | { |
| 51 | | } |
| 52 | | |
| 53 | | Gobby::Encoding& Gobby::Encoding::operator=(const Encoding& other) |
| 54 | | { |
| 55 | | m_name = other.m_name; |
| 56 | | m_charset = other.m_charset; |
| 57 | | return *this; |
| 58 | | } |
| 59 | | |
| 60 | | const Glib::ustring& Gobby::Encoding::get_name() const |
| 61 | | { |
| 62 | | return m_name; |
| 63 | | } |
| 64 | | |
| 65 | | Gobby::Encoding::Charset Gobby::Encoding::get_charset() const |
| 66 | | { |
| 67 | | return m_charset; |
| 68 | | } |
| 69 | | |
| 70 | | Glib::ustring Gobby::Encoding::convert_to_utf8(const std::string& str) |
| 71 | | { |
| 72 | | Glib::ustring utf8 = Glib::convert(str, "UTF-8", m_name); |
| | 28 | Glib::ustring utf8 = Glib::convert(str, "UTF-8", from); |
| 73 | 29 | if(!utf8.validate() ) |
| 74 | 30 | { |
| … |
… |
|
| 78 | 34 | ); |
| 79 | 35 | } |
| | 36 | |
| 80 | 37 | return utf8; |
| 81 | 38 | } |
| 82 | 39 | |
| 83 | | Glib::ustring Gobby::convert_to_utf8(const std::string& str) |
| | 40 | } |
| | 41 | |
| | 42 | const std::vector<std::string>& Gobby::Encoding::get_encodings() |
| | 43 | { |
| | 44 | static const std::string encodings[] = { |
| | 45 | "UTF-8", |
| | 46 | "ISO-8859-1", |
| | 47 | "ISO-8859-15", |
| | 48 | "UTF-7", |
| | 49 | "UTF-16", |
| | 50 | "UCS-2", |
| | 51 | "UCS-4" |
| | 52 | }; |
| | 53 | |
| | 54 | static const std::size_t encoding_count = |
| | 55 | sizeof(encodings) / sizeof(encodings[0]); |
| | 56 | |
| | 57 | static std::vector<std::string> encoding_vec( |
| | 58 | encodings, |
| | 59 | encodings + encoding_count |
| | 60 | ); |
| | 61 | |
| | 62 | return encoding_vec; |
| | 63 | } |
| | 64 | |
| | 65 | Glib::ustring Gobby::Encoding::convert_to_utf8(const std::string& str, |
| | 66 | std::string& encoding) |
| 84 | 67 | { |
| 85 | 68 | if(g_utf8_validate(str.c_str(), str.length(), NULL) == TRUE) |
| | 69 | { |
| | 70 | encoding = "UTF-8"; |
| 86 | 71 | return str; |
| | 72 | } |
| 87 | 73 | |
| 88 | | // Ignore UTF-8 encoding we currently checked |
| 89 | | for(unsigned int i = 1; i < encoding_count; ++ i) |
| | 74 | typedef std::vector<std::string> encoding_list_type; |
| | 75 | const encoding_list_type& encodings = get_encodings(); |
| | 76 | |
| | 77 | for(encoding_list_type::const_iterator iter = encodings.begin(); |
| | 78 | iter != encodings.end(); |
| | 79 | ++ iter) |
| 90 | 80 | { |
| | 81 | // Ignore UTF-8 encoding we currently checked |
| | 82 | if(*iter == "UTF-8") continue; |
| | 83 | |
| 91 | 84 | try |
| 92 | 85 | { |
| 93 | | return encodings[i].convert_to_utf8(str); |
| | 86 | encoding = *iter; |
| | 87 | return ::convert_to_utf8(str, *iter); |
| 94 | 88 | } |
| 95 | 89 | catch(Glib::ConvertError& e) |
-
|
r3ccecb1
|
rfe534f33
|
|
| 35 | 35 | #include "common.hpp" |
| 36 | 36 | #include "encoding.hpp" |
| | 37 | #include "encoding_selector.hpp" |
| 37 | 38 | #include "docwindow.hpp" |
| 38 | 39 | #include "passworddialog.hpp" |
| … |
… |
|
| 604 | 605 | // " " is newly created, so we do not need a path |
| 605 | 606 | if(m_local_file_path != " ") |
| | 607 | { |
| 606 | 608 | window.set_path(m_local_file_path); |
| 607 | | |
| 608 | | // Crear local path |
| | 609 | // TODO: Set document's encoding to m_local_encoding |
| | 610 | // in document settings |
| | 611 | } |
| | 612 | |
| | 613 | // Clear local path |
| 609 | 614 | m_local_file_path.clear(); |
| 610 | 615 | } |
| … |
… |
|
| 661 | 666 | { |
| 662 | 667 | // Create FileChooser |
| 663 | | Gtk::FileChooserDialog dlg(*this, _("Open new document")); |
| | 668 | EncodingFileChooserDialog dlg( |
| | 669 | *this, |
| | 670 | _("Open new document"), |
| | 671 | Gtk::FILE_CHOOSER_ACTION_OPEN |
| | 672 | ); |
| | 673 | |
| | 674 | dlg.get_selector().set_encoding(EncodingSelector::AUTO_DETECT); |
| 664 | 675 | |
| 665 | 676 | // Use the last used path for this dialog, if we have any |
| … |
… |
|
| 684 | 695 | iter != list.end(); |
| 685 | 696 | ++ iter) |
| 686 | | open_local_file(*iter); |
| | 697 | { |
| | 698 | open_local_file( |
| | 699 | *iter, |
| | 700 | dlg.get_selector().get_encoding() |
| | 701 | ); |
| | 702 | } |
| 687 | 703 | } |
| 688 | 704 | } |
| … |
… |
|
| 703 | 719 | if(!doc->get_path().empty() ) |
| 704 | 720 | // Yes, so save the document there |
| 705 | | save_local_file(*doc, doc->get_path() ); |
| | 721 | // TODO: Lookup document's encoding |
| | 722 | save_local_file(*doc, doc->get_path(), "UTF-8"); |
| 706 | 723 | else |
| 707 | 724 | // Open save as dialog otherwise |
| … |
… |
|
| 722 | 739 | |
| 723 | 740 | // Setup dialog |
| 724 | | Gtk::FileChooserDialog dlg(*this, _("Save current document"), |
| 725 | | Gtk::FILE_CHOOSER_ACTION_SAVE); |
| | 741 | EncodingFileChooserDialog dlg( |
| | 742 | *this, |
| | 743 | _("Save current document"), |
| | 744 | Gtk::FILE_CHOOSER_ACTION_SAVE |
| | 745 | ); |
| | 746 | |
| | 747 | // TODO: Preselect document's encoding |
| | 748 | dlg.get_selector().set_encoding("UTF-8"); |
| 726 | 749 | |
| 727 | 750 | #ifdef GTKMM_GEQ_28 |
| … |
… |
|
| 753 | 776 | m_last_path = dlg.get_current_folder(); |
| 754 | 777 | // Save document |
| 755 | | save_local_file(*doc, dlg.get_filename() ); |
| | 778 | save_local_file( |
| | 779 | *doc, |
| | 780 | dlg.get_filename(), |
| | 781 | dlg.get_selector().get_encoding() |
| | 782 | ); |
| 756 | 783 | } |
| 757 | 784 | } |
| … |
… |
|
| 1078 | 1105 | } |
| 1079 | 1106 | |
| 1080 | | void Gobby::Window::open_local_file(const Glib::ustring& file) |
| | 1107 | void Gobby::Window::open_local_file(const Glib::ustring& file, |
| | 1108 | const std::string& encoding) |
| 1081 | 1109 | { |
| 1082 | 1110 | try |
| … |
… |
|
| 1084 | 1112 | // Set local file path for the document_insert callback |
| 1085 | 1113 | m_local_file_path = file; |
| 1086 | | |
| 1087 | | std::string content( |
| 1088 | | convert_to_utf8(Glib::file_get_contents(file)) ); |
| 1089 | | convert2unix(content); |
| | 1114 | m_local_encoding = encoding; |
| | 1115 | |
| | 1116 | std::string utf8_content; |
| | 1117 | if(encoding == EncodingSelector::AUTO_DETECT) |
| | 1118 | { |
| | 1119 | std::string detected_encoding; |
| | 1120 | |
| | 1121 | utf8_content = Encoding::convert_to_utf8( |
| | 1122 | Glib::file_get_contents(file), |
| | 1123 | detected_encoding |
| | 1124 | ); |
| | 1125 | |
| | 1126 | m_local_encoding = detected_encoding; |
| | 1127 | } |
| | 1128 | else |
| | 1129 | { |
| | 1130 | utf8_content = Glib::convert( |
| | 1131 | Glib::file_get_contents(file), |
| | 1132 | "UTF-8", |
| | 1133 | encoding |
| | 1134 | ); |
| | 1135 | } |
| | 1136 | |
| | 1137 | convert2unix(utf8_content); |
| 1090 | 1138 | |
| 1091 | 1139 | m_buffer->document_create( |
| 1092 | | Glib::path_get_basename(file), "UTF-8", content |
| | 1140 | Glib::path_get_basename(file), "UTF-8", utf8_content |
| 1093 | 1141 | ); |
| 1094 | 1142 | } |
| … |
… |
|
| 1100 | 1148 | } |
| 1101 | 1149 | |
| 1102 | | void Gobby::Window::save_local_file(DocWindow& doc, const Glib::ustring& file) |
| | 1150 | void Gobby::Window::save_local_file(DocWindow& doc, |
| | 1151 | const Glib::ustring& file, |
| | 1152 | const std::string& encoding) |
| 1103 | 1153 | { |
| 1104 | 1154 | // Open stream to file |
| … |
… |
|
| 1117 | 1167 | |
| 1118 | 1168 | // Save content into file |
| 1119 | | stream << doc.get_content().raw(); |
| | 1169 | std::string conv_content = doc.get_content().raw(); |
| | 1170 | if(encoding != "UTF-8") |
| | 1171 | { |
| | 1172 | conv_content = Glib::convert( |
| | 1173 | conv_content, |
| | 1174 | encoding, |
| | 1175 | "UTF-8" |
| | 1176 | ); |
| | 1177 | } |
| | 1178 | |
| | 1179 | stream << conv_content; |
| 1120 | 1180 | stream.close(); |
| 1121 | 1181 | |
| … |
… |
|
| 1126 | 1186 | // Unset modifified flag |
| 1127 | 1187 | doc.get_document().get_buffer()->set_modified(false); |
| | 1188 | } |
| | 1189 | catch(Glib::ConvertError& e) |
| | 1190 | { |
| | 1191 | display_error(e.what() ); |
| 1128 | 1192 | } |
| 1129 | 1193 | catch(std::exception& e) |