Skip to content

Commit cc0f9c9

Browse files
teamconsdar5hak
andauthored
Autostart (#94)
* Proper autostart capabilities via libportal * Make the linter happy * Add option to request autostart manually * Linter * fix build * hmpf * hmpf 2.0 * back to hmpf 1.0 * Avoid being annoying * fix * Tested libportal * Use boolean directly * Extract repeated statement out of if/else block * Add the permission * Segregate the sections of autostart command --------- Co-authored-by: Darshak Parikh <[email protected]>
1 parent 981a12e commit cc0f9c9

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

com.github.elfenware.badger.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ finish-args:
1313
- '--socket=wayland'
1414
# Allow Badger to send reminders
1515
- '--socket=session-bus'
16+
# Allow Badger to autostart and keep timers in the background
17+
- '--talk-name=org.freedesktop.portal.Background'
1618

1719
cleanup:
1820
- '/include'

data/com.github.elfenware.badger.gschema.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,10 @@
7777
path="/com/github/elfenware/badger/state/"
7878
id="com.github.elfenware.badger.state"
7979
gettext-domain="com.github.elfenware.badger">
80+
<key name="first-run" type="b">
81+
<default>true</default>
82+
<summary>Whether this is the first time that Badger has opened</summary>
83+
<description>Used to determine whether or not to perform initial install steps</description>
84+
</key>
8085
</schema>
8186
</schemalist>

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ executable(
4040
dependency ('glib-2.0'),
4141
dependency('granite-7'),
4242
dependency('gobject-2.0'),
43-
dependency('gtk4')
43+
dependency('gtk4'),
44+
dependency('libportal')
4445
],
4546
install : true
4647
)

src/Application.vala

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
public class Badger.Application : Gtk.Application {
2222
public bool headless = false;
23+
public bool ask_autostart = false;
2324

2425
private Badger.MainWindow window;
2526

@@ -40,6 +41,7 @@ public class Badger.Application : Gtk.Application {
4041
protected override void activate () {
4142
stdout.printf ("\n✔️ Activated");
4243

44+
var settings = new GLib.Settings ("com.github.elfenware.badger.state");
4345
var gtk_settings = Gtk.Settings.get_default ();
4446
var granite_settings = Granite.Settings.get_default ();
4547
stdout.printf ("\n⚙️ State settings loaded");
@@ -54,6 +56,20 @@ public class Badger.Application : Gtk.Application {
5456
);
5557
});
5658

59+
// On first run, request autostart
60+
if (settings.get_boolean ("first-run") || ask_autostart) {
61+
62+
// Show first run message only if really first run
63+
if (settings.get_boolean ("first-run")) {
64+
stdout.printf ("\n🎉️ First run");
65+
settings.set_boolean ("first-run", false);
66+
} else {
67+
ask_autostart = false;
68+
}
69+
request_autostart ();
70+
71+
}
72+
5773
if (window == null) {
5874
var reminders = set_up_reminders ();
5975
var main = new MainGrid (reminders);
@@ -77,16 +93,20 @@ public class Badger.Application : Gtk.Application {
7793
window.show ();
7894
window.present ();
7995
}
96+
headless = false;
8097
}
8198

8299
public override int command_line (ApplicationCommandLine command_line) {
83100
stdout.printf ("\n💲️ Command line mode started");
84101

85-
bool headless_mode = false;
86-
OptionEntry[] options = new OptionEntry[1];
102+
OptionEntry[] options = new OptionEntry[2];
87103
options[0] = {
88104
"headless", 0, 0, OptionArg.NONE,
89-
ref headless_mode, "Run without window", null
105+
ref headless, "Run without window", null
106+
};
107+
options[1] = {
108+
"request-autostart", 0, 0, OptionArg.NONE,
109+
ref ask_autostart, "Request autostart permission", null
90110
};
91111

92112
// We have to make an extra copy of the array, since .parse assumes
@@ -108,23 +128,33 @@ public class Badger.Application : Gtk.Application {
108128
return 0;
109129
}
110130

111-
headless = headless_mode;
112-
113131
hold ();
114132
activate ();
115133
return 0;
116134
}
117135

118136
public static int main (string[] args) {
119137
var app = new Badger.Application ();
120-
121-
if (args.length > 1 && args[1] == "--headless") {
122-
app.headless = true;
123-
}
124-
125138
return app.run (args);
126139
}
127140

141+
private static void request_autostart () {
142+
Xdp.Portal portal = new Xdp.Portal ();
143+
GenericArray<weak string> cmd = new GenericArray<weak string> ();
144+
cmd.add ("com.github.elfenware.badger");
145+
cmd.add ("--headless");
146+
147+
// TODO: Implicit .begin is deprecated but i have no idea how to fix that
148+
portal.request_background (
149+
null,
150+
"Autostart Badger in headless mode to send reminders",
151+
cmd,
152+
Xdp.BackgroundFlags.AUTOSTART,
153+
null);
154+
155+
stdout.printf ("\n🚀 Requested autostart for Badger");
156+
}
157+
128158
private Reminder[] set_up_reminders () {
129159
Reminder[] reminders = new Reminder[8];
130160
reminders[0] = new Reminder (

0 commit comments

Comments
 (0)