Changeset 883ac1c0c36c0f5370a1dcb4bcde33f4b72c0a7f

Show
Ignore:
Timestamp:
06/22/08 01:06:41 (5 years ago)
Author:
Armin Burgmeier <armin@…>
Parents:
91aa9b20f84f5db304446175b7e75cccb2f4bab7
Children:
b00f66c045b9598773e5dcadfdcf887be78d63a3
git-committer:
Armin Burgmeier <armin@arbur.net> / 2008-06-22T01:06:41Z+0200
Message:

2008-06-22 Armin Burgmeier <armin@…>

  • inc/util/file.hpp:
  • src/util/file.cpp:
  • inc/util/Makefile.am:
  • src/util/Makefile.am: Added file.hpp/.cpp containing utility functions to deal with the filesystem. Currently, a function that creates a directory and all its parent if necessary is implemented.
  • inc/util/config.hpp:
  • src/util/config.cpp: Make use of that function in the destructor.
  • inc/util/resolv.hpp: Include <memory> since we use std::auto_ptr<>.
  • inc/operations/documentinfostorage.hpp:
  • src/operations/documentinfostorage.cpp: Added class that stores local information on known documents such as the local location, the encoding and the eol style.
  • inc/operations/operation-open.hpp:
  • src/operations/operation-open.cpp: Implemented opening documents from files, via giomm.
  • inc/operations/Makefile.am:
  • src/operations/Makefile.am: Add the new source files to the build.
  • inc/operations/operations.hpp:
  • src/operations/operations.cpp: Added encoding parameter to create_document(), added DocumentInfoStorage? member.
  • inc/commands/file-commands.hpp:
  • src/commands/file-commands.cpp: Added functionlity for the Open menu item.
  • src/main.cpp: Initialize giomm.
  • configure.ac: Added giomm as a dependency.
Files:
7 added
16 modified

Legend:

Unmodified
Added
Removed
  • ChangeLog

    r91aa9b2 r883ac1c  
     12008-06-22  Armin Burgmeier  <armin@0x539.de> 
     2 
     3        * inc/util/file.hpp: 
     4        * src/util/file.cpp: 
     5        * inc/util/Makefile.am: 
     6        * src/util/Makefile.am: Added file.hpp/.cpp containing utility 
     7        functions to deal with the filesystem. Currently, a function that 
     8        creates a directory and all its parent if necessary is implemented. 
     9 
     10        * inc/util/config.hpp: 
     11        * src/util/config.cpp: Make use of that function in the destructor. 
     12 
     13        * inc/util/resolv.hpp: Include <memory> since we use std::auto_ptr<>. 
     14 
     15        * inc/operations/documentinfostorage.hpp: 
     16        * src/operations/documentinfostorage.cpp: Added class that stores 
     17        local information on known documents such as the local location, the 
     18        encoding and the eol style. 
     19 
     20        * inc/operations/operation-open.hpp: 
     21        * src/operations/operation-open.cpp: Implemented opening documents 
     22        from files, via giomm. 
     23 
     24        * inc/operations/Makefile.am: 
     25        * src/operations/Makefile.am: Add the new source files to the build. 
     26 
     27        * inc/operations/operations.hpp: 
     28        * src/operations/operations.cpp: Added encoding parameter to 
     29        create_document(), added DocumentInfoStorage member. 
     30 
     31        * inc/commands/file-commands.hpp: 
     32        * src/commands/file-commands.cpp: Added functionlity for the Open menu 
     33        item. 
     34 
     35        * inc/window.hpp: 
     36        * src/window.cpp: Instantiate a DocumentInfoStorage, pass to 
     37        Operations. 
     38 
     39        * src/main.cpp: Initialize giomm. 
     40 
     41        * configure.ac: Added giomm as a dependency. 
     42 
    1432008-05-19  Armin Burgmeier  <armin@0x539.de> 
    244 
  • configure.ac

    r91aa9b2 r883ac1c  
    3737AM_CONDITIONAL(WIN32, test x$win32 = xtrue) 
    3838 
    39 required_libs="libxml++-2.6 glibmm-2.4 >= 2.16.0 gtkmm-2.4 >= 2.12.0 gthread-2.0 gtksourceview-2.0 libinfinity-1.0 libinftext-1.0 libinfgtk-1.0 libinftextgtk-1.0" 
     39required_libs="libxml++-2.6 glibmm-2.4 >= 2.16.0 giomm-2.4 >= 2.16.0 gtkmm-2.4 >= 2.12.0 gthread-2.0 gtksourceview-2.0 libinfinity-1.0 libinftext-1.0 libinfgtk-1.0 libinftextgtk-1.0" 
    4040 
    4141# Checks for libraries. 
  • inc/commands/file-commands.hpp

    r91aa9b2 r883ac1c  
    2626 
    2727#include <gtkmm/window.h> 
     28#include <gtkmm/filechooserdialog.h> 
    2829#include <sigc++/trackable.h> 
    2930 
     
    3940 
    4041protected: 
     42        enum Mode { 
     43                MODE_NEW, 
     44                MODE_OPEN, 
     45                MODE_SAVE 
     46        }; 
     47 
     48        void create_file_dialog(); 
     49        void create_location_dialog(); 
     50 
    4151        void on_new(); 
     52        void on_open(); 
     53 
     54        void on_file_dialog_response(int id); 
    4255        void on_location_dialog_response(int id); 
    4356 
     
    4760        Operations& m_operations; 
    4861 
     62        std::string m_open_uri; 
     63 
     64        Mode m_mode; 
    4965        std::auto_ptr<DocumentLocationDialog> m_location_dialog; 
     66        std::auto_ptr<Gtk::FileChooserDialog> m_file_dialog; 
    5067}; 
    5168 
  • inc/operations/Makefile.am

    r91aa9b2 r883ac1c  
    11 
    22noinst_HEADERS = \ 
     3        documentinfostorage.hpp \ 
    34        operations.hpp \ 
    4         operation-new.hpp 
     5        operation-new.hpp \ 
     6        operation-open.hpp 
  • inc/operations/operations.hpp

    r91aa9b2 r883ac1c  
    2020#define _GOBBY_OPERATIONS_OPERATIONS_HPP_ 
    2121 
     22#include "operations/documentinfostorage.hpp" 
    2223#include "core/statusbar.hpp" 
    2324 
     
    4748                } 
    4849 
     50                DocumentInfoStorage& get_info_storage() 
     51                { 
     52                        return m_operations.m_info_storage; 
     53                } 
     54 
    4955                void remove() { m_operations.remove_operation(this); } 
    5056        private: 
     
    5258        }; 
    5359 
    54         Operations(StatusBar& status_bar); 
     60        Operations(DocumentInfoStorage& info_storage, StatusBar& status_bar); 
    5561        ~Operations(); 
    5662 
     
    6268                             InfcBrowserIter* parent, 
    6369                             const Glib::ustring name, 
    64                              const Glib::ustring& from_uri); 
     70                             const Glib::ustring& from_uri, 
     71                             const char* encoding); 
    6572 
    6673protected: 
    6774        void remove_operation(Operation* operation); 
    6875 
     76        DocumentInfoStorage& m_info_storage; 
    6977        StatusBar& m_status_bar; 
    7078 
  • inc/util/Makefile.am

    rdddf956 r883ac1c  
    55        defaultaccumulator.hpp \ 
    66        encoding.hpp \ 
     7        file.hpp \ 
    78        i18n.hpp \ 
    89        resolv.hpp \ 
  • inc/util/config.hpp

    rdddf956 r883ac1c  
    3838{ 
    3939public: 
    40         class Error: public Glib::Error 
    41         { 
    42         public: 
    43                 enum Code { 
    44                         PATH_CREATION_FAILED 
    45                 }; 
    46  
    47                 Error(Code error_code, const Glib::ustring& error_message); 
    48                 Code code() const; 
    49         }; 
    50  
    5140        /** @brief Abstract base class for configuration file entries. 
    5241         */ 
  • inc/util/resolv.hpp

    rdddf956 r883ac1c  
    2424#include <glibmm/ustring.h> 
    2525#include <sigc++/sigc++.h> 
     26 
    2627#include <stdexcept> 
     28#include <memory> 
    2729 
    2830namespace Gobby 
  • inc/window.hpp

    r91aa9b2 r883ac1c  
    8989 
    9090        // Functionality 
     91        DocumentInfoStorage m_info_storage; 
    9192        Operations m_operations; 
    9293 
  • src/commands/file-commands.cpp

    r91aa9b2 r883ac1c  
    2020#include "util/i18n.hpp" 
    2121 
     22#include <gtkmm/stock.h> 
     23 
    2224Gobby::FileCommands::FileCommands(Gtk::Window& parent, Header& header, 
    2325                                  const Browser& browser, Folder& folder, 
    2426                                  Operations& operations): 
    2527        m_parent(parent), m_browser(browser), m_folder(folder), 
    26         m_operations(operations) 
     28        m_operations(operations), m_mode(MODE_NEW) 
    2729{ 
    2830        header.action_file_new->signal_activate().connect( 
    2931                sigc::mem_fun(*this, &FileCommands::on_new)); 
     32        header.action_file_open->signal_activate().connect( 
     33                sigc::mem_fun(*this, &FileCommands::on_open)); 
     34} 
     35 
     36void Gobby::FileCommands::create_location_dialog() 
     37{ 
     38        m_location_dialog.reset( 
     39                new DocumentLocationDialog( 
     40                        m_parent, 
     41                        INF_GTK_BROWSER_MODEL( 
     42                                m_browser.get_store()))); 
     43 
     44        m_location_dialog->signal_response().connect( 
     45                sigc::mem_fun( 
     46                        *this, 
     47                        &FileCommands::on_location_dialog_response)); 
     48} 
     49 
     50void Gobby::FileCommands::create_file_dialog() 
     51{ 
     52        m_file_dialog.reset(new Gtk::FileChooserDialog(m_parent, "")); 
     53        m_file_dialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); 
     54        m_file_dialog->add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT); 
     55        m_file_dialog->signal_response().connect( 
     56                sigc::mem_fun(*this, &FileCommands::on_file_dialog_response)); 
    3057} 
    3158 
    3259void Gobby::FileCommands::on_new() 
    3360{ 
     61        if(m_file_dialog.get() != NULL) 
     62                m_file_dialog->hide(); 
     63 
    3464        if(m_location_dialog.get() == NULL) 
     65                create_location_dialog(); 
     66 
     67        m_mode = MODE_NEW; 
     68        m_location_dialog->set_document_name(_("New Document")); 
     69        m_location_dialog->present(); 
     70} 
     71 
     72void Gobby::FileCommands::on_open() 
     73{ 
     74        if(m_location_dialog.get() != NULL) 
     75                m_location_dialog->hide(); 
     76 
     77        if(m_file_dialog.get() == NULL) 
     78                create_file_dialog(); 
     79 
     80        // TODO: Allow multiple selection 
     81        // TODO: Encoding selection in file chooser 
     82        m_mode = MODE_OPEN; 
     83        m_file_dialog->set_title("Choose a text file to open"); 
     84        m_file_dialog->set_action(Gtk::FILE_CHOOSER_ACTION_OPEN); 
     85        m_file_dialog->present(); 
     86} 
     87 
     88void Gobby::FileCommands::on_file_dialog_response(int id) 
     89{ 
     90        if(id == Gtk::RESPONSE_ACCEPT) 
    3591        { 
    36                 m_location_dialog.reset( 
    37                         new DocumentLocationDialog( 
    38                                 m_parent, 
    39                                 INF_GTK_BROWSER_MODEL( 
    40                                         m_browser.get_store()))); 
     92                switch(m_mode) 
     93                { 
     94                case MODE_OPEN: 
     95                        if(m_location_dialog.get() == NULL) 
     96                                create_location_dialog(); 
    4197 
    42                 m_location_dialog->signal_response().connect( 
    43                         sigc::mem_fun( 
    44                                 *this, 
    45                                 &FileCommands::on_location_dialog_response)); 
     98                        // TODO: Handle multiple selection 
     99                        m_open_uri = m_file_dialog->get_uri(); 
     100                        m_location_dialog->set_document_name( 
     101                                Glib::path_get_basename( 
     102                                        m_file_dialog->get_filename())); 
     103                        m_location_dialog->present(); 
     104 
     105                        break; 
     106                case MODE_SAVE: 
     107                        // TODO: 
     108                        break; 
     109                case MODE_NEW: 
     110                default: 
     111                        g_assert_not_reached(); 
     112                } 
    46113        } 
    47114 
    48         m_location_dialog->set_document_name(_("New Document")); 
    49         m_location_dialog->present(); 
     115        m_file_dialog->hide(); 
    50116} 
    51117 
     
    59125                g_assert(browser != NULL); 
    60126 
    61                 m_operations.create_document( 
    62                         browser, &iter, 
    63                         m_location_dialog->get_document_name()); 
     127                switch(m_mode) 
     128                { 
     129                case MODE_NEW: 
     130                        m_operations.create_document( 
     131                                browser, &iter, 
     132                                m_location_dialog->get_document_name()); 
     133                        break; 
     134                case MODE_OPEN: 
     135                        m_operations.create_document( 
     136                                browser, &iter, 
     137                                m_location_dialog->get_document_name(), 
     138                                m_open_uri, NULL); 
     139                        break; 
     140                case MODE_SAVE: 
     141                default: 
     142                        g_assert_not_reached(); 
     143                } 
    64144        } 
    65145 
  • src/main.cpp

    rdddf956 r883ac1c  
    2525#include <gtkmm/main.h> 
    2626#include <gtkmm/messagedialog.h> 
     27#include <giomm/init.h> 
    2728#include <glibmm/optionentry.h> 
    2829#include <glibmm/optiongroup.h> 
    2930#include <glibmm/optioncontext.h> 
    3031 
     32#include <libintl.h> // bindtextdomain 
    3133#include <iostream> 
    3234 
     
    4850        g_thread_init(NULL); 
    4951        gnutls_global_init(); 
     52        Gio::init(); 
    5053 
    5154        setlocale(LC_ALL, ""); 
  • src/operations/Makefile.am

    r91aa9b2 r883ac1c  
    22 
    33libgobby_operations_a_SOURCES = \ 
     4        documentinfostorage.cpp \ 
    45        operations.cpp \ 
    5         operation-new.cpp 
     6        operation-new.cpp \ 
     7        operation-open.cpp 
    68 
    79libgobby_operations_a_CPPFLAGS = $(gobby_CFLAGS) -I../../inc/ \ 
  • src/operations/operations.cpp

    r91aa9b2 r883ac1c  
    1818 
    1919#include "operations/operation-new.hpp" 
     20#include "operations/operation-open.hpp" 
    2021#include "operations/operations.hpp" 
    2122 
     
    2526Gobby::Operations::Operation::~Operation() {} 
    2627 
    27 Gobby::Operations::Operations(StatusBar& status_bar): 
    28         m_status_bar(status_bar) 
     28Gobby::Operations::Operations(DocumentInfoStorage& info_storage, 
     29                              StatusBar& status_bar): 
     30        m_info_storage(info_storage), m_status_bar(status_bar) 
    2931{ 
    3032} 
     
    4951                                        InfcBrowserIter* parent, 
    5052                                        const Glib::ustring name, 
    51                                         const Glib::ustring& from_uri) 
     53                                        const Glib::ustring& from_uri, 
     54                                        const char* encoding) 
    5255{ 
     56        m_operations.insert(new OperationOpen(*this, browser, parent, name, 
     57                                              from_uri, encoding)); 
    5358} 
    5459 
  • src/util/Makefile.am

    rdddf956 r883ac1c  
    55        config.cpp \ 
    66        encoding.cpp \ 
     7        file.cpp \ 
    78        i18n.cpp \ 
    89        resolv.cpp \ 
  • src/util/config.cpp

    rdddf956 r883ac1c  
    1818 
    1919#include "util/config.hpp" 
     20#include "util/file.hpp" 
    2021#include "util/i18n.hpp" 
    2122 
     
    2627#include <libxml++/exceptions/exception.h> 
    2728 
    28 // For mkdir / CreateDirectory 
    29 #ifdef WIN32 
    30 #include <windows.h> 
    31 #else 
    32 #include <sys/stat.h> 
    33 #include <sys/types.h> 
    34 #include <cerrno> 
    35 #endif 
    36  
    37 #include <cstring> 
    3829#include <stdexcept> 
    3930 
    4031namespace 
    4132{ 
    42         // Creates a new directory 
    43         void create_directory(const char* path) 
    44         { 
    45                 using namespace Gobby; 
    46 #ifdef WIN32 
    47                 if(CreateDirectoryA(path, NULL) == FALSE) 
    48                 { 
    49                         LPVOID msgbuf; 
    50                         DWORD err = GetLastError(); 
    51  
    52                         FormatMessageA( 
    53                                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    54                                 FORMAT_MESSAGE_FROM_SYSTEM, 
    55                                 NULL, 
    56                                 err, 
    57                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
    58                                 reinterpret_cast<LPSTR>(&msgbuf), 
    59                                 0, 
    60                                 NULL 
    61                         ); 
    62  
    63                         std::string error_message = static_cast<LPSTR>(msgbuf); 
    64                         LocalFree(msgbuf); 
    65  
    66                         // TODO: Convert to UTF-8? 
    67  
    68                         throw Gobby::Config::Error( 
    69                                 Gobby::Config::Error::PATH_CREATION_FAILED, 
    70                                 Glib::ustring::compose( 
    71                                         _("Could not create directory " 
    72                                           "\"%1\": %2"), std::string(path), 
    73                                         error_message)); 
    74                 } 
    75 #else 
    76                 if(mkdir(path, 0755) == -1) 
    77                 { 
    78                         throw Gobby::Config::Error( 
    79                                 Gobby::Config::Error::PATH_CREATION_FAILED, 
    80                                 Glib::ustring::compose( 
    81                                         _("Could not create directory " 
    82                                           "\"%1\": %2"), std::string(path), 
    83                                         strerror(errno))); 
    84                 } 
    85 #endif 
    86         } 
    87  
    88         void create_path_to(const std::string& to) 
    89         { 
    90                 // Directory exists, nothing to do 
    91                 if(Glib::file_test(to, Glib::FILE_TEST_IS_DIR) ) 
    92                         return; 
    93  
    94                 // Find path to the directory to create 
    95                 Glib::ustring path_to = Glib::path_get_dirname(to); 
    96  
    97                 // Create this path, if it doesn't exists 
    98                 create_path_to(path_to); 
    99  
    100                 // Create new directory 
    101                 create_directory(to.c_str() ); 
    102         } 
    103  
    10433        template<typename Map> 
    10534        typename Map::mapped_type ptrmap_find(const Map& map, 
     
    11039                return iter->second; 
    11140        } 
    112 } 
    113  
    114 Gobby::Config::Error::Error(Code error_code, 
    115                             const Glib::ustring& error_message): 
    116         Glib::Error( 
    117                 g_quark_from_static_string("GOBBY_CONFIG_ERROR"), 
    118                 static_cast<int>(error_code), 
    119                 error_message 
    120         ) 
    121 { 
    122 } 
    123  
    124 Gobby::Config::Error::Code Gobby::Config::Error::code() const 
    125 { 
    126         return static_cast<Code>(gobject_->code); 
    12741} 
    12842 
     
    318232        { 
    319233                Glib::ustring dirname = Glib::path_get_dirname(m_filename); 
    320                 create_path_to(dirname); 
     234                create_directory_with_parents(dirname); 
    321235 
    322236                document.write_to_file_formatted(m_filename, "UTF-8"); 
  • src/window.cpp

    r91aa9b2 r883ac1c  
    3131        m_statusbar(m_folder), 
    3232        m_browser(*this, Plugins::TEXT, m_statusbar, m_preferences), 
    33         m_operations(m_statusbar),  
     33        m_info_storage(INF_GTK_BROWSER_MODEL(m_browser.get_store())), 
     34        m_operations(m_info_storage, m_statusbar),  
    3435        m_commands_browser(m_browser, m_folder, m_statusbar, m_preferences), 
    3536        m_commands_file(*this, m_header, m_browser, m_folder, m_operations)