Added progress bar to migration routine

This commit is contained in:
Phil Olynyk 2019-04-02 10:04:44 -04:00
parent abd2389772
commit 98811bca15
2 changed files with 142 additions and 89 deletions

View File

@ -14,6 +14,7 @@
#include <QFileDialog>
#include <QFontDatabase>
#include <QStandardPaths>
#include <QProgressDialog>
#include "version.h"
#include "logger.h"
@ -21,6 +22,9 @@
#include "SleepLib/profiles.h"
#include "translation.h"
#include <ctime>
#include <chrono>
// Gah! I must add the real darn plugin system one day.
#include "SleepLib/loader_plugins/prs1_loader.h"
#include "SleepLib/loader_plugins/cms50_loader.h"
@ -42,8 +46,27 @@ MainWindow *mainwin = nullptr;
int compareVersion(QString version);
bool copyRecursively(QString sourceFolder, QString destFolder)
{
int numFilesCopied = 0;
// Count the number of files in this directory and all subdirectories
int countRecursively(QString sourceFolder) {
QDir sourceDir(sourceFolder);
if(!sourceDir.exists())
return 0;
int numFiles = sourceDir.count();
QStringList dirs = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
for(int i = 0; i< dirs.count(); i++) {
QString srcName = sourceFolder + QDir::separator() + dirs[i];
numFiles += countRecursively(srcName);
}
return numFiles;
}
bool copyRecursively(QString sourceFolder, QString destFolder, QProgressDialog& progress) {
bool success = false;
QDir sourceDir(sourceFolder);
@ -59,24 +82,28 @@ bool copyRecursively(QString sourceFolder, QString destFolder)
QString srcName = sourceFolder + QDir::separator() + files[i];
QString destName = destFolder + QDir::separator() + files[i];
success = QFile::copy(srcName, destName);
numFilesCopied++;
if ((numFilesCopied % 20) == 1) { // Update progress bar every 20 files
progress.setValue(numFilesCopied);
QCoreApplication::processEvents();
}
if(!success)
return false;
}
files.clear();
files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
for(int i = 0; i< files.count(); i++)
{
for(int i = 0; i< files.count(); i++) {
QString srcName = sourceFolder + QDir::separator() + files[i];
QString destName = destFolder + QDir::separator() + files[i];
// qDebug() << "Copy from "+srcName+" to "+destName;
success = copyRecursively(srcName, destName);
success = copyRecursively(srcName, destName, progress);
if(!success)
return false;
}
return true;
}
}
bool processPreferenceFile( QString path ) {
bool success = true;
@ -140,6 +167,8 @@ bool migrateFromSH(QString destDir) {
QString datadir;
bool selectingFolder = true;
bool success = false;
// long int startTime, countDone, allDone;
if (destDir.isEmpty()) {
qDebug() << "Migration path is empty string";
return success;
@ -173,7 +202,19 @@ bool migrateFromSH(QString destDir) {
}
}
success = copyRecursively(datadir, destDir);
auto startTime = std::chrono::steady_clock::now();
int numFiles = countRecursively(datadir); // count number of files to be copied
auto countDone = std::chrono::steady_clock::now();
qDebug() << "Number of files to migrate: " << numFiles;
QProgressDialog progress (QObject::tr("Migrating ") + QString::number(numFiles) + QObject::tr(" files")+"\n"+
QObject::tr("from ") + QDir(datadir).dirName() + "\n"+QObject::tr("to ") +
QDir(destDir).dirName(), QString(), 0, numFiles, 0, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
progress.setValue(0);
progress.setMinimumWidth(300);
progress.show();
success = copyRecursively(datadir, destDir, progress);
if (success) {
qDebug() << "Finished copying " + datadir;
}
@ -186,11 +227,17 @@ bool migrateFromSH(QString destDir) {
success = process_a_Profile( destDir+"/Profiles/"+names[i] );
}
progress.setValue(numFiles);
auto allDone = std::chrono::steady_clock::now();
auto elapsedCount = std::chrono::duration_cast<std::chrono::microseconds>(countDone - startTime);
auto elapsedCopy = std::chrono::duration_cast<std::chrono::microseconds>(allDone - countDone);
qDebug() << "Counting files took " << elapsedCount.count() << " microsecs";
qDebug() << "Migrating files took " << elapsedCopy.count() << " microsecs";
return success;
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
#ifdef Q_WS_X11
XInitThreads();
#endif
@ -294,8 +341,8 @@ int main(int argc, char *argv[])
//#endif
/*************************************************************************************
#ifdef BROKEN_OPENGL_BUILD
/*************************************************************************************
#ifdef BROKEN_OPENGL_BUILD
Q_UNUSED(bad_graphics)
Q_UNUSED(intel_graphics)
@ -311,7 +358,7 @@ int main(int argc, char *argv[])
settings.setValue(BetterBuild, true);
}
}
#else
#else
if (bad_graphics) {
QMessageBox::warning(nullptr, QObject::tr("Incompatible Graphics Hardware"),
QObject::tr("This build of OSCAR requires OpenGL 2.0 support to function correctly, and unfortunately your computer lacks this capability.") + "<br/><br/>"+
@ -322,8 +369,8 @@ int main(int argc, char *argv[])
QMessageBox::Ok, QMessageBox::Ok);
exit(1);
}
#endif
****************************************************************************************************************/
#endif
****************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////////////////
// Datafolder location Selection
////////////////////////////////////////////////////////////////////////////////////////////
@ -513,7 +560,9 @@ int main(int argc, char *argv[])
Q_UNUSED(dont_load_profile)
#ifndef NO_UPDATER
if (check_updates) { mainwin->CheckForUpdates(); }
if (check_updates) {
mainwin->CheckForUpdates();
}
#endif
mainwin->SetupGUI();

View File

@ -75,7 +75,11 @@ MainWindow::MainWindow(QWidget *parent) :
connect(logger, SIGNAL(outputLog(QString)), this, SLOT(logMessage(QString)));
}
// Initialise oscr app registry stuff
// Bring window to top (useful when language is changed) - GTS 3/31/2019
this->activateWindow();
this->raise();
// Initialise oscar app registry stuff
QSettings settings;
// Load previous Window geometry