Tooth/src/Application.vala

147 lines
4.9 KiB
Vala
Raw Normal View History

2018-04-14 12:09:06 +00:00
using Gtk;
using Granite;
2018-10-23 10:05:24 +00:00
namespace Tootle {
2018-04-14 12:09:06 +00:00
public static Application app;
2019-03-11 14:14:37 +00:00
public static Dialogs.MainWindow? window;
2018-05-08 16:09:38 +00:00
public static Window window_dummy;
2019-03-09 11:42:27 +00:00
2018-06-17 07:48:18 +00:00
public static Settings settings;
public static Accounts accounts;
public static Network network;
public static ImageCache image_cache;
2018-07-14 08:37:41 +00:00
public static Watchlist watchlist;
2018-04-14 12:09:06 +00:00
2018-10-27 08:55:40 +00:00
public static bool start_hidden = false;
2018-04-14 12:09:06 +00:00
public class Application : Granite.Application {
2019-03-09 11:42:27 +00:00
2018-05-05 15:38:01 +00:00
public abstract signal void refresh ();
public abstract signal void toast (string title);
public abstract signal void error (string title, string text);
2018-10-27 08:55:40 +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 = {
{"compose-toot", compose_toot_activated },
2019-03-12 08:12:53 +00:00
{"toggle-reveal", on_sensitive_toggled },
{"back", back_activated },
{"refresh", refresh_activated },
{"switch-timeline", switch_timeline_activated, "i" }
};
2019-03-09 11:42:27 +00:00
2018-04-14 12:09:06 +00:00
construct {
application_id = "com.github.bleakgrey.tootle";
flags = ApplicationFlags.FLAGS_NONE;
2018-05-03 08:56:04 +00:00
program_name = "Tootle";
2018-10-31 12:36:56 +00:00
build_version = "0.2.0";
2018-04-14 12:09:06 +00:00
}
2019-03-09 11:42:27 +00:00
public string[] ACCEL_NEW_POST = {"<Ctrl>T"};
2019-03-12 08:12:53 +00:00
public string[] ACCEL_TOGGLE_REVEAL = {"<Ctrl>S"};
2019-03-09 11:42:27 +00:00
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 12:09:06 +00:00
public static int main (string[] args) {
2018-05-03 08:56:04 +00:00
Gtk.init (ref args);
2019-03-09 11:42:27 +00:00
2018-10-27 08:55:40 +00: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 11:42:27 +00:00
2018-04-14 12:09:06 +00:00
app = new Application ();
2018-05-31 12:13:21 +00:00
return app.run (args);
}
2019-03-09 11:42:27 +00:00
2018-05-31 12:13:21 +00:00
protected override void startup () {
base.startup ();
2018-06-19 10:51:04 +00:00
Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.INFO;
2019-03-09 11:42:27 +00:00
2018-06-17 07:48:18 +00:00
settings = new Settings ();
accounts = new Accounts ();
network = new Network ();
image_cache = new ImageCache ();
2018-07-14 08:37:41 +00:00
watchlist = new Watchlist ();
2018-05-27 16:25:16 +00:00
accounts.init ();
2019-03-09 11:42:27 +00:00
2018-05-27 16:25:16 +00:00
app.error.connect (app.on_error);
2019-03-09 11:42:27 +00:00
2018-05-08 16:09:38 +00:00
window_dummy = new Window ();
add_window (window_dummy);
2019-03-09 11:42:27 +00:00
set_accels_for_action ("app.compose-toot", ACCEL_NEW_POST);
2019-03-12 08:12:53 +00:00
set_accels_for_action ("app.toggle-reveal", ACCEL_TOGGLE_REVEAL);
2019-03-09 11:42:27 +00: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 10:22:43 +00:00
add_action_entries (app_entries, this);
2018-04-14 12:09:06 +00:00
}
2019-03-09 11:42:27 +00:00
2018-04-14 12:09:06 +00:00
protected override void activate () {
2018-10-27 08:55:40 +00:00
if (window != null) {
window.present ();
return;
}
2019-03-09 11:42:27 +00:00
2018-10-27 08:55:40 +00:00
if (start_hidden) {
start_hidden = false;
2018-05-31 12:13:21 +00:00
return;
2018-10-27 08:55:40 +00:00
}
2019-03-09 11:42:27 +00:00
2018-05-31 12:13:21 +00:00
debug ("Creating new window");
if (accounts.is_empty ())
2019-03-11 14:14:37 +00:00
Dialogs.NewAccount.open ();
2018-05-03 08:56:04 +00:00
else {
2019-03-11 14:14:37 +00:00
window = new Dialogs.MainWindow (this);
2018-05-31 12:13:21 +00:00
window.present ();
2018-05-03 08:56:04 +00:00
}
2018-04-14 12:09:06 +00:00
}
2019-03-09 11:42:27 +00:00
2018-05-27 16:25:16 +00: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 ();
}
2019-03-12 08:12:53 +00:00
private void on_sensitive_toggled () {
window.button_reveal.clicked ();
}
private void compose_toot_activated () {
2019-03-11 14:14:37 +00:00
Dialogs.Compose.open ();
}
private void back_activated () {
window.back ();
}
private void refresh_activated () {
refresh ();
}
private void switch_timeline_activated (SimpleAction a, Variant? parameter) {
int32 timeline_no = parameter.get_int32 ();
window.switch_timeline (timeline_no);
}
2019-03-09 11:42:27 +00:00
2018-04-14 12:09:06 +00:00
}
}