Subversion Repositories basico

Rev

Rev 341 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
340 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
"""
4
# File: srv_iconmgt.py
5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Icon manager service
8
"""
9
 
10
import os
11
 
12
import pkg_resources
13
 
14
from gi.repository import Gtk
15
from gi.repository import Gio
16
from gi.repository import Pango
17
from gi.repository.GdkPixbuf import Pixbuf
18
 
19
from basico.core.mod_env import GPATH
20
from basico.core.mod_srv import Service
21
 
22
 
23
class IconManager(Service):
24
    def initialize(self):
25
        self.log.debug("Attached Icons DIR: %s" % GPATH['ICONS'])
26
        self.icondict = {}
27
        self.imgdict = {}
28
        self.theme = Gtk.IconTheme.get_default()
29
        self.theme.prepend_search_path (GPATH['ICONS'])
30
 
31
 
32
    def get_themed_icon(self, icon_name):
33
        ICON = GPATH['ICONS'] + icon_name + '.png'
34
        icon = Gio.ThemedIcon.new(ICON)
35
 
36
        return icon
37
 
38
 
39
 
40
    def get_icon(self, name, width=24, height=24):
41
        key = "%s-%d-%d" % (name, width, height)
42
 
43
        # Get icon from cache if exists or add a new one
44
        try:
45
            icon = self.icondict[key]
46
        except:
47
            iconinfo = self.theme.lookup_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
48
            icon = iconinfo.load_icon()
49
            self.icondict[key] = icon
50
 
51
        return icon
52
 
53
 
54
    def get_pixbuf_icon(self, name, width=36, height=36):
55
        key = "%s-%d-%d" % (name, width, height)
56
 
57
        # Get icon from cache if exists or add a new one
58
        try:
59
            icon = self.icondict[key]
60
        except Exception as error:
61
            icon = None
62
            if name in self.theme.list_icons():
63
                icon = self.theme.load_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
64
            self.icondict[key] = icon
65
 
66
        return icon
67
 
68
 
69
    def get_new_image_icon(self, name, width=36, height=36):
70
        pixbuf = self.get_pixbuf_icon(name, width, height)
71
        icon = Gtk.Image()
72
        icon.set_from_pixbuf(pixbuf)
73
 
74
        return icon
75
 
76
 
77
    def get_image_icon(self, name, width=36, height=36):
78
        key = "%s-%d-%d" % (name, width, height)
79
        try:
80
            icon = self.imgdict[key]
81
        except Exception as error:
82
            icon = None
83
            if name in self.theme.list_icons():
84
                pixbuf = self.theme.load_icon(name, width, Gtk.IconLookupFlags.GENERIC_FALLBACK)
85
                icon = Gtk.Image()
86
                icon.set_from_pixbuf(pixbuf)
87
            self.imgdict[key] = icon
88
 
89
        return icon
358 t00mlabs 90
 
91
 
92
    def find_mime_type_pixbuf(self, mimetype, width=36, height=36):
93
        """
94
        Get icon by mimetype
95
        """
96
        icon = Gio.content_type_get_icon(mimetype)
97
        info = self.theme.choose_icon(icon.get_names(), width, 0)
98
        if info is not None:
99
            icon_name = os.path.basename(info.get_filename()[:-4])
100
            return self.get_pixbuf_icon(icon_name, width, height)
101
        else:
102
            return self.get_pixbuf_icon('text-x-generic', width, height)