| 1 | /* i4ucsourcefilesmanager.vala |
|---|
| 2 | * |
|---|
| 3 | * Copyright (C) 2009 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 | using Gtk; |
|---|
| 22 | using Gee; |
|---|
| 23 | |
|---|
| 24 | public class I4uc.SourceFilesManager: GLib.Object |
|---|
| 25 | { |
|---|
| 26 | private const ActionEntry[] _action_entries = |
|---|
| 27 | { |
|---|
| 28 | { "NewAction", STOCK_NEW, null, null, null, on_new }, |
|---|
| 29 | { "OpenAction", STOCK_OPEN, null, null, null, on_open }, |
|---|
| 30 | { "SourceFilesPreferencesAction", null, N_("Source files preferences"), null, null, on_source_files_preferences } |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | private const ActionEntry[] _sensitive_action_entries = |
|---|
| 34 | { |
|---|
| 35 | { "SaveAction", STOCK_SAVE, null, null, null, on_save }, |
|---|
| 36 | { "SaveAsAction", STOCK_SAVE_AS, null, null, null, on_save_as }, |
|---|
| 37 | { "PrintAction", STOCK_PRINT, null, null, null, on_print }, |
|---|
| 38 | { "FindAction", STOCK_FIND, null, null, null, on_find }, |
|---|
| 39 | { "FindAndReplaceAction", STOCK_FIND_AND_REPLACE, null, null, null, on_find_and_replace } |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | private ArrayList <SourcePage> _source_pages = new ArrayList<SourcePage> (); |
|---|
| 43 | private PagesManager _pages_manager; |
|---|
| 44 | private UIManager _ui_manager; |
|---|
| 45 | private ActionGroup _sensitive_action_group; |
|---|
| 46 | |
|---|
| 47 | public SourceFilesManager (UIManager ui_manager, PagesManager pages_manager) |
|---|
| 48 | { |
|---|
| 49 | _ui_manager = ui_manager; |
|---|
| 50 | _pages_manager = pages_manager; |
|---|
| 51 | |
|---|
| 52 | var action_group = new ActionGroup ("I4ucSourceFilesManagerActions"); |
|---|
| 53 | action_group.set_translation_domain (Config.GETTEXT_PACKAGE); |
|---|
| 54 | action_group.add_actions (_action_entries, this); |
|---|
| 55 | |
|---|
| 56 | _sensitive_action_group = new ActionGroup ("I4ucSourceFilesManagerSensitiveActions"); |
|---|
| 57 | _sensitive_action_group.set_translation_domain (Config.GETTEXT_PACKAGE); |
|---|
| 58 | _sensitive_action_group.add_actions (_sensitive_action_entries, this); |
|---|
| 59 | _sensitive_action_group.sensitive = false; |
|---|
| 60 | |
|---|
| 61 | _ui_manager.insert_action_group (action_group, -1); |
|---|
| 62 | _ui_manager.insert_action_group (_sensitive_action_group, -1); |
|---|
| 63 | _ui_manager.add_ui_from_file (Path.build_filename (Config.PACKAGE_DATADIR, "ui", "i4ucsourcefilesmanagerui.xml")); |
|---|
| 64 | |
|---|
| 65 | var toolbar = (Toolbar)_ui_manager.get_widget ("/MainToolbar"); |
|---|
| 66 | |
|---|
| 67 | Gtk.Callback non_homogeneous = (item) => { ((ToolItem)item).set_homogeneous (false); }; |
|---|
| 68 | toolbar.foreach (non_homogeneous); |
|---|
| 69 | |
|---|
| 70 | _pages_manager.page_added.connect ((page) => { |
|---|
| 71 | if (page is SourcePage) |
|---|
| 72 | _source_pages.add (page as SourcePage); |
|---|
| 73 | }); |
|---|
| 74 | _pages_manager.page_removed.connect ((page) => { |
|---|
| 75 | if (page is SourcePage) |
|---|
| 76 | _source_pages.remove (page as SourcePage); |
|---|
| 77 | }); |
|---|
| 78 | _pages_manager.page_changed.connect ((page) => { |
|---|
| 79 | if (page == null || !(page is SourcePage)) |
|---|
| 80 | _sensitive_action_group.sensitive = false; |
|---|
| 81 | else |
|---|
| 82 | _sensitive_action_group.sensitive = true; |
|---|
| 83 | }); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private void on_new () |
|---|
| 87 | { |
|---|
| 88 | string title = _("Unsaved file %i"); |
|---|
| 89 | int i = 0; |
|---|
| 90 | while (get_source_page_from_title (title.printf (++i)) != null) |
|---|
| 91 | ; |
|---|
| 92 | var source_page = new SourcePage (); |
|---|
| 93 | source_page.is_new = true; |
|---|
| 94 | source_page.title = title.printf (i); |
|---|
| 95 | source_page.tab_title = Path.get_basename (title.printf (i)); |
|---|
| 96 | source_page.tab_tooltip = _("<b>Name:</b> %s").printf (title.printf (i)); |
|---|
| 97 | source_page.close_action.connect ((page) => { |
|---|
| 98 | close_page (page as SourcePage); |
|---|
| 99 | }); |
|---|
| 100 | _pages_manager.add_page (source_page); |
|---|
| 101 | _pages_manager.show_page (source_page); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | private void on_open () |
|---|
| 105 | { |
|---|
| 106 | var dialog = new FileChooserDialog (_("Open"), null, FileChooserAction.OPEN); |
|---|
| 107 | dialog.set_current_folder (Configuration.get_instance ().working_directory); |
|---|
| 108 | dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 109 | dialog.add_button (STOCK_OPEN, ResponseType.OK); |
|---|
| 110 | dialog.set_default_response (ResponseType.OK); |
|---|
| 111 | var filter = new FileFilter (); |
|---|
| 112 | filter.set_name ("All files"); |
|---|
| 113 | filter.add_pattern ("*"); |
|---|
| 114 | dialog.add_filter (filter); |
|---|
| 115 | |
|---|
| 116 | if (dialog.run () == ResponseType.OK) |
|---|
| 117 | { |
|---|
| 118 | Configuration.get_instance ().working_directory = Path.get_dirname (dialog.get_filename ()); |
|---|
| 119 | open_file (dialog.get_filename ()); |
|---|
| 120 | } |
|---|
| 121 | dialog.destroy (); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | private void on_save () |
|---|
| 125 | { |
|---|
| 126 | var page = _pages_manager.current_page as SourcePage; |
|---|
| 127 | if (page.is_new) |
|---|
| 128 | on_save_as (); |
|---|
| 129 | else if (page.tab_mark) |
|---|
| 130 | { |
|---|
| 131 | FileUtils.set_contents (page.title, page.text); |
|---|
| 132 | page.tab_mark = false; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | private void on_save_as () |
|---|
| 137 | { |
|---|
| 138 | var page = _pages_manager.current_page as SourcePage; |
|---|
| 139 | var dialog = new FileChooserDialog (_("Save as"), null, FileChooserAction.SAVE); |
|---|
| 140 | dialog.set_current_folder (Configuration.get_instance ().working_directory); |
|---|
| 141 | dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 142 | dialog.add_button (STOCK_SAVE, ResponseType.OK); |
|---|
| 143 | dialog.set_default_response (ResponseType.OK); |
|---|
| 144 | var response = dialog.run (); |
|---|
| 145 | var filename = dialog.get_filename (); |
|---|
| 146 | dialog.destroy (); |
|---|
| 147 | |
|---|
| 148 | if (response == ResponseType.OK) |
|---|
| 149 | { |
|---|
| 150 | if (FileUtils.test (filename, FileTest.EXISTS) && filename != page.title) |
|---|
| 151 | { |
|---|
| 152 | var message = new MessageDialog (null, DialogFlags.MODAL, MessageType.WARNING, ButtonsType.NONE, _("Do you want to overwrite the file <<%s>>?"), filename); |
|---|
| 153 | message.add_button (STOCK_NO, ResponseType.NO); |
|---|
| 154 | message.add_button (STOCK_YES, ResponseType.YES); |
|---|
| 155 | message.set_default_response (ResponseType.YES); |
|---|
| 156 | var message_response = message.run (); |
|---|
| 157 | message.destroy (); |
|---|
| 158 | |
|---|
| 159 | if (message_response == ResponseType.NO) |
|---|
| 160 | return; |
|---|
| 161 | } |
|---|
| 162 | Configuration.get_instance ().working_directory = Path.get_dirname (filename); |
|---|
| 163 | page.title = filename; |
|---|
| 164 | page.tab_title = Path.get_basename (filename); |
|---|
| 165 | FileUtils.set_contents (filename, page.text); |
|---|
| 166 | page.tab_mark = false; |
|---|
| 167 | page.is_new = false; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | private void on_print () |
|---|
| 172 | { |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | private void on_find () |
|---|
| 177 | { |
|---|
| 178 | (_pages_manager.current_page as SourcePage).find_action (); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | private void on_find_and_replace () |
|---|
| 182 | { |
|---|
| 183 | (_pages_manager.current_page as SourcePage).find_and_replace_action (); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private void on_source_files_preferences () |
|---|
| 187 | { |
|---|
| 188 | SourceFilesPreferencesPage preferences_page = null; |
|---|
| 189 | foreach (var page in _pages_manager.list_pages ()) |
|---|
| 190 | if (page.title == "Source files preferences") |
|---|
| 191 | preferences_page = page as SourceFilesPreferencesPage; |
|---|
| 192 | if (preferences_page == null) |
|---|
| 193 | { |
|---|
| 194 | preferences_page = new SourceFilesPreferencesPage (); |
|---|
| 195 | preferences_page.title = "Source files preferences"; |
|---|
| 196 | preferences_page.tab_title = _("Source files preferences"); |
|---|
| 197 | preferences_page.close_action.connect ((preferences_page) => { |
|---|
| 198 | _pages_manager.remove_page (preferences_page); |
|---|
| 199 | }); |
|---|
| 200 | _pages_manager.add_page (preferences_page); |
|---|
| 201 | } |
|---|
| 202 | _pages_manager.show_page (preferences_page); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public void open_file (string filename) |
|---|
| 206 | { |
|---|
| 207 | var source_page = get_source_page_from_title (filename); |
|---|
| 208 | |
|---|
| 209 | if (source_page == null) |
|---|
| 210 | { |
|---|
| 211 | return_if_fail (FileUtils.test (filename, FileTest.EXISTS)); |
|---|
| 212 | source_page = new SourcePage (); |
|---|
| 213 | source_page.title = filename; |
|---|
| 214 | source_page.tab_title = Path.get_basename (filename); |
|---|
| 215 | source_page.tab_tooltip = _("<b>Name:</b> %s").printf (filename); |
|---|
| 216 | if (filename.has_suffix (".asm") || filename.has_suffix (".inc")) |
|---|
| 217 | source_page.language = LanguageType.ASM; |
|---|
| 218 | if (filename.has_suffix (".c") || filename.has_suffix (".h")) |
|---|
| 219 | source_page.language = LanguageType.C; |
|---|
| 220 | if (filename.has_suffix (".vala")) |
|---|
| 221 | source_page.language = LanguageType.VALA; |
|---|
| 222 | string source_content; |
|---|
| 223 | FileUtils.get_contents (filename, out source_content); |
|---|
| 224 | source_page.text = source_content; |
|---|
| 225 | source_page.tab_mark = false; |
|---|
| 226 | source_page.close_action.connect ((page) => { |
|---|
| 227 | close_page (page as SourcePage); |
|---|
| 228 | }); |
|---|
| 229 | _pages_manager.add_page (source_page); |
|---|
| 230 | } |
|---|
| 231 | _pages_manager.show_page (source_page); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | public void save_file (string filename) |
|---|
| 235 | { |
|---|
| 236 | var source_page = get_source_page_from_title (filename); |
|---|
| 237 | if (source_page != null && source_page.tab_mark) |
|---|
| 238 | { |
|---|
| 239 | FileUtils.set_contents (filename, source_page.text); |
|---|
| 240 | source_page.tab_mark = false; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | public void close_file (string filename) |
|---|
| 245 | { |
|---|
| 246 | var source_page = get_source_page_from_title (filename); |
|---|
| 247 | if (source_page != null) |
|---|
| 248 | close_page (source_page); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | public void close_all () |
|---|
| 252 | { |
|---|
| 253 | while (_source_pages.size > 0) |
|---|
| 254 | close_page (_source_pages[0]); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | private void close_page (SourcePage source_page) |
|---|
| 258 | { |
|---|
| 259 | if (source_page.tab_mark) |
|---|
| 260 | { |
|---|
| 261 | var message = new MessageDialog (null, DialogFlags.MODAL, MessageType.WARNING, ButtonsType.NONE, _("Do you want to save the file <<%s>>?"), source_page.title); |
|---|
| 262 | message.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 263 | message.add_button (STOCK_NO, ResponseType.NO); |
|---|
| 264 | message.add_button (STOCK_YES, ResponseType.YES); |
|---|
| 265 | message.set_default_response (ResponseType.YES); |
|---|
| 266 | var response = message.run (); |
|---|
| 267 | message.destroy (); |
|---|
| 268 | |
|---|
| 269 | if (response == ResponseType.CANCEL) |
|---|
| 270 | return; |
|---|
| 271 | |
|---|
| 272 | if (response == ResponseType.YES) |
|---|
| 273 | on_save (); |
|---|
| 274 | } |
|---|
| 275 | _pages_manager.remove_page (source_page); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | private SourcePage? get_source_page_from_title (string title) |
|---|
| 279 | { |
|---|
| 280 | foreach (SourcePage source_page in _source_pages) |
|---|
| 281 | if (source_page.title == title) |
|---|
| 282 | return source_page; |
|---|
| 283 | return null; |
|---|
| 284 | } |
|---|
| 285 | } |
|---|