Fix off by one error causing the scrollbar to show up.
[kdepim.git] / templateparser / templatesconfiguration.cpp
blobff3aa1b659e2d792e254b03bd56262e579df0a05
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 * kmail: KDE mail client
3 * Copyright (C) 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "templatesconfiguration.h"
21 #include "ui_templatesconfiguration_base.h"
22 #include "templatesconfiguration_kfg.h"
23 #include "globalsettings_base.h"
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <kglobal.h>
29 #include <qpushbutton.h>
30 #include <qtextedit.h>
31 #include <qlineedit.h>
32 #include <QWhatsThis>
33 #include <qtoolbox.h>
34 #include <qfont.h>
36 TemplatesConfiguration::TemplatesConfiguration( QWidget *parent, const char *name )
37 : QWidget( parent )
39 setupUi(this);
40 setObjectName(name);
42 QFont f = KGlobalSettings::fixedFont();
43 textEdit_new->setFont( f );
44 textEdit_reply->setFont( f );
45 textEdit_reply_all->setFont( f );
46 textEdit_forward->setFont( f );
48 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
49 sizeHint();
51 connect( textEdit_new, SIGNAL( textChanged() ),
52 this, SLOT( slotTextChanged( void ) ) );
53 connect( textEdit_reply, SIGNAL( textChanged() ),
54 this, SLOT( slotTextChanged( void ) ) );
55 connect( textEdit_reply_all, SIGNAL( textChanged() ),
56 this, SLOT( slotTextChanged( void ) ) );
57 connect( textEdit_forward, SIGNAL( textChanged() ),
58 this, SLOT( slotTextChanged( void ) ) );
59 connect( lineEdit_quote, SIGNAL( textChanged( const QString & ) ),
60 this, SLOT( slotTextChanged( void ) ) );
62 connect( mInsertCommand, SIGNAL( insertCommand(const QString&, int) ),
63 this, SLOT( slotInsertCommand(const QString &, int) ) );
65 mHelpString =
66 i18n( "<p>Here you can create and manage templates to use when "
67 "composing new messages, replies or forwarded messages.</p>"
68 "<p>The message templates support substitution commands, "
69 "either simply type them or select them from "
70 "the <i>Insert command</i> menu.</p>" );
71 if ( QString( name ) == "folder-templates" ) {
72 mHelpString +=
73 i18n( "<p>Templates specified here are folder-specific. "
74 "They override both global templates and per-identity "
75 "templates.</p>" );
76 } else if ( QString( name ) == "identity-templates" ) {
77 mHelpString +=
78 i18n( "<p>Templates specified here are identity-specific. "
79 "They override global templates, but can be overridden by "
80 "per-folder templates if they are specified.</p>" );
81 } else {
82 mHelpString +=
83 i18n( "<p>These are global (default) templates. They can be overridden "
84 "by per-identity templates or per-folder templates "
85 "if they are specified.</p>" );
88 mHelp->setText( i18n( "<a href=\"whatsthis\">How does this work?</a>" ) );
89 connect( mHelp, SIGNAL( linkActivated ( const QString& ) ),
90 this, SLOT( slotHelpLinkClicked( const QString& ) ) );
94 void TemplatesConfiguration::slotHelpLinkClicked( const QString& )
96 QWhatsThis::showText( QCursor::pos(), mHelpString );
99 void TemplatesConfiguration::slotTextChanged()
101 emit changed();
104 void TemplatesConfiguration::resetToDefault()
106 textEdit_new->setText( DefaultTemplates::defaultNewMessage() );
107 textEdit_reply->setText( DefaultTemplates::defaultReply() );
108 textEdit_reply_all->setText( DefaultTemplates::defaultReplyAll() );
109 textEdit_forward->setText( DefaultTemplates::defaultForward() );
110 lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
113 void TemplatesConfiguration::loadFromGlobal()
115 QString str;
116 str = TemplateParser::GlobalSettings::self()->templateNewMessage();
117 if ( str.isEmpty() ) {
118 textEdit_new->setText( DefaultTemplates::defaultNewMessage() );
119 } else {
120 textEdit_new->setText(str);
122 str = TemplateParser::GlobalSettings::self()->templateReply();
123 if ( str.isEmpty() ) {
124 textEdit_reply->setText( DefaultTemplates::defaultReply() );
125 } else {
126 textEdit_reply->setText( str );
128 str = TemplateParser::GlobalSettings::self()->templateReplyAll();
129 if ( str.isEmpty() ) {
130 textEdit_reply_all->setText( DefaultTemplates::defaultReplyAll() );
131 } else {
132 textEdit_reply_all->setText( str );
134 str = TemplateParser::GlobalSettings::self()->templateForward();
135 if ( str.isEmpty() ) {
136 textEdit_forward->setText( DefaultTemplates::defaultForward() );
137 } else {
138 textEdit_forward->setText( str );
140 str = TemplateParser::GlobalSettings::self()->quoteString();
141 if ( str.isEmpty() ) {
142 lineEdit_quote->setText( DefaultTemplates::defaultQuoteString() );
143 } else {
144 lineEdit_quote->setText( str );
148 void TemplatesConfiguration::saveToGlobal()
150 TemplateParser::GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
151 TemplateParser::GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
152 TemplateParser::GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
153 TemplateParser::GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
154 TemplateParser::GlobalSettings::self()->setQuoteString( lineEdit_quote->text() );
155 TemplateParser::GlobalSettings::self()->writeConfig();
158 void TemplatesConfiguration::loadFromIdentity( uint id )
160 Templates t( configIdString( id ) );
162 QString str;
164 str = t.templateNewMessage();
165 if ( str.isEmpty() ) {
166 str = TemplateParser::GlobalSettings::self()->templateNewMessage();
168 if ( str.isEmpty() ) {
169 str = DefaultTemplates::defaultNewMessage();
171 textEdit_new->setText( str );
173 str = t.templateReply();
174 if ( str.isEmpty() ) {
175 str = TemplateParser::GlobalSettings::self()->templateReply();
177 if ( str.isEmpty() ) {
178 str = DefaultTemplates::defaultReply();
180 textEdit_reply->setText( str );
182 str = t.templateReplyAll();
183 if ( str.isEmpty() ) {
184 str = TemplateParser::GlobalSettings::self()->templateReplyAll();
186 if ( str.isEmpty() ) {
187 str = DefaultTemplates::defaultReplyAll();
189 textEdit_reply_all->setText( str );
191 str = t.templateForward();
192 if ( str.isEmpty() ) {
193 str = TemplateParser::GlobalSettings::self()->templateForward();
195 if ( str.isEmpty() ) {
196 str = DefaultTemplates::defaultForward();
198 textEdit_forward->setText( str );
200 str = t.quoteString();
201 if ( str.isEmpty() ) {
202 str = TemplateParser::GlobalSettings::self()->quoteString();
204 if ( str.isEmpty() ) {
205 str = DefaultTemplates::defaultQuoteString();
207 lineEdit_quote->setText( str );
210 void TemplatesConfiguration::saveToIdentity( uint id )
212 Templates t( configIdString( id ) );
213 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
214 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
215 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
216 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
217 t.setQuoteString( lineEdit_quote->text() );
218 t.writeConfig();
221 void TemplatesConfiguration::loadFromFolder( const QString &id, uint identity )
223 Templates t( id );
224 Templates* tid = 0;
226 if ( identity ) {
227 tid = new Templates( configIdString( identity ) );
230 QString str;
232 str = t.templateNewMessage();
233 if ( str.isEmpty() && tid ) {
234 str = tid->templateNewMessage();
236 if ( str.isEmpty() ) {
237 str = TemplateParser::GlobalSettings::self()->templateNewMessage();
239 if ( str.isEmpty() ) {
240 str = DefaultTemplates::defaultNewMessage();
242 textEdit_new->setText( str );
244 str = t.templateReply();
245 if ( str.isEmpty() && tid ) {
246 str = tid->templateReply();
248 if ( str.isEmpty() ) {
249 str = TemplateParser::GlobalSettings::self()->templateReply();
251 if ( str.isEmpty() ) {
252 str = DefaultTemplates::defaultReply();
254 textEdit_reply->setText( str );
256 str = t.templateReplyAll();
257 if ( str.isEmpty() && tid ) {
258 str = tid->templateReplyAll();
260 if ( str.isEmpty() ) {
261 str = TemplateParser::GlobalSettings::self()->templateReplyAll();
263 if ( str.isEmpty() ) {
264 str = DefaultTemplates::defaultReplyAll();
266 textEdit_reply_all->setText( str );
268 str = t.templateForward();
269 if ( str.isEmpty() && tid ) {
270 str = tid->templateForward();
272 if ( str.isEmpty() ) {
273 str = TemplateParser::GlobalSettings::self()->templateForward();
275 if ( str.isEmpty() ) {
276 str = DefaultTemplates::defaultForward();
278 textEdit_forward->setText( str );
280 str = t.quoteString();
281 if ( str.isEmpty() && tid ) {
282 str = tid->quoteString();
284 if ( str.isEmpty() ) {
285 str = TemplateParser::GlobalSettings::self()->quoteString();
287 if ( str.isEmpty() ) {
288 str = DefaultTemplates::defaultQuoteString();
290 lineEdit_quote->setText( str );
292 delete(tid);
295 void TemplatesConfiguration::saveToFolder( const QString &id )
297 Templates t( id );
299 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
300 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
301 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
302 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
303 t.setQuoteString( lineEdit_quote->text() );
304 t.writeConfig();
307 void TemplatesConfiguration::slotInsertCommand( const QString &cmd, int adjustCursor )
309 KTextEdit* edit;
311 if( toolBox1->widget( toolBox1->currentIndex() ) == page_new ) {
312 edit = textEdit_new;
313 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply ) {
314 edit = textEdit_reply;
315 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply_all ) {
316 edit = textEdit_reply_all;
317 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_forward ) {
318 edit = textEdit_forward;
319 } else {
320 kDebug() << "Unknown current page in TemplatesConfiguration!";
321 return;
324 // kDebug() << "Insert command:" << cmd;
325 QTextCursor cursor = edit->textCursor();
326 cursor.insertText( cmd );
327 cursor.setPosition( cursor.position() + adjustCursor );
328 edit->setTextCursor( cursor );
329 edit->setFocus();
332 QString TemplatesConfiguration::strOrBlank( const QString &str ) {
333 if ( str.trimmed().isEmpty() ) {
334 return QString( "%BLANK" );
336 return str;
339 QString TemplatesConfiguration::configIdString( uint id )
341 return QString( "IDENTITY_%1" ).arg( id );
344 #include "templatesconfiguration.moc"