1/*
2 * Copyright 2019–2020 elementary, Inc. (https://elementary.io)
3 * Copyright 2011–2013 Maxwell Barvian <maxwell@elementaryos.org>
4 * SPDX-License-Identifier: LGPL-3.0-or-later
5 */
6
7namespace Granite {
8    /**
9     * This is the base class for all Granite-based apps. It has methods that help
10     * to create a great deal of an app's functionality.
11     */
12    [Version (deprecated = true, deprecated_since = "0.5.0", replacement = "Gtk.Application")]
13    public abstract class Application : Gtk.Application {
14
15        public string build_data_dir;
16        public string build_pkg_data_dir;
17        public string build_release_name;
18        public string build_version;
19        public string build_version_info;
20
21        /**
22         * The user facing name of the application. This name is used
23         * throughout the application and should be capitalized correctly.
24         */
25        public string program_name;
26
27        /**
28         * The compiled binary name, which must match the CMake exec name.
29         * This is used to launch the application from a launcher or the
30         * command line.
31         */
32        public string exec_name;
33
34        /**
35         * Years that the copyright extends to. Usually from the start
36         * of the project to the most recent modification to it.
37         */
38        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
39        public string app_copyright;
40        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
41        public string app_years;
42
43        /**
44         * Icon to be associated with the application.
45         *
46         * This is either the name of an icon shipped by the icon theme,
47         * or the name of an icon shipped with the app (for custom icons).
48         * The name should not include the full path or file extension.
49         * WRONG: /usr/share/icons/myicon.png RIGHT: myicon
50         */
51        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
52        public string app_icon;
53
54        /**
55         * The launcher to be associated with this application.
56         *
57         * This should be the name of a file in /usr/share/applications/.
58         * See [[http://standards.freedesktop.org/desktop-entry-spec/latest/]]
59         * for more information.
60         */
61        public string app_launcher;
62
63        /**
64         * Main website or homepage for the application.
65         *
66         * If the application has no homepage, one should be created on
67         * launchpad.net.
68         */
69        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
70        public string main_url;
71
72        /**
73         * A link to the software's public bug tracker.
74         *
75         * If the application does not have a bug tracker, one should be
76         * created on launchpad.net.
77         */
78        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
79        public string bug_url;
80
81        /**
82         * Link to question and answer site or support forum for the app.
83         *
84         * Launchpad offers a QA service if one is needed.
85         */
86        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
87        public string help_url;
88
89        /**
90         * Link to where users can translate the application.
91         *
92         * Launchad offers a translation service if one is necessary.
93         */
94        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
95        public string translate_url;
96
97        /**
98         * Full names of the application authors for the about dialog.
99         */
100        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
101        public string[] about_authors = {};
102
103        /**
104         * Full names of documenters of the app for the about dialog.
105         */
106        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
107        public string[] about_documenters = {};
108
109        /**
110         * Names of the designers of the application's user interface.
111         */
112        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
113        public string[] about_artists = {};
114        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
115        public string about_comments;
116
117        /**
118         * Names of the translators of the application.
119         */
120        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
121        public string about_translators;
122
123        /**
124         * The copyright license that the work is distributed under.
125         */
126        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
127        public string about_license;
128        [Version (deprecated = true, deprecated_since = "0.4.2", replacement = "")]
129        public Gtk.License about_license_type;
130
131        /**
132         * This creates a new Application class
133         */
134        protected Application () {
135#if LINUX
136            prctl (15, exec_name, 0, 0, 0);
137#elif DRAGON_FLY || FREE_BSD || NET_BSD || OPEN_BSD
138            setproctitle (exec_name);
139#endif
140            Granite.Services.Logger.initialize (program_name);
141            Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.INFO;
142            message ("%s version: %s", program_name, build_version);
143#if !WINDOWS
144            var un = Posix.utsname ();
145            message ("Kernel version: %s", (string) un.release);
146#endif
147            Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.WARN;
148
149            Intl.setlocale (LocaleCategory.ALL, "");
150            string langpack_dir = Path.build_path (Path.DIR_SEPARATOR_S, build_data_dir, "locale");
151            Intl.bindtextdomain (exec_name, langpack_dir);
152            Intl.bind_textdomain_codeset (exec_name, "UTF-8");
153            Intl.textdomain (exec_name);
154
155            handle_local_options.connect (on_handle_local_options);
156        }
157
158#if LINUX
159        [CCode (cheader_filename = "sys/prctl.h", cname = "prctl")]
160        extern static int prctl (int option, string arg2, ulong arg3, ulong arg4, ulong arg5);
161#elif DRAGON_FLY || FREE_BSD
162        [CCode (cheader_filename = "unistd.h", cname = "setproctitle")]
163        extern static void setproctitle (string fmt, ...);
164#elif NET_BSD || OPEN_BSD
165        [CCode (cheader_filename = "stdlib.h", cname = "setproctitle")]
166        extern static void setproctitle (string fmt, ...);
167#endif
168
169        /**
170         * This method runs the application
171         *
172         * @param args array of arguments
173         */
174        public new int run (string[] args) {
175            var option_group = new OptionGroup ("granite", "Granite Options", _("Show Granite Options"));
176            option_group.add_entries (options);
177
178            add_option_group ((owned)option_group);
179
180            return base.run (args);
181        }
182
183        private int on_handle_local_options (VariantDict options) {
184            set_options ();
185            return -1;
186        }
187
188        protected static bool DEBUG = false; // vala-lint=naming-convention
189
190        protected const OptionEntry[] options = { // vala-lint=naming-convention
191            { "debug", 'd', 0, OptionArg.NONE, out DEBUG, "Enable debug logging", null },
192            { null }
193        };
194
195        protected virtual void set_options () {
196
197            if (DEBUG) {
198                Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.DEBUG;
199            }
200        }
201    }
202}
203