1 /**
2  * \file formatreplacer.h
3  * Replaces format codes in a string.
4  *
5  * \b Project: Kid3
6  * \author Urs Fleisch
7  * \date 06 Jul 2008
8  *
9  * Copyright (C) 2008-2018  Urs Fleisch
10  *
11  * This file is part of Kid3.
12  *
13  * Kid3 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * Kid3 is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #pragma once
28 
29 #include <QString>
30 #include "kid3api.h"
31 
32 /**
33  * Replaces format codes in a string.
34  */
35 class KID3_CORE_EXPORT FormatReplacer {
36 public:
37   /** Flags for replacePercentCodes(). */
38   enum FormatStringFlags {
39     FSF_SupportUrlEncode  = (1 << 0),
40     FSF_ReplaceSeparators = (1 << 1),
41     FSF_SupportHtmlEscape = (1 << 2)
42   };
43 
44   /**
45    * Constructor.
46    *
47    * @param str string with format codes
48    */
49   explicit FormatReplacer(const QString& str = QString());
50 
51   /**
52    * Destructor.
53    */
54   virtual ~FormatReplacer();
55 
56   FormatReplacer(const FormatReplacer& other) = delete;
57   FormatReplacer &operator=(const FormatReplacer& other) = delete;
58 
59   /**
60    * Set string with format codes.
61    * @param str string with format codes
62    */
setString(const QString & str)63   void setString(const QString& str) { m_str = str; }
64 
65   /**
66    * Get string.
67    * The string set with setString() can be modified using
68    * replaceEscapedChars() and replacePercentCodes().
69    * @return string.
70    */
getString()71   QString getString() const { return m_str; }
72 
73   /**
74    * Replace escaped characters.
75    * Replaces the escaped characters ("\n", "\t", "\r", "\\", "\a", "\b",
76    * "\f", "\v") with the corresponding characters.
77    */
78   void replaceEscapedChars();
79 
80   /**
81    * Replace percent codes.
82    *
83    * @param flags flags: FSF_SupportUrlEncode to support modifier u
84    *              (with code c "%uc") to URL encode,
85    *              FSF_ReplaceSeparators to replace directory separators
86    *              ('/', '\\', ':') in tags,
87    *              FSF_SupportHtmlEscape to support modifier h
88    *              (with code c "%hc") to replace HTML metacharacters
89    *              ('<', '>', '&', '"', ''', non-ascii) in tags.
90    */
91   void replacePercentCodes(unsigned flags = 0);
92 
93   /**
94    * Converts the plain text string @a plain to a HTML string with
95    * HTML metacharacters replaced by HTML entities.
96    * @param plain plain text
97    * @return html text with HTML entities.
98    */
99   static QString escapeHtml(const QString& plain);
100 
101 protected:
102   /**
103    * Replace a format code (one character %c or multiple characters %{chars}).
104    *
105    * @param code format code
106    *
107    * @return replacement string,
108    *         QString::null if code not found.
109    */
110   virtual QString getReplacement(const QString& code) const = 0;
111 
112 private:
113   QString m_str;
114 };
115