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

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • viewer
mailsourceviewer.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2  *
3  * This file is part of KMail, the KDE mail client.
4  *
5  * Copyright (c) 2002-2003 Carsten Pfeiffer <pfeiffer@kde.org>
6  * Copyright (c) 2003 Zack Rusin <zack@kde.org>
7  *
8  * KMail is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License, version 2, as
10  * published by the Free Software Foundation.
11  *
12  * KMail is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of this program with any edition of
23  * the Qt library by Trolltech AS, Norway (or with modified versions
24  * of Qt that use the same license as Qt), and distribute linked
25  * combinations including the two. You must obey the GNU General
26  * Public License in all respects for all of the code used other than
27  * Qt. If you modify this file, you may extend this exception to
28  * your version of the file, but you are not obligated to do so. If
29  * you do not wish to do so, delete this exception statement from
30  * your version.
31  */
32 
33 
34 #include "mailsourceviewer.h"
35 #include "utils/util.h"
36 #include "findbar/findbarsourceview.h"
37 #include "kpimtextedit/htmlhighlighter.h"
38 #include "pimcommon/util/pimutil.h"
39 #include <kiconloader.h>
40 #include <KLocalizedString>
41 #include <kstandardguiitem.h>
42 #include <kwindowsystem.h>
43 #include <kglobalsettings.h>
44 #include <KTabWidget>
45 #include <KFileDialog>
46 #include <KMessageBox>
47 
48 #include <QtCore/QRegExp>
49 #include <QApplication>
50 #include <QIcon>
51 #include <QShortcut>
52 #include <QVBoxLayout>
53 #include <QContextMenuEvent>
54 #include <QDebug>
55 #include <QMenu>
56 
57 namespace MessageViewer {
58 
59 
60 MailSourceViewTextBrowserWidget::MailSourceViewTextBrowserWidget( QWidget *parent )
61  :QWidget( parent )
62 {
63  QVBoxLayout *lay = new QVBoxLayout;
64  setLayout( lay );
65  lay->setMargin( 0 );
66  mTextBrowser = new MailSourceViewTextBrowser();
67  mTextBrowser->setLineWrapMode( QPlainTextEdit::NoWrap );
68  mTextBrowser->setTextInteractionFlags( Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard );
69  connect( mTextBrowser, SIGNAL(findText()), SLOT(slotFind()) );
70  lay->addWidget( mTextBrowser );
71  mFindBar = new FindBarSourceView( mTextBrowser, this );
72  lay->addWidget( mFindBar );
73  QShortcut *shortcut = new QShortcut( this );
74  shortcut->setKey( Qt::Key_F+Qt::CTRL );
75  connect( shortcut, SIGNAL(activated()), SLOT(slotFind()) );
76 }
77 
78 void MailSourceViewTextBrowserWidget::slotFind()
79 {
80  if ( mTextBrowser->textCursor().hasSelection() )
81  mFindBar->setText( mTextBrowser->textCursor().selectedText() );
82  mFindBar->show();
83  mFindBar->focusAndSetCursor();
84 }
85 
86 void MailSourceViewTextBrowserWidget::setText( const QString& text )
87 {
88  mTextBrowser->setPlainText( text );
89 }
90 
91 void MailSourceViewTextBrowserWidget::setPlainText( const QString& text )
92 {
93  mTextBrowser->setPlainText( text );
94 }
95 
96 void MailSourceViewTextBrowserWidget::setFixedFont()
97 {
98  mTextBrowser->setFont( KGlobalSettings::fixedFont() );
99 }
100 
101 MessageViewer::MailSourceViewTextBrowser *MailSourceViewTextBrowserWidget::textBrowser() const
102 {
103  return mTextBrowser;
104 }
105 
106 MailSourceViewTextBrowser::MailSourceViewTextBrowser( QWidget *parent )
107  :QPlainTextEdit( parent )
108 {
109 }
110 
111 void MailSourceViewTextBrowser::contextMenuEvent( QContextMenuEvent *event )
112 {
113  QMenu *popup = createStandardContextMenu();
114  if (popup) {
115  popup->addSeparator();
116  popup->addAction( KStandardGuiItem::find().text(),this,SIGNAL(findText()) , Qt::Key_F+Qt::CTRL);
117  //Code from KTextBrowser
118  KIconTheme::assignIconsToContextMenu( isReadOnly() ? KIconTheme::ReadOnlyText
119  : KIconTheme::TextEditor,
120  popup->actions() );
121  popup->addSeparator();
122  popup->addAction( KIcon(QLatin1String("preferences-desktop-text-to-speech")),i18n("Speak Text"),this,SLOT(slotSpeakText()));
123 
124  popup->addSeparator();
125  popup->addAction( KIcon(QLatin1String("document-save")),i18n("Save As..."),this,SLOT(slotSaveAs()));
126 
127  popup->exec( event->globalPos() );
128  delete popup;
129  }
130 }
131 
132 void MailSourceViewTextBrowser::slotSaveAs()
133 {
134  PimCommon::Util::saveTextAs( toPlainText(), QString(), this );
135 }
136 
137 void MailSourceViewTextBrowser::slotSpeakText()
138 {
139  QString text;
140  if ( textCursor().hasSelection() ) {
141  text = textCursor().selectedText();
142  } else {
143  text = toPlainText();
144  }
145  MessageViewer::Util::speakSelectedText( text, this);
146 }
147 
148 void MailSourceHighlighter::highlightBlock ( const QString & text ) {
149  // all visible ascii except space and :
150  const QRegExp regexp( QLatin1String("^([\\x21-9;-\\x7E]+:\\s)") );
151 
152  // keep the previous state
153  setCurrentBlockState( previousBlockState() );
154  // If a header is found
155  if( regexp.indexIn( text ) != -1 )
156  {
157  const int headersState = -1; // Also the initial State
158  // Content- header starts a new mime part, and therefore new headers
159  // If a Content-* header is found, change State to headers until a blank line is found.
160  if ( text.startsWith( QLatin1String( "Content-" ) ) )
161  {
162  setCurrentBlockState( headersState );
163  }
164  // highligth it if in headers state
165  if( ( currentBlockState() == headersState ) )
166  {
167  QFont font = document()->defaultFont ();
168  font.setBold( true );
169  setFormat( 0, regexp.matchedLength(), font );
170  }
171  }
172  // Change to body state
173  else if ( text.isEmpty() )
174  {
175  const int bodyState = 0;
176  setCurrentBlockState( bodyState );
177  }
178 }
179 
180 const QString HTMLPrettyFormatter::reformat( const QString &src )
181 {
182  const QRegExp cleanLeadingWhitespace( QLatin1String("(?:\\n)+\\w*") );
183  QStringList tmpSource;
184  QString source( src );
185  int pos = 0;
186  QString indent;
187 
188  //First make sure that each tag is surrounded by newlines
189  while( (pos = htmlTagRegExp.indexIn( source, pos ) ) != -1 )
190  {
191  source.insert(pos, QLatin1Char('\n'));
192  pos += htmlTagRegExp.matchedLength() + 1;
193  source.insert(pos, QLatin1Char('\n'));
194  pos++;
195  }
196 
197  // Then split the source on newlines skiping empty parts.
198  // Now a line is either a tag or pure data.
199  tmpSource = source.split(QLatin1Char('\n'), QString::SkipEmptyParts );
200 
201  // Then clean any leading whitespace
202  for( int i = 0; i != tmpSource.length(); ++i )
203  {
204  tmpSource[i] = tmpSource[i].remove( cleanLeadingWhitespace );
205  }
206 
207  // Then indent as appropriate
208  for( int i = 0; i != tmpSource.length(); ++i ) {
209  if( htmlTagRegExp.indexIn( tmpSource.at(i) ) != -1 ) // A tag
210  {
211  if( htmlTagRegExp.cap( 3 ) == QLatin1String( "/" ) ||
212  htmlTagRegExp.cap( 2 ) == QLatin1String( "img" ) ||
213  htmlTagRegExp.cap( 2 ) == QLatin1String( "br" ) ) {
214  //Self closing tag or no closure needed
215  continue;
216  }
217  if( htmlTagRegExp.cap( 1 ) == QLatin1String( "/" ) ) {
218  // End tag
219  indent.chop( 2 );
220  tmpSource[i].prepend( indent );
221  continue;
222  }
223  // start tag
224  tmpSource[i].prepend( indent );
225  indent.append( QLatin1String(" ") );
226  continue;
227  }
228  // Data
229  tmpSource[i].prepend( indent );
230  }
231 
232  // Finally reassemble and return :)
233  return tmpSource.join( QLatin1String("\n") );
234 }
235 
236 MailSourceViewer::MailSourceViewer( QWidget *parent )
237  : KDialog( parent )
238 {
239  setAttribute( Qt::WA_DeleteOnClose );
240  setButtons( Close );
241 
242  QVBoxLayout *layout = new QVBoxLayout( mainWidget() );
243  layout->setMargin( 0 );
244  connect( this, SIGNAL(closeClicked()), SLOT(close()) );
245 
246  mRawBrowser = new MailSourceViewTextBrowserWidget();
247 
248 #ifndef NDEBUG
249  mTabWidget = new KTabWidget;
250  layout->addWidget( mTabWidget );
251 
252  mTabWidget->addTab( mRawBrowser, i18nc( "Unchanged mail message", "Raw Source" ) );
253  mTabWidget->setTabToolTip( 0, i18n( "Raw, unmodified mail as it is stored on the filesystem or on the server" ) );
254 
255  mHtmlBrowser = new MailSourceViewTextBrowserWidget();
256  mTabWidget->addTab( mHtmlBrowser, i18nc( "Mail message as shown, in HTML format", "HTML Source" ) );
257  mTabWidget->setTabToolTip( 1, i18n( "HTML code for displaying the message to the user" ) );
258  new KPIMTextEdit::HtmlHighlighter( mHtmlBrowser->textBrowser()->document() );
259 
260  mTabWidget->setCurrentIndex( 0 );
261 #else
262  layout->addWidget( mRawBrowser );
263 #endif
264 
265  // combining the shortcuts in one qkeysequence() did not work...
266  QShortcut* shortcut = new QShortcut( this );
267  shortcut->setKey( Qt::Key_Escape );
268  connect( shortcut, SIGNAL(activated()), SLOT(close()) );
269  shortcut = new QShortcut( this );
270  shortcut->setKey( Qt::Key_W+Qt::CTRL );
271  connect( shortcut, SIGNAL(activated()), SLOT(close()) );
272 
273  KWindowSystem::setIcons( winId(),
274  qApp->windowIcon().pixmap( IconSize( KIconLoader::Desktop ),
275  IconSize( KIconLoader::Desktop ) ),
276  qApp->windowIcon().pixmap( IconSize( KIconLoader::Small ),
277  IconSize( KIconLoader::Small ) ) );
278  new MailSourceHighlighter( mRawBrowser->textBrowser()->document() );
279  mRawBrowser->textBrowser()->setFocus();
280 }
281 
282 MailSourceViewer::~MailSourceViewer()
283 {
284 }
285 
286 void MailSourceViewer::setRawSource( const QString &source )
287 {
288  mRawBrowser->setText( source );
289 }
290 
291 void MailSourceViewer::setDisplayedSource( const QString &source )
292 {
293 #ifndef NDEBUG
294  mHtmlBrowser->setPlainText( HTMLPrettyFormatter::reformat( source ) );
295 #else
296  Q_UNUSED( source );
297 #endif
298 }
299 
300 void MailSourceViewer::setFixedFont()
301 {
302  mRawBrowser->setFixedFont();
303 #ifndef NDEBUG
304  mHtmlBrowser->setFixedFont();
305 #endif
306 }
307 
308 #include "mailsourceviewer.moc"
309 }
MessageViewer::MailSourceViewTextBrowser::MailSourceViewTextBrowser
MailSourceViewTextBrowser(QWidget *parent=0)
Definition: mailsourceviewer.cpp:106
MessageViewer::MailSourceViewTextBrowser
Definition: mailsourceviewer.h:79
MessageViewer::Util::speakSelectedText
bool MESSAGEVIEWER_EXPORT speakSelectedText(const QString &text, QWidget *parent)
Definition: util.cpp:530
QWidget
MessageViewer::MailSourceViewer::MailSourceViewer
MailSourceViewer(QWidget *parent=0)
Definition: mailsourceviewer.cpp:236
KDialog
MessageViewer::FindBarSourceView
Definition: findbarsourceview.h:29
MessageViewer::MailSourceViewTextBrowser::findText
void findText()
MessageViewer::MailSourceViewTextBrowserWidget::setFixedFont
void setFixedFont()
Definition: mailsourceviewer.cpp:96
MessageViewer::MailSourceViewTextBrowserWidget::setPlainText
void setPlainText(const QString &text)
Definition: mailsourceviewer.cpp:91
MessageViewer::HTMLPrettyFormatter::htmlTagRegExp
const QRegExp htmlTagRegExp(QLatin1String("<""(/)?""(\\w+)""(?:""(?:\\s+""(?:\\w+)""(?:""\\s*=\\s*""(?:""\\\"(?:[^\\\"]*)\\\"""|'(?:[^']*)'""|(?:[^'"">\\s]+)"")"")?"")+\\s*""|\\s*"")""(/)?>"))
findbarsourceview.h
mailsourceviewer.h
MessageViewer::MailSourceHighlighter::highlightBlock
void highlightBlock(const QString &text)
Definition: mailsourceviewer.cpp:148
MessageViewer::MailSourceViewTextBrowserWidget::textBrowser
MessageViewer::MailSourceViewTextBrowser * textBrowser() const
Definition: mailsourceviewer.cpp:101
MessageViewer::MailSourceViewTextBrowserWidget::setText
void setText(const QString &text)
Definition: mailsourceviewer.cpp:86
MessageViewer::MailSourceHighlighter
Definition: mailsourceviewer.h:52
MessageViewer::MailSourceViewTextBrowserWidget
Definition: mailsourceviewer.h:62
QPlainTextEdit
MessageViewer::MailSourceViewer::setRawSource
void setRawSource(const QString &source)
Definition: mailsourceviewer.cpp:286
util.h
MessageViewer::FindBarBase::setText
void setText(const QString &text)
Definition: findbarbase.cpp:110
MessageViewer::MailSourceViewer::~MailSourceViewer
~MailSourceViewer()
Definition: mailsourceviewer.cpp:282
MessageViewer::MailSourceViewTextBrowserWidget::MailSourceViewTextBrowserWidget
MailSourceViewTextBrowserWidget(QWidget *parent=0)
Definition: mailsourceviewer.cpp:60
MessageViewer::MailSourceViewTextBrowser::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *event)
Definition: mailsourceviewer.cpp:111
MessageViewer::MailSourceViewer::setDisplayedSource
void setDisplayedSource(const QString &source)
Definition: mailsourceviewer.cpp:291
MessageViewer::FindBarBase::focusAndSetCursor
void focusAndSetCursor()
Definition: findbarbase.cpp:115
MessageViewer::HTMLPrettyFormatter::reformat
const QString reformat(const QString &src)
Definition: mailsourceviewer.cpp:180
MessageViewer::MailSourceViewer::setFixedFont
void setFixedFont()
Definition: mailsourceviewer.cpp:300
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • 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