kmdbcreator.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
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 "kmdbcreator.h"
00021 #include "kmfactory.h"
00022 #include "kmmanager.h"
00023 
00024 #include <qprogressdialog.h>
00025 #include <qfileinfo.h>
00026 #include <qdir.h>
00027 #include <klocale.h>
00028 #include <kapplication.h>
00029 #include <kstandarddirs.h>
00030 #include <kdebug.h>
00031 
00032 KMDBCreator::KMDBCreator(QObject *parent, const char *name)
00033 : QObject(parent,name)
00034 {
00035     m_dlg = 0;
00036     m_status = true;
00037 
00038     connect(&m_proc,SIGNAL(receivedStdout(KProcess*,char*,int)),SLOT(slotReceivedStdout(KProcess*,char*,int)));
00039     connect(&m_proc,SIGNAL(receivedStderr(KProcess*,char*,int)),SLOT(slotReceivedStderr(KProcess*,char*,int)));
00040     connect(&m_proc,SIGNAL(processExited(KProcess*)),SLOT(slotProcessExited(KProcess*)));
00041 }
00042 
00043 KMDBCreator::~KMDBCreator()
00044 {
00045     if (m_proc.isRunning())
00046         m_proc.kill();
00047     // do not delete the progress dialog anymore: it's persistent and owned by
00048     // it's parent. It will be destroyed along with its parent.
00049 }
00050 
00051 bool KMDBCreator::checkDriverDB(const QString& dirname, const QDateTime& d)
00052 {
00053     // don't block GUI
00054     kapp->processEvents();
00055 
00056     // first check current directory
00057     QFileInfo   dfi(dirname);
00058     if (dfi.lastModified() > d)
00059         return false;
00060 
00061     // then check most recent file in current directory
00062     QDir    dir(dirname);
00063     const QFileInfoList *list = dir.entryInfoList(QDir::Files,QDir::Time);
00064     if (list && list->count() > 0 && list->getFirst()->lastModified() > d)
00065         return false;
00066 
00067     // then loop into subdirs
00068     QStringList slist = dir.entryList(QDir::Dirs,QDir::Time);
00069     for (QStringList::ConstIterator it=slist.begin(); it!=slist.end(); ++it)
00070         if ((*it) != "." && (*it) != ".." && !checkDriverDB(dir.absFilePath(*it),d))
00071             return false;
00072 
00073     // everything is OK
00074     return true;
00075 }
00076 
00077 bool KMDBCreator::createDriverDB(const QString& dirname, const QString& filename, QWidget *parent)
00078 {
00079     bool    started(true);
00080 
00081     // initialize status
00082     m_status = false;
00083     m_firstflag = true;
00084 
00085     // start the child process
00086     m_proc.clearArguments();
00087     QString exestr = KMFactory::self()->manager()->driverDbCreationProgram();
00088     m_proc << exestr << dirname << filename;
00089     kdDebug() << "executing : " << exestr << " " << dirname << " " << filename << endl;
00090     QString msg;
00091     if (exestr.isEmpty())
00092         msg = i18n("No executable defined for the creation of the "
00093                    "driver database. This operation is not implemented.");
00094     else if (KStandardDirs::findExe(exestr).isEmpty())
00095         msg = i18n("The executable %1 could not be found in your "
00096                    "PATH. Check that this program exists and is "
00097                "accessible in your PATH variable.").arg(exestr);
00098     else if (!m_proc.start(KProcess::NotifyOnExit, KProcess::AllOutput))
00099         msg = i18n("Unable to start the creation of the driver "
00100                    "database. The execution of %1 failed.").arg(exestr);
00101     if (!msg.isEmpty())
00102     {
00103         KMManager::self()->setErrorMsg(msg);
00104         started = false;
00105     }
00106 
00107     // Create the dialog if the process is running and if needed
00108     if (started)
00109     {
00110         if (!m_dlg)
00111         {
00112             m_dlg = new QProgressDialog(parent->topLevelWidget(),"progress-dialog",true);
00113             m_dlg->setLabelText(i18n("Please wait while KDE rebuilds a driver database."));
00114             m_dlg->setCaption(i18n("Driver Database"));
00115             connect(m_dlg,SIGNAL(canceled()),SLOT(slotCancelled()));
00116         }
00117         m_dlg->setMinimumDuration(0);   // always show the dialog
00118         m_dlg->setProgress(0);      // to force showing
00119     }
00120     else
00121         // be sure to emit this signal otherwise the DB widget won't never be notified
00122         emit dbCreated();
00123 
00124     return started;
00125 }
00126 
00127 void KMDBCreator::slotReceivedStdout(KProcess*, char *buf, int len)
00128 {
00129     // save buffer
00130     QString str( QCString(buf, len) );
00131 
00132     // get the number, cut the string at the first '\n' otherwise
00133     // the toInt() will return 0. If that occurs for the first number,
00134     // then the number of steps will be also 0.
00135     bool    ok;
00136     int p = str.find('\n');
00137     int n = str.mid(0, p).toInt(&ok);
00138 
00139     // process the number received
00140     if (ok && m_dlg)
00141     {
00142         if (m_firstflag)
00143         {
00144             m_dlg->setTotalSteps(n);
00145             m_firstflag = false;
00146         }
00147         else
00148         {
00149             m_dlg->setProgress(n);
00150         }
00151     }
00152 }
00153 
00154 void KMDBCreator::slotReceivedStderr(KProcess*, char*, int)
00155 {
00156     // just discard it for the moment
00157 }
00158 
00159 void KMDBCreator::slotProcessExited(KProcess*)
00160 {
00161     // delete the progress dialog
00162     if (m_dlg)
00163     {
00164         m_dlg->reset();
00165     }
00166 
00167     // set exit status
00168     m_status = (m_proc.normalExit() && m_proc.exitStatus() == 0);
00169     if (!m_status)
00170     {
00171         KMFactory::self()->manager()->setErrorMsg(i18n("Error while creating driver database: abnormal child-process termination."));
00172         // remove the incomplete driver DB file so that, it will be
00173         // reconstructed on next check
00174         QFile::remove(m_proc.args()[2]);
00175     }
00176     //else
00177         emit dbCreated();
00178 }
00179 
00180 void KMDBCreator::slotCancelled()
00181 {
00182     if (m_proc.isRunning())
00183         m_proc.kill();
00184     else
00185         emit dbCreated();
00186 }
00187 #include "kmdbcreator.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys