17 #include "ui_DataMigrationWidget.h"
22 #include <QDirIterator>
27 #include <QProgressDialog>
48 if( oldLocalPaths.isEmpty() ) {
53 QDir currentLocalDir( currentLocalPath );
54 if( currentLocalDir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot ).size() != 0 ) {
58 foreach(
const QString& oldLocalPath, oldLocalPaths ) {
59 QDir oldLocalDir( oldLocalPath );
61 if( oldLocalDir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot ).size() == 0 ) {
65 QPointer<QDialog> dialog =
new QDialog();
66 Ui::DataMigrationWidget dataMigrationWidget;
68 dataMigrationWidget.setupUi( dialog );
69 if( dialog->exec() == QDialog::Accepted ) {
70 DataMigration::moveFiles( oldLocalPath, currentLocalPath );
78 void DataMigration::moveFiles(
const QString& source,
const QString& target )
80 if( !QDir().rmdir( target ) ) {
81 mDebug() <<
"Removing of the target directory failed";
88 mDebug() <<
"Rename" << source <<
"to" << target;
89 if( !QDir().rename( source, target ) ) {
90 mDebug() <<
"Simple renaming of the data directory failed. Moving single files";
92 QProgressDialog progressDialog;
93 progressDialog.setWindowModality( Qt::WindowModal );
94 progressDialog.setMinimum( 0 );
95 progressDialog.setMaximum( std::numeric_limits<int>::max() );
96 progressDialog.setAutoReset(
false );
97 progressDialog.setAutoClose(
false );
98 progressDialog.setWindowTitle( tr(
"Marble data conversion" ) );
99 progressDialog.setLabelText( tr(
"Converting data ..." ) );
101 QDir().mkpath( target );
102 QString sourcePath = QDir( source ).canonicalPath();
103 int sourcePathLength = sourcePath.length();
106 QStack<QString> dirs;
107 dirs.push( sourcePath );
109 QStack<int> progressSliceSizeStack;
110 progressSliceSizeStack.push( progressDialog.maximum() );
113 while( !dirs.isEmpty() ) {
114 if( progressDialog.wasCanceled() ) {
118 QString sourceDirPath = dirs.top();
119 mDebug() <<
"DataMigration: Current source dir path ="
121 mDebug() <<
"SliceSize =" << progressSliceSizeStack.top();
123 if( !sourceDirPath.startsWith( sourcePath ) ) {
125 progress += progressSliceSizeStack.pop();
126 progressDialog.setValue( progress );
130 QDir sourceDir( sourceDirPath );
132 QStringList files = sourceDir.entryList( QDir::Files
134 | QDir::NoDotAndDotDot );
135 QStringList childDirs = sourceDir.entryList( QDir::Dirs
137 | QDir::NoDotAndDotDot );
138 int childSliceSize = 0;
139 if( !childDirs.isEmpty() ) {
140 childSliceSize = progressSliceSizeStack.pop() / childDirs.size();
141 progressSliceSizeStack.push( 0 );
144 if( files.isEmpty() && childDirs.isEmpty() )
147 mDebug() <<
"DataMigration:" << dirs.top()
149 QDir().rmdir( dirs.pop() );
150 progress += progressSliceSizeStack.pop();
151 progressDialog.setValue( progress );
155 foreach(
const QString& childDir, childDirs ) {
156 dirs.push( sourceDirPath +
'/' + childDir );
157 progressSliceSizeStack.push( childSliceSize );
161 QString targetDirPath = sourceDirPath;
162 targetDirPath.remove( 0, sourcePathLength );
163 targetDirPath.prepend( target );
164 QDir().mkpath( targetDirPath );
167 foreach(
const QString& file, files ) {
168 if( progressDialog.wasCanceled() ) {
172 QString sourceFilePath = sourceDirPath;
173 sourceFilePath +=
'/';
174 sourceFilePath += file;
176 if( !sourceFilePath.startsWith( sourcePath ) ) {
180 QString targetFilePath = sourceFilePath;
181 targetFilePath.remove( 0, sourcePathLength );
182 targetFilePath.prepend( target );
184 QFile::copy( sourceFilePath, targetFilePath );
185 QFile::remove( sourceFilePath );
194 #include "DataMigration.moc"
static QString localPath()
DataMigration(QObject *parent)
QDebug mDebug()
a function to replace qDebug() in Marble library code
static QStringList oldLocalPaths()