aboutsummaryrefslogtreecommitdiff
path: root/src/DynamicImage.cpp
blob: 0a31d36277f8a67e26a9853b930d8f934ce0f4f3 (plain)
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
#include "../include/DynamicImage.hpp"
#include "../include/GlobalCache.hpp"
#include "../include/GtkGif.hpp"
#include "../include/GtkScaledImage.hpp"

namespace dchat
{
    DynamicImage::DynamicImage(int _downloadLimitBytes) : 
        downloadLimitBytes(_downloadLimitBytes)
    {
        signal_draw().connect(sigc::mem_fun(*this, &DynamicImage::updateContent));
    }

    bool DynamicImage::updateContent(const Cairo::RefPtr<Cairo::Context> &cairo)
    {
        if(!url.empty())
        {
            Gtk::Allocation alloc = get_allocation();
            auto result = getGlobalCache().getContentByUrl(url, downloadLimitBytes);
            if(result.type == ContentByUrlResult::Type::CACHED && result.gif)
            {
                switch(result.cachedType)
                {
                    case ContentByUrlResult::CachedType::STATIC_IMAGE:
                    {
                        GtkScaledImage *staticImage = (GtkScaledImage*)result.staticImage;
                        staticImage->draw(cairo, alloc.get_width(), alloc.get_height(), true);
                        break;
                    }
                    case ContentByUrlResult::CachedType::GIF:
                    {
                        GtkGif *gif = (GtkGif*)result.gif;
                        gif->draw(cairo, alloc.get_width(), alloc.get_height(), true);
                        break;
                    }
                    default:
                        break;
                }
            }
        }
        queue_draw();
        return true;
    }
}