File indexing completed on 2024-05-19 04:56:00
0001 /** 0002 * \file commandformatreplacer.cpp 0003 * Replaces context command format codes in a string. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 09 Aug 2011 0008 * 0009 * Copyright (C) 2011-2024 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 #include "commandformatreplacer.h" 0028 #include <QStringList> 0029 #include <QUrl> 0030 #include <QDir> 0031 #include <QCoreApplication> 0032 #include "networkconfig.h" 0033 #include "config.h" 0034 #include "loadtranslation.h" 0035 0036 /** 0037 * Constructor. 0038 * 0039 * @param frames frame collection 0040 * @param str string with format codes 0041 * @param files file list 0042 * @param isDir true if directory 0043 */ 0044 CommandFormatReplacer::CommandFormatReplacer( 0045 const FrameCollection& frames, const QString& str, 0046 const QStringList& files, bool isDir) 0047 : FrameFormatReplacer(frames, str), m_files(files), m_isDir(isDir) {} 0048 0049 /** 0050 * Replace a format code (one character %c or multiple characters %{chars}). 0051 * Supported format fields: 0052 * Those supported by FrameFormatReplacer::getReplacement() 0053 * %f %{file} filename 0054 * %d %{directory} directory name 0055 * %b %{browser} the web browser set in the configuration 0056 * %q %{qmlpath} base directory for QML files 0057 * 0058 * @param code format code 0059 * 0060 * @return replacement string, 0061 * QString::null if code not found. 0062 */ 0063 QString CommandFormatReplacer::getReplacement(const QString& code) const 0064 { 0065 QString result = FrameFormatReplacer::getReplacement(code); 0066 if (result.isNull()) { 0067 QString name; 0068 0069 if (code.length() == 1) { 0070 static const struct { 0071 const char* longCode; 0072 char shortCode; 0073 } shortToLong[] = { 0074 { "file", 'f' }, 0075 { "directory", 'd' }, 0076 { "browser", 'b' }, 0077 { "qmlpath", 'q' } 0078 }; 0079 const char c = code[0].toLatin1(); 0080 for (const auto& [longCode, shortCode] : shortToLong) { 0081 if (shortCode == c) { 0082 name = QString::fromLatin1(longCode); 0083 break; 0084 } 0085 } 0086 } else if (code.length() > 1) { 0087 name = code; 0088 } 0089 0090 if (!name.isNull()) { 0091 if (name == QLatin1String("file")) { 0092 if (!m_files.isEmpty()) { 0093 result = m_files.front(); 0094 } 0095 } else if (name == QLatin1String("directory")) { 0096 if (!m_files.isEmpty()) { 0097 result = m_files.front(); 0098 if (!m_isDir) { 0099 int sepPos = result.lastIndexOf(QLatin1Char('/')); 0100 if (sepPos < 0) { 0101 sepPos = result.lastIndexOf(QDir::separator()); 0102 } 0103 if (sepPos >= 0) { 0104 result.truncate(sepPos); 0105 } 0106 } 0107 } 0108 } else if (name == QLatin1String("browser")) { 0109 result = NetworkConfig::instance().browser(); 0110 #ifdef CFG_QMLDIR 0111 } else if (name == QLatin1String("qmlpath")) { 0112 result = QLatin1String(CFG_QMLDIR); 0113 Utils::prependApplicationDirPathIfRelative(result); 0114 #endif 0115 } else if (name == QLatin1String("url")) { 0116 if (!m_files.empty()) { 0117 QUrl url; 0118 url.setScheme(QLatin1String("file")); 0119 url.setPath(m_files.front()); 0120 result = url.toString(); 0121 } 0122 } 0123 } 0124 } 0125 0126 return result; 0127 } 0128 0129 /** 0130 * Get help text for supported format codes. 0131 * 0132 * @param onlyRows if true only the tr elements are returned, 0133 * not the surrounding table 0134 * 0135 * @return help text. 0136 */ 0137 QString CommandFormatReplacer::getToolTip(bool onlyRows) 0138 { 0139 QString str; 0140 if (!onlyRows) str += QLatin1String("<table>\n"); 0141 str += FrameFormatReplacer::getToolTip(true); 0142 0143 str += QLatin1String("<tr><td>%f</td><td>%{file}</td><td>"); 0144 str += QCoreApplication::translate("@default", "Filename"); 0145 str += QLatin1String("</td></tr>\n"); 0146 0147 str += QLatin1String("<tr><td>%F</td><td>%{files}</td><td>"); 0148 const char* const filenamesStr = QT_TRANSLATE_NOOP("@default", "Filenames"); 0149 str += QCoreApplication::translate("@default", filenamesStr); 0150 str += QLatin1String("</td></tr>\n"); 0151 0152 str += QLatin1String("<tr><td>%uf</td><td>%{url}</td><td>"); 0153 str += QCoreApplication::translate("@default", "URL"); 0154 str += QLatin1String("</td></tr>\n"); 0155 0156 str += QLatin1String("<tr><td>%uF</td><td>%{urls}</td><td>"); 0157 const char* const urlsStr = QT_TRANSLATE_NOOP("@default", "URLs"); 0158 str += QCoreApplication::translate("@default", urlsStr); 0159 str += QLatin1String("</td></tr>\n"); 0160 0161 str += QLatin1String("<tr><td>%d</td><td>%{directory}</td><td>"); 0162 const char* const directoryNameStr = 0163 QT_TRANSLATE_NOOP("@default", "Folder name"); 0164 str += QCoreApplication::translate("@default", directoryNameStr); 0165 str += QLatin1String("</td></tr>\n"); 0166 0167 str += QLatin1String("<tr><td>%b</td><td>%{browser}</td><td>"); 0168 str += QCoreApplication::translate("@default", "Browser"); 0169 str += QLatin1String("</td></tr>\n"); 0170 0171 #ifdef CFG_QMLDIR 0172 str += QLatin1String("<tr><td>%q</td><td>%{qmlpath}</td><td>"); 0173 const char* const qmlBaseDirectoryStr = 0174 QT_TRANSLATE_NOOP("@default", "QML base folder"); 0175 str += QCoreApplication::translate("@default", qmlBaseDirectoryStr); 0176 str += QLatin1String("</td></tr>\n"); 0177 #endif 0178 0179 str += QLatin1String("<tr><td>%ua...</td><td>%u{artist}...</td><td>"); 0180 const char* const encodeAsUrlStr = 0181 QT_TRANSLATE_NOOP("@default", "Encode as URL"); 0182 str += QCoreApplication::translate("@default", encodeAsUrlStr); 0183 str += QLatin1String("</td></tr>\n"); 0184 0185 str += QLatin1String("<tr><td></td><td>@separator</td><td>"); 0186 const char* const separatorStr = 0187 QT_TRANSLATE_NOOP("@default", "--- separator ---"); 0188 str += QCoreApplication::translate("@default", separatorStr); 0189 str += QLatin1String("</td></tr>\n"); 0190 0191 str += QLatin1String("<tr><td></td><td>@beginmenu</td><td>"); 0192 const char* const beginOfSubmenuStr = 0193 QT_TRANSLATE_NOOP("@default", "Begin of submenu"); 0194 str += QCoreApplication::translate("@default", beginOfSubmenuStr); 0195 str += QLatin1String("</td></tr>\n"); 0196 0197 str += QLatin1String("<tr><td></td><td>@endmenu</td><td>"); 0198 const char* const endOfSubmenuStr = 0199 QT_TRANSLATE_NOOP("@default", "End of submenu"); 0200 str += QCoreApplication::translate("@default", endOfSubmenuStr); 0201 str += QLatin1String("</td></tr>\n"); 0202 0203 if (!onlyRows) str += QLatin1String("</table>\n"); 0204 return str; 0205 }