Imported Upstream version 1.3.99.5_20131030_SE_05e5911_SYSYNC_69de386
[platform/upstream/syncevolution.git] / src / dbus / server / localed-listener.h
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301  USA
18  */
19
20 #ifndef INCL_LOCALED_LISTENER
21 #define INCL_LOCALED_LISTENER
22
23 #include <gdbus-cxx-bridge.h>
24 #include <boost/signals2.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/weak_ptr.hpp>
27
28 #include <syncevo/declarations.h>
29 SE_BEGIN_CXX
30
31 /**
32  * The D-Bus binding for http://www.freedesktop.org/wiki/Software/systemd/localed/
33  */
34 class LocaledListener : public GDBusCXX::DBusRemoteObject
35 {
36  public:
37     /**
38      * Singleton - at most one instance of LocaledListener will exist.
39      * It lives as long as one of the create() callers keeps the reference.
40      */
41     static boost::shared_ptr<LocaledListener> create();
42
43     /**
44      * array of var=value, for example LANG, LC_NUMERIC, etc.
45      */
46     typedef std::vector<std::string> LocaleEnv;
47
48     /**
49      * Emitted for each new set of env variables from localed.
50      * May or may not be different from what we have already.
51      */
52     boost::signals2::signal<void (const LocaleEnv &env)> m_localeValues;
53
54     /**
55      * The result callback is guaranteed to be invoked once,
56      * either with the current settings from localed or, if
57      * retrieving those fails, with the current environment.
58      */
59     void check(const boost::function<void (const LocaleEnv &env)> &result);
60
61     /**
62      * Updates current environment to match the one in the parameter.
63      * Emits m_localeChanged if and only if something really changed.
64      *
65      * Not called by default. To ensure that the current environment
66      * matches localed, do:
67      * - use current settings
68      * - m_localeValues -> setLocale
69      * - check -> setLocale
70      *
71      * Alternatively, one could wait until check() completes and only
72      * then use the current settings.
73      */
74     void setLocale(const LocaleEnv &locale);
75
76     typedef boost::signals2::signal<void ()> LocaleChangedSignal;
77     /**
78      * Emitted by setLocale() only if something really changed in the
79      * local environment.
80      */
81     LocaleChangedSignal m_localeChanged;
82
83  private:
84     boost::weak_ptr<LocaledListener> m_self;
85     typedef boost::variant<LocaleEnv> LocaleVariant;
86     typedef std::map<std::string, LocaleVariant> Properties;
87     typedef std::vector<std::string> Invalidated;
88     GDBusCXX::SignalWatch3<std::string, Properties, Invalidated> m_propertiesChanged;
89     GDBusCXX::DBusClientCall1<LocaleVariant> m_propertiesGet;
90
91     LocaledListener();
92     void onPropertiesChange(const std::string &interface,
93                             const Properties &properties,
94                             const Invalidated &invalidated);
95     typedef boost::function<void (const LocaleEnv &env)> ProcessLocalePropCB_t;
96     void processLocaleProperty(const LocaleVariant &locale,
97                                const std::string &error,
98                                bool mustCall,
99                                const ProcessLocalePropCB_t &result);
100     void emitLocaleEnv(const LocaleEnv &env);
101 };
102
103 SE_END_CXX
104
105 #endif // INCL_LOCALED_LISTENER