• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • lib
  • marble
DataMigration.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Bastian Holst <bastianholst@gmx.de>
9 //
10 
11 // Self
12 #include "DataMigration.h"
13 
14 // Marble
15 #include "MarbleDebug.h"
16 #include "MarbleDirs.h"
17 #include "ui_DataMigrationWidget.h"
18 
19 // Qt
20 #include <QDebug>
21 #include <QDir>
22 #include <QDirIterator>
23 #include <QFileInfo>
24 #include <QPointer>
25 #include <QStack>
26 #include <QDialog>
27 #include <QProgressDialog>
28 
29 // std
30 #include <limits>
31 
32 namespace Marble
33 {
34 
35 DataMigration::DataMigration( QObject *parent )
36  : QObject( parent )
37 {
38 }
39 
40 DataMigration::~DataMigration()
41 {
42 }
43 
44 void DataMigration::exec()
45 {
46  QStringList oldLocalPaths = MarbleDirs::oldLocalPaths();
47 
48  if( oldLocalPaths.isEmpty() ) {
49  return;
50  }
51 
52  QString currentLocalPath = MarbleDirs::localPath();
53  QDir currentLocalDir( currentLocalPath );
54  if( currentLocalDir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot ).size() != 0 ) {
55  return;
56  }
57 
58  foreach( const QString& oldLocalPath, oldLocalPaths ) {
59  QDir oldLocalDir( oldLocalPath );
60 
61  if( oldLocalDir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot ).size() == 0 ) {
62  continue;
63  }
64 
65  QPointer<QDialog> dialog = new QDialog();
66  Ui::DataMigrationWidget dataMigrationWidget;
67 
68  dataMigrationWidget.setupUi( dialog );
69  if( dialog->exec() == QDialog::Accepted ) {
70  DataMigration::moveFiles( oldLocalPath, currentLocalPath );
71  }
72  delete dialog;
73 
74  return;
75  }
76 }
77 
78 void DataMigration::moveFiles( const QString& source, const QString& target )
79 {
80  if( !QDir().rmdir( target ) ) {
81  mDebug() << "Removing of the target directory failed";
82  }
83 
84  // Trying to simply rename the directory. This is the fastest method, but it is not always
85  // possible. For example when the directories are on different file systems.
86  // If the renaming of the directory is not successful, we have to copy and delete each
87  // file separatetly.
88  mDebug() << "Rename" << source << "to" << target;
89  if( !QDir().rename( source, target ) ) {
90  mDebug() << "Simple renaming of the data directory failed. Moving single files";
91 
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 ..." ) );
100 
101  QDir().mkpath( target );
102  QString sourcePath = QDir( source ).canonicalPath();
103  int sourcePathLength = sourcePath.length();
104 
105  // Running through all files recursively
106  QStack<QString> dirs;
107  dirs.push( sourcePath );
108 
109  QStack<int> progressSliceSizeStack;
110  progressSliceSizeStack.push( progressDialog.maximum() );
111  int progress = 0;
112 
113  while( !dirs.isEmpty() ) {
114  if( progressDialog.wasCanceled() ) {
115  return;
116  }
117 
118  QString sourceDirPath = dirs.top();
119  mDebug() << "DataMigration: Current source dir path ="
120  << sourceDirPath;
121  mDebug() << "SliceSize =" << progressSliceSizeStack.top();
122 
123  if( !sourceDirPath.startsWith( sourcePath ) ) {
124  dirs.pop();
125  progress += progressSliceSizeStack.pop();
126  progressDialog.setValue( progress );
127  continue;
128  }
129 
130  QDir sourceDir( sourceDirPath );
131  // Creating child file/dir lists.
132  QStringList files = sourceDir.entryList( QDir::Files
133  | QDir::NoSymLinks
134  | QDir::NoDotAndDotDot );
135  QStringList childDirs = sourceDir.entryList( QDir::Dirs
136  | QDir::NoSymLinks
137  | QDir::NoDotAndDotDot );
138  int childSliceSize = 0;
139  if( !childDirs.isEmpty() ) {
140  childSliceSize = progressSliceSizeStack.pop() / childDirs.size();
141  progressSliceSizeStack.push( 0 );
142  }
143 
144  if( files.isEmpty() && childDirs.isEmpty() )
145  {
146  // Remove empty directory
147  mDebug() << "DataMigration:" << dirs.top()
148  << "finished";
149  QDir().rmdir( dirs.pop() );
150  progress += progressSliceSizeStack.pop();
151  progressDialog.setValue( progress );
152  }
153  else {
154  // Add child directories to the stack
155  foreach( const QString& childDir, childDirs ) {
156  dirs.push( sourceDirPath + '/' + childDir );
157  progressSliceSizeStack.push( childSliceSize );
158  }
159 
160  // Creating target dir
161  QString targetDirPath = sourceDirPath;
162  targetDirPath.remove( 0, sourcePathLength );
163  targetDirPath.prepend( target );
164  QDir().mkpath( targetDirPath );
165 
166  // Copying contents
167  foreach( const QString& file, files ) {
168  if( progressDialog.wasCanceled() ) {
169  return;
170  }
171 
172  QString sourceFilePath = sourceDirPath;
173  sourceFilePath += '/';
174  sourceFilePath += file;
175 
176  if( !sourceFilePath.startsWith( sourcePath ) ) {
177  continue;
178  }
179 
180  QString targetFilePath = sourceFilePath;
181  targetFilePath.remove( 0, sourcePathLength );
182  targetFilePath.prepend( target );
183 
184  QFile::copy( sourceFilePath, targetFilePath );
185  QFile::remove( sourceFilePath );
186  }
187  }
188  }
189  }
190 }
191 
192 }
193 
194 #include "DataMigration.moc"
QDialog
Marble::MarbleDirs::localPath
static QString localPath()
Definition: MarbleDirs.cpp:217
QObject
MarbleDebug.h
MarbleDirs.h
DataMigration.h
Marble::DataMigration::exec
void exec()
Definition: DataMigration.cpp:44
Marble::DataMigration::DataMigration
DataMigration(QObject *parent)
Definition: DataMigration.cpp:35
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::MarbleDirs::oldLocalPaths
static QStringList oldLocalPaths()
Definition: MarbleDirs.cpp:236
Marble::DataMigration::~DataMigration
virtual ~DataMigration()
Definition: DataMigration.cpp:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal