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.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "miscellaneous/iofactory.h"
32 
33 #include "miscellaneous/application.h"
34 
35 #include <QDir>
36 #include <QFile>
37 #include <QTextStream>
38 
39 #if !defined(Q_OS_OS2)
40 #if QT_VERSION >= 0x050000
41 #include <QSound>
42 #else
43 #include <MediaObject>
44 #include <AudioOutput>
45 #endif
46 #endif
47 
48 
49 IOFactory::IOFactory() {
50 }
51 
52 void IOFactory::playWaveFile(const QString &file_path) {
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 }
68 
69 bool IOFactory::copyFile(const QString &source, const QString &destination) {
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 }
84 
85 bool IOFactory::removeDirectory(const QString& directory_name,
86  const QStringList& exception_file_list,
87  const QStringList& exception_folder_list) {
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 }
110 
111 QByteArray IOFactory::fileToBase64(const QString &file_name) {
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 }
124 
125 bool IOFactory::base64ToFile(const QString &source_data, const QString &target_file) {
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 }
136 
137 bool IOFactory::copyDirectory(QString source, QString destination) {
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 base64ToFile(const QString &source_data, const QString &target_file)
Takes base64 byte array and saves it into file.
Definition: iofactory.cpp:125
static bool copyFile(const QString &source, const QString &destination)
Copies source file into destination path.
Definition: iofactory.cpp:69
static QByteArray fileToBase64(const QString &file_name)
Takes input file and reads it into base64 byte array.
Definition: iofactory.cpp:111
static void playWaveFile(const QString &file_path)
Plays selected file with default audio output.
Definition: iofactory.cpp:52
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
static bool copyDirectory(QString source, QString destination)
Copies source directory into destination path.
Definition: iofactory.cpp:137