source: libi4uccore/projectspagelogic.vala @ c15395525506be7df1e1d05b82bfb04f27268ab7

Revision c15395525506be7df1e1d05b82bfb04f27268ab7, 12.6 KB checked in by Matias De la Puente <mfpuente.ar@…>, 2 years ago (diff)

Projects: Cleanup code in ProfileDialog?

  • 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                }
125                dialog_view.close_dialog ();
126        }
127       
128        public void duplicate_current_profile ()
129        {
130                var dialog_view = _view.create_profile_dialog_view ();
131                var profile = _current_project.profiles[_current_profile];
132                var dialog_logic = new ProfileDialogLogic (dialog_view, _current_project, profile, true);
133               
134                if (dialog_logic.run ())
135                {
136                        var new_profile = new Profile ();
137                        new_profile.name = dialog_view.profile_name;
138                        new_profile.is_public = dialog_view.is_public;
139                        new_profile.builder_id = profile.builder_id;
140                        new_profile.device_type = profile.device_type;
141                        new_profile.device = profile.device;
142                        new_profile.link_options = profile.link_options;
143                        new_profile.compile_options.set_all (profile.compile_options);
144                        new_profile.files.add_all (dialog_view.activated_files);
145                        _current_project.profiles[new_profile.name] = new_profile;
146                        _current_project.save ();
147                        on_project_changed ();
148                        _current_profile = new_profile.name;
149                        _view.current_profile = _current_profile;
150                }
151                dialog_view.close_dialog ();
152        }
153
154        private void on_project_changed ()
155        {
156                _view.clear_profiles_list ();
157
158                _current_project = get_project_from_uri (_view.current_project);
159                if (_current_project == null)
160                        return;
161
162                _view.current_project_tooltip = "<b>%s</b> %s\n".printf (_("Name:"), _current_project.name) +
163                                                "<b>%s</b> %s".printf (_("File:"), _current_project.path);
164               
165                //Load profiles combo
166                _view.add_profile ("__NONE__", _("None"), false);
167                foreach (var profile in _current_project.profiles.values)
168                        _view.add_profile (profile.name);
169                _view.current_profile = "__NONE__";
170        }
171       
172        private void on_profile_changed ()
173        {
174                _view.clear_files_list ();
175                _view.profiles_check_list_sensitive = false;
176                _view.profiles_check_list_visible = false;
177                _view.builders_list_sensitive = false;
178                _view.builders_list_visible = false;
179                _view.builders_list.current_builder = "";
180                _view.builders_list.current_device_type = "";
181                _view.builders_list.current_device = "";
182
183                if (_current_project == null)
184                        return;
185               
186                _current_profile = _view.current_profile;
187                if (_current_profile == null)
188                        return;
189
190                if (_current_profile == "__NONE__")
191                {
192                        foreach (var file in _current_project.files)
193                                _view.add_file (file);
194                        _view.profiles_check_list_visible = true;
195                        return;
196                }
197               
198                var profile = _current_project.profiles[_current_profile];
199               
200                foreach (var file in profile.files)
201                        _view.add_file (file);
202               
203                // Check if builder, device type and device exists before using them
204                if (I4uc.Core.Settings.instance.builders.has_key (profile.builder_id))
205                {
206                        var builder = I4uc.Core.Settings.instance.builders[profile.builder_id];
207                        _view.builders_list.current_builder = profile.builder_id;
208                       
209                        if (builder.device_types.has_key (profile.device_type))
210                        {
211                                var device_type = builder.device_types[profile.device_type];
212                                _view.builders_list.current_device_type = profile.device_type;
213                               
214                                if (profile.device in device_type.devices)
215                                        _view.builders_list.current_device = profile.device;
216                        }
217                }
218                _view.builders_list.current_link_options = profile.link_options;
219               
220                _view.builders_list_sensitive = true;
221                _view.builders_list_visible = true;
222        }
223
224        private void on_profile_check_changed (string profile, bool active)
225        {
226                if (!_view.profiles_check_list_sensitive)
227                        return;
228
229                if (_current_project == null || _view.current_file == null)
230                        return;
231
232                if (active)
233                        _current_project.profiles[profile].files.add (_view.current_file);
234                else
235                        _current_project.profiles[profile].files.remove (_view.current_file);
236                _current_project.save ();
237        }
238
239        private void on_file_activated ()
240        {
241                var document_uri = _current_project.get_file_uri (_view.current_file);
242                _documents_logic.open_document (document_uri);
243        }
244
245        private void on_file_changed ()
246        {
247                _view.profiles_check_list_sensitive = false;
248                _view.uncheck_all_profiles ();
249                _view.builders_list.compile_options_visible = false;
250
251                if (_current_project == null || _view.current_file == null)
252                        return;
253               
254                // Check selected profiles
255                foreach (var profile in _current_project.profiles.values)
256                        _view.check_profile (profile.name, _view.current_file in profile.files);
257                _view.profiles_check_list_sensitive = true;
258               
259                // Show compile options if the file can be compiled
260                if (_current_profile == null ||
261                    _view.builders_list.current_builder == null ||
262                    _view.builders_list.current_builder == "" ||
263                    _view.builders_list.current_device_type == null ||
264                    _view.builders_list.current_device_type == "")
265                        return;
266               
267                var profile = _current_project.profiles[_current_profile];
268                var builder = I4uc.Core.Settings.instance.builders[_view.builders_list.current_builder];
269                var device_type = builder.device_types[_view.builders_list.current_device_type];
270               
271                if (device_type.get_source_type_from_filename (_view.current_file) == null)
272                        return;
273               
274                if (profile.compile_options.has_key (_view.current_file))
275                        _view.builders_list.current_compile_options = profile.compile_options[_view.current_file];
276                else
277                        _view.builders_list.current_compile_options = "";
278                _view.builders_list.compile_options_visible = true;
279        }
280
281        private void on_builder_changed ()
282        {
283                _view.builders_list.clear_device_types_list ();
284
285                if (_current_project == null || _current_profile == null)
286                        return;
287               
288                var current_builder = _view.builders_list.current_builder;
289                if (_view.builders_list_sensitive)
290                {
291                        _current_project.profiles[_current_profile].builder_id = current_builder ?? "";
292                        _current_project.profiles[_current_profile].parameters_changed = true;
293                        _current_project.save ();
294                }
295
296                if (current_builder == null || current_builder == "")
297                        return;
298               
299                var builder = I4uc.Core.Settings.instance.builders[current_builder];
300                foreach (var device_type in builder.device_types.keys)
301                        _view.builders_list.add_device_type (device_type);
302        }
303
304        private void on_device_type_changed ()
305        {
306                _view.builders_list.clear_devices_list ();
307
308                if (_current_project == null || _current_profile == null)
309                        return;
310               
311                var current_device_type = _view.builders_list.current_device_type;
312                if (_view.builders_list_sensitive)
313                {
314                        _current_project.profiles[_current_profile].device_type = current_device_type ?? "";
315                        _current_project.profiles[_current_profile].parameters_changed = true;
316                        _current_project.save ();
317                }
318               
319                if (current_device_type == null || current_device_type == "")
320                        return;
321
322                var builder = I4uc.Core.Settings.instance.builders[_view.builders_list.current_builder];
323                var device_type = builder.device_types[current_device_type];
324                foreach (var device in device_type.devices)
325                        _view.builders_list.add_device (device);
326
327                // Show compile options if there's a file selected that can be compiled
328                _view.builders_list.compile_options_visible = false;
329               
330                if (_view.current_file == null)
331                        return;
332               
333                if (device_type.get_source_type_from_filename (_view.current_file) == null)
334                        return;
335                       
336                var profile = _current_project.profiles[_current_profile];
337                if (profile.compile_options.has_key (_view.current_file))
338                        _view.builders_list.current_compile_options = profile.compile_options[_view.current_file];
339                else
340                        _view.builders_list.current_compile_options = "";
341                _view.builders_list.compile_options_visible = true;
342        }
343
344        private void on_device_changed ()
345        {
346                if (!_view.builders_list_sensitive)
347                        return;
348
349                if (_current_project == null || _current_profile == null)
350                        return;
351               
352                var current_device = _view.builders_list.current_device;
353                _current_project.profiles[_current_profile].device = current_device ?? "";
354                _current_project.profiles[_current_profile].parameters_changed = true;
355                _current_project.save ();
356        }
357       
358        private void on_compile_options_changed ()
359        {
360                if (!_view.builders_list_sensitive || !_view.builders_list.compile_options_visible)
361                        return;
362               
363                if (_current_project == null || _current_profile == null || _view.current_file == null)
364                        return;
365               
366                var profile = _current_project.profiles[_current_profile];
367                var current_compile_options = _view.builders_list.current_compile_options;
368               
369                profile.compile_options[_view.current_file] = current_compile_options;
370                _current_project.profiles[_current_profile].parameters_changed = true;
371                _current_project.save ();
372        }
373       
374        private void on_link_options_changed ()
375        {
376                if (!_view.builders_list_sensitive)
377                        return;
378               
379                if (_current_project == null || _current_profile == null)
380                        return;
381               
382                var current_link_options = _view.builders_list.current_link_options;
383                _current_project.profiles[_current_profile].link_options = current_link_options ?? "";
384                _current_project.profiles[_current_profile].parameters_changed = true;
385                _current_project.save ();
386        }
387
388        private Project? get_project_from_uri (string? project_uri)
389        {
390                foreach (var project in _projects)
391                        if (project.uri == project_uri)
392                                return project;
393                return null;
394        }
395}
Note: See TracBrowser for help on using the repository browser.