ce8bb1c4b0c77d72b6c7399cbd83b03aeaf6010e
[platform/upstream/syncevolution.git] / src / dbus / server / session-common.h
1 /*
2  * Copyright (C) 2011 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_COMMON_H
21 #define SESSION_COMMON_H
22
23 #include "source-status.h"
24 #include "source-progress.h"
25 #include <syncevo/util.h>
26 #include <syncevo/DBusTraits.h>
27 #include <syncevo/FilterConfigNode.h>
28 #include <syncevo/SynthesisEngine.h>
29
30 #include <gdbus-cxx-bridge.h>
31
32 SE_BEGIN_CXX
33
34 /**
35  * This namespace holds constants and defines for Sessions and its
36  * consumers.
37  */
38 namespace SessionCommon
39 {
40     const char * const SERVICE_NAME = "org.syncevolution";
41     const char * const CONNECTION_PATH = "/org/syncevolution/Connection";
42     const char * const CONNECTION_IFACE = "org.syncevolution.Connection";
43     const char * const SESSION_PATH = "/org/syncevolution/Session";
44     const char * const SESSION_IFACE = "org.syncevolution.Session";
45     const char * const SERVER_PATH = "/org/syncevolution/Server";
46     const char * const SERVER_IFACE = "org.syncevolution.Server";
47
48     const char * const HELPER_PATH = "/dbushelper";
49     const char * const HELPER_IFACE = "org.syncevolution.Helper";
50     const char * const HELPER_DESTINATION = "direct.peer"; // doesn't matter, routing is off
51
52     /**
53      * The operation running inside the session.
54      */
55     enum RunOperation {
56         OP_SYNC,            /**< running a sync */
57         OP_RESTORE,         /**< restoring data */
58         OP_CMDLINE,         /**< executing command line */
59         OP_NULL             /**< idle, accepting commands via D-Bus */
60     };
61
62     inline std::string runOpToString(RunOperation op) {
63         static const char * const strings[] = {
64             "sync",
65             "restore",
66             "cmdline"
67         };
68         return op >= OP_SYNC && op <= OP_CMDLINE ?
69             strings[op] :
70             "";
71     }
72
73     /**
74      * Used by both Connection class (inside server) and
75      * DBusTransportAgent (inside helper).
76      */
77     enum ConnectionState {
78         SETUP,          /**< ready for first message */
79         PROCESSING,     /**< received message, waiting for engine's reply */
80         WAITING,        /**< waiting for next follow-up message */
81         FINAL,          /**< engine has sent final reply, wait for ACK by peer */
82         DONE,           /**< peer has closed normally after the final reply */
83         FAILED          /**< in a failed state, no further operation possible */
84     };
85
86     /** maps to names for debugging */
87     inline std::string ConnectionStateToString(ConnectionState state)
88     {
89         static const char * const strings[] = {
90             "SETUP",
91             "PROCESSING",
92             "WAITING",
93             "FINAL",
94             "DONE",
95             "FAILED"
96         };
97         return state >= SETUP && state <= FAILED ?
98             strings[state] :
99             "???";
100     }
101
102     typedef StringMap SourceModes_t;
103     typedef std::map<std::string, SyncEvo::FilterConfigNode::ConfigFilter> SourceFilters_t;
104
105     /**
106      * all the information that syncevo-dbus-server needs to
107      * send to syncevo-dbus-helper before the latter can
108      * run a sync
109      */
110     struct SyncParams
111     {
112         SyncParams() :
113            m_serverMode(false),
114            m_serverAlerted(false),
115            m_remoteInitiated(false)
116         {}
117
118         std::string m_config;
119         std::string m_mode;
120         SourceModes_t m_sourceModes;
121         bool m_serverMode;
122         bool m_serverAlerted;
123         bool m_remoteInitiated;
124         std::string m_sessionID;
125         SharedBuffer m_initialMessage;
126         std::string m_initialMessageType;
127         SyncEvo::FilterConfigNode::ConfigFilter m_syncFilter;
128         SyncEvo::FilterConfigNode::ConfigFilter m_sourceFilter;
129         SourceFilters_t m_sourceFilters;
130     };
131 }
132
133 SE_END_CXX
134
135 namespace GDBusCXX {
136     using namespace SyncEvo::SessionCommon;
137     using namespace SyncEvo;
138     template<> struct dbus_traits<SyncParams> :
139         public dbus_struct_traits<SyncParams,
140         dbus_member<SyncParams, std::string, &SyncParams::m_config,
141         dbus_member<SyncParams, std::string, &SyncParams::m_mode,
142         dbus_member<SyncParams, SourceModes_t, &SyncParams::m_sourceModes,
143         dbus_member<SyncParams, bool, &SyncParams::m_serverMode,
144         dbus_member<SyncParams, bool, &SyncParams::m_serverAlerted,
145         dbus_member<SyncParams, bool, &SyncParams::m_remoteInitiated,
146         dbus_member<SyncParams, std::string, &SyncParams::m_sessionID,
147         dbus_member<SyncParams, SharedBuffer, &SyncParams::m_initialMessage,
148         dbus_member<SyncParams, std::string, &SyncParams::m_initialMessageType,
149         dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_syncFilter,
150         dbus_member<SyncParams, FilterConfigNode::ConfigFilter, &SyncParams::m_sourceFilter,
151         dbus_member_single<SyncParams, SourceFilters_t, &SyncParams::m_sourceFilters
152         > > > > > > > > > > > > >
153         {};
154
155     /**
156      * Similar to DBusArray<uint8_t>, but with different native
157      * types. Uses encoding/decoding from the base class, copies
158      * to/from SharedBuffer as needed.
159      *
160      * DBusArray<uint8_t> is more efficient because it avoids
161      * copying the bytes from the D-Bus message when decoding,
162      * but it is harder to use natively (cannot be copied).
163      * SharedBuffer does ref counting for the memory chunk,
164      * so once initialized, copying it is cheap.
165      */
166     template <> struct dbus_traits<SharedBuffer> :
167         public dbus_traits< DBusArray<uint8_t> >
168     {
169         typedef dbus_traits< DBusArray<uint8_t> > base;
170
171         typedef SharedBuffer host_type;
172         typedef const SharedBuffer &arg_type;
173
174 #ifdef GDBUS_CXX_GIO
175         static void get(GDBusCXX::ExtractArgs &context,
176                         GDBusCXX::reader_type &iter, host_type &buffer)
177         {
178             base::host_type array;
179             base::get(context, iter, array);
180             buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
181         }
182 #else
183         static void get(GDBusCXX::connection_type *conn, GDBusCXX::message_type *msg,
184                         GDBusCXX::reader_type &iter, host_type &buffer)
185         {
186             base::host_type array;
187             base::get(conn, msg, iter, array);
188             buffer = SharedBuffer(reinterpret_cast<const char *>(array.second), array.first);
189         }
190 #endif
191
192         static void append(GDBusCXX::builder_type &builder, arg_type buffer)
193         {
194             base::host_type array(buffer.size(), reinterpret_cast<const uint8_t *>(buffer.get()));
195             base::append(builder, array);
196         }
197     };
198 }
199
200 #endif // SESSION_COMMON_H