move fetching icon into a separate thread

This commit is contained in:
Artem Anufrij 2018-02-11 20:15:11 +01:00
parent 4cd8bfb869
commit 3595041e99

View file

@ -213,102 +213,117 @@ namespace Webpin.Widgets.Views {
grab_timer = Timeout.add ( grab_timer = Timeout.add (
500, 500,
() => { () => {
if (tmp_icon_file != "" ) { new Thread<void*> (
FileUtils.remove (tmp_icon_file); "grab_color_and_icon",
tmp_icon_file = ""; () => {
} if (tmp_icon_file != "" ) {
FileUtils.remove (tmp_icon_file);
var url = app_url_entry.text; tmp_icon_file = "";
var session = new Soup.Session.with_options ("user_agent", "WebPin/0.1.0 (https://github.com/artemanufrij/webpin)");
session.timeout = 2;
var msg = new Soup.Message ("GET", url);
session.send_message (msg);
if (msg.status_code == 200) {
var body = (string)msg.response_body.data;
Regex regex = null;
try {
regex = new Regex ("(?<=<meta name=\"theme-color\" content=\")#[0-9a-fA-F]{6}");
} catch (Error err) {
warning (err.message);
}
MatchInfo match_info = null;
if (regex != null && regex.match (body, 0, out match_info)) {
var result = match_info.fetch (0);
Gdk.RGBA return_value = {0, 0, 0, 1};
if (return_value.parse (result)) {
primary_color_button.set_rgba (return_value);
} }
}
if (tmp_icon_file == "") { var url = app_url_entry.text;
try { var session = new Soup.Session.with_options ("user_agent", "WebPin/0.1.0 (https://github.com/artemanufrij/webpin)");
regex = new Regex ("(?<=\"fluid-icon\" href=\")[/\\w\\.:\\-]*"); session.timeout = 2;
if (regex.match (body, 0, out match_info)) { var msg = new Soup.Message ("GET", url);
var icon_path = format_icon_path (url, match_info.fetch (0)); session.send_message (msg);
download_icon (icon_path);
if (msg.status_code == 200) {
var body = (string)msg.response_body.data;
Regex regex = null;
try {
regex = new Regex ("(?<=<meta name=\"theme-color\" content=\")#[0-9a-fA-F]{6}");
} catch (Error err) {
warning (err.message);
} }
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file == "") { MatchInfo match_info = null;
try { if (regex != null && regex.match (body, 0, out match_info)) {
regex = new Regex ("(rel=\"icon\").*href=\"([\\-/\\w]*64.png)"); var result = match_info.fetch (0);
if (regex.match (body, 0, out match_info)) { Gdk.RGBA return_value = {0, 0, 0, 1};
var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1)); if (return_value.parse (result)) {
download_icon (icon_path); Idle.add (
() => {
primary_color_button.set_rgba (return_value);
return false;
});
}
} }
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file == "") { if (tmp_icon_file == "") {
try { try {
regex = new Regex ("(rel=\"icon\").*href=\"([\\-/\\w]*96.png)"); regex = new Regex ("(?<=\"fluid-icon\" href=\")[/\\w\\.:\\-]*");
if (regex.match (body, 0, out match_info)) { if (regex.match (body, 0, out match_info)) {
var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1)); var icon_path = format_icon_path (url, match_info.fetch (0));
download_icon (icon_path); download_icon (icon_path);
}
} catch (Error err) {
warning (err.message);
}
} }
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file == "") { if (tmp_icon_file == "") {
try { try {
regex = new Regex ("(\"apple-touch-icon\").*href=\"([\\-/\\w]*.png)"); regex = new Regex ("(rel=\"icon\").*href=\"([\\-/\\w]*64.png)");
if (regex.match (body, 0, out match_info)) { if (regex.match (body, 0, out match_info)) {
var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1)); var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1));
download_icon (icon_path); download_icon (icon_path);
}
} catch (Error err) {
warning (err.message);
}
} }
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file == "") { if (tmp_icon_file == "") {
try { try {
regex = new Regex ("(?<=\"mask-icon\" href=\")[/\\w\\.:\\-]*"); regex = new Regex ("(rel=\"icon\").*href=\"([\\-/\\w]*96.png)");
if (regex.match (body, 0, out match_info)) { if (regex.match (body, 0, out match_info)) {
var icon_path = format_icon_path (url, match_info.fetch (0)); var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1));
download_icon (icon_path); download_icon (icon_path);
}
} catch (Error err) {
warning (err.message);
}
} }
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file != "") { if (tmp_icon_file == "") {
icon_name_entry.set_text (tmp_icon_file); try {
} regex = new Regex ("(\"apple-touch-icon\").*href=\"([\\-/\\w]*.png)");
} if (regex.match (body, 0, out match_info)) {
msg.dispose (); var icon_path = format_icon_path (url, match_info.fetch (match_info.get_match_count () - 1));
session.dispose (); download_icon (icon_path);
}
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file == "") {
try {
regex = new Regex ("(?<=\"mask-icon\" href=\")[/\\w\\.:\\-]*");
if (regex.match (body, 0, out match_info)) {
var icon_path = format_icon_path (url, match_info.fetch (0));
download_icon (icon_path);
}
} catch (Error err) {
warning (err.message);
}
}
if (tmp_icon_file != "") {
Idle.add (
() => {
icon_name_entry.set_text (tmp_icon_file);
return false;
});
}
}
msg.dispose ();
session.dispose ();
return null;
});
reset_grab_color_and_icon (); reset_grab_color_and_icon ();
return false; return false;