kaddressbook

extensionmanager.cpp
1/*
2 This file is part of KAddressbook.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4
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.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeactionclasses.h>
25#include <tdeconfig.h>
26#include <kdebug.h>
27#include <tdelocale.h>
28#include <ktrader.h>
29
30#include <tqlayout.h>
31#include <tqobjectlist.h>
32#include <tqsignalmapper.h>
33#include <tqsplitter.h>
34#include <tqtimer.h>
35#include <tqwidgetstack.h>
36
37#include "addresseeeditorextension.h"
38#include "core.h"
39#include "kabprefs.h"
40
41#include "extensionmanager.h"
42
43ExtensionData::ExtensionData() : action( 0 ), widget( 0 ), weight( 0 ), isDetailsExtension( false )
44{
45}
46
47ExtensionManager::ExtensionManager( TQWidget* extensionBar, TQWidgetStack* detailsStack, KAB::Core *core, TQObject *parent,
48 const char *name )
49 : TQObject( parent, name ), mExtensionBar( extensionBar ), mCore( core ),
50 mMapper( 0 ), mDetailsStack( detailsStack ), mActiveDetailsWidget( 0 )
51{
52 Q_ASSERT( mExtensionBar );
53 TQVBoxLayout* layout = new TQVBoxLayout( mExtensionBar );
54 mSplitter = new TQSplitter( mExtensionBar );
55 mSplitter->setOrientation( TQt::Vertical );
56 layout->addWidget( mSplitter );
57
58 createExtensionWidgets();
59
60 mActionCollection = new TDEActionCollection( this, "ActionCollection" );
61
62 extensionBar->setShown( false );
63 TQTimer::singleShot( 0, this, TQ_SLOT( createActions() ) );
64}
65
66ExtensionManager::~ExtensionManager()
67{
68}
69
70
71void ExtensionManager::restoreSettings()
72{
73 const TQStringList activeExtensions = KABPrefs::instance()->activeExtensions();
74
75 typedef TQMap<TQString, ExtensionData>::ConstIterator ConstIterator;
76 for ( ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
77 if ( activeExtensions.contains( it.data().identifier ) ) {
78 TDEToggleAction *action = static_cast<TDEToggleAction*>( it.data().action );
79 if ( action )
80 action->setChecked( true );
81 setExtensionActive( it.data().identifier, true );
82 }
83 }
84 const TQValueList<int> sizes = KABPrefs::instance()->extensionsSplitterSizes();
85 mSplitter->setSizes( sizes );
86}
87
88void ExtensionManager::saveSettings()
89{
90 KABPrefs::instance()->setActiveExtensions( mActiveExtensions );
91 KABPrefs::instance()->setExtensionsSplitterSizes( mSplitter->sizes() );
92}
93
94void ExtensionManager::reconfigure()
95{
96 saveSettings();
97 createExtensionWidgets();
98 createActions();
99 restoreSettings();
100 mExtensionBar->setShown( !mActiveExtensions.isEmpty() );
101}
102
103bool ExtensionManager::isQuickEditVisible() const
104{
105 return mActiveExtensions.contains( "contact_editor" );
106}
107
108void ExtensionManager::setSelectionChanged()
109{
110 for ( TQStringList::ConstIterator it = mActiveExtensions.begin(), end = mActiveExtensions.end(); it != end; ++it ) {
111 if ( mExtensionMap.contains( *it ) && mExtensionMap[*it].widget )
112 mExtensionMap[*it].widget->contactsSelectionChanged();
113 }
114}
115
116void ExtensionManager::activationToggled( const TQString &extid )
117{
118 if ( !mExtensionMap.contains( extid ) )
119 return;
120 const ExtensionData data = mExtensionMap[ extid ];
121 const bool activated = data.action->isChecked();
122 setExtensionActive( extid, activated );
123}
124
125void ExtensionManager::setExtensionActive( const TQString& extid, bool active )
126{
127 if ( !mExtensionMap.contains( extid ) )
128 return;
129 if ( mActiveExtensions.contains( extid ) == active )
130 return;
131 const ExtensionData data = mExtensionMap[ extid ];
132 if ( active ) {
133 mActiveExtensions.append( extid );
134 if ( data.widget ) {
135 if ( data.isDetailsExtension ) {
136 mActiveDetailsWidget = data.widget;
137 emit detailsWidgetActivated( data.widget );
138 } else {
139 data.widget->show();
140 }
141 data.widget->contactsSelectionChanged();
142 }
143 } else {
144 mActiveExtensions.remove( extid );
145 if ( data.widget && !data.isDetailsExtension ) {
146 data.widget->hide();
147 }
148 if ( data.isDetailsExtension ) {
149 mActiveDetailsWidget = 0;
150 emit detailsWidgetDeactivated( data.widget );
151 }
152 }
153 mExtensionBar->setShown( !mActiveExtensions.isEmpty() );
154}
155
156void ExtensionManager::createActions()
157{
158 mCore->guiClient()->unplugActionList( "extensions_list" );
159 mActionList.setAutoDelete( true );
160 mActionList.clear();
161 mActionList.setAutoDelete( false );
162
163 delete mMapper;
164 mMapper = new TQSignalMapper( this, "SignalMapper" );
165 connect( mMapper, TQ_SIGNAL( mapped( const TQString& ) ),
166 this, TQ_SLOT( activationToggled( const TQString& ) ) );
167
168 ExtensionData::List::ConstIterator it;
169 for ( TQMap<TQString, ExtensionData>::Iterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
170 ExtensionData& data = it.data();
171 data.action = new TDEToggleAction( data.title, 0, mMapper, TQ_SLOT( map() ),
172 mActionCollection,
173 TQString( data.identifier + "_extension" ).latin1() );
174 mMapper->setMapping( data.action, data.identifier );
175 mActionList.append( data.action );
176
177 if ( mActiveExtensions.contains( data.identifier ) )
178 data.action->setChecked( true );
179 }
180
181 mActionList.append( new TDEActionSeparator( mActionCollection ) );
182 mCore->guiClient()->plugActionList( "extensions_list", mActionList );
183}
184
185TQWidget* ExtensionManager::activeDetailsWidget() const
186{
187 return mActiveDetailsWidget;
188}
189
190void ExtensionManager::createExtensionWidgets()
191{
192 // clean up
193 for ( TQMap<TQString, ExtensionData>::ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
194 delete it.data().widget;
195 }
196 mExtensionMap.clear();
197
198 KAB::ExtensionWidget *wdg = 0;
199
200 {
201 // add addressee editor as default
202 wdg = new AddresseeEditorExtension( mCore, mDetailsStack );
203 wdg->hide();
204
205 connect( wdg, TQ_SIGNAL( modified( const TDEABC::Addressee::List& ) ),
206 TQ_SIGNAL( modified( const TDEABC::Addressee::List& ) ) );
207 connect( wdg, TQ_SIGNAL( deleted( const TQStringList& ) ),
208 TQ_SIGNAL( deleted( const TQStringList& ) ) );
209
210 ExtensionData data;
211 data.identifier = wdg->identifier();
212 data.title = wdg->title();
213 data.widget = wdg;
214 data.isDetailsExtension = true;
215 mExtensionMap.insert( data.identifier, data );
216 }
217
218 // load the other extensions
219 const TDETrader::OfferList plugins = TDETrader::self()->query( "KAddressBook/Extension",
220 TQString( "[X-TDE-KAddressBook-ExtensionPluginVersion] == %1" ).arg( KAB_EXTENSIONWIDGET_PLUGIN_VERSION ) );
221
222 TDETrader::OfferList::ConstIterator it;
223 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
224 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
225 if ( !factory ) {
226 kdDebug(5720) << "ExtensionManager::loadExtensions(): Factory creation failed" << endl;
227 continue;
228 }
229
230 KAB::ExtensionFactory *extensionFactory = static_cast<KAB::ExtensionFactory*>( factory );
231
232 if ( !extensionFactory ) {
233 kdDebug(5720) << "ExtensionManager::loadExtensions(): Cast failed" << endl;
234 continue;
235 }
236
237 wdg = extensionFactory->extension( mCore, mSplitter );
238 if ( wdg ) {
239 if ( wdg->identifier() == "distribution_list_editor_ng" )
240 mSplitter->moveToFirst( wdg );
241 wdg->hide();
242 connect( wdg, TQ_SIGNAL( modified( const TDEABC::Addressee::List& ) ),
243 TQ_SIGNAL( modified( const TDEABC::Addressee::List& ) ) );
244 connect( wdg, TQ_SIGNAL( deleted( const TQStringList& ) ),
245 TQ_SIGNAL( deleted( const TQStringList& ) ) );
246
247 ExtensionData data;
248 data.identifier = wdg->identifier();
249 data.title = wdg->title();
250 data.widget = wdg;
251 mExtensionMap.insert( data.identifier, data );
252 }
253 }
254}
255
256#include "extensionmanager.moc"