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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • actions
kcodecaction.cpp
Go to the documentation of this file.
1 /*
2  kcodecaction.cpp
3 
4  Copyright (c) 2003 Jason Keirstead <jason@keirstead.org>
5  Copyrigth (c) 2006 Michel Hermier <michel.hermier@gmail.com>
6  Copyright (c) 2007 Nick Shaforostoff <shafff@ukr.net>
7 
8  ********************************************************************
9  * *
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2 of the License, or (at your option) any later version. *
14  * *
15  * This library is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU Lesser General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU Lesser General Public *
21  * License along with this library; if not, write to the *
22  * Free Software Foundation, Inc., 51 Franklin Street, *
23  * Fifth Floor, Boston, MA 02110-1301 USA *
24  * *
25  ********************************************************************
26 */
27 
28 #include "kcodecaction.h"
29 
30 #include <kcharsets.h>
31 #include <kdebug.h>
32 #include <klocale.h>
33 #include <kglobal.h>
34 
35 #include <QMenu>
36 #include <QVariant>
37 #include <QtCore/QTextCodec>
38 
39 // Acording to http://www.iana.org/assignments/ianacharset-mib
40 // the default/unknown mib value is 2.
41 #define MIB_DEFAULT 2
42 
43 class KCodecAction::Private
44 {
45 public:
46  Private(KCodecAction *parent)
47  : q(parent),
48  defaultAction(0),
49  currentSubAction(0)
50  {
51  }
52 
53  void init(bool);
54 
55  void _k_subActionTriggered(QAction*);
56 
57  KCodecAction *q;
58  QAction *defaultAction;
59  QAction *currentSubAction;
60 };
61 
62 KCodecAction::KCodecAction(QObject *parent,bool showAutoOptions)
63  : KSelectAction(parent)
64  , d(new Private(this))
65 {
66  d->init(showAutoOptions);
67 }
68 
69 KCodecAction::KCodecAction(const QString &text, QObject *parent,bool showAutoOptions)
70  : KSelectAction(text, parent)
71  , d(new Private(this))
72 {
73  d->init(showAutoOptions);
74 }
75 
76 KCodecAction::KCodecAction(const KIcon &icon, const QString &text, QObject *parent,bool showAutoOptions)
77  : KSelectAction(icon, text, parent)
78  , d(new Private(this))
79 {
80  d->init(showAutoOptions);
81 }
82 
83 KCodecAction::~KCodecAction()
84 {
85  delete d;
86 }
87 
88 void KCodecAction::Private::init(bool showAutoOptions)
89 {
90  q->setToolBarMode(MenuMode);
91  defaultAction = q->addAction(i18nc("Encodings menu", "Default"));
92 
93  int i;
94  foreach(const QStringList &encodingsForScript, KGlobal::charsets()->encodingsByScript())
95  {
96  KSelectAction* tmp = new KSelectAction(encodingsForScript.at(0),q);
97  if (showAutoOptions)
98  {
99  KEncodingDetector::AutoDetectScript scri=KEncodingDetector::scriptForName(encodingsForScript.at(0));
100  if (KEncodingDetector::hasAutoDetectionForScript(scri))
101  {
102  tmp->addAction(i18nc("Encodings menu","Autodetect"))->setData(QVariant((uint)scri));
103  tmp->menu()->addSeparator();
104  }
105  }
106  for (i=1; i<encodingsForScript.size(); ++i)
107  {
108  tmp->addAction(encodingsForScript.at(i));
109  }
110  q->connect(tmp,SIGNAL(triggered(QAction*)),q,SLOT(_k_subActionTriggered(QAction*)));
111  tmp->setCheckable(true);
112  q->addAction(tmp);
113  }
114  q->setCurrentItem(0);
115 }
116 
117 int KCodecAction::mibForName(const QString &codecName, bool *ok) const
118 {
119  // FIXME logic is good but code is ugly
120 
121  bool success = false;
122  int mib = MIB_DEFAULT;
123  KCharsets *charsets = KGlobal::charsets();
124 
125  if (codecName == d->defaultAction->text())
126  success = true;
127  else
128  {
129  QTextCodec *codec = charsets->codecForName(codecName, success);
130  if (!success)
131  {
132  // Maybe we got a description name instead
133  codec = charsets->codecForName(charsets->encodingForName(codecName), success);
134  }
135 
136  if (codec)
137  mib = codec->mibEnum();
138  }
139 
140  if (ok)
141  *ok = success;
142 
143  if (success)
144  return mib;
145 
146  kWarning() << "Invalid codec name: " << codecName;
147  return MIB_DEFAULT;
148 }
149 
150 QTextCodec *KCodecAction::codecForMib(int mib) const
151 {
152  if (mib == MIB_DEFAULT)
153  {
154  // FIXME offer to change the default codec
155  return QTextCodec::codecForLocale();
156  }
157  else
158  return QTextCodec::codecForMib(mib);
159 }
160 
161 void KCodecAction::actionTriggered(QAction *action)
162 {
163 //we don't want to emit any signals from top-level items
164 //except for the default one
165  if (action==d->defaultAction)
166  {
167  emit triggered(KEncodingDetector::SemiautomaticDetection);
168  emit defaultItemTriggered();
169  }
170 }
171 
172 void KCodecAction::Private::_k_subActionTriggered(QAction *action)
173 {
174  if (currentSubAction==action)
175  return;
176  currentSubAction=action;
177  bool ok = false;
178  int mib = q->mibForName(action->text(), &ok);
179  if (ok)
180  {
181  emit q->triggered(action->text());
182  emit q->triggered(q->codecForMib(mib));
183  }
184  else
185  {
186  if (!action->data().isNull())
187  emit q->triggered((KEncodingDetector::AutoDetectScript) action->data().toUInt());
188  }
189 }
190 
191 QTextCodec *KCodecAction::currentCodec() const
192 {
193  return codecForMib(currentCodecMib());
194 }
195 
196 bool KCodecAction::setCurrentCodec( QTextCodec *codec )
197 {
198  if (!codec)
199  return false;
200 
201  int i,j;
202  for (i=0;i<actions().size();++i)
203  {
204  if (actions().at(i)->menu())
205  {
206  for (j=0;j<actions().at(i)->menu()->actions().size();++j)
207  {
208  if (!j && !actions().at(i)->menu()->actions().at(j)->data().isNull())
209  continue;
210  if (codec==KGlobal::charsets()->codecForName(actions().at(i)->menu()->actions().at(j)->text()))
211  {
212  d->currentSubAction=actions().at(i)->menu()->actions().at(j);
213  d->currentSubAction->trigger();
214  return true;
215  }
216  }
217  }
218  }
219  return false;
220 
221 }
222 
223 QString KCodecAction::currentCodecName() const
224 {
225  return d->currentSubAction->text();
226 }
227 
228 bool KCodecAction::setCurrentCodec( const QString &codecName )
229 {
230  return setCurrentCodec(KGlobal::charsets()->codecForName(codecName));
231 }
232 
233 int KCodecAction::currentCodecMib() const
234 {
235  return mibForName(currentCodecName());
236 }
237 
238 bool KCodecAction::setCurrentCodec( int mib )
239 {
240  if (mib == MIB_DEFAULT)
241  return setCurrentAction(d->defaultAction);
242  else
243  return setCurrentCodec(codecForMib(mib));
244 }
245 
246 KEncodingDetector::AutoDetectScript KCodecAction::currentAutoDetectScript() const
247 {
248  return d->currentSubAction->data().isNull()?
249  KEncodingDetector::None :
250  (KEncodingDetector::AutoDetectScript)d->currentSubAction->data().toUInt();
251 }
252 
253 bool KCodecAction::setCurrentAutoDetectScript(KEncodingDetector::AutoDetectScript scri)
254 {
255  if (scri==KEncodingDetector::SemiautomaticDetection)
256  {
257  d->currentSubAction=d->defaultAction;
258  d->currentSubAction->trigger();
259  return true;
260  }
261 
262  int i;
263  for (i=0;i<actions().size();++i)
264  {
265  if (actions().at(i)->menu())
266  {
267  if (!actions().at(i)->menu()->actions().isEmpty()
268  &&!actions().at(i)->menu()->actions().at(0)->data().isNull()
269  &&actions().at(i)->menu()->actions().at(0)->data().toUInt()==(uint)scri
270  )
271  {
272  d->currentSubAction=actions().at(i)->menu()->actions().at(0);
273  d->currentSubAction->trigger();
274  return true;
275  }
276  }
277  }
278  return false;
279 }
280 
281 #include "kcodecaction.moc"
QVariant
KCharsets::codecForName
QTextCodec * codecForName(const QString &name) const
KCodecAction::defaultItemTriggered
void defaultItemTriggered()
If showAutoOptions==true, then better handle triggered(KEncodingDetector::AutoDetectScript) signal...
kcharsets.h
KCodecAction::currentAutoDetectScript
KEncodingDetector::AutoDetectScript currentAutoDetectScript() const
Applicable only if showAutoOptions in c'tor was true.
Definition: kcodecaction.cpp:246
kdebug.h
KCharsets
KCodecAction::currentCodecName
QString currentCodecName() const
Definition: kcodecaction.cpp:223
KCodecAction::setCurrentCodec
bool setCurrentCodec(QTextCodec *codec)
Definition: kcodecaction.cpp:196
QString
KSelectAction::addAction
virtual void addAction(QAction *action)
Add action to the list of selectable actions.
Definition: kselectaction.cpp:230
QObject
klocale.h
KEncodingDetector::scriptForName
static AutoDetectScript scriptForName(const QString &lang)
i18nc
QString i18nc(const char *ctxt, const char *text)
KEncodingDetector::AutoDetectScript
AutoDetectScript
KCodecAction::codecForMib
QTextCodec * codecForMib(int mib) const
Definition: kcodecaction.cpp:150
KSelectAction
Action for selecting one of several items.
Definition: kselectaction.h:51
KCodecAction::triggered
void triggered(QTextCodec *codec)
Specific (proper) codec was selected.
kglobal.h
KCodecAction::mibForName
int mibForName(const QString &codecName, bool *ok=0) const
Definition: kcodecaction.cpp:117
KCodecAction::codecName
QString codecName
Definition: kcodecaction.h:43
QStringList
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
KGlobal::charsets
KCharsets * charsets()
KCharsets::encodingForName
QString encodingForName(const QString &descriptiveName) const
KCodecAction
Action for selecting one of several QTextCodec.
Definition: kcodecaction.h:38
KSelectAction::actions
QList< QAction * > actions() const
Returns the list of selectable actions.
Definition: kselectaction.cpp:110
KCodecAction::~KCodecAction
virtual ~KCodecAction()
Definition: kcodecaction.cpp:83
KCodecAction::currentCodecMib
int currentCodecMib() const
Definition: kcodecaction.cpp:233
KCodecAction::currentCodec
QTextCodec * currentCodec() const
Definition: kcodecaction.cpp:191
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
KEncodingDetector::SemiautomaticDetection
KEncodingDetector::hasAutoDetectionForScript
static bool hasAutoDetectionForScript(AutoDetectScript)
MIB_DEFAULT
#define MIB_DEFAULT
Definition: kcodecaction.cpp:41
KCodecAction::setCurrentAutoDetectScript
bool setCurrentAutoDetectScript(KEncodingDetector::AutoDetectScript)
Applicable only if showAutoOptions in c'tor was true.
Definition: kcodecaction.cpp:253
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
kcodecaction.h
KEncodingDetector::None
KSelectAction::setCurrentAction
bool setCurrentAction(QAction *action)
Sets the currently checked item.
Definition: kselectaction.cpp:133
KCodecAction::KCodecAction
KCodecAction(QObject *parent, bool showAutoOptions=false)
Definition: kcodecaction.cpp:62
QAction
KCodecAction::actionTriggered
virtual void actionTriggered(QAction *)
Definition: kcodecaction.cpp:161
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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