Skip to content

Commit

Permalink
Application: use startup (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Feb 2, 2023
1 parent 9d63c5d commit eba92c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
68 changes: 36 additions & 32 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

public class Torrential.Application : Gtk.Application {
private MainWindow? window = null;
private TorrentManager torrent_manager = null;

const OptionEntry[] ENTRIES = {
Expand All @@ -40,17 +39,36 @@ public class Torrential.Application : Gtk.Application {
torrent_manager = new TorrentManager ();
}

public override void startup () {
base.startup ();

Intl.setlocale (LocaleCategory.ALL, "");
Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain (GETTEXT_PACKAGE);

Gtk.IconTheme.get_default ().add_resource_path ("/com/github/davidmhewitt/torrential");

var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();

gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;

granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
});
}

public override void open (File[] files, string hint) {
if (files[0].has_uri_scheme ("magnet")) {
var magnet = files[0].get_uri ();
magnet = magnet.replace ("magnet:///?", "magnet:?");

if (window != null) {
window.add_magnet (magnet);
} else {
if (active_window == null) {
activate ();
window.add_magnet (magnet);
}

((MainWindow) active_window).add_magnet (magnet);
return;
}

Expand All @@ -60,38 +78,24 @@ public class Torrential.Application : Gtk.Application {
}

activate ();
window.add_files (uris);
((MainWindow) active_window).add_files (uris);
}

public override void activate () {
Intl.setlocale (LocaleCategory.ALL, "");
GLib.Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
GLib.Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
GLib.Intl.textdomain (GETTEXT_PACKAGE);

if (window == null) {
window = new MainWindow (this, torrent_manager);
if (active_window == null) {
var window = new MainWindow (this, torrent_manager);
add_window (window);
} else {
window.present ();
active_window.present ();
}

var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();

gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;

granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
});
}

public override int command_line (GLib.ApplicationCommandLine command_line) {
var options = command_line.get_options_dict ();

if (options.contains ("quit")) {
if (window != null) {
window.quit ();
if (active_window != null) {
((MainWindow) active_window).quit ();
}
}

Expand All @@ -116,16 +120,16 @@ public class Torrential.Application : Gtk.Application {
return Posix.EXIT_SUCCESS;
}

public void close_session () {
private void close_session () {
torrent_manager.close_session ();
}
}

int main (string[] args) {
var app = new Torrential.Application ();
var result = app.run (args);
public static int main (string[] args) {
var app = new Torrential.Application ();
var result = app.run (args);

app.close_session ();
app.close_session ();

return result;
return result;
}
}
2 changes: 0 additions & 2 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow {

this.torrent_manager = torrent_manager;

Gtk.IconTheme.get_default ().add_resource_path ("/com/github/davidmhewitt/torrential");

settings = new GLib.Settings ("com.github.davidmhewitt.torrential.settings");

set_default_size (
Expand Down

0 comments on commit eba92c6

Please sign in to comment.