source: libi4uccore/projectspagelogic.vala @ 0dd271e3c98526974d2a95e1a9fa5b1c77ab0d8c

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

Projects: Remove unnecessary code

  • Property mode set to 100644
Line 
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 */
21using Gee;
22
23public 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                _view.builders_list.compile_options_visible = false;
51                foreach (var builder in I4uc.Core.Settings.instance.builders.keys)
52                        _view.builders_list.add_builder (builder);
53
54                //Connect view signals
55                _view.notify["current-project"].connect (on_project_changed);
56                _view.notify["current-profile"].connect (on_profile_changed);
57                _view.profile_edit_clicked.connect (edit_current_profile);
58                _view.profile_check_changed.connect (on_profile_check_changed);
59                _view.file_activated.connect (on_file_activated);
60                _view.file_changed.connect (on_file_changed);
61                _view.builders_list.notify["current-builder"].connect (on_builder_changed);
62                _view.builders_list.notify["current-device-type"].connect (on_device_type_changed);
63                _view.builders_list.notify["current-device"].connect (on_device_changed);
64                _view.builders_list.notify["current-compile-options"].connect (on_compile_options_changed);
65                _view.builders_list.notify["current-link-options"].connect (on_link_options_changed);
66        }
67
68        public void add_project (Project project)
69        {
70                if (!(project in _projects))
71                {
72                        _view.add_project (project.uri, project.name);
73                        _projects.add (project);
74                }
75                _view.current_project = project.uri;
76        }
77
78        public void remove_project (Project project)
79        {
80                if (!(project in _projects))
81                        return;
82                _projects.remove (project);
83                _view.remove_project (project.uri);
84                if (_projects.size > 0)
85                        _view.current_project = _projects[0].uri;
86        }
87
88        public void remove_all_projects ()
89        {
90                for (int i = _projects.size-1; i >= 0; i--)
91                {
92                        var project = _projects[i];
93                        _projects.remove (project);
94                        _view.remove_project (project.uri);
95                }
96        }
97
98        public void update_profiles_list ()
99        {
100                on_project_changed ();
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.link_options = profile.link_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_link_options = profile.link_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                _view.builders_list.compile_options_visible = false;
233
234                if (_current_project == null || _view.current_file == null)
235                        return;
236               
237                // Check selected profiles
238                foreach (var profile in _current_project.profiles.values)
239                        _view.check_profile (profile.name, _view.current_file in profile.files);
240                _view.profiles_check_list_sensitive = true;
241               
242                // Show compile options if the file can be compiled
243                if (_current_profile == null ||
244                    _view.builders_list.current_builder == null ||
245                    _view.builders_list.current_builder == "" ||
246                    _view.builders_list.current_device_type == null ||
247                    _view.builders_list.current_device_type == "")
248                        return;
249               
250                var profile = _current_project.profiles[_current_profile];
251                var builder = I4uc.Core.Settings.instance.builders[_view.builders_list.current_builder];
252                var device_type = builder.device_types[_view.builders_list.current_device_type];
253               
254                if (device_type.get_source_type_from_filename (_view.current_file) == null)
255                        return;
256               
257                if (profile.compile_options.has_key (_view.current_file))
258                        _view.builders_list.current_compile_options = profile.compile_options[_view.current_file];
259                else
260                        _view.builders_list.current_compile_options = "";
261                _view.builders_list.compile_options_visible = true;
262        }
263
264        private void on_builder_changed ()
265        {
266                _view.builders_list.clear_device_types_list ();
267
268                if (_current_project == null || _current_profile == null)
269                        return;
270               
271                var current_builder = _view.builders_list.current_builder;
272                if (_view.builders_list_sensitive)
273                {
274                        _current_project.profiles[_current_profile].builder_id = current_builder ?? "";
275                        _current_project.save ();
276                }
277
278                if (current_builder == null || current_builder == "")
279                        return;
280               
281                var builder = I4uc.Core.Settings.instance.builders[current_builder];
282                foreach (var device_type in builder.device_types.keys)
283                        _view.builders_list.add_device_type (device_type);
284        }
285
286        private void on_device_type_changed ()
287        {
288                _view.builders_list.clear_devices_list ();
289
290                if (_current_project == null || _current_profile == null)
291                        return;
292               
293                var current_device_type = _view.builders_list.current_device_type;
294                if (_view.builders_list_sensitive)
295                {
296                        _current_project.profiles[_current_profile].device_type = current_device_type ?? "";
297                        _current_project.save ();
298                }
299               
300                if (current_device_type == null || current_device_type == "")
301                        return;
302
303                var builder = I4uc.Core.Settings.instance.builders[_view.builders_list.current_builder];
304                var device_type = builder.device_types[current_device_type];
305                foreach (var device in device_type.devices)
306                        _view.builders_list.add_device (device);
307
308                // Show compile options if there's a file selected that can be compiled
309                _view.builders_list.compile_options_visible = false;
310               
311                if (_view.current_file == null)
312                        return;
313               
314                if (device_type.get_source_type_from_filename (_view.current_file) == null)
315                        return;
316                       
317                var profile = _current_project.profiles[_current_profile];
318                if (profile.compile_options.has_key (_view.current_file))
319                        _view.builders_list.current_compile_options = profile.compile_options[_view.current_file];
320                else
321                        _view.builders_list.current_compile_options = "";
322                _view.builders_list.compile_options_visible = true;
323        }
324
325        private void on_device_changed ()
326        {
327                if (!_view.builders_list_sensitive)
328                        return;
329
330                if (_current_project == null || _current_profile == null)
331                        return;
332               
333                var current_device = _view.builders_list.current_device;
334                _current_project.profiles[_current_profile].device = current_device ?? "";
335                _current_project.save ();
336        }
337       
338        private void on_compile_options_changed ()
339        {
340                if (!_view.builders_list_sensitive || !_view.builders_list.compile_options_visible)
341                        return;
342               
343                if (_current_project == null || _current_profile == null || _view.current_file == null)
344                        return;
345               
346                var profile = _current_project.profiles[_current_profile];
347                var current_compile_options = _view.builders_list.current_compile_options;
348               
349                profile.compile_options[_view.current_file] = current_compile_options;
350                _current_project.save ();
351        }
352       
353        private void on_link_options_changed ()
354        {
355                if (!_view.builders_list_sensitive)
356                        return;
357               
358                if (_current_project == null || _current_profile == null)
359                        return;
360               
361                var current_link_options = _view.builders_list.current_link_options;
362                _current_project.profiles[_current_profile].link_options = current_link_options ?? "";
363                _current_project.save ();
364        }
365
366        private Project? get_project_from_uri (string? project_uri)
367        {
368                foreach (var project in _projects)
369                        if (project.uri == project_uri)
370                                return project;
371                return null;
372        }
373}
Note: See TracBrowser for help on using the repository browser.