| 1 | /* i4ucprojectssidepagepresenter.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.ProjectsSidePagePresenter : GLib.Object |
|---|
| 24 | { |
|---|
| 25 | private ProjectsSidePageViewIface _view; |
|---|
| 26 | private DocumentsPresenter _documents_presenter; |
|---|
| 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 ProjectsSidePagePresenter (ProjectsSidePageViewIface view, DocumentsPresenter documents_presenter) |
|---|
| 40 | { |
|---|
| 41 | _view = view; |
|---|
| 42 | _documents_presenter = documents_presenter; |
|---|
| 43 | |
|---|
| 44 | //Configure view |
|---|
| 45 | _view.tab_title = _("Projects"); |
|---|
| 46 | _view.profiles_check_list_sensitive = false; |
|---|
| 47 | _view.builders_list_sensitive = false; |
|---|
| 48 | foreach (var builder in I4uc.Settings.instance.builders.keys) |
|---|
| 49 | _view.builders_list.add_builder (builder); |
|---|
| 50 | _view.builders_list.linker_script_visible = false; |
|---|
| 51 | |
|---|
| 52 | //Connect view signals |
|---|
| 53 | _view.project_changed.connect (on_project_changed); |
|---|
| 54 | _view.profile_selected.connect (on_profile_selected); |
|---|
| 55 | _view.profile_check_changed.connect (on_profile_check_changed); |
|---|
| 56 | _view.file_activated.connect (on_file_activated); |
|---|
| 57 | _view.file_selected.connect (on_file_selected); |
|---|
| 58 | _view.builders_list.builder_changed.connect (on_builder_changed); |
|---|
| 59 | _view.builders_list.device_type_changed.connect (on_device_type_changed); |
|---|
| 60 | _view.builders_list.device_changed.connect (on_device_changed); |
|---|
| 61 | _view.builders_list.linker_script_changed.connect (on_linker_script_changed); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public void add_project (Project project) |
|---|
| 65 | { |
|---|
| 66 | if (!(project in _projects)) |
|---|
| 67 | { |
|---|
| 68 | _view.add_project (project.uri, project.name); |
|---|
| 69 | _projects.add (project); |
|---|
| 70 | } |
|---|
| 71 | _view.current_project = project.uri; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | public void remove_project (Project project) |
|---|
| 75 | { |
|---|
| 76 | if (!(project in _projects)) |
|---|
| 77 | return; |
|---|
| 78 | _projects.remove (project); |
|---|
| 79 | _view.remove_project (project.uri); |
|---|
| 80 | if (_projects.size > 0) |
|---|
| 81 | _view.current_project = _projects[0].uri; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public void remove_all_projects () |
|---|
| 85 | { |
|---|
| 86 | for (int i = _projects.size-1; i >= 0; i--) |
|---|
| 87 | { |
|---|
| 88 | var project = _projects[i]; |
|---|
| 89 | _projects.remove (project); |
|---|
| 90 | _view.remove_project (project.uri); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public void update_current_project () |
|---|
| 95 | { |
|---|
| 96 | on_project_changed (); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | private void on_project_changed () |
|---|
| 100 | { |
|---|
| 101 | _view.clear_files_list (); |
|---|
| 102 | _view.clear_profiles_list (); |
|---|
| 103 | |
|---|
| 104 | _current_project = get_project_from_uri (_view.current_project); |
|---|
| 105 | if (_current_project == null) |
|---|
| 106 | return; |
|---|
| 107 | |
|---|
| 108 | //Load files list |
|---|
| 109 | foreach (var file in _current_project.files) |
|---|
| 110 | _view.add_file (file); |
|---|
| 111 | |
|---|
| 112 | //Load profiles list |
|---|
| 113 | foreach (var profile in _current_project.profiles.values) |
|---|
| 114 | { |
|---|
| 115 | _view.add_profile (profile.name); |
|---|
| 116 | foreach (var file in profile.files) |
|---|
| 117 | _view.add_profile_file (profile.name, file); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | private void on_profile_selected () |
|---|
| 122 | { |
|---|
| 123 | _view.builders_list_sensitive = false; |
|---|
| 124 | _view.builders_list.linker_script_visible = false; |
|---|
| 125 | |
|---|
| 126 | if (_current_project == null) |
|---|
| 127 | return; |
|---|
| 128 | |
|---|
| 129 | _current_profile = _view.current_profile; |
|---|
| 130 | if (_current_profile == null) |
|---|
| 131 | return; |
|---|
| 132 | |
|---|
| 133 | var profile = _current_project.profiles[_current_profile]; |
|---|
| 134 | |
|---|
| 135 | _view.builders_list.current_builder = profile.builder_id; |
|---|
| 136 | _view.builders_list.current_device_type = profile.device_type; |
|---|
| 137 | _view.builders_list.current_device = profile.device; |
|---|
| 138 | |
|---|
| 139 | var builder = I4uc.Settings.instance.builders[profile.builder_id]; |
|---|
| 140 | if (builder != null) |
|---|
| 141 | { |
|---|
| 142 | var device_type = builder.device_types[profile.device_type]; |
|---|
| 143 | if (device_type != null) |
|---|
| 144 | { |
|---|
| 145 | _view.builders_list.linker_script_visible = device_type.has_linker_script; |
|---|
| 146 | if (_view.builders_list.linker_script_visible) |
|---|
| 147 | _view.builders_list.current_linker_script = profile.linker_script; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | _view.builders_list_sensitive = true; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | private void on_profile_check_changed (string profile, bool active) |
|---|
| 155 | { |
|---|
| 156 | if (!_view.profiles_check_list_sensitive) |
|---|
| 157 | return; |
|---|
| 158 | |
|---|
| 159 | if (_current_project == null || _view.current_file == null) |
|---|
| 160 | return; |
|---|
| 161 | |
|---|
| 162 | if (active) |
|---|
| 163 | { |
|---|
| 164 | _current_project.profiles[profile].files.add (_view.current_file); |
|---|
| 165 | _view.add_profile_file (profile, _view.current_file); |
|---|
| 166 | } |
|---|
| 167 | else |
|---|
| 168 | { |
|---|
| 169 | _current_project.profiles[profile].files.remove (_view.current_file); |
|---|
| 170 | _view.remove_profile_file (profile, _view.current_file); |
|---|
| 171 | } |
|---|
| 172 | _current_project.save (); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | private void on_file_activated () |
|---|
| 176 | { |
|---|
| 177 | var document_uri = _current_project.get_file_uri (_view.current_file); |
|---|
| 178 | _documents_presenter.open_document (document_uri); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | private void on_file_selected () |
|---|
| 182 | { |
|---|
| 183 | _view.profiles_check_list_sensitive = false; |
|---|
| 184 | _view.uncheck_all_profiles (); |
|---|
| 185 | |
|---|
| 186 | if (_current_project == null || _view.current_file == null) |
|---|
| 187 | return; |
|---|
| 188 | |
|---|
| 189 | foreach (var profile in _current_project.profiles.values) |
|---|
| 190 | _view.check_profile (profile.name, _view.current_file in profile.files); |
|---|
| 191 | _view.profiles_check_list_sensitive = true; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | private void on_builder_changed () |
|---|
| 195 | { |
|---|
| 196 | _view.builders_list.clear_device_types_list (); |
|---|
| 197 | |
|---|
| 198 | if (_current_project == null || _current_profile == null) |
|---|
| 199 | return; |
|---|
| 200 | |
|---|
| 201 | var current_builder = _view.builders_list.current_builder; |
|---|
| 202 | if (_view.builders_list_sensitive) |
|---|
| 203 | { |
|---|
| 204 | _current_project.profiles[_current_profile].builder_id = current_builder == null ? "" : current_builder; |
|---|
| 205 | _current_project.save (); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | if (current_builder == null) |
|---|
| 209 | return; |
|---|
| 210 | |
|---|
| 211 | var builder = I4uc.Settings.instance.builders[current_builder]; |
|---|
| 212 | foreach (var device_type in builder.device_types.keys) |
|---|
| 213 | _view.builders_list.add_device_type (device_type); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | private void on_device_type_changed () |
|---|
| 217 | { |
|---|
| 218 | _view.builders_list.clear_devices_list (); |
|---|
| 219 | _view.builders_list.linker_script_visible = false; |
|---|
| 220 | |
|---|
| 221 | if (_current_project == null || _current_profile == null) |
|---|
| 222 | return; |
|---|
| 223 | |
|---|
| 224 | var current_device_type = _view.builders_list.current_device_type; |
|---|
| 225 | if (_view.builders_list_sensitive) |
|---|
| 226 | { |
|---|
| 227 | _current_project.profiles[_current_profile].device_type = current_device_type == null ? "" : current_device_type; |
|---|
| 228 | _current_project.save (); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | if (current_device_type == null) |
|---|
| 232 | return; |
|---|
| 233 | |
|---|
| 234 | var builder = I4uc.Settings.instance.builders[_view.builders_list.current_builder]; |
|---|
| 235 | var device_type = builder.device_types[current_device_type]; |
|---|
| 236 | foreach (var device in device_type.devices) |
|---|
| 237 | _view.builders_list.add_device (device); |
|---|
| 238 | |
|---|
| 239 | _view.builders_list.linker_script_visible = device_type.has_linker_script; |
|---|
| 240 | if (_view.builders_list.linker_script_visible) |
|---|
| 241 | _view.builders_list.current_linker_script = _current_project.profiles[_current_profile].linker_script; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | private void on_device_changed () |
|---|
| 245 | { |
|---|
| 246 | if (!_view.builders_list_sensitive) |
|---|
| 247 | return; |
|---|
| 248 | |
|---|
| 249 | if (_current_project == null || _current_profile == null) |
|---|
| 250 | return; |
|---|
| 251 | |
|---|
| 252 | var current_device = _view.builders_list.current_device; |
|---|
| 253 | _current_project.profiles[_current_profile].device = current_device == null ? "" : current_device; |
|---|
| 254 | _current_project.save (); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | private void on_linker_script_changed () |
|---|
| 258 | { |
|---|
| 259 | if (!_view.builders_list_sensitive) |
|---|
| 260 | return; |
|---|
| 261 | |
|---|
| 262 | if (_current_project == null || _current_profile == null) |
|---|
| 263 | return; |
|---|
| 264 | |
|---|
| 265 | var current_linker_script = _view.builders_list.current_linker_script; |
|---|
| 266 | _current_project.profiles[_current_profile].linker_script = current_linker_script == null ? "" : current_linker_script; |
|---|
| 267 | _current_project.save (); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | private Project? get_project_from_uri (string? project_uri) |
|---|
| 271 | { |
|---|
| 272 | foreach (var project in _projects) |
|---|
| 273 | if (project.uri == project_uri) |
|---|
| 274 | return project; |
|---|
| 275 | return null; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|