tootle-linux-client/src/Application.vala

229 lines
6.0 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
2018-10-23 12:05:24 +02:00
namespace Tootle {
2018-04-14 14:09:06 +02:00
2020-09-05 10:02:42 +02:00
public errordomain Oopsie {
USER,
PARSING,
INSTANCE,
INTERNAL
}
public static Application app;
public static Settings settings;
2021-02-12 16:26:37 +01:00
public static AccountStore accounts;
2020-09-05 10:02:42 +02:00
public static Network network;
public static Streams streams;
2021-07-23 13:41:03 +02:00
public static EntityCache entity_cache;
public static ImageCache image_cache;
2020-09-05 10:02:42 +02:00
public static bool start_hidden = false;
public class Application : Gtk.Application {
2021-07-23 13:41:03 +02:00
public Dialogs.MainWindow? main_window { get; set; }
public Dialogs.NewAccount? add_account_window { get; set; }
2020-09-05 10:02:42 +02:00
// These are used for the GTK Inspector
public Settings app_settings { get {return Tootle.settings; } }
2021-02-12 16:26:37 +01:00
public AccountStore app_accounts { get {return Tootle.accounts; } }
2020-09-05 10:02:42 +02:00
public Network app_network { get {return Tootle.network; } }
public Streams app_streams { get {return Tootle.streams; } }
public signal void refresh ();
public signal void toast (string title);
2020-09-10 19:10:24 +02:00
public CssProvider css_provider = new CssProvider ();
2021-07-23 13:41:03 +02:00
public CssProvider zoom_css_provider = new CssProvider (); //FIXME: Zoom not working
2020-09-05 10:02:42 +02:00
public const GLib.OptionEntry[] app_options = {
{ "hidden", 0, 0, OptionArg.NONE, ref start_hidden, "Do not show main window on start", null },
{ null }
};
public const GLib.ActionEntry[] app_entries = {
{ "about", about_activated },
{ "compose", compose_activated },
{ "back", back_activated },
{ "refresh", refresh_activated },
{ "search", search_activated },
2020-09-05 10:02:42 +02:00
};
construct {
application_id = Build.DOMAIN;
2020-09-10 19:10:24 +02:00
flags = ApplicationFlags.HANDLES_OPEN;
2020-09-05 10:02:42 +02:00
}
public string[] ACCEL_ABOUT = {"F1"};
public string[] ACCEL_NEW_POST = {"<Ctrl>T"};
public string[] ACCEL_BACK = {"<Alt>BackSpace", "<Alt>Left"};
public string[] ACCEL_REFRESH = {"<Ctrl>R", "F5"};
public string[] ACCEL_SEARCH = {"<Ctrl>F"};
2020-09-05 10:02:42 +02:00
public static int main (string[] args) {
2021-07-23 13:41:03 +02:00
Gtk.init ();
2020-09-05 10:02:42 +02:00
try {
var opt_context = new OptionContext ("- Options");
opt_context.add_main_entries (app_options, null);
opt_context.parse (ref args);
}
catch (GLib.OptionError e) {
warning (e.message);
}
app = new Application ();
return app.run (args);
}
protected override void startup () {
base.startup ();
2021-02-12 16:26:37 +01:00
try {
Build.print_info ();
2021-07-23 13:41:03 +02:00
Adw.init ();
2021-02-12 16:26:37 +01:00
settings = new Settings ();
streams = new Streams ();
network = new Network ();
2021-07-23 13:41:03 +02:00
entity_cache = new EntityCache ();
image_cache = new ImageCache ();
accounts = new SecretAccountStore();
2021-02-12 16:26:37 +01:00
accounts.init ();
css_provider.load_from_resource (@"$(Build.RESOURCES)app.css");
2021-07-23 13:41:03 +02:00
StyleContext.add_provider_for_display (Gdk.Display.get_default (), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
StyleContext.add_provider_for_display (Gdk.Display.get_default (), zoom_css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2021-02-12 16:26:37 +01:00
}
catch (Error e) {
var msg = _("Could not start application: %s").printf (e.message);
inform (Gtk.MessageType.ERROR, _("Error"), msg);
error (msg);
}
2020-09-10 19:10:24 +02:00
2020-09-05 10:02:42 +02:00
set_accels_for_action ("app.about", ACCEL_ABOUT);
set_accels_for_action ("app.compose", ACCEL_NEW_POST);
set_accels_for_action ("app.back", ACCEL_BACK);
set_accels_for_action ("app.refresh", ACCEL_REFRESH);
set_accels_for_action ("app.search", ACCEL_SEARCH);
2020-09-05 10:02:42 +02:00
add_action_entries (app_entries, this);
}
protected override void activate () {
2020-09-10 19:10:24 +02:00
present_window ();
2020-09-05 10:02:42 +02:00
if (start_hidden) {
start_hidden = false;
return;
}
2020-09-10 19:10:24 +02:00
}
public override void open (File[] files, string hint) {
foreach (File file in files) {
string uri = file.get_uri ();
2021-07-23 13:41:03 +02:00
if (add_account_window != null)
add_account_window.redirect (uri);
2020-09-10 19:10:24 +02:00
else
warning (@"Received an unexpected uri to open: $uri");
return;
}
}
2020-09-05 10:02:42 +02:00
2020-09-10 19:10:24 +02:00
public void present_window () {
2021-02-12 16:26:37 +01:00
if (accounts.saved.is_empty) {
2020-09-10 19:10:24 +02:00
message ("Presenting NewAccount dialog");
2021-07-23 13:41:03 +02:00
if (add_account_window == null)
2020-09-10 19:10:24 +02:00
new Dialogs.NewAccount ();
2021-07-23 13:41:03 +02:00
add_account_window.present ();
2020-09-10 19:10:24 +02:00
}
else {
message ("Presenting MainWindow");
2021-07-23 13:41:03 +02:00
if (main_window == null)
main_window = new Dialogs.MainWindow (this);
main_window.present ();
2020-09-10 19:10:24 +02:00
}
}
2021-07-23 13:41:03 +02:00
// TODO: Background mode
// public bool on_window_closed () {
// if (!settings.work_in_background || accounts.saved.is_empty)
// app.remove_window (window_dummy);
// return false;
// }
2020-09-05 10:02:42 +02:00
void compose_activated () {
new Dialogs.Compose ();
}
void back_activated () {
2021-07-23 13:41:03 +02:00
main_window.back ();
2020-09-05 10:02:42 +02:00
}
void search_activated () {
2021-07-23 13:41:03 +02:00
main_window.open_view (new Views.Search ());
}
2020-09-05 10:02:42 +02:00
void refresh_activated () {
refresh ();
}
void about_activated () {
2021-07-23 13:41:03 +02:00
var dialog = new AboutDialog () {
transient_for = main_window,
modal = true,
logo_icon_name = Build.DOMAIN,
program_name = Build.NAME,
version = Build.VERSION,
website = Build.SUPPORT_WEBSITE,
website_label = _("Report an issue"),
license_type = License.GPL_3_0_ONLY,
copyright = Build.COPYRIGHT,
system_information = Build.SYSTEM_INFO
};
// For some obscure reason, const arrays produce duplicates in the credits.
// Static functions seem to avoid this peculiar behavior.
dialog.authors = Build.get_authors ();
dialog.artists = Build.get_artists ();
dialog.translator_credits = Build.TRANSLATOR != " " ? Build.TRANSLATOR : null;
dialog.present ();
2020-09-05 10:02:42 +02:00
}
2021-07-23 13:41:03 +02:00
public void inform (Gtk.MessageType type, string text, string? msg = null, Gtk.Window? win = main_window){
2020-09-05 10:02:42 +02:00
var dlg = new Gtk.MessageDialog (
2020-09-10 19:10:24 +02:00
win,
2020-09-05 10:02:42 +02:00
Gtk.DialogFlags.MODAL,
2020-09-10 19:10:24 +02:00
type,
2020-09-05 10:02:42 +02:00
Gtk.ButtonsType.OK,
null
);
2020-09-10 19:10:24 +02:00
dlg.text = text;
2020-09-05 10:02:42 +02:00
dlg.secondary_text = msg;
2020-09-10 19:10:24 +02:00
dlg.transient_for = win;
2021-07-23 13:41:03 +02:00
// dlg.run ();
2020-09-05 10:02:42 +02:00
dlg.destroy ();
}
2019-03-09 12:42:27 +01:00
2021-07-23 13:41:03 +02:00
public bool question (string text, string? msg = null, Gtk.Window? win = main_window) {
2020-09-05 10:02:42 +02:00
var dlg = new Gtk.MessageDialog (
2020-09-10 19:10:24 +02:00
win,
2020-09-05 10:02:42 +02:00
Gtk.DialogFlags.MODAL,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.YES_NO,
null
);
dlg.text = text;
2020-09-10 19:10:24 +02:00
dlg.secondary_text = msg;
2020-09-05 10:02:42 +02:00
dlg.transient_for = win;
2021-07-23 13:41:03 +02:00
// var i = dlg.run ();
2020-09-05 10:02:42 +02:00
dlg.destroy ();
2021-07-23 13:41:03 +02:00
// return i == ResponseType.YES;
return false;
2020-07-28 20:30:45 +02:00
}
2020-09-05 10:02:42 +02:00
}
2018-04-14 14:09:06 +02:00
}