Changeset fe534f331f514bb65fb6f20f00cee4a7c22266fc

Show
Ignore:
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:
2 added
7 modified

Legend:

Unmodified
Added
Removed
  • Makefile.am

    rcf2a884 rfe534f33  
    2323noinst_HEADERS += inc/mimemap.hpp 
    2424noinst_HEADERS += inc/regex.hpp 
     25noinst_HEADERS += inc/encoding_selector.hpp 
    2526noinst_HEADERS += inc/defaultdialog.hpp 
    2627noinst_HEADERS += inc/historyentry.hpp 
     
    7677gobby_SOURCES += src/mimemap.cpp 
    7778gobby_SOURCES += src/regex.cpp 
     79gobby_SOURCES += src/encoding_selector.cpp 
    7880gobby_SOURCES += src/defaultdialog.cpp 
    7981gobby_SOURCES += src/historyentry.cpp 
  • inc/encoding.hpp

    r8ec5e5b rfe534f33  
    2626{ 
    2727 
    28 class Encoding 
     28namespace Encoding 
    2929{ 
    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         }; 
    4030 
    41         Encoding(const Glib::ustring& name, Charset charset); 
    42         Encoding(const Encoding& other); 
    43         ~Encoding(); 
     31const std::vector<std::string>& get_encodings(); 
     32Glib::ustring convert_to_utf8(const std::string& str, 
     33                              std::string& encoding); 
    4434 
    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} 
    5736 
    5837} 
    5938 
    6039#endif // _GOBBY_ENCODING_HPP_ 
    61  
  • inc/window.hpp

    rcf2a884 rfe534f33  
    6060         * local filesystem. 
    6161         */ 
    62         void open_local_file(const Glib::ustring& file); 
     62        void open_local_file(const Glib::ustring& file, 
     63                             const std::string& encoding); 
    6364 
    6465        /** Saves an existing document to the given path. 
    6566         */ 
    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); 
    6770protected: 
    6871        // Gtk::Window overrides 
     
    131134        // Paths 
    132135        std::string m_last_path; 
     136        std::string m_prev_session; 
     137 
    133138        std::string m_local_file_path; 
    134         std::string m_prev_session; 
     139        std::string m_local_encoding; 
    135140 
    136141        // GUI 
  • po/POTFILES.in

    r7bf5515 rfe534f33  
    11# source files 
    22 
    3 src/io/buffer_wrapper.cpp 
    43src/icon.cpp 
    54src/encoding.cpp 
     5src/encoding_selector.cpp 
    66src/header.cpp 
    77src/statusbar.cpp 
  • src/dragdrop.cpp

    r49defa1 rfe534f33  
    2525#endif 
    2626 
     27#include "encoding_selector.hpp" 
    2728#include "dragdrop.hpp" 
    2829#include "window.hpp" 
     
    177178                        DragQueryFileA(drop, i, buf, size + 1); 
    178179 
    179                         m_window.open_local_file(buf); 
     180                        m_window.open_local_file( 
     181                                buf, 
     182                                Gobby::EncodingSelector::AUTO_DETECT 
     183                        ); 
     184 
    180185                        delete[] buf; 
    181186                } 
     
    235240                        } 
    236241 
    237                         window.open_local_file(filename); 
     242                        window.open_local_file( 
     243                                filename, 
     244                                Gobby::EncodingSelector::AUTO_DETECT 
     245                        ); 
    238246                } 
    239247        } 
  • src/encoding.cpp

    r65216bf rfe534f33  
    1717 */ 
    1818 
     19#include <cstdlib> 
    1920#include "common.hpp" 
    2021#include "encoding.hpp" 
     
    2223namespace 
    2324{ 
    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         }; 
    3425 
    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) 
     26Glib::ustring convert_to_utf8(const std::string& str, const std::string& from) 
    4127{ 
    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); 
    7329        if(!utf8.validate() ) 
    7430        { 
     
    7834                ); 
    7935        } 
     36 
    8037        return utf8; 
    8138} 
    8239 
    83 Glib::ustring Gobby::convert_to_utf8(const std::string& str) 
     40} 
     41 
     42const 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 
     65Glib::ustring Gobby::Encoding::convert_to_utf8(const std::string& str, 
     66                                               std::string& encoding) 
    8467{ 
    8568        if(g_utf8_validate(str.c_str(), str.length(), NULL) == TRUE) 
     69        { 
     70                encoding = "UTF-8"; 
    8671                return str; 
     72        } 
    8773 
    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) 
    9080        { 
     81                // Ignore UTF-8 encoding we currently checked 
     82                if(*iter == "UTF-8") continue; 
     83 
    9184                try 
    9285                { 
    93                         return encodings[i].convert_to_utf8(str); 
     86                        encoding = *iter; 
     87                        return ::convert_to_utf8(str, *iter); 
    9488                } 
    9589                catch(Glib::ConvertError& e) 
  • src/window.cpp

    r3ccecb1 rfe534f33  
    3535#include "common.hpp" 
    3636#include "encoding.hpp" 
     37#include "encoding_selector.hpp" 
    3738#include "docwindow.hpp" 
    3839#include "passworddialog.hpp" 
     
    604605                // " " is newly created, so we do not need a path 
    605606                if(m_local_file_path != " ") 
     607                { 
    606608                        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 
    609614                m_local_file_path.clear(); 
    610615        } 
     
    661666{ 
    662667        // 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); 
    664675 
    665676        // Use the last used path for this dialog, if we have any 
     
    684695                    iter != list.end(); 
    685696                    ++ iter) 
    686                         open_local_file(*iter); 
     697                { 
     698                        open_local_file( 
     699                                *iter, 
     700                                dlg.get_selector().get_encoding() 
     701                        ); 
     702                } 
    687703        } 
    688704} 
     
    703719        if(!doc->get_path().empty() ) 
    704720                // 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"); 
    706723        else 
    707724                // Open save as dialog otherwise 
     
    722739 
    723740        // 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"); 
    726749 
    727750#ifdef GTKMM_GEQ_28 
     
    753776                m_last_path = dlg.get_current_folder(); 
    754777                // 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                ); 
    756783        } 
    757784} 
     
    10781105} 
    10791106 
    1080 void Gobby::Window::open_local_file(const Glib::ustring& file) 
     1107void Gobby::Window::open_local_file(const Glib::ustring& file, 
     1108                                    const std::string& encoding) 
    10811109{ 
    10821110        try 
     
    10841112                // Set local file path for the document_insert callback 
    10851113                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); 
    10901138 
    10911139                m_buffer->document_create( 
    1092                         Glib::path_get_basename(file), "UTF-8", content 
     1140                        Glib::path_get_basename(file), "UTF-8", utf8_content 
    10931141                ); 
    10941142        } 
     
    11001148} 
    11011149 
    1102 void Gobby::Window::save_local_file(DocWindow& doc, const Glib::ustring& file) 
     1150void Gobby::Window::save_local_file(DocWindow& doc, 
     1151                                    const Glib::ustring& file, 
     1152                                    const std::string& encoding) 
    11031153{ 
    11041154        // Open stream to file 
     
    11171167 
    11181168                // 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; 
    11201180                stream.close(); 
    11211181 
     
    11261186                // Unset modifified flag 
    11271187                doc.get_document().get_buffer()->set_modified(false); 
     1188        } 
     1189        catch(Glib::ConvertError& e) 
     1190        { 
     1191                display_error(e.what() ); 
    11281192        } 
    11291193        catch(std::exception& e)