Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/web/accountsmanager.py
blob: cd1da836a068f76440eb2d267d360e6ad50a7a7e (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
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
# Copyright (c) 2013 Walter Bender
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import logging

from gi.repository import Gtk

from jarabe import config

_accounts = []


def get_all_accounts():
    ''' Returns a list of all installed online account managers '''
    global _accounts  # No need to do this every time.
    if len(_accounts) > 0:
        return _accounts

    web_path = os.path.join(config.ext_path, 'web')
    try:
        web_path_dirs = os.listdir(web_path)
    except OSError, e:
        web_path_dirs = []
        logging.warning('listdir: %s: %s' % (web_path, e))

    for d in web_path_dirs:
        dir_path = os.path.join(web_path, d)
        module = _load_module(dir_path)
        if module is not None:
            _accounts.append(module)
            _extend_icon_theme_search_path(dir_path)

    return _accounts


def _load_module(dir_path):
    module = None
    if os.path.isdir(dir_path):
        for f in os.listdir(dir_path):
            if f == 'account.py':
                module_name = f[:-3]
                logging.debug('OnlineAccountsManager loading %s' % (
                        module_name))
                module_path = 'web.%s.%s' % (os.path.basename(dir_path),
                                             module_name)
                try:
                    mod = __import__(module_path, globals(), locals(),
                                     [module_name])
                    if hasattr(mod, 'get_account'):
                        module = mod.get_account()

                except Exception as e:
                    logging.exception('Exception while loading %s: %s' % (
                            module_name, str(e)))

    return module


def _extend_icon_theme_search_path(dir_path):
    icon_theme = Gtk.IconTheme.get_default()
    icon_search_path = icon_theme.get_search_path()

    try:
        icon_path_dirs = os.listdir(dir_path)
    except OSError, e:
        icon_path_dirs = []
        logging.warning('listdir: %s: %s' % (dir_path, e))

    for f in icon_path_dirs:
        if f == 'icons':
            icon_path = os.path.join(dir_path, f)
            if os.path.isdir(icon_path) and \
                    icon_path not in icon_search_path:
                icon_theme.append_search_path(icon_path)


def get_configured_accounts():
    return [a for a in get_all_accounts() if a.is_configured()]


def get_active_accounts():
    return [a for a in get_all_accounts() if a.is_active()]