backupwidget.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "backupwidget.h"
  2. #include "ui_backupwidget.h"
  3. #include "src/classes/astandardpaths.h"
  4. #include "src/testing/adebug.h"
  5. #include "src/database/adatabase.h"
  6. #include "src/functions/adatetime.h"
  7. #include <QListView>
  8. #include <QStandardItemModel>
  9. #include <QFileIconProvider>
  10. #include <QMessageBox>
  11. #include <QFileDialog>
  12. BackupWidget::BackupWidget(QWidget *parent) :
  13. QWidget(parent),
  14. ui(new Ui::BackupWidget)
  15. {
  16. ui->setupUi(this);
  17. /* This is just a quick and dirty overview to get something into the view so you have an idea.
  18. * The goal is that the view does not just display file names, but a String, for example
  19. *
  20. * Available Backups
  21. * =================
  22. * 2020-01-01 - 476 Flights, 23 Aircraft, 12 Pilots, 1580:33 Total Time, Last Flight 2019-12-28
  23. * 2020-02-02 - 512 Flights, 25 Aircraft, 13 Pilots, 1640:47 Total Time, Last Flight 2020-01-23
  24. * ...
  25. *
  26. * So instead of QFileSystemModel with QListView, which would be the easiest way, using QAbstractItemModel
  27. * and QTableView might be preferable since it allows us to customize the data we display.
  28. *
  29. * Below is just some sample code to fill the table with data to give you an impression of what
  30. * the end result is supposed to be like. If you have another idea of how to get there, that's fine as well!
  31. *
  32. */
  33. model = new QStandardItemModel(this);
  34. model->setHorizontalHeaderLabels(QStringList{"Backup File","Total Flights", "Total Tails",
  35. "Total Pilots", "Max Doft", "Total Time"}); // [G]: TODO make const but where?
  36. refresh();
  37. }
  38. BackupWidget::~BackupWidget()
  39. {
  40. delete ui;
  41. }
  42. QString BackupWidget::absoluteBackupPath()
  43. {
  44. const QString backup_name = QLatin1String("logbook_backup_")
  45. + ADateTime::toString(QDateTime::currentDateTime(), Opl::Datetime::Backup)
  46. + QLatin1String(".db");
  47. return AStandardPaths::asChildOfDir(AStandardPaths::Backup, backup_name);
  48. }
  49. void BackupWidget::on_tableView_clicked(const QModelIndex &index)
  50. {
  51. selectedFileInfo = static_cast<AFileStandardItem*>(model->item(index.row(), 0));
  52. DEB << "Item at row:" << index.row() << "->" << selectedFileInfo->data(Qt::DisplayRole);
  53. }
  54. void BackupWidget::on_createLocalPushButton_clicked()
  55. {
  56. QString filename = absoluteBackupPath();
  57. DEB << filename;
  58. if(!aDB->createBackup(filename)) {
  59. WARN << "Could not create local file:" << filename;
  60. return;
  61. }
  62. // [G] TODO: propably make a function out of this for future tweaks
  63. QFileIconProvider provider;
  64. QMap<ADatabaseSummaryKey, QString> summary = aDB->databaseSummary(filename);
  65. model->appendRow({new AFileStandardItem(provider.icon(QFileIconProvider::File), QFileInfo(filename)),
  66. new QStandardItem(summary[ADatabaseSummaryKey::total_flights]),
  67. new QStandardItem(summary[ADatabaseSummaryKey::total_tails]),
  68. new QStandardItem(summary[ADatabaseSummaryKey::total_pilots]),
  69. new QStandardItem(summary[ADatabaseSummaryKey::max_doft]),
  70. new QStandardItem(summary[ADatabaseSummaryKey::total_time])
  71. });
  72. }
  73. void BackupWidget::on_restoreLocalPushButton_clicked()
  74. {
  75. NOT_IMPLEMENTED("TODO");
  76. if(selectedFileInfo == nullptr) {
  77. INFO << "No backup selected";
  78. return;
  79. }
  80. QString backup_name = AStandardPaths::asChildOfDir(
  81. AStandardPaths::Backup,
  82. selectedFileInfo->data(Qt::DisplayRole).toString()
  83. );
  84. if(!aDB->restoreBackup(backup_name)) {
  85. WARN << "Couldnt restore" << backup_name;
  86. }
  87. }
  88. void BackupWidget::on_deleteSelectedPushButton_clicked()
  89. {
  90. NOT_IMPLEMENTED("Test external deletion");
  91. if(selectedFileInfo == nullptr) {
  92. INFO << "No backup was selected";
  93. return;
  94. }
  95. const QFileInfo& filename = selectedFileInfo->info();
  96. QFile file(filename.absoluteFilePath());
  97. if(file.exists() == false) {
  98. WARN << "Selected backup file doesnt exist:" << file;
  99. return;
  100. }
  101. DEB << "deleting:" << filename;
  102. if(file.remove() == false) {
  103. WARN << "Unable to remove file:" << file.errorString();
  104. return;
  105. }
  106. model->removeRow(selectedFileInfo->row());
  107. // [G] TODO: figure out selection coordination between view model and selected
  108. if(selectedFileInfo->row() - 1 < 0) {
  109. selectedFileInfo = nullptr;
  110. }
  111. else {
  112. selectedFileInfo = static_cast<AFileStandardItem*>(model->item(selectedFileInfo->row()-1));
  113. }
  114. }
  115. void BackupWidget::on_createExternalPushButton_clicked()
  116. {
  117. NOT_IMPLEMENTED("TODO");
  118. QString filename = QFileDialog::getSaveFileName(
  119. this,
  120. "Choose destination file",
  121. QStandardPaths::displayName(QStandardPaths::HomeLocation),
  122. ".db"
  123. );
  124. // [G]: The window isn resizable and i cant easily debug the buttons (cant find them xD)
  125. // Open something like a QFileDialog and let the user choose where to save the backup
  126. }
  127. void BackupWidget::on_restoreExternalPushButton_clicked()
  128. {
  129. NOT_IMPLEMENTED("TODO");
  130. QString filename = QFileDialog::getSaveFileName(
  131. this,
  132. "Choose backup file",
  133. // [G] TODO: home is the debug directory. Will it be ~ correctly?
  134. // Qt docs say it is (Confirm debug exception)
  135. QStandardPaths::displayName(QStandardPaths::HomeLocation),
  136. ".db"
  137. );
  138. // Open something like a QFileDialog and let the user choose where to load the backup from
  139. }
  140. void BackupWidget::on_aboutPushButton_clicked() {
  141. // [G]: Add message text. Could this be predefined in Opl::Assets?
  142. QMessageBox msg_box(QMessageBox::Information, "About backups", "...", QMessageBox::Ok);
  143. msg_box.exec();
  144. }
  145. // ===================================================================================
  146. // feel free to delete this as you go along, just here for demonstration purposes
  147. void BackupWidget::refresh()
  148. {
  149. // First column in table, would be created by listing the files in backupdir
  150. QDir backup_dir = QDir(AStandardPaths::directory(AStandardPaths::Backup));
  151. QStringList entries = backup_dir.entryList(QStringList{"*.db"}, QDir::Files, QDir::Time);
  152. QFileIconProvider provider;
  153. // [G]: works but a bit too hardcoded perhaps? The aviation industry wont change overnight
  154. // but still it could be worthwile to at least have the names a bit more encapsulated in the
  155. // database so we have them more "centralised" at least.
  156. // Get summary of each db file and populate lists (columns) of data
  157. for (const auto &entry : entries) {
  158. QMap<ADatabaseSummaryKey, QString> summary = aDB->databaseSummary(backup_dir.absoluteFilePath(entry));
  159. model->appendRow({new AFileStandardItem(provider.icon(QFileIconProvider::File), entry, AStandardPaths::Backup),
  160. new QStandardItem(summary[ADatabaseSummaryKey::total_flights]),
  161. new QStandardItem(summary[ADatabaseSummaryKey::total_tails]),
  162. new QStandardItem(summary[ADatabaseSummaryKey::total_pilots]),
  163. new QStandardItem(summary[ADatabaseSummaryKey::max_doft]),
  164. new QStandardItem(summary[ADatabaseSummaryKey::total_time])
  165. });
  166. }
  167. // [G]: Sort entries? based on what? the files are abit inconsistent in their naming atm
  168. // but i assume we could sort based on the time in the file name?
  169. ui->tableView->setModel(model);
  170. ui->tableView->resizeColumnsToContents(); // [G]: Bit hacky couldnt do it by default
  171. }