From 8fc5960eef3d203bc1ef8321192bb4c9d36c00f1 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Mon, 5 Feb 2018 19:48:16 +0100 Subject: [PATCH] improved icon fetching --- .vscode/launch.json | 15 +++++++++++++++ src/Widgets/Views/Editor.vala | 20 +++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6e73aa3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Verwendet IntelliSense zum Ermitteln möglicher Attribute. + // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. + // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug", + "type": "gdb", + "request": "launch", + "target": "./bin/executable", + "cwd": "${workspaceRoot}" + } + ] +} \ No newline at end of file diff --git a/src/Widgets/Views/Editor.vala b/src/Widgets/Views/Editor.vala index 1101b0f..feec357 100644 --- a/src/Widgets/Views/Editor.vala +++ b/src/Widgets/Views/Editor.vala @@ -213,6 +213,11 @@ namespace Webpin.Widgets.Views { grab_timer = Timeout.add ( 500, () => { + if (tmp_icon_file != "" ){ + FileUtils.remove (tmp_icon_file); + tmp_icon_file = ""; + } + var url = app_url_entry.text; var session = new Soup.Session.with_options ("user_agent", "WebPin/0.1.0 (https://github.com/artemanufrij/webpin)"); session.timeout = 2; @@ -334,14 +339,23 @@ namespace Webpin.Widgets.Views { session.send_message (msg); if (msg.status_code == 200) { tmp_icon_ext = ".png"; - if (url.has_suffix (".svg")) { tmp_icon_ext = ".svg"; } tmp_icon_file = GLib.Path.build_filename (WebpinApp.instance.CACHE_FOLDER, Random.next_int ().to_string () + tmp_icon_ext); - var fs = FileStream.open (tmp_icon_file, "w"); - fs.write (msg.response_body.data, (size_t)msg.response_body.length); + + var s_file = File.new_for_uri (url); + var d_file = File.new_for_path (tmp_icon_file); + + if (s_file.copy (d_file, FileCopyFlags.OVERWRITE) && tmp_icon_ext != ".svg") { + var pixbuf = new Gdk.Pixbuf.from_file (tmp_icon_file); + if (pixbuf.width < 48 || pixbuf.height < 48) { + FileUtils.remove (tmp_icon_file); + tmp_icon_file = ""; + tmp_icon_ext = ""; + } + } } msg.dispose (); session.dispose ();