2018-04-25 13:16:57 +00:00
|
|
|
public class Tootle.Utils{
|
|
|
|
|
2018-05-04 20:57:31 +00:00
|
|
|
public static void open_url (string url) {
|
|
|
|
Gtk.show_uri (null, url, Gdk.CURRENT_TIME);
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:29:01 +00:00
|
|
|
public static string escape_html (string content) {
|
|
|
|
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
|
2018-04-25 13:16:57 +00:00
|
|
|
.replace("<br>", "\n")
|
|
|
|
.replace("</br>", "")
|
|
|
|
.replace("<br />", "\n")
|
|
|
|
.replace("<p>", "")
|
2018-05-17 17:29:01 +00:00
|
|
|
.replace("</p>", "\n\n");
|
|
|
|
|
|
|
|
var html_params = new Regex("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
|
|
|
|
var simplified = html_params.replace(divided, -1, 0, "");
|
2018-04-25 13:16:57 +00:00
|
|
|
|
2018-05-17 17:29:01 +00:00
|
|
|
while (simplified.has_suffix ("\n"))
|
|
|
|
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
|
2018-04-25 13:16:57 +00:00
|
|
|
|
2018-05-17 17:29:01 +00:00
|
|
|
return simplified;
|
2018-04-25 13:16:57 +00:00
|
|
|
}
|
2018-05-21 10:40:49 +00:00
|
|
|
|
|
|
|
public static string escape_entities (string content) {
|
2018-05-21 15:23:31 +00:00
|
|
|
return content
|
|
|
|
.replace ("&", "&")
|
|
|
|
.replace ("'", "'");
|
|
|
|
}
|
2018-06-01 13:03:37 +00:00
|
|
|
|
|
|
|
public static string encode (string content) {
|
2018-06-01 17:08:00 +00:00
|
|
|
var to_escape = ";&+";
|
2018-06-01 13:03:37 +00:00
|
|
|
return Soup.URI.encode (content, to_escape);
|
|
|
|
}
|
2018-05-21 15:23:31 +00:00
|
|
|
|
|
|
|
public static void copy (string str) {
|
|
|
|
var display = Tootle.window.get_display ();
|
|
|
|
var clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
|
|
|
|
var normalized = str
|
|
|
|
.replace ("&", "&")
|
|
|
|
.replace ("'", "'");
|
|
|
|
clipboard.set_text (normalized, -1);
|
2018-05-21 10:40:49 +00:00
|
|
|
}
|
2018-05-21 16:13:49 +00:00
|
|
|
|
|
|
|
public static void download (string url) {
|
|
|
|
debug ("Downloading file: %s", url);
|
|
|
|
|
|
|
|
var i = url.last_index_of ("/");
|
|
|
|
var name = url.substring (i + 1, url.length - i - 1);
|
|
|
|
if (name == null)
|
|
|
|
name = "unknown";
|
|
|
|
|
|
|
|
var dir_path = "%s/%s".printf (GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD), Tootle.app.program_name);
|
|
|
|
var file_path = "%s/%s".printf (dir_path, name);
|
|
|
|
|
|
|
|
var msg = new Soup.Message("GET", url);
|
|
|
|
msg.finished.connect(() => {
|
|
|
|
try {
|
|
|
|
var dir = File.new_for_path (dir_path);
|
|
|
|
if (!dir.query_exists ())
|
|
|
|
dir.make_directory ();
|
|
|
|
|
|
|
|
var file = File.new_for_path (file_path);
|
|
|
|
if (!file.query_exists ()) {
|
|
|
|
var data = msg.response_body.data;
|
|
|
|
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE);
|
|
|
|
stream.write (data);
|
|
|
|
}
|
|
|
|
Tootle.app.toast ("Media downloaded");
|
|
|
|
} catch (Error e) {
|
|
|
|
Tootle.app.toast (e.message);
|
|
|
|
warning ("Error: %s\n", e.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Tootle.network.queue (msg);
|
|
|
|
}
|
2018-04-25 13:16:57 +00:00
|
|
|
|
|
|
|
}
|