core.cpp 2.46 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * core.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

20
#include "core-p.h"
21
#include "logger/logger.h"
22
#include "paths/paths.h"
Ronan's avatar
Ronan committed
23

24
// TODO: Remove me later.
25
#include "c-wrapper/c-wrapper.h"
26

Ronan's avatar
Ronan committed
27 28
// =============================================================================

29 30
#define LINPHONE_DB "linphone.db"

31 32
using namespace std;

Ronan's avatar
Ronan committed
33 34
LINPHONE_BEGIN_NAMESPACE

Ronan's avatar
Ronan committed
35 36
Core::Core () : Object(*new CorePrivate) {}

37 38 39 40
Core::~Core () {
	lInfo() << "Destroying core: " << this;
}

Ronan's avatar
Ronan committed
41 42 43 44
shared_ptr<Core> Core::create (LinphoneCore *cCore) {
	// Do not use `make_shared` => Private constructor.
	shared_ptr<Core> core = shared_ptr<Core>(new Core);

45
	CorePrivate *const d = core->getPrivate();
Ronan's avatar
Ronan committed
46

47
	d->cCore = cCore;
Ronan's avatar
Ronan committed
48
	d->mainDb.reset(new MainDb(core->getSharedFromThis()));
49 50

	AbstractDb::Backend backend;
Erwan Croze's avatar
Erwan Croze committed
51
	string uri = L_C_TO_STRING(lp_config_get_string(linphone_core_get_config(d->cCore), "storage", "uri", nullptr));
52
	if (!uri.empty())
Erwan Croze's avatar
Erwan Croze committed
53
		backend = strcmp(lp_config_get_string(linphone_core_get_config(d->cCore), "storage", "backend", nullptr), "mysql") == 0
54 55
			? MainDb::Mysql
			: MainDb::Sqlite3;
56 57
	else {
		backend = AbstractDb::Sqlite3;
Ronan's avatar
Ronan committed
58
		uri = core->getDataPath() + LINPHONE_DB;
59
	}
60

61 62 63 64 65 66
	lInfo() << "Opening " LINPHONE_DB " at: " << uri;
	if (!d->mainDb->connect(backend, uri))
		lFatal() << "Unable to open linphone database.";

	for (auto &chatRoom : d->mainDb->getChatRooms())
		d->insertChatRoom(chatRoom);
Ronan's avatar
Ronan committed
67 68

	return core;
69
}
Ronan's avatar
Ronan committed
70

71 72 73 74 75
LinphoneCore *Core::getCCore () const {
	L_D();
	return d->cCore;
}

76 77 78
string Core::getDataPath() const {
	L_D();
	return Paths::getPath(Paths::Data, static_cast<PlatformHelpers *>(d->cCore->platform_helper));
79 80
}

81 82 83
string Core::getConfigPath() const {
	L_D();
	return Paths::getPath(Paths::Config, static_cast<PlatformHelpers *>(d->cCore->platform_helper));
84 85
}

Ronan's avatar
Ronan committed
86
LINPHONE_END_NAMESPACE