libquentier  0.4.0
The library for rich desktop clients of Evernote service
ENMLConverter.h
1 /*
2  * Copyright 2016 Dmitry Ivanov
3  *
4  * This file is part of libquentier
5  *
6  * libquentier is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * libquentier is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libquentier. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef LIB_QUENTIER_ENML_ENML_CONVERTER_H
20 #define LIB_QUENTIER_ENML_ENML_CONVERTER_H
21 
22 #include <quentier/utility/Printable.h>
23 #include <quentier/utility/Linkage.h>
24 #include <quentier/utility/Macros.h>
25 #include <quentier/types/ErrorString.h>
26 #include <quentier/types/Note.h>
27 #include <QSet>
28 #include <QString>
29 #include <QHash>
30 #include <QTextDocument>
31 
32 namespace quentier {
33 
34 QT_FORWARD_DECLARE_CLASS(Resource)
35 QT_FORWARD_DECLARE_CLASS(DecryptedTextManager)
36 QT_FORWARD_DECLARE_CLASS(ENMLConverterPrivate)
37 
38 
43 class QUENTIER_EXPORT ENMLConverter
44 {
45 public:
46  ENMLConverter();
47  virtual ~ENMLConverter();
48 
58  class QUENTIER_EXPORT SkipHtmlElementRule: public Printable
59  {
60  public:
61  enum ComparisonRule {
62  Equals = 0,
63  StartsWith,
64  EndsWith,
65  Contains
66  };
67 
69  m_elementNameToSkip(),
70  m_elementNameComparisonRule(Equals),
71  m_elementNameCaseSensitivity(Qt::CaseSensitive),
72  m_attributeNameToSkip(),
73  m_attributeNameComparisonRule(Equals),
74  m_attributeNameCaseSensitivity(Qt::CaseSensitive),
75  m_attributeValueToSkip(),
76  m_attributeValueComparisonRule(Equals),
77  m_attributeValueCaseSensitivity(Qt::CaseSensitive),
78  m_includeElementContents(false)
79  {}
80 
81  virtual QTextStream & print(QTextStream & strm) const Q_DECL_OVERRIDE;
82 
83  QString m_elementNameToSkip;
84  ComparisonRule m_elementNameComparisonRule;
85  Qt::CaseSensitivity m_elementNameCaseSensitivity;
86 
87  QString m_attributeNameToSkip;
88  ComparisonRule m_attributeNameComparisonRule;
89  Qt::CaseSensitivity m_attributeNameCaseSensitivity;
90 
91  QString m_attributeValueToSkip;
92  ComparisonRule m_attributeValueComparisonRule;
93  Qt::CaseSensitivity m_attributeValueCaseSensitivity;
94 
95  bool m_includeElementContents;
96  };
97 
98  bool htmlToNoteContent(const QString & html, QString & noteContent,
99  DecryptedTextManager & decryptedTextManager,
100  ErrorString & errorDescription,
101  const QVector<SkipHtmlElementRule> & skipRules = QVector<SkipHtmlElementRule>()) const;
102 
115  bool cleanupExternalHtml(const QString & inputHtml, QString & cleanedUpHtml,
116  ErrorString & errorDescription) const;
117 
131  bool htmlToQTextDocument(const QString & html, QTextDocument & doc, ErrorString & errorDescription,
132  const QVector<SkipHtmlElementRule> & skipRules = QVector<SkipHtmlElementRule>()) const;
133 
135  {
136  quint64 m_numEnToDoNodes;
137  quint64 m_numHyperlinkNodes;
138  quint64 m_numEnCryptNodes;
139  quint64 m_numEnDecryptedNodes;
140  };
141 
142  bool noteContentToHtml(const QString & noteContent, QString & html, ErrorString & errorDescription,
143  DecryptedTextManager & decryptedTextManager, NoteContentToHtmlExtraData & extraData) const;
144 
145  bool validateEnml(const QString & enml, ErrorString & errorDescription) const;
146 
147  bool validateAndFixupEnml(QString & enml, ErrorString & errorDescription) const;
148 
149  static bool noteContentToPlainText(const QString & noteContent, QString & plainText,
150  ErrorString & errorMessage);
151 
152  static bool noteContentToListOfWords(const QString & noteContent, QStringList & listOfWords,
153  ErrorString & errorMessage, QString * plainText = Q_NULLPTR);
154 
155  static QStringList plainTextToListOfWords(const QString & plainText);
156 
157  static QString toDoCheckboxHtml(const bool checked, const quint64 idNumber);
158 
159  static QString encryptedTextHtml(const QString & encryptedText, const QString & hint,
160  const QString & cipher, const size_t keyLength,
161  const quint64 enCryptIndex);
162 
163  static QString decryptedTextHtml(const QString & decryptedText, const QString & encryptedText,
164  const QString & hint, const QString & cipher,
165  const size_t keyLength, const quint64 enDecryptedIndex);
166 
167  static QString resourceHtml(const Resource & resource, ErrorString & errorDescription);
168 
169  static void escapeString(QString & string, const bool simplify = true);
170 
176  {
177  enum type
178  {
179  Yes = 0,
180  No
181  };
182  };
183 
199  bool exportNotesToEnex(const QVector<Note> & notes, const QHash<QString, QString> & tagNamesByTagLocalUids,
200  const EnexExportTags::type exportTagsOption, QString & enex, ErrorString & errorDescription,
201  const QString & version = QString()) const;
202 
217  bool importEnex(const QString & enex, QVector<Note> & notes,
218  QHash<QString, QStringList> & tagNamesByNoteLocalUid,
219  ErrorString & errorDescription) const;
220 
221 private:
222  Q_DISABLE_COPY(ENMLConverter)
223 
224 private:
225  ENMLConverterPrivate * const d_ptr;
226  Q_DECLARE_PRIVATE(ENMLConverter)
227 };
228 
229 } // namespace quentier
230 
231 #endif // LIB_QUENTIER_ENML_ENML_CONVERTER_H
The ErrorString class encapsulates two (or more) strings which are meant to contain translatable (bas...
Definition: ErrorString.h:38
Definition: Resource.h:29
The EnexExportTags struct is a C++98 style scoped enum which allows to specify whether export of note...
Definition: ENMLConverter.h:175
The Printable class is the interface for Quentier's internal classes which should be able to write th...
Definition: Printable.h:54
Definition: DecryptedTextManager.h:29
The SkipHtmlElementRule class describes the set of rules for HTML -> ENML conversion about the HTML e...
Definition: ENMLConverter.h:58
The ENMLConverter class encapsulates a set of methods and helper data structures for performing the c...
Definition: ENMLConverter.h:43