| 1 | /* |
| 2 | * Copyright 2020-2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use crate::config::VERSION; |
| 21 | use adw::{prelude::*, subclass::prelude::*}; |
| 22 | use gettextrs::gettext; |
| 23 | use gtk::{gdk, gio, glib}; |
| 24 | use std::path::Path; |
| 25 | |
| 26 | mod imp { |
| 27 | use super::*; |
| 28 | use crate::{ |
| 29 | assets::{Assets, DirectoryAssets}, |
| 30 | config::DATA_DIRECTORY, |
| 31 | game_config::GameConfigs, |
| 32 | properties_dialog::show_preferences, |
| 33 | window::RobotsWindow, |
| 34 | }; |
| 35 | use std::{cell::OnceCell, rc::Rc}; |
| 36 | |
| 37 | pub struct RobotsApplication { |
| 38 | settings: gio::Settings, |
| 39 | game_configs: GameConfigs, |
| 40 | assets: OnceCell<Rc<dyn Assets>>, |
| 41 | } |
| 42 | |
| 43 | #[glib::object_subclass] |
| 44 | impl ObjectSubclass for RobotsApplication { |
| 45 | const NAME: &'static str = "RobotsApplication"; |
| 46 | type Type = super::RobotsApplication; |
| 47 | type ParentType = adw::Application; |
| 48 | |
| 49 | fn new() -> Self { |
| 50 | Self { |
| 51 | settings: gio::Settings::new("org.gnome.Robots"), |
| 52 | game_configs: GameConfigs::new(), |
| 53 | assets: Default::default(), |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl ObjectImpl for RobotsApplication {} |
| 59 | |
| 60 | impl ApplicationImpl for RobotsApplication { |
| 61 | fn startup(&self) { |
| 62 | self.parent_startup(); |
| 63 | |
| 64 | if let Some(display) = gdk::Display::default() { |
| 65 | gtk::IconTheme::for_display(&display) |
| 66 | .add_search_path(Path::new(DATA_DIRECTORY).join("icons")); |
| 67 | } else { |
| 68 | eprintln!("No default display found"); |
| 69 | } |
| 70 | |
| 71 | glib::set_application_name(&gettext("Robots")); |
| 72 | gtk::Window::set_default_icon_name("org.gnome.Robots"); |
| 73 | |
| 74 | self.obj().add_action_entries([ |
| 75 | gio::ActionEntry::builder("new-game") |
| 76 | .activate(|this: &Self::Type, _, _| this.imp().new_game_cb()) |
| 77 | .build(), |
| 78 | gio::ActionEntry::builder("preferences") |
| 79 | .activate(|this: &Self::Type, _, _| this.imp().preferences_cb()) |
| 80 | .build(), |
| 81 | gio::ActionEntry::builder("scores") |
| 82 | .activate(|this: &Self::Type, _, _| this.imp().scores_cb()) |
| 83 | .build(), |
| 84 | gio::ActionEntry::builder("help") |
| 85 | .activate(|this: &Self::Type, _, _| this.imp().help_cb()) |
| 86 | .build(), |
| 87 | gio::ActionEntry::builder("about") |
| 88 | .activate(|this: &Self::Type, _, _| this.imp().about_cb()) |
| 89 | .build(), |
| 90 | gio::ActionEntry::builder("quit") |
| 91 | .activate(|this: &Self::Type, _, _| this.quit()) |
| 92 | .build(), |
| 93 | ]); |
| 94 | |
| 95 | self.obj() |
| 96 | .set_accels_for_action("app.new-game", &["<Primary>n"]); |
| 97 | self.obj().set_accels_for_action("app.help", &["F1"]); |
| 98 | self.obj() |
| 99 | .set_accels_for_action("app.quit", &["<Primary>q"]); |
| 100 | |
| 101 | match DirectoryAssets::from_directory(Path::new(DATA_DIRECTORY)) { |
| 102 | Ok(assets) => { |
| 103 | self.assets.set(Rc::new(assets)).ok().unwrap(); |
| 104 | } |
| 105 | Err(e) => { |
| 106 | eprintln!("CRITICAL: Failed to load game assets: {}", e); |
| 107 | self.obj().quit(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fn activate(&self) { |
| 113 | self.parent_activate(); |
| 114 | |
| 115 | if let Some(window) = self.obj().active_window().and_downcast::<RobotsWindow>() { |
| 116 | window.present(); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | match RobotsWindow::new( |
| 121 | &*self.obj(), |
| 122 | &self.settings, |
| 123 | self.game_configs.clone(), |
| 124 | self.assets.get().unwrap(), |
| 125 | ) { |
| 126 | Ok(window) => { |
| 127 | window.present(); |
| 128 | window.focus_game(); |
| 129 | } |
| 130 | Err(e) => { |
| 131 | eprintln!("CRITICAL: {}", e); |
| 132 | // TODO message box |
| 133 | self.obj().quit(); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl GtkApplicationImpl for RobotsApplication {} |
| 140 | impl AdwApplicationImpl for RobotsApplication {} |
| 141 | |
| 142 | impl RobotsApplication { |
| 143 | fn new_game_cb(&self) { |
| 144 | if let Some(window) = self.obj().active_window().and_downcast::<RobotsWindow>() { |
| 145 | let dialog = adw::AlertDialog::new( |
| 146 | Some(&gettext("New Game")), |
| 147 | Some(&gettext( |
| 148 | "Are you sure you want to discard the current game?", |
| 149 | )), |
| 150 | ); |
| 151 | |
| 152 | dialog.add_response("cancel", &gettext("Keep _Playing")); |
| 153 | dialog.add_response("new", &gettext("_New Game")); |
| 154 | dialog.set_response_appearance("new", adw::ResponseAppearance::Destructive); |
| 155 | |
| 156 | dialog.set_default_response(Some("new")); |
| 157 | dialog.set_close_response("cancel"); |
| 158 | |
| 159 | dialog.choose(Some(&window.clone()), gio::Cancellable::NONE, move |ret| { |
| 160 | if ret == "new" { |
| 161 | window.start_new_game(); |
| 162 | } |
| 163 | }); |
| 164 | } else { |
| 165 | self.activate(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | fn preferences_cb(&self) { |
| 170 | show_preferences( |
| 171 | self.obj().active_window().as_ref(), |
| 172 | &self.game_configs, |
| 173 | &self.assets.get().unwrap().themes(), |
| 174 | &self.settings, |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | fn scores_cb(&self) { |
| 179 | if let Some(window) = self.obj().active_window().and_downcast::<RobotsWindow>() { |
| 180 | window.show_highscores(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | fn help_cb(&self) { |
| 185 | let launcher = gtk::UriLauncher::new("help:gnome-robots"); |
| 186 | launcher.launch( |
| 187 | self.obj().active_window().as_ref(), |
| 188 | gio::Cancellable::NONE, |
| 189 | |_| {}, |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | fn about_cb(&self) { |
| 194 | adw::AboutDialog::builder() |
| 195 | .application_icon("org.gnome.Robots") |
| 196 | .application_name(gettext("Robots")) |
| 197 | .version(VERSION) |
| 198 | .copyright("Copyright © 1998–2008 Mark Rae\nCopyright © 2014–2016 Michael Catanzaro\nCopyright © 2020-2025 Andrey Kutejko") |
| 199 | .license_type(gtk::License::Gpl30) |
| 200 | .comments(gettext("Based on classic BSD Robots")) |
| 201 | .developers([ |
| 202 | "Mark Rae <[email protected]>", |
| 203 | "Andrey Kutejko <[email protected]>" |
| 204 | ]) |
| 205 | .artists([ |
| 206 | "Kirstie Opstad <[email protected]>", |
| 207 | "Rasoul M.P. Aghdam (player death sound)" |
| 208 | ]) |
| 209 | .documenters([ |
| 210 | "Aruna Sankaranarayanan" |
| 211 | ]) |
| 212 | .translator_credits(gettext("translator-credits")) |
| 213 | .website("https://gitlab.gnome.org/GNOME/gnome-robots") |
| 214 | .build() |
| 215 | .present(self.obj().active_window().as_ref()); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | glib::wrapper! { |
| 221 | pub struct RobotsApplication(ObjectSubclass<imp::RobotsApplication>) |
| 222 | @extends adw::Application, gtk::Application, gio::Application, |
| 223 | @implements gio::ActionGroup, gio::ActionMap; |
| 224 | } |
| 225 | |
| 226 | impl Default for RobotsApplication { |
| 227 | fn default() -> Self { |
| 228 | glib::Object::builder() |
| 229 | .property("application-id", "org.gnome.Robots") |
| 230 | .property("flags", gio::ApplicationFlags::default()) |
| 231 | .build() |
| 232 | } |
| 233 | } |