summaryrefslogtreecommitdiffstats
path: root/konversation/src/irccharsets.cpp
blob: 89fdf4f478ccda995f3d329d9ea942671872ffbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// A wrapper for KCharsets
// Copyright (C) 2004, 2006 Shintaro Matsuoka <shin@shoegazed.org>

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

#include "irccharsets.h"

#include <qglobal.h>
#include <qregexp.h>
#include <qtextcodec.h>
#include <kcharsets.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kstaticdeleter.h>

#if QT_VERSION < 0x030300
#include <klocale.h>
#else
#include <qlocale.h>
#endif


namespace Konversation
{

    IRCCharsets* IRCCharsets::s_self = 0;
    static KStaticDeleter<IRCCharsets> staticIRCCharsetDeleter;

    IRCCharsets *IRCCharsets::self()
    {
        if(!s_self)
            staticIRCCharsetDeleter.setObject(s_self, new IRCCharsets());
        return s_self;
    }

    QStringList IRCCharsets::availableEncodingShortNames()
    {
        return m_shortNames;
    }

    QStringList IRCCharsets::availableEncodingDescriptiveNames()
    {
        return m_descriptiveNames;
    }

    int IRCCharsets::availableEncodingsCount()
    {
        return m_shortNames.count();
    }

    QString IRCCharsets::shortNameToDescriptiveName( const QString& shortName )
    {
        return m_descriptiveNames[ shortNameToIndex( shortName ) ];
    }

    QString descriptiveNameToShortName( const QString& descriptiveName )
    {
        return KGlobal::charsets()->encodingForName( descriptiveName );
    }

    QString IRCCharsets::ambiguousNameToShortName( const QString& ambiguousName )
    {
        // simplify ambiguousName
        QString simplifiedAmbiguousName( ambiguousName.lower() );
        simplifiedAmbiguousName.replace( QRegExp( "[^a-z0-9]" ), "" );

        // search m_simplifiedShortNames
        int index = 0;
        for ( QStringList::iterator it = m_simplifiedShortNames.begin() ; it != m_simplifiedShortNames.end() ; ++it )
        {
            if ( (*it) == simplifiedAmbiguousName )
                return m_shortNames[index];
            ++index;
        }

        // search m_shortNameAliases
        if ( m_shortNameAliases.contains( simplifiedAmbiguousName ) )
            return m_shortNameAliases[ simplifiedAmbiguousName ];

        // failed
        return QString();
    }

    int IRCCharsets::shortNameToIndex( const QString& shortName )
    {
        int index = 0;
        for ( QStringList::iterator it = m_shortNames.begin() ; it != m_shortNames.end() ; ++it )
        {
            if ( (*it) == shortName )
                return index;
            ++index;
        }
        return -1;
    }

    bool IRCCharsets::isValidEncoding( const QString& shortName )
    {
        return ( m_shortNames.contains( shortName ) > 0 );
    }

    QString IRCCharsets::encodingForLocale()
    {
        #if QT_VERSION < 0x030300
        QString locale = KLocale::defaultLanguage();
        #else
        QString locale = QLocale::system().name();
        #endif

        // Special cases
        // don't add conditions for the languages for which QTextCodec::codecForLocale() returns a correct codec.
        if ( locale == "ja_JP" )
            return "jis7";

        // it's a little hacky..
        for ( QStringList::iterator it = m_shortNames.begin() ; it != m_shortNames.end() ; ++it )
            if ( QTextCodec::codecForName( (*it).ascii() ) == QTextCodec::codecForLocale() )
                return *it;

        return "utf8";
    }

    QTextCodec* IRCCharsets::codecForName( const QString& shortName )
    {
        if(shortName == "iso-2022-jp")
            return QTextCodec::codecForName( "jis7" );
        else
            return QTextCodec::codecForName( shortName.ascii() );
    }

    IRCCharsets::IRCCharsets()
    {
        s_self = this;

        // setup m_shortNameAliases
        // use only [a-z0-9] for keys!
        m_shortNameAliases["unicode"] = "utf8";
        m_shortNameAliases["latin1"] = "iso-8859-1";

        // setup m_shortNames, m_descriptiveNames, m_simplifiedShortNames
        QRegExp reSimplify( "[^a-zA-Z0-9]" );
        m_descriptiveNames = KGlobal::charsets()->descriptiveEncodingNames();
        QStringList::Iterator it = m_descriptiveNames.begin();
        while ( it != m_descriptiveNames.end() )
        {
            QString encodingName = KGlobal::charsets()->encodingForName( *it );
            // exclude encodings which are not supported on IRC
            if ( encodingName == "iso-10646-ucs-2" ||
                 encodingName == "ucs2" ||
                 encodingName == "utf16" ||
                 encodingName == "utf7" )
            {
                it = m_descriptiveNames.remove( it );
            }
            else
            {
                m_shortNames.append( encodingName );
                m_simplifiedShortNames.append( encodingName.replace( reSimplify, "" ) );

                if(encodingName == "jis7")        // Add iso-2022-jp which is same as jis7 but not in Qt
                {
                    it = m_descriptiveNames.insert(it, "Japanese ( iso-2022-jp )");
                    m_shortNames.append( "iso-2022-jp" );
                    m_simplifiedShortNames.append( "ISO-2022-JP" );
                    ++it;
                }
                ++it;
            }

        }
    }

}