[Feature] Improve uri handling.
[Khopper.git] / plugins / cuesheet / codecselector.cpp
blob9c773beb4154e958e59cfdc805c281943d500dac
1 /**
2 * @file codecselector.cpp
3 * @author Wei-Cheng Pan
5 * Copyright (C) 2008 Wei-Cheng Pan <legnaleurc@gmail.com>
7 * This file is part of Khopper.
9 * Khopper is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * Khopper is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "codecselector.hpp"
23 #include "ui_codecselector.h"
25 #include <QtCore/QTextCodec>
26 #include <QtCore/QTextStream>
28 using namespace khopper::widget;
30 QString CodecSelector::selectTextCodec( const QByteArray & encoded ) {
31 std::shared_ptr< CodecSelector > dialog( new CodecSelector( encoded ) );
32 if( dialog->exec() == QDialog::Rejected ) {
33 return "";
34 } else {
35 return dialog->decoded_;
39 CodecSelector::CodecSelector( const QByteArray & encoded ):
40 QDialog( 0, 0 ),
41 ui_( new Ui::CodecSelector ),
42 encoded_( encoded ),
43 decoded_() {
44 this->ui_->setupUi( this );
46 this->ui_->codecs->addItem( "UTF-8", QTextCodec::codecForName( "UTF-8" )->mibEnum() );
47 this->ui_->codecs->addItem( "Shift-JIS", QTextCodec::codecForName( "Shift-JIS" )->mibEnum() );
48 this->ui_->codecs->addItem( "Big5", QTextCodec::codecForName( "Big5" )->mibEnum() );
49 QObject::connect( this->ui_->codecs, SIGNAL( currentIndexChanged( int ) ), this, SLOT( update_( int ) ) );
51 this->update_( this->ui_->codecs->currentIndex() );
54 void CodecSelector::update_( int index ) {
55 int mib = this->ui_->codecs->itemData( index ).toInt();
56 QTextStream ts( &this->encoded_ );
57 ts.setCodec( QTextCodec::codecForMib( mib ) );
58 this->decoded_ = ts.readAll();
60 this->ui_->contents->setText( this->decoded_ );