1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(toxsave, profileimporter): Removed code duplication

This commit is contained in:
Diadlo 2016-07-30 13:17:35 +03:00
parent df7bf32072
commit 47cc252f76
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
3 changed files with 25 additions and 37 deletions

View File

@ -21,6 +21,7 @@
#include "src/widget/gui.h"
#include "src/core/core.h"
#include "src/persistence/settings.h"
#include "src/widget/tool/profileimporter.h"
#include <QCoreApplication>
#include <QFileInfo>
@ -52,35 +53,6 @@ bool handleToxSave(const QString& path)
while (!core->isReady())
qApp->processEvents();
QFileInfo info(path);
if (!info.exists())
return false;
QString profile = info.completeBaseName();
if (info.suffix() != "tox")
{
GUI::showWarning(QObject::tr("Ignoring non-Tox file", "popup title"),
QObject::tr("Warning: you've chosen a file that is not a Tox save file; ignoring.", "popup text"));
return false;
}
QString profilePath = Settings::getInstance().getSettingsDirPath() + profile + Core::TOX_EXT;
if (QFileInfo(profilePath).exists())
{
QString title = QObject::tr("Profile already exists", "import confirm title");
QString message = QObject::tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile);
bool erase = GUI::askQuestion(title, message);
if (!erase)
return false;
QFile(profilePath).remove();
}
QFile::copy(path, profilePath);
// no good way to update the ui from here... maybe we need a Widget:refreshUi() function...
// such a thing would simplify other code as well I believe
GUI::showInfo(QObject::tr("Profile imported"), QObject::tr("%1.tox was successfully imported").arg(profile));
return true;
ProfileImporter importer(GUI::getMainWidget());
return importer.importProfile(path);
}

View File

@ -42,18 +42,29 @@ bool ProfileImporter::importProfile()
tr("Tox save file (*.tox)", "import dialog filter"),
0,
QFileDialog::DontUseNativeDialog);
importProfile(path);
}
bool ProfileImporter::importProfile(const QString &path)
{
if (path.isEmpty())
return false;
QFileInfo info(path);
if (!info.exists())
{
GUI::showWarning(tr("File doesn't exist"),
tr("Profile doesn't exist"));
return false;
}
QString profile = info.completeBaseName();
if (info.suffix() != "tox")
{
QMessageBox::warning(this,
tr("Ignoring non-Tox file", "popup title"),
tr("Warning: You have chosen a file that is not a Tox save file; ignoring.", "popup text"),
QMessageBox::Ok);
GUI::showWarning(tr("Ignoring non-Tox file", "popup title"),
tr("Warning: You have chosen a file that is not a Tox save file; ignoring.", "popup text"));
return false; //ingore importing non-tox file
}
@ -61,8 +72,8 @@ bool ProfileImporter::importProfile()
if (QFileInfo(profilePath).exists())
{
QString title = QObject::tr("Profile already exists", "import confirm title");
QString message = QObject::tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile);
QString title = tr("Profile already exists", "import confirm title");
QString message = tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile);
bool erase = GUI::askQuestion(title, message);
if (!erase)
@ -72,5 +83,9 @@ bool ProfileImporter::importProfile()
}
QFile::copy(path, profilePath);
// no good way to update the ui from here... maybe we need a Widget:refreshUi() function...
// such a thing would simplify other code as well I believe
GUI::showInfo(tr("Profile imported"),
tr("%1.tox was successfully imported").arg(profile));
return true; //import successfull
}

View File

@ -28,6 +28,7 @@ class ProfileImporter : public QWidget
public:
explicit ProfileImporter(QWidget *parent = 0);
bool importProfile(const QString &path);
bool importProfile();
signals: