| 1 | /* i4ucdocumentpagepresenter.vala |
|---|
| 2 | * |
|---|
| 3 | * Copyright (C) 2010 Matias De la Puente |
|---|
| 4 | * |
|---|
| 5 | * This program is free software: you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | * |
|---|
| 18 | * Author: |
|---|
| 19 | * Matias De la Puente <mfpuente.ar@gmail.com> |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | public class I4uc.DocumentPagePresenter : GLib.Object |
|---|
| 23 | { |
|---|
| 24 | private DocumentPageViewIface _view; |
|---|
| 25 | private Document _document = new Document (); |
|---|
| 26 | private SearchBarPresenter _search_bar_presenter; |
|---|
| 27 | private bool _externally_modified_adviced; |
|---|
| 28 | private TabImage _prev_tab_image; |
|---|
| 29 | |
|---|
| 30 | public DocumentPageViewIface view { get { return _view; } } |
|---|
| 31 | public bool is_new { set; get; } |
|---|
| 32 | |
|---|
| 33 | public bool can_save |
|---|
| 34 | { |
|---|
| 35 | get |
|---|
| 36 | { |
|---|
| 37 | if (_is_new) |
|---|
| 38 | return true; |
|---|
| 39 | return _document.can_write; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public signal void selected (); |
|---|
| 44 | public signal void refresh_actions (); |
|---|
| 45 | public signal void remove_document (); |
|---|
| 46 | |
|---|
| 47 | public DocumentPagePresenter (DocumentPageViewIface view) |
|---|
| 48 | { |
|---|
| 49 | _view = view; |
|---|
| 50 | _search_bar_presenter = new SearchBarPresenter (_view.search_bar_view); |
|---|
| 51 | |
|---|
| 52 | //configure view |
|---|
| 53 | var settings = I4uc.Settings.instance; |
|---|
| 54 | _view.font_name = settings.documents_font; |
|---|
| 55 | _view.tab_width = settings.documents_tab_width; |
|---|
| 56 | _view.auto_indent = settings.documents_auto_indent; |
|---|
| 57 | _view.highlight_current_line = settings.documents_highlight_current_line; |
|---|
| 58 | _view.insert_spaces_instead_of_tabs = settings.documents_insert_spaces_instead_of_tabs; |
|---|
| 59 | _view.show_line_numbers = settings.documents_show_line_numbers; |
|---|
| 60 | _view.highlight_matching_brackets = settings.documents_highlight_matching_brackets; |
|---|
| 61 | _view.wrap_mode = settings.documents_wrap_mode; |
|---|
| 62 | |
|---|
| 63 | _view.error_bar_visible = false; |
|---|
| 64 | _view.reload_bar_visible = false; |
|---|
| 65 | _view.save_bar_visible = false; |
|---|
| 66 | _view.search_bar_view_visible = false; |
|---|
| 67 | _view.content_modified = false; |
|---|
| 68 | |
|---|
| 69 | //connect view signals |
|---|
| 70 | _view.error_bar_ok_clicked.connect (on_error_bar_ok_clicked); |
|---|
| 71 | _view.reload_bar_reload_clicked.connect (on_reload_bar_reload_clicked); |
|---|
| 72 | _view.reload_bar_cancel_clicked.connect (on_reload_bar_cancel_clicked); |
|---|
| 73 | _view.save_bar_save_clicked.connect (on_save_bar_save_clicked); |
|---|
| 74 | _view.save_bar_cancel_clicked.connect (on_save_bar_cancel_clicked); |
|---|
| 75 | _view.close_clicked.connect (on_close_clicked); |
|---|
| 76 | _view.search_bar_view.close_clicked.connect (() => _view.search_bar_view_visible = false); |
|---|
| 77 | _view.selected.connect (on_selected); |
|---|
| 78 | _view.content_changed.connect (on_content_changed); |
|---|
| 79 | _view.selection_changed.connect (() => this.refresh_actions ()); |
|---|
| 80 | _view.focused_in.connect (on_focused_in); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public void open (string uri) |
|---|
| 84 | { |
|---|
| 85 | var file = File.new_for_uri (uri); |
|---|
| 86 | try |
|---|
| 87 | { |
|---|
| 88 | _view.title = uri; |
|---|
| 89 | var file_info = file.query_info (FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, FileQueryInfoFlags.NONE, null); |
|---|
| 90 | _view.tab_title = file_info.get_display_name (); |
|---|
| 91 | var contents = _document.load_contents (uri); |
|---|
| 92 | _view.tab_tooltip = "<b>%s</b> %s".printf (_("Name:"), _document.parse_name); |
|---|
| 93 | _view.content = contents; |
|---|
| 94 | _view.content_modified = false; |
|---|
| 95 | _view.tab_mark = false; |
|---|
| 96 | _view.add_recent (uri); |
|---|
| 97 | _view.guess_language_id (_document.content_type); |
|---|
| 98 | _externally_modified_adviced = false; |
|---|
| 99 | } |
|---|
| 100 | catch (GLib.Error e) |
|---|
| 101 | { |
|---|
| 102 | _view.content_visible = false; |
|---|
| 103 | _view.error_bar_primary_text = _("Error trying to open ") + file.get_path (); |
|---|
| 104 | _view.error_bar_secondary_text = e.message; |
|---|
| 105 | _view.tab_image = TabImage.ERROR; |
|---|
| 106 | _view.error_bar_visible = true; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public void save (bool forced=false) |
|---|
| 111 | { |
|---|
| 112 | if (_is_new) |
|---|
| 113 | save_as (); |
|---|
| 114 | else if (_view.content_modified) |
|---|
| 115 | { |
|---|
| 116 | try |
|---|
| 117 | { |
|---|
| 118 | _document.save_contents (_view.content, null, forced); |
|---|
| 119 | _view.content_modified = false; |
|---|
| 120 | _view.tab_mark = false; |
|---|
| 121 | _view.add_recent (_document.uri); |
|---|
| 122 | _externally_modified_adviced = false; |
|---|
| 123 | } |
|---|
| 124 | catch (GLib.Error e) |
|---|
| 125 | { |
|---|
| 126 | if (e is IOError.WRONG_ETAG) |
|---|
| 127 | { |
|---|
| 128 | _view.save_bar_primary_text = _("The document was externally changed"); |
|---|
| 129 | _view.save_bar_secondary_text = _("Do you want to save the document?"); |
|---|
| 130 | _prev_tab_image = _view.tab_image; |
|---|
| 131 | _view.tab_image = TabImage.WARNING; |
|---|
| 132 | _view.save_bar_visible = true; |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | _view.error_bar_primary_text = _("Error trying to save ") + _document.path; |
|---|
| 137 | _view.error_bar_secondary_text = e.message; |
|---|
| 138 | _prev_tab_image = _view.tab_image; |
|---|
| 139 | _view.tab_image = TabImage.ERROR; |
|---|
| 140 | _view.error_bar_visible = true; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | public void save_as () |
|---|
| 147 | { |
|---|
| 148 | string document_uri; |
|---|
| 149 | var folder_uri = I4uc.Settings.instance.working_folder; |
|---|
| 150 | var response = _view.show_save_dialog (_view.tab_title, ref folder_uri, out document_uri); |
|---|
| 151 | if (response == Gtk.ResponseType.OK) |
|---|
| 152 | { |
|---|
| 153 | try |
|---|
| 154 | { |
|---|
| 155 | _document.save_contents (_view.content, document_uri); |
|---|
| 156 | _view.title = document_uri; |
|---|
| 157 | _is_new = false; |
|---|
| 158 | _view.tab_title = _document.display_name; |
|---|
| 159 | _view.tab_tooltip = "<b>%s</b> %s".printf (_("Name:"), _document.parse_name); |
|---|
| 160 | _view.tab_image = TabImage.FILE; |
|---|
| 161 | _view.content_modified = false; |
|---|
| 162 | _view.tab_mark = false; |
|---|
| 163 | _view.add_recent (_document.uri); |
|---|
| 164 | I4uc.Settings.instance.working_folder = folder_uri; |
|---|
| 165 | _externally_modified_adviced = false; |
|---|
| 166 | } |
|---|
| 167 | catch (GLib.Error e) |
|---|
| 168 | { |
|---|
| 169 | _view.error_bar_primary_text = _("Error trying to save as ") + File.new_for_uri (document_uri).get_path (); |
|---|
| 170 | _view.error_bar_secondary_text = e.message; |
|---|
| 171 | _prev_tab_image = _view.tab_image; |
|---|
| 172 | _view.tab_image = TabImage.ERROR; |
|---|
| 173 | _view.error_bar_visible = true; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | public void print () |
|---|
| 179 | { |
|---|
| 180 | try |
|---|
| 181 | { |
|---|
| 182 | _view.show_print_dialog (); |
|---|
| 183 | } |
|---|
| 184 | catch (GLib.Error e) |
|---|
| 185 | { |
|---|
| 186 | _view.error_bar_primary_text = _("Error trying to print ") + _document.uri; |
|---|
| 187 | _view.error_bar_secondary_text = e.message; |
|---|
| 188 | _prev_tab_image = _view.tab_image; |
|---|
| 189 | _view.tab_image = TabImage.ERROR; |
|---|
| 190 | _view.error_bar_visible = true; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | public void undo () |
|---|
| 195 | { |
|---|
| 196 | _view.undo (); |
|---|
| 197 | this.refresh_actions (); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | public void redo () |
|---|
| 201 | { |
|---|
| 202 | _view.redo (); |
|---|
| 203 | this.refresh_actions (); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | public void cut () |
|---|
| 207 | { |
|---|
| 208 | _view.cut (); |
|---|
| 209 | this.refresh_actions (); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | public void copy () |
|---|
| 213 | { |
|---|
| 214 | _view.copy (); |
|---|
| 215 | this.refresh_actions (); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | public void paste () |
|---|
| 219 | { |
|---|
| 220 | _view.paste (); |
|---|
| 221 | this.refresh_actions (); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | public void show_search_bar () |
|---|
| 225 | { |
|---|
| 226 | _view.search_bar_view_visible = true; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | private void on_error_bar_ok_clicked () |
|---|
| 230 | { |
|---|
| 231 | //Error produced trying to open a document |
|---|
| 232 | if (!_view.content_visible) |
|---|
| 233 | this.remove_document (); |
|---|
| 234 | _view.tab_image = _prev_tab_image; |
|---|
| 235 | _view.error_bar_visible = false; |
|---|
| 236 | _view.grab_focus (); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | private void on_reload_bar_reload_clicked () |
|---|
| 240 | { |
|---|
| 241 | _view.tab_image = _prev_tab_image; |
|---|
| 242 | _view.reload_bar_visible = false; |
|---|
| 243 | open (_document.uri); |
|---|
| 244 | _view.grab_focus (); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | private void on_reload_bar_cancel_clicked () |
|---|
| 248 | { |
|---|
| 249 | _view.tab_image = _prev_tab_image; |
|---|
| 250 | _view.reload_bar_visible = false; |
|---|
| 251 | _externally_modified_adviced = true; |
|---|
| 252 | _view.grab_focus (); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | private void on_save_bar_save_clicked () |
|---|
| 256 | { |
|---|
| 257 | _view.tab_image = _prev_tab_image; |
|---|
| 258 | _view.save_bar_visible = false; |
|---|
| 259 | save (true); |
|---|
| 260 | _view.grab_focus (); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | private void on_save_bar_cancel_clicked () |
|---|
| 264 | { |
|---|
| 265 | _view.tab_image = _prev_tab_image; |
|---|
| 266 | _view.save_bar_visible = false; |
|---|
| 267 | _view.grab_focus (); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | private void on_selected () |
|---|
| 271 | { |
|---|
| 272 | if (!_is_new) |
|---|
| 273 | I4uc.Settings.instance.working_folder = _document.folder; |
|---|
| 274 | this.selected (); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | private void on_content_changed () |
|---|
| 278 | { |
|---|
| 279 | _view.tab_mark = _view.content_modified; |
|---|
| 280 | this.refresh_actions (); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | private void on_focused_in () |
|---|
| 284 | { |
|---|
| 285 | if (_view.reload_bar_visible || _externally_modified_adviced) |
|---|
| 286 | return; |
|---|
| 287 | |
|---|
| 288 | if (!_document.get_modified ()) |
|---|
| 289 | return; |
|---|
| 290 | |
|---|
| 291 | _view.reload_bar_primary_text = _("The file %s was externally modified").printf (_document.path); |
|---|
| 292 | _view.reload_bar_secondary_text = _("Do you want to reload the file?"); |
|---|
| 293 | _prev_tab_image = _view.tab_image; |
|---|
| 294 | _view.tab_image = TabImage.WARNING; |
|---|
| 295 | _view.reload_bar_visible = true; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | private void on_close_clicked () |
|---|
| 299 | { |
|---|
| 300 | if (_view.content_modified) |
|---|
| 301 | { |
|---|
| 302 | var response = _view.show_close_message (); |
|---|
| 303 | |
|---|
| 304 | if (response == Gtk.ResponseType.CANCEL) |
|---|
| 305 | return; |
|---|
| 306 | |
|---|
| 307 | if (response == Gtk.ResponseType.YES) |
|---|
| 308 | save (); |
|---|
| 309 | } |
|---|
| 310 | this.remove_document (); |
|---|
| 311 | } |
|---|
| 312 | } |
|---|