Commit 370357a9 authored by Ghislain MARY's avatar Ghislain MARY

Respect freedesktop standards for configuration and user data files, and...

Respect freedesktop standards for configuration and user data files, and handle migration from old location to the new one.

The migration is deactivated for the moment and needs to be enabled when the app is ready to be used.
parent a7ea607c
......@@ -205,6 +205,9 @@ void App::parseArgs () {
m_parser.process(*this);
// Initialise logger (do not do this before this point because the application has to be created
// for the logs to be put in the correct directory
Logger::init();
if (m_parser.isSet("verbose")) {
Logger::instance()->setVerbose(true);
}
......
......@@ -23,6 +23,7 @@
#include <QDir>
#include <QFile>
#include <QStandardPaths>
#include <QtDebug>
#include "../utils.hpp"
......@@ -30,36 +31,39 @@
// =============================================================================
#ifdef _WIN32
#define MAIN_PATH \
(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/")
#define PATH_CONFIG "linphonerc"
#define LINPHONE_FOLDER "linphone/"
using namespace std;
#else
#define PATH_AVATARS "/avatars/"
#define PATH_CAPTURES "/captures/"
#define PATH_LOGS "/logs/"
#define PATH_THUMBNAILS "/thumbnails/"
#define MAIN_PATH \
(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/")
#define PATH_CONFIG ".linphonerc"
#define PATH_CONFIG "/linphonerc"
#define PATH_CALL_HISTORY_LIST "/call-history.db"
#define PATH_FRIENDS_LIST "/friends.db"
#define PATH_MESSAGE_HISTORY_LIST "/message-history.db"
#define LINPHONE_FOLDER ".linphone/"
#define PATH_ZRTP_SECRETS "/zidcache"
#define PATH_USER_CERTIFICATES "/usr-crt/"
#endif // ifdef _WIN32
// =============================================================================
#define PATH_AVATARS (LINPHONE_FOLDER "avatars/")
#define PATH_CAPTURES (LINPHONE_FOLDER "captures/")
#define PATH_LOGS (LINPHONE_FOLDER "logs/")
#define PATH_THUMBNAILS (LINPHONE_FOLDER "thumbnails/")
inline bool directoryPathExists (const QString &path) {
QDir dir(path);
return dir.exists();
}
#define PATH_CALL_HISTORY_LIST ".linphone-call-history.db"
#define PATH_FRIENDS_LIST ".linphone-friends.db"
#define PATH_MESSAGE_HISTORY_LIST ".linphone-history.db"
inline bool filePathExists (const QString &path) {
QFileInfo info(path);
if (!directoryPathExists(info.path())) return false;
using namespace std;
QFile file(path);
return file.exists();
}
// =============================================================================
inline bool filePathExists (const string &path) {
return filePathExists(Utils::linphoneStringToQString(path));
}
inline void ensureDirectoryPathExists (const QString &path) {
QDir dir(path);
......@@ -86,39 +90,120 @@ inline string getFilePath (const QString &filename) {
return Utils::qStringToLinphoneString(QDir::toNativeSeparators(filename));
}
static QString getAppConfigFilepath () {
if (QSysInfo::productType() == "macos") {
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_CONFIG;
} else {
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + PATH_CONFIG;
}
}
static QString getAppCallHistoryFilepath () {
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_CALL_HISTORY_LIST;
}
static QString getAppFriendsFilepath () {
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_FRIENDS_LIST;
}
static QString getAppMessageHistoryFilepath () {
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_MESSAGE_HISTORY_LIST;
}
// -----------------------------------------------------------------------------
string Paths::getAvatarsDirpath () {
return getDirectoryPath(MAIN_PATH + PATH_AVATARS);
return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_AVATARS);
}
string Paths::getCallHistoryFilepath () {
return getFilePath(MAIN_PATH + PATH_CALL_HISTORY_LIST);
return getFilePath(getAppCallHistoryFilepath());
}
string Paths::getConfigFilepath (const QString &configPath) {
if (!configPath.isEmpty()) {
return getFilePath(QFileInfo(configPath).absoluteFilePath());
}
return getFilePath(MAIN_PATH + PATH_CONFIG);
return getFilePath(getAppConfigFilepath());
}
string Paths::getFriendsListFilepath () {
return getFilePath(MAIN_PATH + PATH_FRIENDS_LIST);
return getFilePath(getAppFriendsFilepath());
}
string Paths::getLogsDirpath () {
return getDirectoryPath(MAIN_PATH + PATH_LOGS);
return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_LOGS);
}
string Paths::getMessageHistoryFilepath () {
return getFilePath(MAIN_PATH + PATH_MESSAGE_HISTORY_LIST);
return getFilePath(getAppMessageHistoryFilepath());
}
string Paths::getThumbnailsDirpath () {
return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_THUMBNAILS);
}
string Paths::getCapturesDirpath () {
return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_CAPTURES);
}
string Paths::getThumbnailsDirPath () {
return getDirectoryPath(MAIN_PATH + PATH_THUMBNAILS);
string Paths::getZrtpSecretsFilepath () {
return getFilePath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + PATH_ZRTP_SECRETS);
}
string Paths::getCapturesDirPath () {
return getDirectoryPath(MAIN_PATH + PATH_CAPTURES);
string Paths::getUserCertificatesDirpath () {
return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_USER_CERTIFICATES);
}
// -----------------------------------------------------------------------------
static void migrateFile (const QString &oldPath, const QString &newPath) {
QFileInfo info(newPath);
ensureDirectoryPathExists(info.path());
if (QFile::copy(oldPath, newPath)) {
QFile::remove(oldPath);
qInfo() << "Migrated" << oldPath << "to" << newPath;
} else {
qWarning() << "Failed migration of" << oldPath << "to" << newPath;
}
}
static void migrateConfigurationFile (const QString &oldPath, const QString &newPath) {
QFileInfo info(newPath);
ensureDirectoryPathExists(info.path());
if (QFile::copy(oldPath, newPath)) {
QFile oldFile(oldPath);
if (oldFile.open(QIODevice::WriteOnly)) {
QTextStream stream(&oldFile);
stream << "This file has been migrated to " << newPath;
}
QFile::setPermissions(oldPath, QFileDevice::ReadOwner);
qInfo() << "Migrated" << oldPath << "to" << newPath;
} else {
qWarning() << "Failed migration of" << oldPath << "to" << newPath;
}
}
void Paths::migrate () {
QString newPath;
QString oldPath;
QString oldBaseDir;
if (QSysInfo::productType() == "windows") {
oldBaseDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
} else {
oldBaseDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}
newPath = getAppConfigFilepath();
oldPath = oldBaseDir + "/.linphonerc";
if (!filePathExists(newPath) && filePathExists(oldPath)) migrateConfigurationFile(oldPath, newPath);
newPath = getAppCallHistoryFilepath();
oldPath = oldBaseDir + "/.linphone-call-history.db";
if (!filePathExists(newPath) && filePathExists(oldPath)) migrateFile(oldPath, newPath);
newPath = getAppFriendsFilepath();
oldPath = oldBaseDir + "/.linphone-friends.db";
if (!filePathExists(newPath) && filePathExists(oldPath)) migrateFile(oldPath, newPath);
newPath = getAppMessageHistoryFilepath();
oldPath = oldBaseDir + "/.linphone-history.db";
if (!filePathExists(newPath) && filePathExists(oldPath)) migrateFile(oldPath, newPath);
}
......@@ -31,12 +31,16 @@
namespace Paths {
std::string getAvatarsDirpath ();
std::string getCallHistoryFilepath ();
std::string getConfigFilepath (const QString &configPath);
std::string getConfigFilepath (const QString &configPath = QString());
std::string getFriendsListFilepath ();
std::string getCapturesDirPath ();
std::string getCapturesDirpath ();
std::string getLogsDirpath ();
std::string getMessageHistoryFilepath ();
std::string getThumbnailsDirPath ();
std::string getThumbnailsDirpath ();
std::string getZrtpSecretsFilepath ();
std::string getUserCertificatesDirpath ();
void migrate ();
}
#endif // PATHS_H_
......@@ -33,7 +33,7 @@ ThumbnailProvider::ThumbnailProvider () : QQuickImageProvider(
QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading
) {
m_thumbnails_path = Utils::linphoneStringToQString(Paths::getThumbnailsDirPath());
m_thumbnails_path = Utils::linphoneStringToQString(Paths::getThumbnailsDirpath());
}
QImage ThumbnailProvider::requestImage (const QString &id, QSize *, const QSize &) {
......
......@@ -70,7 +70,7 @@ inline void createThumbnail (const shared_ptr<linphone::ChatMessage> &message) {
QString uuid = QUuid::createUuid().toString();
QString file_id = QStringLiteral("%1.jpg").arg(uuid.mid(1, uuid.length() - 2));
if (!thumbnail.save(::Utils::linphoneStringToQString(Paths::getThumbnailsDirPath()) + file_id, "jpg", 100)) {
if (!thumbnail.save(::Utils::linphoneStringToQString(Paths::getThumbnailsDirpath()) + file_id, "jpg", 100)) {
qWarning() << QStringLiteral("Unable to create thumbnail of: `%1`.").arg(thumbnail_path);
return;
}
......@@ -84,7 +84,7 @@ inline void removeFileMessageThumbnail (const shared_ptr<linphone::ChatMessage>
string file_id = message->getAppdata();
if (!file_id.empty()) {
QString thumbnail_path = ::Utils::linphoneStringToQString(Paths::getThumbnailsDirPath() + file_id);
QString thumbnail_path = ::Utils::linphoneStringToQString(Paths::getThumbnailsDirpath() + file_id);
if (!QFile::remove(thumbnail_path))
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(thumbnail_path);
}
......
......@@ -36,6 +36,9 @@ using namespace std;
CoreManager *CoreManager::m_instance = nullptr;
CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject(parent), m_handlers(make_shared<CoreHandlers>()) {
// TODO: activate migration when ready to switch to this new version
//Paths::migrate();
setResourcesPaths();
m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(config_path), "");
......@@ -44,6 +47,7 @@ CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject
m_core->usePreviewWindow(true);
setDatabasesPaths();
setOtherPaths();
}
void CoreManager::enableHandlers () {
......@@ -80,6 +84,11 @@ void CoreManager::setDatabasesPaths () {
m_core->setChatDatabasePath(Paths::getMessageHistoryFilepath());
}
void CoreManager::setOtherPaths () {
m_core->setZrtpSecretsFile(Paths::getZrtpSecretsFilepath());
m_core->setUserCertificatesPath(Paths::getUserCertificatesDirpath());
}
void CoreManager::setResourcesPaths () {
QDir dir(QCoreApplication::applicationDirPath());
if (dir.dirName() == "MacOS") {
......
......@@ -89,6 +89,7 @@ private:
CoreManager (QObject *parent, const QString &config_path);
void setDatabasesPaths ();
void setOtherPaths ();
void setResourcesPaths ();
std::shared_ptr<linphone::Core> m_core;
......
......@@ -572,7 +572,7 @@ void SettingsModel::setDscpVideo (int dscp) {
QString SettingsModel::getSavedScreenshotsFolder () const {
return QDir::cleanPath(
::Utils::linphoneStringToQString(
m_config->getString(UI_SECTION, "saved_screenshots_folder", Paths::getCapturesDirPath())
m_config->getString(UI_SECTION, "saved_screenshots_folder", Paths::getCapturesDirpath())
)
) + QDir::separator();
}
......@@ -589,7 +589,7 @@ void SettingsModel::setSavedScreenshotsFolder (const QString &folder) {
QString SettingsModel::getSavedVideosFolder () const {
return QDir::cleanPath(
::Utils::linphoneStringToQString(
m_config->getString(UI_SECTION, "saved_videos_folder", Paths::getCapturesDirPath())
m_config->getString(UI_SECTION, "saved_videos_folder", Paths::getCapturesDirpath())
)
) + QDir::separator();
}
......
......@@ -30,8 +30,6 @@ using namespace std;
// =============================================================================
int main (int argc, char *argv[]) {
Logger::init();
// Force OpenGLES & shader version 2.0.
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES, true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment