kexi

KexiStartup_p.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2004 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 "KexiStartup_p.h"
21
22#include <kstandarddirs.h>
23#include <kprogress.h>
24#include <kprocess.h>
25#include <kdebug.h>
26#include <tdelocale.h>
27
28#include <tqfileinfo.h>
29#include <tqdir.h>
30#include <tqapplication.h>
31
32SQLite2ToSQLite3Migration::SQLite2ToSQLite3Migration(const TQString& filePath)
33: m_filePath(filePath)
34{
35 m_process = 0;
36 m_dlg = 0;
37 result = false;
38 m_run = false;
39}
40
41SQLite2ToSQLite3Migration::~SQLite2ToSQLite3Migration()
42{
43 delete m_process;
44 m_dlg->close();
45 delete m_dlg;
46}
47
48tristate SQLite2ToSQLite3Migration::run()
49{
50 if (m_run)
51 return false;
52 m_run = true;
53 const TQString ksqlite2to3_app = TDEStandardDirs::findExe( "ksqlite2to3" );
54 if (ksqlite2to3_app.isEmpty())
55 return false;
56
57 TQFileInfo fi(m_filePath);
58 if (fi.isSymLink()) {
59 m_filePath = fi.readLink();
60 fi = TQFileInfo(m_filePath);
61 }
62 //remember permissions of m_filePath
63 m_restoreStat = (0==stat(TQFile::encodeName(m_filePath), &m_st));
64
65 m_process = new TDEProcess(this, "process");
66 *m_process << ksqlite2to3_app << m_filePath;
67 m_process->setWorkingDirectory( fi.dir(true).absPath() );
68 connect( m_process, TQ_SIGNAL(receivedStderr(TDEProcess*,char*,int)),
69 this, TQ_SLOT(receivedStderr(TDEProcess*,char*,int)));
70 connect( m_process, TQ_SIGNAL(processExited(TDEProcess*)), this, TQ_SLOT(processExited(TDEProcess*)) );
71 if (!m_process->start(TDEProcess::NotifyOnExit, TDEProcess::Stderr))
72 return false;
73
74 m_dlg = new KProgressDialog(0, 0, TQString(),
75 i18n("Saving \"%1\" project file to a new \"%2\" database format...")
76 .arg(TQDir::convertSeparators(TQFileInfo(m_filePath).fileName())).arg("SQLite3")
77 );
78 m_dlg->setModal(true);
79 connect(m_dlg, TQ_SIGNAL(cancelClicked()), this, TQ_SLOT(cancelClicked()));
80 m_dlg->setMinimumDuration(1000);
81 m_dlg->setAutoClose(true);
82 m_dlg->progressBar()->setTotalSteps(100);
83 m_dlg->progressBar()->setProgress(0);
84 m_dlg->exec();
85
86 if (result!=true)
87 return result;
88
89 return result;
90}
91
92extern void updateProgressBar(KProgressDialog *pd, char *buffer, int buflen);
93
94void SQLite2ToSQLite3Migration::receivedStderr(TDEProcess *, char *buffer, int buflen)
95{
96 updateProgressBar(m_dlg, buffer, buflen);
97}
98
99void SQLite2ToSQLite3Migration::processExited(TDEProcess* process)
100{
101 kdDebug() << "EXIT " << process->name() << endl;
102
103 kdDebug() << process->isRunning() << " " << process->exitStatus() << endl;
104 m_dlg->close();
105 result = !process->isRunning() && 0==process->exitStatus();//m_process->normalExit();
106 kdDebug() << result.toString() << endl;
107 if (result == true) {
108 if (m_restoreStat) {
109 //restore permissions for m_filePath
110 chmod(TQFile::encodeName(m_filePath), m_st.st_mode);
111 chown(TQFile::encodeName(m_filePath), m_st.st_uid, m_st.st_gid);
112 }
113 }
114}
115
116void SQLite2ToSQLite3Migration::cancelClicked()
117{
118 kdDebug() << result.toString() << " cancelClicked() " <<m_process->isRunning() << " "
119 << m_process->exitStatus() << endl;
120 if (!m_process->isRunning() && 0==m_process->exitStatus())
121 return;
122 result = cancelled;
123 m_process->kill();
124}
125
126#include "KexiStartup_p.moc"
127
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100