Changeset 55a539329eeb4bd6efda73fcde6ecc5e7e4a83eb


Ignore:
Timestamp:
11/07/10 23:20:07 (3 years ago)
Author:
Matias De la Puente <mfpuente.ar@…>
Children:
53ea8825b659440db8993e7e37898c851e59d94d
Parents:
b8a031146b9319b4220dbeca550e0d4a96caf376
git-author:
Matias De la Puente <mfpuente.ar@…> (11/07/10 21:48:09)
git-committer:
Matias De la Puente <mfpuente.ar@…> (11/07/10 23:20:07)
Message:

Documents: Warn if the document was externally modified

Location:
libi4uc
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libi4uc/i4ucdocument.vala

    r3cb25d2 r55a5393  
    2323{ 
    2424        private File _file; 
     25        private string _etag; 
    2526         
    2627        public string uri { owned get { return _file.get_uri (); } } 
    2728         
    2829        public string path { owned get { return _file.get_path (); } } 
    29          
    30         public string etag { private set; get; } 
    3130         
    3231        public string display_name 
     
    8281                if (_file == null || !_file.equal (file)) 
    8382                        _file = file; 
    84                 this.etag = new_etag; 
     83                _etag = new_etag; 
    8584                string utf8_contents; 
    8685                to_utf8 (contents, out utf8_contents); 
     
    101100                if (_file == null || (uri != null && _file.get_uri () != uri)) 
    102101                        _file = file; 
    103                 this.etag = new_etag; 
     102                _etag = new_etag; 
     103        } 
     104         
     105        public bool get_modified () 
     106        { 
     107                if (_file == null) 
     108                        return false; 
     109                 
     110                string current_etag = null; 
     111                try 
     112                { 
     113                        var file_info = _file.query_info (FILE_ATTRIBUTE_ETAG_VALUE, FileQueryInfoFlags.NONE, null); 
     114                        current_etag = file_info.get_etag (); 
     115                } 
     116                catch (GLib.Error e) 
     117                { 
     118                        warning (e.message); 
     119                        return false; 
     120                } 
     121                return (current_etag != null && current_etag != _etag); 
    104122        } 
    105123         
  • libi4uc/i4ucdocumentpagepresenter.vala

    r3cb25d2 r55a5393  
    2525        private Document _document = new Document (); 
    2626        private SearchBarPresenter _search_bar_presenter; 
     27        private bool _externally_modified_adviced; 
    2728         
    2829        public DocumentPageViewIface view { get { return _view; } } 
     
    6061                 
    6162                _view.error_bar_visible = false; 
     63                _view.reload_bar_visible = false; 
     64                _view.save_bar_visible = false; 
    6265                _view.search_bar_view_visible = false; 
    6366                 
    6467                //connect view signals 
    6568                _view.error_bar_ok_clicked.connect (on_error_bar_ok_clicked); 
     69                _view.reload_bar_reload_clicked.connect (on_reload_bar_reload_clicked); 
     70                _view.reload_bar_cancel_clicked.connect (on_reload_bar_cancel_clicked); 
     71                _view.save_bar_save_clicked.connect (on_save_bar_save_clicked); 
     72                _view.save_bar_cancel_clicked.connect (() => _view.save_bar_visible = false); 
    6673                _view.close_clicked.connect (on_close_clicked); 
    6774                _view.search_bar_view.close_clicked.connect (() => _view.search_bar_view_visible = false); 
     
    6976                _view.content_changed.connect (on_content_changed); 
    7077                _view.selection_changed.connect (() => this.refresh_actions ()); 
     78                _view.focused_in.connect (on_focused_in); 
    7179        } 
    7280         
     
    8492                        _view.tab_mark = false; 
    8593                        _view.guess_language_id (_document.content_type); 
     94                        _externally_modified_adviced = false; 
    8695                } 
    8796                catch (GLib.Error e) 
     
    94103        } 
    95104         
    96         public void save () 
     105        public void save (bool forced=false) 
    97106        { 
    98107                if (_is_new) 
     
    102111                        try 
    103112                        { 
    104                                 _document.save_contents (_view.content); 
     113                                _document.save_contents (_view.content, null, forced); 
    105114                                _view.tab_mark = false; 
     115                                _externally_modified_adviced = false; 
    106116                        } 
    107117                        catch (GLib.Error e) 
    108118                        { 
    109                                 _view.error_bar_primary_text = _("Error trying to save ") + _document.path; 
    110                                 _view.error_bar_secondary_text = e.message; 
    111                                 _view.error_bar_visible = true; 
     119                                if (e is IOError.WRONG_ETAG) 
     120                                { 
     121                                        _view.save_bar_primary_text = _("The document was externally changed"); 
     122                                        _view.save_bar_secondary_text = _("Do you want to save the document?"); 
     123                                        _view.save_bar_visible = true; 
     124                                } 
     125                                else 
     126                                { 
     127                                        _view.error_bar_primary_text = _("Error trying to save ") + _document.path; 
     128                                        _view.error_bar_secondary_text = e.message; 
     129                                        _view.error_bar_visible = true; 
     130                                } 
    112131                        } 
    113132                } 
     
    198217        } 
    199218         
     219        private void on_reload_bar_reload_clicked () 
     220        { 
     221                _view.reload_bar_visible = false; 
     222                open (_document.uri); 
     223        } 
     224         
     225        private void on_reload_bar_cancel_clicked () 
     226        { 
     227                _view.reload_bar_visible = false; 
     228                _externally_modified_adviced = true; 
     229        } 
     230         
     231        private void on_save_bar_save_clicked () 
     232        { 
     233                save (true); 
     234                _view.save_bar_visible = false; 
     235        } 
     236         
    200237        private void on_selected () 
    201238        { 
     
    209246                _view.tab_mark = true; 
    210247                this.refresh_actions (); 
     248        } 
     249         
     250        private void on_focused_in () 
     251        { 
     252                if (_view.reload_bar_visible || _externally_modified_adviced) 
     253                        return; 
     254                 
     255                if (!_document.get_modified ()) 
     256                        return; 
     257                 
     258                _view.reload_bar_primary_text = _("The file %s was externally modified").printf (_document.path); 
     259                _view.reload_bar_secondary_text = _("Do you want to reload the file?"); 
     260                _view.reload_bar_visible = true; 
    211261        } 
    212262         
  • libi4uc/i4ucdocumentpageview.vala

    r3cb25d2 r55a5393  
    2323public class I4uc.DocumentPageView : Page, DocumentPageViewIface 
    2424{ 
     25        //Error bar 
    2526        private InfoBar _error_bar = new InfoBar (); 
    2627        private Label _error_bar_primary_label = new Label (""); 
    2728        private Label _error_bar_secondary_label = new Label (""); 
     29         
     30        //Reload bar 
     31        private InfoBar _reload_bar = new InfoBar (); 
     32        private Label _reload_bar_primary_label = new Label (""); 
     33        private Label _reload_bar_secondary_label = new Label (""); 
     34         
     35        //Save bar 
     36        private InfoBar _save_bar = new InfoBar (); 
     37        private Label _save_bar_primary_label = new Label (""); 
     38        private Label _save_bar_secondary_label = new Label (""); 
     39         
     40        //Search and Replace bar 
    2841        private SearchBarView _search_bar_view; 
     42         
     43        //Content 
    2944        private SourceView _source_view; 
    3045        private SourceBuffer _source_buffer = new SourceBuffer (null); 
     
    125140        } 
    126141         
     142        //Implementation of error bar properties 
    127143        public bool error_bar_visible 
    128144        { 
     
    143159        } 
    144160         
     161        //Implementation of reload bar properties 
     162        public bool reload_bar_visible 
     163        { 
     164                set { _reload_bar.visible = value; } 
     165                get { return _reload_bar.visible; } 
     166        } 
     167         
     168        public string reload_bar_primary_text 
     169        { 
     170                set { _reload_bar_primary_label.label = "<b>" + value + "</b>"; } 
     171                get { return _reload_bar_primary_label.label; } 
     172        } 
     173         
     174        public string reload_bar_secondary_text 
     175        { 
     176                set { _reload_bar_secondary_label.label = "<small>" + value + "</small>"; } 
     177                get { return _reload_bar_secondary_label.label; } 
     178        } 
     179         
     180        //Implementation of save bar properties 
     181        public bool save_bar_visible 
     182        { 
     183                set { _save_bar.visible = value; } 
     184                get { return _save_bar.visible; } 
     185        } 
     186         
     187        public string save_bar_primary_text 
     188        { 
     189                set { _save_bar_primary_label.label = "<b>" + value + "</b>"; } 
     190                get { return _save_bar_primary_label.label; } 
     191        } 
     192         
     193        public string save_bar_secondary_text 
     194        { 
     195                set { _save_bar_secondary_label.label = "<small>" + value + "</small>"; } 
     196                get { return _save_bar_secondary_label.label; } 
     197        } 
     198         
    145199        public SearchBarViewIface search_bar_view { get { return _search_bar_view; } } 
    146200         
     
    160214        { 
    161215                create_error_bar (); 
     216                create_reload_bar (); 
     217                create_save_bar (); 
    162218                 
    163219                _source_view = new SourceView.with_buffer (_source_buffer); 
     
    169225                 
    170226                pack_start (_error_bar, false, false, 2); 
     227                pack_start (_reload_bar, false, false, 2); 
     228                pack_start (_save_bar, false, false, 2); 
    171229                pack_start (_search_bar_view, false, false, 2); 
    172230                pack_start (scrolled_window, true, true, 0); 
    173231                 
    174232                _error_bar.response.connect (() => this.error_bar_ok_clicked ()); 
     233                _reload_bar.response.connect (on_reload_bar_response); 
     234                _save_bar.response.connect (on_save_bar_response); 
    175235                _source_buffer.changed.connect (() => this.content_changed ()); 
    176236                _source_buffer.notify["has-selection"].connect (() => this.selection_changed ()); 
     237                _source_view.focus_in_event.connect (() => { 
     238                        this.focused_in (); 
     239                        return false; 
     240                }); 
    177241        } 
    178242         
     
    280344        private void create_error_bar () 
    281345        { 
    282                 var image = new Image.from_stock (STOCK_DIALOG_ERROR, IconSize.DIALOG); 
    283                  
    284                 _error_bar_primary_label.set_use_markup (true); 
    285                 _error_bar_primary_label.set_line_wrap (true); 
    286                 _error_bar_primary_label.xalign = 0;             
    287                 _error_bar_secondary_label.set_use_markup (true); 
    288                 _error_bar_secondary_label.set_line_wrap (true); 
    289                 _error_bar_secondary_label.xalign = 0;           
     346                pack_bar (_error_bar, STOCK_DIALOG_ERROR, _error_bar_primary_label, _error_bar_secondary_label); 
     347                 
     348                _error_bar.can_focus = false; 
     349                _error_bar.set_message_type (MessageType.ERROR); 
     350                _error_bar.add_button (STOCK_OK, ResponseType.OK); 
     351        } 
     352         
     353        private void create_reload_bar () 
     354        { 
     355                pack_bar (_reload_bar, STOCK_DIALOG_WARNING, _reload_bar_primary_label, _reload_bar_secondary_label); 
     356                 
     357                _reload_bar.can_focus = false; 
     358                _reload_bar.set_message_type (MessageType.WARNING); 
     359                _reload_bar.add_button (STOCK_REFRESH, ResponseType.OK); 
     360                _reload_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL); 
     361        } 
     362         
     363        private void on_reload_bar_response (int response) 
     364        { 
     365                if (response == ResponseType.OK) 
     366                        this.reload_bar_reload_clicked (); 
     367                 
     368                if (response == ResponseType.CANCEL) 
     369                        this.reload_bar_cancel_clicked (); 
     370        } 
     371 
     372        private void create_save_bar () 
     373        { 
     374                pack_bar (_save_bar, STOCK_DIALOG_WARNING, _save_bar_primary_label, _save_bar_secondary_label); 
     375                 
     376                _save_bar.can_focus = false; 
     377                _save_bar.set_message_type (MessageType.WARNING); 
     378                _save_bar.add_button (STOCK_SAVE, ResponseType.OK); 
     379                _save_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL); 
     380        } 
     381         
     382        private void on_save_bar_response (int response) 
     383        { 
     384                if (response == ResponseType.OK) 
     385                        this.save_bar_save_clicked (); 
     386                 
     387                if (response == ResponseType.CANCEL) 
     388                        this.save_bar_cancel_clicked (); 
     389        } 
     390         
     391        private void pack_bar (InfoBar bar, string icon, Label primary_label, Label secondary_label) 
     392        { 
     393                var image = new Image.from_stock (icon, IconSize.DIALOG); 
     394                 
     395                primary_label.set_use_markup (true); 
     396                primary_label.set_line_wrap (true); 
     397                primary_label.xalign = 0;                
     398                secondary_label.set_use_markup (true); 
     399                secondary_label.set_line_wrap (true); 
     400                secondary_label.xalign = 0;              
    290401                 
    291402                var vbox = new VBox (false, 0); 
    292                 vbox.pack_start (_error_bar_primary_label, false, false, 0); 
    293                 vbox.pack_start (_error_bar_secondary_label, false, false, 0); 
    294                  
    295                 var hbox = _error_bar.get_content_area () as HBox; 
     403                vbox.pack_start (primary_label, true, true, 0); 
     404                vbox.pack_start (secondary_label, true, true, 0); 
     405                 
     406                var hbox = bar.get_content_area () as HBox; 
    296407                hbox.pack_start (image, false, false, 0); 
    297408                hbox.pack_start (vbox, true, true, 0); 
    298                  
    299                 _error_bar.set_message_type (MessageType.ERROR); 
    300                 _error_bar.add_button (STOCK_OK, ResponseType.OK); 
    301409        } 
    302410} 
  • libi4uc/i4ucdocumentpageviewiface.vala

    r3cb25d2 r55a5393  
    2222public interface I4uc.DocumentPageViewIface : GLib.Object, PageIface 
    2323{ 
     24        //Settings properties 
    2425        public abstract string font_name { set; get; } 
    2526        public abstract int tab_width { set; get; } 
     
    3637        public abstract string language_id { set; get; } 
    3738         
     39        //Error bar properties 
    3840        public abstract bool error_bar_visible { set; get; } 
    3941        public abstract string error_bar_primary_text { set; get; } 
    4042        public abstract string error_bar_secondary_text { set; get; } 
     43         
     44        //Reload bar properties 
     45        public abstract bool reload_bar_visible { set; get; } 
     46        public abstract string reload_bar_primary_text { set; get; } 
     47        public abstract string reload_bar_secondary_text { set; get; } 
     48         
     49        //Save bar properties 
     50        public abstract bool save_bar_visible { set; get; } 
     51        public abstract string save_bar_primary_text { set; get; } 
     52        public abstract string save_bar_secondary_text { set; get; } 
     53         
     54        //Search and Replace properties 
    4155        public abstract SearchBarViewIface search_bar_view { get; } 
    4256        public abstract bool search_bar_view_visible { set; get; } 
     57         
     58        //Content properties 
    4359        public abstract bool content_visible { set; get; } 
    4460         
     61        //Signals 
    4562        public signal void selected (); 
    4663        public signal void content_changed (); 
    4764        public signal void selection_changed (); 
     65        public signal void focused_in (); 
    4866        public signal void error_bar_ok_clicked (); 
     67        public signal void reload_bar_reload_clicked (); 
     68        public signal void reload_bar_cancel_clicked (); 
     69        public signal void save_bar_save_clicked (); 
     70        public signal void save_bar_cancel_clicked (); 
    4971         
     72        //Methods 
    5073        public abstract void undo (); 
    5174        public abstract void redo (); 
Note: See TracChangeset for help on using the changeset viewer.