| 943 | } |
| 944 | |
| 945 | void MemoryDialog::onImport() |
| 946 | { |
| 947 | const QString path = QFileDialog::getOpenFileName( |
| 948 | this, |
| 949 | "Import Memories", |
| 950 | QDir::home().filePath("Documents"), |
| 951 | "CSV Files (*.csv)"); |
| 952 | if (path.isEmpty()) |
| 953 | return; |
| 954 | |
| 955 | QFile file(path); |
| 956 | if (!file.open(QIODevice::ReadOnly)) { |
| 957 | QMessageBox::warning(this, "Import Memories", |
| 958 | QString("Couldn't open %1 for reading.") |
| 959 | .arg(QDir::toNativeSeparators(path))); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | const MemoryCsvParseResult parsed = MemoryCsvCompat::parse(file.readAll()); |
| 964 | |
| 965 | struct ImportState { |
| 966 | QList<MemoryCsvRecord> records; |
| 967 | QStringList issues; |
| 968 | QString fileName; |
| 969 | int nextRecord{0}; |
| 970 | int processedCount{0}; |
| 971 | int importedCount{0}; |
| 972 | }; |
| 973 | |
| 974 | const auto state = QSharedPointer<ImportState>::create(); |
| 975 | state->records = parsed.records; |
| 976 | state->issues = parsed.errors; |
| 977 | state->fileName = QFileInfo(path).fileName(); |
| 978 | |
| 979 | const QPointer<MemoryDialog> dialogGuard(this); |
| 980 | auto showSummary = [dialogGuard, state]() { |
| 981 | if (!dialogGuard) |
| 982 | return; |
| 983 | |
| 984 | dialogGuard->populateTable(); |
| 985 | |
| 986 | QMessageBox summary(dialogGuard); |
| 987 | summary.setWindowTitle("Import Memories"); |
| 988 | summary.setIcon(state->issues.isEmpty() ? QMessageBox::Information : QMessageBox::Warning); |
| 989 | summary.setText(QString("Imported %1 %2 from %3.") |
| 990 | .arg(state->importedCount) |
| 991 | .arg(state->importedCount == 1 ? "record" : "records") |
| 992 | .arg(state->fileName)); |
| 993 | if (!state->issues.isEmpty()) { |
| 994 | summary.setInformativeText( |
| 995 | QString("%1 %2 skipped or couldn't be imported. See Details for the row names and messages.") |
| 996 | .arg(state->issues.size()) |
| 997 | .arg(state->issues.size() == 1 ? "row was" : "rows were")); |
| 998 | summary.setDetailedText(state->issues.join('\n')); |
| 999 | } |
| 1000 | summary.exec(); |
| 1001 | if (dialogGuard) |
| 1002 | dialogGuard->focusTableOnCurrentRow(); |
nothing calls this directly
no test coverage detected