split Utils module into Desktop and Html ;)

This commit is contained in:
martensitingale 2018-05-30 19:49:16 +00:00
parent f12240a364
commit 1b8ef062e4
12 changed files with 64 additions and 60 deletions

View file

@ -19,12 +19,13 @@ executable(
meson.project_name(), meson.project_name(),
asresources, asresources,
'src/Application.vala', 'src/Application.vala',
'src/Desktop.vala',
'src/Html.vala',
'src/MainWindow.vala', 'src/MainWindow.vala',
'src/SettingsManager.vala', 'src/SettingsManager.vala',
'src/AccountManager.vala', 'src/AccountManager.vala',
'src/ImageCache.vala', 'src/ImageCache.vala',
'src/NetManager.vala', 'src/NetManager.vala',
'src/Utils.vala',
'src/Notificator.vala', 'src/Notificator.vala',
'src/InstanceAccount.vala', 'src/InstanceAccount.vala',
'src/API/Account.vala', 'src/API/Account.vala',

View file

@ -1,9 +1,10 @@
src/Application.vala src/Application.vala
src/Desktop.vala
src/Html.vala
src/MainWindow.vala src/MainWindow.vala
src/SettingsManager.vala src/SettingsManager.vala
src/AccountManager.vala src/AccountManager.vala
src/NetManager.vala src/NetManager.vala
src/Utils.vala
src/API/StatusVisibility.vala src/API/StatusVisibility.vala
src/API/NotificationType.vala src/API/NotificationType.vala
src/API/Attachment.vala src/API/Attachment.vala

View file

@ -39,13 +39,13 @@ public class Tootle.Status {
status.created_at = obj.get_string_member ("created_at"); status.created_at = obj.get_string_member ("created_at");
status.reblogs_count = obj.get_int_member ("reblogs_count"); status.reblogs_count = obj.get_int_member ("reblogs_count");
status.favourites_count = obj.get_int_member ("favourites_count"); status.favourites_count = obj.get_int_member ("favourites_count");
status.content = Utils.simplify_html ( obj.get_string_member ("content")); status.content = Html.simplify ( obj.get_string_member ("content"));
status.sensitive = obj.get_boolean_member ("sensitive"); status.sensitive = obj.get_boolean_member ("sensitive");
status.visibility = StatusVisibility.from_string (obj.get_string_member ("visibility")); status.visibility = StatusVisibility.from_string (obj.get_string_member ("visibility"));
var spoiler = obj.get_string_member ("spoiler_text"); var spoiler = obj.get_string_member ("spoiler_text");
if (spoiler != "") if (spoiler != "")
status.spoiler_text = Utils.simplify_html (spoiler); status.spoiler_text = Html.simplify (spoiler);
if (obj.has_member ("reblogged")) if (obj.has_member ("reblogged"))
status.reblogged = obj.get_boolean_member ("reblogged"); status.reblogged = obj.get_boolean_member ("reblogged");

View file

@ -1,42 +1,10 @@
public class Tootle.Utils{ public class Tootle.Desktop {
// open a URI in the user's default browser
public static void open_url (string url) { public static void open_url (string url) {
Gtk.show_uri (null, url, Gdk.CURRENT_TIME); Gtk.show_uri (null, url, Gdk.CURRENT_TIME);
} }
public static string escape_html (string content) { // copy a string to the clipboard
var all_tags = new Regex("<(.|\n)*?>", RegexCompileFlags.CASELESS);
return all_tags.replace(content, -1, 0, "");
}
public static string simplify_html (string content) {
var divided = content
.replace("<br>", "\n")
.replace("</br>", "")
.replace("<br />", "\n")
.replace("<p>", "")
.replace("</p>", "\n\n");
var html_params = new Regex("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
var simplified = html_params.replace(divided, -1, 0, "");
while (simplified.has_suffix ("\n"))
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
return simplified;
}
public static string escape_entities (string content) {
return content
.replace ("&", "&amp;")
.replace ("'", "&apos;");
}
public static string encode (string content) {
var to_escape = ";&+";
return Soup.URI.encode (content, to_escape);
}
public static void copy (string str) { public static void copy (string str) {
var display = Tootle.window.get_display (); var display = Tootle.window.get_display ();
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD); var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
@ -45,8 +13,9 @@ public class Tootle.Utils{
.replace ("&apos;", "'"); .replace ("&apos;", "'");
clipboard.set_text (normalized, -1); clipboard.set_text (normalized, -1);
} }
public static void download (string url) { // download a file from the web to a user's configured Downloads folder
public static void download_file (string url) {
debug ("Downloading file: %s", url); debug ("Downloading file: %s", url);
var i = url.last_index_of ("/"); var i = url.last_index_of ("/");
@ -78,5 +47,4 @@ public class Tootle.Utils{
}); });
Tootle.network.queue (msg); Tootle.network.queue (msg);
} }
} }

View file

@ -176,14 +176,14 @@ public class Tootle.PostDialog : Gtk.Dialog {
} }
public void publish_post () { public void publish_post () {
var pars = "?status=%s&visibility=%s".printf (Utils.encode (text.buffer.text), visibility_opt.to_string ()); var pars = "?status=%s&visibility=%s".printf (Html.uri_encode (text.buffer.text), visibility_opt.to_string ());
pars += attachments.get_uri_array (); pars += attachments.get_uri_array ();
if (in_reply_to != null) if (in_reply_to != null)
pars += "&in_reply_to_id=%s".printf (in_reply_to.id.to_string ()); pars += "&in_reply_to_id=%s".printf (in_reply_to.id.to_string ());
if (spoiler.active) { if (spoiler.active) {
pars += "&sensitive=true"; pars += "&sensitive=true";
pars += "&spoiler_text=" + Utils.encode (spoiler_text.buffer.text); pars += "&spoiler_text=" + Html.uri_encode (spoiler_text.buffer.text);
} }
var url = "%s/api/v1/statuses%s".printf (Tootle.accounts.formal.instance, pars); var url = "%s/api/v1/statuses%s".printf (Tootle.accounts.formal.instance, pars);

34
src/Html.vala Normal file
View file

@ -0,0 +1,34 @@
public class Tootle.Html {
public static string remove_tags (string content) {
var all_tags = new Regex("<(.|\n)*?>", RegexCompileFlags.CASELESS);
return all_tags.replace(content, -1, 0, "");
}
public static string simplify (string content) {
var divided = content
.replace("<br>", "\n")
.replace("</br>", "")
.replace("<br />", "\n")
.replace("<p>", "")
.replace("</p>", "\n\n");
var html_params = new Regex("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
var simplified = html_params.replace(divided, -1, 0, "");
while (simplified.has_suffix ("\n"))
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
return simplified;
}
public static string escape_entities (string content) {
return content
.replace ("&", "&amp;")
.replace ("'", "&apos;");
}
public static string uri_encode (string content) {
var to_escape = ";&+";
return Soup.URI.encode (content, to_escape);
}
}

View file

@ -70,13 +70,13 @@ public class Tootle.InstanceAccount : GLib.Object {
} }
private void notification (ref Notification obj) { private void notification (ref Notification obj) {
var title = Utils.escape_html (obj.type.get_desc (obj.account)); var title = Html.remove_tags (obj.type.get_desc (obj.account));
var notification = new GLib.Notification (title); var notification = new GLib.Notification (title);
if (obj.status != null) { if (obj.status != null) {
var body = ""; var body = "";
body += get_pretty_instance (); body += get_pretty_instance ();
body += "\n"; body += "\n";
body += Utils.escape_html (obj.status.content); body += Html.remove_tags (obj.status.content);
notification.set_body (body); notification.set_body (body);
} }

View file

@ -141,9 +141,9 @@ public class Tootle.AccountView : TimelineView {
public void rebind (){ public void rebind (){
display_name.label = "<b>%s</b>".printf (Utils.escape_entities(account.display_name)); display_name.label = "<b>%s</b>".printf (Html.escape_entities(account.display_name));
username.label = "@" + account.acct; username.label = "@" + account.acct;
note.label = Utils.simplify_html (account.note); note.label = Html.simplify (account.note);
button_follow.visible = !account.is_self (); button_follow.visible = !account.is_self ();
Tootle.network.load_avatar (account.avatar, avatar, 128); Tootle.network.load_avatar (account.avatar, avatar, 128);

View file

@ -26,9 +26,9 @@ public class Tootle.AccountWidget : StatusWidget {
}); });
var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser")); var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser"));
item_open_link.activate.connect (() => Utils.open_url (status.url)); item_open_link.activate.connect (() => Desktop.open_url (status.url));
var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link")); var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link"));
item_copy_link.activate.connect (() => Utils.copy (status.url)); item_copy_link.activate.connect (() => Desktop.copy (status.url));
menu.add (item_open_link); menu.add (item_open_link);
menu.add (item_copy_link); menu.add (item_copy_link);

View file

@ -108,7 +108,7 @@ public class Tootle.AttachmentWidget : Gtk.EventBox {
if (ev.button == 3) if (ev.button == 3)
return open_menu (ev.button, ev.time); return open_menu (ev.button, ev.time);
Tootle.Utils.open_url (attachment.url); Desktop.open_url (attachment.url);
return true; return true;
} }
@ -120,11 +120,11 @@ public class Tootle.AttachmentWidget : Gtk.EventBox {
}); });
var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser")); var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser"));
item_open_link.activate.connect (() => Utils.open_url (attachment.url)); item_open_link.activate.connect (() => Desktop.open_url (attachment.url));
var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link")); var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link"));
item_copy_link.activate.connect (() => Utils.copy (attachment.url)); item_copy_link.activate.connect (() => Desktop.copy (attachment.url));
var item_download = new Gtk.MenuItem.with_label (_("Download")); var item_download = new Gtk.MenuItem.with_label (_("Download"));
item_download.activate.connect (() => Utils.download (attachment.url)); item_download.activate.connect (() => Desktop.download_file (attachment.url));
menu.add (item_open_link); menu.add (item_open_link);
if (attachment.type != "unknown") if (attachment.type != "unknown")
menu.add (item_download); menu.add (item_download);

View file

@ -49,7 +49,7 @@ public class Tootle.RichLabel : Gtk.Label {
return true; return true;
} }
Tootle.Utils.open_url (url); Desktop.open_url (url);
return true; return true;
} }

View file

@ -200,7 +200,7 @@ public class Tootle.StatusWidget : Gtk.EventBox {
public void rebind () { public void rebind () {
var formal = status.get_formal (); var formal = status.get_formal ();
title_user.label = "<b>%s</b>".printf (Utils.escape_entities (formal.account.display_name)); title_user.label = "<b>%s</b>".printf (Html.escape_entities (formal.account.display_name));
title_acct.label = "@" + formal.account.acct; title_acct.label = "@" + formal.account.acct;
content_label.label = formal.content; content_label.label = formal.content;
content_label.mentions = formal.mentions; content_label.mentions = formal.mentions;
@ -272,13 +272,13 @@ public class Tootle.StatusWidget : Gtk.EventBox {
var item_delete = new Gtk.MenuItem.with_label (_("Delete")); var item_delete = new Gtk.MenuItem.with_label (_("Delete"));
item_delete.activate.connect (() => status.poof ()); item_delete.activate.connect (() => status.poof ());
var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser")); var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser"));
item_open_link.activate.connect (() => Utils.open_url (status.url)); item_open_link.activate.connect (() => Desktop.open_url (status.url));
var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link")); var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link"));
item_copy_link.activate.connect (() => Utils.copy (status.url)); item_copy_link.activate.connect (() => Desktop.copy (status.url));
var item_copy = new Gtk.MenuItem.with_label (_("Copy Text")); var item_copy = new Gtk.MenuItem.with_label (_("Copy Text"));
item_copy.activate.connect (() => { item_copy.activate.connect (() => {
var sanitized = Utils.escape_html (status.content); var sanitized = Html.remove_tags (status.content);
Utils.copy (sanitized); Desktop.copy (sanitized);
}); });
if (this.status.is_owned ()) { if (this.status.is_owned ()) {