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

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
IncludeManager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2010-2011 Wagner Reck <wagner.reck@gmail.com>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "IncludeManager.h"
20 #include <QDebug>
21 #include <QFile>
22 #include <KLocalizedString>
23 #include <QDir>
24 #include <KDebug>
25 #include <KGlobal>
26 #include <kstandarddirs.h>
27 
28 IncludeManager::IncludeManager()
29 {
30  addPath(KGlobal::dirs()->findDirs("appdata", "examples"));
31 
32 // QStringList list = Settings::includePath();
33 //
34 // while(!list.isEmpty()){
35 // addPath(list.last());
36 // list.removeLast();
37 // }
38 // kDebug() << _tempPath;
39 
40 }
41 void IncludeManager::initialize(const QStringList& tempPath)
42 {
43  _tempPath.clear();
44  _wasIncluded.clear();
45 
46  addPath(KGlobal::dirs()->findDirs("appdata", "examples"));
47  addPath(tempPath);
48 }
49 
50 
51 QString IncludeManager::include(const QString& script, const QString& actualPath, const QString &filename)
52 {
53  int pos;
54  bool inComment = false;
55  if (!actualPath.isEmpty()) {
56  _actualDir = QDir(actualPath); // try the path of saved file
57  _tempPath << _actualDir;
58  if (!seekFile(filename).isNull()) {
59  _wasIncluded << seekFile(filename);
60  }
61  } else {
62  if (!_tempPath.isEmpty()) {
63  _actualDir = _tempPath.last(); // not disponible the path to saved file, use the first path from list
64  } else {
65  _actualDir = QDir(); //No path in list? use application path!
66  }
67  }
68  QStringList lines = script.split('\n');
69  for (int i = 0; i < lines.count(); ++i) {
70 
71  if ((pos = lines[i].indexOf("/*")) != -1) { // entering multi line comment
72  inComment = true;
73  }
74  if (inComment && (pos = lines[i].indexOf("*/")) != -1) { // leaving multi line comment
75  inComment = false;
76  }
77 
78  QRegExp reg("^\\s*include\\s*\\(\\s*.*.js\\s*\\)");
79  if (!inComment && lines[i].indexOf(reg) != -1) {
80  QString ret = processInclude(reg.cap());
81  lines[i].replace(reg.cap(), ret);
82  }
83 
84  }
85  QString str = lines.join("\n");
86  return str;
87 }
88 
89 QString IncludeManager::processInclude(QString arg1)
90 {
91  QString fileContent;
92  QString file;
93  int pos;
94  QString path;
95  if (arg1.indexOf('(') != -1) {
96  file = arg1.replace(')', '(').section('(', 1, 1).trimmed();
97  // To avoid more ifs-elses
98  fileContent = QString("debug(\"%1\")").arg(i18n("Cannot open file %1.", file));
99  // Add the path first
100  if ((pos = file.lastIndexOf('/')) != -1) { //add the path of file to list
101  path = file.left(pos + 1);
102 // QString filename = file.right(file.length() - pos +1);
103  if (!path.startsWith(QDir::rootPath())) {
104  path.prepend(_actualDir.absolutePath() + '/');
105  }
106  _tempPath << QDir(path);
107  }
108 // then, try to open
109  if (!_actualDir.exists(file)) {
110  file = seekFile(file);
111  } else
112  file = _actualDir.absoluteFilePath(file);
113 
114 
115  if (!file.isEmpty()) {
116  if (!checkIfWasIncluded(file)) {
117  _wasIncluded << file;
118  QFile fp(file);
119  if (fp.open(QFile::ReadOnly | QFile::Text)) {
120  fileContent = fp.readAll();
121  fileContent = include(fileContent);
122  }
123  } else {
124  return QString();
125  }
126  }
127  } else {
128  fileContent = QString("debug(\"%1\")").arg(i18n("Invalid include directive: %1. Cannot find file in directive.", arg1));
129  }
130 
131  return fileContent;
132 }
133 
134 
135 QString IncludeManager::seekFile(const QString & arg1)
136 {
137 
138  if (arg1.isEmpty()) {
139  return QString();
140  }
141  if (arg1.indexOf('/') != -1) {
142  QDir dir(arg1.section('/', 0, -2));
143  if (dir.isAbsolute() && dir.exists(arg1))
144  return arg1;
145  }
146 
147  for (int count = _tempPath.count() - 1; count >= 0; -- count) {
148  if (_tempPath.at(count).exists(arg1.trimmed())) {
149  return _tempPath.at(count).absoluteFilePath(arg1.trimmed());
150  }
151  }
152  return QString();
153 }
154 
155 void IncludeManager::addPath(const QString& str)
156 {
157  QString tmp = !str.endsWith('/') ? str + '/' : str;
158 
159  if (!tempPath().contains(tmp)) {
160  _tempPath << QDir(tmp);
161  }
162 }
163 
164 
165 void IncludeManager::addPath(const QStringList& str)
166 {
167  foreach(const QString & s, str) {
168 
169  QDir dir(s);
170  if (!_tempPath.contains(dir)) {
171  _tempPath.append(dir);
172  }
173  }
174 }
175 
176 QStringList const IncludeManager::tempPath() const
177 {
178  QStringList list;
179  foreach(const QDir & dir, _tempPath) {
180  list << dir.path() + '/';
181  }
182  return list;
183 }
IncludeManager::tempPath
QStringList const tempPath() const
Definition: IncludeManager.cpp:176
IncludeManager::IncludeManager
IncludeManager()
Definition: IncludeManager.cpp:28
IncludeManager::include
QString include(const QString &script, const QString &actualPath=QString(), const QString &filename=QString())
Definition: IncludeManager.cpp:51
IncludeManager.h
IncludeManager::initialize
void initialize(const QStringList &tempPath=QStringList())
Definition: IncludeManager.cpp:41
IncludeManager::addPath
void addPath(const QString &str)
insert additional path to seek files.
Definition: IncludeManager.cpp:155
IncludeManager::seekFile
QString seekFile(const QString &arg1)
Try find the fileName in the paths' list.
Definition: IncludeManager.cpp:135
IncludeManager::checkIfWasIncluded
bool checkIfWasIncluded(const QString &file)
check if the file was included before.
Definition: IncludeManager.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • 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