Browse Source

Improved checking for file changes outside of juCi++

merge-requests/365/head
eidheim 9 years ago
parent
commit
319283bc6f
  1. 14
      src/source.cc
  2. 2
      src/source.h
  3. 20
      src/source_diff.cc
  4. 2
      src/source_diff.h

14
src/source.cc

@ -91,7 +91,6 @@ const std::regex Source::View::no_bracket_no_para_statement_regex("^([ \\t]*)(el
Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language): Gsv::View(), SpellCheckView(), DiffView(file_path), language(language), status_diagnostics(0, 0, 0) { Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language): Gsv::View(), SpellCheckView(), DiffView(file_path), language(language), status_diagnostics(0, 0, 0) {
get_source_buffer()->begin_not_undoable_action(); get_source_buffer()->begin_not_undoable_action();
last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
if(language) { if(language) {
if(filesystem::read_non_utf8(file_path, get_buffer())==-1) if(filesystem::read_non_utf8(file_path, get_buffer())==-1)
Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n"); Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n");
@ -454,7 +453,10 @@ bool Source::View::save(const std::vector<Source::View*> &views) {
cleanup_whitespace_characters(); cleanup_whitespace_characters();
if(filesystem::write(file_path, get_buffer())) { if(filesystem::write(file_path, get_buffer())) {
last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); boost::system::error_code ec;
last_write_time=boost::filesystem::last_write_time(file_path, ec);
if(ec)
last_write_time=static_cast<std::time_t>(-1);
get_buffer()->set_modified(false); get_buffer()->set_modified(false);
Directories::get().on_save_file(file_path); Directories::get().on_save_file(file_path);
return true; return true;
@ -2049,14 +2051,6 @@ bool Source::View::on_button_press_event(GdkEventButton *event) {
return Gsv::View::on_button_press_event(event); return Gsv::View::on_button_press_event(event);
} }
bool Source::View::on_focus_in_event(GdkEventFocus* focus_event){
boost::system::error_code ec;
auto last_write_time=boost::filesystem::last_write_time(file_path, ec);
if(!ec && last_write_time>last_read_time)
Info::get().print("Caution: " + file_path.filename().string() + " was altered outside of juCi++");
return Gsv::View::on_focus_in_event(focus_event);
};
std::pair<char, unsigned> Source::View::find_tab_char_and_size() { std::pair<char, unsigned> Source::View::find_tab_char_and_size() {
std::unordered_map<char, size_t> tab_chars; std::unordered_map<char, size_t> tab_chars;
std::unordered_map<unsigned, size_t> tab_sizes; std::unordered_map<unsigned, size_t> tab_sizes;

2
src/source.h

@ -103,7 +103,6 @@ namespace Source {
virtual void soft_reparse() {soft_reparse_needed=false;} virtual void soft_reparse() {soft_reparse_needed=false;}
virtual void full_reparse() {full_reparse_needed=false;} virtual void full_reparse() {full_reparse_needed=false;}
protected: protected:
std::time_t last_read_time;
bool parsed=false; bool parsed=false;
Tooltips diagnostic_tooltips; Tooltips diagnostic_tooltips;
Tooltips type_tooltips; Tooltips type_tooltips;
@ -146,7 +145,6 @@ namespace Source {
bool on_key_press_event_smart_brackets(GdkEventKey* key); bool on_key_press_event_smart_brackets(GdkEventKey* key);
bool on_key_press_event_smart_inserts(GdkEventKey* key); bool on_key_press_event_smart_inserts(GdkEventKey* key);
bool on_button_press_event(GdkEventButton *event) override; bool on_button_press_event(GdkEventButton *event) override;
bool on_focus_in_event(GdkEventFocus* focus_event) override;
std::pair<char, unsigned> find_tab_char_and_size(); std::pair<char, unsigned> find_tab_char_and_size();
unsigned tab_size; unsigned tab_size;

20
src/source_diff.cc

@ -55,6 +55,16 @@ Source::DiffView::DiffView(const boost::filesystem::path &file_path) : Gsv::View
renderer->tag_removed_below=get_buffer()->create_tag(); renderer->tag_removed_below=get_buffer()->create_tag();
renderer->tag_removed_above=get_buffer()->create_tag(); renderer->tag_removed_above=get_buffer()->create_tag();
boost::system::error_code ec;
last_write_time=boost::filesystem::last_write_time(file_path, ec);
if(ec)
last_write_time=static_cast<std::time_t>(-1);
signal_focus_in_event().connect([this](GdkEventFocus *event) {
check_last_write_time();
return false;
});
configure(); configure();
} }
@ -74,6 +84,15 @@ Source::DiffView::~DiffView() {
} }
} }
void Source::DiffView::check_last_write_time() {
if(has_focus()) {
boost::system::error_code ec;
auto last_write_time=boost::filesystem::last_write_time(file_path, ec);
if(!ec && this->last_write_time!=static_cast<std::time_t>(-1) && last_write_time!=this->last_write_time)
Info::get().print("Caution: " + file_path.filename().string() + " was changed outside of juCi++");
}
}
void Source::DiffView::configure() { void Source::DiffView::configure() {
if(Config::get().source.show_git_diff) { if(Config::get().source.show_git_diff) {
if(repository) if(repository)
@ -158,6 +177,7 @@ void Source::DiffView::configure() {
if(monitor_event!=Gio::FileMonitorEvent::FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { if(monitor_event!=Gio::FileMonitorEvent::FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
delayed_monitor_changed_connection.disconnect(); delayed_monitor_changed_connection.disconnect();
delayed_monitor_changed_connection=Glib::signal_timeout().connect([this]() { delayed_monitor_changed_connection=Glib::signal_timeout().connect([this]() {
check_last_write_time();
monitor_changed=true; monitor_changed=true;
parse_state=ParseState::STARTING; parse_state=ParseState::STARTING;
std::unique_lock<std::mutex> lock(parse_mutex); std::unique_lock<std::mutex> lock(parse_mutex);

2
src/source_diff.h

@ -36,6 +36,8 @@ namespace Source {
boost::filesystem::path file_path; boost::filesystem::path file_path;
protected: protected:
std::mutex file_path_mutex; std::mutex file_path_mutex;
std::time_t last_write_time;
void check_last_write_time();
public: public:
virtual void configure(); virtual void configure();

Loading…
Cancel
Save