| 1 | /* projectslogic.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 | using Gee; |
|---|
| 22 | |
|---|
| 23 | public class I4uc.Core.ProjectsLogic : DocumentOpener, GLib.Object |
|---|
| 24 | { |
|---|
| 25 | private ProjectsView _view; |
|---|
| 26 | private DocumentsLogic _documents_logic; |
|---|
| 27 | private ProgrammersLogic _programmers_logic; |
|---|
| 28 | private ProjectsPageLogic _page_logic; |
|---|
| 29 | private ProfileBuilder _profile_builder = new ProfileBuilder (); |
|---|
| 30 | |
|---|
| 31 | public ProjectsLogic (ProjectsView view, DocumentsLogic documents_logic, ProgrammersLogic programmers_logic) |
|---|
| 32 | { |
|---|
| 33 | _view = view; |
|---|
| 34 | _documents_logic = documents_logic; |
|---|
| 35 | _programmers_logic = programmers_logic; |
|---|
| 36 | |
|---|
| 37 | _documents_logic.add_document_opener (".i4uc", this); |
|---|
| 38 | |
|---|
| 39 | _page_logic = new ProjectsPageLogic (_view.page_view, _documents_logic); |
|---|
| 40 | |
|---|
| 41 | //Configure view |
|---|
| 42 | _view.add_file_sensitive = false; |
|---|
| 43 | _view.remove_file_from_project_sensitive = false; |
|---|
| 44 | _view.remove_file_from_profile_sensitive = false; |
|---|
| 45 | _view.add_profile_sensitive = false; |
|---|
| 46 | _view.edit_profile_sensitive = false; |
|---|
| 47 | _view.page_view.profile_edit_sensitive = false; |
|---|
| 48 | _view.duplicate_profile_sensitive = false; |
|---|
| 49 | _view.remove_profile_sensitive = false; |
|---|
| 50 | _view.build_profile_sensitive = false; |
|---|
| 51 | _view.program_profile_sensitive = false; |
|---|
| 52 | _view.stop_sensitive = false; |
|---|
| 53 | _view.clean_profile_sensitive = false; |
|---|
| 54 | _view.close_sensitive = false; |
|---|
| 55 | _view.build_log_page.tab_title = _("Build process"); |
|---|
| 56 | _view.build_log_page.tab_image = TabImage.BUILDER; |
|---|
| 57 | _view.build_log_page.message_format = "<b>%s</b>"; |
|---|
| 58 | _view.build_log_page.command_format = "\t%s"; |
|---|
| 59 | |
|---|
| 60 | //Connect view signals |
|---|
| 61 | _view.new_clicked.connect (on_new_clicked); |
|---|
| 62 | _view.add_file_clicked.connect (on_add_file_clicked); |
|---|
| 63 | _view.remove_file_from_project_clicked.connect (on_remove_file_from_project_clicked); |
|---|
| 64 | _view.remove_file_from_profile_clicked.connect (on_remove_file_from_profile_clicked); |
|---|
| 65 | _view.add_profile_clicked.connect (on_add_profile_clicked); |
|---|
| 66 | _view.edit_profile_clicked.connect (() => _page_logic.show_current_profile_dialog ()); |
|---|
| 67 | _view.duplicate_profile_clicked.connect (() => _page_logic.show_current_profile_dialog (true)); |
|---|
| 68 | _view.remove_profile_clicked.connect (on_remove_profile_clicked); |
|---|
| 69 | _view.build_profile_clicked.connect (on_build_profile_clicked); |
|---|
| 70 | _view.program_profile_clicked.connect (on_program_profile_clicked); |
|---|
| 71 | _view.stop_clicked.connect (on_stop_clicked); |
|---|
| 72 | _view.clean_profile_clicked.connect (on_clean_profile_clicked); |
|---|
| 73 | _view.close_clicked.connect (on_close_clicked); |
|---|
| 74 | _view.page_view.project_changed.connect (on_project_changed); |
|---|
| 75 | _view.page_view.profile_changed.connect (on_profile_changed); |
|---|
| 76 | _view.page_view.file_changed.connect (on_file_changed); |
|---|
| 77 | _view.build_log_page.command_activated.connect (on_command_activated); |
|---|
| 78 | _profile_builder.notify["is-building"].connect (on_profile_builder_is_building); |
|---|
| 79 | _profile_builder.new_message.connect (on_profile_builder_new_message); |
|---|
| 80 | _profile_builder.command_finished.connect (on_profile_builder_command_finished); |
|---|
| 81 | _programmers_logic.notify["can-write-flash"].connect (on_programmers_logic_can_write_flash_changed); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public bool open_project (string project_uri) |
|---|
| 85 | { |
|---|
| 86 | var project = new Project (); |
|---|
| 87 | try |
|---|
| 88 | { |
|---|
| 89 | project.open (project_uri); |
|---|
| 90 | _page_logic.add_project (project); |
|---|
| 91 | _view.add_recent (project_uri); |
|---|
| 92 | } |
|---|
| 93 | catch (GLib.Error e) |
|---|
| 94 | { |
|---|
| 95 | _view.show_error_message (_("Error trying to open <<%s>>: %s").printf (project_uri, e.message)); |
|---|
| 96 | return false; |
|---|
| 97 | } |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public bool open_document (string document_uri) |
|---|
| 102 | { |
|---|
| 103 | return open_project (document_uri); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | public void close_all_projects () |
|---|
| 107 | { |
|---|
| 108 | _page_logic.remove_all_projects (); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | private void on_new_clicked () |
|---|
| 112 | { |
|---|
| 113 | string project_uri; |
|---|
| 114 | string project_name; |
|---|
| 115 | Gee.List<string> authors; |
|---|
| 116 | if (_view.show_new_dialog (out project_uri, out project_name, out authors) == Gtk.ResponseType.OK) |
|---|
| 117 | { |
|---|
| 118 | var project = new Project (); |
|---|
| 119 | project.name = project_name; |
|---|
| 120 | project.authors.add_all (authors); |
|---|
| 121 | |
|---|
| 122 | var main_profile = new ProjectProfile (); |
|---|
| 123 | main_profile.name = "main"; |
|---|
| 124 | project.profiles["main"] = main_profile; |
|---|
| 125 | |
|---|
| 126 | project.save (project_uri); |
|---|
| 127 | _view.add_recent (project_uri); |
|---|
| 128 | _page_logic.add_project (project); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | private void on_add_file_clicked () |
|---|
| 133 | { |
|---|
| 134 | var current_project = _page_logic.current_project; |
|---|
| 135 | var dialog_logic = new AddFileDialogLogic (_view.create_add_file_dialog_view (), current_project); |
|---|
| 136 | string file; |
|---|
| 137 | Gee.List<string> profiles; |
|---|
| 138 | if (dialog_logic.run (out file, out profiles)) |
|---|
| 139 | { |
|---|
| 140 | if (!(file in current_project.files)) |
|---|
| 141 | current_project.files.add (file); |
|---|
| 142 | foreach (var profile in profiles) |
|---|
| 143 | if (!(file in current_project.profiles[profile].files)) |
|---|
| 144 | current_project.profiles[profile].files.add (file); |
|---|
| 145 | current_project.save (); |
|---|
| 146 | _page_logic.update_files_list (); |
|---|
| 147 | _documents_logic.open_document (current_project.get_file_uri (file)); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private void on_remove_file_from_project_clicked () |
|---|
| 152 | { |
|---|
| 153 | var current_project = _page_logic.current_project; |
|---|
| 154 | var current_file = _view.page_view.current_file; |
|---|
| 155 | var message = _("Do you want to remove the file <<%s>> from %s project?").printf (current_file, current_project.name); |
|---|
| 156 | if (_view.show_yes_no_message (message, false) != Gtk.ResponseType.YES) |
|---|
| 157 | return; |
|---|
| 158 | foreach (var profile in current_project.profiles.values) |
|---|
| 159 | if (current_file in profile.files) |
|---|
| 160 | profile.files.remove (current_file); |
|---|
| 161 | current_project.files.remove (current_file); |
|---|
| 162 | current_project.save (); |
|---|
| 163 | _page_logic.update_files_list (); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | private void on_remove_file_from_profile_clicked () |
|---|
| 167 | { |
|---|
| 168 | var current_project = _page_logic.current_project; |
|---|
| 169 | var current_profile = _view.page_view.current_profile; |
|---|
| 170 | var current_file = _view.page_view.current_file; |
|---|
| 171 | var message = _("Do you want to remove the file <<%s>> from %s profile?").printf (current_file, current_profile); |
|---|
| 172 | if (_view.show_yes_no_message (message, false) != Gtk.ResponseType.YES) |
|---|
| 173 | return; |
|---|
| 174 | current_project.profiles[current_profile].files.remove (current_file); |
|---|
| 175 | current_project.save (); |
|---|
| 176 | _page_logic.update_files_list (); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | private void on_add_profile_clicked () |
|---|
| 180 | { |
|---|
| 181 | var current_project = _page_logic.current_project; |
|---|
| 182 | var dialog_logic = new AddProfileDialogLogic (_view.create_add_profile_dialog_view (), current_project); |
|---|
| 183 | if (dialog_logic.run ()) |
|---|
| 184 | _page_logic.update_profiles_list (); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | private void on_remove_profile_clicked () |
|---|
| 188 | { |
|---|
| 189 | var current_project = _page_logic.current_project; |
|---|
| 190 | var current_profile = _view.page_view.current_profile; |
|---|
| 191 | var message = _("Do you want to remove the profile <<%s>> from %s project?").printf (current_profile, current_project.name); |
|---|
| 192 | if (_view.show_yes_no_message (message, false) != Gtk.ResponseType.YES) |
|---|
| 193 | return; |
|---|
| 194 | current_project.profiles.unset (current_profile); |
|---|
| 195 | current_project.save (); |
|---|
| 196 | _page_logic.update_profiles_list (); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | private void on_build_profile_clicked () |
|---|
| 200 | { |
|---|
| 201 | var current_project = _page_logic.current_project; |
|---|
| 202 | var current_profile = current_project.profiles[_view.page_view.current_profile]; |
|---|
| 203 | var message = _("The %s of the profile isn't selected"); |
|---|
| 204 | |
|---|
| 205 | if (current_profile.builder_id == "") |
|---|
| 206 | { |
|---|
| 207 | _view.show_error_message (message.printf (_("builder"))); |
|---|
| 208 | return; |
|---|
| 209 | } |
|---|
| 210 | if (current_profile.device_type == "") |
|---|
| 211 | { |
|---|
| 212 | _view.show_error_message (message.printf (_("device type"))); |
|---|
| 213 | return; |
|---|
| 214 | } |
|---|
| 215 | if (current_profile.device == "") |
|---|
| 216 | { |
|---|
| 217 | _view.show_error_message (message.printf (_("device"))); |
|---|
| 218 | return; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | //Save profile's files before building |
|---|
| 222 | var files_to_save = new ArrayList<string> (); |
|---|
| 223 | foreach (var file in current_profile.files) |
|---|
| 224 | files_to_save.add (current_project.get_file_uri (file)); |
|---|
| 225 | _documents_logic.save_documents (files_to_save); |
|---|
| 226 | |
|---|
| 227 | _profile_builder.project = current_project; |
|---|
| 228 | _profile_builder.profile = current_profile.name; |
|---|
| 229 | _view.show_build_log_page (); |
|---|
| 230 | |
|---|
| 231 | try |
|---|
| 232 | { |
|---|
| 233 | _profile_builder.build (); |
|---|
| 234 | } |
|---|
| 235 | catch (GLib.Error e) |
|---|
| 236 | { |
|---|
| 237 | _view.show_error_message (_("Error trying to build profile %s: %s").printf (current_profile.name, e.message)); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | private void on_program_profile_clicked () |
|---|
| 242 | { |
|---|
| 243 | var current_project = _page_logic.current_project; |
|---|
| 244 | var current_profile = current_project.profiles[_view.page_view.current_profile]; |
|---|
| 245 | var hexfile = current_project.name + "-" + current_profile.name + ".hex"; |
|---|
| 246 | var hexfile_uri = current_project.get_file_uri (hexfile.replace (" ", "_")); |
|---|
| 247 | var hexfile_file = File.new_for_uri (hexfile_uri); |
|---|
| 248 | |
|---|
| 249 | if (!hexfile_file.query_exists (null)) |
|---|
| 250 | { |
|---|
| 251 | _view.show_error_message (_("The file %s doesn't exists").printf (hexfile_file.get_path ())); |
|---|
| 252 | return; |
|---|
| 253 | } |
|---|
| 254 | _programmers_logic.write_flash (hexfile_uri); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | private void on_stop_clicked () |
|---|
| 258 | { |
|---|
| 259 | _profile_builder.stop (); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | private void on_clean_profile_clicked () |
|---|
| 263 | { |
|---|
| 264 | var current_project = _page_logic.current_project; |
|---|
| 265 | var current_profile = current_project.profiles[_view.page_view.current_profile]; |
|---|
| 266 | |
|---|
| 267 | if (current_profile.builder_id == "") |
|---|
| 268 | return; |
|---|
| 269 | |
|---|
| 270 | _profile_builder.project = current_project; |
|---|
| 271 | _profile_builder.profile = current_profile.name; |
|---|
| 272 | |
|---|
| 273 | try |
|---|
| 274 | { |
|---|
| 275 | _profile_builder.clean (); |
|---|
| 276 | } |
|---|
| 277 | catch (GLib.Error e) |
|---|
| 278 | { |
|---|
| 279 | _view.show_error_message (_("Error trying to clean profile %s: %s").printf (current_profile.name, e.message)); |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | private void on_close_clicked () |
|---|
| 284 | { |
|---|
| 285 | _page_logic.remove_project (_page_logic.current_project); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | private void on_project_changed () |
|---|
| 289 | { |
|---|
| 290 | var project_selected = _page_logic.current_project != null; |
|---|
| 291 | _view.add_file_sensitive = project_selected; |
|---|
| 292 | _view.add_profile_sensitive = project_selected; |
|---|
| 293 | _view.close_sensitive = project_selected; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | private void on_profile_changed () |
|---|
| 297 | { |
|---|
| 298 | var profile_selected = _view.page_view.current_profile != null && |
|---|
| 299 | _view.page_view.current_profile != "__NONE__"; |
|---|
| 300 | var file_selected = _view.page_view.current_file != null; |
|---|
| 301 | _view.remove_file_from_profile_sensitive = profile_selected && file_selected; |
|---|
| 302 | _view.edit_profile_sensitive = profile_selected; |
|---|
| 303 | _view.page_view.profile_edit_sensitive = profile_selected; |
|---|
| 304 | _view.duplicate_profile_sensitive = profile_selected; |
|---|
| 305 | _view.remove_profile_sensitive = profile_selected; |
|---|
| 306 | _view.build_profile_sensitive = profile_selected && !_profile_builder.is_building; |
|---|
| 307 | _view.clean_profile_sensitive = profile_selected && !_profile_builder.is_building; |
|---|
| 308 | _view.program_profile_sensitive = profile_selected && !_profile_builder.is_building && _programmers_logic.can_write_flash; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | private void on_file_changed () |
|---|
| 312 | { |
|---|
| 313 | var profile_selected = _view.page_view.current_profile != null && |
|---|
| 314 | _view.page_view.current_profile != "__NONE__"; |
|---|
| 315 | var file_selected = _view.page_view.current_file != null; |
|---|
| 316 | _view.remove_file_from_project_sensitive = file_selected; |
|---|
| 317 | _view.remove_file_from_profile_sensitive = profile_selected && file_selected; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | private void on_command_activated (string output, CommandContext command_context) |
|---|
| 321 | { |
|---|
| 322 | if (command_context.output_regex == null || |
|---|
| 323 | command_context.output_regex == "") |
|---|
| 324 | return; |
|---|
| 325 | |
|---|
| 326 | try |
|---|
| 327 | { |
|---|
| 328 | var regex = new Regex (command_context.output_regex); |
|---|
| 329 | |
|---|
| 330 | MatchInfo match_info; |
|---|
| 331 | if (!regex.match (output, 0, out match_info)) |
|---|
| 332 | return; |
|---|
| 333 | |
|---|
| 334 | var line = match_info.fetch_named ("line"); |
|---|
| 335 | _documents_logic.open_document (command_context.source_uri, line.to_int ()); |
|---|
| 336 | } |
|---|
| 337 | catch (Error e) |
|---|
| 338 | { |
|---|
| 339 | warning (e.message); |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | private void on_profile_builder_is_building () |
|---|
| 344 | { |
|---|
| 345 | var is_building = _profile_builder.is_building; |
|---|
| 346 | _view.build_profile_sensitive = !is_building; |
|---|
| 347 | _view.stop_sensitive = is_building; |
|---|
| 348 | _view.program_profile_sensitive = !is_building; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | private void on_profile_builder_new_message (string message) |
|---|
| 352 | { |
|---|
| 353 | _view.build_log_page.add_message (message); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | private void on_profile_builder_command_finished (CommandContext command_context) |
|---|
| 357 | { |
|---|
| 358 | _view.build_log_page.add_command (command_context); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | private void on_programmers_logic_can_write_flash_changed () |
|---|
| 362 | { |
|---|
| 363 | var profile_selected = _view.page_view.current_profile != null && |
|---|
| 364 | _view.page_view.current_profile != "__NONE__"; |
|---|
| 365 | _view.program_profile_sensitive = profile_selected && !_profile_builder.is_building && _programmers_logic.can_write_flash; |
|---|
| 366 | } |
|---|
| 367 | } |
|---|