kitchensync

pluckerpart.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2004 Holger Hans Peter Freyther <freyther@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 */
00020 
00021 
00022 #include "pluckerpart.h"
00023 #include "kspluckerconfigwidget.h"
00024 #include "pluckerconfig.h"
00025 #include "pluckerfilehandle.h"
00026 #include "pluckerprocesshandler.h"
00027 
00028 #include <unknownsyncee.h>
00029 
00030 #include <profile.h>
00031 #include <core.h>
00032 #include <konnectorview.h>
00033 
00034 #include <kglobal.h>
00035 #include <kiconloader.h>
00036 #include <ktempdir.h>
00037 
00038 #include <kparts/genericfactory.h>
00039 
00040 #include <qapplication.h>
00041 #include <qdir.h>
00042 #include <qeventloop.h>
00043 #include <qlayout.h>
00044 #include <qtextedit.h>
00045 
00046 typedef KParts::GenericFactory<KSPlucker::PluckerPart> PluckerPartFactory;
00047 K_EXPORT_COMPONENT_FACTORY( libksync_pluckerpart, PluckerPartFactory )
00048 
00049 
00050 namespace KSPlucker {
00051 
00052 PluckerPart::PluckerPart( QWidget* parent, const char* name,
00053                           QObject* , const char* ,
00054                           const QStringList& )
00055   : DCOPObject("PluckerInterface"),
00056     KSync::ActionPart( parent, name ),
00057     m_widget( 0 ), m_config( 0 ), m_edit( 0 ), m_view( 0 ),
00058     m_temp( 0 )
00059 {
00060   m_pixmap = KGlobal::iconLoader()->loadIcon( "knode",
00061                                               KIcon::Desktop, 48 );
00062 
00063   connectDoneSync(SLOT(slotCleanUp()));
00064   connectProfileChanged(SLOT(slotProfileChanged(const Profile&)));
00065 }
00066 
00067 PluckerPart::~PluckerPart()
00068 {
00069   PluckerConfig *conf = PluckerConfig::self();
00070   conf->load( core()->currentProfile().uid() );
00071   kdDebug() << "Selected Konnectors are " << m_view->selectedKonnectorsList() << endl;
00072   conf->setKonnectorIds( m_view->selectedKonnectorsList() );
00073   conf->save( core()->currentProfile().uid() );
00074 }
00075 
00076 KAboutData *PluckerPart::createAboutData()
00077 {
00078   return new KAboutData( "Plucker Part",
00079                          I18N_NOOP("Plucker Converter"),
00080                          "0.0" );
00081 
00082 }
00083 
00084 QString PluckerPart::type()const
00085 {
00086   return QString::fromLatin1( "PluckerPart" );
00087 }
00088 
00089 QString PluckerPart::title()const
00090 {
00091   return i18n( "Plucker" );
00092 }
00093 
00094 QString PluckerPart::description()const
00095 {
00096   return i18n( "Pluck Websites and News-Feeds" );
00097 }
00098 
00099 bool PluckerPart::hasGui()const
00100 {
00101   return true;
00102 }
00103 
00104 QPixmap *PluckerPart::pixmap()
00105 {
00106   return &m_pixmap;
00107 }
00108 
00109 QString PluckerPart::iconName()const
00110 {
00111   return QString::fromLatin1("knode");
00112 }
00113 
00114 QWidget *PluckerPart::widget()
00115 {
00116   /*
00117    * Create a KonnectorView so the user can choose which
00118    * Konnectors should get a Plucker Upload
00119    * And then we present the log of the JPlucker Output
00120    */
00121   if ( !m_widget ) {
00122     m_widget = new QWidget();
00123     QVBoxLayout *lay = new QVBoxLayout(m_widget);
00124 
00125     m_view = new KSync::KonnectorView( m_widget , "pluckers view" );
00126     lay->addWidget( m_view );
00127 
00128     m_edit = new QTextEdit( m_widget );
00129     m_edit->setReadOnly( true );
00130     lay->addWidget( m_edit );
00131   }
00132 
00133   return m_widget;
00134 }
00135 
00136 bool PluckerPart::needsKonnectorWrite()const
00137 {
00138   return true;
00139 }
00140 
00141 /*
00142  * Start the Action. We will start the
00143  * conversion and then do uploading
00144  * via KSync::UnknownSyncee
00145  */
00146 void PluckerPart::executeAction()
00147 {
00148   /*
00149    * Load and Safe Configuration
00150    */
00151   PluckerConfig *conf = PluckerConfig::self();
00152   conf->load( core()->currentProfile().uid() );
00153   QStringList lst = conf->pluckerFiles();
00154   conf->setKonnectorIds( m_view->selectedKonnectorsList() );
00155   conf->save( core()->currentProfile().uid() );
00156 
00157 
00158   m_temp = new KTempDir();
00159   PluckerProcessHandler *handler = new PluckerProcessHandler( PluckerProcessHandler::Convert,
00160                                                 false, lst, m_temp->name(), this );
00161 
00162   connect(handler, SIGNAL(sigProgress(const QString&)),
00163           m_edit, SLOT(append(const QString&)));
00164   connect(handler, SIGNAL(sigFinished(PluckerProcessHandler*)),
00165           this, SLOT(slotFinished(PluckerProcessHandler*)));
00166 
00167   handler->run();
00168   /*
00169    * some hack due the synchronus of syncing!
00170    * this will be set to true in slotFinished.
00171    * We will allow SocketNotifier so that
00172    * Repainting is allowed and Konnectors can communicate
00173    *
00174    * #FIXME make it async soon
00175    */
00176   m_done = false;
00177 
00178   while ( !m_done )
00179     qApp->eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
00180 
00181   /*
00182    * Now let us create the Syncees
00183    */
00184   QDir *dir = m_temp->qDir();
00185   lst = dir->entryList();
00186 
00187   lst.remove( "." ); lst.remove( ".." );
00188   delete dir;
00189 
00190   KSync::Konnector::List list = m_view->selectedKonnectors();
00191 
00192 
00193   for (KSync::Konnector::List::Iterator it2 = list.begin(); it2 != list.end(); ++it2 ) {
00194     KSync::UnknownSyncee *sync = new KSync::UnknownSyncee();
00195     for(QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00196       sync->addEntry( new KSync::UnknownSyncEntry(m_temp->name()+"/"+*it,
00197                                                   QString::null, sync));
00198     (*it2)->appendSyncee( sync );
00199   }
00200 
00201 
00202 }
00203 
00204 bool PluckerPart::configIsVisible()const
00205 {
00206   return true;
00207 }
00208 
00209 QWidget* PluckerPart::configWidget()
00210 {
00211   KSPlucker::PluckerConfig::self()->load( core()->currentProfile().uid() );
00212 
00213   m_config = new KSPluckerConfigWidget();
00214   m_config->readConfig();
00215 
00216 
00217   return m_config;
00218 }
00219 
00220 void PluckerPart::addPluckerUrl( KURL url)
00221 {
00222   KSPlucker::PluckerFileHandle::addFile( url, core()->currentProfile().uid() );
00223 }
00224 
00225 void PluckerPart::addPluckerFeed( KURL url )
00226 {
00227   KSPlucker::PluckerFileHandle::addFile( url, core()->currentProfile().uid(),
00228                                          false );
00229 }
00230 
00231 void PluckerPart::slotConfigOk()
00232 {
00233   /*
00234    * Safe the Config Data to the current profile
00235    */
00236   if ( m_config ) {
00237     m_config->slotConfigOk();
00238     PluckerConfig::self()->save( core()->currentProfile().uid() );
00239   }
00240 
00241   m_config = 0;
00242 }
00243 
00244 /*
00245  * Clean Up the intermediate pluckers files
00246  * and remove the dir
00247  */
00248 void PluckerPart::slotCleanUp()
00249 {
00250   kdDebug() << "Cleaning up " << endl << endl << endl;
00251   if ( m_temp )
00252     m_temp->unlink();
00253 
00254   delete m_temp;
00255   m_temp = 0l;
00256 }
00257 
00258 void PluckerPart::slotFinished( PluckerProcessHandler* handle )
00259 {
00260   handle->deleteLater();
00261   m_done = true;
00262 }
00263 
00264 void PluckerPart::slotProfileChanged(const KSync::Profile& )
00265 {
00266   /*
00267    * Apply settings to the KonnectorView
00268    */
00269   PluckerConfig* conf = PluckerConfig::self();
00270   conf->load( core()->currentProfile().uid() );
00271   m_view->setSelectedKonnectors( conf->konnectorIds() );
00272 }
00273 
00274 }
00275 
00276 #include "pluckerpart.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys