kexi
sqlitevacuum.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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() );
00110 if (s.isEmpty())
00111 break;
00112 m_dlg->progressBar()->setProgress(m_percent);
00113
00114 if (s.startsWith("VACUUM: ")) {
00115
00116 m_dlg->progressBar()->setProgress(m_percent);
00117
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
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");
00146 m_result = cancelled;
00147 }
00148 }
00149
00150 #include "sqlitevacuum.moc"
|