Tooth/src/Application.vala

212 lines
5.6 KiB
Vala
Raw Normal View History

2018-04-14 12:09:06 +00:00
using Gtk;
2018-10-23 10:05:24 +00:00
namespace Tootle {
2018-04-14 12:09:06 +00:00
2020-09-05 08:02:42 +00:00
public errordomain Oopsie {
USER,
PARSING,
INSTANCE,
INTERNAL
}
public static Application app;
public static Dialogs.MainWindow? window;
2020-09-10 17:10:24 +00:00
public static Dialogs.NewAccount? new_account_window;
2020-09-05 08:02:42 +00:00
public static Window window_dummy;
public static Settings settings;
public static Accounts accounts;
public static Network network;
public static Cache cache;
public static Streams streams;
public static bool start_hidden = false;
public class Application : Gtk.Application {
// These are used for the GTK Inspector
public Settings app_settings { get {return Tootle.settings; } }
public Accounts app_accounts { get {return Tootle.accounts; } }
public Network app_network { get {return Tootle.network; } }
public Cache app_cache { get {return Tootle.cache; } }
public Streams app_streams { get {return Tootle.streams; } }
public signal void refresh ();
public signal void toast (string title);
2020-09-10 17:10:24 +00:00
public signal void error (string title, string? text);
public CssProvider css_provider = new CssProvider ();
public CssProvider zoom_css_provider = new CssProvider ();
2020-09-05 08:02:42 +00: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 },
{ "switch-timeline", switch_timeline_activated, "i" }
};
construct {
application_id = Build.DOMAIN;
2020-09-10 17:10:24 +00:00
flags = ApplicationFlags.HANDLES_OPEN;
2020-09-05 08:02:42 +00: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_TIMELINE_0 = {"<Alt>1"};
public string[] ACCEL_TIMELINE_1 = {"<Alt>2"};
public string[] ACCEL_TIMELINE_2 = {"<Alt>3"};
public string[] ACCEL_TIMELINE_3 = {"<Alt>4"};
public static int main (string[] args) {
Gtk.init (ref args);
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 ();
Build.print_info ();
2020-07-06 17:04:01 +00:00
Hdy.init ();
2019-03-09 11:42:27 +00:00
2020-09-05 08:02:42 +00:00
settings = new Settings ();
streams = new Streams ();
accounts = new Accounts ();
network = new Network ();
cache = new Cache ();
accounts.init ();
2020-09-10 17:10:24 +00:00
app.error.connect ((title, msg) => {
inform (Gtk.MessageType.ERROR, title, msg);
});
2020-09-05 08:02:42 +00:00
window_dummy = new Window ();
add_window (window_dummy);
2020-09-10 17:10:24 +00:00
css_provider.load_from_resource (@"$(Build.RESOURCES)app.css");
StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), zoom_css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2020-09-05 08:02:42 +00: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.switch-timeline(0)", ACCEL_TIMELINE_0);
set_accels_for_action ("app.switch-timeline(1)", ACCEL_TIMELINE_1);
set_accels_for_action ("app.switch-timeline(2)", ACCEL_TIMELINE_2);
set_accels_for_action ("app.switch-timeline(3)", ACCEL_TIMELINE_3);
add_action_entries (app_entries, this);
}
protected override void activate () {
2020-09-10 17:10:24 +00:00
present_window ();
2020-09-05 08:02:42 +00:00
if (start_hidden) {
start_hidden = false;
return;
}
2020-09-10 17:10:24 +00:00
}
public override void open (File[] files, string hint) {
foreach (File file in files) {
string uri = file.get_uri ();
if (new_account_window != null)
new_account_window.redirect (uri);
else
warning (@"Received an unexpected uri to open: $uri");
return;
}
}
2020-09-05 08:02:42 +00:00
2020-09-10 17:10:24 +00:00
public void present_window () {
if (accounts.is_empty ()) {
message ("Presenting NewAccount dialog");
if (new_account_window == null)
new Dialogs.NewAccount ();
}
else {
message ("Presenting MainWindow");
if (window == null)
window = new Dialogs.MainWindow (this);
window.present ();
}
}
public bool on_window_closed () {
if (!settings.work_in_background || accounts.is_empty ())
app.remove_window (window_dummy);
return false;
2020-09-05 08:02:42 +00:00
}
void compose_activated () {
new Dialogs.Compose ();
}
void back_activated () {
window.back ();
}
void refresh_activated () {
refresh ();
}
void switch_timeline_activated (SimpleAction a, Variant? v) {
int32 num = v.get_int32 ();
window.switch_timeline (num);
}
void about_activated () {
new Dialogs.About ();
}
2020-09-10 17:10:24 +00:00
public void inform (Gtk.MessageType type, string text, string? msg = null, Gtk.Window? win = window){
2020-09-05 08:02:42 +00:00
var dlg = new Gtk.MessageDialog (
2020-09-10 17:10:24 +00:00
win,
2020-09-05 08:02:42 +00:00
Gtk.DialogFlags.MODAL,
2020-09-10 17:10:24 +00:00
type,
2020-09-05 08:02:42 +00:00
Gtk.ButtonsType.OK,
null
);
2020-09-10 17:10:24 +00:00
dlg.text = text;
2020-09-05 08:02:42 +00:00
dlg.secondary_text = msg;
2020-09-10 17:10:24 +00:00
dlg.transient_for = win;
2020-09-05 08:02:42 +00:00
dlg.run ();
dlg.destroy ();
}
2019-03-09 11:42:27 +00:00
2020-09-10 17:10:24 +00:00
public bool question (string text, string? msg = null, Gtk.Window? win = window) {
2020-09-05 08:02:42 +00:00
var dlg = new Gtk.MessageDialog (
2020-09-10 17:10:24 +00:00
win,
2020-09-05 08:02:42 +00:00
Gtk.DialogFlags.MODAL,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.YES_NO,
null
);
dlg.text = text;
2020-09-10 17:10:24 +00:00
dlg.secondary_text = msg;
2020-09-05 08:02:42 +00:00
dlg.transient_for = win;
var i = dlg.run ();
dlg.destroy ();
return i == ResponseType.YES;
2020-07-28 18:30:45 +00:00
}
2020-09-05 08:02:42 +00:00
}
2018-04-14 12:09:06 +00:00
}