From ae706fb066331bb41770ae751cdccee9080f4c04 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 13 Sep 2017 21:51:34 +0800 Subject: [PATCH] add created_time and modified_time to file --- src/dialog/vfileinfodialog.cpp | 39 ++++++++++++++++++++------ src/dialog/vfileinfodialog.h | 2 -- src/main.cpp | 6 ++-- src/vconstants.h | 2 ++ src/vdirectory.cpp | 27 +++++++++++++----- src/vdirectory.h | 1 + src/vfile.cpp | 47 +++++++++++++++++++++++++++++--- src/vfile.h | 50 ++++++++++++++++++++++++++++++++-- src/vorphanfile.cpp | 7 ++++- src/vorphanfile.h | 7 +++++ 10 files changed, 161 insertions(+), 27 deletions(-) diff --git a/src/dialog/vfileinfodialog.cpp b/src/dialog/vfileinfodialog.cpp index 51ffe3b0..94e95019 100644 --- a/src/dialog/vfileinfodialog.cpp +++ b/src/dialog/vfileinfodialog.cpp @@ -10,7 +10,7 @@ extern VConfigManager *g_config; VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info, VDirectory *directory, const VFile *file, QWidget *parent) - : QDialog(parent), infoLabel(NULL), title(title), info(info), + : QDialog(parent), title(title), info(info), m_directory(directory), m_file(file) { setupUI(); @@ -22,13 +22,38 @@ VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info, void VFileInfoDialog::setupUI() { + QLabel *infoLabel = NULL; if (!info.isEmpty()) { infoLabel = new QLabel(info); } - nameLabel = new QLabel(tr("Note &name:")); - nameEdit = new QLineEdit(m_file->getName()); - nameEdit->selectAll(); - nameLabel->setBuddy(nameEdit); + + // File name. + QString name = m_file->getName(); + nameEdit = new QLineEdit(name); + int baseStart = 0, baseLength = name.size(); + int dotIdx = name.lastIndexOf('.'); + if (dotIdx != -1) { + baseLength = dotIdx; + } + + // Select without suffix. + nameEdit->setSelection(baseStart, baseLength); + + // Created time. + QString createdTimeStr = m_file->getCreatedTimeUtc().toLocalTime() + .toString(Qt::DefaultLocaleLongDate); + QLabel *createdTimeLabel = new QLabel(createdTimeStr); + + // Modified time. + createdTimeStr = m_file->getModifiedTimeUtc().toLocalTime() + .toString(Qt::DefaultLocaleLongDate); + QLabel *modifiedTimeLabel = new QLabel(createdTimeStr); + modifiedTimeLabel->setToolTip(tr("Last modified time within VNote")); + + QFormLayout *topLayout = new QFormLayout(); + topLayout->addRow(tr("Note &name:"), nameEdit); + topLayout->addRow(tr("Created time:"), createdTimeLabel); + topLayout->addRow(tr("Modified time:"), modifiedTimeLabel); m_warnLabel = new QLabel(); m_warnLabel->setWordWrap(true); @@ -39,10 +64,6 @@ void VFileInfoDialog::setupUI() connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); - QHBoxLayout *topLayout = new QHBoxLayout(); - topLayout->addWidget(nameLabel); - topLayout->addWidget(nameEdit); - QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok); nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3); diff --git a/src/dialog/vfileinfodialog.h b/src/dialog/vfileinfodialog.h index 7cd4b1d7..d8ee89be 100644 --- a/src/dialog/vfileinfodialog.h +++ b/src/dialog/vfileinfodialog.h @@ -25,8 +25,6 @@ private slots: private: void setupUI(); - QLabel *infoLabel; - QLabel *nameLabel; QLineEdit *nameEdit; QLabel *m_warnLabel; QDialogButtonBox *m_btnBox; diff --git a/src/main.cpp b/src/main.cpp index 69799c7f..af14e446 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -148,7 +148,10 @@ int main(int argc, char *argv[]) g_config = &vconfig; QString locale = VUtils::getLocale(); - qDebug() << "use locale" << locale; + // Set default locale. + if (locale == "zh_CN") { + QLocale::setDefault(QLocale(QLocale::Chinese, QLocale::China)); + } // load translation for Qt QTranslator qtTranslator; @@ -161,7 +164,6 @@ int main(int argc, char *argv[]) // load translation for vnote QTranslator translator; if (translator.load("vnote_" + locale, ":/translations")) { - qDebug() << "install VNote translator"; app.installTranslator(&translator); } diff --git a/src/vconstants.h b/src/vconstants.h index c90ee660..61eafaf7 100644 --- a/src/vconstants.h +++ b/src/vconstants.h @@ -31,6 +31,8 @@ namespace DirConfig static const QString c_files = "files"; static const QString c_imageFolder = "image_folder"; static const QString c_name = "name"; + static const QString c_createdTime = "created_time"; + static const QString c_modifiedTime = "modified_time"; } static const QString c_emptyHeaderName = "[EMPTY]"; diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index d9d05015..a525a804 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -43,7 +43,10 @@ bool VDirectory::open() QJsonArray fileJson = configJson[DirConfig::c_files].toArray(); for (int i = 0; i < fileJson.size(); ++i) { QJsonObject fileItem = fileJson[i].toObject(); - VFile *file = new VFile(fileItem[DirConfig::c_name].toString(), this); + VFile *file = VFile::fromJson(fileItem, + this, + FileType::Normal, + true); m_files.append(file); } @@ -123,11 +126,9 @@ QJsonObject VDirectory::toConfigJson() const QJsonArray files; for (int i = 0; i < m_files.size(); ++i) { - QJsonObject item; - item[DirConfig::c_name] = m_files[i]->getName(); - - files.append(item); + files.append(m_files[i]->toConfigJson()); } + dirJson[DirConfig::c_files] = files; return dirJson; @@ -272,7 +273,13 @@ VFile *VDirectory::createFile(const QString &p_name) file.close(); - VFile *ret = new VFile(p_name, this); + QDateTime dateTime = QDateTime::currentDateTimeUtc(); + VFile *ret = new VFile(this, + p_name, + FileType::Normal, + true, + dateTime, + dateTime); m_files.append(ret); if (!writeToConfig()) { file.remove(); @@ -321,7 +328,13 @@ VFile *VDirectory::addFile(const QString &p_name, int p_index) return NULL; } - VFile *file = new VFile(p_name, this); + QDateTime dateTime = QDateTime::currentDateTimeUtc(); + VFile *file = new VFile(this, + p_name, + FileType::Normal, + true, + dateTime, + dateTime); if (!file) { return NULL; } diff --git a/src/vdirectory.h b/src/vdirectory.h index e4a8e112..88e7d47a 100644 --- a/src/vdirectory.h +++ b/src/vdirectory.h @@ -42,6 +42,7 @@ public: bool removeSubDirectory(VDirectory *p_dir); // Add the file in the config and m_files. If @p_index is -1, add it at the end. + // @p_name: the file name of the file to add. // Return the VFile if succeed. VFile *addFile(const QString &p_name, int p_index); diff --git a/src/vfile.cpp b/src/vfile.cpp index f22106e5..292fe7d8 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -6,11 +6,21 @@ #include #include "utils/vutils.h" -VFile::VFile(const QString &p_name, QObject *p_parent, - FileType p_type, bool p_modifiable) - : QObject(p_parent), m_name(p_name), m_opened(false), m_modified(false), +VFile::VFile(QObject *p_parent, + const QString &p_name, + FileType p_type, + bool p_modifiable, + QDateTime p_createdTimeUtc, + QDateTime p_modifiedTimeUtc) + : QObject(p_parent), + m_name(p_name), + m_opened(false), + m_modified(false), m_docType(VUtils::docTypeFromName(p_name)), - m_type(p_type), m_modifiable(p_modifiable) + m_type(p_type), + m_modifiable(p_modifiable), + m_createdTimeUtc(p_createdTimeUtc), + m_modifiedTimeUtc(p_modifiedTimeUtc) { } @@ -66,6 +76,10 @@ bool VFile::save() { Q_ASSERT(m_opened); bool ret = VUtils::writeFileToDisk(fetchPath(), m_content); + if (ret) { + m_modifiedTimeUtc = QDateTime::currentDateTimeUtc(); + } + return ret; } @@ -260,3 +274,28 @@ QString VFile::getImageFolderInLink() const { return getNotebook()->getImageFolder(); } + +VFile *VFile::fromJson(const QJsonObject &p_json, + QObject *p_parent, + FileType p_type, + bool p_modifiable) +{ + return new VFile(p_parent, + p_json[DirConfig::c_name].toString(), + p_type, + p_modifiable, + QDateTime::fromString(p_json[DirConfig::c_createdTime].toString(), + Qt::ISODate), + QDateTime::fromString(p_json[DirConfig::c_modifiedTime].toString(), + Qt::ISODate)); +} + +QJsonObject VFile::toConfigJson() const +{ + QJsonObject item; + item[DirConfig::c_name] = m_name; + item[DirConfig::c_createdTime] = m_createdTimeUtc.toString(Qt::ISODate); + item[DirConfig::c_modifiedTime] = m_modifiedTimeUtc.toString(Qt::ISODate); + + return item; +} diff --git a/src/vfile.h b/src/vfile.h index d1717e03..bbef1bde 100644 --- a/src/vfile.h +++ b/src/vfile.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "vdirectory.h" #include "vconstants.h" @@ -13,8 +14,13 @@ class VFile : public QObject { Q_OBJECT public: - VFile(const QString &p_name, QObject *p_parent, - FileType p_type = FileType::Normal, bool p_modifiable = true); + VFile(QObject *p_parent, + const QString &p_name, + FileType p_type, + bool p_modifiable, + QDateTime p_createdTimeUtc, + QDateTime p_modifiedTimeUtc); + virtual ~VFile(); virtual bool open(); virtual void close(); @@ -59,6 +65,19 @@ public: // Return the image folder part in an image link. virtual QString getImageFolderInLink() const; + // Create a VFile from @p_json Json object. + static VFile *fromJson(const QJsonObject &p_json, + QObject *p_parent, + FileType p_type, + bool p_modifiable); + + // Create a Json object from current instance. + QJsonObject toConfigJson() const; + + QDateTime getCreatedTimeUtc() const; + + QDateTime getModifiedTimeUtc() const; + public slots: void setModified(bool p_modified); @@ -69,16 +88,43 @@ protected: // Delete local images of DocType::Markdown. void deleteLocalImages(); + // Name of this file. QString m_name; + + // Whether this file has been opened. bool m_opened; + // File has been modified in editor bool m_modified; + + // DocType of this file: Html, Markdown. DocType m_docType; + + // Content of this file. QString m_content; + FileType m_type; + + // Whether this file is modifiable. bool m_modifiable; + // UTC time when creating this file. + QDateTime m_createdTimeUtc; + + // UTC time of last modification to this file in VNote. + QDateTime m_modifiedTimeUtc; + friend class VDirectory; }; +inline QDateTime VFile::getCreatedTimeUtc() const +{ + return m_createdTimeUtc; +} + +inline QDateTime VFile::getModifiedTimeUtc() const +{ + return m_modifiedTimeUtc; +} + #endif // VFILE_H diff --git a/src/vorphanfile.cpp b/src/vorphanfile.cpp index 0d864091..e05d2d4c 100644 --- a/src/vorphanfile.cpp +++ b/src/vorphanfile.cpp @@ -10,7 +10,12 @@ extern VConfigManager *g_config; VOrphanFile::VOrphanFile(const QString &p_path, QObject *p_parent, bool p_modifiable, bool p_systemFile) - : VFile(VUtils::fileNameFromPath(p_path), p_parent, FileType::Orphan, p_modifiable), + : VFile(p_parent, + VUtils::fileNameFromPath(p_path), + FileType::Orphan, + p_modifiable, + QDateTime(), + QDateTime()), m_path(p_path), m_notebookName(tr("[EXTERNAL]")), m_systemFile(p_systemFile) { qDebug() << "VOrphanFile" << p_path << m_name << p_modifiable; diff --git a/src/vorphanfile.h b/src/vorphanfile.h index 4deb3a8e..bd67ab0e 100644 --- a/src/vorphanfile.h +++ b/src/vorphanfile.h @@ -45,6 +45,13 @@ private: void setContent(const QString &p_content) Q_DECL_OVERRIDE; bool isInternalImageFolder(const QString &p_path) const Q_DECL_OVERRIDE; + static VFile *fromJson(const QJsonObject &p_json, + QObject *p_parent, + FileType p_type, + bool p_modifiable); + + QJsonObject toConfigJson() const; + QString m_path; QString m_notebookName;