kaddressbook

contacteditorwidgetmanager.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2004 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 <tqlayout.h>
25
26#include <tdeapplication.h>
27#include <kdialog.h>
28#include <klibloader.h>
29#include <ktrader.h>
30
31// include non-plugin contact editor widgets
32#include "customfieldswidget.h"
33#include "freebusywidget.h"
34#include "geowidget.h"
35#include "imagewidget.h"
36#include "soundwidget.h"
37
38#include "contacteditorwidget.h"
39#include "contacteditorwidgetmanager.h"
40
41ContactEditorWidgetManager *ContactEditorWidgetManager::mSelf = 0;
42
43ContactEditorWidgetManager::ContactEditorWidgetManager()
44 : TQObject( tqApp )
45{
46 reload();
47}
48
49ContactEditorWidgetManager::~ContactEditorWidgetManager()
50{
51 mFactories.clear();
52}
53
54ContactEditorWidgetManager *ContactEditorWidgetManager::self()
55{
56 kdWarning( !tdeApp, 7520 ) << "No TQApplication object available!" << endl;
57
58 if ( !mSelf )
59 mSelf = new ContactEditorWidgetManager();
60
61 return mSelf;
62}
63
64int ContactEditorWidgetManager::count() const
65{
66 return mFactories.count();
67}
68
69KAB::ContactEditorWidgetFactory *ContactEditorWidgetManager::factory( int pos ) const
70{
71 return mFactories[ pos ];
72}
73
74void ContactEditorWidgetManager::reload()
75{
76 mFactories.clear();
77 kdDebug(5720) << "ContactEditorWidgetManager::reload()" << endl;
78 const TDETrader::OfferList plugins = TDETrader::self()->query( "KAddressBook/ContactEditorWidget",
79 TQString( "[X-TDE-KAddressBook-CEWPluginVersion] == %1" ).arg( KAB_CEW_PLUGIN_VERSION ) );
80
81 TDETrader::OfferList::ConstIterator it;
82 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
83 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
84 if ( !factory ) {
85 kdDebug(5720) << "ContactEditorWidgetManager::reload(): Factory creation failed" << endl;
86 continue;
87 }
88
89 KAB::ContactEditorWidgetFactory *pageFactory =
90 static_cast<KAB::ContactEditorWidgetFactory*>( factory );
91
92 if ( !pageFactory ) {
93 kdDebug(5720) << "ContactEditorWidgetManager::reload(): Cast failed" << endl;
94 continue;
95 }
96
97 mFactories.append( pageFactory );
98 }
99
100 // add all non-plugin contact editor factories
101 mFactories.append( new FreeBusyWidgetFactory );
102 mFactories.append( new ImageWidgetFactory );
103 mFactories.append( new SoundWidgetFactory );
104 mFactories.append( new GeoWidgetFactory );
105 mFactories.append( new CustomFieldsWidgetFactory );
106}
107
109
110ContactEditorTabPage::ContactEditorTabPage( TQWidget *parent, const char *name )
111 : TQWidget( parent, name )
112{
113 mLayout = new TQGridLayout( this, 0, 2, KDialog::marginHint(),
114 KDialog::spacingHint() );
115}
116
117void ContactEditorTabPage::addWidget( KAB::ContactEditorWidget *widget )
118{
119 if ( widget->logicalWidth() == 2 ) {
120 mWidgets.prepend( widget );
121 connect( widget, TQ_SIGNAL( changed() ), TQ_SIGNAL( changed() ) );
122 return;
123 }
124
125 // insert it in descending order
126 KAB::ContactEditorWidget::List::Iterator it;
127 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
128 if ( widget->logicalHeight() > (*it)->logicalHeight() &&
129 (*it)->logicalWidth() == 1 ) {
130 --it;
131 break;
132 }
133 }
134 mWidgets.insert( ++it, widget );
135
136 connect( widget, TQ_SIGNAL( changed() ), TQ_SIGNAL( changed() ) );
137}
138
139void ContactEditorTabPage::loadContact( TDEABC::Addressee *addr )
140{
141 KAB::ContactEditorWidget::List::Iterator it;
142 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
143 (*it)->setModified( false );
144 (*it)->loadContact( addr );
145 }
146}
147
148void ContactEditorTabPage::storeContact( TDEABC::Addressee *addr )
149{
150 KAB::ContactEditorWidget::List::Iterator it;
151 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
152 if ( (*it)->modified() ) {
153 (*it)->storeContact( addr );
154 (*it)->setModified( false );
155 }
156 }
157}
158
159void ContactEditorTabPage::setReadOnly( bool readOnly )
160{
161 KAB::ContactEditorWidget::List::Iterator it;
162 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
163 (*it)->setReadOnly( readOnly );
164}
165
166void ContactEditorTabPage::updateLayout()
167{
168 KAB::ContactEditorWidget::List::ConstIterator it;
169
170 int row = 0;
171 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
172 if ( (*it)->logicalWidth() == 2 ) {
173 mLayout->addMultiCellWidget( *it, row, row + (*it)->logicalHeight() - 1, 0, 1 );
174 row += (*it)->logicalHeight();
175
176 if ( it != mWidgets.fromLast() ) {
177 TQFrame *frame = new TQFrame( this );
178 frame->setFrameStyle( TQFrame::HLine | TQFrame::Sunken );
179 mLayout->addMultiCellWidget( frame, row, row, 0, 1 );
180 row++;
181 }
182 continue;
183 }
184
185 // fill left side
186 int leftHeight = (*it)->logicalHeight();
187
188 if ( it == mWidgets.fromLast() ) { // last widget gets full width
189 mLayout->addMultiCellWidget( *it, row, row + leftHeight - 1, 0, 1 );
190 return;
191 } else {
192 mLayout->addMultiCellWidget( *it, row, row + leftHeight - 1, 0, 0 );
193 TQFrame *frame = new TQFrame( this );
194 frame->setFrameStyle( TQFrame::HLine | TQFrame::Sunken );
195 mLayout->addMultiCellWidget( frame, row + leftHeight, row + leftHeight, 0, 1 );
196 }
197
198 // fill right side
199 for ( int i = 0; i < leftHeight; ++i ) {
200 ++it;
201 if ( it == mWidgets.end() )
202 break;
203
204 int rightHeight = (*it)->logicalHeight();
205 if ( rightHeight + i <= leftHeight )
206 mLayout->addMultiCellWidget( *it, row + i, row + i + rightHeight - 1, 1, 1 );
207 else {
208 --i;
209 break;
210 }
211 }
212
213 row += 2;
214 }
215}
216
217#include "contacteditorwidgetmanager.moc"