File indexing completed on 2024-04-21 16:32:36

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