File indexing completed on 2022-10-04 17:30:24

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "translitplugin.h"
0005 
0006 #include "batchrenamer.h"
0007 
0008 #include <QListWidget>
0009 #include <KLocalizedString>
0010 
0011 #include <QLabel>
0012 #include <QHBoxLayout>
0013 
0014 /*
0015 const QString TranslitPlugin::s_strUtf8[] = {"а","б","в","г","д","е","ё","ж","з","и",
0016     "й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь",
0017     "э","ю","я",
0018     "А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О","П",
0019     "Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я",
0020     "á","ä","č","ď","é","ě","í","ľ","ĺ","ň","ó","ô","ö","ő","ř","ŕ","š","ť","ú","ů","ü","ű","ý","ž",
0021     "Á","Ä","Č","Ď","É","Ě","Í","Ľ","Ĺ","Ň","Ó","Ô","Ö","Ő","Ř","Ŕ","Š","Ť","Ú","Ů","Ü","Ű","Ý","Ž",QString()};
0022 */
0023 const QString TranslitPlugin::s_strUtf8[] = {
0024     "\xD0\xB0", "\xD0\xB1", "\xD0\xB2",
0025     "\xD0\xB3", "\xD0\xB4", "\xD0\xB5",
0026     "\xD1\x91", "\xD0\xB6", "\xD0\xB7",
0027     "\xD0\xB8", "\xD0\xB9", "\xD0\xBA",
0028     "\xD0\xBB", "\xD0\xBC", "\xD0\xBD",
0029     "\xD0\xBE", "\xD0\xBF", "\xD1\x80",
0030     "\xD1\x81", "\xD1\x82", "\xD1\x83",
0031     "\xD1\x84", "\xD1\x85", "\xD1\x86",
0032     "\xD1\x87", "\xD1\x88", "\xD1\x89",
0033     "\xD1\x8A", "\xD1\x8B", "\xD1\x8C",
0034     "\xD1\x8D", "\xD1\x8E", "\xD1\x8F",
0035     "\xD0\x90", "\xD0\x91", "\xD0\x92",
0036     "\xD0\x93", "\xD0\x94", "\xD0\x95",
0037     "\xD0\x81", "\xD0\x96", "\xD0\x97",
0038     "\xD0\x98", "\xD0\x99", "\xD0\x9A",
0039     "\xD0\x9B", "\xD0\x9C", "\xD0\x9D",
0040     "\xD0\x9E", "\xD0\x9F", "\xD0\xA0",
0041     "\xD0\xA1", "\xD0\xA2", "\xD0\xA3",
0042     "\xD0\xA4", "\xD0\xA5", "\xD0\xA6",
0043     "\xD0\xA7", "\xD0\xA8", "\xD0\xA9",
0044     "\xD0\xAA", "\xD0\xAB", "\xD0\xAC",
0045     "\xD0\xAD", "\xD0\xAE", "\xD0\xAF",
0046     "\xC3\xA1", "\xC3\xA4", "\xC4\x8D",
0047     "\xC4\x8F", "\xC3\xA9", "\xC4\x9B",
0048     "\xC3\xAD", "\xC4\xBE", "\xC4\xBA",
0049     "\xC5\x88", "\xC3\xB3", "\xC3\xB4",
0050     "\xC3\xB6", "\xC5\x91", "\xC5\x99",
0051     "\xC5\x95", "\xC5\xA1", "\xC5\xA5",
0052     "\xC3\xBA", "\xC5\xAF", "\xC3\xBC",
0053     "\xC5\xB1", "\xC3\xBD", "\xC5\xBE",
0054     "\xC3\x81", "\xC3\x84", "\xC4\x8C",
0055     "\xC4\x8E", "\xC3\x89", "\xC4\x9A",
0056     "\xC3\x8D", "\xC4\xBD", "\xC4\xB9",
0057     "\xC5\x87", "\xC3\x93", "\xC3\x94",
0058     "\xC3\x96", "\xC5\x90", "\xC5\x94",
0059     "\xC5\xA0", "\xC5\xA4", "\xC3\x9A",
0060     "\xC5\xAE", "\xC3\x9C", "\xC5\xB0",
0061     "\xC3\x9D", "\xC5\xBD",  QString()
0062 };
0063 
0064 const QString TranslitPlugin::s_strEngl[] = {"a", "b", "v", "g", "d", "e", "yo", "zh", "z", "i",
0065                                              "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "f", "h", "c", "ch", "sh", "sh", "", "y", "",
0066                                              "e", "yu", "ya",
0067                                              "A", "B", "V", "G", "D", "E", "Yo", "Zh", "Z", "I", "J", "K", "L", "M", "N", "O", "P",
0068                                              "R", "S", "T", "U", "F", "H", "C", "Ch", "Sh", "Sh", "", "Y", "", "E", "Yu", "Ya",
0069                                              "a", "a", "c", "d", "e", "e", "i", "l", "l", "n", "o", "o", "o", "o", "r", "r", "s", "t", "u", "u", "u", "u", "y", "z",
0070                                              "A", "A", "C", "D", "E", "E", "I", "L", "L", "N", "O", "O", "O", "O", "R", "R", "S", "T", "U", "U", "U", "U", "Y", "Z", QString()
0071                                             };
0072 
0073 QHash<QString, QString> TranslitPlugin::s_mapFromUTF8;
0074 
0075 TranslitPlugin::TranslitPlugin(PluginLoader *loader)
0076     : FilePlugin(loader)
0077 {
0078     this->addSupportedToken("transliterated");
0079     this->addSupportedToken("transliterated;.*");
0080 
0081     m_help.append(Plugin::createHelpEntry("transliterated", i18n("Inserts the transliterated original filename")));
0082     m_help.append(Plugin::createHelpEntry("%transliterated", i18n("Inserts the transliterated original filename converted to lower case")));
0083     m_help.append(Plugin::createHelpEntry("&transliterated", i18n("Inserts the transliterated original filename converted to upper case")));
0084     m_help.append(Plugin::createHelpEntry("*transliterated", i18n("Inserts the transliterated original filename capitalized")));
0085     m_help.append(Plugin::createHelpEntry("transliterated;Some string", i18n("Transliterates the string following the semicolon")));
0086 
0087     m_name = i18n("Transliteration");
0088     m_icon = "applications-education-language";
0089     m_comment = i18n("<qt>This plugin can transcribe a filename "
0090                      "into English (i.e. perform transliteration).</qt>");
0091 
0092     if (TranslitPlugin::s_mapFromUTF8.isEmpty()) {
0093         int i = 0;
0094         while (! TranslitPlugin::s_strUtf8[i].isEmpty()) {
0095             TranslitPlugin::s_mapFromUTF8[ TranslitPlugin::s_strUtf8[i] ] = TranslitPlugin::s_strEngl[i];
0096 
0097             ++i;
0098         }
0099 
0100     }
0101 }
0102 
0103 TranslitPlugin::~TranslitPlugin()
0104 {
0105 
0106 }
0107 
0108 QString TranslitPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType eCurrentType)
0109 {
0110     QString src;
0111     QString token;
0112 
0113     // This plugin supports to types
0114     if (eCurrentType == ePluginType_Token) {
0115         if (filenameOrToken.contains(";")) {
0116             src   = filenameOrToken.section(';', 1, 1);
0117             token = filenameOrToken.section(';', 0, 0).toLower();
0118         } else {
0119             token = filenameOrToken.toLower();
0120         }
0121 
0122         if (token == "transliterated") {
0123             if (src.isEmpty()) {
0124                 // TODO: If extension .....
0125                 if (true) {
0126                     src = b->files()->at(index).srcFilename();
0127                 } else {
0128                     src = b->files()->at(index).srcExtension();
0129                 }
0130             }
0131 
0132             return this->transliterate(src);
0133         }
0134     }
0135     /*
0136     else if( eCurrentType == ePluginType_Filename )
0137     {
0138         // TODO: If extension .....
0139         if( true )
0140             src = b->files()->at( index ).srcFilename();
0141         else
0142             src = b->files()->at( index ).srcExtension();
0143 
0144         return this->transliterate( src );
0145     }
0146     */
0147     return QString();
0148 }
0149 
0150 /*
0151 void TranslitPlugin::createUI( QWidget* parent ) const
0152 {
0153     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Expanding );
0154 
0155     QVBoxLayout* l    = new QVBoxLayout( parent );
0156     QHBoxLayout* hbox = new QHBoxLayout( parent );
0157 
0158     QLabel* pix = new QLabel( parent );
0159     pix->setPixmap( KIconLoader::global()->loadIcon( m_icon, K3Icon::Desktop ) );
0160 
0161     hbox->addWidget( pix );
0162     hbox->addWidget( new QLabel( "<qt><b>"+name()+"</b></qt>", parent  ) );
0163     hbox->addItem( spacer );
0164 
0165     l->addLayout( hbox );
0166     l->addWidget( new QLabel( i18n("This plugin will transliterate the following characters."), parent  ) );
0167 
0168     QListWidget* list = new QListWidget( parent );
0169 
0170     int i=0;
0171     while( TranslitPlugin::s_strUtf8[i]!=QString() )
0172     {
0173         QString text = QString::fromUtf8( TranslitPlugin::s_strUtf8[i].toUtf8().data() );
0174         text += " -> ";
0175         text += TranslitPlugin::s_strEngl[i];;
0176 
0177         list->insertItem( 0, text );
0178 
0179         ++i;
0180     }
0181 
0182     l->addWidget( list );
0183     l->setStretchFactor( list, 2 );
0184 }
0185 */
0186 
0187 QString TranslitPlugin::transliterate(const QString &unicoded)
0188 {
0189     int i;
0190     QString transed = "";
0191 
0192     transed.reserve(unicoded.length());
0193 
0194     for (i = 0; i < (int)unicoded.length(); i++) {
0195         QString charIn = unicoded.mid(i, 1);
0196         if (TranslitPlugin::s_mapFromUTF8.contains(charIn.toUtf8())) {
0197             QString charTrans = TranslitPlugin::s_mapFromUTF8[charIn.toUtf8()];
0198             transed.append(charTrans);
0199         } else {
0200             transed.append(charIn);
0201         }
0202 
0203     }
0204 
0205     return transed;
0206 }
0207