source: libi4uc/i4ucaddprofiledialogpresenter.vala @ 4b2dccae29686a8f5267e527921cfb42df0148e1

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

Projects: Add 'add profile' action

  • Property mode set to 100644
Line 
1/* i4ucaddprofiledialogpresenter.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 */
21
22public class I4uc.AddProfileDialogPresenter : GLib.Object
23{
24        private AddProfileDialogViewIface _view;
25        private Project _project;
26        private string _last_project;
27
28        public AddProfileDialogPresenter (AddProfileDialogViewIface view, Project project)
29        {
30                _view = view;
31                _project = project;
32
33                //Configure view
34                _view.warning_message_visible = false;
35                _view.import_project_sensitive = false;
36                _view.import_profile_sensitive = false;
37                _view.accept_sensitive = false;
38                _view.folder_uri = _project.folder_uri;
39
40                //Connect view signals
41                _view.is_new_changed.connect (on_is_new_changed);
42                _view.new_profile_changed.connect (on_new_profile_changed);
43                _view.import_project_changed.connect (on_import_project_changed);
44                _view.import_profile_changed.connect (on_import_profile_changed);
45        }
46
47        public bool run ()
48        {
49                var response = _view.run ();
50                var is_new = _view.is_new;
51                var project_uri = _view.project_uri;
52                var profile_name = _view.profile_name;
53                var profile = new ProjectProfile ();
54                profile.name = profile_name;
55                _view.close_dialog ();
56
57                if (response != Gtk.ResponseType.OK)
58                        return false;
59
60                if (!is_new)
61                {
62                        var import_project = new Project ();
63                        import_project.open (project_uri);
64                        if (_project.folder_uri != import_project.folder_uri)
65                        {
66                                var message = _("The files are outside the project's folder, do you want to copy the files?");
67                                if (_view.show_yes_no_message (message, false) == Gtk.ResponseType.NO)
68                                        return false;
69
70                                var file_in_project = false;
71                                foreach (var file in import_project.profiles[profile_name].files)
72                                        if (file in _project.files)
73                                                file_in_project = true;
74                                if (file_in_project)
75                                {
76                                        _view.show_error_message (_("One of the files to copy already exists in the project"));
77                                        return false;
78                                }
79
80                                foreach (var file in import_project.profiles[profile_name].files)
81                                {
82                                        var orig_file = File.new_for_uri (import_project.get_file_uri (file));
83                                        var dest_file = File.new_for_uri (_project.get_file_uri (file));
84                                        message = _("Do you want to overwrite the file <<%s>>?").printf (dest_file.get_path ());
85                                        if (dest_file.query_exists (null) && _view.show_yes_no_message (message, false) == Gtk.ResponseType.NO)
86                                                continue;
87                                        orig_file.copy (dest_file, FileCopyFlags.OVERWRITE, null, null);
88                                        _project.files.add (file);
89                                        profile.files.add (file);
90                                }
91                        }
92                        else
93                        {
94                                foreach (var file in import_project.profiles[profile_name].files)
95                                {
96                                        _project.files.add (file);
97                                        profile.files.add (file);
98                                }
99                        }
100                }
101                _project.profiles[profile_name] = profile;
102                _project.save ();
103                return true;
104        }
105
106        private void on_is_new_changed ()
107        {
108                var is_new = _view.is_new;
109                _view.new_profile_sensitive = is_new;
110                _view.import_project_sensitive = !is_new;
111                _view.import_profile_sensitive = !is_new;
112                if (is_new)
113                        on_new_profile_changed ();
114                else
115                {
116                        on_import_project_changed ();
117                        on_import_profile_changed ();
118                }
119        }
120
121        private void on_new_profile_changed ()
122        {
123                var profile_name = _view.profile_name;
124                _view.accept_sensitive = !(profile_name == "" ||
125                                           profile_name.has_prefix (".") ||
126                                           profile_name in _project.profiles.keys);
127                if (profile_name in _project.profiles.keys)
128                        _view.warning_message = _("The profile already exists in the project");
129                _view.warning_message_visible = profile_name in _project.profiles.keys;
130        }
131
132        private void on_import_project_changed ()
133        {
134                _view.import_profile_sensitive = false;
135                _view.accept_sensitive = false;
136                if (_view.project_uri == "" || _view.project_uri == null)
137                        return;
138                if (_view.project_uri == _project.uri)
139                {
140                        _view.warning_message = _("Can't choose the same project");
141                        _view.warning_message_visible = true;
142                        return;
143                }
144
145                if (_view.project_uri != _last_project)
146                {
147                        var import_project = new Project ();
148                        if (!import_project.open (_view.project_uri))
149                        {
150                                _view.warning_message = _("Can't open the project");
151                                _view.warning_message_visible = true;
152                                return;
153                        }
154       
155                        _view.clear_profiles_list ();
156                        _view.add_profiles (import_project.profiles.keys);
157                        _last_project = _view.project_uri;
158                }
159
160                _view.warning_message_visible = false;
161                _view.import_profile_sensitive = true;
162                _view.accept_sensitive = true;
163        }
164
165        private void on_import_profile_changed ()
166        {
167                _view.accept_sensitive = false;
168                if (_view.profile_name == "" || _view.profile_name == null)
169                        return;
170                if (_view.profile_name in _project.profiles.keys)
171                {
172                        _view.warning_message = _("There's already a profile with that name");
173                        _view.warning_message_visible = true;
174                        return;
175                }
176
177                _view.warning_message_visible = false;
178                _view.accept_sensitive = true;
179        }
180}
Note: See TracBrowser for help on using the repository browser.