• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • editor
  • codec
codecaction.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KMail.
3  * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 // Own
21 #include "codecaction.h"
22 
23 // KMail
24 #include "codecmanager.h"
25 
26 #include "messageviewer/viewer/nodehelper.h"
27 
28 #include <KCharsets>
29 // Qt
30 #include <QTextCodec>
31 
32 // KDE libs
33 #include <KDebug>
34 #include <KLocalizedString>
35 
36 class CodecAction::Private
37 {
38 public:
39  Private( CodecAction::Mode mod, CodecAction *qq )
40  : mode( mod )
41  , q( qq )
42  {
43  }
44 
45  const CodecAction::Mode mode;
46  CodecAction *const q;
47 };
48 
49 
50 
51 CodecAction::CodecAction( Mode mode, QObject *parent )
52  : KCodecAction( parent, mode == ReaderMode )
53  , d( new Private( mode, this ) )
54 {
55  if( mode == ComposerMode ) {
56  // Add 'us-ascii' entry. We want it at the top, so remove then re-add everything.
57  // FIXME is there a better way?
58  QList<QAction*> oldActions = actions();
59  removeAllActions();
60  addAction( oldActions.takeFirst() ); // 'Default'
61  addAction( i18nc( "Encodings menu", "us-ascii" ) );
62  foreach( QAction *a, oldActions ) {
63  addAction( a );
64  }
65  } else if( mode == ReaderMode ) {
66  // Nothing to do.
67  }
68 
69  // Eye candy.
70  setIcon( KIcon( QLatin1String("accessories-character-map") ) );
71  setText( i18nc( "Menu item", "Encoding" ) );
72 }
73 
74 CodecAction::~CodecAction()
75 {
76  delete d;
77 }
78 
79 QList<QByteArray> CodecAction::mimeCharsets() const
80 {
81  QList<QByteArray> ret;
82  kDebug() << "current item" << currentItem() << currentText();
83  if( currentItem() == 0 ) {
84  // 'Default' selected: return the preferred charsets.
85  ret = CodecManager::self()->preferredCharsets();
86  } else if( currentItem() == 1 ) {
87  // 'us-ascii' selected.
88  ret << "us-ascii";
89  } else {
90  // Specific codec selected.
91  // ret << currentCodecName().toLatin1().toLower(); // FIXME in kdelibs: returns e.g. '&koi8-r'
92  ret << currentCodec()->name();
93  kDebug() << "current codec name" << ret.at(0);
94  }
95  return ret;
96 }
97 
98 void CodecAction::setAutoCharset()
99 {
100  setCurrentItem( 0 );
101 }
102 
103 // We can't simply use KCodecAction::setCurrentCodec(), since that doesn't
104 // use fixEncoding().
105 static QString selectCharset( KSelectAction *root, const QString &encoding )
106 {
107  foreach( QAction *action, root->actions() ) {
108  KSelectAction *subMenu = dynamic_cast<KSelectAction *>( action );
109  if ( subMenu ) {
110  const QString codecNameToSet = selectCharset( subMenu, encoding );
111  if ( !codecNameToSet.isEmpty() )
112  return codecNameToSet;
113  }
114  else {
115  const QString fixedActionText = MessageViewer::NodeHelper::fixEncoding( action->text() );
116  if ( KGlobal::charsets()->codecForName(
117  KGlobal::charsets()->encodingForName( fixedActionText ) )
118  == KGlobal::charsets()->codecForName( encoding ) ) {
119  return action->text();
120  }
121  }
122  }
123  return QString();
124 }
125 
126 
127 void CodecAction::setCharset( const QByteArray &charset )
128 {
129  const QString codecNameToSet = selectCharset( this, QString::fromLatin1(charset) );
130  if ( codecNameToSet.isEmpty() ) {
131  kWarning() << "Could not find charset" << charset;
132  setAutoCharset();
133  }
134  else
135  setCurrentCodec( codecNameToSet );
136 }
QAction::text
text
CodecAction::setCharset
void setCharset(const QByteArray &charset)
Definition: codecaction.cpp:127
CodecAction::setAutoCharset
void setAutoCharset()
Definition: codecaction.cpp:98
QByteArray
CodecAction::Mode
Mode
Definition: codecaction.h:36
selectCharset
static QString selectCharset(KSelectAction *root, const QString &encoding)
Definition: codecaction.cpp:105
QList::at
const T & at(int i) const
CodecAction::ReaderMode
which uses one of the preferred codecs. Also show 'us-ascii'.
Definition: codecaction.h:40
CodecAction::Private
friend class Private
Definition: codecaction.h:58
QObject
CodecManager::preferredCharsets
QList< QByteArray > preferredCharsets() const
A list of preferred charsets to use when composing messages.
Definition: codecmanager.cpp:75
QString::isEmpty
bool isEmpty() const
CodecAction::ComposerMode
Used in the composer.
Definition: codecaction.h:38
QString
QList< QAction * >
KCodecAction
CodecAction
Definition: codecaction.h:31
codecmanager.h
CodecAction::CodecAction
CodecAction(Mode mode, QObject *parent=0)
Definition: codecaction.cpp:51
codecaction.h
QList::takeFirst
T takeFirst()
QLatin1String
QAction
CodecManager::self
static CodecManager * self()
Returns the CodecManager instance.
Definition: codecmanager.cpp:70
QString::fromLatin1
QString fromLatin1(const char *str, int size)
CodecAction::~CodecAction
~CodecAction()
Definition: codecaction.cpp:74
CodecAction::mimeCharsets
QList< QByteArray > mimeCharsets() const
The name of the charset, if a specific encoding was selected, or a list containing the names of the p...
Definition: codecaction.cpp:79
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal