source: libi4uc/i4ucprojectssidepagepresenter.vala @ cf36b3a262f09427a72acf9406f81506ecf9016b

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

Projects: Select builder, device type and device in profiles

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