text: /**
   * Sky Dolly - The Black Sheep for your Flight Recordings
   *
   * Copyright (c) Oliver Knoll
   * All rights reserved.
   *
   * MIT License
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy of this
   * software and associated documentation files (the "Software"), to deal in the Software
   * without restriction, including without limitation the rights to use, copy, modify, merge,
   * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
   * to whom the Software is furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all copies or
   * substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
   * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
   * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
   * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   * DEALINGS IN THE SOFTWARE.
   */
  #ifndef CONNECTIONMANAGER_H
  #define CONNECTIONMANAGER_H
  
  #include <memory>
  
  #include <QObject>
  
  class QString;
  class QWidget;
  
  class Metadata;
  class Version;
  
  #include "PersistenceLib.h"
  
  class Metadata;
  class Version;
  class ConnectionManagerPrivate;
  
  class PERSISTENCE_API ConnectionManager : public QObject
  {
      Q_OBJECT
  public:
      static ConnectionManager &getInstance() noexcept;
      static void destroyInstance() noexcept;
  
      /*!
       * Connects with the database given by \c logbookPath
       * and initialises the database by applying the required migrations.
       *
       * If a problem with opening the database occurs (e.g. a version
       * mismatch) then a dialog asks the user for alternative logbook
       * paths (or to quit the application altogether).
       *
       * The actual logbook path (which is usually the given \c logbookPath)
       * is stored in the Settings.
       *
       * \param logbookPath
       *        the path of the logbook (database) file to connect with
       * \return \c true if the connection succeeded; \c false else
       * \sa Settings#setLogbookPath
       * \sa connectionChanged
       */
      bool connectWithLogbook(const QString &logbookPath, QWidget *parent) noexcept;
  
      void disconnectFromLogbook() noexcept;
      bool isConnected() const noexcept;
      const QString &getLogbookPath() const noexcept;
  
      bool migrate() noexcept;
      bool optimise() noexcept;
      bool backup(const QString &backupLogbookPath) noexcept;
  
      bool getMetadata(Metadata &metadata) const noexcept;
      bool getDatabaseVersion(Version &databaseVersion) const noexcept;
      bool getBackupDirectoryPath(QString &backupDirectoryPath) const noexcept;
  
      QString getBackupFileName(const QString &backupDirectoryPath) const noexcept;
      static QString createBackupPathIfNotExists(const QString &relativeOrAbsoluteBackupDirectoryPath) noexcept;
  
  signals:
      void connectionChanged(bool connected);
  
  protected:
      virtual ~ConnectionManager() noexcept;
  
  private:
      Q_DISABLE_COPY(ConnectionManager)
      std::unique_ptr<ConnectionManagerPrivate> d;
  
      ConnectionManager() noexcept;
  
      bool connectDb(const QString &logbookPath) noexcept;
      bool checkDatabaseVersion(Version &databaseVersion) const noexcept;
  };
  
  #endif // CONNECTIONMANAGER_H
  