kexi

sqlitevacuum.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <kexidb/global.h>
21#include "sqlitevacuum.h"
22
23#include <kstandarddirs.h>
24#include <kprogress.h>
25#include <kdebug.h>
26#include <tdelocale.h>
27#include <tdetempfile.h>
28#include <tdemessagebox.h>
29#include <tdeio/global.h>
30
31#include <tqfileinfo.h>
32#include <tqdir.h>
33#include <tqapplication.h>
34#include <tqprocess.h>
35#include <tqcursor.h>
36
37#include <unistd.h>
38
39SQLiteVacuum::SQLiteVacuum(const TQString& filePath)
40: m_filePath(filePath)
41{
42 m_process = 0;
43 m_percent = 0;
44 m_dlg = 0;
45 m_result = true;
46}
47
48SQLiteVacuum::~SQLiteVacuum()
49{
50 delete m_process;
51 if (m_dlg)
52 m_dlg->close();
53 delete m_dlg;
54}
55
57{
58 const TQString ksqlite_app = TDEStandardDirs::findExe( "ksqlite" );
59 if (ksqlite_app.isEmpty()) {
60 m_result = false;
61 return m_result;
62 }
63 TQFileInfo fi(m_filePath);
64 if (!fi.isReadable()) {
65 KexiDBDrvWarn << "SQLiteVacuum::run(): No such file" << m_filePath << endl;
66 return false;
67 }
68 const uint origSize = fi.size();
69
70 TQStringList args;
71 args << ksqlite_app << "-verbose-vacuum" << m_filePath << "vacuum";
72 m_process = new TQProcess(args, this, "process");
73 m_process->setWorkingDirectory( TQFileInfo(m_filePath).dir(true) );
74 connect( m_process, TQ_SIGNAL(readyReadStdout()), this, TQ_SLOT(readFromStdout()) );
75 connect( m_process, TQ_SIGNAL(processExited()), this, TQ_SLOT(processExited()) );
76 if (!m_process->start()) {
77 m_result = false;
78 return m_result;
79 }
80 m_dlg = new KProgressDialog(0, 0, i18n("Compacting database"),
81 "<qt>"+i18n("Compacting database \"%1\"...")
82 .arg("<nobr>"+TQDir::convertSeparators(TQFileInfo(m_filePath).fileName())+"</nobr>")
83 );
84 m_dlg->adjustSize();
85 m_dlg->resize(300, m_dlg->height());
86 connect(m_dlg, TQ_SIGNAL(cancelClicked()), this, TQ_SLOT(cancelClicked()));
87 m_dlg->setMinimumDuration(1000);
88 m_dlg->setAutoClose(true);
89 m_dlg->progressBar()->setTotalSteps(100);
90 m_dlg->exec();
91 while (m_process->isRunning()) {
92 readFromStdout();
93 usleep(50000);
94 }
95 delete m_process;
96 m_process = 0;
97 if (m_result == true) {
98 const uint newSize = TQFileInfo(m_filePath).size();
99 const uint decrease = 100-100*newSize/origSize;
100 KMessageBox::information(0, i18n("The database has been compacted. Current size decreased by %1% to %2.")
101 .arg(decrease).arg(TDEIO::convertSize(newSize)));
102 }
103 return m_result;
104}
105
106void SQLiteVacuum::readFromStdout()
107{
108 while (true) {
109 TQString s( m_process->readLineStdout() ); //readStdout();
110 if (s.isEmpty())
111 break;
112 m_dlg->progressBar()->setProgress(m_percent);
113// KexiDBDrvDbg << m_percent << " " << s << endl;
114 if (s.startsWith("VACUUM: ")) {
115 //set previously known progress
116 m_dlg->progressBar()->setProgress(m_percent);
117 //update progress info
118 if (s.mid(8,4)=="100%") {
119 m_percent = 100;
120 m_dlg->setAllowCancel(false);
121 m_dlg->setCursor(TQCursor(TQt::WaitCursor));
122 }
123 else if (s.mid(9,1)=="%") {
124 m_percent = s.mid(8,1).toInt();
125 }
126 else if (s.mid(10,1)=="%") {
127 m_percent = s.mid(8,2).toInt();
128 }
129 m_process->writeToStdin(TQString(" "));
130 }
131 }
132}
133
134void SQLiteVacuum::processExited()
135{
136// KexiDBDrvDbg << sender()->name() << " EXIT" << endl;
137 m_dlg->close();
138 delete m_dlg;
139 m_dlg = 0;
140}
141
142void SQLiteVacuum::cancelClicked()
143{
144 if (!m_process->normalExit()) {
145 m_process->writeToStdin(TQString("q")); //quit
146 m_result = cancelled;
147 }
148}
149
150#include "sqlitevacuum.moc"
tristate run()
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100