mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
add created_time and modified_time to file
This commit is contained in:
parent
7a384b1592
commit
ae706fb066
@ -10,7 +10,7 @@ extern VConfigManager *g_config;
|
|||||||
VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info,
|
VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info,
|
||||||
VDirectory *directory, const VFile *file,
|
VDirectory *directory, const VFile *file,
|
||||||
QWidget *parent)
|
QWidget *parent)
|
||||||
: QDialog(parent), infoLabel(NULL), title(title), info(info),
|
: QDialog(parent), title(title), info(info),
|
||||||
m_directory(directory), m_file(file)
|
m_directory(directory), m_file(file)
|
||||||
{
|
{
|
||||||
setupUI();
|
setupUI();
|
||||||
@ -22,13 +22,38 @@ VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info,
|
|||||||
|
|
||||||
void VFileInfoDialog::setupUI()
|
void VFileInfoDialog::setupUI()
|
||||||
{
|
{
|
||||||
|
QLabel *infoLabel = NULL;
|
||||||
if (!info.isEmpty()) {
|
if (!info.isEmpty()) {
|
||||||
infoLabel = new QLabel(info);
|
infoLabel = new QLabel(info);
|
||||||
}
|
}
|
||||||
nameLabel = new QLabel(tr("Note &name:"));
|
|
||||||
nameEdit = new QLineEdit(m_file->getName());
|
// File name.
|
||||||
nameEdit->selectAll();
|
QString name = m_file->getName();
|
||||||
nameLabel->setBuddy(nameEdit);
|
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 = new QLabel();
|
||||||
m_warnLabel->setWordWrap(true);
|
m_warnLabel->setWordWrap(true);
|
||||||
@ -39,10 +64,6 @@ void VFileInfoDialog::setupUI()
|
|||||||
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||||
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
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);
|
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
||||||
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
|
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
|
||||||
|
|
||||||
|
@ -25,8 +25,6 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
void setupUI();
|
void setupUI();
|
||||||
|
|
||||||
QLabel *infoLabel;
|
|
||||||
QLabel *nameLabel;
|
|
||||||
QLineEdit *nameEdit;
|
QLineEdit *nameEdit;
|
||||||
QLabel *m_warnLabel;
|
QLabel *m_warnLabel;
|
||||||
QDialogButtonBox *m_btnBox;
|
QDialogButtonBox *m_btnBox;
|
||||||
|
@ -148,7 +148,10 @@ int main(int argc, char *argv[])
|
|||||||
g_config = &vconfig;
|
g_config = &vconfig;
|
||||||
|
|
||||||
QString locale = VUtils::getLocale();
|
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
|
// load translation for Qt
|
||||||
QTranslator qtTranslator;
|
QTranslator qtTranslator;
|
||||||
@ -161,7 +164,6 @@ int main(int argc, char *argv[])
|
|||||||
// load translation for vnote
|
// load translation for vnote
|
||||||
QTranslator translator;
|
QTranslator translator;
|
||||||
if (translator.load("vnote_" + locale, ":/translations")) {
|
if (translator.load("vnote_" + locale, ":/translations")) {
|
||||||
qDebug() << "install VNote translator";
|
|
||||||
app.installTranslator(&translator);
|
app.installTranslator(&translator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,8 @@ namespace DirConfig
|
|||||||
static const QString c_files = "files";
|
static const QString c_files = "files";
|
||||||
static const QString c_imageFolder = "image_folder";
|
static const QString c_imageFolder = "image_folder";
|
||||||
static const QString c_name = "name";
|
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]";
|
static const QString c_emptyHeaderName = "[EMPTY]";
|
||||||
|
@ -43,7 +43,10 @@ bool VDirectory::open()
|
|||||||
QJsonArray fileJson = configJson[DirConfig::c_files].toArray();
|
QJsonArray fileJson = configJson[DirConfig::c_files].toArray();
|
||||||
for (int i = 0; i < fileJson.size(); ++i) {
|
for (int i = 0; i < fileJson.size(); ++i) {
|
||||||
QJsonObject fileItem = fileJson[i].toObject();
|
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);
|
m_files.append(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,11 +126,9 @@ QJsonObject VDirectory::toConfigJson() const
|
|||||||
|
|
||||||
QJsonArray files;
|
QJsonArray files;
|
||||||
for (int i = 0; i < m_files.size(); ++i) {
|
for (int i = 0; i < m_files.size(); ++i) {
|
||||||
QJsonObject item;
|
files.append(m_files[i]->toConfigJson());
|
||||||
item[DirConfig::c_name] = m_files[i]->getName();
|
|
||||||
|
|
||||||
files.append(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dirJson[DirConfig::c_files] = files;
|
dirJson[DirConfig::c_files] = files;
|
||||||
|
|
||||||
return dirJson;
|
return dirJson;
|
||||||
@ -272,7 +273,13 @@ VFile *VDirectory::createFile(const QString &p_name)
|
|||||||
|
|
||||||
file.close();
|
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);
|
m_files.append(ret);
|
||||||
if (!writeToConfig()) {
|
if (!writeToConfig()) {
|
||||||
file.remove();
|
file.remove();
|
||||||
@ -321,7 +328,13 @@ VFile *VDirectory::addFile(const QString &p_name, int p_index)
|
|||||||
return NULL;
|
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) {
|
if (!file) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ public:
|
|||||||
bool removeSubDirectory(VDirectory *p_dir);
|
bool removeSubDirectory(VDirectory *p_dir);
|
||||||
|
|
||||||
// Add the file in the config and m_files. If @p_index is -1, add it at the end.
|
// 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.
|
// Return the VFile if succeed.
|
||||||
VFile *addFile(const QString &p_name, int p_index);
|
VFile *addFile(const QString &p_name, int p_index);
|
||||||
|
|
||||||
|
@ -6,11 +6,21 @@
|
|||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include "utils/vutils.h"
|
#include "utils/vutils.h"
|
||||||
|
|
||||||
VFile::VFile(const QString &p_name, QObject *p_parent,
|
VFile::VFile(QObject *p_parent,
|
||||||
FileType p_type, bool p_modifiable)
|
const QString &p_name,
|
||||||
: QObject(p_parent), m_name(p_name), m_opened(false), m_modified(false),
|
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_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);
|
Q_ASSERT(m_opened);
|
||||||
bool ret = VUtils::writeFileToDisk(fetchPath(), m_content);
|
bool ret = VUtils::writeFileToDisk(fetchPath(), m_content);
|
||||||
|
if (ret) {
|
||||||
|
m_modifiedTimeUtc = QDateTime::currentDateTimeUtc();
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,3 +274,28 @@ QString VFile::getImageFolderInLink() const
|
|||||||
{
|
{
|
||||||
return getNotebook()->getImageFolder();
|
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;
|
||||||
|
}
|
||||||
|
50
src/vfile.h
50
src/vfile.h
@ -4,6 +4,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <QDateTime>
|
||||||
#include "vdirectory.h"
|
#include "vdirectory.h"
|
||||||
#include "vconstants.h"
|
#include "vconstants.h"
|
||||||
|
|
||||||
@ -13,8 +14,13 @@ class VFile : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
VFile(const QString &p_name, QObject *p_parent,
|
VFile(QObject *p_parent,
|
||||||
FileType p_type = FileType::Normal, bool p_modifiable = true);
|
const QString &p_name,
|
||||||
|
FileType p_type,
|
||||||
|
bool p_modifiable,
|
||||||
|
QDateTime p_createdTimeUtc,
|
||||||
|
QDateTime p_modifiedTimeUtc);
|
||||||
|
|
||||||
virtual ~VFile();
|
virtual ~VFile();
|
||||||
virtual bool open();
|
virtual bool open();
|
||||||
virtual void close();
|
virtual void close();
|
||||||
@ -59,6 +65,19 @@ public:
|
|||||||
// Return the image folder part in an image link.
|
// Return the image folder part in an image link.
|
||||||
virtual QString getImageFolderInLink() const;
|
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:
|
public slots:
|
||||||
void setModified(bool p_modified);
|
void setModified(bool p_modified);
|
||||||
|
|
||||||
@ -69,16 +88,43 @@ protected:
|
|||||||
// Delete local images of DocType::Markdown.
|
// Delete local images of DocType::Markdown.
|
||||||
void deleteLocalImages();
|
void deleteLocalImages();
|
||||||
|
|
||||||
|
// Name of this file.
|
||||||
QString m_name;
|
QString m_name;
|
||||||
|
|
||||||
|
// Whether this file has been opened.
|
||||||
bool m_opened;
|
bool m_opened;
|
||||||
|
|
||||||
// File has been modified in editor
|
// File has been modified in editor
|
||||||
bool m_modified;
|
bool m_modified;
|
||||||
|
|
||||||
|
// DocType of this file: Html, Markdown.
|
||||||
DocType m_docType;
|
DocType m_docType;
|
||||||
|
|
||||||
|
// Content of this file.
|
||||||
QString m_content;
|
QString m_content;
|
||||||
|
|
||||||
FileType m_type;
|
FileType m_type;
|
||||||
|
|
||||||
|
// Whether this file is modifiable.
|
||||||
bool m_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;
|
friend class VDirectory;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline QDateTime VFile::getCreatedTimeUtc() const
|
||||||
|
{
|
||||||
|
return m_createdTimeUtc;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QDateTime VFile::getModifiedTimeUtc() const
|
||||||
|
{
|
||||||
|
return m_modifiedTimeUtc;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // VFILE_H
|
#endif // VFILE_H
|
||||||
|
@ -10,7 +10,12 @@ extern VConfigManager *g_config;
|
|||||||
|
|
||||||
VOrphanFile::VOrphanFile(const QString &p_path, QObject *p_parent,
|
VOrphanFile::VOrphanFile(const QString &p_path, QObject *p_parent,
|
||||||
bool p_modifiable, bool p_systemFile)
|
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)
|
m_path(p_path), m_notebookName(tr("[EXTERNAL]")), m_systemFile(p_systemFile)
|
||||||
{
|
{
|
||||||
qDebug() << "VOrphanFile" << p_path << m_name << p_modifiable;
|
qDebug() << "VOrphanFile" << p_path << m_name << p_modifiable;
|
||||||
|
@ -45,6 +45,13 @@ private:
|
|||||||
void setContent(const QString &p_content) Q_DECL_OVERRIDE;
|
void setContent(const QString &p_content) Q_DECL_OVERRIDE;
|
||||||
bool isInternalImageFolder(const QString &p_path) const 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_path;
|
||||||
|
|
||||||
QString m_notebookName;
|
QString m_notebookName;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user