korganizer

templatemanagementdialog.cpp
1 /*******************************************************************************
2 **
3 ** Filename : templatemanagementdialog.cpp
4 ** Created on : 05 June, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
7 **
8 *******************************************************************************/
9 
10 /*******************************************************************************
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** It is distributed in the hope that it will be useful, but
18 ** WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 **
26 ** In addition, as a special exception, the copyright holders give
27 ** permission to link the code of this program with any edition of
28 ** the TQt library by Trolltech AS, Norway (or with modified versions
29 ** of TQt that use the same license as TQt), and distribute linked
30 ** combinations including the two. You must obey the GNU General
31 ** Public License in all respects for all of the code used other than
32 ** TQt. If you modify this file, you may extend this exception to
33 ** your version of the file, but you are not obligated to do so. If
34 ** you do not wish to do so, delete this exception statement from
35 ** your version.
36 **
37 *******************************************************************************/
38 #include "templatemanagementdialog.h"
39 
40 #include <tqstringlist.h>
41 #include <tqtimer.h>
42 
43 #include <kpushbutton.h>
44 #include <kinputdialog.h>
45 #include <tdelocale.h>
46 #include <tdemessagebox.h>
47 
48 TemplateManagementDialog::TemplateManagementDialog(TQWidget *parent, const TQStringList &templates )
49  :KDialogBase( parent, "template_management_dialog", true,
50  i18n("Manage Templates"), Ok|Cancel, Ok, true , i18n("Apply Template")),
51  m_templates( templates ), m_newTemplate( TQString() ), m_changed( false )
52 {
53  m_base = new TemplateManagementDialog_base( this, "template_management_dialog_base" );
54  setMainWidget( m_base );
55  connect( m_base->m_buttonAdd, TQ_SIGNAL( clicked() ),
56  TQ_SLOT( slotAddTemplate() ) );
57  connect( m_base->m_buttonDelete, TQ_SIGNAL( clicked() ),
58  TQ_SLOT( slotDeleteTemplate() ) );
59  m_base->m_listBox->insertStringList( m_templates );
60  connect( m_base->m_listBox, TQ_SIGNAL( selectionChanged( TQListBoxItem * ) ),
61  TQ_SLOT( slotUpdateDeleteButton( TQListBoxItem * ) ) );
62  connect( m_base->m_buttonApply, TQ_SIGNAL( clicked() ),
63  TQ_SLOT( slotApplyTemplate() ) );
64 
65 }
66 
67 void TemplateManagementDialog::slotAddTemplate()
68 {
69  bool ok;
70  bool duplicate = false;
71  const TQString newTemplate = KInputDialog::getText( i18n("Template Name"),
72  i18n("Please enter a name for the new template:"),
73  i18n("New Template"), &ok );
74  if ( newTemplate.isEmpty() || !ok ) return;
75  if ( m_templates.find( newTemplate) != m_templates.end() ) {
76  int rc = KMessageBox::warningContinueCancel( this, i18n("A template with that name already exists, do you want to overwrite it?."), i18n("Duplicate Template Name"), i18n("Overwrite"));
77  if ( rc == KMessageBox::Cancel ) {
78  TQTimer::singleShot(0, this, TQ_SLOT( slotAddTemplate() ) );
79  return;
80  }
81  duplicate = true;
82  }
83  if ( !duplicate ) {
84  m_templates.append( newTemplate );
85  m_base->m_listBox->clear();
86  m_base->m_listBox->insertStringList( m_templates );
87  }
88  m_newTemplate = newTemplate;
89  m_changed = true;
90  // From this point on we need to keep the original event around until the user has
91  // closed the dialog, applying a template would make little sense
92  m_base->m_buttonApply->setEnabled( false );
93  // neither does adding it again
94  m_base->m_buttonAdd->setEnabled( false );
95 }
96 
97 void TemplateManagementDialog::slotDeleteTemplate()
98 {
99  TQListBoxItem *const item = m_base->m_listBox->selectedItem();
100  if ( !item ) return; // can't happen (TM)
101  unsigned int current = m_base->m_listBox->index(item);
102  m_templates.remove( item->text() );
103  m_base->m_listBox->removeItem( m_base->m_listBox->currentItem() );
104  m_changed = true;
105  m_base->m_listBox->setSelected(TQMAX(current -1, 0), true);
106 }
107 
108 void TemplateManagementDialog::slotUpdateDeleteButton( TQListBoxItem *item )
109 {
110  m_base->m_buttonDelete->setEnabled( item != 0 );
111 }
112 
113 void TemplateManagementDialog::slotApplyTemplate()
114 {
115  // Once the user has applied the current template to the event, it makes no sense to add it again
116  m_base->m_buttonAdd->setEnabled( false );
117  const TQString &cur = m_base->m_listBox->currentText();
118  if ( !cur.isEmpty() && cur != m_newTemplate )
119  emit loadTemplate( cur );
120 }
121 
122 void TemplateManagementDialog::slotOk()
123 {
124  // failure is not an option *cough*
125  if ( !m_newTemplate.isEmpty() )
126  emit saveTemplate( m_newTemplate );
127  if ( m_changed )
128  emit templatesChanged( m_templates );
129  KDialogBase::slotOk();
130 }
131 
132 
133 #include "templatemanagementdialog.moc"