MainWindow::MainWindow(const char* workspace, const char* project,
ReLogger* logger, QWidget *parent) :
QMainWindow(parent),
- ui(new Ui::MainWindow),
- m_project(NULL),
m_workspace(NULL),
m_logger(logger),
- m_fileTree(NULL),
- m_dockProjectTree(NULL),
m_perspectives(this) {
+ setLayout(new QVBoxLayout);
if (workspace == NULL)
workspace = QDir::homePath().toUtf8();
changeWorkspace(workspace == NULL ? QDir::homePath() : workspace);
if (dir.isDir() && dir.isWritable())
proj = lastProjects.at(ix);
}
- if (!proj.isEmpty())
- changeProject(proj);
+ if (proj.isEmpty())
+ proj = QDir::homePath();
+
}
+ changeProject(proj);
- //ui->setupUi(this);
- //ReEdit* edit = ui->widget;
- //edit->setLines(m_file);
- //edit->setCursorLine(0);
//connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));
Perspective* mainPerspective = new StartPerspective(this);
m_perspectives.addPerspective(mainPerspective);
- m_perspectives.changePerspective(mainPerspective->name());
- //open();
+ m_perspectives.change(mainPerspective->name());
+ m_perspectives.addPerspective(new ProjectPerspective(proj, this));
}
+/**
+ * Destructor.
+ */
MainWindow::~MainWindow() {
- delete ui;
}
/**
* @param path the directory containing the project data
*/
void MainWindow::changeProject(QString path) {
- delete m_project;
- if (path.endsWith(OS_SEPARATOR_STR))
- path.remove(path.size() - 1, 1);
- m_project = new Project(path, this);
- int maxEntries = m_workspace->intValue("history.max_projects");
- m_workspace->addHistoryEntry(Workspace::KEY_HISTORY_PROJECTS, path, ';',
- maxEntries);
- if (m_fileTree == NULL) {
- m_fileTree = new ReFileTree(path, m_logger, this);
- m_dockProjectTree = new QDockWidget("", this);
- m_dockProjectTree->setWidget(m_fileTree);
- addDockWidget(Qt::LeftDockWidgetArea, m_dockProjectTree);
- }
- m_fileTree->setPath(path);
- m_dockProjectTree->setWindowTitle(
- tr("Project") + " " + ReQStringUtils::nodeOf(path));
-
+ ProjectPerspective* perspective = perspectives().project(true);
+ if (perspective != NULL)
+ perspective->changeProject(path);
}
/**
* Calls the file selection dialog.
*/
void MainWindow::openFile(const QString& name) {
- m_file = new ReFile(name, false);
-
- //ReEdit* edit = ui->widget;
- //edit->setLines(m_file);
- //edit->setCursorLine(0);
- int maxEntries = m_workspace->intValue("history.max_files");
- m_workspace->addHistoryEntry(Workspace::KEY_HISTORY_FILES, name, ';',
- maxEntries);
-}
-
-/**
- * Closes the current project.
- */
-void MainWindow::closeProject() {
- delete m_project;
- m_project = NULL;
+ ProjectPerspective* perspective = perspectives().project(true);
+ if (perspective != NULL)
+ perspective->openFile(name);
}
/**
ProjectSelection dialog(this);
dialog.exec();
}
-ReLogger* MainWindow::logger() const {
- return m_logger;
+/**
+ * Returns the perspective list.
+ *
+ * @return the perspective list
+ */
+PerspectiveList& MainWindow::perspectives() {
+ return m_perspectives;
}
/**
- * Returns the current workspace.
+ * Returns the logger.
*
- * @return the current workspace
+ * @return the logger
*/
-Project* MainWindow::project() const {
- return m_project;
+ReLogger* MainWindow::logger() const {
+ return m_logger;
}
+/**
+ * Returns the workspace.
+ *
+ * @return the workspace
+ */
Workspace* MainWindow::workspace() const {
return m_workspace;
}
#ifndef REBASE_HPP
#include "reide.hpp"
#endif
-namespace Ui {
-class MainWindow;
-}
class MainWindow: public QMainWindow {
Q_OBJECT
~MainWindow();
void changeProject(QString path);
void changeWorkspace(const QString& path);
- void closeProject();
void openFile(const QString& name);
- Project* project() const;
Workspace* workspace() const;
ReLogger* logger() const;
+ PerspectiveList& perspectives();
public slots:
void open();
private:
- Ui::MainWindow *ui;
- ReFile* m_file;
- Project* m_project;
Workspace* m_workspace;
ReLogger* m_logger;
- ReFileTree* m_fileTree;
- QDockWidget* m_dockProjectTree;
PerspectiveList m_perspectives;
};
*/
void ProjectSelection::selectDir() {
QString name = ui->lineEditOpen->text();
- if (name.isEmpty() && m_mainWindow->project() != NULL)
- name = m_mainWindow->project()->path();
+ if (name.isEmpty())
+ name = m_mainWindow->perspectives().project()->path();
name = QFileDialog::getExistingDirectory(this,
tr("Select Project Directory"), name);
if (!name.isEmpty()) {
#ifndef PROJECTSELECTION_HPP
#define PROJECTSELECTION_HPP
-#include "reide.hpp"
#include <QDialog>
#include <QTableWidget>
+#if ! defined REBASE_HPP
+#include "base/rebase.hpp"
+#endif
+#if ! defined REGUI_HPP
+#include "gui/regui.hpp"
+#endif
namespace Ui {
class ProjectSelection;
}
#ifndef REIDE_HPP
#define REIDE_HPP
#include <QDockWidget>
+#include <QVBoxLayout>
+#include <QTableWidget>
+#include <QWidget>
#include "base/rebase.hpp"
#include "gui/regui.hpp"
#include "views/View.hpp"
*/
EditorView::EditorView(MainWindow* mainWindow) :
View("EditorView", mainWindow),
- m_edit(new ReEdit(NULL)) {
+ m_edit(new ReEdit(NULL)),
+ m_file(NULL) {
+ m_edit->setLines(&m_dummyFile);
}
/**
m_edit = NULL;
}
+/**
+ * Opens a file into the editor widget.
+ *
+ * @param filename the filename with path
+ */
+void EditorView::openFile(const QString& filename) {
+ delete m_file;
+ m_file = new ReFile(filename, false, m_mainWindow->logger());
+ m_edit->setLines(m_file);
+}
+
public:
EditorView(MainWindow* mainWindow);
~EditorView();
+public:
+ void openFile(const QString& filename);
+
public:
virtual QWidget* widget() {
return m_edit;
}
- ;
protected:
ReEdit* m_edit;
+ ReFile* m_file;
+ ReLines m_dummyFile;
};
#endif // EDITORVIEW_HPP
#include "reide.hpp"
+const char* FileTreeView::NAME = "FileTree";
/**
* Constructor.
*
* @param mainWindow the parent (main window)
*/
FileTreeView::FileTreeView(const QString& directory, MainWindow* mainWindow) :
- View("FileTreeView", mainWindow),
+ View(NAME, mainWindow),
m_fileTree(new ReFileTree(directory, mainWindow->logger())) {
}
+
+FileTreeView::~FileTreeView() {
+ delete m_fileTree;
+}
* A view is a widget displayed as a dock in the window displaying a perspective.
*/
class FileTreeView: public View {
+public:
+ static const char* NAME;
public:
FileTreeView(const QString& directory, MainWindow* mainWindow);
+ ~FileTreeView();
public:
/** Returns the view specific widget.
* @return the view specific widget
*/
void Perspective::activate() {
// the first view is the central widget, not a dock
- m_mainWindow->setCentralWidget(m_views.at(0)->m_view->widget());
+ QWidget* root = m_views.at(0)->m_view->widget();
+ m_mainWindow->setCentralWidget(root);
for (int ix = 1; ix < m_views.size(); ix++) {
ViewInfo* info = m_views.at(ix);
m_mainWindow->addDockWidget(info->m_position, info->m_dockWidget);
/**
* Deactivates the current perspective and activates another.
*
- * @param perspective name of the new active perspective
+ * @param name name of the new active perspective
*/
-void PerspectiveList::changePerspective(const QByteArray& perspective) {
- if (m_current != NULL)
- m_current->deactivate();
- m_current = m_map.value(perspective);
- if (m_current != NULL)
- m_current->activate();
+Perspective* PerspectiveList::change(const QByteArray& name) {
+ if (m_current == NULL || m_current->name() != name) {
+ if (m_current != NULL)
+ m_current->deactivate();
+ m_current = get(name);
+ if (m_current != NULL)
+ m_current->activate();
+ }
+ return m_current;
+}
+
+/**
+ * Returns the perspective given by name.
+ *
+ * @param name the name of the wanted perspective
+ * @return NULL: not known<br>
+ * otherwise: the wanted perspective
+ */
+Perspective*PerspectiveList::get(const QByteArray& name) {
+ Perspective* rc = m_map.value(name);
+ return rc;
+}
+
+/**
+ * Returns the project perspective.
+ *
+ * @param activate <code>true</code>: the project perspective will be activated
+ * @return the project perspective
+ */
+ProjectPerspective* PerspectiveList::project(bool activate) {
+ ProjectPerspective* rc;
+ if (activate)
+ rc = reinterpret_cast<ProjectPerspective*>(change(
+ ProjectPerspective::NAME));
+ else
+ rc =
+ reinterpret_cast<ProjectPerspective*>(get(ProjectPerspective::NAME));
+ return rc;
}
MainWindow* m_mainWindow;
QList<ViewInfo*> m_views;
};
-
+class ProjectPerspective;
/**
* Manages a collection of <code>Perspective</code>s.
*/
PerspectiveList(MainWindow* mainWindow);
public:
void addPerspective(Perspective* perspective);
- void changePerspective(const QByteArray& perspective);
+ Perspective* change(const QByteArray& name);
+ Perspective* get(const QByteArray& name);
+ ProjectPerspective* project(bool activate = false);
private:
Perspective* m_current;
QMap<QByteArray, Perspective*> m_map;
#include "reide.hpp"
+const char* ProjectPerspective::NAME = "Project";
+
+const char* ProjectPerspective::KEY_HISTORY_OPEN_FILES = "openFiles";
/**
* Constructor.
*
* @param mainWindow the parent (main window)
*/
-ProjectPerspective::ProjectPerspective(MainWindow* mainWindow) :
- Perspective("ProjectPerspective", mainWindow) {
+ProjectPerspective::ProjectPerspective(const QString& path,
+ MainWindow* mainWindow) :
+ Perspective(NAME, mainWindow),
+ ReSettings(path, ".reditor.proj", mainWindow->logger()),
+ m_editorView(NULL) {
setDefaultViews();
+ //QString filename = topOfHistory(KEY_HISTORY_OPEN_FILES);
+ //QFileInfo info(filename);
+ //if (!filename.isEmpty() && info.exists() && !info.isDir()) {
+ // openFile(filename);
+ //}
}
+/**
+ * Change the project.
+ *
+ * @param path the path of the new current project
+ */
+void ProjectPerspective::changeProject(const QString& path) {
+ setPath(path);
+ int maxEntries = m_mainWindow->workspace()->intValue(
+ "history.max_projects");
+ m_mainWindow->workspace()->addHistoryEntry(Workspace::KEY_HISTORY_PROJECTS,
+ m_path, ';', maxEntries);
+ /*<pre>
+ if (m_fileTree == NULL) {
+ m_fileTree = new ReFileTree(path, m_logger, this);
+ m_dockProjectTree = new QDockWidget("", this);
+ m_dockProjectTree->setWidget(m_fileTree);
+ addDockWidget(Qt::LeftDockWidgetArea, m_dockProjectTree);
+ }
+ m_fileTree->setPath(path);
+ m_dockProjectTree->setWindowTitle(
+ tr("Project") + " " + ReQStringUtils::nodeOf(path));
+ </pre>
+ */
+
+}
+
+/**
+ * Opens a file in the project directory.
+ *
+ * @param filename the filename relative to the project directory
+ */
+void ProjectPerspective::openFile(const QString& filename) {
+ QString full;
+ if (ReFileUtils::isAbsolutPath(filename)) {
+ full = filename;
+ } else {
+ full = m_path + OS_SEPARATOR_STR + filename;
+ }
+ m_editorView->openFile(full);
+ addHistoryEntry(KEY_HISTORY_OPEN_FILES, full, ';', 1);
+}
+
+/**
+ * Sets the name of the project directory.
+ *
+ * @param path the directory name
+ */
+void ProjectPerspective::setPath(const QString& path) {
+ if (!path.endsWith(OS_SEPARATOR_STR))
+ ReSettings::setPath(path);
+ else
+ ReSettings::setPath(m_path.mid(0, path.size() - 1));
+}
+
+/**
+ * Sets the default views of the project perspective.
+ */
void ProjectPerspective::setDefaultViews() {
if (m_views.size() == 0) {
- append(new EditorView(m_mainWindow), Qt::NoDockWidgetArea);
+ m_editorView = new EditorView(m_mainWindow);
+ append(m_editorView, Qt::NoDockWidgetArea);
append(new FileTreeView(QDir::homePath(), m_mainWindow),
Qt::LeftDockWidgetArea);
}
}
+
/**
* Manages the aspects of a standard project.
*/
-class ProjectPerspective: public Perspective {
+class ProjectPerspective: public Perspective, public ReSettings {
public:
- ProjectPerspective(MainWindow* mainWindow);
+ static const char* NAME;
+public:
+ static const char* KEY_HISTORY_OPEN_FILES;
+public:
+ ProjectPerspective(const QString& path, MainWindow* mainWindow);
public:
virtual void setDefaultViews();
+public:
+ void openFile(const QString& filename);
+ void changeProject(const QString& path);
+ void setPath(const QString& path);
+
+private:
+ EditorView* m_editorView;
};
#endif // PROJECTPERSPECTIVE_HPP
#include "reide.hpp"
+const char* StartPerspective::NAME = "Start";
+
/**
* Constructor.
*
* @param mainWindow the parent (main window)
*/
StartPerspective::StartPerspective(MainWindow* mainWindow) :
- Perspective("StartPerspective", mainWindow) {
+ Perspective(NAME, mainWindow) {
setDefaultViews();
}
void StartPerspective::setDefaultViews() {
- if (m_views.size() == 0){
+ if (m_views.size() == 0) {
append(new StartView(m_mainWindow), Qt::NoDockWidgetArea);
}
}
* Manages the aspects of a standard project.
*/
class StartPerspective: public Perspective {
+public:
+ static const char* NAME;
public:
StartPerspective(MainWindow* mainWindow);
public:
*/
#include "reide.hpp"
+#include <QFileDialog>
+#include <QMessageBox>
#include "ui_startview.h"
/**
View("StartView", mainWindow),
ui(new Ui::StartView) {
ui->setupUi(this);
+ connect(ui->toolButtonSelectFile, SIGNAL(clicked()), this,
+ SLOT(selectFile()));
+ connect(ui->toolButtonSelectProject, SIGNAL(clicked()), this,
+ SLOT(selectDir()));
+ connect(ui->pushButtonOpen, SIGNAL(clicked()), this, SLOT(open()));
+ connect(ui->lineEditFilterLastFile, SIGNAL(textChanged(QString)), this,
+ SLOT(textChangedFilterFiles(QString)));
+ connect(ui->tableWidgetFiles, SIGNAL(cellEntered(int,int)), this,
+ SLOT(cellEnteredFiles(int, int)));
+ connect(ui->tableWidgetProjects, SIGNAL(cellEntered(int,int)), this,
+ SLOT(cellEnteredProjects(int, int)));
+ Workspace* workspace = mainWindow->workspace();
+ buildTableInfo(workspace, Workspace::KEY_HISTORY_FILES, true, m_files);
+ buildTableInfo(workspace, Workspace::KEY_HISTORY_PROJECTS, false,
+ m_projects);
+ buildTable("", m_files, ui->tableWidgetFiles);
+ buildTable("", m_projects, ui->tableWidgetProjects);
}
/**
delete ui;
}
+/**
+ * Handles the event cellEntered for the last opened files.
+ *
+ * @param row the row of the entered cell
+ * @param col the column of the entered cell
+ */
+void StartView::cellEnteredFiles(int row, int col) {
+ ReUseParameter(col);
+ QString file = fileOfTable(ui->tableWidgetFiles, row);
+ ui->lineEditOpen->setText(file);
+}
+
+/**
+ * Handles the event cellEntered for the last opened projects.
+ *
+ * @param row the row of the entered cell
+ * @param col the column of the entered cell
+ */
+void StartView::cellEnteredProjects(int row, int col) {
+ ReUseParameter(col);
+ QString file = fileOfTable(ui->tableWidgetProjects, row);
+ ui->lineEditOpen->setText(file);
+}
+
+/**
+ * Builds the table from the table using a filter expression.
+ *
+ * @param filter a filter expression with wildcards '*'
+ * @param lines the full table info
+ * @param table OUT: will be filled with all lines matching the filter
+ */
+void StartView::buildTable(const QString& filter, const QStringList& lines,
+ QTableWidget* table) {
+ QStringList::const_iterator it;
+ int rowCount = 0;
+ ReMatcher matcher(filter, Qt::CaseInsensitive, true);
+ for (it = lines.cbegin(); it != lines.cend(); ++it) {
+ if (matcher.matches(*it))
+ rowCount++;
+ }
+ table->setRowCount(rowCount);
+ int row = -1;
+ for (it = lines.cbegin(); it != lines.cend(); ++it) {
+ if (matcher.matches(*it)) {
+ row++;
+ QStringList cols = it->split('\t');
+ for (int col = 0; col < cols.size(); col++) {
+ QTableWidgetItem* item = table->item(row, col);
+ if (item != NULL)
+ item->setText(cols.at(col));
+ else {
+ item = new QTableWidgetItem(cols.at(col));
+ table->setItem(row, col, item);
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Build the info for a table (last opened files or last opened projects).
+ *
+ * Note: the table shows a filtered part of this info.
+ *
+ * @param settings the history container
+ * @param key the name of the history entry
+ * @param withDate the file's name is part of the info
+ * @param tableContent OUT: the list containing the table info
+ */
+void StartView::buildTableInfo(ReSettings* settings, const char* key,
+ bool withDate, QStringList& tableContent) {
+ QStringList files;
+ settings->historyAsList(key, files);
+ QStringList::const_iterator it;
+ for (it = files.cbegin(); it != files.cend(); ++it) {
+ QFileInfo file(*it);
+ if (file.exists()) {
+ QString info = file.fileName();
+ if (withDate)
+ info.append("\t").append(
+ file.lastModified().toString("yyyy.mm.dd/HH:MM:SS"));
+ info.append("\t").append(file.path());
+ tableContent.append(info);
+ }
+ }
+}
+
+/**
+ * Shows an error message.
+ *
+ * @param message message to show
+ */
+void StartView::error(const QString& message) {
+ QMessageBox dialog(QMessageBox::Critical, "Error", message,
+ QMessageBox::Close);
+ dialog.exec();
+}
+
+/**
+ * Extracts the full filename of a given table.
+ *
+ * The node is the first column, the path the last.
+ *
+ * @param table the table from which the filename is taken
+ * @param row the row where the filename is
+ * @return the full name of the file in the given row
+ */
+QString StartView::fileOfTable(QTableWidget* table, int row) {
+ int colPath = table->columnCount() - 1;
+ QString file = table->item(row, colPath)->text() + OS_SEPARATOR_STR
+ + table->item(row, 0)->text();
+ return file;
+}
+/**
+ * Opens a file or a directory (project directory).
+ */
+void StartView::open() {
+ QString name = ui->lineEditOpen->text();
+ if (name.isEmpty())
+ error("missing filename/project directory");
+ else {
+ QFileInfo file(name);
+ if (!file.exists())
+ error("does not exists: " + name);
+ else {
+ if (file.isDir())
+ m_mainWindow->perspectives().project(true)->openFile(name);
+ else
+ m_mainWindow->openFile(name);
+ close();
+ }
+ }
+}
+
+/**
+ * Selects a directory (project directory) with an open dialog.
+ */
+void StartView::selectDir() {
+ QString name = ui->lineEditOpen->text();
+ if (name.isEmpty())
+ name = m_mainWindow->perspectives().project()->path();
+ name = QFileDialog::getExistingDirectory(this,
+ tr("Select Project Directory"), name);
+ if (!name.isEmpty()) {
+ ui->lineEditOpen->setText(name);
+ open();
+ }
+}
+
+/**
+ * Selects a file with a file open dialog.
+ */
+void StartView::selectFile() {
+ QString name = ui->lineEditOpen->text();
+ name = QFileDialog::getOpenFileName(this, tr("Select File"), name);
+ if (!name.isEmpty()) {
+ ui->lineEditOpen->setText(name);
+ open();
+ }
+}
+
+/**
+ * Handles the filter text change for a given table.
+ *
+ * @param text the filter text
+ * @param table the table which will be filled
+ * @param lines the full (unfiltered) table info
+ */
+void StartView::textChanged(const QString& text, QTableWidget* table,
+ const QStringList& lines) {
+ buildTable(text, lines, table);
+ if (table->rowCount() > 0) {
+ QString file = fileOfTable(table, 0);
+ ui->lineEditOpen->setText(file);
+ }
+}
+
+/**
+ * Handles the event "text changed" of the last opened files.
+ *
+ * @param text the new text
+ */
+void StartView::textChangedFilterFiles(const QString& text) {
+ textChanged(text, ui->tableWidgetFiles, m_files);
+}
+
+/**
+ * Handles the event "text changed" of the last opened projects.
+ *
+ * @param text the new text
+ */
+void StartView::textChangedFilterProjects(const QString& text) {
+ textChanged(text, ui->tableWidgetProjects, m_projects);
+}
/**
* Returns the view specific widget.
*
* @return the view specific widget
*/
-QWidget*StartView::widget() {
- return NULL; // ui->startWidget
+QWidget* StartView::widget() {
+ return this;
}
#ifndef STARTVIEW_HPP
#define STARTVIEW_HPP
-
-#include <QWidget>
+#include <QTableWidget>
#include "View.hpp"
namespace Ui {
class StartView;
public:
virtual QWidget* widget();
+protected:
+ void buildTable(const QString& filter, const QStringList& lines,
+ QTableWidget* table);
+ void buildTableInfo(ReSettings* settings, const char* key, bool withDate,
+ QStringList& tableContent);
+ QString fileOfTable(QTableWidget* table, int row);public slots:
+ void open();
+ void selectDir();
+ void selectFile();protected slots:
+ void textChanged(const QString& text, QTableWidget* table,
+ const QStringList& lines);protected slots:
+ void cellEnteredFiles(int row, int col);
+ void cellEnteredProjects(int row, int col);
+ void textChangedFilterFiles(const QString& text);
+ void textChangedFilterProjects(const QString& text);
+private:
+ void error(const QString& message);
private:
Ui::StartView *ui;
+ QStringList m_files;
+ QStringList m_projects;
};
#endif // STARTVIEW_HPP
*/
View::View(const char* name, MainWindow* mainWindow) :
m_name(name),
- m_mainWindow(mainWindow) {
+ m_mainWindow(mainWindow),
+ m_rootLayout(NULL) {
+}
+
+/**
+ * Returns the outermost widget of the view which must be a layout.
+ *
+ * @return return m_rootLayout;
+ */
+QLayout* View::rootLayout() {
+ if (m_rootLayout == NULL) {
+ m_rootLayout = new QVBoxLayout;
+ m_rootLayout->addWidget(widget());
+ }
+ return m_rootLayout;
}
#define VIEW_HPP
class MainWindow;
-
+#if ! defined REBASE_HPP
+#include "base/rebase.hpp"
+#endif
+#if ! defined REGUI_HPP
+#include "gui/regui.hpp"
+#endif
/**
* Base class of the views.
*
*/
virtual QWidget* widget() = 0;
+ virtual QLayout* rootLayout();
protected:
QByteArray m_name;
MainWindow* m_mainWindow;
+ QLayout* m_rootLayout;
};
#endif // VIEW_HPP
LOC_DELETE_TREE_3, // 11803
};
-
/**
* Constructor.
*/
ReTreeStatistic::ReTreeStatistic() :
- m_files(0),
- m_directories(0),
- m_fileSizes(0L){
+ m_files(0),
+ m_directories(0),
+ m_fileSizes(0L) {
+}
+
+/**
+ * Returns whether a path is an absolute path.
+ *
+ * @param path the path to test
+ * @return <code>true</code>: the path is absolute<br>
+ * <code>false</code>: the path is relative
+ */
+bool ReFileUtils::isAbsolutPath(const QString& path) {
+ bool rc;
+#ifdef __linux__
+ rc = path.startsWith(OS_SEPARATOR);
+#else
+ rc = path.startsWith(OS_SEPARATOR)
+ || path.length() > 2 && path.at(1) == ':';
+#endif
+ return rc;
}
/**
* @return <code>true</code>: all files deleted<br>
* <code>false</code>: at least one deletion failed
*/
-bool ReFileUtils::deleteTree(const QString& path, bool withBase, ReLogger* logger){
+bool ReFileUtils::deleteTree(const QString& path, bool withBase,
+ ReLogger* logger) {
bool rc = true;
QDir dir(path);
if (dir.exists(path)) {
QFileInfo info;
- QStringList names = dir.entryList(QDir::NoDotAndDotDot
- | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files);
+ QStringList names = dir.entryList(
+ QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs
+ | QDir::Files);
QStringList::const_iterator it;
- for (it = names.constBegin(); it != names.constEnd(); ++it){
+ for (it = names.constBegin(); it != names.constEnd(); ++it) {
QString full(path);
full.append(OS_SEPARATOR_STR).append(*it);
QFileInfo info(full);
if (info.isDir()) {
- if (! deleteTree(full, false, logger))
+ if (!deleteTree(full, false, logger))
rc = false;
- else if (rmdir(full.toUtf8()) != 0){
+ else if (rmdir(full.toUtf8()) != 0) {
rc = false;
if (logger != NULL)
logger->logv(LOG_ERROR, LOC_DELETE_TREE_1,
- "cannot delete directory (%d): %s",
- errno, full.toUtf8().constData());
+ "cannot delete directory (%d): %s", errno,
+ full.toUtf8().constData());
}
} else {
- if (! QFile::remove(full)){
+ if (!QFile::remove(full)) {
rc = false;
if (logger != NULL)
logger->logv(LOG_ERROR, LOC_DELETE_TREE_2,
- "cannot delete file (%d): %s",
- errno, full.toUtf8().constData());
+ "cannot delete file (%d): %s", errno,
+ full.toUtf8().constData());
}
}
}
}
- if (withBase && (rmdir(path.toUtf8())) != 0){
+ if (withBase && (rmdir(path.toUtf8())) != 0) {
rc = false;
logger->logv(LOG_ERROR, LOC_DELETE_TREE_3,
- "cannot delete directory (%d): %s",
- errno, path.toUtf8());
+ "cannot delete directory (%d): %s", errno, path.toUtf8());
}
return rc;
}
-
-
-
-
/**
* Reads a string from a given file.
*
* @param buffer OUT: the buffer to write
* @return <code>buffer</code> (for chaining)
*/
-QByteArray& ReFileUtils::readFromFile(const char* filename, QByteArray& buffer) {
+QByteArray& ReFileUtils::readFromFile(const char* filename,
+ QByteArray& buffer) {
FILE* fp = fopen(filename, "r");
if (fp != NULL) {
struct stat info;
* @return the name of an existing directory
*/
QByteArray ReFileUtils::tempDir(const char* node, const char* parent,
- bool withSeparator) {
+ bool withSeparator) {
#if defined __linux__
QByteArray temp("/tmp");
static const char* firstVar = "TMP";
* @return the full name of a temporary file
*/
QByteArray ReFileUtils::tempFile(const char* node, const char* parent,
- bool deleteIfExists) {
+ bool deleteIfExists) {
QByteArray rc(tempDir(parent));
if (!rc.endsWith('/'))
rc += '/';
* @param mode file write mode: "w" (write) or "a" (append)
*/
void ReFileUtils::writeToFile(const char* filename, const char* content,
- size_t contentLength, const char* mode) {
+ size_t contentLength, const char* mode) {
FILE* fp = fopen(filename, mode);
if (fp != NULL) {
if (contentLength == (size_t) - 1)
*/
class ReFileUtils {
public:
+ static bool isAbsolutPath(const QString& path);
+ static bool isAbsolutPath(const char* path);
static QByteArray tempDir(const char* node, const char* parent = NULL,
- bool withSeparator = true);
+ bool withSeparator = true);
static QByteArray tempFile(const char* node, const char* parent = NULL,
- bool deleteIfExists = true);
+ bool deleteIfExists = true);
static QByteArray& readFromFile(const char* filename, QByteArray& buffer);
static void writeToFile(const char* filename, const char* content,
- size_t contentLength = (size_t) - 1, const char* mode = "w");
- static bool deleteTree(const QString& path, bool withBase, ReLogger* logger);
+ size_t contentLength = (size_t) - 1, const char* mode = "w");
+ static bool deleteTree(const QString& path, bool withBase,
+ ReLogger* logger);
};
#endif // REFILEUTILS_HPP
* @param path the parent directory for the storage files
* @param prefix type of the storage: "proj" or "ws" (workspace)
*/
-ReSettings::ReSettings(const QString& path, const QString prefix,
+ReSettings::ReSettings(const QString& path, const QString& prefix,
ReLogger* logger) :
- m_path(path),
- m_fileHistory(path + OS_SEPARATOR + prefix + ".history"),
- m_fileSettings(path + OS_SEPARATOR + prefix + ".settings"),
+ m_prefix(prefix),
+ m_path(),
+ m_fileHistory(),
+ m_fileSettings(),
m_settings(),
m_chapters(),
m_logger(logger) {
-
+ setPath(path);
}
/*
*/
void ReSettings::addHistoryEntry(const char* key, const QString& value,
char separator, int maxEntries) {
- ReStateStorage store(m_fileHistory);
+ ReStateStorage store(m_fileHistory, m_logger);
store.initForRead();
store.addHistoryEntry(key, value, separator, maxEntries);
store.close();
* otherwise: the first item of the history entry
*/
QString ReSettings::topOfHistory(const char* key, const QString& defaultValue) {
- ReStateStorage store(m_fileHistory);
+ ReStateStorage store(m_fileHistory, m_logger);
store.initForRead();
QString rc = store.map().value(key, "\t");
if (rc == "\t")
*/
QStringList&ReSettings::historyAsList(const char* key, QStringList& list,
const char* form) {
- ReStateStorage store(m_fileHistory);
+ ReStateStorage store(m_fileHistory, m_logger);
store.initForRead();
QStringList& rc = store.historyAsList(key, list, form);
store.close();
}
}
+/**
+ * Sets the path containing the settings files.
+ *
+ * @param path the new path
+ */
+void ReSettings::setPath(const QString& path) {
+ m_path = path;
+ m_fileHistory = path + OS_SEPARATOR + m_prefix + ".history";
+ m_fileSettings = path + OS_SEPARATOR + m_prefix + ".settings";
+}
+
/**
* Returns the value of a string property.
*
static QString TRUE;
static QString FALSE;
public:
- ReSettings(const QString& path, const QString prefix, ReLogger* logger);
+ ReSettings(const QString& path, const QString& prefix, ReLogger* logger);
public:
void addHistoryEntry(const char* key, const QString& value, char separator,
int maxEntries);
int intValue(const char* name);
const QString& path() const;
void readSettings();
+ void setPath(const QString& path);
QString stringValue(const char* name);
QString topOfHistory(const char* key, const QString& defaultValue = "");
void writeSettings();
+
protected:
+ QString m_prefix;
QString m_path;
QString m_fileHistory;
QString m_fileSettings;
#include "base/rebase.hpp"
#include "gui/regui.hpp"
+enum {
+ LOC_INIT_FOR_WRITE_1 = LOC_FIRST_OF(LOC_STATESTORAGE), // 12001
+ LOC_INIT_FOR_READ_1, // 12002
+};
/**
* Constructor.
*
* @param filename filename with path of the storage file
*/
-ReStateStorage::ReStateStorage(const QString& filename) :
+ReStateStorage::ReStateStorage(const QString& filename, ReLogger* logger) :
m_filename(filename),
m_fp(NULL),
m_stream(NULL),
- m_form() {
+ m_form(),
+ m_logger(logger) {
}
/**
* Writes the content of the map into the file.
*/
void ReStateStorage::flushMap() {
- initForWrite();
- QMap<QByteArray, QString>::const_iterator it;
- for (it = m_map.constBegin(); it != m_map.constEnd(); ++it) {
- *m_stream << it.key() << "=" << it.value() << endl;
+ if (initForWrite()) {
+ QMap<QByteArray, QString>::const_iterator it;
+ for (it = m_map.constBegin(); it != m_map.constEnd(); ++it) {
+ *m_stream << it.key() << "=" << it.value() << endl;
+ }
+ m_stream->flush();
}
- m_stream->flush();
}
/**
* Returns the name of the current form.
*/
bool ReStateStorage::initForRead() {
if (m_fp == NULL)
- m_fp = fopen(m_filename.toUtf8().constData(), "rb");
+ if ((m_fp = fopen(m_filename.toUtf8().constData(), "rb")) == NULL) {
+ if (m_logger != NULL)
+ m_logger->logv(LOG_ERROR, LOC_INIT_FOR_READ_1,
+ "cannot open %s: %d", m_filename.toUtf8().constData(),
+ errno);
+ }
if (m_fp != NULL && m_stream == NULL) {
m_stream = new QTextStream(m_fp, QIODevice::ReadOnly);
QString line;
bool ReStateStorage::initForWrite() {
if (m_fp == NULL)
m_fp = fopen(m_filename.toUtf8().constData(), "wb");
- if (m_fp != NULL)
+ if (m_fp == NULL) {
+ if (m_logger != NULL)
+ m_logger->logv(LOG_ERROR, LOC_INIT_FOR_WRITE_1,
+ "cannot open %s: %d", m_filename.toUtf8().constData(), errno);
+ } else
m_stream = new QTextStream(m_fp, QIODevice::ReadWrite);
return m_stream != NULL;
}
class ReStateStorage {
public:
- ReStateStorage(const QString& filename);
+ ReStateStorage(const QString& filename, ReLogger* logger);
virtual ~ReStateStorage();
public:
void addHistoryEntry(const char* key, const QString& value, char separator,
QTextStream* m_stream;
QByteArray m_form;
QMap<QByteArray, QString> m_map;
+ ReLogger* m_logger;
};
#endif /* GUI_RESTATESTORAGE_HPP_ */
* The latest sources: https://github.com/republib
*/
-#ifndef GUI_REGUI_HPP_
-#define GUI_REGUI_HPP_
+#ifndef REGUI_HPP
+#define REGUI_HPP
#include "gui/ReStateStorage.hpp"
#include "gui/ReGuiValidator.hpp"
#endif
}
-#endif /* GUI_REGUI_HPP_ */
+#endif /* REGUI_HPP */
LOC_SETTINGS,
LOC_FILE,
LOC_FILETREE,
+ LOC_STATESTORAGE, // 120
};
#define LOC_FIRST_OF(moduleNo) (moduleNo*100+1)
class RplModules {