| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2022 Chris Hennes <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library 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 Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <QDateTime> |
| 24 |
#include <QPushButton> |
| 25 |
|
| 26 |
#include "Dialogs/DlgRevertToBackupConfigImp.h" |
| 27 |
#include "ui_DlgRevertToBackupConfig.h" |
| 28 |
#include "Application.h" |
| 29 |
#include "PreferencePackManager.h" |
| 30 |
|
| 31 |
|
| 32 |
using namespace Gui; |
| 33 |
using namespace Gui::Dialog; |
| 34 |
namespace fs = std::filesystem; |
| 35 |
|
| 36 |
/* TRANSLATOR Gui::Dialog::DlgRevertToBackupConfigImp */ |
| 37 |
|
| 38 |
DlgRevertToBackupConfigImp::DlgRevertToBackupConfigImp(QWidget* parent) |
| 39 |
: QDialog(parent) |
| 40 |
, ui(new Ui_DlgRevertToBackupConfig) |
| 41 |
{ |
| 42 |
ui->setupUi(this); |
| 43 |
connect( |
| 44 |
ui->listWidget, |
| 45 |
&QListWidget::itemSelectionChanged, |
| 46 |
this, |
| 47 |
&DlgRevertToBackupConfigImp::onItemSelectionChanged |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
DlgRevertToBackupConfigImp::~DlgRevertToBackupConfigImp() = default; |
| 52 |
|
| 53 |
void Gui::Dialog::DlgRevertToBackupConfigImp::onItemSelectionChanged() |
| 54 |
{ |
| 55 |
auto items = ui->listWidget->selectedItems(); |
| 56 |
if (items.count() == 1) { |
| 57 |
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(true); |
| 58 |
} |
| 59 |
else { |
| 60 |
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Sets the strings of the subwidgets using the current language. |
| 66 |
*/ |
| 67 |
void DlgRevertToBackupConfigImp::changeEvent(QEvent* e) |
| 68 |
{ |
| 69 |
if (e->type() == QEvent::LanguageChange) { |
| 70 |
ui->retranslateUi(this); |
| 71 |
} |
| 72 |
else { |
| 73 |
QWidget::changeEvent(e); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// FIXME: Replace with more accurate C++20 solution once its usable: https://stackoverflow.com/a/68593141 |
| 78 |
template<typename TP> |
| 79 |
static std::time_t to_time_t(TP tp) |
| 80 |
{ |
| 81 |
using namespace std::chrono; |
| 82 |
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now()); |
| 83 |
return system_clock::to_time_t(sctp); |
| 84 |
} |
| 85 |
|
| 86 |
void DlgRevertToBackupConfigImp::showEvent(QShowEvent* event) |
| 87 |
{ |
| 88 |
ui->listWidget->clear(); |
| 89 |
const auto& backups = Application::Instance->prefPackManager()->configBackups(); |
| 90 |
for (const auto& backup : backups) { |
| 91 |
auto modification_date = QDateTime::fromSecsSinceEpoch(to_time_t(fs::last_write_time(backup))); |
| 92 |
auto item = new QListWidgetItem(QLocale().toString(modification_date)); |
| 93 |
item->setData(Qt::UserRole, QString::fromStdString(backup.string())); |
| 94 |
ui->listWidget->addItem(item); |
| 95 |
} |
| 96 |
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(false); |
| 97 |
QDialog::showEvent(event); |
| 98 |
} |
| 99 |
|
| 100 |
void DlgRevertToBackupConfigImp::accept() |
| 101 |
{ |
| 102 |
auto items = ui->listWidget->selectedItems(); |
| 103 |
if (items.count() != 1) { |
| 104 |
Base::Console().error( |
| 105 |
tr("No selection in dialog, cannot load backup file").toStdString().c_str() |
| 106 |
); |
| 107 |
return; |
| 108 |
} |
| 109 |
auto item = items[0]; |
| 110 |
auto path = item->data(Qt::UserRole).toString().toStdString(); |
| 111 |
if (fs::exists(path)) { |
| 112 |
auto newParameters = ParameterManager::Create(); |
| 113 |
newParameters->LoadDocument(path.c_str()); |
| 114 |
auto baseAppGroup = App::GetApplication().GetUserParameter().GetGroup("BaseApp"); |
| 115 |
newParameters->GetGroup("BaseApp")->copyTo(baseAppGroup); |
| 116 |
} |
| 117 |
else { |
| 118 |
Base::Console().error("Preference Pack Internal Error: Invalid backup file location"); |
| 119 |
} |
| 120 |
|
| 121 |
QDialog::accept(); |
| 122 |
} |
| 123 |
|
| 124 |
#include "moc_DlgRevertToBackupConfigImp.cpp" |