File indexing completed on 2024-11-24 04:52:56

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "ReplaceSignature.h"
0024 #include <QTextBlock>
0025 #include <QTextCursor>
0026 #include <QTextDocument>
0027 
0028 namespace Composer {
0029 namespace Util {
0030 
0031 void replaceSignature(QTextDocument *document, const QString &newSignature)
0032 {
0033     // The QTextEdit is set up in such a way as to treat a fully terminated line as a standalone text block,
0034     // hence no newlines in the signature separator
0035     const QLatin1String signatureSeperator("-- ");
0036 
0037     QTextBlock block = document->lastBlock();
0038     while (block.isValid() && block.blockNumber() > 0) {
0039         if (block.text() == signatureSeperator) {
0040             // So this block holds the last signature separator -- great!
0041             break;
0042         }
0043         block = block.previous();
0044     }
0045 
0046     QTextCursor cursor(block);
0047     if (block.text() == signatureSeperator) {
0048         // Remove everything till the end of the document since the end of the previous block
0049         if (block.previous().isValid()) {
0050             // Prevent adding newlines when switching signatures
0051             block = block.previous();
0052             cursor = QTextCursor(block);
0053             cursor.movePosition(QTextCursor::EndOfBlock);
0054         }
0055         cursor.beginEditBlock();
0056         cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
0057         cursor.removeSelectedText();
0058         cursor.endEditBlock();
0059     } else {
0060         // We have not removed anything, so we have to "fake" an edit action so that we're adding the signature to a correct place
0061         block = document->lastBlock();
0062         cursor = QTextCursor(block);
0063         cursor.movePosition(QTextCursor::EndOfBlock);
0064         cursor.beginEditBlock();
0065         cursor.endEditBlock();
0066     }
0067 
0068     if (!newSignature.isEmpty()) {
0069         cursor.joinPreviousEditBlock();
0070         cursor.insertBlock();
0071         cursor.insertText(signatureSeperator);
0072         cursor.insertBlock();
0073         cursor.insertText(newSignature);
0074         cursor.endEditBlock();
0075     }
0076 }
0077 
0078 }
0079 }
0080 
0081