Skip to content

Commit

Permalink
Load more templates from external server (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Scott committed Aug 22, 2018
1 parent fc62f77 commit 13e4656
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -27,6 +27,7 @@ set (PKG_DEPS
json-glib-1.0
gudev-1.0
libevdev
libsoup-2.4
)

set (VALA_DEPS
Expand All @@ -37,6 +38,7 @@ set (VALA_DEPS
gudev-1.0
libevdev
linux
libsoup-2.4
)


Expand Down
1 change: 0 additions & 1 deletion data/assets.gresource.xml
Expand Up @@ -17,7 +17,6 @@
<file alias="save-footer" compressed="false" preprocess="">SaveFooter-min.html</file>
</gresource>
<gresource prefix="/com/github/philip-scott/spice-up/templates">
<file alias="BigCity.spice" compressed="false" preprocess="">assets/templates/BigCity.spice</file>
<file alias="Black.spice" compressed="false" preprocess="">assets/templates/Black.spice</file>
<file alias="Colorful.spice" compressed="false" preprocess="">assets/templates/Colorful.spice</file>
<file alias="Green.spice" compressed="false" preprocess="">assets/templates/Green.spice</file>
Expand Down
1 change: 0 additions & 1 deletion data/assets/templates/BigCity.spice

This file was deleted.

6 changes: 6 additions & 0 deletions schemas/com.github.philip-scott.spice-up.gschema.xml
Expand Up @@ -41,5 +41,11 @@
<summary>Controler configuration for presentation mode</summary>
<description>In JSON format</description>
</key>

<key name="last-fetch" type="s">
<default>"0"</default>
<summary>Last time it fetched templates from server</summary>
<description>Stored to fetch templates only once every day</description>
</key>
</schema>
</schemalist>
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -17,6 +17,7 @@ vala_precompile(VALA_C
Services/Settings.vala
Services/Slide.vala
Services/SlideManager.vala
Services/Fetcher.vala
Services/FileManager.vala
Services/HistoryManager.vala
Services/ImageHandler.vala
Expand Down
92 changes: 92 additions & 0 deletions src/Services/Fetcher.vala
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2018 Felipe Escoto (https://github.com/Philip-Scott/Spice-up)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: Felipe Escoto <felescoto95@hotmail.com>
*/

public class Spice.Services.Fetcher {
private const string TEMPLATES_URL = "https://spice-up-dev.azurewebsites.net/api/get-templates";
private const int64 CACHE_LIFE = 43200; // 1/2 a day
public const int CURRENT_VERSION = 1;

private File cache_file;
private string cache = "";
private Mutex mutex = Mutex ();

public Fetcher () {
cache_file = File.new_for_path (Environment.get_tmp_dir () + "/com.github.philip-scott.spice-up.cache.json");
}

public void fetch () {
var now = new DateTime.now_utc ().to_unix ();

var settigns = Spice.Services.Settings.get_instance ();
var previous_fetch = int64.parse (settigns.last_fetch);

var try_to_fetch = now - previous_fetch > CACHE_LIFE || !cache_file.query_exists ();

if (try_to_fetch) {
debug ("Getting templates from server\n");
new Thread<void*> ("fetch-templates", () => {
mutex.lock ();

var session = new Soup.Session ();
var message = new Soup.Message ("GET", TEMPLATES_URL);

session.send_message (message);

var data = new StringBuilder ();
foreach (var c in message.response_body.data) {
data.append ("%c".printf (c));
}

cache = data.str;
if (cache != "") {
save_to_cache (cache);
settigns.last_fetch = now.to_string ();
}

mutex.unlock ();

return null;
});
} else {
debug ("Getting templates from cache\n");
}
}

public string get_data () {
mutex.lock ();
if (cache == "" && cache_file.query_exists ()) {
cache = Services.FileManager.get_data (cache_file);
}

string data = cache;
mutex.unlock ();

return data;
}

private void save_to_cache (string data) {
try {
GLib.FileUtils.set_data (cache_file.get_path (), data.data);
} catch (Error e) {
warning ("Could not write cache to temp file \"%s\": %s", cache_file.get_basename (), e.message);
}
}
}
1 change: 1 addition & 0 deletions src/Services/Settings.vala
Expand Up @@ -26,6 +26,7 @@ public class Spice.Services.Settings : Granite.Services.Settings {
public int pos_y { get; set; }
public int window_width { get; set; }
public int window_height { get; set; }
public string last_fetch { get; set; }
public string[] last_files { get; set; }
public string controler_config { get; set; }

Expand Down
7 changes: 6 additions & 1 deletion src/Widgets/Library/Library.vala
Expand Up @@ -21,7 +21,7 @@

public class Spice.Widgets.Library.Library : Gtk.ScrolledWindow {
private const string RESOURCE_PATH = "resource:///com/github/philip-scott/spice-up/templates/%s";
private const string TEMPLATES[] = {"Black.spice", "White.spice", "Green.spice", "Spice-Up.spice", "Paper.spice", "BigCity.spice", "Colorful.spice", "Landscape.spice"};
private const string TEMPLATES[] = {"Black.spice", "White.spice", "Green.spice", "Spice-Up.spice", "Paper.spice", "Colorful.spice", "Landscape.spice"};

public signal void item_selected (string data);
public signal void file_selected (File file);
Expand Down Expand Up @@ -79,4 +79,9 @@ public class Spice.Widgets.Library.Library : Gtk.ScrolledWindow {
var item = new LibraryItem (file, real_file);
item_box.add (item);
}

public void add_from_data (string data, string? file_name) {
var item = new LibraryItem.from_data (data, file_name);
item_box.add (item);
}
}
23 changes: 17 additions & 6 deletions src/Widgets/Library/LibraryItem.vala
Expand Up @@ -51,6 +51,13 @@ public class Spice.Widgets.Library.LibraryItem : Gtk.FlowBoxChild {
get_thumbnail ();
}

public LibraryItem.from_data (string data, string? file_name) {
Object (data: data);

title_label.label = file_name;
load_thumbnail ();
}

construct {
margin_top = 3;
halign = Gtk.Align.CENTER;
Expand Down Expand Up @@ -197,15 +204,19 @@ public class Spice.Widgets.Library.LibraryItem : Gtk.FlowBoxChild {
get_template_data ();
}

var pixbuf = Utils.base64_to_pixbuf (Utils.get_thumbnail_data (data));

Idle.add (() => {
slide_widget.pixbuf = pixbuf;
return GLib.Source.REMOVE;
});
load_thumbnail ();

return null;
});
}
}

private void load_thumbnail () {
var pixbuf = Utils.base64_to_pixbuf (Utils.get_thumbnail_data (data));

Idle.add (() => {
slide_widget.pixbuf = pixbuf;
return GLib.Source.REMOVE;
});
}
}
30 changes: 28 additions & 2 deletions src/Widgets/Welcome.vala
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 Felipe Escoto (https://github.com/Philip-Scott/Spice-up)
* Copyright (c) 2018 Felipe Escoto (https://github.com/Philip-Scott/Spice-up)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
Expand All @@ -20,15 +20,21 @@
*/

public class Spice.Welcome : Gtk.Box {
private const string TEMPLATES_URL = "https://spice-up-dev.azurewebsites.net/api/get-templates";

public signal void open_file (File file);

private Granite.Widgets.Welcome welcome;
private Spice.Widgets.Library.Library? library = null;
private Spice.Widgets.Library.Library? templates = null;
private Spice.Services.Fetcher fetcher;
private Gtk.Separator separator;
private Gtk.Stack welcome_stack;

public Welcome () {
fetcher = new Spice.Services.Fetcher ();
fetcher.fetch ();

orientation = Gtk.Orientation.HORIZONTAL;
get_style_context ().add_class ("view");

Expand Down Expand Up @@ -75,6 +81,8 @@ public class Spice.Welcome : Gtk.Box {
open_file (file);
}
});

load_remote_templates ();
}

welcome_stack.set_visible_child_full ("templates", Gtk.StackTransitionType.SLIDE_RIGHT);
Expand Down Expand Up @@ -107,4 +115,22 @@ public class Spice.Welcome : Gtk.Box {
separator.no_show_all = true;
}
}
}

private void load_remote_templates () {
var json_data = Spice.Utils.get_json_object (fetcher.get_data ());

if (json_data == null) return;

if (json_data.get_int_member ("version") > Spice.Services.Fetcher.CURRENT_VERSION) return;

var templates_array = json_data.get_array_member ("templates");

Idle.add (() => {
foreach (var raw in templates_array.get_elements ()) {
var template = raw.get_object ();
templates.add_from_data (template.get_string_member ("data"), template.get_string_member ("name"));
}
return GLib.Source.REMOVE;
});
}
}

0 comments on commit 13e4656

Please sign in to comment.