• 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
FileStoragePolicy.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 2007 Tobias Koenig <tokoe@kde.org>
9 //
10 
11 
12 // Own
13 #include "FileStoragePolicy.h"
14 
15 // Qt
16 #include <QDir>
17 #include <QDirIterator>
18 #include <QFile>
19 #include <QFileInfo>
20 
21 // Marble
22 #include "MarbleDebug.h"
23 #include "MarbleGlobal.h"
24 #include "MarbleDirs.h"
25 
26 using namespace Marble;
27 
28 FileStoragePolicy::FileStoragePolicy( const QString &dataDirectory, QObject *parent )
29  : StoragePolicy( parent ),
30  m_dataDirectory( dataDirectory )
31 {
32  if ( m_dataDirectory.isEmpty() )
33  m_dataDirectory = MarbleDirs::localPath() + "/cache/";
34 
35  if ( !QDir( m_dataDirectory ).exists() )
36  QDir::root().mkpath( m_dataDirectory );
37 }
38 
39 FileStoragePolicy::~FileStoragePolicy()
40 {
41 }
42 
43 bool FileStoragePolicy::fileExists( const QString &fileName ) const
44 {
45  const QString fullName( m_dataDirectory + '/' + fileName );
46  return QFile::exists( fullName );
47 }
48 
49 bool FileStoragePolicy::updateFile( const QString &fileName, const QByteArray &data )
50 {
51  QFileInfo const dirInfo( fileName );
52  QString const fullName = dirInfo.isAbsolute() ? fileName : m_dataDirectory + '/' + fileName;
53 
54  // Create directory if it doesn't exist yet...
55  QFileInfo info( fullName );
56 
57  const QDir localFileDir = info.dir();
58  const QString localFileDirPath = localFileDir.absolutePath();
59 
60  if ( !QDir( localFileDirPath ).exists() )
61  QDir::root().mkpath( localFileDirPath );
62 
63  // ... and save the file content
64  QFile file( fullName );
65  if ( !file.open( QIODevice::WriteOnly ) ) {
66  m_errorMsg = QString( "%1: %2" ).arg( fullName ).arg( file.errorString() );
67  qCritical() << "file.open" << m_errorMsg;
68  return false;
69  }
70 
71  quint64 oldSize = file.size();
72 
73  if ( !file.write( data ) ) {
74  m_errorMsg = QString( "%1: %2" ).arg( fullName ).arg( file.errorString() );
75  qCritical() << "file.write" << m_errorMsg;
76  emit sizeChanged( file.size() - oldSize );
77  return false;
78  }
79 
80  emit sizeChanged( file.size() - oldSize );
81  file.close();
82 
83  return true;
84 }
85 
86 void FileStoragePolicy::clearCache()
87 {
88  if ( m_dataDirectory.isEmpty() || !m_dataDirectory.endsWith(QLatin1String( "data" )) )
89  {
90  mDebug() << "Error: Refusing to erase files under unknown conditions for safety reasons!";
91  return;
92  }
93 
94  QString cachedMapsDirectory = m_dataDirectory + "/maps";
95 
96  QDirIterator it( cachedMapsDirectory, QDir::NoDotAndDotDot | QDir::Dirs );
97  mDebug() << cachedMapsDirectory;
98  while (it.hasNext()) {
99  it.next();
100  QString planetDirectory = it.filePath();
101  QDirIterator itPlanet( planetDirectory, QDir::NoDotAndDotDot | QDir::Dirs );
102  while (itPlanet.hasNext()) {
103  itPlanet.next();
104  QString themeDirectory = itPlanet.filePath();
105  QDirIterator itTheme( themeDirectory, QDir::NoDotAndDotDot | QDir::Dirs );
106  while (itTheme.hasNext()) {
107  itTheme.next();
108  QString tileDirectory = itTheme.filePath();
109 
110  if ( itTheme.fileName().toInt() <= maxBaseTileLevel ) {
111  continue;
112  }
113 
114  QDirIterator itTile( tileDirectory, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories );
115  while (itTile.hasNext()) {
116  itTile.next();
117  QString filePath = itTile.filePath();
118  QString lowerCase = filePath.toLower();
119 
120  // We try to be very careful and just delete images
121  // FIXME, when vectortiling I suppose also vector tiles will have
122  // to be deleted
123  if ( lowerCase.endsWith( QLatin1String( ".jpg" ) )
124  || lowerCase.endsWith( QLatin1String( ".png" ) )
125  || lowerCase.endsWith( QLatin1String( ".gif" ) )
126  || lowerCase.endsWith( QLatin1String( ".svg" ) )
127  )
128  {
129  // We cannot emit clear, because we don't make a full clear
130  QFile file( filePath );
131  emit sizeChanged( -file.size() );
132  file.remove();
133  }
134  }
135  }
136  }
137  }
138 }
139 
140 QString FileStoragePolicy::lastErrorMessage() const
141 {
142  return m_errorMsg;
143 }
144 
145 #include "FileStoragePolicy.moc"
Marble::FileStoragePolicy::fileExists
bool fileExists(const QString &fileName) const
Returns whether the fileName exists already.
Definition: FileStoragePolicy.cpp:43
FileStoragePolicy.h
Marble::StoragePolicy
Definition: StoragePolicy.h:25
Marble::StoragePolicy::sizeChanged
void sizeChanged(qint64)
Marble::MarbleDirs::localPath
static QString localPath()
Definition: MarbleDirs.cpp:217
QObject
MarbleDebug.h
Marble::FileStoragePolicy::lastErrorMessage
QString lastErrorMessage() const
Returns the last error message.
Definition: FileStoragePolicy.cpp:140
Marble::maxBaseTileLevel
const int maxBaseTileLevel
Definition: MarbleGlobal.h:241
Marble::FileStoragePolicy::clearCache
void clearCache()
Clears the cache.
Definition: FileStoragePolicy.cpp:86
MarbleDirs.h
Marble::FileStoragePolicy::FileStoragePolicy
FileStoragePolicy(const QString &dataDirectory=QString(), QObject *parent=0)
Creates a new file storage policy.
Definition: FileStoragePolicy.cpp:28
MarbleGlobal.h
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::FileStoragePolicy::updateFile
bool updateFile(const QString &fileName, const QByteArray &data)
Updates the fileName with the given data.
Definition: FileStoragePolicy.cpp:49
Marble::FileStoragePolicy::~FileStoragePolicy
~FileStoragePolicy()
Destroys the cache storage policy.
Definition: FileStoragePolicy.cpp:39
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