kexi

KexiStartup_p.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2004 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 "KexiStartup_p.h"
00021 
00022 #include <kstandarddirs.h>
00023 #include <kprogress.h>
00024 #include <kprocess.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 #include <qfileinfo.h>
00029 #include <qdir.h>
00030 #include <qapplication.h>
00031 
00032 SQLite2ToSQLite3Migration::SQLite2ToSQLite3Migration(const QString& filePath)
00033 : m_filePath(filePath)
00034 {
00035     m_process = 0;
00036     m_dlg = 0;
00037     result = false;
00038     m_run = false;
00039 }
00040 
00041 SQLite2ToSQLite3Migration::~SQLite2ToSQLite3Migration()
00042 {
00043     delete m_process;
00044     m_dlg->close();
00045     delete m_dlg;
00046 }
00047 
00048 tristate SQLite2ToSQLite3Migration::run()
00049 {
00050     if (m_run)
00051         return false;
00052     m_run = true;
00053     const QString ksqlite2to3_app = KStandardDirs::findExe( "ksqlite2to3" );
00054     if (ksqlite2to3_app.isEmpty())
00055         return false;
00056 
00057     QFileInfo fi(m_filePath);
00058     if (fi.isSymLink()) {
00059         m_filePath = fi.readLink();
00060         fi = QFileInfo(m_filePath);
00061     }
00062     //remember permissions of m_filePath
00063     m_restoreStat = (0==stat(QFile::encodeName(m_filePath), &m_st));
00064 
00065     m_process = new KProcess(this, "process");
00066     *m_process << ksqlite2to3_app << m_filePath;
00067     m_process->setWorkingDirectory( fi.dir(true).absPath() );
00068     connect( m_process, SIGNAL(receivedStderr(KProcess*,char*,int)),
00069         this, SLOT(receivedStderr(KProcess*,char*,int)));
00070     connect( m_process, SIGNAL(processExited(KProcess*)), this, SLOT(processExited(KProcess*)) );
00071     if (!m_process->start(KProcess::NotifyOnExit, KProcess::Stderr))
00072         return false;
00073 
00074     m_dlg = new KProgressDialog(0, 0, QString::null, 
00075         i18n("Saving \"%1\" project file to a new \"%2\" database format...")
00076         .arg(QDir::convertSeparators(QFileInfo(m_filePath).fileName())).arg("SQLite3")
00077     );
00078     m_dlg->setModal(true);
00079     connect(m_dlg, SIGNAL(cancelClicked()), this, SLOT(cancelClicked()));
00080     m_dlg->setMinimumDuration(1000);
00081     m_dlg->setAutoClose(true);
00082     m_dlg->progressBar()->setTotalSteps(100);
00083     m_dlg->progressBar()->setProgress(0);
00084     m_dlg->exec();
00085 
00086     if (result!=true)
00087         return result;
00088 
00089     return result;
00090 }
00091 
00092 extern void updateProgressBar(KProgressDialog *pd, char *buffer, int buflen);
00093 
00094 void SQLite2ToSQLite3Migration::receivedStderr(KProcess *, char *buffer, int buflen)
00095 {
00096     updateProgressBar(m_dlg, buffer, buflen);
00097 }
00098 
00099 void SQLite2ToSQLite3Migration::processExited(KProcess* process)
00100 {
00101     kdDebug() << "EXIT " << process->name() << endl;
00102 
00103     kdDebug() << process->isRunning() << " " << process->exitStatus() << endl;
00104     m_dlg->close();
00105     result = !process->isRunning() && 0==process->exitStatus();//m_process->normalExit();
00106     kdDebug() << result.toString() << endl;
00107     if (result == true) {
00108         if (m_restoreStat) {
00109             //restore permissions for m_filePath
00110             chmod(QFile::encodeName(m_filePath), m_st.st_mode);
00111             chown(QFile::encodeName(m_filePath), m_st.st_uid, m_st.st_gid);
00112         }
00113     }
00114 }
00115 
00116 void SQLite2ToSQLite3Migration::cancelClicked()
00117 {
00118     kdDebug() << result.toString() << " cancelClicked() " <<m_process->isRunning() << " " 
00119         << m_process->exitStatus() << endl;
00120     if (!m_process->isRunning() && 0==m_process->exitStatus())
00121         return;
00122     result = cancelled;
00123     m_process->kill();
00124 }
00125 
00126 #include "KexiStartup_p.moc"
00127 
KDE Home | KDE Accessibility Home | Description of Access Keys