Skip to content

Commit

Permalink
Disable xmlpatterns and co. for Qt-6 (#1304)
Browse files Browse the repository at this point in the history
* Disable xmlpatterns and co. for Qt-6

* No explicit redirect needed in qt6
  • Loading branch information
Waqar144 committed Apr 12, 2021
1 parent edd4074 commit ac0ef23
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/dialogs/attachmentdialog.cpp
Expand Up @@ -88,7 +88,7 @@ void AttachmentDialog::on_downloadButton_clicked() {
QUrl url(ui->fileEdit->text());
QNetworkRequest networkRequest(url);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/dictionarymanagerdialog.cpp
Expand Up @@ -253,7 +253,7 @@ void DictionaryManagerDialog::on_downloadButton_clicked() {
void DictionaryManagerDialog::downloadFile(const QString &url) {
QNetworkRequest networkRequest(url);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
#endif
Expand Down
211 changes: 111 additions & 100 deletions src/dialogs/evernoteimportdialog.cpp
Expand Up @@ -13,8 +13,12 @@
#include <QRegularExpressionMatchIterator>
#include <QSettings>
#include <QTemporaryFile>
#include <QXmlQuery>
#include <QXmlResultItems>

#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QXmlQuery>
#include <QXmlResultItems>
#endif

#include <utility>

#include "filedialog.h"
Expand Down Expand Up @@ -79,6 +83,22 @@ void EvernoteImportDialog::on_fileButton_clicked() {
}
}

/**
* Initializes the progress bar with the count of notes from the XML in data
*
* @param data
*/
void EvernoteImportDialog::initNoteCount(const QString &data) {
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
int count = countNotes(data);

ui->progressBar->setMaximum(count);
ui->progressBar->show();
#endif
}

/** Disabled Till we have an xmlpatterns alternative **/
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
/**
* Counts the notes from the XML in data
*
Expand All @@ -102,18 +122,6 @@ int EvernoteImportDialog::countNotes(const QString &data) {
return count;
}

/**
* Initializes the progress bar with the count of notes from the XML in data
*
* @param data
*/
void EvernoteImportDialog::initNoteCount(const QString &data) {
int count = countNotes(data);

ui->progressBar->setMaximum(count);
ui->progressBar->show();
}

/**
* Imports and embeds images from an Evernote note
*
Expand Down Expand Up @@ -414,51 +422,6 @@ QString EvernoteImportDialog::importAttachments(const Note &note,
return content;
}

/**
* Returns the markdown code for an image file data entry
*
* @param mediaFileData
* @return
*/
QString EvernoteImportDialog::getMarkdownForMediaFileData(
Note note, const EvernoteImportDialog::MediaFileData &mediaFileData) {
QString data = mediaFileData.data;
QString imageSuffix = mediaFileData.suffix;

return note.importMediaFromBase64(data, imageSuffix);
}

/**
* Returns the markdown code for an attachment file data entry
*
* @param mediaFileData
* @return
*/
QString EvernoteImportDialog::getMarkdownForAttachmentFileData(
Note note, const EvernoteImportDialog::MediaFileData &mediaFileData) {
QString data = mediaFileData.data;
QString suffix = mediaFileData.suffix;
QString fileName = mediaFileData.fileName;

// create a temporary file for the attachment
auto *tempFile =
new QTemporaryFile(QDir::tempPath() + QDir::separator() +
QStringLiteral("media-XXXXXX.") + suffix);

if (!tempFile->open()) {
return QString();
}

// write file data to the temporary file
tempFile->write(QByteArray::fromBase64(data.toLatin1()));

// store the temporary file in the media folder and return the
// markdown code
QString markdownCode = note.getInsertAttachmentMarkdown(tempFile, fileName);

return markdownCode;
}

/**
* Imports the notes from the XML in data
*
Expand Down Expand Up @@ -630,6 +593,48 @@ void EvernoteImportDialog::tagNote(QXmlQuery &query, Note &note) {
}
}

/**
* Generates the metadata markdown table for a note
*/
QString EvernoteImportDialog::generateMetaDataMarkdown(QXmlQuery query) {
QString resultText;
QString tableText;
QList<QTreeWidgetItem *> items = ui->metaDataTreeWidget->findItems(
QStringLiteral("*"),
Qt::MatchWrap | Qt::MatchWildcard | Qt::MatchRecursive);

Q_FOREACH (QTreeWidgetItem *item, items) {
if (item->checkState(0) != Qt::Checked) {
continue;
}

QString name = item->text(0);
QString attributeName = item->data(0, Qt::UserRole).toString();

QString attribute;
query.setQuery(attributeName + QStringLiteral("/text()"));
query.evaluateTo(&attribute);
attribute = attribute.trimmed();

if (attribute.isEmpty()) {
continue;
}

tableText += QStringLiteral("| ") + name + (" | ") + attribute +
QStringLiteral(" |\n");
}

if (!tableText.isEmpty()) {
resultText = "| " + tr("Attribute") + " | " + tr("Value") +
" |\n"
"|---|---|\n" +
tableText;
}

return resultText + QStringLiteral("\n");
}
#endif

/**
* Adds a metadata tree widget item
*
Expand Down Expand Up @@ -764,47 +769,6 @@ bool EvernoteImportDialog::isMetaDataChecked() {
return false;
}

/**
* Generates the metadata markdown table for a note
*/
QString EvernoteImportDialog::generateMetaDataMarkdown(QXmlQuery query) {
QString resultText;
QString tableText;
QList<QTreeWidgetItem *> items = ui->metaDataTreeWidget->findItems(
QStringLiteral("*"),
Qt::MatchWrap | Qt::MatchWildcard | Qt::MatchRecursive);

Q_FOREACH (QTreeWidgetItem *item, items) {
if (item->checkState(0) != Qt::Checked) {
continue;
}

QString name = item->text(0);
QString attributeName = item->data(0, Qt::UserRole).toString();

QString attribute;
query.setQuery(attributeName + QStringLiteral("/text()"));
query.evaluateTo(&attribute);
attribute = attribute.trimmed();

if (attribute.isEmpty()) {
continue;
}

tableText += QStringLiteral("| ") + name + (" | ") + attribute +
QStringLiteral(" |\n");
}

if (!tableText.isEmpty()) {
resultText = "| " + tr("Attribute") + " | " + tr("Value") +
" |\n"
"|---|---|\n" +
tableText;
}

return resultText + QStringLiteral("\n");
}

/**
* Reads the enex files and imports the notes
*/
Expand All @@ -826,10 +790,57 @@ void EvernoteImportDialog::on_importButton_clicked() {

QString data = file.readAll();

#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QCoreApplication::processEvents();
initNoteCount(data);
QCoreApplication::processEvents();
importNotes(data);
#endif

ui->importButton->setEnabled(true);
}

/**
* Returns the markdown code for an image file data entry
*
* @param mediaFileData
* @return
*/
QString EvernoteImportDialog::getMarkdownForMediaFileData(
Note note, const EvernoteImportDialog::MediaFileData &mediaFileData) {
QString data = mediaFileData.data;
QString imageSuffix = mediaFileData.suffix;

return note.importMediaFromBase64(data, imageSuffix);
}

/**
* Returns the markdown code for an attachment file data entry
*
* @param mediaFileData
* @return
*/
QString EvernoteImportDialog::getMarkdownForAttachmentFileData(
Note note, const EvernoteImportDialog::MediaFileData &mediaFileData) {
QString data = mediaFileData.data;
QString suffix = mediaFileData.suffix;
QString fileName = mediaFileData.fileName;

// create a temporary file for the attachment
auto *tempFile =
new QTemporaryFile(QDir::tempPath() + QDir::separator() +
QStringLiteral("media-XXXXXX.") + suffix);

if (!tempFile->open()) {
return QString();
}

// write file data to the temporary file
tempFile->write(QByteArray::fromBase64(data.toLatin1()));

// store the temporary file in the media folder and return the
// markdown code
QString markdownCode = note.getInsertAttachmentMarkdown(tempFile, fileName);

return markdownCode;
}
30 changes: 17 additions & 13 deletions src/dialogs/evernoteimportdialog.h
Expand Up @@ -34,34 +34,38 @@ class EvernoteImportDialog : public MasterDialog {
Ui::EvernoteImportDialog *ui;
int _importCount;

void importNotes(const QString &data);
void setupMetaDataTreeWidgetItems();

int countNotes(const QString &data);
void storeMetaDataTreeWidgetItemsCheckedState();

void initNoteCount(const QString &data);

QString importImages(const Note &note, QString content, QXmlQuery query);

QString getMarkdownForMediaFileData(Note note,
const MediaFileData &mediaFileData);

QString getMarkdownForAttachmentFileData(
Note note, const MediaFileData &mediaFileData);

void tagNote(QXmlQuery &query, Note &note);

QString importAttachments(const Note &note, QString content,
QXmlQuery query);

QTreeWidgetItem *addMetaDataTreeWidgetItem(
const QString &name, const QString &attributeName = QString(),
QTreeWidgetItem *parentItem = nullptr);

void setupMetaDataTreeWidgetItems();

bool isMetaDataChecked();

QString generateMetaDataMarkdown(QXmlQuery query);

void storeMetaDataTreeWidgetItemsCheckedState();
/** These require xml patterns **/
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
void importNotes(const QString &data);

int countNotes(const QString &data);

QString importImages(const Note &note, QString content, QXmlQuery query);

void tagNote(QXmlQuery &query, Note &note);

QString importAttachments(const Note &note, QString content,
QXmlQuery query);

QString generateMetaDataMarkdown(QXmlQuery query);
#endif
};
6 changes: 3 additions & 3 deletions src/dialogs/scriptrepositorydialog.cpp
Expand Up @@ -120,7 +120,7 @@ void ScriptRepositoryDialog::searchScript(int page) {
QNetworkRequest networkRequest(url);
_page = page;

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
#endif
Expand Down Expand Up @@ -152,7 +152,7 @@ void ScriptRepositoryDialog::searchForUpdates() {
QUrl url = script.repositoryInfoJsonUrl();
QNetworkRequest networkRequest(url);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
#endif
Expand Down Expand Up @@ -242,7 +242,7 @@ void ScriptRepositoryDialog::parseCodeSearchReply(const QByteArray &arr) {
QUrl url(_rawContentUrlPrefix + path);
QNetworkRequest networkRequest(url);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute,
true);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/updatedialog.cpp
Expand Up @@ -183,7 +183,7 @@ void UpdateDialog::dialogButtonClicked(QAbstractButton *button) {
QUrl url(releaseUrl);
QNetworkRequest networkRequest(url);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
// we really need redirects for GitHub urls!
networkRequest.setAttribute(
QNetworkRequest::FollowRedirectsAttribute, true);
Expand Down

0 comments on commit ac0ef23

Please sign in to comment.