kitchensync

pluginpicker.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2005 Cornelius Schumacher <schumacher@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,
00019     USA.
00020 */
00021 
00022 #include "pluginpicker.h"
00023 
00024 #include "memberinfo.h"
00025 #include "syncprocessmanager.h"
00026 
00027 #include <libqopensync/environment.h>
00028 
00029 #include <kdialog.h>
00030 #include <kglobal.h>
00031 #include <kiconloader.h>
00032 #include <klocale.h>
00033 
00034 #include <qlabel.h>
00035 #include <qlayout.h>
00036 
00037 PluginItem::PluginItem( KWidgetList *list, const QSync::Plugin &plugin )
00038   : KWidgetListItem( list ), mPlugin( plugin )
00039 {
00040   QString iconName = MemberInfo::pluginIconName( mPlugin.name() );
00041   QGridLayout *layout = new QGridLayout( this, 2, 2, KDialog::marginHint(), KDialog::spacingHint() );
00042 
00043   QLabel *icon = new QLabel( this );
00044   icon->setPixmap( KGlobal::iconLoader()->loadIcon( iconName, KIcon::Desktop ) );
00045   icon->setFixedSize( icon->sizeHint() );
00046 
00047   QLabel *name = new QLabel( plugin.longName(), this );
00048   QLabel *description = new QLabel( plugin.description(), this );
00049 
00050   QFont font = name->font();
00051   font.setBold( true );
00052   name->setFont( font );
00053 
00054   layout->addWidget( icon, 0, 0 );
00055   layout->addWidget( name, 0, 1 );
00056   layout->addWidget( description, 1, 1 );
00057 }
00058 
00059 
00060 PluginPicker::PluginPicker( QWidget *parent )
00061   : QWidget( parent )
00062 {
00063   QBoxLayout *layout = new QVBoxLayout( this );
00064 
00065   mPluginList = new KWidgetList( this );
00066   layout->addWidget( mPluginList );
00067 
00068   connect( mPluginList, SIGNAL( doubleClicked( KWidgetListItem* ) ),
00069            SIGNAL( selected() ) );
00070 
00071   updatePluginList();
00072 
00073   mPluginList->setFocus();
00074 }
00075 
00076 void PluginPicker::updatePluginList()
00077 {
00078   mPluginList->clear();
00079 
00080   QSync::Environment *env = SyncProcessManager::self()->environment();
00081 
00082   QSync::Environment::PluginIterator it( env->pluginBegin() );
00083   for( ; it != env->pluginEnd(); ++it ) {
00084     QSync::Plugin plugin = *it;
00085     mPluginList->appendItem( new PluginItem( mPluginList, plugin ) );
00086   }
00087 }
00088 
00089 QSync::Plugin PluginPicker::selectedPlugin() const
00090 {
00091   PluginItem *item = static_cast<PluginItem *>( mPluginList->selectedItem() );
00092   if ( item ) return item->plugin();
00093   else return QSync::Plugin();
00094 }
00095 
00096 
00097 PluginPickerDialog::PluginPickerDialog( QWidget *parent )
00098   : KDialogBase( parent, 0, true, i18n("Select Member Type"), Ok | Cancel )
00099 {
00100   QFrame *topFrame = makeMainWidget();
00101 
00102   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00103 
00104   mPicker = new PluginPicker( topFrame );
00105   topLayout->addWidget( mPicker );
00106 
00107   connect( mPicker, SIGNAL( selected() ), SLOT( slotOk() ) );
00108 
00109   setInitialSize( QSize( 460, 380 ) );
00110 }
00111 
00112 QSync::Plugin PluginPickerDialog::selectedPlugin() const
00113 {
00114   return mPicker->selectedPlugin();
00115 }
00116 
00117 QSync::Plugin PluginPickerDialog::getPlugin( QWidget *parent )
00118 {
00119   PluginPickerDialog dlg( parent );
00120   if ( dlg.exec() )
00121     return dlg.selectedPlugin();
00122   else
00123     return QSync::Plugin();
00124 }
00125 
00126 void PluginPickerDialog::slotOk()
00127 {
00128   accept();
00129 }
00130 
00131 void PluginPickerDialog::slotCancel()
00132 {
00133   reject();
00134 }
00135 
00136 #include "pluginpicker.moc"