1 /** \file
2 * \brief The top level class for managing the application.
3 *
4 * Authors:
5 * Bryce W. Harrington <bryce@bryceharrington.org>
6 * Ralf Stephan <ralf@ark.in-berlin.de>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <gtkmm/main.h>
19 #include "preferences.h"
20 #include "application.h"
21 #include "editor.h"
23 int sp_main_gui(int argc, char const **argv);
24 int sp_main_console(int argc, char const **argv);
26 static Gtk::Main *_gtk_main;
27 static bool _use_gui, _new_gui;
29 namespace Inkscape {
30 namespace NSApplication {
32 Application::Application(int argc, char **argv, bool use_gui, bool new_gui)
33 : _argc(argc),
34 _argv(NULL),
35 _app_impl(NULL),
36 _path_home(NULL)
37 {
38 _use_gui = use_gui;
39 _new_gui = new_gui;
41 if (argv != NULL) {
42 _argv = argv; // TODO: Is this correct?
43 }
45 Inkscape::Preferences::loadSkeleton();
46 if (new_gui) {
47 _gtk_main = new Gtk::Main(argc, argv, true);
49 // TODO: Determine class by arguments
50 g_warning("Creating new Editor");
51 _app_impl = (AppPrototype*) Editor::create(_argc, _argv);
53 } else if (use_gui) {
54 // No op - we'll use the old interface
55 } else {
56 _app_impl = NULL; // = Cmdline(_argc, _argv);
57 }
59 /// \todo Install segv handler here?
61 // Inkscape::Extension::init();
62 }
64 Application::~Application()
65 {
66 g_free(_path_home);
67 }
70 /** Returns the current home directory location */
71 gchar const*
72 Application::homedir() const
73 {
74 if ( !_path_home ) {
75 _path_home = g_strdup(g_get_home_dir());
76 gchar* utf8Path = g_filename_to_utf8( _path_home, -1, NULL, NULL, NULL );
77 if ( utf8Path ) {
78 _path_home = utf8Path;
79 if ( !g_utf8_validate(_path_home, -1, NULL) ) {
80 g_warning( "Home directory is non-UTF-8" );
81 }
82 }
83 }
84 if ( !_path_home && _argv != NULL) {
85 gchar * path = g_path_get_dirname(_argv[0]);
86 gchar * utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
87 g_free(path);
88 if (utf8Path) {
89 _path_home = utf8Path;
90 if ( !g_utf8_validate(_path_home, -1, NULL) ) {
91 g_warning( "Application run directory is non-UTF-8" );
92 }
93 }
94 }
95 return _path_home;
96 }
98 gint
99 Application::run()
100 {
101 gint result = 0;
103 /* Note: This if loop should be replaced by calls to the
104 * various subclasses of I::A::AppPrototype.
105 */
106 if (_gtk_main != NULL) {
107 g_assert(_app_impl != NULL);
109 Inkscape::Preferences::load();
110 g_warning("Running main window");
111 Gtk::Window *win = static_cast<Gtk::Window*>(_app_impl->getWindow());
112 g_assert(win != NULL);
113 _gtk_main->run(*win);
114 result = 0;
116 } else if (_use_gui) {
117 result = sp_main_gui(_argc, (const char**)_argv);
119 } else {
120 result = sp_main_console(_argc, (const char**)_argv);
121 }
123 return result;
124 }
126 void
127 Application::exit()
128 {
129 Inkscape::Preferences::save();
131 if (_gtk_main != NULL) {
132 _gtk_main->quit();
133 }
135 }
137 bool
138 Application::getUseGui()
139 {
140 return _use_gui;
141 }
143 bool
144 Application::getNewGui()
145 {
146 return _new_gui;
147 }
150 } // namespace NSApplication
151 } // namespace Inkscape
154 /*
155 Local Variables:
156 mode:c++
157 c-file-style:"stroustrup"
158 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
159 indent-tabs-mode:nil
160 fill-column:99
161 End:
162 */
163 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :