Imported Upstream version 1.3.99.5_20131030_SE_05e5911_SYSYNC_69de386
[platform/upstream/syncevolution.git] / src / dbus / server / session-helper.h
1 /*
2  * Copyright (C) 2012 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 SESSION_HELPER_H
21 #define SESSION_HELPER_H
22
23 #include "session-common.h"
24 #include "dbus-sync.h"
25
26 #include <gdbus-cxx-bridge.h>
27 #include <boost/function.hpp>
28 #include <glib.h>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <syncevo/declarations.h>
33 SE_BEGIN_CXX
34
35 class LogRedirect;
36 class ForkExecChild;
37 class SessionHelperLogger;
38
39 /**
40  * Waits for requests via the internal D-Bus connection in run(), sent
41  * by the Session class in syncevo-dbus-server. Then for each request
42  * it remembers what to do in m_operation and returns from the event
43  * loop and executes the requested operation, pretty much like the
44  * traditional syncevo-dbus-server did.
45  */
46 class SessionHelper : public GDBusCXX::DBusObjectHelper,
47     private boost::noncopyable
48 {
49     GMainLoop *m_loop;
50     GDBusCXX::DBusConnectionPtr m_conn;
51     boost::shared_ptr<ForkExecChild> m_forkexec;
52     boost::function<bool ()> m_operation;
53     boost::shared_ptr<SessionHelperLogger> m_logger;
54     PushLogger<Logger> m_pushLogger;
55
56     /** valid during doSync() */
57     boost::scoped_ptr<DBusSync> m_sync;
58
59     /** called by main event loop: initiate a sync operation */
60     void sync(const SessionCommon::SyncParams &params,
61               const boost::shared_ptr< GDBusCXX::Result2<bool, SyncReport> > &result);
62
63     /**
64      * called by run(): do the sync operation
65      * @return true if the helper is meant to terminate
66      */
67     bool doSync(const SessionCommon::SyncParams &params,
68                 const boost::shared_ptr< GDBusCXX::Result2<bool, SyncReport> > &result);
69
70     void restore(const std::string &configName,
71                  const string &dir, bool before, const std::vector<std::string> &sources,
72                  const boost::shared_ptr< GDBusCXX::Result1<bool> > &result);
73     bool doRestore(const std::string &configName,
74                    const string &dir, bool before, const std::vector<std::string> &sources,
75                    const boost::shared_ptr< GDBusCXX::Result1<bool> > &result);
76
77     void execute(const vector<string> &args, const map<string, string> &vars,
78                  const boost::shared_ptr< GDBusCXX::Result1<bool> > &result);
79     bool doExecute(const vector<string> &args, const map<string, string> &vars,
80                    const boost::shared_ptr< GDBusCXX::Result1<bool> > &result);
81
82     /** SessionHelper.PasswordResponse */
83     void passwordResponse(bool timedOut, bool aborted, const std::string &password);
84
85  public:
86     SessionHelper(GMainLoop *loop,
87                   const GDBusCXX::DBusConnectionPtr &conn,
88                   const boost::shared_ptr<ForkExecChild> &forkexec);
89     ~SessionHelper();
90
91     void setDBusLogLevel(Logger::Level level);
92     Logger::Level getDBusLogLevel() const;
93
94     void activate();
95     void run();
96     GMainLoop *getLoop() const { return m_loop; }
97
98     /** Still have connection to parent. Shortcut which asks the ForkExecChild class. */
99     bool connected();
100
101     boost::shared_ptr<ForkExecChild> getForkExecChild() { return m_forkexec; }
102
103     /** Server.LogOutput for the session D-Bus object */
104     GDBusCXX::EmitSignal3<std::string,
105         std::string, std::string, true> emitLogOutput;
106
107     /** SyncContext::displaySyncProgress */
108     GDBusCXX::EmitSignal4<sysync::TProgressEventEnum,
109         int32_t, int32_t, int32_t, true> emitSyncProgress;
110
111     /** SyncContext::displaySourceProgress */
112     GDBusCXX::EmitSignal6<sysync::TProgressEventEnum,
113         std::string, SyncMode,
114         int32_t, int32_t, int32_t, true> emitSourceProgress;
115
116     /** SyncContext::m_sourceSyncedSignal */
117     GDBusCXX::EmitSignal2<std::string, SyncSourceReport, true> emitSourceSynced;
118
119     /** SyncContext::reportStepCmd -> true/false for "waiting on IO" */
120     GDBusCXX::EmitSignal1<bool, true> emitWaiting;
121
122     /** SyncContext::syncSuccessStart */
123     GDBusCXX::EmitSignal0Template<true> emitSyncSuccessStart;
124
125     /** Cmdline::configWasModified() */
126     GDBusCXX::EmitSignal0Template<true> emitConfigChanged;
127
128     /** SyncContext::askPassword */
129     GDBusCXX::EmitSignal2<std::string, ConfigPasswordKey> emitPasswordRequest;
130
131     /** send message to parent's connection (buffer, type, url) */
132     GDBusCXX::EmitSignal3<GDBusCXX::DBusArray<uint8_t>, std::string, std::string> emitMessage;
133
134     /** tell parent's connection to shut down */
135     GDBusCXX::EmitSignal0Template<false> emitShutdown;
136
137     /** store the next message received by the session's connection */
138     void storeMessage(const GDBusCXX::DBusArray<uint8_t> &message,
139                       const std::string &type) {
140         m_messageSignal(message, type);
141     }
142     typedef boost::signals2::signal<void (const GDBusCXX::DBusArray<uint8_t> &,
143                                           const std::string &)> MessageSignal_t;
144     MessageSignal_t m_messageSignal;
145
146     /** store the latest connection state information */
147     void connectionState(const std::string &error)
148     {
149         m_connectionStateSignal(error);
150     }
151     typedef boost::signals2::signal<void (const std::string &)> ConnectionStateSignal_t;
152     ConnectionStateSignal_t m_connectionStateSignal;
153 };
154
155 SE_END_CXX
156
157 #endif // SESSION_HELPER_H