tootle-linux-client/src/Application.vala

148 lines
4.6 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
using Granite;
2018-10-23 12:05:24 +02:00
namespace Tootle {
2018-04-14 14:09:06 +02:00
2020-05-29 14:19:35 +02:00
public errordomain Oopsie {
USER,
PARSING,
2020-06-03 17:06:11 +02:00
INSTANCE,
INTERNAL
2020-05-29 14:19:35 +02:00
}
2018-04-14 14:09:06 +02:00
public static Application app;
2019-03-11 15:14:37 +01:00
public static Dialogs.MainWindow? window;
2018-05-08 18:09:38 +02:00
public static Window window_dummy;
2019-03-09 12:42:27 +01:00
2018-06-17 09:48:18 +02:00
public static Settings settings;
public static Accounts accounts;
public static Network network;
2020-05-29 14:19:35 +02:00
public static Cache cache;
public static Streams streams;
2018-04-14 14:09:06 +02:00
2018-10-27 10:55:40 +02:00
public static bool start_hidden = false;
2020-05-31 14:47:12 +02:00
public class Application : Gtk.Application {
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
// 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);
public signal void error (string title, string text);
2018-10-27 10:55:40 +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 = {
2020-05-29 14:19:35 +02:00
{"compose", compose_activated },
{"back", back_activated },
{"refresh", refresh_activated },
{"switch-timeline", switch_timeline_activated, "i" }
};
2019-03-09 12:42:27 +01:00
2018-04-14 14:09:06 +02:00
construct {
2020-05-29 14:19:35 +02:00
application_id = Build.DOMAIN;
2018-04-14 14:09:06 +02:00
flags = ApplicationFlags.FLAGS_NONE;
}
2019-03-09 12:42:27 +01:00
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"};
2018-04-14 14:09:06 +02:00
public static int main (string[] args) {
2018-05-03 10:56:04 +02:00
Gtk.init (ref args);
2018-10-27 10:55:40 +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);
}
2019-03-09 12:42:27 +01:00
2018-04-14 14:09:06 +02:00
app = new Application ();
2018-05-31 14:13:21 +02:00
return app.run (args);
}
2019-03-09 12:42:27 +01:00
2018-05-31 14:13:21 +02:00
protected override void startup () {
base.startup ();
2020-05-31 14:47:12 +02:00
Build.print_info ();
2020-07-06 19:04:01 +02:00
Hdy.init ();
2019-03-09 12:42:27 +01:00
2018-06-17 09:48:18 +02:00
settings = new Settings ();
2020-05-29 14:19:35 +02:00
streams = new Streams ();
2018-06-17 09:48:18 +02:00
accounts = new Accounts ();
network = new Network ();
2020-05-29 14:19:35 +02:00
cache = new Cache ();
2018-05-27 18:25:16 +02:00
accounts.init ();
2019-03-09 12:42:27 +01:00
2018-05-27 18:25:16 +02:00
app.error.connect (app.on_error);
2019-03-09 12:42:27 +01:00
2018-05-08 18:09:38 +02:00
window_dummy = new Window ();
add_window (window_dummy);
2020-05-29 14:19:35 +02:00
set_accels_for_action ("app.compose", ACCEL_NEW_POST);
2019-03-09 12:42:27 +01:00
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);
2018-10-23 12:22:43 +02:00
add_action_entries (app_entries, this);
2018-04-14 14:09:06 +02:00
}
2019-03-09 12:42:27 +01:00
2018-04-14 14:09:06 +02:00
protected override void activate () {
2018-10-27 10:55:40 +02:00
if (window != null) {
window.present ();
return;
}
2019-03-09 12:42:27 +01:00
2018-10-27 10:55:40 +02:00
if (start_hidden) {
start_hidden = false;
2018-05-31 14:13:21 +02:00
return;
2018-10-27 10:55:40 +02:00
}
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
info ("Creating new window");
window = new Dialogs.MainWindow (this);
window.present ();
2018-04-14 14:09:06 +02:00
}
2019-03-09 12:42:27 +01:00
2018-05-27 18:25:16 +02:00
protected void on_error (string title, string msg){
var message_dialog = new Granite.MessageDialog.with_image_from_icon_name (title, msg, "dialog-warning");
message_dialog.transient_for = window;
message_dialog.run ();
message_dialog.destroy ();
}
2020-05-31 14:47:12 +02:00
void compose_activated () {
2020-05-29 14:19:35 +02:00
new Dialogs.Compose ();
}
2020-05-31 14:47:12 +02:00
void back_activated () {
window.back ();
}
2020-05-31 14:47:12 +02:00
void refresh_activated () {
refresh ();
}
2020-05-31 14:47:12 +02:00
void switch_timeline_activated (SimpleAction a, Variant? v) {
2020-05-31 12:28:35 +02:00
int32 num = v.get_int32 ();
window.switch_timeline (num);
}
2019-03-09 12:42:27 +01:00
2018-04-14 14:09:06 +02:00
}
}