diff --git a/README.md b/README.md index 35c9986..eacfbd1 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ Build the cli interface. Build the gui interface. +- build-tray + +Build the tray icon + - always-authenticated Authentication is not required to use the TUF Server that runs in the background as a daemon, if this is set to false polkit is used for authentication. Setting this to false is more secure, but also makes things like auto restore of settings on login impossible to do without a password. @@ -67,22 +71,27 @@ Install a systemd unit. If this option is not set, the unit is still generated d ## Usage -There are 2 programs and 1 daemon supplied by TUF Manager. +There are 3 programs and 1 daemon supplied by TUF Manager. - tuf-cli This allows controlling the TUF laptop functions via command line interface. + - tuf-gui This supplies a graphical interface to control the TUF laptop. + - tuf-server This is the daemon that runs in the background and handles all requests from both tuf-cli and tuf-gui. +- tuf-tray + +This is the tray icon to control the TUF Manager program + ## Future plans - Manpage - Config options for both tuf-gui and tuf-cli -- Status bar icon that allows control of the notebook - Notifications of fan mode change via status bar - Restore last known settings on login via status bar icon diff --git a/docs/meson.build b/docs/meson.build index db18318..85af82a 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -7,6 +7,9 @@ if docs_enabled endif if build_gui vala_doc_sources += gui_vala_sources + if build_tray + vala_doc_sources += tray_vala_sources + endif endif vala_doc_sources += error_vala_sources vala_doc_sources += config_data_file @@ -14,7 +17,9 @@ if docs_enabled docs_deps = [ '--pkg=polkit-gobject-1', '--pkg=posix', - '--pkg=gtk+-3.0' + '--pkg=gtk+-3.0', + '--pkg=libnotify', + '--pkg=appindicator3-0.1' ] if valadocs_deps docs_deps += ['--deps'] diff --git a/meson.build b/meson.build index 2623dd3..4db4418 100644 --- a/meson.build +++ b/meson.build @@ -21,6 +21,7 @@ add_global_arguments( always_authenticated = get_option('always-authenticated') build_cli = get_option('build-cli') build_gui = get_option('build-gui') +build_tray = get_option('build-tray') subdir('po') subdir('data') diff --git a/meson_options.txt b/meson_options.txt index 14d1ab8..0e3ab14 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -22,6 +22,12 @@ option( value: true, description: 'Build the graphical user interface to TUF Manager' ) +option( + 'build-tray', + type : 'boolean', + value: true, + description: 'Build the tray icon, this option depends on build-gui' +) option( 'always-authenticated', type : 'boolean', diff --git a/src/cli.vala b/src/cli.vala index 436c45a..e1c1205 100644 --- a/src/cli.vala +++ b/src/cli.vala @@ -23,7 +23,7 @@ namespace TUFManager { /** * This class contains the app that runs on the command line */ - public class TUFManagerApp : Application { + public class TUFManagerApp : Gtk.Application { #if ALWAYS_AUTHENTICATED #else /** @@ -184,6 +184,8 @@ namespace TUFManager { } catch (TUFError e) { command_line.printerr (_ ("Error: ") + e.message + "\n"); + this.release (); + return 1; } string[] args = command_line.get_arguments (); diff --git a/src/meson.build b/src/meson.build index 517c62e..449e44d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,7 +1,7 @@ server_dependencies = [ dependency('glib-2.0'), dependency('gio-unix-2.0'), - dependency('gtk+-3.0', version: '>=3.0.0'), + dependency('gtk+-3.0', version: '>=3.10.0'), meson.get_compiler('c').find_library('m', required: true), meson.get_compiler('vala').find_library('posix') ] @@ -14,7 +14,7 @@ endif if build_cli cli_dependencies = [ dependency('glib-2.0'), - dependency('gtk+-3.0', version: '>=3.0.0'), + dependency('gtk+-3.0', version: '>=3.10.0'), meson.get_compiler('c').find_library('m', required: true) ] endif @@ -26,6 +26,14 @@ if build_gui dependency('gtk+-3.0', version: '>=3.10.0'), dependency('gmodule-2.0', version: '>=2.0') # gmodule-export-2.0 is needed to connect the handlers from glade ui ] + + if build_tray + tray_dependencies = [ + dependency('appindicator3-0.1'), + dependency('gtk+-3.0', version: '>=3.10.0'), + dependency('libnotify') + ] + endif endif config_data = configuration_data() @@ -69,6 +77,12 @@ server_vala_sources_main = files( 'server-main.vala' ) +if build_tray + tray_vala_sources_main = files( + 'tray-main.vala' + ) +endif + server_vala_sources = files( 'server.vala', 'server-interface.vala' @@ -89,6 +103,14 @@ if build_gui 'server-interface.vala', 'common.vala' ) + + if build_tray + tray_vala_sources = files( + 'tray.vala', + 'tray-icon.vala', + 'server-interface.vala' + ) + endif endif server_sources = server_vala_sources @@ -109,6 +131,13 @@ if build_gui gui_sources += error_vala_sources gui_sources += [tuf_manager_gresource] gui_sources += config_data_file + + if build_tray + tray_sources = tray_vala_sources + tray_sources += tray_vala_sources_main + tray_sources += error_vala_sources + tray_sources += config_data_file + endif endif server_vala_args = [] @@ -154,4 +183,13 @@ if build_gui vala_args: gui_vala_args, install: true ) + + if build_tray + tray_exe = executable( + 'tuf-tray', + tray_sources, + dependencies: tray_dependencies, + install: true + ) + endif endif diff --git a/src/tray-icon.vala b/src/tray-icon.vala new file mode 100644 index 0000000..cc856b7 --- /dev/null +++ b/src/tray-icon.vala @@ -0,0 +1,126 @@ +/* + * Copyright 2020 Chris Cromer + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * The TUF Manager namespace + */ +namespace TUFManager { + /** + * The tray namespace handles everything related to the system tray + */ + namespace Tray { + public class TrayIcon { + private AppIndicator.Indicator indicator; + private Notify.Notification notification; + private Gtk.IconTheme icon_theme; + + public TrayIcon (TUFManagerApp parent) { + icon_theme = Gtk.IconTheme.get_default (); + icon_theme.changed.connect (on_icon_theme_changed); + + indicator = new AppIndicator.Indicator (_ ("TUF Manager"), "tuf-manager", AppIndicator.IndicatorCategory.APPLICATION_STATUS); + + var menu = new Gtk.Menu (); + var item = new Gtk.MenuItem.with_label (_ ("TUF Manager")); + item.activate.connect (execute_manager); + menu.append (item); + item = new Gtk.MenuItem.with_mnemonic (_ ("_Quit")); + item.activate.connect (parent.release); + menu.append (item); + menu.show_all (); + + indicator.set_menu (menu); + + indicator.set_icon_full ("tuf-manager", "tuf-manager"); + indicator.set_title (_ ("TUF Manager")); + set_icon_visible (true); + + Timeout.add (200, () => { + indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE); + return false; + }); + + Notify.init (_ ("TUF Manager")); + + //show_notification ("test"); + //update_notification ("test2"); + } + + private void execute_manager () { + try { + Process.spawn_command_line_async ("tuf-gui"); + } + catch (SpawnError e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + } + + private void set_icon_visible (bool visible) { + if (visible) { + indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE); + } + else { + indicator.set_status (AppIndicator.IndicatorStatus.PASSIVE); + } + } + + private void show_notification (string info) { + try { + close_notification (); + notification = new Notify.Notification (_ ("TUF Manager"), info, "tuf-manager"); + notification.set_timeout (Notify.EXPIRES_DEFAULT); + notification.add_action ("default", _ ("Details"), execute_manager); + notification.show (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + } + + private void update_notification (string info) { + try { + if (notification != null) { + if (notification.get_closed_reason () == -1 && notification.body != info) { + notification.update (_ ("TUF Manager"), info, "tuf-manager"); + notification.show (); + } + } + else { + show_notification (info); + } + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + } + + private void close_notification () { + try { + if (notification != null && notification.get_closed_reason () == -1) { + notification.close (); + notification = null; + } + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + } + + private void on_icon_theme_changed () { + icon_theme = Gtk.IconTheme.get_default (); + indicator.set_icon_full ("tuf-manager", "tuf-manager"); + } + } + } +} diff --git a/src/tray-main.vala b/src/tray-main.vala new file mode 100644 index 0000000..39e3851 --- /dev/null +++ b/src/tray-main.vala @@ -0,0 +1,45 @@ +/* + * Copyright 2020 Chris Cromer + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * The TUF Manager namespace + */ +namespace TUFManager { + /** + * The server namespace contains anything releated to working as a dbus daemon + * and handling root related tasks + */ + namespace Tray { + /** + * The entry point to the tray icon + * + * @param args Arguments passed from the command line + * @return Returns 0 on success + */ + public static int main (string[] args) { + var application = new Tray.TUFManagerApp (); + try { + application.register (); + } + catch (Error e) { + stderr.printf (_ ("Error: %s\n"), e.message); + } + if (application.get_is_remote ()) { + stderr.printf (_ ("Error: ") + _ ("Another instance of TUF Manager is already running!\n")); + return -1; + } + return application.run (); + } + } +} diff --git a/src/tray.vala b/src/tray.vala new file mode 100644 index 0000000..a3d2456 --- /dev/null +++ b/src/tray.vala @@ -0,0 +1,42 @@ +/* + * Copyright 2020 Chris Cromer + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * The TUF Manager namespace + */ +namespace TUFManager { + /** + * The tray namespace handles everything related to the system tray + */ + namespace Tray { + public class TUFManagerApp : Gtk.Application { + public TUFManagerApp () { + application_id = "cl.cromer.tuf.manager.tray"; + } + + public override void activate () { + new TrayIcon (this); + } + + public override void startup () { + Intl.textdomain ("tuf-manager"); + Intl.setlocale (LocaleCategory.ALL, ""); + + base.startup (); + + this.hold (); + } + } + } +}