Skip to content

Commit 431d952

Browse files
authored
Remote templates v2 (#237)
1 parent 445b484 commit 431d952

5 files changed

Lines changed: 60 additions & 18 deletions

File tree

data/com.github.philip-scott.spice-up.appdata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<binary>spice-up</binary>
2727
</provides>
2828
<releases>
29-
<release version="1.6.0" date="2018-9-23">
29+
<release version="1.6.1" date="2018-10-3">
3030
<description>
3131
<p>Slide Transitions and the Slide List Redesign</p>
3232
<ul>

src/Services/Fetcher.vala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*/
2121

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

2727
private File cache_file;
2828
private string cache = "";
@@ -32,7 +32,7 @@ public class Spice.Services.Fetcher {
3232
cache_file = File.new_for_path (Environment.get_tmp_dir () + "/com.github.philip-scott.spice-up.cache.json");
3333
}
3434

35-
public void fetch () {
35+
public void fetch_templates () {
3636
var now = new DateTime.now_utc ().to_unix ();
3737

3838
var settigns = Spice.Services.Settings.get_instance ();
@@ -44,7 +44,7 @@ public class Spice.Services.Fetcher {
4444
debug ("Getting templates from server\n");
4545
new Thread<void*> ("fetch-templates", () => {
4646
var session = new Soup.Session ();
47-
var message = new Soup.Message ("GET", TEMPLATES_URL);
47+
var message = new Soup.Message ("GET", TEMPLATES_URL + "get-templates");
4848

4949
session.send_message (message);
5050

@@ -56,6 +56,7 @@ public class Spice.Services.Fetcher {
5656
mutex.lock ();
5757

5858
cache = data.str;
59+
5960
if (cache != "") {
6061
save_to_cache (cache);
6162
settigns.last_fetch = now.to_string ();
@@ -70,6 +71,20 @@ public class Spice.Services.Fetcher {
7071
}
7172
}
7273

74+
public static string get_template_data (string template) {
75+
var session = new Soup.Session ();
76+
var message = new Soup.Message ("GET", TEMPLATES_URL + template);
77+
78+
session.send_message (message);
79+
80+
var data = new StringBuilder ();
81+
foreach (var c in message.response_body.data) {
82+
data.append ("%c".printf (c));
83+
}
84+
85+
return data.str;
86+
}
87+
7388
public string get_data () {
7489
mutex.lock ();
7590
if (cache == "" && cache_file.query_exists ()) {

src/Widgets/Library/Library.vala

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,26 @@ public class Spice.Widgets.Library.Library : Gtk.ScrolledWindow {
5757
item_box.min_children_per_line = 2;
5858

5959
item_box.child_activated.connect ((child) => {
60-
item_selected ((child as LibraryItem).data);
60+
var library_item = child as LibraryItem;
61+
if (library_item == null) return;
62+
63+
if (library_item.remote_path == null) {
64+
item_selected (library_item.data);
65+
} else {
66+
sensitive = false;
67+
new Thread<void*> ("fetch-template", () => {
68+
var data = Spice.Services.Fetcher.get_template_data (library_item.remote_path);
69+
70+
Idle.add (() => {
71+
item_selected (data);
72+
sensitive = true;
73+
return GLib.Source.REMOVE;
74+
});
75+
return null;
76+
});
77+
}
78+
79+
6180
});
6281
}
6382

@@ -80,8 +99,8 @@ public class Spice.Widgets.Library.Library : Gtk.ScrolledWindow {
8099
item_box.add (item);
81100
}
82101

83-
public void add_from_data (string data, string? file_name) {
84-
var item = new LibraryItem.from_data (data, file_name);
102+
public void add_remote (string file_name, string path, string data) {
103+
var item = new LibraryItem.from_remote (data, file_name, path);
85104
item_box.add (item);
86105
}
87106
}

src/Widgets/Library/LibraryItem.vala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class Spice.Widgets.Library.LibraryItem : Gtk.FlowBoxChild {
3737
}
3838

3939
public string data { get; construct set; }
40+
public string? remote_path {get; construct set; default = null; }
4041

4142
private SlideWidget slide_widget;
4243
private Gtk.Popover? popover = null;
@@ -51,11 +52,12 @@ public class Spice.Widgets.Library.LibraryItem : Gtk.FlowBoxChild {
5152
get_thumbnail ();
5253
}
5354

54-
public LibraryItem.from_data (string data, string? file_name) {
55-
Object (data: data);
55+
public LibraryItem.from_remote (string data, string file_name, string path) {
56+
Object (data: data, remote_path: path);
5657

5758
title_label.label = file_name;
58-
load_thumbnail ();
59+
load_thumbnail (data);
60+
set_tooltip_text (_("Create Presentation"));
5961
}
6062

6163
construct {
@@ -202,19 +204,21 @@ public class Spice.Widgets.Library.LibraryItem : Gtk.FlowBoxChild {
202204
new Thread<void*> ("content-loading", () => {
203205
if (real_file) {
204206
get_file_data ();
207+
load_thumbnail (Utils.get_thumbnail_data (data));
208+
} else if (remote_path != null) {
209+
load_thumbnail (data);
205210
} else {
206211
get_template_data ();
212+
load_thumbnail (Utils.get_thumbnail_data (data));
207213
}
208214

209-
load_thumbnail ();
210-
211215
return null;
212216
});
213217
}
214218
}
215219

216-
private void load_thumbnail () {
217-
var pixbuf = Utils.base64_to_pixbuf (Utils.get_thumbnail_data (data));
220+
private void load_thumbnail (string base64_image) {
221+
var pixbuf = Utils.base64_to_pixbuf (base64_image);
218222

219223
Idle.add (() => {
220224
slide_widget.pixbuf = pixbuf;

src/Widgets/Welcome.vala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Spice.Welcome : Gtk.Box {
3333

3434
public Welcome () {
3535
fetcher = new Spice.Services.Fetcher ();
36-
fetcher.fetch ();
36+
fetcher.fetch_templates ();
3737

3838
orientation = Gtk.Orientation.HORIZONTAL;
3939
get_style_context ().add_class ("view");
@@ -121,14 +121,18 @@ public class Spice.Welcome : Gtk.Box {
121121

122122
if (json_data == null) return;
123123

124-
if (json_data.get_int_member ("version") > Spice.Services.Fetcher.CURRENT_VERSION) return;
124+
if (json_data.get_string_member ("version") != Spice.Services.Fetcher.CURRENT_VERSION) return;
125125

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

128128
Idle.add (() => {
129129
foreach (var raw in templates_array.get_elements ()) {
130130
var template = raw.get_object ();
131-
templates.add_from_data (template.get_string_member ("data"), template.get_string_member ("name"));
131+
templates.add_remote (
132+
template.get_string_member ("name"),
133+
template.get_string_member ("url"),
134+
template.get_string_member ("preview")
135+
);
132136
}
133137
return GLib.Source.REMOVE;
134138
});

0 commit comments

Comments
 (0)