BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
IOFactory Class Reference

IO/files processing methods. More...

#include <iofactory.h>

Collaboration diagram for IOFactory:
Collaboration graph

Static Public Member Functions

static void playWaveFile (const QString &file_path)
 Plays selected file with default audio output. More...
 
static bool copyFile (const QString &source, const QString &destination)
 Copies source file into destination path. More...
 
static bool copyDirectory (QString source, QString destination)
 Copies source directory into destination path. More...
 
static bool removeDirectory (const QString &directory_name, const QStringList &exception_file_list=QStringList(), const QStringList &exception_folder_list=QStringList())
 Removes given directory and all its contents if not exceptions are given. More...
 
static QByteArray fileToBase64 (const QString &file_name)
 Takes input file and reads it into base64 byte array. More...
 
static bool base64ToFile (const QString &source_data, const QString &target_file)
 Takes base64 byte array and saves it into file. More...
 

Detailed Description

IO/files processing methods.

Definition at line 39 of file iofactory.h.

Member Function Documentation

bool IOFactory::base64ToFile ( const QString &  source_data,
const QString &  target_file 
)
static

Takes base64 byte array and saves it into file.

Parameters
source_dataSource base64 string.
target_filePath to target file.
Returns
Returns true if data was saved to file, false otherwise.

Definition at line 125 of file iofactory.cpp.

125  {
126  QFile target(target_file);
127 
128  if (!target.open(QIODevice::Unbuffered | QIODevice::WriteOnly | QIODevice::Truncate)) {
129  return false;
130  }
131 
132  target.write(QByteArray::fromBase64(source_data.toUtf8()));
133  target.close();
134  return true;
135 }

Here is the caller graph for this function:

bool IOFactory::copyDirectory ( QString  source,
QString  destination 
)
static

Copies source directory into destination path.

Parameters
sourcePath to source directory.
destinationPath to destination directory.
Returns
Returns true if copy operation was successfull.
Note
If destination exists, then it is overwritten.

Definition at line 137 of file iofactory.cpp.

137  {
138  QDir dir(source);
139 
140  if (! dir.exists()) {
141  return false;
142  }
143 
144  foreach (QString d, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
145  QString dst_path = destination + QDir::separator() + d;
146  dir.mkpath(dst_path);
147  copyDirectory(source + QDir::separator() + d, dst_path);
148  }
149 
150  foreach (QString f, dir.entryList(QDir::Files)) {
151  QString original_file = source + QDir::separator() + f;
152  QString destination_file = destination + QDir::separator() + f;
153 
154  if (!QFile::exists(destination_file) || QFile::remove(destination_file)) {
155  if (!QFile::copy(original_file, destination_file)) {
156  qDebug("Failed to copy file \'%s\'.", qPrintable(QDir::toNativeSeparators(original_file)));
157  }
158  }
159  else {
160  qDebug("Failed to remove file \'%s\'.", qPrintable(QDir::toNativeSeparators(original_file)));
161  }
162  }
163 
164  return true;
165 }
static bool copyDirectory(QString source, QString destination)
Copies source directory into destination path.
Definition: iofactory.cpp:137
bool IOFactory::copyFile ( const QString &  source,
const QString &  destination 
)
static

Copies source file into destination path.

Parameters
sourcePath to source file.
destinationPath to destination path.
Returns
Returns true if copy operation was successfull.
Warning
If destination exists, then it is overwritten.

Definition at line 69 of file iofactory.cpp.

69  {
70  if (!QFile::exists(source)) {
71  qDebug("Source file \'%s\' does not exist.", qPrintable(QDir::toNativeSeparators(source)));
72  return false;
73  }
74 
75  if (QFile::exists(destination)) {
76  if (!QFile::remove(destination)) {
77  qDebug("Destination file \'%s\' could not be removed.", qPrintable(QDir::toNativeSeparators(destination)));
78  return false;
79  }
80  }
81 
82  return QFile::copy(source, destination);
83 }

Here is the caller graph for this function:

QByteArray IOFactory::fileToBase64 ( const QString &  file_name)
static

Takes input file and reads it into base64 byte array.

Parameters
file_namePath to file.
Returns
Returns base64 byte array on success or empty array on failure.

Definition at line 111 of file iofactory.cpp.

111  {
112  QFile file(file_name);
113 
114  if (!file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
115  return QByteArray();
116  }
117  else {
118  QByteArray file_contents = file.readAll();
119  file.flush();
120  file.close();
121  return file_contents.toBase64();
122  }
123 }

Here is the caller graph for this function:

void IOFactory::playWaveFile ( const QString &  file_path)
static

Plays selected file with default audio output.

Parameters
file_pathPath to file to play, it should be WAVE file.

Definition at line 52 of file iofactory.cpp.

52  {
53 #if QT_VERSION >= 0x050000
54  QSound::play(file_path);
55 #elif !defined(Q_OS_OS2)
56  Phonon::AudioOutput *out = new Phonon::AudioOutput(Phonon::MusicCategory, qApp);
57  out->setVolume(100.0f);
58  out->setMuted(false);
59 
60  Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory, Phonon::MediaSource(file_path));
61 
62  Phonon::createPath(music, out);
63  music->play();
64  QObject::connect(music, SIGNAL(finished()), music, SLOT(deleteLater()));
65  QObject::connect(music, SIGNAL(finished()), out, SLOT(deleteLater()));
66 #endif
67 }
bool IOFactory::removeDirectory ( const QString &  directory_name,
const QStringList &  exception_file_list = QStringList(),
const QStringList &  exception_folder_list = QStringList() 
)
static

Removes given directory and all its contents if not exceptions are given.

Parameters
directory_namePath to directory.
exception_file_listList of file names which should be skipped.
exception_folder_listList of folder names which should be skipped.
Returns
Returns true if delete operation was successfull.

Definition at line 85 of file iofactory.cpp.

87  {
88  bool result = true;
89  QDir dir(directory_name);
90 
91  if (dir.exists(directory_name)) {
92  foreach (QFileInfo info,
93  dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System |
94  QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
95  if (info.isDir()) {
96  if (!exception_folder_list.contains(info.fileName())) {
97  result &= removeDirectory(info.absoluteFilePath(), exception_file_list);
98  }
99  }
100  else if (!exception_file_list.contains(info.fileName())) {
101  result &= QFile::remove(info.absoluteFilePath());
102  }
103  }
104 
105  result &= dir.rmdir(directory_name);
106  }
107 
108  return result;
109 }
static bool removeDirectory(const QString &directory_name, const QStringList &exception_file_list=QStringList(), const QStringList &exception_folder_list=QStringList())
Removes given directory and all its contents if not exceptions are given.
Definition: iofactory.cpp:85

Here is the caller graph for this function:


The documentation for this class was generated from the following files: