source: libi4uc/i4ucprojectssidepagepresenter.vala @ f12219a27c07e386a589e6cd32c6a806f3f65319

Revision f12219a27c07e386a589e6cd32c6a806f3f65319, 8.8 KB checked in by Matias De la Puente <mfpuente.ar@…>, 3 years ago (diff)

Projects: Remove redundant code when device changed

  • Property mode set to 100644
Line 
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 */
21using Gee;
22
23public class I4uc.ProjectsSidePagePresenter : GLib.Object
24{
25        private ProjectsSidePageViewIface _view;
26        private DocumentsPresenter _documents_presenter;
27        private ArrayList<Project> _projects = new ArrayList<Project> ();
28
29        public Project current_project
30        {
31                set { _view.current_project = value.uri; }
32                owned get { return get_project_from_uri (_view.current_project); }
33        }
34       
35        public Gee.List<Project> projects { owned get { return _projects.read_only_view; } }
36
37        public ProjectsSidePagePresenter (ProjectsSidePageViewIface view, DocumentsPresenter documents_presenter)
38        {
39                _view = view;
40                _documents_presenter = documents_presenter;
41
42                //Configure view
43                _view.tab_title = _("Projects");
44                _view.profiles_check_list_sensitive = false;
45                _view.builders_list_sensitive = false;
46                foreach (var builder in I4uc.Settings.instance.builders.keys)
47                        _view.builders_list.add_builder (builder);
48                _view.builders_list.linker_script_visible = false;
49
50                //Connect view signals
51                _view.project_changed.connect (on_project_changed);
52                _view.profile_selected.connect (on_profile_selected);
53                _view.profile_check_changed.connect (on_profile_check_changed);
54                _view.file_activated.connect (on_file_activated);
55                _view.file_selected.connect (on_file_selected);
56                _view.builders_list.builder_changed.connect (on_builder_changed);
57                _view.builders_list.device_type_changed.connect (on_device_type_changed);
58                _view.builders_list.device_changed.connect (on_device_changed);
59                _view.builders_list.linker_script_changed.connect (on_linker_script_changed);
60        }
61
62        public void add_project (Project project)
63        {
64                if (!(project in _projects))
65                {
66                        _view.add_project (project.uri, project.name);
67                        _projects.add (project);
68                }
69                _view.current_project = project.uri;
70        }
71
72        public void remove_project (Project project)
73        {
74                if (!(project in _projects))
75                        return;
76                _projects.remove (project);
77                _view.remove_project (project.uri);
78                if (_projects.size > 0)
79                        _view.current_project = _projects[0].uri;
80        }
81
82        public void remove_all_projects ()
83        {
84                for (int i = _projects.size-1; i >= 0; i--)
85                {
86                        var project = _projects[i];
87                        _projects.remove (project);
88                        _view.remove_project (project.uri);
89                }
90        }
91
92        public void update_current_project ()
93        {
94                on_project_changed ();
95        }
96
97        private void on_project_changed ()
98        {
99                _view.clear_files_list ();
100                _view.clear_profiles_list ();
101
102                var current_project = get_project_from_uri (_view.current_project);
103                if (current_project == null)
104                        return;
105
106                //Load files list
107                foreach (var file in current_project.files)
108                        _view.add_file (file);
109
110                //Load profiles list
111                foreach (var profile in current_project.profiles.values)
112                {
113                        _view.add_profile (profile.name);
114                        foreach (var file in profile.files)
115                                _view.add_profile_file (profile.name, file);
116                }
117        }
118
119        private void on_profile_selected ()
120        {
121                _view.builders_list_sensitive = false;
122                _view.builders_list.linker_script_visible = false;
123
124                Project current_project;
125                string current_profile;
126                if (!get_current_project_and_profile (out current_project, out current_profile))
127                        return;
128
129                _view.builders_list.current_builder = current_project.profiles[current_profile].builder_id;
130                _view.builders_list.current_device_type = current_project.profiles[current_profile].device_type;
131                _view.builders_list.current_device = current_project.profiles[current_profile].device;
132               
133                var builder = I4uc.Settings.instance.builders[current_project.profiles[current_profile].builder_id];
134                if (builder != null)
135                {
136                        var device_type = builder.device_types[current_project.profiles[current_profile].device_type];
137                        if (device_type != null)
138                        {
139                                _view.builders_list.linker_script_visible = device_type.has_linker_script;
140                                if (_view.builders_list.linker_script_visible)
141                                        _view.builders_list.current_linker_script = current_project.profiles[current_profile].linker_script;
142                        }
143                }
144               
145                _view.builders_list_sensitive = true;
146        }
147
148        private void on_profile_check_changed (string profile, bool active)
149        {
150                if (!_view.profiles_check_list_sensitive)
151                        return;
152
153                var current_project = get_project_from_uri (_view.current_project);
154                if (current_project == null || _view.current_file == null)
155                        return;
156
157                if (active)
158                {
159                        current_project.profiles[profile].files.add (_view.current_file);
160                        _view.add_profile_file (profile, _view.current_file);
161                }
162                else
163                {
164                        current_project.profiles[profile].files.remove (_view.current_file);
165                        _view.remove_profile_file (profile, _view.current_file);
166                }
167                current_project.save ();
168        }
169
170        private void on_file_activated ()
171        {
172                var document_uri = this.current_project.get_file_uri (_view.current_file);
173                _documents_presenter.open_document (document_uri);
174        }
175
176        private void on_file_selected ()
177        {
178                _view.profiles_check_list_sensitive = false;
179                _view.uncheck_all_profiles ();
180
181                var current_project = get_project_from_uri (_view.current_project);
182                if (current_project == null || _view.current_file == null)
183                        return;
184
185                foreach (var profile in current_project.profiles.values)
186                        _view.check_profile (profile.name, _view.current_file in profile.files);
187                _view.profiles_check_list_sensitive = true;
188        }
189
190        private void on_builder_changed ()
191        {
192                _view.builders_list.clear_device_types_list ();
193
194                Project current_project;
195                string current_profile;
196                if (!get_current_project_and_profile (out current_project, out current_profile))
197                        return;
198               
199                var current_builder = _view.builders_list.current_builder;
200                if (_view.builders_list_sensitive)
201                {
202                        current_project.profiles[current_profile].builder_id = current_builder == null ? "" : current_builder;
203                        current_project.save ();
204                }
205
206                if (current_builder == null)
207                        return;
208               
209                var builder = I4uc.Settings.instance.builders[current_builder];
210                foreach (var device_type in builder.device_types.keys)
211                        _view.builders_list.add_device_type (device_type);
212        }
213
214        private void on_device_type_changed ()
215        {
216                _view.builders_list.clear_devices_list ();
217                _view.builders_list.linker_script_visible = false;
218
219                Project current_project;
220                string current_profile;
221                if (!get_current_project_and_profile (out current_project, out current_profile))
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                Project current_project;
250                string current_profile;
251                if (!get_current_project_and_profile (out current_project, out current_profile))
252                        return;
253               
254                var current_device = _view.builders_list.current_device;
255                current_project.profiles[current_profile].device = current_device == null ? "" : current_device;
256                current_project.save ();
257        }
258       
259        private void on_linker_script_changed ()
260        {
261                if (!_view.builders_list_sensitive)
262                        return;
263               
264                Project current_project;
265                string current_profile;
266                if (!get_current_project_and_profile (out current_project, out current_profile))
267                        return;
268               
269                var current_linker_script = _view.builders_list.current_linker_script;
270                current_project.profiles[current_profile].linker_script = current_linker_script == null ? "" : current_linker_script;
271                current_project.save ();
272        }
273
274        private Project? get_project_from_uri (string? project_uri)
275        {
276                foreach (var project in _projects)
277                        if (project.uri == project_uri)
278                                return project;
279                return null;
280        }
281
282        private bool get_current_project_and_profile (out Project current_project, out string current_profile)
283        {
284                current_project = get_project_from_uri (_view.current_project);
285                current_profile = _view.current_profile;
286                return (current_project != null) && (current_profile != null);
287        }
288}
Note: See TracBrowser for help on using the repository browser.