summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/kcodecaction.cpp
blob: f7e4ee24f74509fa6958c2dfda70a32c995bdbfd (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
/*
    kcodecaction.cpp

    Copyright (c) 2005      by Tommi Rantala  <[email protected]>
    Copyright (c) 2003      by Jason Keirstead        <[email protected]>
    Kopete    (c) 2003-2005 by the Kopete developers  <[email protected]>

    *************************************************************************
    *                                                                       *
    * 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 <tqstringlist.h>
#include <tqtextcodec.h>
#include <kcharsets.h>

#include "kcodecaction.h"

KCodecAction::KCodecAction( const TQString &text, const KShortcut &cut,
		TQObject *parent, const char *name ) : KSelectAction( text, "", cut, parent, name )
{
	TQObject::connect( this, TQT_SIGNAL( activated( const TQString & ) ),
			this, TQT_SLOT( slotActivated( const TQString & ) ) );

	setItems( KCodecAction::supportedEncodings() );
}

void KCodecAction::slotActivated( const TQString & text )
{
	/* text is something like "Western European ( iso-8859-1 )", but we must give
	 * codecForName() only the "iso-8859-1" part.
	 */
	TQString encoding = KGlobal::charsets()->encodingForName(text);

	emit activated( KGlobal::charsets()->codecForName(encoding) );
}

void KCodecAction::setCodec( const TQTextCodec *codec )
{
	TQStringList items = this->items();
	int i = 0;
	for (TQStringList::ConstIterator it = items.begin(), end = items.end(); it != end; ++it, ++i) {
		TQString encoding = KGlobal::charsets()->encodingForName(*it);

		if (KGlobal::charsets()->codecForName(encoding)->mibEnum() == codec->mibEnum()) {
			setCurrentItem(i);
			break;
		}
	}
}

/* Create a list of supported encodings, and keep only one of each encoding
 * mime name.
 *
 * This piece of code from kdepim/kmail/kmmsgbase.cpp
 */

TQStringList KCodecAction::supportedEncodings(bool usAscii)
{
	TQStringList encodingNames = KGlobal::charsets()->availableEncodingNames();
	TQStringList encodings;
	TQMap<TQString, bool> mimeNames;

	for (TQStringList::ConstIterator it = encodingNames.begin();
			it != encodingNames.end(); ++it)
	{
		TQTextCodec *codec = KGlobal::charsets()->codecForName(*it);
		TQString mimeName = (codec) ? TQString(codec->mimeName()).lower() : (*it);
		if (mimeNames.find(mimeName) == mimeNames.end())
		{
			encodings.append(KGlobal::charsets()->languageForEncoding(*it)
					+ " ( " + mimeName + " )");
			mimeNames.insert(mimeName, true);
		}
	}

	encodings.sort();
	if (usAscii) encodings.prepend(KGlobal::charsets()
			->languageForEncoding("us-ascii") + " ( us-ascii )");
	return encodings;
}

#include "kcodecaction.moc"