tootle-linux-client/src/Dialogs/Compose.vala

165 lines
4.8 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
2020-05-29 14:19:35 +02:00
[GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/dialogs/compose.ui")]
public class Tootle.Dialogs.Compose : Window {
public API.Status? status { get; construct set; }
public string style_class { get; construct set; }
public string label { get; construct set; }
public int char_limit {
get {
return 250;
2018-10-24 14:01:32 +02:00
}
2020-05-29 14:19:35 +02:00
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
[GtkChild]
protected Box box;
[GtkChild]
protected Revealer cw_revealer;
[GtkChild]
protected ToggleButton cw_button;
[GtkChild]
protected Entry cw;
[GtkChild]
protected Label counter;
[GtkChild]
protected MenuButton visibility_button;
[GtkChild]
protected Image visibility_icon;
protected Widgets.VisibilityPopover visibility_popover;
[GtkChild]
protected Button post_button;
[GtkChild]
protected TextView content;
construct {
transient_for = window;
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
post_button.label = label;
foreach (Widget w in new Widget[] { visibility_button, post_button })
w.get_style_context ().add_class (style_class);
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
visibility_popover = new Widgets.VisibilityPopover.with_button (visibility_button);
visibility_popover.bind_property ("selected", visibility_icon, "icon-name", BindingFlags.SYNC_CREATE, (b, src, ref target) => {
target.set_string (((API.Visibility)src).get_icon ());
return true;
});
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
cw_button.bind_property ("active", cw_revealer, "reveal_child", BindingFlags.SYNC_CREATE);
cw_button.toggled.connect (validate);
cw.buffer.deleted_text.connect (() => validate ());
cw.buffer.inserted_text.connect (() => validate ());
content.buffer.changed.connect (validate);
post_button.clicked.connect (on_post_button_clicked);
if (status.spoiler_text != null) {
cw.text = status.spoiler_text;
cw_button.active = true;
2018-04-15 13:29:55 +02:00
}
2020-05-29 14:19:35 +02:00
content.buffer.text = Html.remove_tags (status.content);
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
show ();
2018-04-15 13:29:55 +02:00
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
public Compose () {
2020-05-30 11:21:50 +02:00
Object (
status: new API.Status.empty (),
style_class: STYLE_CLASS_SUGGESTED_ACTION,
label: _("Post")
);
2020-06-01 02:54:20 +02:00
set_visibility (status.visibility);
2020-05-29 14:19:35 +02:00
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
public Compose.redraft (API.Status status) {
2020-05-30 11:21:50 +02:00
Object (
status: status,
style_class: STYLE_CLASS_DESTRUCTIVE_ACTION,
label: _("Redraft")
);
2020-06-01 02:54:20 +02:00
set_visibility (status.visibility);
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
2020-06-01 02:54:20 +02:00
public Compose.reply (API.Status to) {
2020-05-29 14:19:35 +02:00
var template = new API.Status.empty ();
2020-06-01 02:54:20 +02:00
template.in_reply_to_id = to.id.to_string ();
template.in_reply_to_account_id = to.account.id.to_string ();
template.content = to.formal.get_reply_mentions ();
2020-05-30 11:21:50 +02:00
Object (
status: template,
style_class: STYLE_CLASS_SUGGESTED_ACTION,
label: _("Reply")
);
2020-06-01 02:54:20 +02:00
set_visibility (to.visibility);
2020-05-29 14:19:35 +02:00
}
2019-03-07 17:16:52 +01:00
2020-06-01 02:54:20 +02:00
void set_visibility (API.Visibility v) {
visibility_popover.selected = v;
visibility_popover.invalidate ();
}
void validate () {
2020-05-29 14:19:35 +02:00
var remain = char_limit - content.buffer.get_char_count ();
if (cw_button.active)
remain -= (int)cw.buffer.length;
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
counter.label = remain.to_string ();
post_button.sensitive = remain >= 0;
visibility_button.sensitive = true;
box.sensitive = true;
}
2019-03-07 17:16:52 +01:00
2020-06-01 02:54:20 +02:00
void on_error (int32 code, string reason) { //TODO: display errors
2020-05-29 14:19:35 +02:00
warning (reason);
validate ();
2018-04-24 16:50:38 +02:00
}
2019-03-07 17:16:52 +01:00
2020-06-01 02:54:20 +02:00
void on_post_button_clicked () {
2020-05-29 14:19:35 +02:00
post_button.sensitive = false;
visibility_button.sensitive = false;
box.sensitive = false;
2019-03-07 17:16:52 +01:00
2020-06-20 12:04:58 +02:00
if (status.id != "") {
2020-05-29 14:19:35 +02:00
info ("Removing old status...");
status.poof (publish, on_error);
}
else {
publish ();
2018-10-24 14:01:32 +02:00
}
}
2019-03-07 17:16:52 +01:00
2020-06-01 02:54:20 +02:00
void publish () {
2020-05-29 14:19:35 +02:00
info ("Publishing new status...");
status.content = content.buffer.text;
status.spoiler_text = cw.text;
var req = new Request.POST ("/api/v1/statuses")
2020-05-30 11:21:50 +02:00
.with_account (accounts.active)
2020-05-29 14:19:35 +02:00
.with_param ("visibility", visibility_popover.selected.to_string ())
.with_param ("status", Html.uri_encode (status.content));
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
if (cw_button.active) {
req.with_param ("sensitive", "true");
req.with_param ("spoiler_text", Html.uri_encode (cw.text));
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
if (status.in_reply_to_id != null)
req.with_param ("in_reply_to_id", status.in_reply_to_id);
if (status.in_reply_to_account_id != null)
req.with_param ("in_reply_to_account_id", status.in_reply_to_account_id);
req.then ((sess, mess) => {
2020-06-20 12:04:58 +02:00
var node = network.parse_node (mess);
var status = API.Status.from (node);
2020-05-29 14:19:35 +02:00
info ("OK: status id is %s", status.id.to_string ());
2019-03-14 12:55:27 +01:00
destroy ();
2020-05-29 14:19:35 +02:00
})
.on_error (on_error)
.exec ();
2018-10-24 14:01:32 +02:00
}
2018-04-14 14:09:06 +02:00
}