Browse Source

Implemented create local backup and tested deletion for it.

A simple subclass of QStandardItem is used for the table view's first row. AStandardPath has a new utility func `asChildOfDir` to help path creation from simple name.
George 4 years ago
parent
commit
b5eebdfda9

+ 5 - 0
src/classes/astandardpaths.cpp

@@ -40,6 +40,11 @@ const QDir& AStandardPaths::directory(Directories location)
     return directories[location];
 }
 
+const QString AStandardPaths::asChildOfDir(Directories location, const QString &filename)
+{
+    return directories[location].absoluteFilePath(filename);
+}
+
 const QMap<AStandardPaths::Directories, QDir>& AStandardPaths::allDirectories()
 {
     return directories;

+ 4 - 0
src/classes/astandardpaths.h

@@ -56,6 +56,10 @@ public:
      */
     static const QDir &directory(Directories location);
 
+    /*!
+     * \brief Returns a string of the absolute path to directory location concatenated with filename
+     */
+    static const QString asChildOfDir(Directories location, const QString& filename);
     /*!
      * \brief returns the static map of all standard directories
      * \return static const QMap<Directories, QDir>

+ 1 - 4
src/database/adatabase.cpp

@@ -728,8 +728,7 @@ bool ADatabase::createBackup(const QString& dest_file)
     DEB << "File:" << file;  // [G]: Check adebug.h got INFO WARN, ... additions and discuss convention of use.
 
     if (!file.copy(dest_file)) {
-        WARN << "Unable to backup old database.";
-        DEB << file.errorString();
+        WARN << "Unable to backup old database:" << file.errorString();
         return false;
     }
 
@@ -751,8 +750,6 @@ bool ADatabase::restoreBackup(const QString& backup_file)
     QFile backup(backup_file);
     QFile current_db(databaseFile.absoluteFilePath());
 
-    // [G]: Wrong permissions would end up making a inf loop
-    // So i go defensively by checking first
     if(backup.isWritable() == false || current_db.isWritable() == false) {
         WARN << backup << "is not writtable check PERMISSIONS";
         return false;

+ 38 - 20
src/gui/widgets/backupwidget.cpp

@@ -48,25 +48,23 @@ BackupWidget::~BackupWidget()
 
 void BackupWidget::on_tableView_clicked(const QModelIndex &index)
 {
-    selectedBackupName = model->item(index.row(), 0);
-    DEB << "Item at row:" << index.row() << "->" << selectedBackupName->data(Qt::DisplayRole);
+    selectedFileInfo = static_cast<AFileStandardItem*>(model->item(index.row(), 0));
+    DEB << "Item at row:" << index.row() << "->" << selectedFileInfo->data(Qt::DisplayRole);
 }
 
 void BackupWidget::on_createLocalPushButton_clicked()
 {
-    QString filename = QFileDialog::getSaveFileName(
-                this,
-                "Choose destination file",
-                AStandardPaths::directory(AStandardPaths::Backup).absolutePath(),
-                "*.db"
-    );
+    QString filename = AStandardPaths::asChildOfDir(AStandardPaths::Backup, "backup");
+
+    DEB << filename;
 
-    if(filename.endsWith(".db") == false) {  // [G]: clunky im sure it can be enforced by QFileDialog
+    if(filename.endsWith(".db") == false) {  // [G] TODO: get function that generates the name automatically
         filename.append(".db");
     }
 
     if(aDB->createBackup(filename) == false) {
         WARN << "Could not create local file:" << filename;
+        return;
     }
 
     // [G] TODO: propably make a function out of this for future tweaks
@@ -83,12 +81,17 @@ void BackupWidget::on_createLocalPushButton_clicked()
 
 void BackupWidget::on_restoreLocalPushButton_clicked()
 {
-    NOT_IMPLEMENTED
-    if(selectedBackupName == nullptr) {
+    NOT_IMPLEMENTED("TODO");
+
+    if(selectedFileInfo == nullptr) {
         INFO << "No backup selected";
         return;
     }
-    QString backup_name = selectedBackupName->data(Qt::DisplayRole).toString();
+
+    QString backup_name = AStandardPaths::asChildOfDir(
+                AStandardPaths::Backup,
+                selectedFileInfo->data(Qt::DisplayRole).toString()
+                );
     if(aDB->restoreBackup(backup_name) == false) {
         WARN << "Couldnt restore" << backup_name;
     }
@@ -96,21 +99,36 @@ void BackupWidget::on_restoreLocalPushButton_clicked()
 
 void BackupWidget::on_deleteSelectedPushButton_clicked()
 {
-    NOT_IMPLEMENTED
-    if(selectedBackupName == nullptr) {
+    NOT_IMPLEMENTED("Test external deletion");
+    if(selectedFileInfo == nullptr) {
         INFO << "No backup was selected";
         return;
     }
-    DEB << "deleting:" << selectedBackupName->data(Qt::DisplayRole);
+
+    const QFileInfo& filename = selectedFileInfo->info();
+    QFile file(filename.absoluteFilePath());
+
+    if(file.exists() == false) {
+        WARN << "Selected backup file doesnt exist:" << file;
+        return;
+    }
+
+    DEB << "deleting:" << filename;
+    if(file.remove() == false) {
+        WARN << "Unable to remove file:" << file.errorString();
+        return;
+    }
+
+    model->removeRow(selectedFileInfo->row());
 }
 
 void BackupWidget::on_createExternalPushButton_clicked()
 {
-    NOT_IMPLEMENTED
+    NOT_IMPLEMENTED("TODO");
     QString filename = QFileDialog::getSaveFileName(
                 this,
                 "Choose destination file",
-                AStandardPaths::directory(AStandardPaths::Backup).absolutePath(),
+                QStandardPaths::displayName(QStandardPaths::HomeLocation),
                 ".db"
     );
     // [G]: The window isn resizable and i cant easily debug the buttons (cant find them xD)
@@ -120,7 +138,7 @@ void BackupWidget::on_createExternalPushButton_clicked()
 
 void BackupWidget::on_restoreExternalPushButton_clicked()
 {
-    NOT_IMPLEMENTED
+    NOT_IMPLEMENTED("TODO");
     QString filename = QFileDialog::getSaveFileName(
                 this,
                 "Choose backup file",
@@ -158,8 +176,8 @@ void BackupWidget::fillTableWithSampleData()
     // Get summary of each db file and populate lists (columns) of data
     for (const auto &entry : entries) {
         QMap<QString, QString> summary = aDB->databaseSummary(backup_dir.absoluteFilePath(entry));
-
-        model->appendRow({new QStandardItem(provider.icon(QFileIconProvider::File), entry),
+//        model->appendRow({new QStandardItem(provider.icon(QFileIconProvider::File), entry),
+        model->appendRow({new AFileStandardItem(provider.icon(QFileIconProvider::File), entry, AStandardPaths::Backup),
                           new QStandardItem(summary["total_flights"]),
                           new QStandardItem(summary["total_tails"]),
                           new QStandardItem(summary["total_pilots"]),

+ 27 - 3
src/gui/widgets/backupwidget.h

@@ -1,6 +1,8 @@
 #ifndef BACKUPWIDGET_H
 #define BACKUPWIDGET_H
 
+#include "src/classes/astandardpaths.h"
+
 #include <QWidget>
 #include <QStandardItemModel>
 #include <QFileSystemModel>
@@ -11,6 +13,28 @@ namespace Ui {
 class BackupWidget;
 }
 
+/*!
+ * \brief Simple QStandardItem subclass to encapsulate necessary file info.
+ * Using only a QStandardItem would mean that the full path should be inputted
+ * as data and of course displayed by default. However this way we create
+ * the absolute path in the fileInfo attribute for further use while
+ * displaying only the base name.
+ */
+class AFileStandardItem : public QStandardItem {
+private:
+    QFileInfo fileInfo;
+public:
+    AFileStandardItem(const QIcon& icon, const QString& filename, const AStandardPaths::Directories dir)
+        : QStandardItem(icon, filename),
+          fileInfo(QFileInfo(AStandardPaths::asChildOfDir(dir, filename)))
+    {}
+
+    const QFileInfo& info() const
+    {
+        return fileInfo;
+    }
+};
+
 class BackupWidget : public QWidget
 {
     Q_OBJECT
@@ -38,9 +62,9 @@ private:
     Ui::BackupWidget *ui;
 
     QStandardItemModel *model;
-    QStandardItem *selectedBackupName = nullptr;  // The QStandardItemModel returns QStandardItem
-                                                  // even for single cells
-
+    AFileStandardItem *selectedFileInfo = nullptr;  // Only the first column is necessary for
+                                                    // any operation and it is encapsulated in the
+                                                    // AFileStandardItem class
     void fillTableWithSampleData();
 };
 

+ 2 - 1
src/testing/adebug.h

@@ -29,7 +29,8 @@
 #define INFO qInfo() << "info:"
 #define WARN qWarning() << "warning:"
 #define CRIT qCritical() << "critical:"
-#define NOT_IMPLEMENTED qCritical() << FUNC_IDENT << "\n\t" << "~~ NOT IMPLEMENTED ~~";
+//#define NOT_IMPLEMENTED qCritical() << FUNC_IDENT << "\n\t" << "~~ NOT IMPLEMENTED ~~";
+#define NOT_IMPLEMENTED(msg) qCritical() << FUNC_IDENT << "\n\tNOT IMPLEMENTED:" << msg
 
 /*!
  * Representation macro for custom classes.