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 () { ...@@ -205,6 +205,9 @@ void App::parseArgs () {
m_parser.process(*this); 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")) { if (m_parser.isSet("verbose")) {
Logger::instance()->setVerbose(true); Logger::instance()->setVerbose(true);
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
#include <QtDebug>
#include "../utils.hpp" #include "../utils.hpp"
...@@ -30,36 +31,39 @@ ...@@ -30,36 +31,39 @@
// ============================================================================= // =============================================================================
#ifdef _WIN32 using namespace std;
#define MAIN_PATH \
(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/")
#define PATH_CONFIG "linphonerc"
#define LINPHONE_FOLDER "linphone/"
#else #define PATH_AVATARS "/avatars/"
#define PATH_CAPTURES "/captures/"
#define PATH_LOGS "/logs/"
#define PATH_THUMBNAILS "/thumbnails/"
#define MAIN_PATH \ #define PATH_CONFIG "/linphonerc"
(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/") #define PATH_CALL_HISTORY_LIST "/call-history.db"
#define PATH_CONFIG ".linphonerc" #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/") inline bool directoryPathExists (const QString &path) {
#define PATH_CAPTURES (LINPHONE_FOLDER "captures/") QDir dir(path);
#define PATH_LOGS (LINPHONE_FOLDER "logs/") return dir.exists();
#define PATH_THUMBNAILS (LINPHONE_FOLDER "thumbnails/") }
#define PATH_CALL_HISTORY_LIST ".linphone-call-history.db" inline bool filePathExists (const QString &path) {
#define PATH_FRIENDS_LIST ".linphone-friends.db" QFileInfo info(path);
#define PATH_MESSAGE_HISTORY_LIST ".linphone-history.db" 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) { inline void ensureDirectoryPathExists (const QString &path) {
QDir dir(path); QDir dir(path);
...@@ -86,39 +90,120 @@ inline string getFilePath (const QString &filename) { ...@@ -86,39 +90,120 @@ inline string getFilePath (const QString &filename) {
return Utils::qStringToLinphoneString(QDir::toNativeSeparators(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 () { string Paths::getAvatarsDirpath () {
return getDirectoryPath(MAIN_PATH + PATH_AVATARS); return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_AVATARS);
} }
string Paths::getCallHistoryFilepath () { string Paths::getCallHistoryFilepath () {
return getFilePath(MAIN_PATH + PATH_CALL_HISTORY_LIST); return getFilePath(getAppCallHistoryFilepath());
} }
string Paths::getConfigFilepath (const QString &configPath) { string Paths::getConfigFilepath (const QString &configPath) {
if (!configPath.isEmpty()) { if (!configPath.isEmpty()) {
return getFilePath(QFileInfo(configPath).absoluteFilePath()); return getFilePath(QFileInfo(configPath).absoluteFilePath());
} }
return getFilePath(MAIN_PATH + PATH_CONFIG); return getFilePath(getAppConfigFilepath());
} }
string Paths::getFriendsListFilepath () { string Paths::getFriendsListFilepath () {
return getFilePath(MAIN_PATH + PATH_FRIENDS_LIST); return getFilePath(getAppFriendsFilepath());
} }
string Paths::getLogsDirpath () { string Paths::getLogsDirpath () {
return getDirectoryPath(MAIN_PATH + PATH_LOGS); return getDirectoryPath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + PATH_LOGS);
} }
string Paths::getMessageHistoryFilepath () { 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::getZrtpSecretsFilepath () {
return getFilePath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + PATH_ZRTP_SECRETS);
}
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;
}
} }
string Paths::getThumbnailsDirPath () { static void migrateConfigurationFile (const QString &oldPath, const QString &newPath) {
return getDirectoryPath(MAIN_PATH + PATH_THUMBNAILS); 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;
}
} }
string Paths::getCapturesDirPath () { void Paths::migrate () {
return getDirectoryPath(MAIN_PATH + PATH_CAPTURES); 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 @@ ...@@ -31,12 +31,16 @@
namespace Paths { namespace Paths {
std::string getAvatarsDirpath (); std::string getAvatarsDirpath ();
std::string getCallHistoryFilepath (); std::string getCallHistoryFilepath ();
std::string getConfigFilepath (const QString &configPath); std::string getConfigFilepath (const QString &configPath = QString());
std::string getFriendsListFilepath (); std::string getFriendsListFilepath ();
std::string getCapturesDirPath (); std::string getCapturesDirpath ();
std::string getLogsDirpath (); std::string getLogsDirpath ();
std::string getMessageHistoryFilepath (); std::string getMessageHistoryFilepath ();
std::string getThumbnailsDirPath (); std::string getThumbnailsDirpath ();
std::string getZrtpSecretsFilepath ();
std::string getUserCertificatesDirpath ();
void migrate ();
} }
#endif // PATHS_H_ #endif // PATHS_H_
...@@ -33,7 +33,7 @@ ThumbnailProvider::ThumbnailProvider () : QQuickImageProvider( ...@@ -33,7 +33,7 @@ ThumbnailProvider::ThumbnailProvider () : QQuickImageProvider(
QQmlImageProviderBase::Image, QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading 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 &) { QImage ThumbnailProvider::requestImage (const QString &id, QSize *, const QSize &) {
......
...@@ -70,7 +70,7 @@ inline void createThumbnail (const shared_ptr<linphone::ChatMessage> &message) { ...@@ -70,7 +70,7 @@ inline void createThumbnail (const shared_ptr<linphone::ChatMessage> &message) {
QString uuid = QUuid::createUuid().toString(); QString uuid = QUuid::createUuid().toString();
QString file_id = QStringLiteral("%1.jpg").arg(uuid.mid(1, uuid.length() - 2)); 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); qWarning() << QStringLiteral("Unable to create thumbnail of: `%1`.").arg(thumbnail_path);
return; return;
} }
...@@ -84,7 +84,7 @@ inline void removeFileMessageThumbnail (const shared_ptr<linphone::ChatMessage> ...@@ -84,7 +84,7 @@ inline void removeFileMessageThumbnail (const shared_ptr<linphone::ChatMessage>
string file_id = message->getAppdata(); string file_id = message->getAppdata();
if (!file_id.empty()) { 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)) if (!QFile::remove(thumbnail_path))
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(thumbnail_path); qWarning() << QStringLiteral("Unable to remove `%1`.").arg(thumbnail_path);
} }
......
...@@ -36,6 +36,9 @@ using namespace std; ...@@ -36,6 +36,9 @@ using namespace std;
CoreManager *CoreManager::m_instance = nullptr; CoreManager *CoreManager::m_instance = nullptr;
CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject(parent), m_handlers(make_shared<CoreHandlers>()) { 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(); setResourcesPaths();
m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(config_path), ""); 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 ...@@ -44,6 +47,7 @@ CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject
m_core->usePreviewWindow(true); m_core->usePreviewWindow(true);
setDatabasesPaths(); setDatabasesPaths();
setOtherPaths();
} }
void CoreManager::enableHandlers () { void CoreManager::enableHandlers () {
...@@ -80,6 +84,11 @@ void CoreManager::setDatabasesPaths () { ...@@ -80,6 +84,11 @@ void CoreManager::setDatabasesPaths () {
m_core->setChatDatabasePath(Paths::getMessageHistoryFilepath()); m_core->setChatDatabasePath(Paths::getMessageHistoryFilepath());
} }
void CoreManager::setOtherPaths () {
m_core->setZrtpSecretsFile(Paths::getZrtpSecretsFilepath());
m_core->setUserCertificatesPath(Paths::getUserCertificatesDirpath());
}
void CoreManager::setResourcesPaths () { void CoreManager::setResourcesPaths () {
QDir dir(QCoreApplication::applicationDirPath()); QDir dir(QCoreApplication::applicationDirPath());
if (dir.dirName() == "MacOS") { if (dir.dirName() == "MacOS") {
......
...@@ -89,6 +89,7 @@ private: ...@@ -89,6 +89,7 @@ private:
CoreManager (QObject *parent, const QString &config_path); CoreManager (QObject *parent, const QString &config_path);
void setDatabasesPaths (); void setDatabasesPaths ();
void setOtherPaths ();
void setResourcesPaths (); void setResourcesPaths ();
std::shared_ptr<linphone::Core> m_core; std::shared_ptr<linphone::Core> m_core;
......
...@@ -572,7 +572,7 @@ void SettingsModel::setDscpVideo (int dscp) { ...@@ -572,7 +572,7 @@ void SettingsModel::setDscpVideo (int dscp) {
QString SettingsModel::getSavedScreenshotsFolder () const { QString SettingsModel::getSavedScreenshotsFolder () const {
return QDir::cleanPath( return QDir::cleanPath(
::Utils::linphoneStringToQString( ::Utils::linphoneStringToQString(
m_config->getString(UI_SECTION, "saved_screenshots_folder", Paths::getCapturesDirPath()) m_config->getString(UI_SECTION, "saved_screenshots_folder", Paths::getCapturesDirpath())
) )
) + QDir::separator(); ) + QDir::separator();
} }
...@@ -589,7 +589,7 @@ void SettingsModel::setSavedScreenshotsFolder (const QString &folder) { ...@@ -589,7 +589,7 @@ void SettingsModel::setSavedScreenshotsFolder (const QString &folder) {
QString SettingsModel::getSavedVideosFolder () const { QString SettingsModel::getSavedVideosFolder () const {
return QDir::cleanPath( return QDir::cleanPath(
::Utils::linphoneStringToQString( ::Utils::linphoneStringToQString(
m_config->getString(UI_SECTION, "saved_videos_folder", Paths::getCapturesDirPath()) m_config->getString(UI_SECTION, "saved_videos_folder", Paths::getCapturesDirpath())
) )
) + QDir::separator(); ) + QDir::separator();
} }
......
...@@ -30,8 +30,6 @@ using namespace std; ...@@ -30,8 +30,6 @@ using namespace std;
// ============================================================================= // =============================================================================
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
Logger::init();
// Force OpenGLES & shader version 2.0. // Force OpenGLES & shader version 2.0.
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES, true); 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