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 if (new_gui) {
46 _gtk_main = new Gtk::Main(argc, argv, true);
48 // TODO: Determine class by arguments
49 g_warning("Creating new Editor");
50 _app_impl = (AppPrototype*) Editor::create(_argc, _argv);
52 } else if (use_gui) {
53 // No op - we'll use the old interface
54 } else {
55 _app_impl = NULL; // = Cmdline(_argc, _argv);
56 }
58 /// \todo Install segv handler here?
60 // Inkscape::Extension::init();
61 }
63 Application::~Application()
64 {
65 g_free(_path_home);
66 }
69 /** Returns the current home directory location */
70 gchar const*
71 Application::homedir() const
72 {
73 if ( !_path_home ) {
74 _path_home = g_strdup(g_get_home_dir());
75 gchar* utf8Path = g_filename_to_utf8( _path_home, -1, NULL, NULL, NULL );
76 if ( utf8Path ) {
77 _path_home = utf8Path;
78 if ( !g_utf8_validate(_path_home, -1, NULL) ) {
79 g_warning( "Home directory is non-UTF-8" );
80 }
81 }
82 }
83 if ( !_path_home && _argv != NULL) {
84 gchar * path = g_path_get_dirname(_argv[0]);
85 gchar * utf8Path = g_filename_to_utf8( path, -1, NULL, NULL, NULL );
86 g_free(path);
87 if (utf8Path) {
88 _path_home = utf8Path;
89 if ( !g_utf8_validate(_path_home, -1, NULL) ) {
90 g_warning( "Application run directory is non-UTF-8" );
91 }
92 }
93 }
94 return _path_home;
95 }
97 gint
98 Application::run()
99 {
100 gint result = 0;
102 /* Note: This if loop should be replaced by calls to the
103 * various subclasses of I::A::AppPrototype.
104 */
105 if (_gtk_main != NULL) {
106 g_assert(_app_impl != NULL);
107 g_warning("Running main window");
108 Gtk::Window *win = static_cast<Gtk::Window*>(_app_impl->getWindow());
109 g_assert(win != NULL);
110 _gtk_main->run(*win);
111 result = 0;
113 } else if (_use_gui) {
114 result = sp_main_gui(_argc, (const char**)_argv);
116 } else {
117 result = sp_main_console(_argc, (const char**)_argv);
118 }
120 return result;
121 }
123 void
124 Application::exit()
125 {
126 Inkscape::Preferences::unload();
128 if (_gtk_main != NULL) {
129 _gtk_main->quit();
130 }
132 }
134 bool
135 Application::getUseGui()
136 {
137 return _use_gui;
138 }
140 bool
141 Application::getNewGui()
142 {
143 return _new_gui;
144 }
147 } // namespace NSApplication
148 } // namespace Inkscape
151 /*
152 Local Variables:
153 mode:c++
154 c-file-style:"stroustrup"
155 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156 indent-tabs-mode:nil
157 fill-column:99
158 End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :