source: libi4uccore/programmerslogic.vala @ 47b701a6646050bc639a18969b1ada4845b6e1c3

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

Rework to use ViewFactory? and App

  • Property mode set to 100644
Line 
1/* programmerslogic.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.Core.ProgrammersLogic : GLib.Object
23{
24        private App _app;
25        private ProgrammersView _view;
26        private DocumentsLogic _documents_logic;
27        private Programmer _current_programmer;
28        private Command _command = create_command ();
29        private SerialPortUtils _serial_port_utils = create_serial_port_utils ();
30        private string _read_firmware;
31       
32        public bool can_write_flash { private set; get; }
33       
34        public ProgrammersLogic (App app)
35        {
36                _app = app;
37                _view = _app.programmers_view;
38                _documents_logic = _app.documents_logic;
39               
40                //Configure view
41                _view.page.tab_title = _("Programmers");
42                _view.log_page.tab_title = _("Programmer's log");
43               
44                //Load available serial ports
45                on_update_serial_port_clicked ();
46               
47                //Load baud rates
48                var baud_rates = _serial_port_utils.list_baud_rates ();
49                foreach (var baud_rate in baud_rates)
50                        _view.page.add_baud_rate (baud_rate);
51                if (_app.settings.baud_rate in baud_rates)
52                        _view.page.current_baud_rate = _app.settings.baud_rate;
53               
54                //Load programmers
55                foreach (var programmer in _app.settings.programmers.values)
56                        _view.page.add_programmer (programmer.id);
57                if (_app.settings.programmer in _app.settings.programmers.keys)
58                        _view.page.current_programmer = _app.settings.programmer;
59                on_programmer_changed ();
60               
61                //Connect signals
62                _view.pages_panel.page_changed.connect (on_page_changed);
63                _view.page.notify["current-programmer"].connect (on_programmer_changed);
64                _view.page.notify["current-serial-port"].connect (on_serial_port_changed);
65                _view.page.update_serial_port_clicked.connect (on_update_serial_port_clicked);
66                _view.page.notify["current-baud-rate"].connect (on_baud_rate_changed);
67                _view.page.notify["current-devices-file"].connect (on_devices_file_changed);
68                _view.page.notify["current-devices-folder"].connect (on_devices_folder_changed);
69                _view.page.notify["current-clock"].connect (on_clock_changed);
70                _view.page.notify["current-device"].connect (on_device_changed);
71                _view.page.notify["current-fuses"].connect (on_fuses_changed);
72                _view.write_flash_clicked.connect (on_write_flash_clicked);
73                _view.read_flash_clicked.connect (on_read_flash_clicked);
74                _view.verify_flash_clicked.connect (() => run_command (_current_programmer.verify_flash_command));
75                _view.erase_flash_clicked.connect (on_erase_flash_clicked);
76                _view.write_fuses_clicked.connect (on_write_fuses_clicked);
77                _view.read_fuses_clicked.connect (() => run_command (_current_programmer.read_fuses_command));
78                _view.get_version_clicked.connect (() => run_command (_current_programmer.get_version_command));
79                _view.stop_clicked.connect (() => _command.stop ());
80                _command.finished.connect (on_command_finished);
81        }
82       
83        public void write_flash (string uri)
84        {
85                if (_app.view_factory.show_yes_no_message (_("Are you sure you want to write the flash memory?")) == DialogResponse.NO)
86                        return;
87               
88                var filtered_command = filter_command (_current_programmer.write_flash_command);
89               
90                var firmware = File.new_for_uri (uri).get_path ();
91                filtered_command = filtered_command.replace ("%firmware", Shell.quote (firmware));
92               
93                try
94                {
95                        _view.log_page.add_message ("<b>" + _("Running:") + " </b><i>" + filtered_command + "</i>");
96                        _command.run (filtered_command);
97                        set_parameters_sensitive (false);
98                        disable_actions ();
99                        _view.stop_sensitive = true;
100                }
101                catch (Error e)
102                {
103                        _view.log_page.add_message (e.message);
104                }
105               
106                _view.show_log_page ();
107        }
108       
109        private void on_page_changed (Page? page)
110        {
111                disable_actions ();
112               
113                if (_view.page.current_programmer == null)
114                        return;
115               
116                enable_actions ();
117        }
118       
119        private void on_programmer_changed ()
120        {
121                hide_parameters ();
122                disable_actions ();
123               
124                if (_view.page.current_programmer == null)
125                {
126                        _current_programmer = null;
127                        return;
128                }
129               
130                _current_programmer = _app.settings.programmers[_view.page.current_programmer];
131               
132                if (_app.settings.programmer != _current_programmer.id)
133                        _app.settings.programmer = _current_programmer.id;
134               
135                var working_folder = _app.settings.working_folder;
136               
137                //Load devices file if the programmer has devices file
138                var has_devices_file_key = _app.settings.devices_files.has_key (_current_programmer.id);
139                if (_current_programmer.has_devices_file)
140                        _view.page.current_devices_file = has_devices_file_key ? _app.settings.devices_files[_current_programmer.id] : working_folder;
141               
142                //Load devices folder if the programmer has devices folder
143                var has_devices_folder_key = _app.settings.devices_folders.has_key (_current_programmer.id);
144                if (_current_programmer.has_devices_folder)
145                        _view.page.current_devices_folder = has_devices_folder_key ? _app.settings.devices_folders[_current_programmer.id] : working_folder;
146               
147                //Load clock if the programmer has clock
148                var has_clock_key = _app.settings.clocks.has_key (_current_programmer.id);
149                if (_current_programmer.has_clock)
150                        _view.page.current_clock = has_clock_key ? _app.settings.clocks[_current_programmer.id] : "";
151               
152                //Load devices list
153                _view.page.clear_devices_list ();
154                foreach (var device in _current_programmer.devices)
155                        _view.page.add_device (device);
156                var has_device_key = _app.settings.devices.has_key (_current_programmer.id);
157                if (has_device_key)
158                        _view.page.current_device = _app.settings.devices[_current_programmer.id];
159               
160                //Load fuses if the programmer can write fuses
161                var can_write_fuses_key = _app.settings.fuses.has_key (_current_programmer.id);
162                if (_current_programmer.can_write_fuses)
163                        _view.page.current_fuses = can_write_fuses_key ? _app.settings.fuses[_current_programmer.id] : "";
164               
165                _view.page.serial_port_visible = _current_programmer.has_serial_port;
166                _view.page.baud_rate_visible = _current_programmer.has_baud_rate;
167                _view.page.devices_file_visible = _current_programmer.has_devices_file;
168                _view.page.devices_folder_visible = _current_programmer.has_devices_folder;
169                _view.page.clock_visible = _current_programmer.has_clock;
170                _view.page.device_visible = true;
171                _view.page.fuses_visible = _current_programmer.can_write_fuses;
172                enable_actions ();
173        }
174       
175        private void on_serial_port_changed ()
176        {
177                disable_actions ();
178               
179                if (_current_programmer == null)
180                        return;
181               
182                var current_serial_port = _view.page.current_serial_port;
183               
184                if (current_serial_port != null && _app.settings.serial_port != current_serial_port)
185                        _app.settings.serial_port = current_serial_port;
186               
187                enable_actions ();
188        }
189       
190        private void on_update_serial_port_clicked ()
191        {
192                _view.page.clear_serial_ports_list ();
193                var serial_ports = _serial_port_utils.list_serial_ports ();
194                foreach (var serial_port in serial_ports)
195                        _view.page.add_serial_port (serial_port);
196                if (_app.settings.serial_port in serial_ports)
197                        _view.page.current_serial_port = _app.settings.serial_port;
198        }
199       
200        private void on_baud_rate_changed ()
201        {
202                disable_actions ();
203               
204                if (_current_programmer == null)
205                        return;
206               
207                var current_baud_rate = _view.page.current_baud_rate;
208               
209                if (current_baud_rate != null && _app.settings.baud_rate != current_baud_rate)
210                        _app.settings.baud_rate = current_baud_rate;
211               
212                enable_actions ();
213        }
214       
215        private void on_devices_file_changed ()
216        {
217                disable_actions ();
218               
219                if (_current_programmer == null)
220                        return;
221               
222                var current_devices_file = _view.page.current_devices_file;
223               
224                if (_app.settings.devices_files[_current_programmer.id] != current_devices_file)
225                        _app.settings.devices_files[_current_programmer.id] = current_devices_file;
226               
227                enable_actions ();
228        }
229       
230        private void on_devices_folder_changed ()
231        {
232                disable_actions ();
233               
234                if (_current_programmer == null)
235                        return;
236               
237                var current_devices_folder = _view.page.current_devices_folder;
238               
239                if (_app.settings.devices_folders[_current_programmer.id] != current_devices_folder)
240                        _app.settings.devices_folders[_current_programmer.id] = current_devices_folder;
241               
242                enable_actions ();
243        }
244       
245        private void on_clock_changed ()
246        {
247                disable_actions ();
248               
249                if (_current_programmer == null)
250                        return;
251               
252                var current_clock = _view.page.current_clock;
253               
254                if (_app.settings.clocks[_current_programmer.id] != current_clock)
255                        _app.settings.clocks[_current_programmer.id] = current_clock;
256               
257                enable_actions ();
258        }
259       
260        private void on_device_changed ()
261        {
262                disable_actions ();
263               
264                if (_current_programmer == null)
265                        return;
266               
267                var current_device = _view.page.current_device;
268               
269                if (current_device != null && _app.settings.devices[_current_programmer.id] != current_device)
270                        _app.settings.devices[_current_programmer.id] = current_device;
271               
272                enable_actions ();
273        }
274       
275        private void on_fuses_changed ()
276        {
277                disable_actions ();
278               
279                if (_current_programmer == null)
280                        return;
281               
282                var current_fuses = _view.page.current_fuses;
283               
284                if (_app.settings.fuses[_current_programmer.id] != current_fuses)
285                        _app.settings.fuses[_current_programmer.id] = current_fuses;
286               
287                enable_actions ();
288        }
289       
290        private void on_write_flash_clicked ()
291        {
292                if (_app.view_factory.show_yes_no_message (_("Are you sure you want to write the flash memory?")) == DialogResponse.YES)
293                        run_command (_current_programmer.write_flash_command);
294        }
295       
296        private void on_read_flash_clicked ()
297        {
298                string folder_uri = _app.settings.working_folder;
299                string firmware_uri;
300                var response = _app.view_factory.show_save_dialog (_("New firmware"), ref folder_uri, out firmware_uri);
301                if (response == DialogResponse.OK)
302                {
303                        _app.settings.working_folder = folder_uri;
304                        _read_firmware = firmware_uri;
305                        run_command (_current_programmer.read_flash_command);
306                }
307        }
308       
309        private void on_erase_flash_clicked ()
310        {
311                if (_app.view_factory.show_yes_no_message (_("Are you sure you want to erase the flash memory?")) == DialogResponse.YES)
312                        run_command (_current_programmer.erase_flash_command);
313        }
314       
315        private void on_write_fuses_clicked ()
316        {
317                if (_app.view_factory.show_yes_no_message (_("Are you sure you want to write the fuses?")) == DialogResponse.YES)
318                        run_command (_current_programmer.write_fuses_command);
319        }
320       
321        private void on_command_finished (bool exited_ok, string standard_output, string standard_error)
322        {
323                // Show programmer's output, adding a tab at the beginning of each line
324                _view.log_page.add_message ("\t" + standard_output.replace ("\n", "\n\t") +
325                                            "\t" + standard_error.replace ("\n", "\n\t"));
326               
327                // Show exit status message
328                var exit_message = exited_ok ? _("Command finished ok") : _("Command finished with errors");
329                _view.log_page.add_message ("<b>" + exit_message + "</b>");
330               
331                // Open the hex file if read flash was selected
332                if (_read_firmware != null)
333                {
334                        _documents_logic.open_document (_read_firmware);
335                        _read_firmware = null;
336                }
337               
338                // Restore actions sensitive
339                set_parameters_sensitive (true);
340                enable_actions ();
341        }
342       
343        private void run_command (string command)
344        {
345                var filtered_command = filter_command (command);
346               
347                if (_read_firmware == null)
348                {
349                        if (_view.pages_panel.current_page != null &&
350                            _current_programmer.is_valid_firmware_type (_view.pages_panel.current_page.tab_title))
351                        {
352                                var firmware = File.new_for_uri (_view.pages_panel.current_page.title).get_path ();
353                                filtered_command = filtered_command.replace ("%firmware", Shell.quote (firmware));
354                        }
355                }
356                else
357                {
358                        var firmware = File.new_for_uri (_read_firmware).get_path ();
359                        filtered_command = filtered_command.replace ("%firmware", Shell.quote (firmware));
360                }
361               
362                try
363                {
364                        _view.log_page.add_message ("<b>" + _("Running:") + " </b><i>" + filtered_command + "</i>");
365                        _command.run (filtered_command);
366                        set_parameters_sensitive (false);
367                        disable_actions ();
368                        _view.stop_sensitive = true;
369                }
370                catch (Error e)
371                {
372                        _view.log_page.add_message (e.message);
373                }
374               
375                _view.show_log_page ();
376        }
377       
378        private void hide_parameters ()
379        {
380                _view.page.serial_port_visible = false;
381                _view.page.baud_rate_visible = false;
382                _view.page.devices_file_visible = false;
383                _view.page.devices_folder_visible = false;
384                _view.page.clock_visible = false;
385                _view.page.device_visible = false;
386                _view.page.fuses_visible = false;
387        }
388       
389        private void set_parameters_sensitive (bool sensitive)
390        {
391                _view.page.programmer_sensitive = sensitive;
392                _view.page.serial_port_sensitive = sensitive;
393                _view.page.baud_rate_sensitive = sensitive;
394                _view.page.devices_file_sensitive = sensitive;
395                _view.page.devices_folder_sensitive = sensitive;
396                _view.page.clock_sensitive = sensitive;
397                _view.page.device_sensitive = sensitive;
398                _view.page.fuses_sensitive = sensitive;
399        }
400       
401        private void enable_actions ()
402        {
403                var valid_page = _view.pages_panel.current_page != null &&
404                                 _current_programmer.is_valid_firmware_type (_view.pages_panel.current_page.tab_title);
405                var valid_serial_port =  !_current_programmer.has_serial_port || _view.page.current_serial_port != null;
406                var valid_baud_rate = !_current_programmer.has_baud_rate || _view.page.current_baud_rate != null;
407                var valid_devices_file = !_current_programmer.has_devices_file || _view.page.current_devices_file != null;
408                var valid_devices_folder =  !_current_programmer.has_devices_folder || _view.page.current_devices_folder != null;
409                var valid_clock =  !_current_programmer.has_clock || _view.page.current_clock != "";
410                var valid_device = _view.page.current_device != null;
411                var valid_fuses = !_current_programmer.can_write_fuses || _view.page.current_fuses != "";
412               
413                if (!valid_serial_port ||
414                    !valid_baud_rate ||
415                    !valid_devices_file ||
416                    !valid_devices_folder ||
417                    !valid_clock ||
418                    !valid_device)
419                    return;
420               
421                this.can_write_flash = _current_programmer.can_write_flash;
422                _view.write_flash_sensitive = valid_page && _current_programmer.can_write_flash;
423                _view.read_flash_sensitive = _current_programmer.can_read_flash;
424                _view.verify_flash_sensitive = valid_page && _current_programmer.can_verify_flash;
425                _view.erase_flash_sensitive = _current_programmer.can_erase_flash;
426                _view.write_fuses_sensitive = valid_fuses && _current_programmer.can_write_fuses;
427                _view.read_fuses_sensitive = _current_programmer.can_read_fuses;
428                _view.get_version_sensitive = _current_programmer.can_get_version;
429                _view.stop_sensitive = _command.is_running;
430        }
431       
432        private void disable_actions ()
433        {
434                this.can_write_flash = false;
435                _view.write_flash_sensitive = false;
436                _view.read_flash_sensitive = false;
437                _view.verify_flash_sensitive = false;
438                _view.erase_flash_sensitive = false;
439                _view.write_fuses_sensitive = false;
440                _view.read_fuses_sensitive = false;
441                _view.get_version_sensitive = false;
442                _view.stop_sensitive = false;
443        }
444       
445        private string filter_command (string command)
446        {
447                var filtered_command = command;
448               
449                if (_current_programmer.has_serial_port)
450                        filtered_command = filtered_command.replace ("%serial_port", _view.page.current_serial_port);
451                if (_current_programmer.has_baud_rate)
452                        filtered_command = filtered_command.replace ("%baud_rate", _view.page.current_baud_rate);
453                if (_current_programmer.has_devices_file)
454                {
455                        var devices_file = File.new_for_uri (_view.page.current_devices_file).get_path ();
456                        filtered_command = filtered_command.replace ("%devices_file", Shell.quote (devices_file));
457                }
458                if (_current_programmer.has_devices_folder)
459                {
460                        var devices_folder = File.new_for_uri (_view.page.current_devices_folder).get_path ();
461                        filtered_command = filtered_command.replace ("%devices_folder",  Shell.quote (devices_folder));
462                }
463                if (_current_programmer.has_clock)
464                        filtered_command = filtered_command.replace ("%clock", _view.page.current_clock);
465               
466                filtered_command = filtered_command.replace ("%device", _view.page.current_device);
467               
468                if (_current_programmer.can_write_fuses)
469                        filtered_command = filtered_command.replace ("%fuses", _view.page.current_fuses);
470               
471                return filtered_command;
472        }
473}
Note: See TracBrowser for help on using the repository browser.