Browse Source

refactor: use correct paths provided by Paths

reviewable/pr6649/r6
sudden6 8 years ago
parent
commit
d683e3b57a
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 3
      src/model/profile/profileinfo.cpp
  2. 54
      src/persistence/profile.cpp
  3. 4
      src/persistence/profile.h
  4. 3
      src/persistence/profilelocker.cpp
  5. 2
      src/persistence/settings.cpp
  6. 3
      src/persistence/settings.h
  7. 6
      src/widget/form/profileform.cpp
  8. 5
      src/widget/tool/profileimporter.cpp

3
src/model/profile/profileinfo.cpp

@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
#include "profileinfo.h"
#include "src/core/core.h"
#include "src/nexus.h"
#include "src/persistence/paths.h"
#include "src/persistence/profile.h"
#include "src/persistence/settings.h"
@ -202,7 +203,7 @@ IProfileInfo::SaveResult ProfileInfo::exportProfile(const QString& path) const @@ -202,7 +203,7 @@ IProfileInfo::SaveResult ProfileInfo::exportProfile(const QString& path) const
return SaveResult::NoWritePermission;
}
if (!QFile::copy(Settings::getInstance().getSettingsDirPath() + current, path)) {
if (!QFile::copy(Settings::getInstance().getPaths().getProfilesDir() + current, path)) {
return SaveResult::Error;
}

54
src/persistence/profile.cpp

@ -23,11 +23,13 @@ @@ -23,11 +23,13 @@
#include <QFileInfo>
#include <QObject>
#include <QSaveFile>
#include <QStringBuilder>
#include <QThread>
#include <cassert>
#include <sodium.h>
#include "paths.h"
#include "profile.h"
#include "profilelocker.h"
#include "settings.h"
@ -96,6 +98,16 @@ void Profile::initCore(const QByteArray& toxsave, ICoreSettings& s, bool isNewPr @@ -96,6 +98,16 @@ void Profile::initCore(const QByteArray& toxsave, ICoreSettings& s, bool isNewPr
Qt::ConnectionType::QueuedConnection);
}
QString Profile::getProfilePath(const QString &name)
{
return Settings::getInstance().getPaths().getProfilesDir() % name;
}
QString Profile::getToxSavePath(const QString &name)
{
return Settings::getInstance().getPaths().getToxSaveDir() % name % ".tox";
}
Profile::Profile(QString name, const QString& password, bool isNewProfile, const QByteArray& toxsave)
: name{name}
, isRemoved{false}
@ -135,7 +147,7 @@ Profile* Profile::loadProfile(QString name, const QString& password) @@ -135,7 +147,7 @@ Profile* Profile::loadProfile(QString name, const QString& password)
Profile* p = nullptr;
qint64 fileSize = 0;
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QString path = getToxSavePath(name);
QFile saveFile(path);
qDebug() << "Loading tox save " << path;
@ -259,9 +271,9 @@ Profile::~Profile() @@ -259,9 +271,9 @@ Profile::~Profile()
* @param extension Raw extension, e.g. "jpeg" not ".jpeg".
* @return Vector of filenames.
*/
QStringList Profile::getFilesByExt(QString extension)
QStringList Profile::getFilesByExt(const QString& directory, const QString& extension)
{
QDir dir(Settings::getInstance().getSettingsDirPath());
QDir dir{directory};
QStringList out;
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
dir.setNameFilters(QStringList("*." + extension));
@ -281,7 +293,11 @@ QStringList Profile::getFilesByExt(QString extension) @@ -281,7 +293,11 @@ QStringList Profile::getFilesByExt(QString extension)
void Profile::scanProfiles()
{
profiles.clear();
QStringList toxfiles = getFilesByExt("tox"), inifiles = getFilesByExt("ini");
const QString toxDir{Settings::getInstance().getPaths().getToxSaveDir()};
const QString profileDir{Settings::getInstance().getPaths().getProfilesDir()};
QStringList toxfiles = getFilesByExt(toxDir, "tox");
QStringList inifiles = getFilesByExt(profileDir, "ini");
for (QString toxfile : toxfiles) {
if (!inifiles.contains(toxfile)) {
Settings::getInstance().createPersonal(toxfile);
@ -358,7 +374,7 @@ bool Profile::saveToxSave(QByteArray data) @@ -358,7 +374,7 @@ bool Profile::saveToxSave(QByteArray data)
ProfileLocker::assertLock();
assert(ProfileLocker::getCurLockName() == name);
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QString path = getToxSavePath(name);
qDebug() << "Saving tox save to " << path;
QSaveFile saveFile(path);
if (!saveFile.open(QIODevice::WriteOnly)) {
@ -399,8 +415,9 @@ bool Profile::saveToxSave(QByteArray data) @@ -399,8 +415,9 @@ bool Profile::saveToxSave(QByteArray data)
QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted)
{
const QString ownerStr = owner.toString();
const QString avatarDir{Settings::getInstance().getPaths().getAvatarsDir()};
if (!encrypted || forceUnencrypted) {
return Settings::getInstance().getSettingsDirPath() + "avatars/" + ownerStr + ".png";
return avatarDir + ownerStr + ".png";
}
QByteArray idData = ownerStr.toUtf8();
@ -414,7 +431,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted) @@ -414,7 +431,7 @@ QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted)
QByteArray hash(hashSize, 0);
crypto_generichash((uint8_t*)hash.data(), hashSize, (uint8_t*)idData.data(), idData.size(),
(uint8_t*)pubkeyData.data(), pubkeyData.size());
return Settings::getInstance().getSettingsDirPath() + "avatars/" + hash.toHex().toUpper() + ".png";
return avatarDir + hash.toHex().toUpper() + ".png";
}
/**
@ -591,7 +608,7 @@ void Profile::saveAvatar(const ToxPk& owner, const QByteArray& avatar) @@ -591,7 +608,7 @@ void Profile::saveAvatar(const ToxPk& owner, const QByteArray& avatar)
const QByteArray& pic = needEncrypt ? passkey->encrypt(avatar) : avatar;
QString path = avatarPath(owner);
QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars");
QDir{}.mkpath(Settings::getInstance().getPaths().getAvatarsDir());
if (pic.isEmpty()) {
QFile::remove(path);
} else {
@ -669,8 +686,8 @@ void Profile::removeAvatar(const ToxPk& owner) @@ -669,8 +686,8 @@ void Profile::removeAvatar(const ToxPk& owner)
bool Profile::exists(QString name)
{
QString path = Settings::getInstance().getSettingsDirPath() + name;
return QFile::exists(path + ".tox");
const QString path{getToxSavePath(name)};
return QFile::exists(path);
}
/**
@ -691,7 +708,7 @@ bool Profile::isEncrypted() const @@ -691,7 +708,7 @@ bool Profile::isEncrypted() const
bool Profile::isEncrypted(QString name)
{
uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH] = {0};
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QString path = getToxSavePath(name);
QFile saveFile(path);
if (!saveFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't open tox save " << path;
@ -725,11 +742,11 @@ QStringList Profile::remove() @@ -725,11 +742,11 @@ QStringList Profile::remove()
i--;
}
}
QString path = Settings::getInstance().getSettingsDirPath() + name;
ProfileLocker::unlock();
QFile profileMain{path + ".tox"};
QFile profileConfig{path + ".ini"};
QFile profileMain{getToxSavePath(name)};
QFile profileConfig{getProfilePath(name) + ".ini"};
QStringList ret;
@ -761,15 +778,12 @@ QStringList Profile::remove() @@ -761,15 +778,12 @@ QStringList Profile::remove()
*/
bool Profile::rename(QString newName)
{
QString path = Settings::getInstance().getSettingsDirPath() + name,
newPath = Settings::getInstance().getSettingsDirPath() + newName;
if (!ProfileLocker::lock(newName)) {
return false;
}
QFile::rename(path + ".tox", newPath + ".tox");
QFile::rename(path + ".ini", newPath + ".ini");
QFile::rename(getToxSavePath(name), getToxSavePath(newName));
QFile::rename(getProfilePath(name) + ".ini", getProfilePath(newName) + ".ini");
if (database) {
database->rename(newName);
}
@ -878,5 +892,5 @@ QString Profile::setPassword(const QString& newPassword) @@ -878,5 +892,5 @@ QString Profile::setPassword(const QString& newPassword)
*/
QString Profile::getDbPath(const QString& profileName)
{
return Settings::getInstance().getSettingsDirPath() + profileName + ".db";
return getProfilePath(profileName) + ".db";
}

4
src/persistence/profile.h

@ -99,10 +99,12 @@ private slots: @@ -99,10 +99,12 @@ private slots:
private:
Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave);
static QStringList getFilesByExt(QString extension);
static QStringList getFilesByExt(const QString& directory, const QString& extension);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
bool saveToxSave(QByteArray data);
void initCore(const QByteArray& toxsave, ICoreSettings& s, bool isNewProfile);
static QString getProfilePath(const QString& name);
static QString getToxSavePath(const QString& name);
private:
std::unique_ptr<Core> core = nullptr;

3
src/persistence/profilelocker.cpp

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
#include "profilelocker.h"
#include "src/persistence/paths.h"
#include "src/persistence/settings.h"
#include <QDebug>
#include <QDir>
@ -38,7 +39,7 @@ QString ProfileLocker::curLockName; @@ -38,7 +39,7 @@ QString ProfileLocker::curLockName;
QString ProfileLocker::lockPathFromName(const QString& name)
{
return Settings::getInstance().getSettingsDirPath() + '/' + name + ".lock";
return Settings::getInstance().getPaths().getProfilesDir() + name + ".lock";
}
/**

2
src/persistence/settings.cpp

@ -725,7 +725,7 @@ QString Settings::getSettingsDirPath() const @@ -725,7 +725,7 @@ QString Settings::getSettingsDirPath() const
{
QMutexLocker locker{&bigLock};
QString settingsFile {paths.getGlobalSettingsPath()};
return QFileInfo{settingsFile}.dir().absolutePath();
return QFileInfo{settingsFile}.dir().absolutePath() + QDir::separator();
}
const QList<DhtServer>& Settings::getDhtServerList() const

3
src/persistence/settings.h

@ -138,7 +138,6 @@ public: @@ -138,7 +138,6 @@ public:
static Settings* makeSettings(const Paths& paths);
~Settings() override;
static Settings& getInstance();
QString getSettingsDirPath() const;
void createSettingsDir();
void createPersonal(QString basename);
@ -579,6 +578,8 @@ private: @@ -579,6 +578,8 @@ private:
void savePersonal(QString profileName, const ToxEncrypt* passkey);
friendProp& getOrInsertFriendPropRef(const ToxPk& id);
QString getSettingsDirPath() const;
public slots:
void savePersonal(Profile* profile);

6
src/widget/form/profileform.cpp

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
#include "src/core/core.h"
#include "src/model/profile/iprofileinfo.h"
#include "src/net/toxme.h"
#include "src/persistence/paths.h"
#include "src/persistence/profile.h"
#include "src/persistence/profilelocker.h"
#include "src/persistence/settings.h"
@ -227,10 +228,7 @@ void ProfileForm::show(ContentLayout* contentLayout) @@ -227,10 +228,7 @@ void ProfileForm::show(ContentLayout* contentLayout)
contentLayout->mainContent->layout()->addWidget(this);
QWidget::show();
prFileLabelUpdate();
bool portable = Settings::getInstance().getMakeToxPortable();
QString defaultPath = QDir(Settings::getInstance().getSettingsDirPath()).path().trimmed();
QString appPath = QApplication::applicationDirPath();
QString dirPath = portable ? appPath : defaultPath;
QString dirPath = Settings::getInstance().getPaths().getProfilesDir();
QString dirPrLink =
tr("Current profile location: %1").arg(QString("<a href=\"file://%1\">%1</a>").arg(dirPath));

5
src/widget/tool/profileimporter.cpp

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
#include <QPushButton>
#include "src/core/core.h"
#include "src/persistence/paths.h"
#include "src/persistence/settings.h"
/**
@ -105,8 +106,8 @@ bool ProfileImporter::importProfile(const QString& path) @@ -105,8 +106,8 @@ bool ProfileImporter::importProfile(const QString& path)
return false; // ingore importing non-tox file
}
QString settingsPath = Settings::getInstance().getSettingsDirPath();
QString profilePath = QDir(settingsPath).filePath(profile + Core::TOX_EXT);
QString profileDir = Settings::getInstance().getPaths().getProfilesDir();
QString profilePath = QDir(profileDir).filePath(profile + Core::TOX_EXT);
if (QFileInfo(profilePath).exists()) {
QString title = tr("Profile already exists", "import confirm title");

Loading…
Cancel
Save