lrc.cpp 5.3 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
/****************************************************************************
2
 *    Copyright (C) 2017-2019 Savoir-faire Linux Inc.                       *
Nicolas Jager's avatar
Nicolas Jager committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *   Author : Nicolas Jäger <nicolas.jager@savoirfairelinux.com>            *
 *   Author : Sébastien Blin <sebastien.blin@savoirfairelinux.com>          *
 *                                                                          *
 *   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) 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 General Public License      *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#include "api/lrc.h"

21
22
23
24
25
26
#ifndef _MSC_VER
#include <unistd.h>
#else
#include "../../daemon/MSVC/unistd.h"
#endif // !_MSC_VER

27
28
#include "call_const.h"

Nicolas Jager's avatar
Nicolas Jager committed
29
// Models and database
30
#include "api/avmodel.h"
31
#include "api/behaviorcontroller.h"
32
33
#include "api/datatransfermodel.h"
#include "api/newaccountmodel.h"
Nicolas Jager's avatar
Nicolas Jager committed
34
#include "callbackshandler.h"
35
#include "dbus/callmanager.h"
36
#include "dbus/configurationmanager.h"
37
#include "dbus/instancemanager.h"
38
#include "dbus/configurationmanager.h"
39
#include "authority/storagehelper.h"
Nicolas Jager's avatar
Nicolas Jager committed
40
41
42
43
44
45

namespace lrc
{

using namespace api;

46
47
48
// To judge whether the call is finished or not depending on callState
bool isFinished(const QString& callState);

Nicolas Jager's avatar
Nicolas Jager committed
49
50
51
52
class LrcPimpl
{

public:
53
    LrcPimpl(Lrc& linked, MigrationCb& willMigrateCb, MigrationCb& didMigrateCb);
Nicolas Jager's avatar
Nicolas Jager committed
54

Nicolas Jager's avatar
Nicolas Jager committed
55
    const Lrc& linked;
56
    std::unique_ptr<BehaviorController> behaviorController;
Nicolas Jager's avatar
Nicolas Jager committed
57
    std::unique_ptr<CallbacksHandler> callbackHandler;
Nicolas Jager's avatar
Nicolas Jager committed
58
    std::unique_ptr<NewAccountModel> accountModel;
Anthony Léonard's avatar
Anthony Léonard committed
59
    std::unique_ptr<DataTransferModel> dataTransferModel;
60
    std::unique_ptr<AVModel> AVModel_;
61

Nicolas Jager's avatar
Nicolas Jager committed
62
63
};

64
Lrc::Lrc(MigrationCb willDoMigrationCb, MigrationCb didDoMigrationCb)
Nicolas Jager's avatar
Nicolas Jager committed
65
{
66
67
68
    // Ensure Daemon is running/loaded (especially on non-DBus platforms)
    // before instantiating LRC and its members
    InstanceManager::instance();
69
    lrcPimpl_ = std::make_unique<LrcPimpl>(*this, willDoMigrationCb, didDoMigrationCb);
Nicolas Jager's avatar
Nicolas Jager committed
70
71
72
73
}

Lrc::~Lrc()
{
74
75
76
77
78
79
    //Unregister from the daemon
    InstanceManagerInterface& instance = InstanceManager::instance();
    Q_NOREPLY instance.Unregister(getpid());
#ifndef ENABLE_LIBWRAP
    instance.connection().disconnectFromBus(instance.connection().baseService());
#endif //ENABLE_LIBWRAP
Nicolas Jager's avatar
Nicolas Jager committed
80
81
}

82
NewAccountModel&
Nicolas Jager's avatar
Nicolas Jager committed
83
Lrc::getAccountModel() const
Nicolas Jager's avatar
Nicolas Jager committed
84
{
85
    return *lrcPimpl_->accountModel;
Nicolas Jager's avatar
Nicolas Jager committed
86
87
}

88
BehaviorController&
89
90
91
92
93
Lrc::getBehaviorController() const
{
    return *lrcPimpl_->behaviorController;
}

Anthony Léonard's avatar
Anthony Léonard committed
94
95
96
97
98
99
DataTransferModel&
Lrc::getDataTransferModel() const
{
    return *lrcPimpl_->dataTransferModel;
}

100
101
102
103
104
105
AVModel&
Lrc::getAVModel() const
{
    return *lrcPimpl_->AVModel_;
}

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
void
Lrc::connectivityChanged() const
{
    ConfigurationManager::instance().connectivityChanged();
}

bool
Lrc::isConnected()
{
#ifdef ENABLE_LIBWRAP
    return true;
#else
    return ConfigurationManager::instance().connection().isConnected();
#endif
}

bool
Lrc::dbusIsValid()
{
#ifdef ENABLE_LIBWRAP
    return true;
#else
    return ConfigurationManager::instance().isValid();
#endif
}

132
133
134
135
136
137
void
Lrc::subscribeToDebugReceived()
{
    lrcPimpl_->callbackHandler->subscribeToDebugReceived();
}

138
139
140
141
142
143
144
std::vector<std::string>
Lrc::activeCalls()
{
    QStringList callLists = CallManager::instance().getCallList();
    std::vector<std::string> result;
    result.reserve(callLists.size());
    for (const auto &call : callLists) {
145
146
147
        MapStringString callDetails = CallManager::instance().getCallDetails(call);
        if(!isFinished(callDetails[QString(DRing::Call::Details::CALL_STATE)]))
            result.emplace_back(call.toStdString());
148
149
150
151
    }
    return result;
}

152
153
154
155
156
157
158
159
160
161
162
163
164
165
bool
isFinished(const QString& callState)
{
    if (callState == QLatin1String(DRing::Call::StateEvent::HUNGUP) ||
        callState == QLatin1String(DRing::Call::StateEvent::BUSY) ||
        callState == QLatin1String(DRing::Call::StateEvent::PEER_BUSY) ||
        callState == QLatin1String(DRing::Call::StateEvent::FAILURE) ||
        callState == QLatin1String(DRing::Call::StateEvent::INACTIVE) ||
        callState == QLatin1String(DRing::Call::StateEvent::OVER)) {
        return true;
    }
    return false;
}

166
LrcPimpl::LrcPimpl(Lrc& linked, MigrationCb& willMigrateCb, MigrationCb& didMigrateCb)
Nicolas Jager's avatar
Nicolas Jager committed
167
: linked(linked)
168
, behaviorController(std::make_unique<BehaviorController>())
169
, callbackHandler(std::make_unique<CallbacksHandler>(linked))
170
, accountModel(std::make_unique<NewAccountModel>(linked, *callbackHandler, *behaviorController, willMigrateCb, didMigrateCb))
Anthony Léonard's avatar
Anthony Léonard committed
171
, dataTransferModel {std::make_unique<DataTransferModel>()}
Sébastien Blin's avatar
Sébastien Blin committed
172
, AVModel_ {std::make_unique<AVModel>(*callbackHandler)}
Nicolas Jager's avatar
Nicolas Jager committed
173
174
175
{
}

176
} // namespace lrc