File: url_image.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (127 lines) | stat: -rw-r--r-- 4,511 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*  Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (c)  2003-2011, University of Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(url_image, []).
:- use_module(library(pce)).

resource(noimg, image, image('16x16/noimg.xpm')).

:- pce_autoload(http_client, library(http_client)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simple class to provide URL-based  images.   Currently  deals  only with
fetching images using the file and http protocols.

Fetched images may be cached in a table using @on for the cache argument
of ->initialise. Creating a url_image  for   the  2nd  time with caching
enabled returns the previously loaded image.

->initialise also provides a no_image argument. This  image is used as a
default image if the system cannot find the required image.

TBD: What to  do/how  to  implement  reload?   Could  we  be  using  the
broadcasting service for this?

This class was designed to be used with the library scaledbitmap.pl.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- pce_begin_class(url_image, image,
                   "Image whose source comes from a URL").

variable(url,    name, get, "Source of the image").
variable(cache,  bool, get, "Image is cached").
variable(exists, bool, get, "We succeeded loading the image from URL").

initialise(I, URL:url=name, Cache:cache=[bool], NoImage:no_image=[image]*) :->
    "Create image from URL data"::
    send_super(I, initialise),
    (   (   NoImage == @nil
        ->  send(I, load, URL, Cache)
        ;   pce_catch_error(_, send(I, load, URL, Cache))
        )
    ->  send(I, slot, exists, @on)
    ;   send(I, slot, exists, @off),
        (   send(NoImage, instance_of, image)
        ->  send(I, copy, NoImage)
        ;   NoImage == @default
        ->  send_super(I, load, resource(noimg))
        )
    ).

:- pce_global(@url_image_table, new(hash_table)).

lookup(_, URL:name, Cache:[bool], I:url_image) :<-
    "Lookup from image table"::
    Cache \== @off,
    get(@url_image_table, member, URL, I).

unlink(I) :->
    get(I, url, URL),
    (   get(I, cache, @on)
    ->  send(@url_image_table, delete, URL)
    ;   true
    ),
    send_super(I, unlink).

free(I) :->
    "Only free if not referenced"::
    (   get(I, references, 1)       % 1 from hash-table
    ->  send_super(I, free)
    ).

:- pce_group(file).

load(I, URL:name, Cache:[bool]) :->
    "load from URL data"::
    send(I, slot, url, URL),
    (   new(Re, regex('file:(.*)', @off)),
        send(Re, match, URL)
    ->  get(Re, register_value, URL, 1, name, FileName),
        send_super(I, load, FileName)
    ;   send(URL, prefix, 'http:', @on)
    ->  new(HC, http_client(URL)),
        new(TB, text_buffer),
        send(HC, fetch_data, TB),
        send_super(I, load, TB),
        free(TB),
        free(HC)
    ),
    (   Cache == @off
    ->  send(I, slot, cache, @off)
    ;   send(@url_image_table, append, URL, I),
        send(I, slot, cache, @on)
    ).

:- pce_end_class.