void FlyEmBodySplitProjectDialog::connectSignalSlot() { connect(ui->bookmarkView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(locateBookmark(QModelIndex))); /* connect(ui->bookmarkView, SIGNAL(pressed(QModelIndex)), this, SLOT(showBookmarkContextMenu(QModelIndex))); */ //Progress signal-slot connect(this, SIGNAL(progressDone()), getMainWindow(), SIGNAL(progressDone())); connect(this, SIGNAL(messageDumped(QString,bool)), this, SLOT(dump(QString,bool))); connect(this, SIGNAL(sideViewCanceled()), this, SLOT(resetSideView())); connect(&m_project, SIGNAL(messageGenerated(QString, bool)), this, SIGNAL(messageDumped(QString, bool))); connect(&m_project, SIGNAL(progressStarted(QString,int)), this, SIGNAL(progressStarted(QString,int))); connect(&m_project, SIGNAL(progressAdvanced(double)), this, SIGNAL(progressAdvanced(double))); connect(&m_project, SIGNAL(progressDone()), this, SIGNAL(progressDone())); // connect(&m_project, SIGNAL(messageGenerated(QString)), // this, SLOT(dump(QString))); connect(&m_project, SIGNAL(errorGenerated(QString)), this, SLOT(dumpError(QString))); connect(this, SIGNAL(progressStarted(QString, int)), getMainWindow(), SLOT(startProgress(QString, int))); connect(this, SIGNAL(progressAdvanced(double)), getMainWindow(), SLOT(advanceProgress(double))); connect(this, SIGNAL(progressDone()), getMainWindow(), SLOT(endProgress())); }
bool RecipeDB::restore( const QString &file, QString *errMsg ) { kDebug(); m_dumpFile = KFilterDev::deviceForFile(file,"application/x-gzip"); if ( m_dumpFile->open( QIODevice::ReadOnly ) ) { m_dumpFile->setTextModeEnabled( true ); QString firstLine = QString::fromUtf8(m_dumpFile->readLine()).trimmed(); QString dbVersion = QString::fromUtf8(m_dumpFile->readLine()).trimmed(); dbVersion = dbVersion.right( dbVersion.length() - dbVersion.indexOf(":") - 2 ); if ( qRound(dbVersion.toDouble()*1e5) > qRound(latestDBVersion()*1e5) ) { //correct for float's imprecision if ( errMsg ) *errMsg = i18n( "This backup was created with a newer version of Krecipes and cannot be restored." ); delete m_dumpFile; return false; } KConfigGroup config = KGlobal::config()->group( "DBType" ); QString dbType = QString::fromUtf8(m_dumpFile->readLine()).trimmed(); dbType = dbType.right( dbType.length() - dbType.indexOf(":") - 2 ); if ( dbType.isEmpty() || !firstLine.startsWith("-- Generated for Krecipes") ) { if ( errMsg ) *errMsg = i18n("This file is not a Krecipes backup file or has become corrupt."); delete m_dumpFile; return false; } else if ( dbType != config.readEntry("Type",QString()) ) { if ( errMsg ) *errMsg = i18n("This backup was created using the \"%1\" backend. It can only be restored into a database using this backend." ,dbType); delete m_dumpFile; return false; } process = new KProcess; QStringList command = restoreCommand(); kDebug()<<"Restoring backup using: "<<command[0]; *process << command; QApplication::connect( process, SIGNAL(started()), this, SLOT(processStarted()) ); QApplication::connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)) ); QApplication::connect( process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)) ); m_processStarted = false; m_processFinished = false; m_processError = false; m_operationHalted = false; emit progressBegin(0,QString(), QString("<center><b>%1</b></center>%2") .arg(i18nc("@info:progress", "Restoring backup")) .arg(i18n("Depending on the number of recipes and amount of data, this could take some time.")),50); process->start(); m_localEventLoop = new QEventLoop; m_localEventLoop->exec(); if ( m_processError && !m_processStarted ) { if ( errMsg ) *errMsg = i18n("Unable to find or run the program '%1'. Either it is not installed on your system or it is not in $PATH.", command.first()); delete m_localEventLoop; delete process; process = NULL; delete m_dumpFile; emit progressDone(); return false; } //We have to first wipe the database structure. Note that if we load a dump //with from a previous version of Krecipes, the difference in structure // wouldn't allow the data to be inserted. This remains forward-compatibity //by loading the old schema and then porting it to the current version. kDebug()<<"Wiping database..."; empty(); //the user had better be warned! kDebug()<<"Database wiped."; kDebug()<<"Reading backup file..."; m_timer = new QTimer; QApplication::connect( m_timer, SIGNAL(timeout()), this, SLOT(processReadDump()) ); m_timer->start(); m_localEventLoop->exec(); delete m_timer; kDebug()<<"Done."; //Since the process will exit when all stdin has been sent and processed, //just loop until the process is no longer running. If something goes //wrong, the user can still hit cancel. process->closeWriteChannel(); if (!m_processFinished && !m_processError) { kDebug()<<"Waiting for process exit..."; m_localEventLoop->exec(); } delete m_localEventLoop; delete process; process = NULL; emit progressDone(); //Since we just loaded part of a file, the database won't be in a usable state. //We'll wipe out the database structure and recreate it, leaving no data. if ( haltOperation ) { haltOperation=false; empty(); checkIntegrity(); delete m_dumpFile; if ( errMsg ) { *errMsg = i18n("Restore Failed"); } return false; } m_dumpFile->close(); checkIntegrity(); }
bool RecipeDB::backup( const QString &backup_file, QString *errMsg ) { kDebug()<<"Backing up current database to "<<backup_file; process = new KProcess; m_dumpFile = KFilterDev::deviceForFile(backup_file,"application/x-gzip"); if ( !m_dumpFile->open( QIODevice::WriteOnly ) ) { kDebug()<<"Couldn't open "<<backup_file; return false; } m_dumpFile->setTextModeEnabled( true ); QStringList command = backupCommand(); if ( command.count() == 0 ) { kDebug()<<"Backup not available for this database backend"; return false; } KConfigGroup config = KGlobal::config()->group( "DBType" ); QString dbVersionString = QString::number(latestDBVersion()); m_dumpFile->write(QByteArray("-- Generated for Krecipes v")); m_dumpFile->write(krecipes_version().toUtf8()); m_dumpFile->write(QByteArray("\n")); m_dumpFile->write(QByteArray("-- Krecipes database schema: ")); m_dumpFile->write(dbVersionString.toUtf8()); m_dumpFile->write(QByteArray("\n")); m_dumpFile->write(QByteArray("-- Krecipes database backend: ")); m_dumpFile->write(config.readEntry( "Type" ).toUtf8()); m_dumpFile->write(QByteArray("\n")); kDebug()<<"Running '"<<command.first()<<"' to create backup file"; *process << command /*<< ">" << backup_file*/; QApplication::connect( process, SIGNAL(readyRead()), this, SLOT(processDumpOutput()) ); QApplication::connect( process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)) ); QApplication::connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)) ); process->setOutputChannelMode( KProcess::MergedChannels ); m_processFinished = false; m_processError = false; haltOperation = false; m_operationHalted = false; emit progressBegin(0,QString(), QString("<center><b>%1</b></center>%2") .arg(i18nc("@info:progress", "Creating complete backup")) .arg(i18n("Depending on the number of recipes and amount of data, this could take some time.")),50); m_localEventLoop = new QEventLoop; //This timer is to let the GUI kill the process even if it's hanged, //i.e. we will not receive readyRead() signals anymore for any reason. m_timer = new QTimer; QApplication::connect( m_timer, SIGNAL(timeout()), this, SLOT(cancelWatcher()) ); m_timer->start(1000); process->start(); m_localEventLoop->exec(); delete m_timer; delete m_localEventLoop; if ( m_operationHalted ) { //User cancelled it; we'll still consider the operation successful, //but delete the file we created. kDebug()<<"Process killed, deleting partial backup."; QFile::remove(backup_file); } else if ( m_processError && !m_processFinished) { if ( errMsg ) *errMsg = i18n("Unable to find or run the program '%1'. Either it is not installed on your system or it is not in $PATH.",command.first()); QFile::remove(backup_file); delete process; process = NULL; delete m_dumpFile; emit progressDone(); return false; } else if ((m_exitCode != 0) || (m_exitStatus != QProcess::NormalExit)) { kDebug()<<"Process failed."; //Since the process failed, dumpStream should have output from the app as to why it did QString appOutput; m_dumpFile->close(); if ( m_dumpFile->open( QIODevice::ReadOnly ) ) { QTextStream appErrStream( m_dumpFile ); //ignore our own versioning output appErrStream.readLine(); appErrStream.readLine(); appErrStream.readLine(); appOutput = appErrStream.readAll(); } else kDebug()<<"Unable to open file to get error output."; if ( errMsg ) *errMsg = i18n("Backup failed.\n%1", appOutput); QFile::remove(backup_file); delete process; process = NULL; delete m_dumpFile; emit progressDone(); return false; } kDebug()<<"Backup finished."; delete process; process = NULL; delete m_dumpFile; emit progressDone(); return true; }