kaddressbook

xxportmanager.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 <tqlayout.h>
25 
26 #include <tdeabc/addressbook.h>
27 #include <tdeabc/resource.h>
28 #include <kdebug.h>
29 #include <tdelocale.h>
30 #include <tdemessagebox.h>
31 #include <ktrader.h>
32 #include <tdeapplication.h>
33 
34 #include "core.h"
35 #include "kablock.h"
36 #include "undocmds.h"
37 #include "xxportselectdialog.h"
38 
39 #include "xxportmanager.h"
40 
41 KURL XXPortManager::importURL = KURL();
42 TQString XXPortManager::importData = TQString();
43 
44 XXPortManager::XXPortManager( KAB::Core *core, TQObject *parent, const char *name )
45  : TQObject( parent, name ), mCore( core )
46 {
47  loadPlugins();
48 }
49 
50 XXPortManager::~XXPortManager()
51 {
52 }
53 
54 void XXPortManager::restoreSettings()
55 {
56 }
57 
58 void XXPortManager::saveSettings()
59 {
60 }
61 
62 void XXPortManager::importVCard( const KURL &url )
63 {
64  importURL = url;
65  slotImport( "vcard", "<empty>" );
66  importURL = KURL();
67 }
68 
69 void XXPortManager::importVCardFromData( const TQString &vCard )
70 {
71  importData = vCard;
72  slotImport( "vcard", "<empty>" );
73  importData = "";
74 }
75 
76 void XXPortManager::slotImport( const TQString &identifier, const TQString &data )
77 {
78  KAB::XXPort *obj = mXXPortObjects[ identifier ];
79  if ( !obj ) {
80  KMessageBox::error( mCore->widget(), i18n( "<qt>No import plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
81  return;
82  }
83 
84  TDEABC::Resource *resource = mCore->requestResource( mCore->widget() );
85  if ( !resource )
86  return;
87 
88  TDEABC::AddresseeList list = obj->importContacts( data );
89  TDEABC::AddresseeList::Iterator it;
90  for ( it = list.begin(); it != list.end(); ++it )
91  (*it).setResource( resource );
92 
93  if ( !list.isEmpty() ) {
94  NewCommand *command = new NewCommand( mCore->addressBook(), list );
95  mCore->commandHistory()->addCommand( command );
96  emit modified();
97  }
98 }
99 
100 void XXPortManager::slotExport( const TQString &identifier, const TQString &data )
101 {
102  KAB::XXPort *obj = mXXPortObjects[ identifier ];
103  if ( !obj ) {
104  KMessageBox::error( mCore->widget(), i18n( "<qt>No export plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
105  return;
106  }
107 
108  TDEABC::AddresseeList addrList;
109  XXPortSelectDialog dlg( mCore, obj->requiresSorting(), mCore->widget() );
110  if ( dlg.exec() )
111  addrList = dlg.contacts();
112  else
113  return;
114 
115  if ( !obj->exportContacts( addrList, data ) )
116  KMessageBox::error( mCore->widget(), i18n( "Unable to export contacts." ) );
117 }
118 
119 void XXPortManager::loadPlugins()
120 {
121  mXXPortObjects.clear();
122 
123  const TDETrader::OfferList plugins = TDETrader::self()->query( "KAddressBook/XXPort",
124  TQString( "[X-TDE-KAddressBook-XXPortPluginVersion] == %1" ).arg( KAB_XXPORT_PLUGIN_VERSION ) );
125  TDETrader::OfferList::ConstIterator it;
126  for ( it = plugins.begin(); it != plugins.end(); ++it ) {
127  if ( !(*it)->hasServiceType( "KAddressBook/XXPort" ) )
128  continue;
129 
130  KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
131  if ( !factory ) {
132  kdDebug(5720) << "XXPortManager::loadExtensions(): Factory creation failed" << endl;
133  continue;
134  }
135 
136  KAB::XXPortFactory *xxportFactory = static_cast<KAB::XXPortFactory*>( factory );
137 
138  if ( !xxportFactory ) {
139  kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl;
140  continue;
141  }
142 
143  KAB::XXPort *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore->widget() );
144  if ( obj ) {
145  if ( mCore->guiClient() )
146  mCore->guiClient()->insertChildClient( obj );
147 
148  mXXPortObjects.insert( obj->identifier(), obj );
149  connect( obj, TQ_SIGNAL( exportActivated( const TQString&, const TQString& ) ),
150  this, TQ_SLOT( slotExport( const TQString&, const TQString& ) ) );
151  connect( obj, TQ_SIGNAL( importActivated( const TQString&, const TQString& ) ),
152  this, TQ_SLOT( slotImport( const TQString&, const TQString& ) ) );
153 
154  obj->setTDEApplication( kapp );
155  }
156  }
157 }
158 
159 #include "xxportmanager.moc"