qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
profileimporter.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "profileimporter.h"
21 
22 #include <QApplication>
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <QPushButton>
26 
27 #include "src/core/core.h"
29 
39  : QWidget(parent)
40 {
41 }
42 
48 {
49  QString title = tr("Import profile", "import dialog title");
50  QString filter = tr("Tox save file (*.tox)", "import dialog filter");
51  QString dir = QDir::homePath();
52 
53  // TODO: Change all QFileDialog instances across project to use
54  // this instead of Q_NULLPTR. Possibly requires >Qt 5.9 due to:
55  // https://bugreports.qt.io/browse/QTBUG-59184
56  QString path = QFileDialog::getOpenFileName(Q_NULLPTR, title, dir, filter);
57 
58  return importProfile(path);
59 }
60 
67 bool ProfileImporter::askQuestion(QString title, QString message)
68 {
69  QMessageBox::Icon icon = QMessageBox::Warning;
70  QMessageBox box(icon, title, message, QMessageBox::NoButton, this);
71  QPushButton* pushButton1 = box.addButton(QApplication::tr("Yes"), QMessageBox::AcceptRole);
72  QPushButton* pushButton2 = box.addButton(QApplication::tr("No"), QMessageBox::RejectRole);
73  box.setDefaultButton(pushButton2);
74  box.setEscapeButton(pushButton2);
75 
76  box.exec();
77  return box.clickedButton() == pushButton1;
78 }
79 
85 bool ProfileImporter::importProfile(const QString& path)
86 {
87  if (path.isEmpty())
88  return false;
89 
90  QFileInfo info(path);
91  if (!info.exists()) {
92  QMessageBox::warning(this, tr("File doesn't exist"), tr("Profile doesn't exist"),
93  QMessageBox::Ok);
94  return false;
95  }
96 
97  QString profile = info.completeBaseName();
98 
99  if (info.suffix() != "tox") {
100  QMessageBox::warning(this, tr("Ignoring non-Tox file", "popup title"),
101  tr("Warning: You have chosen a file that is not a "
102  "Tox save file; ignoring.",
103  "popup text"),
104  QMessageBox::Ok);
105  return false; // ingore importing non-tox file
106  }
107 
108  QString settingsPath = Settings::getInstance().getPaths().getSettingsDirPath();
109  QString profilePath = QDir(settingsPath).filePath(profile + Core::TOX_EXT);
110 
111  if (QFileInfo(profilePath).exists()) {
112  QString title = tr("Profile already exists", "import confirm title");
113  QString message = tr("A profile named \"%1\" already exists. "
114  "Do you want to erase it?",
115  "import confirm text")
116  .arg(profile);
117  bool erase = askQuestion(title, message);
118 
119  if (!erase)
120  return false; // import canelled
121 
122  QFile(profilePath).remove();
123  }
124 
125  QFile::copy(path, profilePath);
126  // no good way to update the ui from here... maybe we need a Widget:refreshUi() function...
127  // such a thing would simplify other code as well I believe
128  QMessageBox::information(this, tr("Profile imported"),
129  tr("%1.tox was successfully imported").arg(profile), QMessageBox::Ok);
130 
131  return true; // import successfull
132 }
Paths::getSettingsDirPath
QString getSettingsDirPath() const
Get path to directory, where the settings files are stored.
Definition: paths.cpp:286
settings.h
Core::TOX_EXT
static const QString TOX_EXT
Definition: core.h:89
ProfileImporter::askQuestion
bool askQuestion(QString title, QString message)
Asks the user a question with Yes/No choices.
Definition: profileimporter.cpp:67
HistMessageContentType::message
@ message
profileimporter.h
ProfileImporter::importProfile
bool importProfile()
Show a file dialog. Selected file will be imported as Tox profile.
Definition: profileimporter.cpp:47
Settings::getPaths
Paths & getPaths()
Definition: settings.cpp:834
ProfileImporter::ProfileImporter
ProfileImporter(QWidget *parent=nullptr)
Definition: profileimporter.cpp:38
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
core.h