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