| 1 | /* main.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 | using Gtk; |
|---|
| 22 | using I4uc.Core; |
|---|
| 23 | using I4uc.GtkFrontend; |
|---|
| 24 | |
|---|
| 25 | //Constants |
|---|
| 26 | const double _PULSE_STEP = 0.25; |
|---|
| 27 | const OptionEntry[] _OPTIONS = { |
|---|
| 28 | { "version", 0, 0, OptionArg.NONE, ref _option_version, N_("Display version number"), null }, |
|---|
| 29 | { "", 0, 0, OptionArg.FILENAME_ARRAY, ref _option_files, null, N_("[FILE...]") }, |
|---|
| 30 | { null } |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | //Command line options |
|---|
| 34 | bool _option_version; |
|---|
| 35 | [CCode (array_length = false, array_null_terminated = true)] |
|---|
| 36 | string[] _option_files; |
|---|
| 37 | |
|---|
| 38 | //Variables for splash screen |
|---|
| 39 | SplashScreen _splash_screen; |
|---|
| 40 | |
|---|
| 41 | //Views and presenters |
|---|
| 42 | I4uc.GtkFrontend.MainWindowView _main_window_view; |
|---|
| 43 | I4uc.GtkFrontend.DocumentsView _documents_view; |
|---|
| 44 | I4uc.GtkFrontend.ProjectsView _projects_view; |
|---|
| 45 | I4uc.GtkFrontend.ProgrammersView _programmers_view; |
|---|
| 46 | MainWindowLogic _main_window_logic; |
|---|
| 47 | DocumentsLogic _documents_logic; |
|---|
| 48 | ProjectsLogic _projects_logic; |
|---|
| 49 | ProgrammersLogic _programmers_logic; |
|---|
| 50 | |
|---|
| 51 | int main (string[] args) |
|---|
| 52 | { |
|---|
| 53 | //I18n |
|---|
| 54 | Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALE_DIR); |
|---|
| 55 | Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8"); |
|---|
| 56 | |
|---|
| 57 | //Command line options |
|---|
| 58 | try |
|---|
| 59 | { |
|---|
| 60 | var context = new OptionContext (_("- IDE for microcontrollers")); |
|---|
| 61 | context.set_help_enabled (true); |
|---|
| 62 | context.add_main_entries (_OPTIONS, Config.GETTEXT_PACKAGE); |
|---|
| 63 | context.add_group (get_option_group (false)); |
|---|
| 64 | context.parse (ref args); |
|---|
| 65 | } |
|---|
| 66 | catch (OptionError e) |
|---|
| 67 | { |
|---|
| 68 | stderr.printf (e.message + "\n"); |
|---|
| 69 | stderr.printf (_("Run '%s --help' to see a full list of available command line options.\n"), args[0]); |
|---|
| 70 | return 1; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | if (_option_version) |
|---|
| 74 | { |
|---|
| 75 | print ("i4uc %s\n", Config.VERSION); |
|---|
| 76 | return 0; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | Gtk.init (ref args); |
|---|
| 80 | |
|---|
| 81 | _splash_screen = new SplashScreen (); |
|---|
| 82 | _splash_screen.show_all (); |
|---|
| 83 | |
|---|
| 84 | start_loading.begin (); |
|---|
| 85 | |
|---|
| 86 | Gtk.main (); |
|---|
| 87 | |
|---|
| 88 | try |
|---|
| 89 | { |
|---|
| 90 | I4uc.Core.Settings.instance.save (); |
|---|
| 91 | } |
|---|
| 92 | catch (GLib.FileError e) |
|---|
| 93 | { |
|---|
| 94 | warning (e.message); |
|---|
| 95 | } |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | async void start_loading () |
|---|
| 100 | { |
|---|
| 101 | Idle.add (start_loading.callback); |
|---|
| 102 | yield; |
|---|
| 103 | try |
|---|
| 104 | { |
|---|
| 105 | //Load settings |
|---|
| 106 | _splash_screen.text = _("Loading settings..."); |
|---|
| 107 | load_settings (); |
|---|
| 108 | _splash_screen.fraction += _PULSE_STEP; |
|---|
| 109 | Idle.add (start_loading.callback); |
|---|
| 110 | yield; |
|---|
| 111 | |
|---|
| 112 | //Load builders |
|---|
| 113 | _splash_screen.text = _("Loading builders..."); |
|---|
| 114 | load_builders (); |
|---|
| 115 | _splash_screen.fraction += _PULSE_STEP; |
|---|
| 116 | Idle.add (start_loading.callback); |
|---|
| 117 | yield; |
|---|
| 118 | |
|---|
| 119 | //Load programmers |
|---|
| 120 | _splash_screen.text = _("Loading programmers..."); |
|---|
| 121 | load_programmers (); |
|---|
| 122 | _splash_screen.fraction += _PULSE_STEP; |
|---|
| 123 | Idle.add (start_loading.callback); |
|---|
| 124 | yield; |
|---|
| 125 | |
|---|
| 126 | //Load images |
|---|
| 127 | _splash_screen.text = _("Loading images..."); |
|---|
| 128 | load_stock_items (); |
|---|
| 129 | _splash_screen.fraction += _PULSE_STEP; |
|---|
| 130 | Idle.add (start_loading.callback); |
|---|
| 131 | yield; |
|---|
| 132 | |
|---|
| 133 | //Load user interface |
|---|
| 134 | load_gui (); |
|---|
| 135 | _splash_screen.hide (); |
|---|
| 136 | _splash_screen.destroy (); |
|---|
| 137 | _splash_screen = null; |
|---|
| 138 | Idle.add (start_loading.callback); |
|---|
| 139 | yield; |
|---|
| 140 | |
|---|
| 141 | //Open documents |
|---|
| 142 | foreach (var file in _option_files) |
|---|
| 143 | { |
|---|
| 144 | var uri = File.new_for_path (file).get_uri (); |
|---|
| 145 | _documents_logic.open_document (uri); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | catch (GLib.Error e) |
|---|
| 149 | { |
|---|
| 150 | warning (e.message); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void load_gui () |
|---|
| 155 | { |
|---|
| 156 | _main_window_view = new I4uc.GtkFrontend.MainWindowView (); |
|---|
| 157 | _main_window_view.show_all (); |
|---|
| 158 | |
|---|
| 159 | var ui_manager = _main_window_view.ui_manager; |
|---|
| 160 | var pages_panel = _main_window_view.pages_panel; |
|---|
| 161 | var side_panel = _main_window_view.side_panel; |
|---|
| 162 | var bottom_panel = _main_window_view.bottom_panel; |
|---|
| 163 | |
|---|
| 164 | _documents_view = new I4uc.GtkFrontend.DocumentsView (ui_manager, pages_panel, side_panel); |
|---|
| 165 | _projects_view = new I4uc.GtkFrontend.ProjectsView (ui_manager, side_panel, bottom_panel); |
|---|
| 166 | _programmers_view = new I4uc.GtkFrontend.ProgrammersView (ui_manager, pages_panel, side_panel, bottom_panel); |
|---|
| 167 | |
|---|
| 168 | _main_window_logic = new MainWindowLogic (_main_window_view); |
|---|
| 169 | _documents_logic = new DocumentsLogic (_documents_view); |
|---|
| 170 | _programmers_logic = new ProgrammersLogic (_programmers_view, _documents_logic); |
|---|
| 171 | _projects_logic = new ProjectsLogic (_projects_view, _documents_logic, _programmers_logic); |
|---|
| 172 | |
|---|
| 173 | _main_window_view.exit_clicked.connect (() => { |
|---|
| 174 | if (!_documents_logic.close_all ()) |
|---|
| 175 | return true; |
|---|
| 176 | _projects_logic.close_all_projects (); |
|---|
| 177 | return false; |
|---|
| 178 | }); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | void load_settings () throws GLib.Error, GLib.KeyFileError |
|---|
| 182 | { |
|---|
| 183 | var settings_folder = Path.build_filename (Environment.get_home_dir (), ".i4uc"); |
|---|
| 184 | if (!FileUtils.test (settings_folder, FileTest.EXISTS)) |
|---|
| 185 | DirUtils.create (settings_folder, 0755); |
|---|
| 186 | var settings_filename = Path.build_filename (settings_folder, "i4ucrc"); |
|---|
| 187 | I4uc.Core.Settings.instance.open (settings_filename); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void load_builders () throws GLib.Error, GLib.KeyFileError |
|---|
| 191 | { |
|---|
| 192 | //Load builders in $(pkgdatadir)/builders/ |
|---|
| 193 | var builders_dir = File.new_for_path (Config.BUILDERS_DIR); |
|---|
| 194 | |
|---|
| 195 | var enumerator = builders_dir.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, FileQueryInfoFlags.NONE, null); |
|---|
| 196 | FileInfo file_info; |
|---|
| 197 | while ((file_info = enumerator.next_file (null)) != null) |
|---|
| 198 | { |
|---|
| 199 | if (!file_info.get_name ().has_suffix (".i4ucbuilder")) |
|---|
| 200 | continue; |
|---|
| 201 | |
|---|
| 202 | var builder_file = builders_dir.get_child (file_info.get_name ()); |
|---|
| 203 | var builder = new I4uc.Core.Builder (); |
|---|
| 204 | try |
|---|
| 205 | { |
|---|
| 206 | builder.open (builder_file.get_uri ()); |
|---|
| 207 | I4uc.Core.Settings.instance.builders[builder.id] = builder; |
|---|
| 208 | } |
|---|
| 209 | catch (GLib.KeyFileError e) |
|---|
| 210 | { |
|---|
| 211 | warning (builder_file.get_path () + ": " + e.message); |
|---|
| 212 | continue; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | //check if $(HOME)/.i4uc/builders/ exists. If not, create it |
|---|
| 217 | builders_dir = File.new_for_path (Path.build_filename (Environment.get_home_dir (), ".i4uc", "builders")); |
|---|
| 218 | if (!builders_dir.query_exists (null)) |
|---|
| 219 | builders_dir.make_directory (null); |
|---|
| 220 | |
|---|
| 221 | //Load builders in $(HOME)/.i4uc/builders/ |
|---|
| 222 | enumerator = builders_dir.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, FileQueryInfoFlags.NONE, null); |
|---|
| 223 | while ((file_info = enumerator.next_file (null)) != null) |
|---|
| 224 | { |
|---|
| 225 | if (!file_info.get_name ().has_suffix (".i4ucbuilder")) |
|---|
| 226 | continue; |
|---|
| 227 | |
|---|
| 228 | var builder_file = builders_dir.get_child (file_info.get_name ()); |
|---|
| 229 | var builder = new I4uc.Core.Builder (); |
|---|
| 230 | try |
|---|
| 231 | { |
|---|
| 232 | builder.open (builder_file.get_uri ()); |
|---|
| 233 | I4uc.Core.Settings.instance.builders[builder.id] = builder; |
|---|
| 234 | } |
|---|
| 235 | catch (GLib.KeyFileError e) |
|---|
| 236 | { |
|---|
| 237 | warning (builder_file.get_path () + ": " + e.message); |
|---|
| 238 | continue; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | void load_programmers () throws GLib.Error, GLib.KeyFileError |
|---|
| 244 | { |
|---|
| 245 | //Load programmers in $(pkgdatadir)/programmers/ |
|---|
| 246 | var programmers_dir = File.new_for_path (Config.PROGRAMMERS_DIR); |
|---|
| 247 | |
|---|
| 248 | var enumerator = programmers_dir.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, FileQueryInfoFlags.NONE, null); |
|---|
| 249 | FileInfo file_info; |
|---|
| 250 | while ((file_info = enumerator.next_file (null)) != null) |
|---|
| 251 | { |
|---|
| 252 | if (!file_info.get_name ().has_suffix (".i4ucprogrammer")) |
|---|
| 253 | continue; |
|---|
| 254 | |
|---|
| 255 | var programmer_file = programmers_dir.get_child (file_info.get_name ()); |
|---|
| 256 | var programmer = new Programmer (); |
|---|
| 257 | try |
|---|
| 258 | { |
|---|
| 259 | programmer.open (programmer_file.get_uri ()); |
|---|
| 260 | I4uc.Core.Settings.instance.programmers[programmer.id] = programmer; |
|---|
| 261 | } |
|---|
| 262 | catch (KeyFileError e) |
|---|
| 263 | { |
|---|
| 264 | warning (programmer_file.get_path () + ": " + e.message); |
|---|
| 265 | continue; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | //check if $(HOME)/.i4uc/programmers/ exists. If not, create it |
|---|
| 270 | programmers_dir = File.new_for_path (Path.build_filename (Environment.get_home_dir (), ".i4uc", "programmers")); |
|---|
| 271 | if (!programmers_dir.query_exists (null)) |
|---|
| 272 | programmers_dir.make_directory (null); |
|---|
| 273 | |
|---|
| 274 | //Load programmers in $(HOME)/.i4uc/programmers/ |
|---|
| 275 | enumerator = programmers_dir.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, FileQueryInfoFlags.NONE, null); |
|---|
| 276 | while ((file_info = enumerator.next_file (null)) != null) |
|---|
| 277 | { |
|---|
| 278 | if (!file_info.get_name ().has_suffix (".i4ucprogrammer")) |
|---|
| 279 | continue; |
|---|
| 280 | |
|---|
| 281 | var programmer_file = programmers_dir.get_child (file_info.get_name ()); |
|---|
| 282 | var programmer = new Programmer (); |
|---|
| 283 | try |
|---|
| 284 | { |
|---|
| 285 | programmer.open (programmer_file.get_uri ()); |
|---|
| 286 | I4uc.Core.Settings.instance.programmers[programmer.id] = programmer; |
|---|
| 287 | } |
|---|
| 288 | catch (KeyFileError e) |
|---|
| 289 | { |
|---|
| 290 | warning (programmer_file.get_path () + ": " + e.message); |
|---|
| 291 | continue; |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /* |
|---|
| 297 | Load i4uc stock items located in $(pkgdatadir)/pixmaps |
|---|
| 298 | */ |
|---|
| 299 | void load_stock_items () throws GLib.Error |
|---|
| 300 | { |
|---|
| 301 | var icon_factory = new IconFactory (); |
|---|
| 302 | |
|---|
| 303 | var icon_set = new IconSet.from_pixbuf (new Gdk.Pixbuf.from_file (Path.build_filename (Config.PIXMAPS_DIR, "programmer-icon.svg"))); |
|---|
| 304 | icon_factory.add ("i4uc-programmer-icon", icon_set); |
|---|
| 305 | |
|---|
| 306 | icon_set = new IconSet.from_pixbuf (new Gdk.Pixbuf.from_file (Path.build_filename (Config.PIXMAPS_DIR, "write-flash.svg"))); |
|---|
| 307 | icon_factory.add ("i4uc-write-flash", icon_set); |
|---|
| 308 | |
|---|
| 309 | icon_set = new IconSet.from_pixbuf (new Gdk.Pixbuf.from_file (Path.build_filename (Config.PIXMAPS_DIR, "read-flash.svg"))); |
|---|
| 310 | icon_factory.add ("i4uc-read-flash", icon_set); |
|---|
| 311 | |
|---|
| 312 | icon_set = new IconSet.from_pixbuf (new Gdk.Pixbuf.from_file (Path.build_filename (Config.PIXMAPS_DIR, "verify-flash.svg"))); |
|---|
| 313 | icon_factory.add ("i4uc-verify-flash", icon_set); |
|---|
| 314 | |
|---|
| 315 | icon_set = new IconSet.from_pixbuf (new Gdk.Pixbuf.from_file (Path.build_filename (Config.PIXMAPS_DIR, "erase-flash.svg"))); |
|---|
| 316 | icon_factory.add ("i4uc-erase-flash", icon_set); |
|---|
| 317 | |
|---|
| 318 | icon_factory.add_default (); |
|---|
| 319 | } |
|---|