| 1 |
/* |
| 2 |
* Copyright (C) 2020-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "file_io_error_message_box.hpp" |
| 27 |
#include <QMessageBox> |
| 28 |
#include <QCoreApplication> |
| 29 |
|
| 30 |
namespace |
| 31 |
{ |
| 32 |
const std::unordered_map<io::FileType, const char*> FILE_NAMES = { |
| 33 |
{ io::FileType::Mod, QT_TRANSLATE_NOOP("FileType", "module") }, |
| 34 |
{ io::FileType::S98, QT_TRANSLATE_NOOP("FileType", "s98") }, |
| 35 |
{ io::FileType::VGM, QT_TRANSLATE_NOOP("FileType", "vgm") }, |
| 36 |
{ io::FileType::WAV, QT_TRANSLATE_NOOP("FileType", "wav") }, |
| 37 |
{ io::FileType::Bank, QT_TRANSLATE_NOOP("FileType", "bank") }, |
| 38 |
{ io::FileType::Inst, QT_TRANSLATE_NOOP("FileType", "instrument") } |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
FileIOErrorMessageBox::FileIOErrorMessageBox(const QString& file, bool isInput, io::FileType ftype, const QString desc, QWidget* parent) |
| 43 |
: parent_(parent), desc_(desc) |
| 44 |
{ |
| 45 |
setText(file, isInput, ftype); |
| 46 |
} |
| 47 |
|
| 48 |
FileIOErrorMessageBox::FileIOErrorMessageBox(const QString& file, bool isInput, const io::FileIOError& e, QWidget* parent) |
| 49 |
: parent_(parent) |
| 50 |
{ |
| 51 |
const io::FileIOError *err = &e; |
| 52 |
QString type = QCoreApplication::translate("FileType", FILE_NAMES.at(err->fileType())); |
| 53 |
|
| 54 |
if (dynamic_cast<const io::FileNotExistError*>(err)) { |
| 55 |
desc_ = tr("Path does not exist."); |
| 56 |
} |
| 57 |
else if (dynamic_cast<const io::FileUnsupportedError*>(err)) { |
| 58 |
desc_ = tr("Unsupported file format."); |
| 59 |
} |
| 60 |
else if (dynamic_cast<const io::FileVersionError*>(err)) { |
| 61 |
desc_ = tr("Could not load the %1 properly. " |
| 62 |
"Please make sure that you have the latest version of BambooTracker.").arg(file); |
| 63 |
} |
| 64 |
else if (auto ce = dynamic_cast<const io::FileCorruptionError*>(err)) { |
| 65 |
desc_ = tr("Could not load the %1. It may be corrupted. Stopped at %2.").arg(type).arg(ce->position()); |
| 66 |
} |
| 67 |
|
| 68 |
setText(file, isInput, e.fileType()); |
| 69 |
} |
| 70 |
|
| 71 |
void FileIOErrorMessageBox::setText(const QString& file, bool isInput, io::FileType ftype) |
| 72 |
{ |
| 73 |
if (isInput) { |
| 74 |
text_ = tr("Failed to load %1.").arg(file); |
| 75 |
} |
| 76 |
else { |
| 77 |
switch (ftype) { |
| 78 |
case io::FileType::S98: |
| 79 |
case io::FileType::VGM: |
| 80 |
case io::FileType::WAV: |
| 81 |
text_ = tr("Failed to export to %1."); |
| 82 |
break; |
| 83 |
default: |
| 84 |
text_ = tr("Failed to save the %1."); |
| 85 |
break; |
| 86 |
} |
| 87 |
text_ = text_.arg(file); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void FileIOErrorMessageBox::exec() |
| 92 |
{ |
| 93 |
QMessageBox::critical(parent_, tr("Error"), text_ + "\n" + desc_); |
| 94 |
} |