Commit 239812a9 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make it possible to load scripts from kalgebramobile

parent 865a57e3
......@@ -14,9 +14,7 @@ KAlgebraPage
// variables: app.variables
mode: ConsoleModel.Evaluate
onErrorMessage: {
var toadd = i18n("Error: %1", error)
itemModel.insert(0, { result: toadd })
itemModel.insert(0, { result: error })
input.selectAll()
view.currentIndex = 0
}
......@@ -36,7 +34,7 @@ KAlgebraPage
}
function proceedLoadScript() {
consoleModel.loadScript(fileDialog.fileUrl)
}
contextualActions: [
......@@ -44,6 +42,7 @@ KAlgebraPage
text: i18n("Load Script...")
onTriggered: {
fileDialog.title = text
fileDialog.proceed = page.proceedLoadScript
var v = fileDialog.open()
console.log("opened...", v)
}
......
......@@ -63,20 +63,19 @@ Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, s_css, (
"\tp { font-size: " +QByteArray::number(QFontMetrics(QApplication::font()).height())+ "px; }\n"
"</style>\n"))
static QString temporaryPath()
static QUrl temporaryPath()
{
QTemporaryFile temp(QStringLiteral("consolelog"));
temp.open();
temp.close();
temp.setAutoRemove(false);
return temp.fileName();
return QUrl::fromLocalFile(temp.fileName());
}
static QString retrieve(const QUrl& remoteUrl)
static QUrl retrieve(const QUrl& remoteUrl)
{
QString path = temporaryPath();
KIO::CopyJob* job=KIO::copyAs(remoteUrl, QUrl::fromLocalFile(path));
const QUrl path = temporaryPath();
KIO::CopyJob* job=KIO::copyAs(remoteUrl, path);
job->exec();
......@@ -153,15 +152,15 @@ Analitza::Analyzer* ConsoleHtml::analitza()
bool ConsoleHtml::loadScript(const QUrl& path)
{
return m_model->loadScript(path.isLocalFile() ? path.toLocalFile() : retrieve(path));
return m_model->loadScript(path.isLocalFile() ? path : retrieve(path));
}
bool ConsoleHtml::saveScript(const QUrl & path) const
{
const QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
const QUrl savePath=path.isLocalFile() ? path : temporaryPath();
bool correct = m_model->saveScript(savePath);
if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl(savePath), path);
KIO::CopyJob* job=KIO::move(savePath, path);
correct=job->exec();
}
return correct;
......@@ -171,8 +170,8 @@ bool ConsoleHtml::saveLog(const QUrl& path) const
{
Q_ASSERT(!path.isEmpty());
//FIXME: We have to choose between txt and html
QString savePath=path.isLocalFile() ? path.toLocalFile() : temporaryPath();
QFile file(savePath);
const QUrl savePath=path.isLocalFile() ? path : temporaryPath();
QFile file(savePath.toLocalFile());
bool correct = file.open(QIODevice::WriteOnly | QIODevice::Text);
if(correct) {
......@@ -185,7 +184,7 @@ bool ConsoleHtml::saveLog(const QUrl& path) const
}
if(!path.isLocalFile()) {
KIO::CopyJob* job=KIO::move(QUrl::fromLocalFile(savePath), path);
KIO::CopyJob* job=KIO::move(savePath, path);
correct=job->exec();
}
return correct;
......
......@@ -63,13 +63,13 @@ bool ConsoleModel::addOperation(const Analitza::Expression& e, const QString& in
return a.isCorrect();
}
bool ConsoleModel::loadScript(const QString& path)
bool ConsoleModel::loadScript(const QUrl& path)
{
Q_ASSERT(!path.isEmpty());
Q_ASSERT(!path.isEmpty() && path.isLocalFile());
//FIXME: We have expression-only script support
bool correct=false;
QFile file(path);
QFile file(path.toLocalFile());
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
......@@ -79,20 +79,20 @@ bool ConsoleModel::loadScript(const QString& path)
}
if(!correct) {
Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path, a.errors().join(QStringLiteral("<br/>"))));
Q_EMIT errorMessage(i18n("<ul class='error'>Error: Could not load %1. <br /> %2</ul>", path.toDisplayString(), a.errors().join(QStringLiteral("<br/>"))));
Q_EMIT updateView(QString());
}
else
Q_EMIT updateView(i18n("Imported: %1", path));
Q_EMIT updateView(i18n("Imported: %1", path.toDisplayString()));
return correct;
}
bool ConsoleModel::saveScript(const QString& savePath)
bool ConsoleModel::saveScript(const QUrl& savePath)
{
Q_ASSERT(!savePath.isEmpty());
QFile file(savePath);
QFile file(savePath.toLocalFile());
bool correct=file.open(QIODevice::WriteOnly | QIODevice::Text);
if(correct) {
......
......@@ -41,8 +41,8 @@ public:
Q_SCRIPTABLE bool addOperation(const QString& input);
bool addOperation(const Analitza::Expression& e, const QString& input);
Q_SCRIPTABLE bool loadScript(const QString &path);
Q_SCRIPTABLE bool saveScript(const QString &path);
Q_SCRIPTABLE bool loadScript(const QUrl &path);
Q_SCRIPTABLE bool saveScript(const QUrl &path);
Q_SCRIPTABLE void clear() { m_script.clear(); }
ConsoleMode mode() const { return m_mode; }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment