Framework for looking up contacts directly in nepomuk in addition to going through...
[kdepim.git] / templateparser / templatesconfiguration.cpp
blob833463d262b5a4ce93cd9d6c1ed1e1b4a422f354
1 /*
2 * Copyright (C) 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "templatesconfiguration.h"
20 #include "globalsettings_base.h"
21 #include "templatesconfiguration_kfg.h"
23 #include <KMessageBox>
25 #include <QWhatsThis>
27 using namespace TemplateParser;
29 TemplatesConfiguration::TemplatesConfiguration( QWidget *parent, const char *name )
30 : QWidget( parent )
32 setupUi( this );
33 setObjectName( name );
35 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
36 sizeHint();
38 connect( textEdit_new, SIGNAL(textChanged()),
39 this, SLOT(slotTextChanged()) );
40 connect( textEdit_reply, SIGNAL(textChanged()),
41 this, SLOT(slotTextChanged()) );
42 connect( textEdit_reply_all, SIGNAL(textChanged()),
43 this, SLOT(slotTextChanged()) );
44 connect( textEdit_forward, SIGNAL(textChanged()),
45 this, SLOT(slotTextChanged()) );
46 connect( lineEdit_quote, SIGNAL(textChanged(QString)),
47 this, SLOT(slotTextChanged()) );
49 connect( mInsertCommand, SIGNAL(insertCommand(QString,int)),
50 this, SLOT(slotInsertCommand(QString,int)) );
52 mHelpString =
53 i18n( "<p>Here you can create and manage templates to use when "
54 "composing new messages, replies or forwarded messages.</p>"
55 "<p>The message templates support substitution commands, "
56 "either simply type them or select them from "
57 "the <i>Insert command</i> menu.</p>" );
58 const QString templateConfigurationName( name );
59 if ( templateConfigurationName == QLatin1String( "folder-templates" ) ) {
60 mHelpString +=
61 i18n( "<p>Templates specified here are folder-specific. "
62 "They override both global templates and per-identity "
63 "templates.</p>" );
64 } else if ( templateConfigurationName == QLatin1String( "identity-templates" ) ) {
65 mHelpString +=
66 i18n( "<p>Templates specified here are identity-specific. "
67 "They override global templates, but can be overridden by "
68 "per-folder templates if they are specified.</p>" );
69 } else {
70 mHelpString +=
71 i18n( "<p>These are global (default) templates. They can be overridden "
72 "by per-identity templates or per-folder templates "
73 "if they are specified.</p>" );
76 mHelp->setText( i18n( "<a href=\"whatsthis\">How does this work?</a>" ) );
77 connect( mHelp, SIGNAL(linkActivated(QString)),
78 this, SLOT(slotHelpLinkClicked(QString)) );
81 void TemplatesConfiguration::slotHelpLinkClicked( const QString & )
83 QWhatsThis::showText( QCursor::pos(), mHelpString );
86 void TemplatesConfiguration::slotTextChanged()
88 emit changed();
91 void TemplatesConfiguration::resetToDefault()
93 textEdit_new->setText( DefaultTemplates::defaultNewMessage() );
94 textEdit_reply->setText( DefaultTemplates::defaultReply() );
95 textEdit_reply_all->setText( DefaultTemplates::defaultReplyAll() );
96 textEdit_forward->setText( DefaultTemplates::defaultForward() );
97 lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
100 void TemplatesConfiguration::loadFromGlobal()
102 QString str;
103 str = GlobalSettings::self()->templateNewMessage();
104 if ( str.isEmpty() ) {
105 textEdit_new->setText( DefaultTemplates::defaultNewMessage() );
106 } else {
107 textEdit_new->setText(str);
109 str = GlobalSettings::self()->templateReply();
110 if ( str.isEmpty() ) {
111 textEdit_reply->setText( DefaultTemplates::defaultReply() );
112 } else {
113 textEdit_reply->setText( str );
115 str = GlobalSettings::self()->templateReplyAll();
116 if ( str.isEmpty() ) {
117 textEdit_reply_all->setText( DefaultTemplates::defaultReplyAll() );
118 } else {
119 textEdit_reply_all->setText( str );
121 str = GlobalSettings::self()->templateForward();
122 if ( str.isEmpty() ) {
123 textEdit_forward->setText( DefaultTemplates::defaultForward() );
124 } else {
125 textEdit_forward->setText( str );
127 str = GlobalSettings::self()->quoteString();
128 if ( str.isEmpty() ) {
129 lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
130 } else {
131 lineEdit_quote->setText( str );
135 void TemplatesConfiguration::saveToGlobal()
137 GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
138 GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
139 GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
140 GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
141 GlobalSettings::self()->setQuoteString( lineEdit_quote->text() );
142 GlobalSettings::self()->writeConfig();
145 void TemplatesConfiguration::loadFromIdentity( uint id )
147 Templates t( configIdString( id ) );
149 QString str;
151 str = t.templateNewMessage();
152 if ( str.isEmpty() ) {
153 str = GlobalSettings::self()->templateNewMessage();
155 if ( str.isEmpty() ) {
156 str = DefaultTemplates::defaultNewMessage();
158 textEdit_new->setText( str );
160 str = t.templateReply();
161 if ( str.isEmpty() ) {
162 str = GlobalSettings::self()->templateReply();
164 if ( str.isEmpty() ) {
165 str = DefaultTemplates::defaultReply();
167 textEdit_reply->setText( str );
169 str = t.templateReplyAll();
170 if ( str.isEmpty() ) {
171 str = GlobalSettings::self()->templateReplyAll();
173 if ( str.isEmpty() ) {
174 str = DefaultTemplates::defaultReplyAll();
176 textEdit_reply_all->setText( str );
178 str = t.templateForward();
179 if ( str.isEmpty() ) {
180 str = GlobalSettings::self()->templateForward();
182 if ( str.isEmpty() ) {
183 str = DefaultTemplates::defaultForward();
185 textEdit_forward->setText( str );
187 str = t.quoteString();
188 if ( str.isEmpty() ) {
189 str = GlobalSettings::self()->quoteString();
191 if ( str.isEmpty() ) {
192 str = DefaultTemplates::defaultQuoteString();
194 lineEdit_quote->setText( str );
197 void TemplatesConfiguration::saveToIdentity( uint id )
199 Templates t( configIdString( id ) );
200 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
201 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
202 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
203 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
204 t.setQuoteString( lineEdit_quote->text() );
205 t.writeConfig();
208 void TemplatesConfiguration::loadFromFolder( const QString &id, uint identity )
210 Templates t( id );
211 Templates *tid = 0;
213 if ( identity ) {
214 tid = new Templates( configIdString( identity ) );
217 QString str;
219 str = t.templateNewMessage();
220 if ( str.isEmpty() && tid ) {
221 str = tid->templateNewMessage();
223 if ( str.isEmpty() ) {
224 str = GlobalSettings::self()->templateNewMessage();
226 if ( str.isEmpty() ) {
227 str = DefaultTemplates::defaultNewMessage();
229 textEdit_new->setText( str );
231 str = t.templateReply();
232 if ( str.isEmpty() && tid ) {
233 str = tid->templateReply();
235 if ( str.isEmpty() ) {
236 str = GlobalSettings::self()->templateReply();
238 if ( str.isEmpty() ) {
239 str = DefaultTemplates::defaultReply();
241 textEdit_reply->setText( str );
243 str = t.templateReplyAll();
244 if ( str.isEmpty() && tid ) {
245 str = tid->templateReplyAll();
247 if ( str.isEmpty() ) {
248 str = GlobalSettings::self()->templateReplyAll();
250 if ( str.isEmpty() ) {
251 str = DefaultTemplates::defaultReplyAll();
253 textEdit_reply_all->setText( str );
255 str = t.templateForward();
256 if ( str.isEmpty() && tid ) {
257 str = tid->templateForward();
259 if ( str.isEmpty() ) {
260 str = GlobalSettings::self()->templateForward();
262 if ( str.isEmpty() ) {
263 str = DefaultTemplates::defaultForward();
265 textEdit_forward->setText( str );
267 str = t.quoteString();
268 if ( str.isEmpty() && tid ) {
269 str = tid->quoteString();
271 if ( str.isEmpty() ) {
272 str = GlobalSettings::self()->quoteString();
274 if ( str.isEmpty() ) {
275 str = DefaultTemplates::defaultQuoteString();
277 lineEdit_quote->setText( str );
279 delete(tid);
282 void TemplatesConfiguration::saveToFolder( const QString &id )
284 Templates t( id );
286 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
287 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
288 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
289 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
290 t.setQuoteString( lineEdit_quote->text() );
291 t.writeConfig();
294 void TemplatesConfiguration::slotInsertCommand( const QString &cmd, int adjustCursor )
296 KTextEdit *edit;
298 if( toolBox1->widget( toolBox1->currentIndex() ) == page_new ) {
299 edit = textEdit_new;
300 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply ) {
301 edit = textEdit_reply;
302 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply_all ) {
303 edit = textEdit_reply_all;
304 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_forward ) {
305 edit = textEdit_forward;
306 } else {
307 kDebug() << "Unknown current page in TemplatesConfiguration!";
308 return;
311 // kDebug() << "Insert command:" << cmd;
312 const QString editText( edit->toPlainText() );
313 if ( ( editText.contains( "%FORCEDPLAIN" ) && ( cmd == QLatin1String( "%FORCEDHTML" ) ) ) ||
314 ( editText.contains( "%FORCEDHTML" ) && ( cmd == QLatin1String( "%FORCEDPLAIN" ) ) ) ) {
315 KMessageBox::error(
316 this,
317 i18n( "Use of \"Reply using plain text\" and \"Reply using HTML text\" in pairs"
318 " is not correct. Use only one of the aforementioned commands with \" Reply as"
319 " Quoted Message command\" as per your need\n"
320 "(a)Reply using plain text for quotes to be strictly in plain text\n"
321 "(b)Reply using HTML text for quotes being in HTML format if present" ) );
322 } else {
323 QTextCursor cursor = edit->textCursor();
324 cursor.insertText( cmd );
325 cursor.setPosition( cursor.position() + adjustCursor );
326 edit->setTextCursor( cursor );
327 edit->setFocus();
331 QString TemplatesConfiguration::strOrBlank( const QString &str )
333 if ( str.trimmed().isEmpty() ) {
334 return QString::fromLatin1( "%BLANK" );
336 return str;
339 QString TemplatesConfiguration::configIdString( uint id )
341 return QString::fromLatin1( "IDENTITY_%1" ).arg( id );
344 #include "templatesconfiguration.moc"