summaryrefslogtreecommitdiffstats
path: root/src/Gui/ConfBackend.py
blob: e0f64340d61b8f6b504f0a0d7698b52a9675619b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from ABRTExceptions import ConfBackendInitError
from abrt_utils import _

#FIXME: add some backend factory

try:
    import gnomekeyring as gkey
except ImportError, e:
    gkey = None

class ConfBackend(object):
    def __init__(self):
        pass

    def save(self, name, settings):
        """ Default save method has to be implemented in derived class """
        raise NotImplementedError

    def load(self, name):
        """ Default load method has to be implemented in derived class """
        raise NotImplementedError


class ConfBackendGnomeKeyring(ConfBackend):
    def __init__(self):
        ConfBackend.__init__(self)
        self.default_key_ring = gkey.get_default_keyring_sync()
        if not gkey.is_available():
            raise ConfBackendInitError(_("Can't connect do Gnome Keyring daemon"))

    def save(self, name, settings):
        settings_tmp = settings.copy()
        settings_tmp["AbrtPluginInfo"] = name
        password = ""

        if "Password" in settings_tmp:
            password = settings_tmp["Password"]
            del settings_tmp["Password"]
        gkey.item_create_sync(self.default_key_ring,
                                    gkey.ITEM_GENERIC_SECRET,
                                    "abrt:%s" % name,
                                    settings_tmp,
                                    password,
                                    True)

    def load(self, name):
        item_list = None
        try:
            item_list = gkey.find_items_sync(gkey.ITEM_GENERIC_SECRET, {"AbrtPluginInfo":str(name)})
        except gkey.NoMatchError, ex:
            # nothing found
            pass

        if item_list:
            retval = item_list[0].attributes.copy()
            retval["Password"] = item_list[0].secret
            return retval
        else:
            return {}
            #for i in item_list:
            #    for attr in i.attributes:
            #        print attr, i.attributes[attr]