kexi

sqlitevacuum.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kexidb/global.h>
00021 #include "sqlitevacuum.h"
00022 
00023 #include <kstandarddirs.h>
00024 #include <kprogress.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <ktempfile.h>
00028 #include <kmessagebox.h>
00029 #include <kio/global.h>
00030 
00031 #include <qfileinfo.h>
00032 #include <qdir.h>
00033 #include <qapplication.h>
00034 #include <qprocess.h>
00035 #include <qcursor.h>
00036 
00037 #include <unistd.h>
00038 
00039 SQLiteVacuum::SQLiteVacuum(const QString& filePath)
00040 : m_filePath(filePath)
00041 {
00042     m_process = 0;
00043     m_percent = 0;
00044     m_dlg = 0;
00045     m_result = true;
00046 }
00047 
00048 SQLiteVacuum::~SQLiteVacuum()
00049 {
00050     delete m_process;
00051     if (m_dlg)
00052         m_dlg->close();
00053     delete m_dlg;
00054 }
00055 
00056 tristate SQLiteVacuum::run()
00057 {
00058     const QString ksqlite_app = KStandardDirs::findExe( "ksqlite" );
00059     if (ksqlite_app.isEmpty()) {
00060         m_result = false;
00061         return m_result;
00062     }
00063     QFileInfo fi(m_filePath);
00064     if (!fi.isReadable()) {
00065         KexiDBDrvWarn << "SQLiteVacuum::run(): No such file" << m_filePath << endl;
00066         return false;
00067     }
00068     const uint origSize = fi.size();
00069 
00070     QStringList args;
00071     args << ksqlite_app << "-verbose-vacuum" << m_filePath << "vacuum";
00072     m_process = new QProcess(args, this, "process");
00073     m_process->setWorkingDirectory( QFileInfo(m_filePath).dir(true) );
00074     connect( m_process, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()) );
00075     connect( m_process, SIGNAL(processExited()), this, SLOT(processExited()) );
00076     if (!m_process->start()) {
00077         m_result = false;
00078         return m_result;
00079     }
00080     m_dlg = new KProgressDialog(0, 0, i18n("Compacting database"), 
00081         "<qt>"+i18n("Compacting database \"%1\"...")
00082         .arg("<nobr>"+QDir::convertSeparators(QFileInfo(m_filePath).fileName())+"</nobr>")
00083     );
00084     m_dlg->adjustSize();
00085     m_dlg->resize(300, m_dlg->height());
00086     connect(m_dlg, SIGNAL(cancelClicked()), this, SLOT(cancelClicked()));
00087     m_dlg->setMinimumDuration(1000);
00088     m_dlg->setAutoClose(true);
00089     m_dlg->progressBar()->setTotalSteps(100);
00090     m_dlg->exec();
00091     while (m_process->isRunning()) {
00092         readFromStdout();
00093         usleep(50000);
00094     }
00095     delete m_process;
00096     m_process = 0;
00097     if (m_result == true) {
00098         const uint newSize = QFileInfo(m_filePath).size();
00099         const uint decrease = 100-100*newSize/origSize;
00100         KMessageBox::information(0, i18n("The database has been compacted. Current size decreased by %1% to %2.")
00101             .arg(decrease).arg(KIO::convertSize(newSize)));
00102     }
00103     return m_result;
00104 }
00105 
00106 void SQLiteVacuum::readFromStdout()
00107 {
00108     while (true) {
00109         QString s( m_process->readLineStdout() ); //readStdout();
00110         if (s.isEmpty())
00111             break;
00112         m_dlg->progressBar()->setProgress(m_percent);
00113 //      KexiDBDrvDbg << m_percent << " " << s << endl;
00114         if (s.startsWith("VACUUM: ")) {
00115             //set previously known progress
00116             m_dlg->progressBar()->setProgress(m_percent);
00117             //update progress info
00118             if (s.mid(8,4)=="100%") {
00119                 m_percent = 100;
00120                 m_dlg->setAllowCancel(false);
00121                 m_dlg->setCursor(QCursor(Qt::WaitCursor));
00122             }
00123             else if (s.mid(9,1)=="%") {
00124                 m_percent = s.mid(8,1).toInt();
00125             }
00126             else if (s.mid(10,1)=="%") {
00127                 m_percent = s.mid(8,2).toInt();
00128             }
00129             m_process->writeToStdin(" ");
00130         }
00131     }
00132 }
00133 
00134 void SQLiteVacuum::processExited()
00135 {
00136 //  KexiDBDrvDbg << sender()->name() << " EXIT" << endl;
00137     m_dlg->close();
00138     delete m_dlg;
00139     m_dlg = 0;
00140 }
00141 
00142 void SQLiteVacuum::cancelClicked()
00143 {
00144     if (!m_process->normalExit()) {
00145         m_process->writeToStdin("q"); //quit
00146         m_result = cancelled;
00147     }
00148 }
00149 
00150 #include "sqlitevacuum.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys