Permalink
Browse files

Added local file support.

  • Loading branch information...
1 parent 78afec7 commit 6a230cceb0a19f85fe410ea0e67eec9e0e0b1f2e @ssharunas committed Jan 5, 2013
@@ -11,43 +11,46 @@ TEMPLATE = app
SOURCES += main.cpp\
- mainwindow.cpp \
+ mainwindow.cpp \
ui/jshighlighter.cpp \
- qadvancedinputpanel.cpp \
+ qadvancedinputpanel.cpp \
presentation/fileloadsave.cpp \
- presentation/queryexecution.cpp \
- model/qtabdocument.cpp \
- qadvancedtabbar.cpp \
- model/qfilesystemaccess.cpp \
- model/filesystem/filesystemfactory.cpp \
- model/filesystem/permission.cpp \
- model/filesystem/local/localfolderobject.cpp \
- model/filesystem/filesystemobject.cpp \
- model/filesystem/local/localfilesystemhandler.cpp \
- model/irepleisiukasscriptobject.cpp
+ presentation/queryexecution.cpp \
+ model/qtabdocument.cpp \
+ qadvancedtabbar.cpp \
+ model/qfilesystemaccess.cpp \
+ model/filesystem/filesystemfactory.cpp \
+ model/filesystem/permission.cpp \
+ model/filesystem/local/localfolderobject.cpp \
+ model/filesystem/filesystemobject.cpp \
+ model/filesystem/local/localfilesystemhandler.cpp \
+ model/irepleisiukasscriptobject.cpp \
+ model/filesystem/local/localfileobject.cpp \
+ model/filesystem/openmodes.cpp
HEADERS += mainwindow.h \
ui/jshighlighter.h \
- qadvancedinputpanel.h \
+ qadvancedinputpanel.h \
presentation/fileloadsave.h \
- presentation/queryexecution.h \
- model/qtabdocument.h \
- qadvancedtabbar.h \
- model/qfilesystemaccess.h \
- model/filesystem/filesystemfactory.h \
- model/filesystem/ifilesystemhandler.h \
- model/filesystem/ifilesystemobject.h \
- model/filesystem/whence.h \
- model/filesystem/permission.h \
- model/filesystem/local/localfolderobject.h \
- model/filesystem/filesystemobject.h \
- model/filesystem/local/localfilesystemhandler.h \
- model/filesystem/openmodes.h \
- model/irepleisiukasscriptobject.h \
- model/scriptvalueconverttemplate.h
+ presentation/queryexecution.h \
+ model/qtabdocument.h \
+ qadvancedtabbar.h \
+ model/qfilesystemaccess.h \
+ model/filesystem/filesystemfactory.h \
+ model/filesystem/ifilesystemhandler.h \
+ model/filesystem/ifilesystemobject.h \
+ model/filesystem/whence.h \
+ model/filesystem/permission.h \
+ model/filesystem/local/localfolderobject.h \
+ model/filesystem/filesystemobject.h \
+ model/filesystem/local/localfilesystemhandler.h \
+ model/filesystem/openmodes.h \
+ model/irepleisiukasscriptobject.h \
+ model/scriptvalueconverttemplate.h \
+ model/filesystem/local/localfileobject.h
FORMS += mainwindow.ui \
- qadvancedinputpanel.ui
+ qadvancedinputpanel.ui
OTHER_FILES += \
other/extensions.js
@@ -1,6 +1,5 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
-#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
@@ -58,6 +57,9 @@ MainWindow::MainWindow(QWidget *parent) :
shortcut = new QShortcut(QKeySequence(tr("Ctrl+Enter")), query);
connect(shortcut, SIGNAL(activated()), ui->actionExecute, SLOT(trigger()));
+ shortcut = new QShortcut(QKeySequence(tr("Ctrl+Shift+z")), query);
+ connect(shortcut, SIGNAL(activated()), query, SLOT(redo()));
+
LoadSession();
connect(query, SIGNAL(textChanged()), this, SLOT(onQueryChanged()));
@@ -127,7 +129,6 @@ void MainWindow::on_openFile_clicked()
if(senderAction)
{
QString file = senderAction->data().toString();
- qDebug() << "trying to open file" << file;
LoadQueryToGUI(fileOperations->LoadFromFile(file));
if(tabDocument != 0)
@@ -156,7 +157,6 @@ void MainWindow::LoadQueryToGUI(QString query)
void MainWindow::on_pushButton_Go_clicked()
{
- qDebug() << contructionInProgress;
if(!contructionInProgress)
{
fileOperations->SetLastQuery(query->text());
@@ -0,0 +1,250 @@
+#include "localfileobject.h"
+#include <QtScript/QScriptEngine>
+#include <QDebug>
+#include <QFileInfo>
+#include <QDir>
+
+LocalFileObject::LocalFileObject(QUrl url, QString mode, QScriptEngine *engine) :
+ FileSystemObject(engine), _file(url.path())
+{
+ if(is_valid_open_mode(mode)){
+ _mode = mode;
+ }else{
+ _mode = MODE_READ;
+ }
+
+ _url = url;
+}
+
+LocalFileObject::~LocalFileObject() { }
+
+
+QString LocalFileObject::getName()
+{
+ QFileInfo fileInfo(_file.fileName());
+ return fileInfo.fileName();
+}
+
+void LocalFileObject::setName(QString name)
+{
+ if(_mode == MODE_WRITE || _mode == MODE_READ_WRITE)
+ {
+ QFileInfo fileInfo(_file.fileName());
+ QString newName = QDir::cleanPath(fileInfo.absolutePath() + "/" + name);
+
+ if(!QFile::exists(newName)){
+ if(!_file.rename(newName)){
+ context()->throwError("Rename failed.");
+ }else{
+ _url.setPath(newName);
+ }
+ }else{
+ context()->throwError(QString("File or folder with name '%1' already exists.").arg(newName));
+ }
+ }else{
+ context()->throwError("Could not rename. File must be opened in write mode ('w')");
+ }
+}
+
+void LocalFileObject::setPermissions(Permission *permissions)
+{
+ Q_UNUSED(permissions)
+}
+
+Permission *LocalFileObject::getPermissions()
+{
+ Permission* permission = 0;
+ QFileInfo fileInfo(_url.path());
+ if(fileInfo.exists()){
+ permission = new Permission(_engine);
+
+ permission->uR = fileInfo.permission(QFile::ReadOwner);
+ permission->gR = fileInfo.permission(QFile::ReadGroup);
+ permission->oR = fileInfo.permission(QFile::ReadOther);
+
+ permission->uW = fileInfo.permission(QFile::WriteOwner);
+ permission->gW = fileInfo.permission(QFile::WriteGroup);
+ permission->oW = fileInfo.permission(QFile::WriteOther);
+
+ permission->uX = fileInfo.permission(QFile::ExeOwner);
+ permission->gX = fileInfo.permission(QFile::ExeGroup);
+ permission->oX = fileInfo.permission(QFile::ExeOther);
+
+ permission->user = fileInfo.owner();
+ permission->uid = fileInfo.ownerId();
+ permission->group = fileInfo.group();
+ permission->gid = fileInfo.groupId();
+ }
+
+
+ return permission;
+}
+
+QString LocalFileObject::getPath()
+{
+ QFileInfo fileInfo(_file.fileName());
+ return fileInfo.absolutePath();
+}
+
+QString LocalFileObject::getContent()
+{
+ if(_mode == MODE_READ || _mode == MODE_READ_WRITE || _mode == MODE_APPEND){
+ if(!tryOpenFile(false, true)){
+ return QString();
+ }
+
+ return _file.readAll();
+
+ }else{
+ context()->throwError("Could get content. File must be opened in read ('r') or append ('a') mode.");
+ }
+
+ return QString();
+}
+
+void LocalFileObject::setContent(QString content)
+{
+ doWrite(content, true);
+}
+
+qint64 LocalFileObject::getPos()
+{
+ if(tryOpenFile(true, true))
+ return _file.pos();
+
+ return -1;
+}
+
+void LocalFileObject::setPos(qint64 pos)
+{
+ if(tryOpenFile(true, true))
+ _file.seek(pos);
+}
+
+bool LocalFileObject::getIsEnd()
+{
+ if(tryOpenFile(true, true))
+ return _file.atEnd();
+
+ return true;
+}
+
+QString LocalFileObject::toString()
+{
+ QString permissionsStr;
+ Permission* permissions = getPermissions();
+ if(permissions){
+ permissionsStr = permissions->toString() + " ";
+ permissions->deleteLater();
+ }
+
+ return QString("{F%2%1}").arg(_file.fileName()).arg(permissionsStr);
+}
+
+bool LocalFileObject::tryOpenFile(bool writing, bool reading)
+{
+ bool result = true;
+
+ if(!_file.isOpen()){
+ if(!writing && !reading) {
+ result = false;
+ }
+ else {
+ QIODevice::OpenMode mode;
+
+ if(writing && _mode == MODE_WRITE) {
+ mode = QFile::WriteOnly;
+ }
+ else if(writing && _mode == MODE_APPEND) {
+ mode = QFile::Append;
+ }
+ else if(reading && _mode == MODE_READ) {
+ mode = QFile::Append;
+ }
+ else if((reading || writing) && _mode == MODE_READ_WRITE) {
+ mode = QFile::ReadWrite;
+ }
+ else {
+ result = false;
+ }
+
+ if(result)
+ result = _file.open(mode);
+ }
+ }
+
+ if(!result)
+ context()->throwError(QString("Failed to open file in mode '%1'.").arg(_mode));
+
+ return true;
+}
+
+qint64 LocalFileObject::doWrite(QString content, bool truncate)
+{
+ qint64 result = -1;
+
+ if(_mode == MODE_WRITE || _mode == MODE_APPEND || _mode == MODE_READ_WRITE){
+ if(!tryOpenFile(true, false)){
+ return -1;
+ }
+
+ if(truncate)
+ _file.seek(0);
+
+ result = _file.write(content.toUtf8());
+
+ if(truncate)
+ _file.resize(_file.pos());
+
+ }else{
+ context()->throwError("Could set content. File must be opened in write ('w') or append ('a') mode.");
+ }
+
+ return result;
+}
+
+
+qint64 LocalFileObject::write(QString content)
+{
+ return doWrite(content, false);
+}
+
+QString LocalFileObject::read(qint64 length)
+{
+ QString result;
+
+ if(tryOpenFile(false, true)){
+ char buf[length];
+
+ qint64 dataLength = _file.read(buf, sizeof(buf));
+
+ if(dataLength > 0)
+ result = QByteArray(buf, dataLength);
+ }
+
+ return result;
+}
+
+QString LocalFileObject::readLine()
+{
+ QString result;
+
+ if(tryOpenFile(false, true)){
+ const int BUFFER_SIZE = 1024;
+ char buf[BUFFER_SIZE + 1];
+
+ qint64 lineLength = BUFFER_SIZE;
+ int infinityPrevent = 1024;
+
+ while(lineLength == BUFFER_SIZE && infinityPrevent > 0){
+ infinityPrevent--;
+
+ lineLength = _file.readLine(buf, sizeof(buf));
+
+ if(lineLength > 0)
+ result += QByteArray(buf, lineLength);
+ }
+ }
+
+ return result;
+}
Oops, something went wrong.

0 comments on commit 6a230cc

Please sign in to comment.