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:
parent
df7bf32072
commit
47cc252f76
|
@ -21,6 +21,7 @@
|
||||||
#include "src/widget/gui.h"
|
#include "src/widget/gui.h"
|
||||||
#include "src/core/core.h"
|
#include "src/core/core.h"
|
||||||
#include "src/persistence/settings.h"
|
#include "src/persistence/settings.h"
|
||||||
|
#include "src/widget/tool/profileimporter.h"
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
|
||||||
|
@ -52,35 +53,6 @@ bool handleToxSave(const QString& path)
|
||||||
while (!core->isReady())
|
while (!core->isReady())
|
||||||
qApp->processEvents();
|
qApp->processEvents();
|
||||||
|
|
||||||
QFileInfo info(path);
|
ProfileImporter importer(GUI::getMainWidget());
|
||||||
if (!info.exists())
|
return importer.importProfile(path);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,18 +42,29 @@ bool ProfileImporter::importProfile()
|
||||||
tr("Tox save file (*.tox)", "import dialog filter"),
|
tr("Tox save file (*.tox)", "import dialog filter"),
|
||||||
0,
|
0,
|
||||||
QFileDialog::DontUseNativeDialog);
|
QFileDialog::DontUseNativeDialog);
|
||||||
|
|
||||||
|
importProfile(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ProfileImporter::importProfile(const QString &path)
|
||||||
|
{
|
||||||
if (path.isEmpty())
|
if (path.isEmpty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QFileInfo info(path);
|
QFileInfo info(path);
|
||||||
|
if (!info.exists())
|
||||||
|
{
|
||||||
|
GUI::showWarning(tr("File doesn't exist"),
|
||||||
|
tr("Profile doesn't exist"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
QString profile = info.completeBaseName();
|
QString profile = info.completeBaseName();
|
||||||
|
|
||||||
if (info.suffix() != "tox")
|
if (info.suffix() != "tox")
|
||||||
{
|
{
|
||||||
QMessageBox::warning(this,
|
GUI::showWarning(tr("Ignoring non-Tox file", "popup title"),
|
||||||
tr("Ignoring non-Tox file", "popup title"),
|
tr("Warning: You have chosen a file that is not a Tox save file; ignoring.", "popup text"));
|
||||||
tr("Warning: You have chosen a file that is not a Tox save file; ignoring.", "popup text"),
|
|
||||||
QMessageBox::Ok);
|
|
||||||
return false; //ingore importing non-tox file
|
return false; //ingore importing non-tox file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,8 +72,8 @@ bool ProfileImporter::importProfile()
|
||||||
|
|
||||||
if (QFileInfo(profilePath).exists())
|
if (QFileInfo(profilePath).exists())
|
||||||
{
|
{
|
||||||
QString title = QObject::tr("Profile already exists", "import confirm title");
|
QString title = 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 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);
|
bool erase = GUI::askQuestion(title, message);
|
||||||
|
|
||||||
if (!erase)
|
if (!erase)
|
||||||
|
@ -72,5 +83,9 @@ bool ProfileImporter::importProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
QFile::copy(path, profilePath);
|
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
|
return true; //import successfull
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ class ProfileImporter : public QWidget
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ProfileImporter(QWidget *parent = 0);
|
explicit ProfileImporter(QWidget *parent = 0);
|
||||||
|
bool importProfile(const QString &path);
|
||||||
bool importProfile();
|
bool importProfile();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user