Warning, file /multimedia/kid3/src/core/tags/formatreplacer.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file formatreplacer.h
0003  * Replaces format codes in a string.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 06 Jul 2008
0008  *
0009  * Copyright (C) 2008-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QString>
0030 #include "kid3api.h"
0031 
0032 /**
0033  * Replaces format codes in a string.
0034  */
0035 class KID3_CORE_EXPORT FormatReplacer {
0036 public:
0037   /** Flags for replacePercentCodes(). */
0038   enum FormatStringFlags {
0039     FSF_SupportUrlEncode  = (1 << 0),
0040     FSF_ReplaceSeparators = (1 << 1),
0041     FSF_SupportHtmlEscape = (1 << 2)
0042   };
0043 
0044   /**
0045    * Constructor.
0046    *
0047    * @param str string with format codes
0048    */
0049   explicit FormatReplacer(const QString& str = QString());
0050 
0051   /**
0052    * Destructor.
0053    */
0054   virtual ~FormatReplacer();
0055 
0056   FormatReplacer(const FormatReplacer& other) = delete;
0057   FormatReplacer &operator=(const FormatReplacer& other) = delete;
0058 
0059   /**
0060    * Set string with format codes.
0061    * @param str string with format codes
0062    */
0063   void setString(const QString& str) { m_str = str; }
0064 
0065   /**
0066    * Get string.
0067    * The string set with setString() can be modified using
0068    * replaceEscapedChars() and replacePercentCodes().
0069    * @return string.
0070    */
0071   QString getString() const { return m_str; }
0072 
0073   /**
0074    * Replace escaped characters.
0075    * Replaces the escaped characters ("\n", "\t", "\r", "\\", "\a", "\b",
0076    * "\f", "\v") with the corresponding characters.
0077    */
0078   void replaceEscapedChars();
0079 
0080   /**
0081    * Replace percent codes.
0082    *
0083    * @param flags flags: FSF_SupportUrlEncode to support modifier u
0084    *              (with code c "%uc") to URL encode,
0085    *              FSF_ReplaceSeparators to replace directory separators
0086    *              ('/', '\\', ':') in tags,
0087    *              FSF_SupportHtmlEscape to support modifier h
0088    *              (with code c "%hc") to replace HTML metacharacters
0089    *              ('<', '>', '&', '"', ''', non-ascii) in tags.
0090    */
0091   void replacePercentCodes(unsigned flags = 0);
0092 
0093   /**
0094    * Converts the plain text string @a plain to a HTML string with
0095    * HTML metacharacters replaced by HTML entities.
0096    * @param plain plain text
0097    * @return html text with HTML entities.
0098    */
0099   static QString escapeHtml(const QString& plain);
0100 
0101 protected:
0102   /**
0103    * Replace a format code (one character %c or multiple characters %{chars}).
0104    *
0105    * @param code format code
0106    *
0107    * @return replacement string,
0108    *         QString::null if code not found.
0109    */
0110   virtual QString getReplacement(const QString& code) const = 0;
0111 
0112 private:
0113   QString m_str;
0114 };