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

knode

  • sources
  • kde-4.12
  • kdepim
  • knode
  • utils
locale.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2009 Olivier Trichet <nive@nivalis.org>
3 
4  Permission to use, copy, modify, and distribute this software
5  and its documentation for any purpose and without fee is hereby
6  granted, provided that the above copyright notice appear in all
7  copies and that both that the copyright notice and this
8  permission notice and warranty disclaimer appear in supporting
9  documentation, and that the name of the author not be used in
10  advertising or publicity pertaining to distribution of the
11  software without specific, written prior permission.
12 
13  The author disclaim all warranties with regard to this
14  software, including all implied warranties of merchantability
15  and fitness. In no event shall the author be liable for any
16  special, indirect or consequential damages or any damages
17  whatsoever resulting from loss of use, data or profits, whether
18  in an action of contract, negligence or other tortious action,
19  arising out of or in connection with the use or performance of
20  this software.
21 */
22 
23 #include "locale.h"
24 
25 #include "knglobals.h"
26 #include "kngroup.h"
27 #include "settings.h"
28 
29 #include <KCharsets>
30 #include <KGlobal>
31 #include <kmime/kmime_charfreq.h>
32 #include <KLocale>
33 
34 #include <QTextCodec>
35 
36 
37 using namespace KNode::Utilities;
38 
39 
40 QString Locale::toMimeCharset( const QString &charset )
41 {
42  QString c = charset;
43 
44  // First, get the user preferred encoding
45  if ( c.isEmpty() ) {
46  c = KGlobal::locale()->encoding();
47  if ( c.isEmpty() ) { // Let's test to be really sure...
48  return "UTF-8";
49  }
50  }
51 
52  // Second, convert to something sensible
53  bool ok;
54  QTextCodec *codec = KGlobal::charsets()->codecForName( c, ok );
55  if ( ok && !codec->name().isEmpty() ) {
56  c = codec->name();
57  } else {
58  return "UTF-8";
59  }
60 
61  // special logic for japanese users:
62  // "euc-jp" is default encoding for them, but in the news
63  // "iso-2022-jp" is used (#36638)
64  if ( c.toUpper() == "EUC-JP" ) {
65  c = "ISO-2022-JP";
66  }
67 
68  // Uppercase is preferred in MIME headers
69  c = c.toUpper();
70 
71  return c;
72 }
73 
74 
75 
76 QByteArray Locale::defaultCharset()
77 {
78  return toMimeCharset( knGlobals.settings()->charset() ).toLatin1();
79 }
80 
81 QByteArray Locale::defaultCharset( KNGroup::Ptr g )
82 {
83  if ( g && g->useCharset() ) {
84  return toMimeCharset( g->defaultCharset() ).toLatin1();
85  } else {
86  return defaultCharset();
87  }
88 }
89 
90 
91 
92 
93 void Locale::recodeString( const QString &s, KNGroup::Ptr g, QByteArray &result )
94 {
95  Q_ASSERT( g );
96 
97  encodeTo7Bit( s.toLatin1(), defaultCharset( g ), result );
98 }
99 
100 
101 void Locale::encodeTo7Bit( const QByteArray &raw, const QByteArray &charset, QByteArray &result )
102 {
103  if ( raw.isEmpty() ) {
104  result = raw;
105  return;
106  }
107 
108  KMime::CharFreq cf( raw );
109  if ( cf.isSevenBitText() ) {
110  result = raw;
111  return;
112  }
113 
114  // Transform 8-bit data
115  QString properData = QTextCodec::codecForName( charset )->toUnicode( raw );
116  result = KMime::encodeRFC2047String( properData, "UTF-8" );
117 }
118 
119 
120 QStringList Locale::encodings()
121 {
122  QStringList encodings = KGlobal::charsets()->availableEncodingNames();
123  QStringList ret;
124  QStringList seenEncs;
125 
126  // Blacklist 'UTF-16' which gives garbage (bug #168327).
127  seenEncs << "UTF-16";
128 
129  foreach ( const QString &enc, encodings ) {
130  // Valid codec only
131  bool ok;
132 
133  KGlobal::charsets()->codecForName( enc, ok );
134  if ( !ok ) {
135  continue;
136  }
137 
138  // One encoding description per MIME-charset
139  QString mimeEnc = toMimeCharset( enc );
140  if ( !seenEncs.contains( mimeEnc ) ) {
141  seenEncs << mimeEnc;
142  ret << KGlobal::charsets()->descriptionForEncoding( enc );
143  }
144  }
145 
146  ret.sort();
147  return ret;
148 }
149 
KNode::Utilities::Locale::encodings
static QStringList encodings()
Returns a list of encodings along with a description.
Definition: locale.cpp:120
KNode::Utilities::Locale::recodeString
static void recodeString(const QString &s, KNGroup::Ptr g, QByteArray &result)
Try to convert a 8bits string to the equivalent 7bits RFC-2047 string.
Definition: locale.cpp:93
kngroup.h
KNode::Utilities::Locale::defaultCharset
static QByteArray defaultCharset()
Returns the charset defined in the global configuration.
Definition: locale.cpp:76
locale.h
knglobals.h
settings.h
KNode::Utilities::Locale::encodeTo7Bit
static void encodeTo7Bit(const QByteArray &raw, const QByteArray &charset, QByteArray &result)
Convert raw data raw to a RFC-2047 string.
Definition: locale.cpp:101
KNode::Utilities::Locale::toMimeCharset
static QString toMimeCharset(const QString &charset)
Converts a given charset to a charset that can be used in message headers (e.g.
Definition: locale.cpp:40
knGlobals
#define knGlobals
Keep compatibility with the old way.
Definition: knglobals.h:28
KNGroup::Ptr
boost::shared_ptr< KNGroup > Ptr
Shared pointer to a KNGroup.
Definition: kngroup.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knode

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

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

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