summaryrefslogtreecommitdiff
path: root/src/dbus/server/pim/manager.h
blob: ef83806144c4105ec416d1739c9a93bfb2edbfc7 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * 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.1 of the License, or (at your option) version 3.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

/**
 * The D-Bus IPC binding for folks.h and SyncEvolution's
 * PBAP support.
 */

#ifndef INCL_SYNCEVO_DBUS_SERVER_PIM_MANAGER
#define INCL_SYNCEVO_DBUS_SERVER_PIM_MANAGER

#include "folks.h"
#include "locale-factory.h"
#include "../server.h"
#include "../session.h"
#include <syncevo/EDSClient.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * Implementation of org._01.pim.contacts.Manager.
 */
class Manager : public GDBusCXX::DBusObjectHelper
{
    GThread *m_mainThread;
    boost::weak_ptr<Manager> m_self;
    boost::shared_ptr<Server> m_server;
    boost::shared_ptr<IndividualAggregator> m_folks;
    boost::shared_ptr<LocaleFactory> m_locale;
    /** Stores "sort" property in XDG ~/.config/syncevolution/pim-manager.ini'. */
    boost::shared_ptr<ConfigNode> m_configNode;
    std::string m_sortOrder;
    Bool m_preventingAutoTerm;

    /**
     * Contains the EDS UUIDs of all address books contributing to the current
     * unified address book.
     */
    std::set<std::string> m_enabledEBooks;

    typedef std::list< std::pair< boost::shared_ptr<GDBusCXX::Result>, boost::shared_ptr<Session> > > Pending_t;
    /** holds the references to pending session requests, see runInSession() */
    Pending_t m_pending;

    Manager(const boost::shared_ptr<Server> &server);
    void init();
    void initFolks();
    void initDatabases();
    void initSorting(const std::string &order);

 public:
    /** Manager.Start() */
    void start();
    /** Manager.Stop() */
    void stop();
    /** Manager.IsRunning() */
    bool isRunning();
    /** Manager.SetSortOrder() */
    void setSortOrder(const std::string &order);
    /** Manager.GetSortOrder() */
    std::string getSortOrder();
    /** Manager.Search() */
    void search(const boost::shared_ptr< GDBusCXX::Result1<GDBusCXX::DBusObject_t> > &result,
                const GDBusCXX::Caller_t &ID,
                const boost::shared_ptr<GDBusCXX::Watch> &watch,
                const LocaleFactory::Filter_t &filter,
                const GDBusCXX::DBusObject_t &agentPath);
 private:
    void searchWithRegistry(const ESourceRegistryCXX &registry,
                            const GError *gerror,
                            const boost::shared_ptr< GDBusCXX::Result1<GDBusCXX::DBusObject_t> > &result,
                            const GDBusCXX::Caller_t &ID,
                            const boost::shared_ptr<GDBusCXX::Watch> &watch,
                            const LocaleFactory::Filter_t &filter,
                            const GDBusCXX::DBusObject_t &agentPath) throw();
    void doSearch(const ESourceRegistryCXX &registry,
                  const boost::shared_ptr< GDBusCXX::Result1<GDBusCXX::DBusObject_t> > &result,
                  const GDBusCXX::Caller_t &ID,
                  const boost::shared_ptr<GDBusCXX::Watch> &watch,
                  const LocaleFactory::Filter_t &filter,
                  const GDBusCXX::DBusObject_t &agentPath);

 public:
    /** Manager.GetActiveAddressBooks() */
    void getActiveAddressBooks(std::vector<std::string> &dbIDs);

    /** Manager.SetActiveAddressBooks() */
    void setActiveAddressBooks(const std::vector<std::string> &dbIDs);

    /** Manager.CreatePeer() */
    void createPeer(const boost::shared_ptr<GDBusCXX::Result0> &result,
                    const std::string &uid, const StringMap &properties);
    /** Manager.ModifyPeer() */
    void modifyPeer(const boost::shared_ptr<GDBusCXX::Result0> &result,
                    const std::string &uid, const StringMap &properties);
 private:
    enum ConfigureMode {
        SET_PEER,
        CREATE_PEER,
        MODIFY_PEER
    };

    void setPeer(const boost::shared_ptr<GDBusCXX::Result0> &result,
                 const std::string &uid, const StringMap &properties,
                 ConfigureMode mode);
    void doSetPeer(const boost::shared_ptr<Session> &session,
                   const boost::shared_ptr<GDBusCXX::Result0> &result,
                   const std::string &uid, const StringMap &properties,
                   ConfigureMode mode);

 public:
    /** Manager.RemovePeer() */
    void removePeer(const boost::shared_ptr<GDBusCXX::Result0> &result,
                    const std::string &uid);

 private:
    void doRemovePeer(const boost::shared_ptr<Session> &session,
                      const boost::shared_ptr<GDBusCXX::Result0> &result,
                      const std::string &uid);

 public:
    typedef std::map<std::string, boost::variant<bool, int> > SyncResult;
    /** Manager.SyncPeer() */
    void syncPeer(const boost::shared_ptr<GDBusCXX::Result1<SyncResult> > &result,
                  const std::string &uid);

    /** Manager.SyncProgress */
    GDBusCXX::EmitSignal3<const std::string &,
                          const std::string &,
                          const SyncResult &> emitSyncProgress;


 private:
    void doSyncPeer(const boost::shared_ptr<Session> &session,
                    const boost::shared_ptr<GDBusCXX::Result1<SyncResult> > &result,
                    const std::string &uid);

    void report2SyncProgress(const std::string &uid,
                             const std::string &sourceName,
                             const SyncSourceReport &source);

 public:
    /** Manager.StopSync() */
    void stopSync(const boost::shared_ptr<GDBusCXX::Result0> &result,
                  const std::string &uid);

    typedef std::map<std::string, StringMap> PeersMap;
    /** Manager.GetAllPeers() */
    PeersMap getAllPeers();

    /** Manager.AddContact() */
    void addContact(const boost::shared_ptr< GDBusCXX::Result1<std::string> > &result,
                    const std::string &addressbook,
                    const PersonaDetails &details);
    /** Manager.ModifyContact() */
    void modifyContact(const boost::shared_ptr<GDBusCXX::Result0> &result,
                       const std::string &addressbook,
                       const std::string &localID,
                       const PersonaDetails &details);
    /** Manager.RemoveContact() */
    void removeContact(const boost::shared_ptr<GDBusCXX::Result0> &result,
                       const std::string &addressbook,
                       const std::string &localID);

 private:
    /**
     * Starts a session for the given config and with the
     * given flags, then when it is active, invokes the callback.
     * Failures will be reported back to via the result
     * pointer.
     */
    void runInSession(const std::string &config,
                      Server::SessionFlags flags,
                      const boost::shared_ptr<GDBusCXX::Result> &result,
                      const boost::function<void (const boost::shared_ptr<Session> &session)> &callback);

    /**
     * Common boilerplate code for anything that runs inside
     * an active session in response to some D-Bus method call
     * (like doSetPeer).
     */
    void doSession(const boost::weak_ptr<Session> &session,
                   const boost::shared_ptr<GDBusCXX::Result> &result,
                   const boost::function<void (const boost::shared_ptr<Session> &session)> &callback);

    /** true if the current thread is the one handling the event loop and running all operations */
    bool isMain() { return g_thread_self() == m_mainThread; }

    /**
     * Runs the operation inside the main thread and returns once the
     * main thread is done with it.
     */
    void runInMainVoid(const boost::function<void ()> &operation);
    template <class R> R runInMainRes(const boost::function<R ()> &operation);

    void runInMainV(void (Manager::*method)()) { runInMainVoid(boost::bind(method, this)); }
    template <class R> R runInMainR(R (Manager::*method)()) { return runInMainRes<R>(boost::bind(method, this)); }
    template <class A1, class B1> void runInMainV(void (Manager::*method)(B1), A1 a1) { runInMainVoid(boost::bind(method, this, a1)); }
    template <class R, class A1, class B1> R runInMainR(R (Manager::*method)(B1), A1 a1) { return runInMainRes<R>(boost::bind(method, this, a1)); }

 public:
    /**
     * Creates an instance of the Manager which runs as part
     * of the given server, using the same D-Bus connection
     * and using the server's services (tracking clients,
     * startup/shutdown).
     *
     * While the Manager exists, it blocks auto-termination
     * of the server and serves method calls as part of the
     * main event loop.
     */
    static boost::shared_ptr<Manager> create(const boost::shared_ptr<Server> &server);

    ~Manager();
};

SE_END_CXX

#endif // INCL_SYNCEVO_DBUS_SERVER_PIM_MANAGER