summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/drivers/sqlite/sqlitevacuum.cpp
blob: 71ba2321681b64ed1f714cfb6489fcaec864dfec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* This file is part of the KDE project
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <kexidb/global.h>
#include "sqlitevacuum.h"

#include <kstandarddirs.h>
#include <kprogress.h>
#include <kdebug.h>
#include <klocale.h>
#include <ktempfile.h>
#include <kmessagebox.h>
#include <tdeio/global.h>

#include <tqfileinfo.h>
#include <tqdir.h>
#include <tqapplication.h>
#include <tqprocess.h>
#include <tqcursor.h>

#include <unistd.h>

SQLiteVacuum::SQLiteVacuum(const TQString& filePath)
: m_filePath(filePath)
{
	m_process = 0;
	m_percent = 0;
	m_dlg = 0;
	m_result = true;
}

SQLiteVacuum::~SQLiteVacuum()
{
	delete m_process;
	if (m_dlg)
		m_dlg->close();
	delete m_dlg;
}

tristate SQLiteVacuum::run()
{
	const TQString ksqlite_app = TDEStandardDirs::findExe( "ksqlite" );
	if (ksqlite_app.isEmpty()) {
		m_result = false;
		return m_result;
	}
	TQFileInfo fi(m_filePath);
	if (!fi.isReadable()) {
		KexiDBDrvWarn << "SQLiteVacuum::run(): No such file" << m_filePath << endl;
		return false;
	}
	const uint origSize = fi.size();

	TQStringList args;
	args << ksqlite_app << "-verbose-vacuum" << m_filePath << "vacuum";
	m_process = new TQProcess(args, this, "process");
	m_process->setWorkingDirectory( TQFileInfo(m_filePath).dir(true) );
	connect( m_process, TQT_SIGNAL(readyReadStdout()), this, TQT_SLOT(readFromStdout()) );
	connect( m_process, TQT_SIGNAL(processExited()), this, TQT_SLOT(processExited()) );
	if (!m_process->start()) {
		m_result = false;
		return m_result;
	}
	m_dlg = new KProgressDialog(0, 0, i18n("Compacting database"), 
		"<qt>"+i18n("Compacting database \"%1\"...")
		.arg("<nobr>"+TQDir::convertSeparators(TQFileInfo(m_filePath).fileName())+"</nobr>")
	);
	m_dlg->adjustSize();
	m_dlg->resize(300, m_dlg->height());
	connect(m_dlg, TQT_SIGNAL(cancelClicked()), this, TQT_SLOT(cancelClicked()));
	m_dlg->setMinimumDuration(1000);
	m_dlg->setAutoClose(true);
	m_dlg->progressBar()->setTotalSteps(100);
	m_dlg->exec();
	while (m_process->isRunning()) {
		readFromStdout();
		usleep(50000);
	}
	delete m_process;
	m_process = 0;
	if (m_result == true) {
		const uint newSize = TQFileInfo(m_filePath).size();
		const uint decrease = 100-100*newSize/origSize;
		KMessageBox::information(0, i18n("The database has been compacted. Current size decreased by %1% to %2.")
			.arg(decrease).arg(TDEIO::convertSize(newSize)));
	}
	return m_result;
}

void SQLiteVacuum::readFromStdout()
{
	while (true) {
		TQString s( m_process->readLineStdout() ); //readStdout();
		if (s.isEmpty())
			break;
		m_dlg->progressBar()->setProgress(m_percent);
//		KexiDBDrvDbg << m_percent << " " << s << endl;
		if (s.startsWith("VACUUM: ")) {
			//set previously known progress
			m_dlg->progressBar()->setProgress(m_percent);
			//update progress info
			if (s.mid(8,4)=="100%") {
				m_percent = 100;
				m_dlg->setAllowCancel(false);
				m_dlg->setCursor(TQCursor(TQt::WaitCursor));
			}
			else if (s.mid(9,1)=="%") {
				m_percent = s.mid(8,1).toInt();
			}
			else if (s.mid(10,1)=="%") {
				m_percent = s.mid(8,2).toInt();
			}
			m_process->writeToStdin(TQString(" "));
		}
	}
}

void SQLiteVacuum::processExited()
{
//	KexiDBDrvDbg << sender()->name() << " EXIT" << endl;
	m_dlg->close();
	delete m_dlg;
	m_dlg = 0;
}

void SQLiteVacuum::cancelClicked()
{
	if (!m_process->normalExit()) {
		m_process->writeToStdin(TQString("q")); //quit
		m_result = cancelled;
	}
}

#include "sqlitevacuum.moc"