• Skip to content
  • Skip to link menu
KDE API Documentation - File.cpp Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
File.cpp
1 /* GCompris - File.cpp
2  *
3  * Copyright (C) 2014 Holger Kaelberer <holger.k@elberer.de>
4  *
5  * Authors:
6  * Holger Kaelberer <holger.k@elberer.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "File.h"
23 
24 #include <QFile>
25 #include <QString>
26 #include <QTextStream>
27 #include <QQmlComponent>
28 
29 File::File(QObject *parent) : QObject(parent)
30 {
31 }
32 
33 QString File::name() const
34 {
35  return m_name;
36 }
37 
38 QString File::sanitizeUrl(const QString &str)
39 {
40  QString target(str);
41 
42  // make sure we strip off invalid URL schemes:
43  if (target.startsWith(QLatin1String("file://")))
44  target.remove(0, 7);
45  else if (target.startsWith(QLatin1String("qrc:/")))
46  target.remove(0, 3);
47 
48  return target;
49 }
50 
51 void File::setName(const QString &str)
52 {
53  QString target = sanitizeUrl(str);
54 
55  if (target != m_name) {
56  m_name = target;
57  emit nameChanged();
58  }
59 }
60 
61 QString File::read(const QString& name)
62 {
63  if (!name.isEmpty())
64  setName(name);
65 
66  if (m_name.isEmpty()){
67  emit error("source is empty");
68  return QString();
69  }
70 
71  QFile file(m_name);
72  QString fileContent;
73  if (file.open(QIODevice::ReadOnly) ) {
74  QString line;
75  QTextStream t(&file);
76  /* Force utf-8 : for some languages, it seems to be loaded in other
77  encoding even if the file is in utf-8 */
78  t.setCodec("UTF-8");
79 
80  do {
81  line = t.readLine();
82  fileContent += line;
83  } while (!line.isNull());
84 
85  file.close();
86  } else {
87  emit error("Unable to open the file");
88  return QString();
89  }
90  return fileContent;
91 }
92 
93 bool File::write(const QString& data, const QString& name)
94 {
95  if (!name.isEmpty())
96  setName(name);
97 
98  if (m_name.isEmpty()) {
99  emit error("source is empty");
100  return false;
101  }
102 
103  QFile file(m_name);
104  if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
105  emit error("could not open file " + m_name);
106  return false;
107  }
108 
109  QTextStream out(&file);
110  out << data;
111 
112  file.close();
113 
114  return true;
115 }
116 
117 void File::init()
118 {
119  qmlRegisterType<File>("GCompris", 1, 0, "File");
120 }
121 
122 bool File::exists(const QString& path)
123 {
124  return QFile::exists(sanitizeUrl(path));
125 }
File::name
QString name
Filename.
Definition: File.h:44
File::error
void error(const QString &msg)
Emitted when an error occurs.
File::read
Q_INVOKABLE QString read(const QString &name=QString())
Reads contents of a file.
Definition: File.cpp:61
File::exists
static Q_INVOKABLE bool exists(const QString &path)
Checks whether file path exists.
Definition: File.cpp:122
File::write
Q_INVOKABLE bool write(const QString &data, const QString &name=QString())
Writes data to a file.
Definition: File.cpp:93
File::nameChanged
void nameChanged()
Emitted when the name changes.
File::File
File(QObject *parent=0)
Constructor.
Definition: File.cpp:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

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