| 1 | /* projectspagelogic.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.ProjectsPageLogic : GLib.Object |
|---|
| 24 | { |
|---|
| 25 | private ProjectsPageView _view; |
|---|
| 26 | private DocumentsLogic _documents_logic; |
|---|
| 27 | private ArrayList<Project> _projects = new ArrayList<Project> (); |
|---|
| 28 | private Project _current_project; |
|---|
| 29 | private string _current_profile; |
|---|
| 30 | |
|---|
| 31 | public Project current_project |
|---|
| 32 | { |
|---|
| 33 | set { _view.current_project = value.uri; } |
|---|
| 34 | get { return _current_project; } |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public Gee.List<Project> projects { owned get { return _projects.read_only_view; } } |
|---|
| 38 | |
|---|
| 39 | public ProjectsPageLogic (ProjectsPageView view, DocumentsLogic documents_logic) |
|---|
| 40 | { |
|---|
| 41 | _view = view; |
|---|
| 42 | _documents_logic = documents_logic; |
|---|
| 43 | |
|---|
| 44 | //Configure view |
|---|
| 45 | _view.tab_title = _("Projects"); |
|---|
| 46 | _view.profiles_check_list_sensitive = false; |
|---|
| 47 | _view.profiles_check_list_visible = false; |
|---|
| 48 | _view.builders_list_sensitive = false; |
|---|
| 49 | _view.builders_list_visible = false; |
|---|
| 50 | foreach (var builder in I4uc.Core.Settings.instance.builders.keys) |
|---|
| 51 | _view.builders_list.add_builder (builder); |
|---|
| 52 | |
|---|
| 53 | //Connect view signals |
|---|
| 54 | _view.project_changed.connect (on_project_changed); |
|---|
| 55 | _view.profile_changed.connect (on_profile_changed); |
|---|
| 56 | _view.profile_edit_clicked.connect (edit_current_profile); |
|---|
| 57 | _view.profile_check_changed.connect (on_profile_check_changed); |
|---|
| 58 | _view.file_activated.connect (on_file_activated); |
|---|
| 59 | _view.file_changed.connect (on_file_changed); |
|---|
| 60 | _view.builders_list.builder_changed.connect (on_builder_changed); |
|---|
| 61 | _view.builders_list.device_type_changed.connect (on_device_type_changed); |
|---|
| 62 | _view.builders_list.device_changed.connect (on_device_changed); |
|---|
| 63 | _view.builders_list.notify["current-extra-options"].connect (on_extra_options_changed); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | public void add_project (Project project) |
|---|
| 67 | { |
|---|
| 68 | if (!(project in _projects)) |
|---|
| 69 | { |
|---|
| 70 | _view.add_project (project.uri, project.name); |
|---|
| 71 | _projects.add (project); |
|---|
| 72 | } |
|---|
| 73 | _view.current_project = project.uri; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | public void remove_project (Project project) |
|---|
| 77 | { |
|---|
| 78 | if (!(project in _projects)) |
|---|
| 79 | return; |
|---|
| 80 | _projects.remove (project); |
|---|
| 81 | _view.remove_project (project.uri); |
|---|
| 82 | if (_projects.size > 0) |
|---|
| 83 | _view.current_project = _projects[0].uri; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public void remove_all_projects () |
|---|
| 87 | { |
|---|
| 88 | for (int i = _projects.size-1; i >= 0; i--) |
|---|
| 89 | { |
|---|
| 90 | var project = _projects[i]; |
|---|
| 91 | _projects.remove (project); |
|---|
| 92 | _view.remove_project (project.uri); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public void update_profiles_list () |
|---|
| 97 | { |
|---|
| 98 | on_project_changed (); |
|---|
| 99 | if (_current_profile in _current_project.profiles.keys) |
|---|
| 100 | _view.current_profile = _current_profile; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public void update_files_list () |
|---|
| 104 | { |
|---|
| 105 | on_profile_changed (); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | public void edit_current_profile () |
|---|
| 109 | { |
|---|
| 110 | var dialog_view = _view.create_profile_dialog_view (); |
|---|
| 111 | var profile = _current_project.profiles[_current_profile]; |
|---|
| 112 | var dialog_logic = new ProfileDialogLogic (dialog_view, _current_project, profile); |
|---|
| 113 | |
|---|
| 114 | if (dialog_logic.run ()) |
|---|
| 115 | { |
|---|
| 116 | _current_project.profiles.unset (profile.name); |
|---|
| 117 | profile.name = dialog_view.profile_name; |
|---|
| 118 | profile.is_public = dialog_view.is_public; |
|---|
| 119 | profile.files.clear (); |
|---|
| 120 | profile.files.add_all (dialog_view.activated_files); |
|---|
| 121 | _current_project.profiles[profile.name] = profile; |
|---|
| 122 | _current_project.save (); |
|---|
| 123 | _view.change_profile (_current_profile, profile.name); |
|---|
| 124 | _current_profile = profile.name; |
|---|
| 125 | on_profile_changed (); |
|---|
| 126 | } |
|---|
| 127 | dialog_logic.close (); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | public void duplicate_current_profile () |
|---|
| 131 | { |
|---|
| 132 | var dialog_view = _view.create_profile_dialog_view (); |
|---|
| 133 | var profile = _current_project.profiles[_current_profile]; |
|---|
| 134 | var dialog_logic = new ProfileDialogLogic (dialog_view, _current_project, profile, true); |
|---|
| 135 | |
|---|
| 136 | if (dialog_logic.run ()) |
|---|
| 137 | { |
|---|
| 138 | var new_profile = new ProjectProfile (); |
|---|
| 139 | new_profile.name = dialog_view.profile_name; |
|---|
| 140 | new_profile.is_public = dialog_view.is_public; |
|---|
| 141 | new_profile.builder_id = profile.builder_id; |
|---|
| 142 | new_profile.device_type = profile.device_type; |
|---|
| 143 | new_profile.device = profile.device; |
|---|
| 144 | new_profile.extra_options = profile.extra_options; |
|---|
| 145 | new_profile.files.add_all (dialog_view.activated_files); |
|---|
| 146 | _current_project.profiles[new_profile.name] = new_profile; |
|---|
| 147 | _current_project.save (); |
|---|
| 148 | on_project_changed (); |
|---|
| 149 | _current_profile = new_profile.name; |
|---|
| 150 | _view.current_profile = _current_profile; |
|---|
| 151 | } |
|---|
| 152 | dialog_logic.close (); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | private void on_project_changed () |
|---|
| 156 | { |
|---|
| 157 | _view.clear_profiles_list (); |
|---|
| 158 | |
|---|
| 159 | _current_project = get_project_from_uri (_view.current_project); |
|---|
| 160 | if (_current_project == null) |
|---|
| 161 | return; |
|---|
| 162 | |
|---|
| 163 | //Load profiles combo |
|---|
| 164 | _view.add_profile ("__NONE__", _("None"), false); |
|---|
| 165 | foreach (var profile in _current_project.profiles.values) |
|---|
| 166 | _view.add_profile (profile.name); |
|---|
| 167 | _view.current_profile = "__NONE__"; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | private void on_profile_changed () |
|---|
| 171 | { |
|---|
| 172 | _view.clear_files_list (); |
|---|
| 173 | _view.profiles_check_list_sensitive = false; |
|---|
| 174 | _view.profiles_check_list_visible = false; |
|---|
| 175 | _view.builders_list_sensitive = false; |
|---|
| 176 | _view.builders_list_visible = false; |
|---|
| 177 | |
|---|
| 178 | if (_current_project == null) |
|---|
| 179 | return; |
|---|
| 180 | |
|---|
| 181 | _current_profile = _view.current_profile; |
|---|
| 182 | if (_current_profile == null) |
|---|
| 183 | return; |
|---|
| 184 | |
|---|
| 185 | if (_current_profile == "__NONE__") |
|---|
| 186 | { |
|---|
| 187 | foreach (var file in _current_project.files) |
|---|
| 188 | _view.add_file (file); |
|---|
| 189 | _view.profiles_check_list_visible = true; |
|---|
| 190 | return; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | var profile = _current_project.profiles[_current_profile]; |
|---|
| 194 | |
|---|
| 195 | foreach (var file in profile.files) |
|---|
| 196 | _view.add_file (file); |
|---|
| 197 | |
|---|
| 198 | _view.builders_list.current_builder = profile.builder_id; |
|---|
| 199 | _view.builders_list.current_device_type = profile.device_type; |
|---|
| 200 | _view.builders_list.current_device = profile.device; |
|---|
| 201 | _view.builders_list.current_extra_options = profile.extra_options; |
|---|
| 202 | |
|---|
| 203 | _view.builders_list_sensitive = true; |
|---|
| 204 | _view.builders_list_visible = true; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | private void on_profile_check_changed (string profile, bool active) |
|---|
| 208 | { |
|---|
| 209 | if (!_view.profiles_check_list_sensitive) |
|---|
| 210 | return; |
|---|
| 211 | |
|---|
| 212 | if (_current_project == null || _view.current_file == null) |
|---|
| 213 | return; |
|---|
| 214 | |
|---|
| 215 | if (active) |
|---|
| 216 | _current_project.profiles[profile].files.add (_view.current_file); |
|---|
| 217 | else |
|---|
| 218 | _current_project.profiles[profile].files.remove (_view.current_file); |
|---|
| 219 | _current_project.save (); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | private void on_file_activated () |
|---|
| 223 | { |
|---|
| 224 | var document_uri = _current_project.get_file_uri (_view.current_file); |
|---|
| 225 | _documents_logic.open_document (document_uri); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | private void on_file_changed () |
|---|
| 229 | { |
|---|
| 230 | _view.profiles_check_list_sensitive = false; |
|---|
| 231 | _view.uncheck_all_profiles (); |
|---|
| 232 | |
|---|
| 233 | if (_current_project == null || _view.current_file == null) |
|---|
| 234 | return; |
|---|
| 235 | |
|---|
| 236 | foreach (var profile in _current_project.profiles.values) |
|---|
| 237 | _view.check_profile (profile.name, _view.current_file in profile.files); |
|---|
| 238 | _view.profiles_check_list_sensitive = true; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | private void on_builder_changed () |
|---|
| 242 | { |
|---|
| 243 | _view.builders_list.clear_device_types_list (); |
|---|
| 244 | |
|---|
| 245 | if (_current_project == null || _current_profile == null) |
|---|
| 246 | return; |
|---|
| 247 | |
|---|
| 248 | var current_builder = _view.builders_list.current_builder; |
|---|
| 249 | if (_view.builders_list_sensitive) |
|---|
| 250 | { |
|---|
| 251 | _current_project.profiles[_current_profile].builder_id = current_builder ?? ""; |
|---|
| 252 | _current_project.save (); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | if (current_builder == null) |
|---|
| 256 | return; |
|---|
| 257 | |
|---|
| 258 | var builder = I4uc.Core.Settings.instance.builders[current_builder]; |
|---|
| 259 | foreach (var device_type in builder.device_types.keys) |
|---|
| 260 | _view.builders_list.add_device_type (device_type); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | private void on_device_type_changed () |
|---|
| 264 | { |
|---|
| 265 | _view.builders_list.clear_devices_list (); |
|---|
| 266 | |
|---|
| 267 | if (_current_project == null || _current_profile == null) |
|---|
| 268 | return; |
|---|
| 269 | |
|---|
| 270 | var current_device_type = _view.builders_list.current_device_type; |
|---|
| 271 | if (_view.builders_list_sensitive) |
|---|
| 272 | { |
|---|
| 273 | _current_project.profiles[_current_profile].device_type = current_device_type ?? ""; |
|---|
| 274 | _current_project.save (); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if (current_device_type == null) |
|---|
| 278 | return; |
|---|
| 279 | |
|---|
| 280 | var builder = I4uc.Core.Settings.instance.builders[_view.builders_list.current_builder]; |
|---|
| 281 | var device_type = builder.device_types[current_device_type]; |
|---|
| 282 | foreach (var device in device_type.devices) |
|---|
| 283 | _view.builders_list.add_device (device); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | private void on_device_changed () |
|---|
| 287 | { |
|---|
| 288 | if (!_view.builders_list_sensitive) |
|---|
| 289 | return; |
|---|
| 290 | |
|---|
| 291 | if (_current_project == null || _current_profile == null) |
|---|
| 292 | return; |
|---|
| 293 | |
|---|
| 294 | var current_device = _view.builders_list.current_device; |
|---|
| 295 | _current_project.profiles[_current_profile].device = current_device ?? ""; |
|---|
| 296 | _current_project.save (); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | private void on_extra_options_changed () |
|---|
| 300 | { |
|---|
| 301 | if (!_view.builders_list_sensitive) |
|---|
| 302 | return; |
|---|
| 303 | |
|---|
| 304 | if (_current_project == null || _current_profile == null) |
|---|
| 305 | return; |
|---|
| 306 | |
|---|
| 307 | var current_extra_options = _view.builders_list.current_extra_options; |
|---|
| 308 | _current_project.profiles[_current_profile].extra_options = current_extra_options ?? ""; |
|---|
| 309 | _current_project.save (); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | private Project? get_project_from_uri (string? project_uri) |
|---|
| 313 | { |
|---|
| 314 | foreach (var project in _projects) |
|---|
| 315 | if (project.uri == project_uri) |
|---|
| 316 | return project; |
|---|
| 317 | return null; |
|---|
| 318 | } |
|---|
| 319 | } |
|---|