add created_time and modified_time to file

This commit is contained in:
Le Tan 2017-09-13 21:51:34 +08:00
parent 7a384b1592
commit ae706fb066
10 changed files with 161 additions and 27 deletions

View File

@ -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);

View File

@ -25,8 +25,6 @@ private slots:
private:
void setupUI();
QLabel *infoLabel;
QLabel *nameLabel;
QLineEdit *nameEdit;
QLabel *m_warnLabel;
QDialogButtonBox *m_btnBox;

View File

@ -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);
}

View File

@ -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]";

View File

@ -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;
}

View File

@ -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);

View File

@ -6,11 +6,21 @@
#include <QFileInfo>
#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;
}

View File

@ -4,6 +4,7 @@
#include <QObject>
#include <QString>
#include <QUrl>
#include <QDateTime>
#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

View File

@ -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;

View File

@ -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;