source: libi4uccore/projectslogic.vala @ 4d634c019030af4f5b05625e436185d63551190a

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

Projects: Allow to overwrite files that already exists in the project

  • Property mode set to 100644
Line 
1/* projectslogic.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.ProjectsLogic : DocumentOpener, GLib.Object
24{
25        private ProjectsView _view;
26        private DocumentsLogic _documents_logic;
27        private ProgrammersLogic _programmers_logic;
28        private ProjectsPageLogic _page_logic;
29        private ProfileBuilder _profile_builder = new ProfileBuilder ();
30
31        public ProjectsLogic (ProjectsView view, DocumentsLogic documents_logic, ProgrammersLogic programmers_logic)
32        {
33                _view = view;
34                _documents_logic = documents_logic;
35                _programmers_logic = programmers_logic;
36               
37                _documents_logic.add_document_opener (".i4uc", this);
38
39                _page_logic = new ProjectsPageLogic (_view.page_view, _documents_logic);
40
41                //Configure view
42                _view.add_file_sensitive = false;
43                _view.remove_file_from_project_sensitive = false;
44                _view.remove_file_from_profile_sensitive = false;
45                _view.add_profile_sensitive = false;
46                _view.edit_profile_sensitive = false;
47                _view.page_view.profile_edit_sensitive = false;
48                _view.duplicate_profile_sensitive = false;
49                _view.remove_profile_sensitive = false;
50                _view.build_profile_sensitive = false;
51                _view.program_profile_sensitive = false;
52                _view.stop_sensitive = false;
53                _view.clean_profile_sensitive = false;
54                _view.close_sensitive = false;
55                _view.build_log_page.tab_title = _("Build process");
56                _view.build_log_page.tab_image = TabImage.BUILDER;
57                _view.build_log_page.message_format = "<b>%s</b>";
58                _view.build_log_page.error_format = "<b><span color=\"#D00000\">%s</span></b>";
59                _view.build_log_page.command_format = "\t<i>%s</i>";
60                _view.build_log_page.output_format = "\t\t%s";
61
62                //Connect view signals
63                _view.new_clicked.connect (on_new_clicked);
64                _view.add_file_clicked.connect (on_add_file_clicked);
65                _view.remove_file_from_project_clicked.connect (on_remove_file_from_project_clicked);
66                _view.remove_file_from_profile_clicked.connect (on_remove_file_from_profile_clicked);
67                _view.add_profile_clicked.connect (on_add_profile_clicked);
68                _view.edit_profile_clicked.connect (() => _page_logic.edit_current_profile ());
69                _view.duplicate_profile_clicked.connect (() => _page_logic.duplicate_current_profile ());
70                _view.remove_profile_clicked.connect (on_remove_profile_clicked);
71                _view.build_profile_clicked.connect (on_build_profile_clicked);
72                _view.program_profile_clicked.connect (on_program_profile_clicked);
73                _view.stop_clicked.connect (() => _profile_builder.stop ());
74                _view.clean_profile_clicked.connect (on_clean_profile_clicked);
75                _view.close_clicked.connect (() => _page_logic.remove_project (_page_logic.current_project));
76                _view.page_view.notify["current-project"].connect (on_project_changed);
77                _view.page_view.notify["current-profile"].connect (on_profile_changed);
78                _view.page_view.file_changed.connect (on_file_changed);
79                _view.build_log_page.command_activated.connect (on_command_activated);
80                _profile_builder.notify["is-building"].connect (on_profile_builder_is_building);
81                _profile_builder.new_message.connect ((message) => _view.build_log_page.add_message (message));
82                _profile_builder.new_error.connect ((error) => _view.build_log_page.add_error (error));
83                _profile_builder.command_finished.connect ((command_context) => _view.build_log_page.add_command (command_context));
84                _programmers_logic.notify["can-write-flash"].connect (on_programmers_logic_can_write_flash_changed);
85        }
86
87        public bool open_project (string project_uri)
88        {
89                var project = new Project ();
90                try
91                {
92                        project.open (project_uri);
93                        _page_logic.add_project (project);
94                        _view.add_recent (project_uri);
95                }
96                catch (GLib.Error e)
97                {
98                        _view.show_error_message (_("Error trying to open <<%s>>: %s").printf (project_uri, e.message));
99                        return false;
100                }
101                return true;
102        }
103       
104        public bool open_document (string document_uri)
105        {
106                return open_project (document_uri);
107        }
108
109        public void close_all_projects ()
110        {
111                _page_logic.remove_all_projects ();
112        }
113
114        private void on_new_clicked ()
115        {
116                string project_uri;
117                string project_name;
118                if (_view.show_new_dialog (out project_uri, out project_name) == DialogResponse.OK)
119                {
120                        var project = new Project ();
121                        project.name = project_name;
122
123                        var main_profile = new Profile ();
124                        main_profile.name = "main";
125                        project.profiles["main"] = main_profile;
126
127                        project.save (project_uri);
128                        _view.add_recent (project_uri);
129                        _page_logic.add_project (project);
130                }
131        }
132
133        private void on_add_file_clicked ()
134        {
135                var dialog_view = _view.create_add_file_dialog_view ();
136                var current_project = _page_logic.current_project;
137                var dialog_logic = new AddFileDialogLogic (dialog_view, current_project);
138               
139                if (dialog_logic.run ())
140                {
141                        string file;
142                        if (dialog_view.is_new)
143                        {
144                                // Create a new empty file
145                                var new_file = File.new_for_uri (current_project.get_file_uri (dialog_view.filename));
146                                var overwrite = true;
147                                if (new_file.query_exists (null))
148                                {
149                                        // The file already exists, ask to create a new one
150                                        var message = _("The file <<%s>> already exists, do you want to create a new one?").printf (new_file.get_path ());
151                                        var response = _view.show_yes_no_message (message, true);
152       
153                                        if (response == DialogResponse.CANCEL)
154                                        {
155                                                dialog_view.close_dialog ();
156                                                return;
157                                        }
158       
159                                        if (response == DialogResponse.NO)
160                                                overwrite = false;
161                                }
162                                if (overwrite)
163                                        new_file.replace_contents ("", 0, null, false, FileCreateFlags.NONE, null, null);
164                                file = dialog_view.filename;
165                        }
166                        else
167                        {
168                                // Import a file
169                                var import_file = File.new_for_uri (dialog_view.filename);
170                                var file_info = import_file.query_info (FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, FileQueryInfoFlags.NONE, null);
171                                var import_filename = file_info.get_display_name ();
172                                if (current_project.folder_uri != import_file.get_parent ().get_uri ())
173                                {
174                                        // The file is in another folder, ask to copy the file
175                                        var message = _("The file <<%s>> is outside the project's folder, do you want to copy the file?").printf (import_file.get_path ());
176                                        var response = _view.show_yes_no_message (message, false);
177                                       
178                                        if (response == DialogResponse.YES)
179                                        {
180                                                // The file already exists, ask to overwrite the file
181                                                var dest_file = File.new_for_uri (current_project.get_file_uri (import_filename));
182                                                message = _("Do you want to overwrite the file <<%s>>?").printf (dest_file.get_path ());
183                                                if (dest_file.query_exists (null) && _view.show_yes_no_message (message, false) == DialogResponse.NO)
184                                                {
185                                                        dialog_view.close_dialog ();
186                                                        return;
187                                                }
188                                                import_file.copy (dest_file, FileCopyFlags.OVERWRITE, null, null);
189                                        }
190                                }
191                                file = import_filename;
192                        }
193                       
194                        // Add the file to the project and to the selected profiles and open the file
195                        if (!(file in current_project.files))
196                                current_project.files.add (file);
197                        foreach (var profile in dialog_view.activated_profiles)
198                                if (!(file in current_project.profiles[profile].files))
199                                        current_project.profiles[profile].files.add (file);
200                        current_project.save ();
201                        _page_logic.update_files_list ();
202                        _documents_logic.open_document (current_project.get_file_uri (file));
203                }
204                dialog_view.close_dialog ();
205        }
206
207        private void on_remove_file_from_project_clicked ()
208        {
209                var current_project = _page_logic.current_project;
210                var current_file = _view.page_view.current_file;
211                var message = _("Do you want to remove the file <<%s>> from %s project?").printf (current_file, current_project.name);
212                if (_view.show_yes_no_message (message, false) != DialogResponse.YES)
213                        return;
214                foreach (var profile in current_project.profiles.values)
215                        if (current_file in profile.files)
216                                profile.files.remove (current_file);
217                current_project.files.remove (current_file);
218                current_project.save ();
219                _page_logic.update_files_list ();
220        }
221
222        private void on_remove_file_from_profile_clicked ()
223        {
224                var current_project = _page_logic.current_project;
225                var current_profile = _view.page_view.current_profile;
226                var current_file = _view.page_view.current_file;
227                var message = _("Do you want to remove the file <<%s>> from %s profile?").printf (current_file, current_profile);
228                if (_view.show_yes_no_message (message, false) != DialogResponse.YES)
229                        return;
230                current_project.profiles[current_profile].files.remove (current_file);
231                current_project.save ();
232                _page_logic.update_files_list ();
233        }
234
235        private void on_add_profile_clicked ()
236        {
237                var dialog_view = _view.create_add_profile_dialog_view ();
238                var current_project = _page_logic.current_project;
239                var dialog_logic = new AddProfileDialogLogic (dialog_view, current_project);
240                if (dialog_logic.run ())
241                {
242                        var profile = new Profile ();
243                        profile.name = dialog_view.profile_name;
244                        profile.is_public = dialog_view.is_public;
245                       
246                        if (!dialog_view.is_new)
247                        {
248                                var import_project = new Project ();
249                                try
250                                {
251                                        import_project.open (dialog_view.project_uri);
252                                }
253                                catch (GLib.Error e)
254                                {
255                                        _view.show_error_message (_("Error trying to open %s: %s\n").printf (dialog_view.project_uri, e.message));
256                                        dialog_view.close_dialog ();
257                                        return;
258                                }
259                                var import_profile = import_project.profiles[dialog_view.profile_name];
260                                if (current_project.folder_uri != import_project.folder_uri)
261                                {
262                                        // The projects are in diferent folders, ask to copy the files
263                                        var message = _("The files are outside the project's folder, do you want to copy the files?");
264                                        if (_view.show_yes_no_message (message, false) == DialogResponse.NO)
265                                        {
266                                                dialog_view.close_dialog ();
267                                                return;
268                                        }
269                                       
270                                        // If some of the files are in the project's folder, ask to overwrite the file
271                                        foreach (var file in import_profile.files)
272                                        {
273                                                var orig_file = File.new_for_uri (import_project.get_file_uri (file));
274                                                var dest_file = File.new_for_uri (current_project.get_file_uri (file));
275                                                message = _("Do you want to overwrite the file <<%s>>?").printf (dest_file.get_path ());
276                                                if (dest_file.query_exists (null) && _view.show_yes_no_message (message, false) == DialogResponse.NO)
277                                                        continue;
278                                                orig_file.copy (dest_file, FileCopyFlags.OVERWRITE, null, null);
279                                                current_project.files.add (file);
280                                                profile.files.add (file);
281                                        }
282                                }
283                                else
284                                {
285                                        // The projects are in the same folder, just add them
286                                        foreach (var file in import_profile.files)
287                                        {
288                                                current_project.files.add (file);
289                                                profile.files.add (file);
290                                        }
291                                }
292                               
293                                // Copy build options
294                                profile.builder_id = import_profile.builder_id;
295                                profile.device_type = import_profile.device_type;
296                                profile.device = import_profile.device;
297                                profile.link_options = import_profile.link_options;
298                                profile.compile_options.set_all (import_profile.compile_options);
299                        }
300                       
301                        // Add the new profile, save the project and update de profiles list
302                        current_project.profiles[dialog_view.profile_name] = profile;
303                        current_project.save ();
304                        _page_logic.update_profiles_list ();
305                }
306                dialog_view.close_dialog ();
307        }
308       
309        private void on_remove_profile_clicked ()
310        {
311                var current_project = _page_logic.current_project;
312                var current_profile = _view.page_view.current_profile;
313                var message = _("Do you want to remove the profile <<%s>> from %s project?").printf (current_profile, current_project.name);
314                if (_view.show_yes_no_message (message, false) != DialogResponse.YES)
315                        return;
316                current_project.profiles.unset (current_profile);
317                current_project.save ();
318                _page_logic.update_profiles_list ();
319        }
320       
321        private void on_build_profile_clicked ()
322        {
323                var current_project = _page_logic.current_project;
324                var current_profile = current_project.profiles[_view.page_view.current_profile];
325                var message = _("The %s of the profile isn't selected");
326               
327                if (current_profile.builder_id == "")
328                {
329                        _view.show_error_message (message.printf (_("builder")));
330                        return;
331                }
332                if (current_profile.device_type == "")
333                {
334                        _view.show_error_message (message.printf (_("device type")));
335                        return;
336                }
337                if (current_profile.device == "")
338                {
339                        _view.show_error_message (message.printf (_("device")));
340                        return;
341                }
342               
343                //Save profile's files before building
344                var files_to_save = new ArrayList<string> ();
345                foreach (var file in current_profile.files)
346                        files_to_save.add (current_project.get_file_uri (file));
347                _documents_logic.save_documents (files_to_save);
348               
349                _profile_builder.project = current_project;
350                _profile_builder.profile = current_profile.name;
351                _view.show_build_log_page ();
352               
353                try
354                {
355                        _profile_builder.build ();
356                }
357                catch (GLib.Error e)
358                {
359                        _view.show_error_message (_("Error trying to build profile %s: %s").printf (current_profile.name, e.message));
360                }
361        }
362       
363        private void on_program_profile_clicked ()
364        {
365                var current_project = _page_logic.current_project;
366                var current_profile = current_project.profiles[_view.page_view.current_profile];
367                var hexfile = current_project.name + "-" + current_profile.name + ".hex";
368                var hexfile_uri = current_project.get_file_uri (hexfile.replace (" ", "_"));
369                var hexfile_file = File.new_for_uri (hexfile_uri);
370               
371                if (!hexfile_file.query_exists (null))
372                {
373                        _view.show_error_message (_("The file %s doesn't exists").printf (hexfile_file.get_path ()));
374                        return;
375                }
376                _programmers_logic.write_flash (hexfile_uri);
377        }
378       
379        private void on_clean_profile_clicked ()
380        {
381                var current_project = _page_logic.current_project;
382                var current_profile = current_project.profiles[_view.page_view.current_profile];
383               
384                if (current_profile.builder_id == "")
385                        return;
386               
387                _profile_builder.project = current_project;
388                _profile_builder.profile = current_profile.name;
389               
390                try
391                {
392                        _profile_builder.clean ();
393                }
394                catch (GLib.Error e)
395                {
396                        _view.show_error_message (_("Error trying to clean profile %s: %s").printf (current_profile.name, e.message));
397                }
398        }
399       
400        private void on_project_changed ()
401        {
402                var project_selected = _page_logic.current_project != null;
403                _view.add_file_sensitive = project_selected;
404                _view.add_profile_sensitive = project_selected;
405                _view.close_sensitive = project_selected;
406        }
407
408        private void on_profile_changed ()
409        {
410                var profile_selected = _view.page_view.current_profile != null &&
411                                       _view.page_view.current_profile != "__NONE__";
412                var file_selected = _view.page_view.current_file != null;
413                _view.remove_file_from_profile_sensitive = profile_selected && file_selected;
414                _view.edit_profile_sensitive = profile_selected;
415                _view.page_view.profile_edit_sensitive = profile_selected;
416                _view.duplicate_profile_sensitive = profile_selected;
417                _view.remove_profile_sensitive = profile_selected;
418                _view.build_profile_sensitive = profile_selected && !_profile_builder.is_building;
419                _view.clean_profile_sensitive = profile_selected && !_profile_builder.is_building;
420                _view.program_profile_sensitive = profile_selected && !_profile_builder.is_building && _programmers_logic.can_write_flash;
421        }
422
423        private void on_file_changed ()
424        {
425                var profile_selected = _view.page_view.current_profile != null &&
426                                       _view.page_view.current_profile != "__NONE__";
427                var file_selected = _view.page_view.current_file != null;
428                _view.remove_file_from_project_sensitive = file_selected;
429                _view.remove_file_from_profile_sensitive = profile_selected && file_selected;
430        }
431       
432        private void on_command_activated (string output, CommandContext command_context)
433        {
434                if (command_context.output_regex == null ||
435                    command_context.output_regex == "")
436                        return;
437               
438                try
439                {
440                        var regex = new Regex (command_context.output_regex);
441                       
442                        MatchInfo match_info;
443                        if (!regex.match (output, 0, out match_info))
444                                return;
445                       
446                        var line = match_info.fetch_named ("line");
447                        _documents_logic.open_document (command_context.source_uri, line.to_int ());
448                }
449                catch (Error e)
450                {
451                        warning (e.message);
452                }
453        }
454       
455        private void on_profile_builder_is_building ()
456        {
457                var is_building = _profile_builder.is_building;
458                _view.build_profile_sensitive = !is_building;
459                _view.stop_sensitive = is_building;
460                _view.program_profile_sensitive = !is_building;
461        }
462       
463        private void on_programmers_logic_can_write_flash_changed ()
464        {
465                var profile_selected = _view.page_view.current_profile != null &&
466                                       _view.page_view.current_profile != "__NONE__";
467                _view.program_profile_sensitive = profile_selected && !_profile_builder.is_building && _programmers_logic.can_write_flash;
468        }
469}
Note: See TracBrowser for help on using the repository browser.