Skip to content

Commit

Permalink
[filemanager] Add engine for file manager. Contributes to JB#5771
Browse files Browse the repository at this point in the history
  • Loading branch information
Joona Petrell committed Mar 11, 2016
0 parents commit ae3b67c
Show file tree
Hide file tree
Showing 19 changed files with 2,341 additions and 0 deletions.
2 changes: 2 additions & 0 deletions filemanager.pro
@@ -0,0 +1,2 @@
TEMPLATE = subdirs
SUBDIRS = src
34 changes: 34 additions & 0 deletions rpm/nemo-qml-plugin-filemanager.spec
@@ -0,0 +1,34 @@
Name: nemo-qml-plugin-filemanager
Summary: File manager plugin for Nemo Mobile
Version: 0.0.0
Release: 1
Group: System/Libraries
License: BSD
URL: https://git.merproject.org/mer-core/nemo-qml-plugin-filemanager
Source0: %{name}-%{version}.tar.bz2
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5Gui)
BuildRequires: pkgconfig(Qt5Qml)
BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(Qt5Test)

%description
%{summary}.

%prep
%setup -q -n %{name}-%{version}

%build

%qmake5

make %{?_smp_mflags}

%install
rm -rf %{buildroot}
%qmake5_install

%files
%defattr(-,root,root,-)
%{_libdir}/qt5/qml/Nemo/FileManager/libnemofilemanager.so
%{_libdir}/qt5/qml/Nemo/FileManager/qmldir
114 changes: 114 additions & 0 deletions src/consolemodel.cpp
@@ -0,0 +1,114 @@
#include "consolemodel.h"
#include "globals.h"

enum {
ModelDataRole = Qt::UserRole + 1
};

ConsoleModel::ConsoleModel(QObject *parent) :
QAbstractListModel(parent), m_process(0)
{
}

ConsoleModel::~ConsoleModel()
{
}

int ConsoleModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_lines.count();
}

QVariant ConsoleModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(role);
if (!index.isValid() || index.row() > m_lines.count()-1)
return QVariant();

QString line = m_lines.at(index.row());
return line;
}

QHash<int, QByteArray> ConsoleModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
roles.insert(ModelDataRole, QByteArray("modelData"));
return roles;
}

void ConsoleModel::setLines(QStringList lines)
{
if (m_lines == lines)
return;

beginResetModel();
m_lines = lines;
endResetModel();

emit linesChanged();
}

void ConsoleModel::setLines(QString lines)
{
beginResetModel();
m_lines = lines.split(QRegExp("[\n\r]"));
endResetModel();
emit linesChanged();
}

void ConsoleModel::appendLine(QString line)
{
beginInsertRows(QModelIndex(), m_lines.count(), m_lines.count());
m_lines.append(line);
endInsertRows();
}

bool ConsoleModel::executeCommand(QString command, QStringList arguments)
{
// don't execute the command if an old command is still running
if (m_process && m_process->state() != QProcess::NotRunning) {
// if the old process doesn't stop in 1/2 secs, then don't run the new command
if (!m_process->waitForFinished(500))
return false;
}
setLines(QStringList());
m_process = new QProcess(this);
m_process->setReadChannel(QProcess::StandardOutput);
m_process->setProcessChannelMode(QProcess::MergedChannels); // merged stderr channel with stdout channel
connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessChannels()));
connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinish(int, QProcess::ExitStatus)));
connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleProcessError(QProcess::ProcessError)));
m_process->start(command, arguments);
// the process is killed when ConsoleModel is destroyed (usually when Page is closed)
// should we run the process in bg thread to allow the command to finish(?)

return true;
}

void ConsoleModel::readProcessChannels()
{
while (m_process->canReadLine()) {
QString line = m_process->readLine();
appendLine(line);
}
}

void ConsoleModel::handleProcessFinish(int exitCode, QProcess::ExitStatus status)
{
if (status == QProcess::CrashExit) { // if it crashed, then use some error exit code
exitCode = -99999;
appendLine(tr("** crashed"));

} else if (exitCode != 0) {
appendLine(tr("** error: %1").arg(exitCode));
}
emit processExited(exitCode);
}

void ConsoleModel::handleProcessError(QProcess::ProcessError error)
{
Q_UNUSED(error);
emit processExited(-88888); // if error, then use some error exit code
appendLine(tr("** error"));
}
48 changes: 48 additions & 0 deletions src/consolemodel.h
@@ -0,0 +1,48 @@
#ifndef CONSOLEMODEL_H
#define CONSOLEMODEL_H

#include <QAbstractListModel>
#include <QStringList>
#include <QProcess>

/**
* @brief The ConsoleModel class holds a list of strings for a QML list model.
*/
class ConsoleModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QStringList lines READ lines() WRITE setLines(QString) NOTIFY linesChanged())

public:
explicit ConsoleModel(QObject *parent = 0);
~ConsoleModel();

// methods needed by ListView
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;

// property accessors
QStringList lines() const { return m_lines; }
void setLines(QStringList lines);
void setLines(QString lines);

void appendLine(QString line);

Q_INVOKABLE bool executeCommand(QString command, QStringList arguments);

signals:
void linesChanged();
void processExited(int exitCode);

private slots:
void readProcessChannels();
void handleProcessFinish(int exitCode, QProcess::ExitStatus status);
void handleProcessError(QProcess::ProcessError error);

private:
QProcess *m_process;
QStringList m_lines;
};

#endif // CONSOLEMODEL_H

0 comments on commit ae3b67c

Please sign in to comment.