recursive import external folders (#332)

* recursive import external folders

* rollback translation

* fix when path does not exist

* add blank line
This commit is contained in:
Xinjun Ma 2018-08-05 18:15:26 +08:00 committed by Le Tan
parent 19bd159aba
commit 09c610e3a9
5 changed files with 71 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/VNote.pro.user

View File

@ -14,7 +14,7 @@ VNewNotebookDialog::VNewNotebookDialog(const QString &title, const QString &info
QWidget *parent) QWidget *parent)
: QDialog(parent), : QDialog(parent),
defaultName(defaultName), defaultPath(defaultPath), defaultName(defaultName), defaultPath(defaultPath),
m_importNotebook(false), m_manualPath(false), m_manualName(false), m_importNotebook(false), m_importExternalProject(false), m_manualPath(false), m_manualName(false),
m_notebooks(p_notebooks) m_notebooks(p_notebooks)
{ {
setupUI(title, info); setupUI(title, info);
@ -185,6 +185,11 @@ bool VNewNotebookDialog::isImportExistingNotebook() const
return m_importNotebook; return m_importNotebook;
} }
bool VNewNotebookDialog::isImportExternalProject() const
{
return m_importExternalProject;
}
void VNewNotebookDialog::showEvent(QShowEvent *event) void VNewNotebookDialog::showEvent(QShowEvent *event)
{ {
m_nameEdit->setFocus(); m_nameEdit->setFocus();
@ -227,20 +232,25 @@ void VNewNotebookDialog::handleInputChanged()
} else if (QFileInfo::exists(path)) { } else if (QFileInfo::exists(path)) {
QDir dir(path); QDir dir(path);
QStringList files = dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden); QStringList files = dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
if (files.isEmpty()) { if (!files.isEmpty()) {
pathOk = true;
} else {
// Folder is not empty. // Folder is not empty.
configExist = VConfigManager::directoryConfigExist(path); configExist = VConfigManager::directoryConfigExist(path);
if (configExist) { if (configExist) {
pathOk = true;
m_warnLabel->setText(infoText); m_warnLabel->setText(infoText);
} else { } else {
QString warnText = tr("<span style=\"%1\">WARNING</span>: The folder chosen is NOT empty! "
"VNote will try to create a new notebook and import existing files.")
.arg(g_config->c_warningTextStyle);
m_warnLabel->setText(warnText); m_warnLabel->setText(warnText);
m_importExternalProject = true;
// If ok button is clicked, automatically create a configuration file
configExist = true;
} }
showWarnLabel = true; showWarnLabel = true;
} }
pathOk = true;
} else { } else {
pathOk = true; pathOk = true;
} }

View File

@ -27,6 +27,9 @@ public:
// Whether import existing notebook by reading the config file. // Whether import existing notebook by reading the config file.
bool isImportExistingNotebook() const; bool isImportExistingNotebook() const;
// Whether import external project
bool isImportExternalProject() const;
// Get the custom image folder for this notebook. // Get the custom image folder for this notebook.
// Empty string indicates using global config. // Empty string indicates using global config.
QString getImageFolder() const; QString getImageFolder() const;
@ -72,6 +75,9 @@ private:
// Whether import existing notebook config file. // Whether import existing notebook config file.
bool m_importNotebook; bool m_importNotebook;
// Whether import external project
bool m_importExternalProject;
// True if user has change the content of the path edit. // True if user has change the content of the path edit.
bool m_manualPath; bool m_manualPath;

View File

@ -1,6 +1,7 @@
#include "vnotebookselector.h" #include "vnotebookselector.h"
#include <QDebug> #include <QDebug>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonArray>
#include <QListWidget> #include <QListWidget>
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
@ -118,6 +119,46 @@ int VNotebookSelector::itemIndexOfNotebook(const VNotebook *p_notebook) const
return -1; return -1;
} }
void VNotebookSelector::createConfigFiles(const QString &p_path)
{
QDir root(p_path);
QStringList filters;
filters << "*.md" << "*.markdown";
QJsonObject dirJson;
dirJson[DirConfig::c_version] = "1";
dirJson[DirConfig::c_createdTime] = QDateTime::currentDateTime().toString(Qt::ISODate);
QJsonArray subDirs;
QJsonArray files;
QFileInfoList dirInfoList = root.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList fileInfoList = root.entryInfoList(filters,QDir::Files);
for(const QFileInfo &dirInfo : dirInfoList) {
const QString dirname = dirInfo.fileName();
if (dirname != "_v_recycle_bin") {
QJsonObject item;
item[DirConfig::c_name] = dirname;
subDirs.append(item);
createConfigFiles(dirInfo.absoluteFilePath());
}
}
for(const QFileInfo &fileInfo : fileInfoList) {
QJsonObject item;
item[DirConfig::c_createdTime] = fileInfo.created().toString(Qt::ISODate);
item[DirConfig::c_name] = fileInfo.fileName();
files.append(item);
}
dirJson[DirConfig::c_subDirectories] = subDirs;
dirJson[DirConfig::c_files] = files;
g_config->writeDirectoryConfig(p_path,dirJson);
}
void VNotebookSelector::insertAddNotebookItem() void VNotebookSelector::insertAddNotebookItem()
{ {
QListWidgetItem *item = new QListWidgetItem(); QListWidgetItem *item = new QListWidgetItem();
@ -199,6 +240,11 @@ bool VNotebookSelector::newNotebook()
m_notebooks, m_notebooks,
this); this);
if (dialog.exec() == QDialog::Accepted) { if (dialog.exec() == QDialog::Accepted) {
if(dialog.isImportExternalProject()) {
createConfigFiles(dialog.getPathInput());
}
createNotebook(dialog.getNameInput(), createNotebook(dialog.getNameInput(),
dialog.getPathInput(), dialog.getPathInput(),
dialog.isImportExistingNotebook(), dialog.isImportExistingNotebook(),

View File

@ -71,6 +71,9 @@ private:
// Return the item index of @p_notebook. // Return the item index of @p_notebook.
int itemIndexOfNotebook(const VNotebook *p_notebook) const; int itemIndexOfNotebook(const VNotebook *p_notebook) const;
// Recursively create config files
void createConfigFiles(const QString &p_path);
// If @p_import is true, we will use the existing config file. // If @p_import is true, we will use the existing config file.
// If @p_imageFolder is empty, we will use the global one. // If @p_imageFolder is empty, we will use the global one.
// If @p_attachmentFolder is empty, we will use the global one. // If @p_attachmentFolder is empty, we will use the global one.