23 #include <tqpopupmenu.h>
24 #include <tqpushbutton.h>
25 #include <tqtextedit.h>
27 #include <tqlineedit.h>
28 #include <tqtoolbox.h>
29 #include <tqtooltip.h>
30 #include <tqwhatsthis.h>
34 #include <tdelocale.h>
35 #include <tdeglobal.h>
36 #include <kiconloader.h>
37 #include <kpushbutton.h>
38 #include <tdelistview.h>
39 #include <klineedit.h>
40 #include <tqcombobox.h>
41 #include <tdeshortcut.h>
42 #include <tdemessagebox.h>
43 #include <kkeybutton.h>
44 #include <kactivelabel.h>
46 #include "customtemplates_base.h"
47 #include "customtemplates_kfg.h"
48 #include "globalsettings.h"
50 #include "kmmainwidget.h"
51 #include "kmfawidgets.h"
53 #include "customtemplates.h"
55 CustomTemplates::CustomTemplates( TQWidget *parent,
const char *name )
56 :CustomTemplatesBase( parent, name ),
58 mBlockChangeSignal( false )
60 TQFont f = TDEGlobalSettings::fixedFont();
63 mAdd->setIconSet( BarIconSet(
"add", TDEIcon::SizeSmall ) );
64 mRemove->setIconSet( BarIconSet(
"remove", TDEIcon::SizeSmall ) );
66 mList->setColumnWidth( 0, 50 );
67 mList->setColumnWidth( 1, 100 );
69 mEditFrame->setEnabled(
false );
71 connect( mName, TQ_SIGNAL( textChanged (
const TQString &) ),
72 this, TQ_SLOT( slotNameChanged(
const TQString & ) ) );
73 connect( mEdit, TQ_SIGNAL( textChanged() ),
74 this, TQ_SLOT( slotTextChanged(
void ) ) );
75 connect( mToEdit, TQ_SIGNAL( textChanged(
const TQString&) ),
76 this, TQ_SLOT( slotTextChanged(
void ) ) );
77 connect( mCCEdit, TQ_SIGNAL( textChanged(
const TQString&) ),
78 this, TQ_SLOT( slotTextChanged(
void ) ) );
80 connect( mInsertCommand, TQ_SIGNAL( insertCommand(TQString,
int) ),
81 this, TQ_SLOT( slotInsertCommand(TQString,
int) ) );
83 connect( mAdd, TQ_SIGNAL( clicked() ),
84 this, TQ_SLOT( slotAddClicked() ) );
85 connect( mRemove, TQ_SIGNAL( clicked() ),
86 this, TQ_SLOT( slotRemoveClicked() ) );
87 connect( mList, TQ_SIGNAL( selectionChanged() ),
88 this, TQ_SLOT( slotListSelectionChanged() ) );
89 connect( mType, TQ_SIGNAL( activated(
int ) ),
90 this, TQ_SLOT( slotTypeActivated(
int ) ) );
92 connect( mKeyButton, TQ_SIGNAL( capturedShortcut(
const TDEShortcut& ) ),
93 this, TQ_SLOT( slotShortcutCaptured(
const TDEShortcut& ) ) );
95 mReplyPix = TDEIconLoader().loadIcon(
"mail-reply-sender", TDEIcon::Small );
96 mReplyAllPix = TDEIconLoader().loadIcon(
"mail-reply-all", TDEIcon::Small );
97 mForwardPix = TDEIconLoader().loadIcon(
"mail-forward", TDEIcon::Small );
100 mType->insertItem( TQPixmap(), i18n(
"Message->",
"Universal" ), TUniversal );
101 mType->insertItem( mReplyPix, i18n(
"Message->",
"Reply" ), TReply );
102 mType->insertItem( mReplyAllPix, i18n(
"Message->",
"Reply to All" ), TReplyAll );
103 mType->insertItem( mForwardPix, i18n(
"Message->",
"Forward" ), TForward );
107 "<p>Here you can add, edit, and delete custom message "
108 "templates to use when you compose a reply or forwarding message. "
109 "Create the custom template by selecting it using the right mouse "
110 " button menu or toolbar menu. Also, you can bind a keyboard "
111 "combination to the template for faster operations.</p>"
112 "<p>Message templates support substitution commands "
113 "by simple typing them or selecting them from menu "
114 "<i>Insert command</i>.</p>"
115 "<p>There are four types of custom templates: used to "
116 "<i>Reply</i>, <i>Reply to All</i>, <i>Forward</i>, and "
117 "<i>Universal</i> which can be used for all kind of operations. "
118 "You cannot bind keyboard shortcut to <i>Universal</i> templates.</p>"
120 mHelp->setText( i18n(
"<a href=\"whatsthis:%1\">How does this work?</a>" ).arg( help ) );
122 const TQString toToolTip = i18n(
"Additional recipients of the message when forwarding" );
123 const TQString ccToolTip = i18n(
"Additional recipients who get a copy of the message when forwarding" );
124 const TQString toWhatsThis = i18n(
"When using this template for forwarding, the default recipients are those you enter here. This is a comma-separated list of mail addresses." );
125 const TQString ccWhatsThis = i18n(
"When using this template for forwarding, the recipients you enter here will by default get a copy of this message. This is a comma-separated list of mail addresses." );
130 KLineEdit *ccLineEdit =
dynamic_cast<KLineEdit*
>( mCCEdit->child(
"addressEdit" ) );
131 KLineEdit *toLineEdit =
dynamic_cast<KLineEdit*
>( mToEdit->child(
"addressEdit" ) );
132 Q_ASSERT( ccLineEdit && toLineEdit );
134 TQToolTip::add( mCCLabel, ccToolTip );
135 TQToolTip::add( ccLineEdit, ccToolTip );
136 TQToolTip::add( mToLabel, toToolTip );
137 TQToolTip::add( toLineEdit, toToolTip );
138 TQWhatsThis::add( mCCLabel, ccWhatsThis );
139 TQWhatsThis::add( ccLineEdit, ccWhatsThis );
140 TQWhatsThis::add( mToLabel, toWhatsThis );
141 TQWhatsThis::add( toLineEdit, toWhatsThis );
143 slotNameChanged( mName->text() );
146 CustomTemplates::~CustomTemplates()
148 TQDictIterator<CustomTemplateItem> it(mItemList);
149 for ( ; it.current() ; ++it ) {
150 CustomTemplateItem *vitem = mItemList.take( it.currentKey() );
157 void CustomTemplates::setRecipientsEditsEnabled(
bool enabled )
159 mToEdit->setHidden( !enabled );
160 mCCEdit->setHidden( !enabled );
161 mToLabel->setHidden( !enabled );
162 mCCLabel->setHidden( !enabled );
165 void CustomTemplates::slotNameChanged(
const TQString& text )
167 mAdd->setEnabled( !text.isEmpty() );
170 TQString CustomTemplates::indexToType(
int index )
180 typeStr = i18n(
"Message->",
"Reply" );
break;
182 typeStr = i18n(
"Message->",
"Reply to All" );
break;
184 typeStr = i18n(
"Message->",
"Forward" );
break;
186 typeStr = i18n(
"Message->",
"Unknown" );
break;
191 void CustomTemplates::slotTextChanged()
193 if ( !mBlockChangeSignal )
197 void CustomTemplates::load()
199 TQStringList list = GlobalSettings::self()->customTemplates();
200 for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it ) {
204 TDEShortcut shortcut( t.shortcut() );
205 CustomTemplateItem *vitem =
206 new CustomTemplateItem( *it, t.content(),
208 static_cast<Type
>( t.type() ), t.to(), t.cC() );
209 mItemList.insert( *it, vitem );
210 TQListViewItem *item =
new TQListViewItem( mList, typeStr, *it, t.content() );
211 switch ( t.type() ) {
213 item->setPixmap( 0, mReplyPix );
216 item->setPixmap( 0, mReplyAllPix );
219 item->setPixmap( 0, mForwardPix );
222 item->setPixmap( 0, TQPixmap() );
223 item->setText( 0, indexToType( t.type() ) );
229 void CustomTemplates::save()
233 for ( TQStringList::const_iterator it = mItemsToDelete.constBegin();
234 it != mItemsToDelete.constEnd(); ++it ) {
235 CTemplates t( (*it) );
236 const TQString configGroup = t.currentGroup();
237 kmkernel->config()->deleteGroup( configGroup );
240 if ( mCurrentItem ) {
241 CustomTemplateItem *vitem = mItemList[ mCurrentItem->text( 1 ) ];
243 vitem->mContent = mEdit->text();
244 vitem->mShortcut = mKeyButton->shortcut();
245 vitem->mTo = mToEdit->text();
246 vitem->mCC = mCCEdit->text();
250 TQListViewItemIterator lit( mList );
251 while ( lit.current() ) {
252 list.append( (*lit)->text( 1 ) );
255 for ( TQDictIterator<CustomTemplateItem> it( mItemList ); it.current() ; ++it ) {
257 CTemplates t( (*it)->mName );
258 TQString &content = (*it)->mContent;
259 if ( content.stripWhiteSpace().isEmpty() ) {
262 t.setContent( content );
263 t.setShortcut( (*it)->mShortcut.toString() );
264 t.setType( (*it)->mType );
265 t.setTo( (*it)->mTo );
266 t.setCC( (*it)->mCC );
269 GlobalSettings::self()->setCustomTemplates( list );
270 GlobalSettings::self()->writeConfig();
273 if ( kmkernel->getKMMainWidget() )
274 kmkernel->getKMMainWidget()->updateCustomTemplateMenus();
277 void CustomTemplates::slotInsertCommand( TQString cmd,
int adjustCursor )
280 mEdit->getCursorPosition( ¶, &index );
281 mEdit->insertAt( cmd, para, index );
283 index += adjustCursor;
285 mEdit->setCursorPosition( para, index + cmd.length() );
288 void CustomTemplates::slotAddClicked()
290 TQString str = mName->text();
291 if ( !str.isEmpty() ) {
292 CustomTemplateItem *vitem = mItemList[ str ];
294 vitem =
new CustomTemplateItem( str,
"", TDEShortcut::null(), TUniversal,
295 TQString(), TQString() );
296 mItemList.insert( str, vitem );
297 TQListViewItem *item =
298 new TQListViewItem( mList, indexToType( TUniversal ), str,
"" );
299 mList->setSelected( item,
true );
300 mKeyButton->setEnabled(
false );
301 if ( !mBlockChangeSignal )
307 void CustomTemplates::slotRemoveClicked()
309 if ( mCurrentItem ) {
310 const TQString templateName = mCurrentItem->text( 1 );
311 mItemsToDelete.append( templateName );
312 CustomTemplateItem *vitem = mItemList.take( templateName );
316 if ( !mBlockChangeSignal )
321 void CustomTemplates::slotListSelectionChanged()
323 if ( mCurrentItem ) {
324 CustomTemplateItem *vitem = mItemList[ mCurrentItem->text( 1 ) ];
326 vitem->mContent = mEdit->text();
327 vitem->mShortcut = mKeyButton->shortcut();
330 TQListViewItem *item = mList->selectedItem();
332 mEditFrame->setEnabled(
true );
334 CustomTemplateItem *vitem = mItemList[ mCurrentItem->text( 1 ) ];
337 mBlockChangeSignal =
true;
338 mEdit->setText( vitem->mContent );
339 mKeyButton->setShortcut( vitem->mShortcut,
false );
340 mType->setCurrentItem( vitem->mType );
341 mToEdit->setText( vitem->mTo );
342 mCCEdit->setText( vitem->mCC );
343 mBlockChangeSignal =
false;
345 if ( vitem->mType == TUniversal )
347 mKeyButton->setEnabled(
false );
349 mKeyButton->setEnabled(
true );
351 setRecipientsEditsEnabled( vitem->mType == TForward ||
352 vitem->mType == TUniversal );
355 mEditFrame->setEnabled(
false );
360 mKeyButton->setShortcut( TDEShortcut::null(),
false );
361 mType->setCurrentItem( 0 );
365 void CustomTemplates::slotTypeActivated(
int index )
367 if ( mCurrentItem ) {
369 CustomTemplateItem *vitem = mItemList[ mCurrentItem->text( 1 ) ];
373 vitem->mType =
static_cast<Type
>(index);
374 switch ( vitem->mType ) {
376 mCurrentItem->setPixmap( 0, mReplyPix );
379 mCurrentItem->setPixmap( 0, mReplyAllPix );
382 mCurrentItem->setPixmap( 0, mForwardPix );
385 mCurrentItem->setPixmap( 0, TQPixmap() );
388 if ( index == TUniversal )
390 mKeyButton->setEnabled(
false );
392 mKeyButton->setEnabled(
true );
395 setRecipientsEditsEnabled( vitem->mType == TForward ||
396 vitem->mType == TUniversal );
397 if ( !mBlockChangeSignal )
401 setRecipientsEditsEnabled(
false );
404 void CustomTemplates::slotShortcutCaptured(
const TDEShortcut &shortcut )
406 TDEShortcut sc( shortcut );
407 if ( sc == mKeyButton->shortcut() )
409 if ( sc.isNull() || sc.toString().isEmpty() )
412 bool customused =
false;
414 TQDictIterator<CustomTemplateItem> it(mItemList);
415 for ( ; it.current() ; ++it ) {
416 if ( !mCurrentItem || (*it)->mName != mCurrentItem->text( 1 ) )
418 if ( (*it)->mShortcut == sc )
420 TQString title( I18N_NOOP(
"Key Conflict") );
421 TQString msg( I18N_NOOP(
"The selected shortcut is already used "
422 "for another custom template, "
423 "would you still like to continue with the assignment?" ) );
424 assign = ( KMessageBox::warningYesNo(
this, msg, title )
425 == KMessageBox::Yes );
428 (*it)->mShortcut = TDEShortcut::null();
435 if ( !customused && !sc.isNull() &&
436 !( kmkernel->getKMMainWidget()->shortcutIsValid( sc ) ) ) {
437 TQString title( I18N_NOOP(
"Key Conflict") );
438 TQString msg( I18N_NOOP(
"The selected shortcut is already used, "
439 "would you still like to continue with the assignment?" ) );
440 assign = ( KMessageBox::warningYesNo(
this, msg, title )
441 == KMessageBox::Yes );
444 mKeyButton->setShortcut( sc,
false );
445 if ( !mBlockChangeSignal )
450 #include "customtemplates.moc"