1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
* Copyright (C) 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
* Copyright (C) 2012 Laurent Montel <montel@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "templatesconfiguration.h"
#include "globalsettings_base.h"
#include "templatesconfiguration_kfg.h"
#include "pimcommon/texteditor/richtexteditor/richtexteditor.h"
#include <KMessageBox>
#include <QWhatsThis>
using namespace TemplateParser;
TemplatesConfiguration::TemplatesConfiguration( QWidget *parent, const QString &name )
: QWidget( parent )
{
setupUi( this );
setObjectName( name );
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
sizeHint();
connect( textEdit_new->editor(), SIGNAL(textChanged()),
this, SLOT(slotTextChanged()) );
connect( textEdit_reply->editor(), SIGNAL(textChanged()),
this, SLOT(slotTextChanged()) );
connect( textEdit_reply_all->editor(), SIGNAL(textChanged()),
this, SLOT(slotTextChanged()) );
connect( textEdit_forward->editor(), SIGNAL(textChanged()),
this, SLOT(slotTextChanged()) );
connect( lineEdit_quote, SIGNAL(textChanged(QString)),
this, SLOT(slotTextChanged()) );
connect( mInsertCommand, SIGNAL(insertCommand(QString,int)),
this, SLOT(slotInsertCommand(QString,int)) );
mHelpString =
i18n( "<p>Here you can create and manage templates to use when "
"composing new messages, replies or forwarded messages.</p>"
"<p>The message templates support substitution commands, "
"either simply type them or select them from "
"the <i>Insert command</i> menu.</p>" );
const QString templateConfigurationName( name );
if ( templateConfigurationName == QLatin1String( "folder-templates" ) ) {
mHelpString +=
i18n( "<p>Templates specified here are folder-specific. "
"They override both global templates and per-identity "
"templates.</p>" );
} else if ( templateConfigurationName == QLatin1String( "identity-templates" ) ) {
mHelpString +=
i18n( "<p>Templates specified here are identity-specific. "
"They override global templates, but can be overridden by "
"per-folder templates if they are specified.</p>" );
} else {
mHelpString +=
i18n( "<p>These are global (default) templates. They can be overridden "
"by per-identity templates or per-folder templates "
"if they are specified.</p>" );
}
mHelp->setText( i18n( "<a href=\"whatsthis\">How does this work?</a>" ) );
connect( mHelp, SIGNAL(linkActivated(QString)),
this, SLOT(slotHelpLinkClicked(QString)) );
}
void TemplatesConfiguration::slotHelpLinkClicked( const QString & )
{
QWhatsThis::showText( QCursor::pos(), mHelpString );
}
void TemplatesConfiguration::slotTextChanged()
{
emit changed();
}
void TemplatesConfiguration::resetToDefault()
{
const int choice =
KMessageBox::questionYesNoCancel(
0,
i18n( "Do you want to reset current template or all templates to default?" ),
i18n( "Reset to default" ),
KGuiItem( i18n( "Reset Current Template" ) ),
KGuiItem( i18n( "Reset All Templates" ) ),
KStandardGuiItem::cancel() );
if ( choice == KMessageBox::Cancel ) {
return;
} else if ( choice == KMessageBox::Yes ) {
const int toolboxCurrentIndex( toolBox1->currentIndex() );
if( toolBox1->widget( toolboxCurrentIndex ) == page_new ) {
textEdit_new->setPlainText( DefaultTemplates::defaultNewMessage() );
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_reply ) {
textEdit_reply->setPlainText( DefaultTemplates::defaultReply() );
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_reply_all ) {
textEdit_reply_all->setPlainText( DefaultTemplates::defaultReplyAll() );
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_forward ) {
textEdit_forward->setPlainText( DefaultTemplates::defaultForward() );
} else {
kDebug() << "Unknown current page in TemplatesConfiguration!";
}
} else {
textEdit_new->setPlainText( DefaultTemplates::defaultNewMessage() );
textEdit_reply->setPlainText( DefaultTemplates::defaultReply() );
textEdit_reply_all->setPlainText( DefaultTemplates::defaultReplyAll() );
textEdit_forward->setPlainText( DefaultTemplates::defaultForward() );
}
lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
}
void TemplatesConfiguration::loadFromGlobal()
{
QString str;
str = GlobalSettings::self()->templateNewMessage();
if ( str.isEmpty() ) {
textEdit_new->setPlainText( DefaultTemplates::defaultNewMessage() );
} else {
textEdit_new->setPlainText(str);
}
str = GlobalSettings::self()->templateReply();
if ( str.isEmpty() ) {
textEdit_reply->setPlainText( DefaultTemplates::defaultReply() );
} else {
textEdit_reply->setPlainText( str );
}
str = GlobalSettings::self()->templateReplyAll();
if ( str.isEmpty() ) {
textEdit_reply_all->setPlainText( DefaultTemplates::defaultReplyAll() );
} else {
textEdit_reply_all->setPlainText( str );
}
str = GlobalSettings::self()->templateForward();
if ( str.isEmpty() ) {
textEdit_forward->setPlainText( DefaultTemplates::defaultForward() );
} else {
textEdit_forward->setPlainText( str );
}
str = GlobalSettings::self()->quoteString();
if ( str.isEmpty() ) {
lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
} else {
lineEdit_quote->setText( str );
}
}
void TemplatesConfiguration::saveToGlobal()
{
GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
GlobalSettings::self()->setQuoteString( lineEdit_quote->text() );
GlobalSettings::self()->writeConfig();
}
void TemplatesConfiguration::loadFromIdentity( uint id )
{
Templates t( configIdString( id ) );
QString str;
str = t.templateNewMessage();
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateNewMessage();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultNewMessage();
}
textEdit_new->setPlainText( str );
str = t.templateReply();
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateReply();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultReply();
}
textEdit_reply->setPlainText( str );
str = t.templateReplyAll();
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateReplyAll();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultReplyAll();
}
textEdit_reply_all->setPlainText( str );
str = t.templateForward();
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateForward();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultForward();
}
textEdit_forward->setPlainText( str );
str = t.quoteString();
if ( str.isEmpty() ) {
str = GlobalSettings::self()->quoteString();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultQuoteString();
}
lineEdit_quote->setText( str );
}
void TemplatesConfiguration::saveToIdentity( uint id )
{
Templates t( configIdString( id ) );
t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
t.setQuoteString( lineEdit_quote->text() );
t.writeConfig();
}
void TemplatesConfiguration::loadFromFolder( const QString &id, uint identity )
{
Templates t( id );
Templates *tid = 0;
if ( identity ) {
tid = new Templates( configIdString( identity ) );
}
QString str;
str = t.templateNewMessage();
if ( str.isEmpty() && tid ) {
str = tid->templateNewMessage();
}
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateNewMessage();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultNewMessage();
}
textEdit_new->setPlainText( str );
str = t.templateReply();
if ( str.isEmpty() && tid ) {
str = tid->templateReply();
}
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateReply();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultReply();
}
textEdit_reply->setPlainText( str );
str = t.templateReplyAll();
if ( str.isEmpty() && tid ) {
str = tid->templateReplyAll();
}
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateReplyAll();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultReplyAll();
}
textEdit_reply_all->setPlainText( str );
str = t.templateForward();
if ( str.isEmpty() && tid ) {
str = tid->templateForward();
}
if ( str.isEmpty() ) {
str = GlobalSettings::self()->templateForward();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultForward();
}
textEdit_forward->setPlainText( str );
str = t.quoteString();
if ( str.isEmpty() && tid ) {
str = tid->quoteString();
}
if ( str.isEmpty() ) {
str = GlobalSettings::self()->quoteString();
}
if ( str.isEmpty() ) {
str = DefaultTemplates::defaultQuoteString();
}
lineEdit_quote->setText( str );
delete(tid);
}
void TemplatesConfiguration::saveToFolder( const QString &id )
{
Templates t( id );
t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
t.setQuoteString( lineEdit_quote->text() );
t.writeConfig();
}
QTextEdit *TemplatesConfiguration::currentTextEdit() const
{
QTextEdit *edit;
const int toolboxCurrentIndex( toolBox1->currentIndex() );
if( toolBox1->widget( toolboxCurrentIndex ) == page_new ) {
edit = textEdit_new->editor();
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_reply ) {
edit = textEdit_reply->editor();
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_reply_all ) {
edit = textEdit_reply_all->editor();
} else if( toolBox1->widget( toolboxCurrentIndex ) == page_forward ) {
edit = textEdit_forward->editor();
} else {
kDebug() << "Unknown current page in TemplatesConfiguration!";
edit = 0;
}
return edit;
}
void TemplatesConfiguration::slotInsertCommand( const QString &cmd, int adjustCursor )
{
QTextEdit *edit = currentTextEdit();
if ( !edit ) {
return;
}
// kDebug() << "Insert command:" << cmd;
const QString editText( edit->toPlainText() );
if ( ( editText.contains( QLatin1String("%FORCEDPLAIN") ) && ( cmd == QLatin1String( "%FORCEDHTML" ) ) ) ||
( editText.contains( QLatin1String("%FORCEDHTML") ) && ( cmd == QLatin1String( "%FORCEDPLAIN" ) ) ) ) {
KMessageBox::error(
this,
i18n( "Use of \"Reply using plain text\" and \"Reply using HTML text\" in pairs"
" is not correct. Use only one of the aforementioned commands with \" Reply as"
" Quoted Message command\" as per your need\n"
"(a)Reply using plain text for quotes to be strictly in plain text\n"
"(b)Reply using HTML text for quotes being in HTML format if present" ) );
} else {
QTextCursor cursor = edit->textCursor();
cursor.insertText( cmd );
cursor.setPosition( cursor.position() + adjustCursor );
edit->setTextCursor( cursor );
edit->setFocus();
}
}
QString TemplatesConfiguration::strOrBlank( const QString &str )
{
if ( str.trimmed().isEmpty() ) {
return QString::fromLatin1( "%BLANK" );
}
return str;
}
QString TemplatesConfiguration::configIdString( uint id )
{
return QString::fromLatin1( "IDENTITY_%1" ).arg( id );
}
|