Skip to content

Commit

Permalink
issue #377, #323 - finishing up?
Browse files Browse the repository at this point in the history
* add app.cache_clear to Shoes. Modify bug377.rb and cobbler.rb
  to use it.
* add Manual description for cache_clear
  • Loading branch information
Cecil committed Oct 30, 2017
1 parent b894619 commit 0751630
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 81 deletions.
19 changes: 6 additions & 13 deletions bugs/bug377.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
require 'shoes/image'
stack do
flow do
@el = edit_line "#{DIR}/static/shoes-icon-walkabout.png"
#@el = edit_line "https://shoes.mvmanila.com/public/images/dino.jpg"
#@el = edit_line "#{DIR}/static/shoes-icon-walkabout.png"
@el = edit_line "https://shoes.mvmanila.com/public/images/dino.jpg"
@cb = check; para "Cached?"
button "(Re)load" do
@img.clear
@img.append do
image @el.text, cache: @cb.checked
end
end
button "Show cache" do
button "Show external cache" do
@cview.clear
@cview.append do
eb = edit_box width: 400
Expand All @@ -21,16 +21,9 @@
end
end
end
button "clear caches" do
DATABASE.each do |k, val|
v = val.split('|')
path = Shoes::image_cache_path v[1], File.extname(k)
puts "delete: #{path}"
File.delete path if File.exist? path
end
DATABASE.clear
DATABASE.close
quit if confirm "Please restart Shoes"
button "clear all caches" do
app.cache_clear :all
quit if confirm "Please restart Shoes for best results"
end
para "Global cache: "
@sw = switch width: 80 do |n|
Expand Down
45 changes: 10 additions & 35 deletions lib/shoes/cobbler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,42 +330,17 @@ def cachescreen
require 'shoes/image'
litems = ['-all-']
@panel.append do
para "Select image to delete or all"
@dblist = list_box :items => litems, :choose =>'-all-'
#para "DB at: #{LIB_DIR}"
#para "cache: #{CACHE_DIR}"
DATABASE.each do |key, value|
litems << "#{key}"
end
@dblist.items = litems
end
@panel.append do
flow do
button "Delete From Cache" do
sel = @dblist.text
if confirm "Delete #{sel}"
fdel = @filestoo.checked?
if sel == '-all-'
#puts 'delete all'
DATABASE.each do |k, val|
v = val.split('|')
path = Shoes::image_cache_path v[1], File.extname(k)
#puts "Deleted #{path}"
File.delete path if File.exist? path
end
DATABASE.clear
else
#delete single item.
if fdel
v = DATABASE[sel].split('|')
path = Shoes::image_cache_path v[1], File.extname(sel)
File.delete path if File.exist? path
end
DATABASE.delete(sel) # block doesn't work as expected
end
end
@cflow = flow do
button "Delete Image Cache" do
app.cache_clear :all if @cache_all.checked?
app.cache_clear :memory if @cache_int.checked?
app.cache_clear :external if @cache_ext.checked?
quit if confirm "You should restart Shoes"
end
@filestoo = check; para "Remove file? (I really need disk space)"
@cache_all = radio :imgcache; para "Both caches"
@cache_int = radio :imgcache ; para "Internal images"
@cache_ext = radio :imgcache; para "External images"
@cache_all.checked = true
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/shoes/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@ def notify_cache_of url, etag, hash
val = [etag, hash, Time.now].join('|')
DATABASE[url] = val
end

def delete_cache
DATABASE.each do |k, val|
v = val.split('|')
path = Shoes::image_cache_path v[1], File.extname(k)
$stderr.puts "ext cache delete: #{path}"
File.delete path if File.exist? path
end
DATABASE.clear
DATABASE.close
# TODO: reopen - somehow.
end
end

20 changes: 20 additions & 0 deletions shoes/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,26 @@ VALUE shoes_app_get_cache(VALUE app) {
return shoes_cache_setting ? Qtrue: Qnil;
}

VALUE shoes_app_clear_cache(VALUE app, VALUE opts) {
int mem, ext = 0;
if (opts == ID2SYM(rb_intern("memory")))
mem = 1;
if (opts == ID2SYM(rb_intern("eternal")))
ext = 1;
if (opts == ID2SYM(rb_intern("all"))) {
ext = 1;
mem = 1;
}
if (mem)
st_clear(shoes_world->image_cache);
if (ext) {
// call into shoes/ruby
rb_require("shoes/data");
rb_funcall(rb_const_get(rb_cObject, rb_intern("DATABASE")), rb_intern("delete_cache"), 0);
}
return Qtrue;
}


shoes_code shoes_app_start(VALUE allapps, char *uri) {
int i;
Expand Down
3 changes: 2 additions & 1 deletion shoes/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ VALUE shoes_app_set_window_position(VALUE, VALUE, VALUE);
VALUE shoes_app_resize_window(VALUE, VALUE, VALUE);
VALUE shoes_app_get_resizable(VALUE);
VALUE shoes_app_set_resizable(VALUE, VALUE);
VALUE shoes_app_set_cache(VALUE app, VALUE decorated);
VALUE shoes_app_set_cache(VALUE app, VALUE setting);
VALUE shoes_app_get_cache(VALUE app);
VALUE shoes_app_clear_cache(VALUE app, VALUE opts);
// global var for image cache - declared in types/image.c
extern int shoes_cache_setting;
// global var for console up and running
Expand Down
54 changes: 23 additions & 31 deletions shoes/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ char shoes_has_ext(char *fname, int len, const char *ext) {
return strncmp(fname + (len - strlen(ext)), ext, strlen(ext)) == 0;
}

unsigned char shoes_check_file_exists(VALUE path) {
unsigned char shoes_check_file_exists(VALUE path) {
if (!RTEST(rb_funcall(rb_cFile, rb_intern("exists?"), 1, path))) {
StringValue(path);
shoes_error("Shoes could not find the file %s.", RSTRING_PTR(path));
Expand Down Expand Up @@ -764,6 +764,11 @@ void shoes_cache_insert(unsigned char type, VALUE imgpath, shoes_cached_image *i
st_insert(shoes_world->image_cache, (st_data_t)path, (st_data_t)cached);
}

void shoes_cache_delete(char *imgpath) {
int err = st_delete(shoes_world->image_cache, (st_data_t *)imgpath, (st_data_t *)0);
// FYI st_ functions in rubys ruby/st.h
}

shoes_image_format shoes_image_detect(VALUE imgpath, int *width, int *height) {
shoes_image_format format = SHOES_IMAGE_NONE;
shoes_cached_image *cached = NULL;
Expand All @@ -772,12 +777,12 @@ shoes_image_format shoes_image_detect(VALUE imgpath, int *width, int *height) {
char *fname = RSTRING_PTR(filename);
int len = (int)RSTRING_LEN(filename);

if (shoes_cache_lookup(RSTRING_PTR(imgpath), &cached)) {
if (shoes_cache_setting && shoes_cache_lookup(RSTRING_PTR(imgpath), &cached)) {
*width = cached->width;
*height = cached->height;
return cached->format;
}

if (!shoes_check_file_exists(imgpath))
return SHOES_IMAGE_NONE;
else if (shoes_has_ext(fname, len, ".png")) {
Expand Down Expand Up @@ -819,27 +824,6 @@ shoes_code shoes_load_imagesize(VALUE imgpath, int *width, int *height) {
return SHOES_FAIL;
return SHOES_OK;
}
/*
shoes_cached_image* shoes_no_cache_download(shoes_image_download_event *evt) {
int width, height;
shoes_cached_image *cached = shoes_world->blank_cache;
fprintf(stderr,"http no cache, finishing\n");
if (evt->status != 200)
shoes_error("Shoes could not load the file at %s. [%lu]", evt->uripath, evt->status);
cairo_surface_t *img = shoes_surface_create_from_file(rb_str_new2(evt->filepath), &width, &height);
if (img != shoes_world->blank_image) {
cached = shoes_cached_image_new(width, height, img);
}
shoes_canvas_repaint_all(evt->slot);
free(evt->filepath);
free(evt->uripath);
if (evt->etag != NULL)
free(evt->etag);
free(evt);
return cached;
}
*/

unsigned char shoes_image_downloaded(shoes_image_download_event *idat) {
int i, j, width, height;
Expand All @@ -854,7 +838,7 @@ unsigned char shoes_image_downloaded(shoes_image_download_event *idat) {
shoes_error("Shoes could not load the file at %s. [%lu]", idat->uripath, idat->status);
return 0;
}

fprintf(stderr, "osx download finished %s\n", idat->filepath);
cairo_surface_t *img = shoes_surface_create_from_file(rb_str_new2(idat->filepath), &width, &height);
if (img != NULL) {
shoes_cached_image *cached;
Expand Down Expand Up @@ -940,8 +924,7 @@ shoes_cached_image *shoes_load_image_nocache (VALUE slot, VALUE imgpath) {
int width = 1, height = 1;

if (strlen(fname) > 7 && (strncmp(fname, "http://", 7) == 0 || strncmp(fname, "https://", 8) == 0)) {
struct timeval tv;
VALUE cache, uext, hdrs, tmppath, uri, scheme, host, port, requ, path, cachepath = Qnil, digest = Qnil;
VALUE uext, hdrs, tmppath, uri, scheme, host, port, requ, path, cachepath = Qnil, digest = Qnil;
rb_require("shoes/data");
uri = rb_funcall(cShoes, rb_intern("uri"), 1, imgpath);
scheme = rb_funcall(uri, s_scheme, 0);
Expand Down Expand Up @@ -982,11 +965,20 @@ shoes_cached_image *shoes_load_image_nocache (VALUE slot, VALUE imgpath) {
if (!NIL_P(hdrs))
req->headers = shoes_http_headers(hdrs);
req->data = idat;
fprintf(stderr, "download to %s\n", req->filepath);
cached = shoes_cached_image_new(1, 1, shoes_world->blank_image);
shoes_cache_insert(SHOES_CACHE_FILE, imgpath, cached);
shoes_native_download(req);
img = shoes_surface_create_from_file(tmppath, &width, &height);
if (img != shoes_world->blank_image) {
cached = shoes_cached_image_new(width, height, img);
}
#ifndef SHOES_GTK
// osx download is asynchrouus (run loop threaded)
// that dummieds cached var above is replaced later - hence the
// need to insert it into the cache.
#else
//img = shoes_surface_create_from_file(tmppath, &width, &height);
//if (img != shoes_world->blank_image) {
// cached = shoes_cached_image_new(width, height, img);
//}
#endif
} else {
// read user file
fprintf(stderr, "no cache read from %s\n", RSTRING_PTR(imgpath));
Expand Down
1 change: 1 addition & 0 deletions shoes/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ void shoes_ruby_init() {
rb_define_alias(cApp, "decorated?", "decorated");
rb_define_method(cApp, "cache", CASTHOOK(shoes_app_get_cache), 0);
rb_define_method(cApp, "cache=", CASTHOOK(shoes_app_set_cache), 1);
rb_define_method(cApp, "cache_clear", CASTHOOK(shoes_app_clear_cache), 1);

cDialog = rb_define_class_under(cTypes, "Dialog", cApp);

Expand Down
8 changes: 7 additions & 1 deletion shoes/types/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ VALUE shoes_response_status(VALUE self) {
* This a lot more than 'catch' - that's a poor name
*
*/
extern void shoes_cache_delete(char *); // in image.c

int shoes_catch_message(unsigned int name, VALUE obj, void *data) {
int ret = SHOES_DOWNLOAD_CONTINUE;
switch (name) {
Expand All @@ -280,7 +282,11 @@ int shoes_catch_message(unsigned int name, VALUE obj, void *data) {
realpath = rb_funcall(cShoes, rb_intern("image_cache_path"), 2, hash, uext);
rename(side->filepath, RSTRING_PTR(realpath));
}
}
} else {
// remove from cache - crash
// shoes_cache_delete(RSTRING_PTR(side->uripath));
st_clear(shoes_world->image_cache);
}
}

free(side->filepath);
Expand Down
6 changes: 6 additions & 0 deletions static/manual-en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5223,6 +5223,12 @@ cached (aka 'true'). Sometimes, that is not desired. You can turn off the cachin
for all image calls by setting this to 'false' Note: individual calls to image can
override the global setting - see [[Image]]

=== app.cache_clear sym » boolean ===

Clears the internal image cache or the external image cache or both depending on the
symbol provided for the argument - one of :memory, :external, :all

Always returns true.

=== app.decorated » true or false ===

Expand Down

0 comments on commit 0751630

Please sign in to comment.