File indexing completed on 2024-04-28 15:58:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006-2012 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KDB_SQLITEVACUUM_H
0021 #define KDB_SQLITEVACUUM_H
0022 
0023 #include <QObject>
0024 #include <QString>
0025 #include <QProcess>
0026 
0027 #include "KDbTristate.h"
0028 #include "KDbResult.h"
0029 
0030 class QProgressDialog;
0031 
0032 //! @short Helper class performing interactive compacting (VACUUM) of the SQLite database
0033 /*! Proved SQLite database filename in the constructor.
0034  Then execute run() should be executed.
0035 
0036  QProgressDialog will be displayed. Its progress bar will be updated whenever another
0037  table's data compacting is performed. User can click "Cancel" button in any time
0038  (except the final committing) to cancel the operation. In this case,
0039  it's guaranteed that the original file remains unchanged.
0040 
0041  This is possible because we rely on SQLite's VACUUM SQL command, which itself temporarily
0042  creates a copy of the original database file, and replaces the orginal with the new only
0043  on success.
0044 */
0045 class SqliteVacuum : public QObject, public KDbResultable
0046 {
0047     Q_OBJECT
0048 public:
0049     explicit SqliteVacuum(const QString& filePath);
0050     ~SqliteVacuum() override;
0051 
0052     /*! Performs compacting procedure.
0053      @return true on success, false on failure and cancelled if user
0054      clicked "Cancel" button in the progress dialog. */
0055     tristate run();
0056 
0057 public Q_SLOTS:
0058     void readFromStdErr();
0059     void dumpProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
0060     void sqliteProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
0061     void cancelClicked();
0062 
0063 private:
0064     QString m_filePath;
0065     QString m_tmpFilePath;
0066     QProcess *m_dumpProcess;
0067     QProcess *m_sqliteProcess;
0068     QProgressDialog* m_dlg; // krazy:exclude=qclasses
0069     int m_percent;
0070     bool m_canceled;
0071     Q_DISABLE_COPY(SqliteVacuum)
0072 };
0073 
0074 #endif