From 8d879a736373aaa57eb2c596f802772a96d5e8c4 Mon Sep 17 00:00:00 2001 From: hama Date: Tue, 27 Oct 2015 23:59:33 +0100 Subject: [PATCH] reidos initializations --- appl/reidos/FileCommander.cpp | 39 ++++++++++++++++ appl/reidos/FileCommander.hpp | 30 +++++++++++++ appl/reidos/idosmain.cpp | 78 ++++++++++++++++++++++++++++++-- appl/reidos/idosmain.hpp | 18 ++++++-- appl/reidos/main.cpp | 6 ++- appl/reidos/reidos.hpp | 22 +++++++++ appl/reidos/reidos.pro | 8 +++- base/ReLogger.cpp | 84 ++++++++++++++++++++--------------- base/ReLogger.hpp | 28 +++++++----- gui/ReGuiValidator.cpp | 6 +-- 10 files changed, 257 insertions(+), 62 deletions(-) create mode 100644 appl/reidos/FileCommander.cpp create mode 100644 appl/reidos/FileCommander.hpp create mode 100644 appl/reidos/reidos.hpp diff --git a/appl/reidos/FileCommander.cpp b/appl/reidos/FileCommander.cpp new file mode 100644 index 0000000..a1bb704 --- /dev/null +++ b/appl/reidos/FileCommander.cpp @@ -0,0 +1,39 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. +*/ + + +#include "reidos.hpp" + +/** + * Constructor. + */ +FileCommander::FileCommander(IDosMain* main) : + m_main(main), + m_topFS(NULL), + m_bottomFS(NULL), + m_activeFS(NULL), + m_inactiveFS(NULL), + m_logger(new ReMemoryLogger) +{ +} + +/** + * Sets a filesystem to a filesystem given by its url. + * + * @param url the Uniform Resource Locator, e.g. "file:/home/jonny" + * @param filesystem IN/OUT: the filesystem to change + */ +void FileCommander::buildFs(const QString& url, ReFileSystem& filesystem) +{ + if (url.startsWith("file:", Qt::CaseInsensitive)){ + + } else { + m_main->setStatusMessage(true, QObject::tr("unknown protocol in url: ") + url); + } +} + diff --git a/appl/reidos/FileCommander.hpp b/appl/reidos/FileCommander.hpp new file mode 100644 index 0000000..5df917b --- /dev/null +++ b/appl/reidos/FileCommander.hpp @@ -0,0 +1,30 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. +*/ + + +#ifndef FILECOMMANDER_HPP +#define FILECOMMANDER_HPP + +class IDosMain; + +class FileCommander +{ +public: + FileCommander(IDosMain* m_main); +public: + void buildFs(const QString& url, ReFileSystem& filesystem); +protected: + IDosMain* m_main; + ReFileSystem* m_topFS; + ReFileSystem* m_bottomFS; + ReFileSystem* m_activeFS; + ReFileSystem* m_inactiveFS; + ReLogger* m_logger; +}; + +#endif // FILECOMMANDER_HPP diff --git a/appl/reidos/idosmain.cpp b/appl/reidos/idosmain.cpp index e2a4e6e..e76926f 100644 --- a/appl/reidos/idosmain.cpp +++ b/appl/reidos/idosmain.cpp @@ -6,18 +6,88 @@ * The original sources can be found on https://github.com/republib. */ - -#include "idosmain.hpp" +#include "reidos.hpp" #include "ui_idosmain.h" -IDosMain::IDosMain(QWidget *parent) : +IDosMain::IDosMain(const QString& startDir, const QString& homeDir, + QWidget *parent) : QMainWindow(parent), - ui(new Ui::IDosMain) + ReGuiValidator(), + FileCommander(this), + ui(new Ui::IDosMain), + m_statusMessage(NULL), + m_homeDir(homeDir) + //m_storageFile(), { ui->setupUi(this); + initializeHome(); + m_statusMessage = new QLabel(tr("Welcome at reidos")); } IDosMain::~IDosMain() { delete ui; } + +void IDosMain::error(const QString& message) +{ + m_statusMessage->setText("+++ " + message); +} + +/** + * initializeHomeializes the program home directory. + */ +void IDosMain::initializeHome(){ + if (m_homeDir.isEmpty()){ + m_homeDir = QDir::home().absoluteFilePath(".reidos"); + } + + QDir home(m_homeDir); + if (!home.exists()){ + if (!home.mkpath(m_homeDir)){ + m_homeDir = home.tempPath() + "/.redos"; + home.mkpath(m_homeDir); + } + } + if (!m_homeDir.endsWith("/")) + m_homeDir += "/"; + m_storageFile = m_homeDir + "state.conf"; + restoreState(); +} + +/** + * Writes a text to the status line. + * + * @param error true: the message is an error message + * @param message the text to set + */ +void IDosMain::setStatusMessage(bool error, const QString& message){ + m_statusMessage->setText(error ? "+++ " + message : message); +} + +/** + * Reads the history of the widget values and other parameters and set it. + */ +void IDosMain::restoreState(){ + ReStateStorage storage(m_storageFile, m_logger); + storage.setForm("main"); + storage.restore(ui->comboBoxPathTop, "comboBoxPathTop", true); + storage.restore(ui->comboBoxPatternTop, "comboBoxPatternTop", false); + storage.restore(ui->comboBoxPathBottom, "comboBoxPathBottom", true); + storage.restore(ui->comboBoxPatternBottom, "comboBoxPatternBottom", false); + + storage.close(); +} + +/** + * Stores the history of the widget values and other parameters. + */ +void IDosMain::saveState(){ + ReStateStorage storage(m_storageFile, m_logger); + storage.setForm("main"); + storage.store(ui->comboBoxPathTop, "comboBoxPathTop"); + storage.store(ui->comboBoxPatternTop, "comboBoxPatternTop"); + storage.store(ui->comboBoxPathBottom, "comboBoxPathBottom"); + storage.store(ui->comboBoxPatternBottom, "comboBoxPatternBottom"); + storage.close(); +} diff --git a/appl/reidos/idosmain.hpp b/appl/reidos/idosmain.hpp index 769371f..7aa8612 100644 --- a/appl/reidos/idosmain.hpp +++ b/appl/reidos/idosmain.hpp @@ -15,17 +15,29 @@ namespace Ui { class IDosMain; } - -class IDosMain : public QMainWindow +#include "reidos.hpp" +class IDosMain : public QMainWindow, public ReGuiValidator, public FileCommander { Q_OBJECT public: - explicit IDosMain(QWidget *parent = 0); + explicit IDosMain(const QString& startDir, const QString& homeDir, + QWidget *parent = 0); ~IDosMain(); +public: + void error(const QString& message); + virtual void setStatusMessage(bool error, const QString& message); +protected: + void initializeHome(); + void restoreState(); + void saveState(); private: Ui::IDosMain *ui; + QLabel* m_statusMessage; + QString m_homeDir; + QString m_storageFile; + ReLogger* m_logger; }; #endif // IDOSMAIN_HPP diff --git a/appl/reidos/main.cpp b/appl/reidos/main.cpp index 83d2a71..4010211 100644 --- a/appl/reidos/main.cpp +++ b/appl/reidos/main.cpp @@ -7,13 +7,15 @@ */ -#include "idosmain.hpp" +#include "reidos.hpp" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); - IDosMain w; + QString startDir; + QString homeDir; + IDosMain w(startDir, homeDir); w.show(); return a.exec(); diff --git a/appl/reidos/reidos.hpp b/appl/reidos/reidos.hpp new file mode 100644 index 0000000..5b03e48 --- /dev/null +++ b/appl/reidos/reidos.hpp @@ -0,0 +1,22 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. +*/ + + +#ifndef REIDOS_HPP +#define REIDOS_HPP +#include "QLabel" + + +#include "base/rebase.hpp" +#include "os/reos.hpp" +#include "gui/regui.hpp" +#include "FileCommander.hpp" +#include "idosmain.hpp" + +#endif // REIDOS_HPP + diff --git a/appl/reidos/reidos.pro b/appl/reidos/reidos.pro index 499926d..34f7400 100644 --- a/appl/reidos/reidos.pro +++ b/appl/reidos/reidos.pro @@ -18,6 +18,7 @@ SOURCES += \ ../../gui/ReStateStorage.cpp \ ../../gui/ReSettings.cpp \ ../../gui/ReFileTree.cpp \ + ../../gui/ReGuiValidator.cpp \ ../../base/ReMatcher.cpp \ ../../base/ReFile.cpp \ ../../base/ReStringUtils.cpp \ @@ -29,7 +30,8 @@ SOURCES += \ ../../base/ReFileUtils.cpp \ ../../base/ReException.cpp \ idosmain.cpp \ - main.cpp + main.cpp \ + FileCommander.cpp HEADERS += ../../base/rebase.hpp \ ../../gui/regui.hpp \ @@ -40,6 +42,8 @@ HEADERS += ../../base/rebase.hpp \ ../../base/ReQStringUtils.hpp \ ../../base/ReException.hpp \ ../../os/reos.hpp \ - idosmain.hpp + idosmain.hpp \ + FileCommander.hpp \ + reidos.hpp FORMS += idosmain.ui diff --git a/base/ReLogger.cpp b/base/ReLogger.cpp index 94c931b..955963f 100644 --- a/base/ReLogger.cpp +++ b/base/ReLogger.cpp @@ -61,8 +61,8 @@ void ReLogger::destroyGlobalLogger() { * @param name identifies the logger. Useful for ReLogger::findLogger() */ ReAppender::ReAppender(const QByteArray& name) : - m_name(name), - m_level(LOG_INFO) { + m_name(name), + m_level(LOG_INFO) { } /** @@ -149,11 +149,11 @@ bool ReAppender::isAutoDelete() const { * @param isGlobal true: the logger becomes the global logger */ ReLogger::ReLogger(bool isGlobal) : - // m_appenders(), - m_countAppenders(0), - m_stdPrefix(), - m_mutex(), - m_withLocking(false) { + // m_appenders(), + m_countAppenders(0), + m_stdPrefix(), + m_mutex(), + m_withLocking(false) { memset(m_appenders, 0, sizeof m_appenders); if (isGlobal) { m_globalLogger = this; @@ -288,7 +288,7 @@ bool ReLogger::log(ReLoggerLevel level, int location, const char* message) { * @return true: for chaining */ bool ReLogger::log(ReLoggerLevel level, int location, - const QByteArray& message) { + const QByteArray& message) { return log(level, location, message.data()); } @@ -314,7 +314,7 @@ bool ReLogger::log(ReLoggerLevel level, int location, const ReString& message) { * @return true: for chaining */ bool ReLogger::logv(ReLoggerLevel level, int location, const char* format, - ...) { + ...) { char buffer[64000]; va_list ap; va_start(ap, format); @@ -333,7 +333,7 @@ bool ReLogger::logv(ReLoggerLevel level, int location, const char* format, * @return true: for chaining */ bool ReLogger::logv(ReLoggerLevel level, int location, const QByteArray& format, - ...) { + ...) { char buffer[64000]; va_list ap; va_start(ap, format); @@ -352,7 +352,7 @@ bool ReLogger::logv(ReLoggerLevel level, int location, const QByteArray& format, * @return true: for chaining */ bool ReLogger::log(ReLoggerLevel level, int location, const char* format, - va_list& varlist) { + va_list& varlist) { char buffer[64000]; qvsnprintf(buffer, sizeof buffer, format, varlist); return log(level, location, buffer); @@ -369,8 +369,8 @@ QByteArray ReLogger::buildStdPrefix(ReLoggerLevel level, int location) { struct tm* now2 = localtime(&now); char buffer[64]; qsnprintf(buffer, sizeof buffer, "%c%d.%02d.%02d %02d:%02d:%02d (%d): ", - getPrefixOfLevel(level), now2->tm_year + 1900, now2->tm_mon + 1, - now2->tm_mday, now2->tm_hour, now2->tm_min, now2->tm_sec, location); + getPrefixOfLevel(level), now2->tm_year + 1900, now2->tm_mon + 1, + now2->tm_mday, now2->tm_hour, now2->tm_min, now2->tm_sec, location); return QByteArray(buffer); } @@ -418,10 +418,10 @@ ReAppender* ReLogger::findAppender(const char* name) const { * configuration file */ void ReLogger::buildStandardAppender(ReConfig* config, const char* prefix, - const char* defaultLogfilePrefix) { + const char* defaultLogfilePrefix) { QByteArray sPrefix(prefix); QByteArray logFilePrefix = config->asString(sPrefix + "name", - defaultLogfilePrefix); + defaultLogfilePrefix); int maxSize = config->asInt(+"maxsize", 10100100); int maxCount = config->asInt(sPrefix + "maxfiles", 5); @@ -445,12 +445,12 @@ void ReLogger::buildStandardAppender(ReConfig* config, const char* prefix, * @param maxCount the maximal count of files. If neccessary the oldest file will be deleted */ void ReLogger::buildStandardAppender(const QByteArray& prefix, int maxSize, - int maxCount) { + int maxCount) { ReStreamAppender* streamAppender = new ReStreamAppender(stderr); streamAppender->setAutoDelete(true); addAppender((ReAppender*) streamAppender); ReFileAppender* fileAppender = new ReFileAppender(prefix, maxSize, - maxCount); + maxCount); fileAppender->setAutoDelete(true); addAppender((ReAppender*) fileAppender); } @@ -466,8 +466,8 @@ void ReLogger::buildStandardAppender(const QByteArray& prefix, int maxSize, * @brief Constructor. */ ReStreamAppender::ReStreamAppender(FILE* file, const char* appenderName) : - ReAppender(QByteArray(appenderName)), - m_fp(file) { + ReAppender(QByteArray(appenderName)), + m_fp(file) { } /** @@ -486,7 +486,7 @@ ReStreamAppender::~ReStreamAppender() { * @param logger the calling logger */ void ReStreamAppender::log(ReLoggerLevel level, int location, - const char* message, ReLogger* logger) { + const char* message, ReLogger* logger) { const QByteArray& prefix = logger->getStdPrefix(level, location); fputs(prefix, m_fp); fputs(message, m_fp); @@ -516,14 +516,14 @@ void ReStreamAppender::log(ReLoggerLevel level, int location, * @param appenderName the name of the appender. @see ReLogger::findAppender() */ ReFileAppender::ReFileAppender(const QByteArray& prefix, int maxSize, - int maxCount, const char* appenderName) : - ReAppender(QByteArray(appenderName)), - m_prefix(prefix), - m_maxSize(maxSize), - m_maxCount(maxCount), - m_currentSize(0), - m_currentNo(0), - m_fp(NULL) { + int maxCount, const char* appenderName) : + ReAppender(QByteArray(appenderName)), + m_prefix(prefix), + m_maxSize(maxSize), + m_maxCount(maxCount), + m_currentSize(0), + m_currentNo(0), + m_fp(NULL) { open(); } @@ -545,7 +545,7 @@ void ReFileAppender::open() { fclose(m_fp); char fullName[512]; qsnprintf(fullName, sizeof fullName, "%s.%03d.log", m_prefix.data(), - ++m_currentNo); + ++m_currentNo); m_fp = fopen(fullName, "a"); if (m_fp == NULL) fprintf(stderr, "cannot open: %s\n", fullName); @@ -565,7 +565,7 @@ void ReFileAppender::open() { */ #pragma GCC diagnostic ignored "-Wunused-parameter" void ReFileAppender::log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger) { + ReLogger* logger) { if (m_fp != NULL) { const QByteArray& prefix = logger->getStdPrefix(level, location); fputs(prefix, m_fp); @@ -591,10 +591,10 @@ void ReFileAppender::log(ReLoggerLevel level, int location, const char* message, * @param appenderName NULL or the name of the appender */ ReMemoryAppender::ReMemoryAppender(int maxLines, const char* appenderName) : - ReAppender(appenderName), - m_lines(), - m_maxLines(maxLines), - m_addPrefix(true) { + ReAppender(appenderName), + m_lines(), + m_maxLines(maxLines), + m_addPrefix(true) { m_lines.reserve(maxLines); } @@ -614,7 +614,7 @@ ReMemoryAppender::~ReMemoryAppender() { */ #pragma GCC diagnostic ignored "-Wunused-parameter" void ReMemoryAppender::log(ReLoggerLevel level, int location, - const char* message, ReLogger* logger) { + const char* message, ReLogger* logger) { if (m_lines.size() >= m_maxLines) m_lines.removeFirst(); if (!m_addPrefix) @@ -649,7 +649,7 @@ void ReMemoryAppender::clear() { * @param appenderName a name for the appender */ ReDebugAppender::ReDebugAppender(const char* appenderName) : - ReAppender(appenderName) { + ReAppender(appenderName) { } /** * Destructor. @@ -666,7 +666,17 @@ ReDebugAppender::~ReDebugAppender() { * @param logger the calling logger */ void ReDebugAppender::log(ReLoggerLevel level, int location, - const char* message, ReLogger* logger) { + const char* message, ReLogger* logger) { QByteArray msg(logger->getStdPrefix(level, location)); qDebug("%s%s", msg.constData(), message); } + +/** + * Constructor. + */ +ReMemoryLogger::ReMemoryLogger() : + ReLogger(true), + ReMemoryAppender() +{ + addAppender(this); +} diff --git a/base/ReLogger.hpp b/base/ReLogger.hpp index e3be2af..c8b24fe 100644 --- a/base/ReLogger.hpp +++ b/base/ReLogger.hpp @@ -41,7 +41,7 @@ private: ReAppender& operator =(const ReAppender& source); public: virtual void log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger) = 0; + ReLogger* logger) = 0; bool isActive(ReLoggerLevel level); void setLevel(ReLoggerLevel level); void setAutoDelete(bool onNotOff); @@ -74,14 +74,14 @@ public: bool logv(ReLoggerLevel level, int location, const char* format, ...); bool logv(ReLoggerLevel level, int location, const QByteArray& format, ...); bool log(ReLoggerLevel level, int location, const char* format, - va_list& varlist); + va_list& varlist); void addAppender(ReAppender* appender); ReAppender* findAppender(const char* name) const; void buildStandardAppender(ReConfig* config, - const char* prefix = "logfile.", const char* defaultLoggerName = - "logger"); + const char* prefix = "logfile.", const char* defaultLoggerName = + "logger"); void buildStandardAppender(const QByteArray& prefix, - int maxSize = 10 * 1024 * 1024, int maxCount = 5); + int maxSize = 10 * 1024 * 1024, int maxCount = 5); QByteArray buildStdPrefix(ReLoggerLevel level, int location); const QByteArray& getStdPrefix(ReLoggerLevel level, int location); char getPrefixOfLevel(ReLoggerLevel level) const; @@ -114,7 +114,7 @@ public: virtual ~ReStreamAppender(); public: virtual void log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger); + ReLogger* logger); private: // stdout or stderr: FILE* m_fp; @@ -126,12 +126,12 @@ private: class ReFileAppender: public ReAppender { public: ReFileAppender(const QByteArray& name, int maxSize, int maxCount, - const char* appenderName = "FileAppender"); + const char* appenderName = "FileAppender"); virtual ~ReFileAppender(); public: void open(); virtual void log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger); + ReLogger* logger); private: // prefix of the log file name. Will be appended by "..log" @@ -154,11 +154,11 @@ private: class ReMemoryAppender: public ReAppender { public: ReMemoryAppender(int maxLines = 1024, const char* appenderName = - "MemoryAppender"); + "MemoryAppender"); ~ReMemoryAppender(); public: virtual void log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger); + ReLogger* logger); const QList& getLines() const; void clear(); private: @@ -178,7 +178,13 @@ public: ~ReDebugAppender(); public: virtual void log(ReLoggerLevel level, int location, const char* message, - ReLogger* logger); + ReLogger* logger); }; +class ReMemoryLogger : public ReLogger, public ReMemoryAppender { +public: + ReMemoryLogger(); +}; + + #endif // RELOGGER_HPP diff --git a/gui/ReGuiValidator.cpp b/gui/ReGuiValidator.cpp index 74306b6..d09a6e1 100644 --- a/gui/ReGuiValidator.cpp +++ b/gui/ReGuiValidator.cpp @@ -16,7 +16,7 @@ * Constructor. */ ReGuiValidator::ReGuiValidator() : - m_errors(0) { + m_errors(0) { } /** @@ -61,7 +61,7 @@ QDateTime ReGuiValidator::comboDate(QComboBox* combo) { * otherwise: or the size resulting from the formula */ int ReGuiValidator::comboInt(QComboBox* combo, int defaultValue, - const char* specialString, int specialValue) { + const char* specialString, int specialValue) { QString value = combo->currentText(); int rc = defaultValue; if (!value.isEmpty()) { @@ -69,7 +69,7 @@ int ReGuiValidator::comboInt(QComboBox* combo, int defaultValue, rc = specialValue; } else { uint nValue = 0; - if (ReQStringUtil::lengthOfUInt(value, 0, 10, &nValue) == 0) + if (ReQStringUtils::lengthOfUInt(value, 0, 10, &nValue) == 0) guiError(combo, QObject::tr("not an integer: ") + value); else { setInHistory(combo, value); -- 2.39.5