Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / plugins / autoreplace / autoreplacepreferences.cpp
blob2d6edb76999004431ee6097a37655ca6f616f47f
1 /***************************************************************************
2 autoreplacepreferences.cpp - description
3 -------------------
4 begin : 20030426
5 copyright : (C) 2003 by Roberto Pariset
6 email : victorheremita@fastwebnet.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #define QT3_SUPPORT
19 #include <qlayout.h>
20 #include <qpushbutton.h>
21 #include <q3header.h>
22 #include <q3listview.h>
24 #include <klocale.h>
25 #include <klineedit.h>
26 #include <kglobal.h>
27 #include <kgenericfactory.h>
29 #include "ui_autoreplaceprefs.h"
30 #include "autoreplacepreferences.h"
31 #include "autoreplaceconfig.h"
33 K_PLUGIN_FACTORY(AutoReplacePreferencesFactory, registerPlugin<AutoReplacePreferences>();)
34 K_EXPORT_PLUGIN(AutoReplacePreferencesFactory( "kcm_kopete_autoreplace" ))
37 // TODO: Use KConfigXT
38 AutoReplacePreferences::AutoReplacePreferences( QWidget *parent, const QVariantList &args )
39 : KCModule( AutoReplacePreferencesFactory::componentData(), parent, args )
41 QVBoxLayout* l = new QVBoxLayout( this );
42 QWidget* w = new QWidget;
43 preferencesDialog = new Ui::AutoReplacePrefsUI;
44 preferencesDialog->setupUi( w );
45 l->addWidget( w );
47 // creates table columns (avoids new columns every time)
48 preferencesDialog->m_list->addColumn( i18n("Text" ) );
49 preferencesDialog->m_list->addColumn( i18n("Replacement" ) );
50 preferencesDialog->m_list->header()->setStretchEnabled( true , 1 );
52 // connect SIGNALS/SLOTS
53 connect( preferencesDialog->m_add, SIGNAL(pressed()),
54 SLOT( slotAddCouple()) );
55 connect( preferencesDialog->m_edit, SIGNAL(pressed()),
56 SLOT( slotEditCouple()) );
57 connect( preferencesDialog->m_remove, SIGNAL(pressed()),
58 SLOT(slotRemoveCouple()) );
59 connect( preferencesDialog->m_list, SIGNAL(selectionChanged()),
60 SLOT(slotSelectionChanged()) );
61 connect( preferencesDialog->m_key, SIGNAL(textChanged ( const QString & )),
62 SLOT( slotEnableAddEdit( const QString & )) );
64 connect( preferencesDialog->AutoReplaceIncoming, SIGNAL(toggled ( bool )),
65 SLOT( slotWidgetModified()) );
66 connect( preferencesDialog->AutoReplaceOutgoing, SIGNAL(toggled ( bool )),
67 SLOT( slotWidgetModified()) );
68 connect( preferencesDialog->DotEndSentence, SIGNAL(toggled ( bool )),
69 SLOT( slotWidgetModified()) );
70 connect( preferencesDialog->CapitalizeBeginningSentence, SIGNAL(toggled ( bool )),
71 SLOT( slotWidgetModified()) );
73 //setMainWidget( preferencesDialog->gb_options, "AutoReplace Plugin" );
75 m_config = new AutoReplaceConfig;
78 AutoReplacePreferences::~AutoReplacePreferences()
80 delete m_config;
81 delete preferencesDialog;
84 // reload configuration reading it from kopeterc
85 void AutoReplacePreferences::load()
87 m_config->load();
89 // Removes and deletes all the items in this list view and triggers an update
90 preferencesDialog->m_list->clear();
92 // show keys/values on gui
93 AutoReplaceConfig::WordsToReplace::Iterator it;
94 AutoReplaceConfig::WordsToReplace map = m_config->map();
95 for ( it = map.begin(); it != map.end(); ++it )
97 // notice: insertItem is called automatically by the constructor
98 new Q3ListViewItem( preferencesDialog->m_list, it.key(), it.value() );
101 preferencesDialog->AutoReplaceIncoming->setChecked(m_config->autoReplaceIncoming());
102 preferencesDialog->AutoReplaceOutgoing->setChecked(m_config->autoReplaceOutgoing());
103 preferencesDialog->DotEndSentence->setChecked(m_config->dotEndSentence());
104 preferencesDialog->CapitalizeBeginningSentence->setChecked(m_config->capitalizeBeginningSentence());
107 // save list to kopeterc and creates map out of it
108 void AutoReplacePreferences::save()
110 // make a list reading all values from gui
111 AutoReplaceConfig::WordsToReplace newWords;
112 for ( Q3ListViewItem * i = preferencesDialog->m_list->firstChild(); i != 0; i = i->nextSibling() )
113 newWords[ i->text( 0 ) ] = i->text( 1 );
115 // save the words list
116 m_config->setMap( newWords );
118 m_config->setAutoReplaceIncoming(preferencesDialog->AutoReplaceIncoming->isChecked());
119 m_config->setAutoReplaceOutgoing(preferencesDialog->AutoReplaceOutgoing->isChecked());
120 m_config->setDotEndSentence(preferencesDialog->DotEndSentence->isChecked());
121 m_config->setCapitalizeBeginningSentence(preferencesDialog->CapitalizeBeginningSentence->isChecked());
123 m_config->save();
126 // read m_key m_value, create a QListViewItem
127 void AutoReplacePreferences::slotAddCouple()
129 QString k = preferencesDialog->m_key->text();
130 QString v = preferencesDialog->m_value->text();
131 if ( !k.isEmpty() && !k.isNull() && !v.isEmpty() && !v.isNull() )
133 Q3ListViewItem * lvi;
134 Q3ListViewItem * oldLvi = 0;
135 // see if we are replacing an existing entry
136 if ( ( oldLvi = preferencesDialog->m_list->findItem( k, 0 ) ) )
137 delete oldLvi;
138 lvi = new Q3ListViewItem( preferencesDialog->m_list, k, v );
139 // Triggers a size, geometry and content update
140 // during the next iteration of the event loop
141 preferencesDialog->m_list->triggerUpdate();
142 // select last added
143 preferencesDialog->m_list->setSelected( lvi, true );
146 slotWidgetModified();
149 // edit the selected item
150 void AutoReplacePreferences::slotEditCouple()
152 QString k = preferencesDialog->m_key->text();
153 QString v = preferencesDialog->m_value->text();
154 Q3ListViewItem * lvi;
155 if ( ( lvi = preferencesDialog->m_list->selectedItem() ) && !k.isEmpty() && !k.isNull() && !v.isEmpty() && !v.isNull() )
157 lvi->setText( 0, k );
158 lvi->setText( 1, v );
159 preferencesDialog->m_list->triggerUpdate();
160 slotWidgetModified();
164 // Returns a pointer to the selected item if the list view is in
165 // Single selection mode and an item is selected
166 void AutoReplacePreferences::slotRemoveCouple()
168 delete preferencesDialog->m_list->selectedItem();
170 slotWidgetModified();
173 void AutoReplacePreferences::slotEnableAddEdit( const QString & keyText )
175 preferencesDialog->m_add->setEnabled( !keyText.isEmpty() );
176 preferencesDialog->m_edit->setEnabled( !keyText.isEmpty() && preferencesDialog->m_list->selectedItem() );
179 void AutoReplacePreferences::slotSelectionChanged()
181 Q3ListViewItem *selection = 0;
182 if ( ( selection = preferencesDialog->m_list->selectedItem() ) )
184 // enable the remove button
185 preferencesDialog->m_remove->setEnabled( true );
186 // put the selection contents into the text entry widgets so they can be edited
187 preferencesDialog->m_key->setText( selection->text( 0 ) );
188 preferencesDialog->m_value->setText( selection->text( 1 ) );
190 else
192 preferencesDialog->m_remove->setEnabled( false );
193 preferencesDialog->m_key->clear();
194 preferencesDialog->m_value->clear();
198 void AutoReplacePreferences::slotWidgetModified()
200 emit KCModule::changed( true );
203 void AutoReplacePreferences::defaults()
205 preferencesDialog->m_list->clear();
206 m_config->loadDefaultAutoReplaceList();
207 AutoReplaceConfig::WordsToReplace::Iterator it;
208 AutoReplaceConfig::WordsToReplace map = m_config->map();
209 for ( it = map.begin(); it != map.end(); ++it )
211 // notice: insertItem is called automatically by the constructor
212 new Q3ListViewItem( preferencesDialog->m_list, it.key(), it.value() );
215 preferencesDialog->AutoReplaceIncoming->setChecked(false);
216 preferencesDialog->AutoReplaceOutgoing->setChecked(true);
217 preferencesDialog->DotEndSentence->setChecked(false);
218 preferencesDialog->CapitalizeBeginningSentence->setChecked(false);
220 slotWidgetModified();
223 #include "autoreplacepreferences.moc"
225 // vim: set noet ts=4 sts=4 sw=4: