Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

blogchooser.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 - 2008 by Martin Mueller                           *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "blogchooser.h"
00021 #include "blogchooser.moc"
00022 
00023 #include "../blokkalaccountmanager.h"
00024 #include "../blokkalaccount.h"
00025 #include "../blokkalblog.h"
00026 
00027 #include <kdebug.h>
00028 #include <k3listbox.h>
00029 #include <klocale.h>
00030 
00031 #include <QLayout>
00032 #include <QLabel>
00033 #include <QPushButton>
00034 #include <QList>
00035 
00036 class Blokkal::Ui::BlogChooser::Private
00037 {
00038 public:
00039         Private( void ) :
00040         accountListBox( 0 ),
00041         blogListBox( 0 ),
00042         account( 0 ),
00043         blog( 0 ) {}
00044 
00045         K3ListBox * accountListBox;
00046         K3ListBox * blogListBox;
00047 
00048         Account * account;
00049         Blog * blog;
00050 
00051         QMap< Account*, Q3ListBoxItem *> accountItemMap;
00052         QMap< Blog*, Q3ListBoxItem *> blogItemMap;
00053 };
00054 
00055 Blokkal::Ui::BlogChooser::BlogChooser( QWidget * parent ) :
00056 QWidget( parent ),
00057 d( new Private() )
00058 {
00059         QHBoxLayout * layout = new QHBoxLayout( this );
00060         layout->setSpacing( KDialog::spacingHint() );
00061         d->accountListBox = new K3ListBox( this );
00062         layout->addWidget( d->accountListBox );
00063         d->blogListBox = new K3ListBox( this );
00064         layout->addWidget( d->blogListBox );
00065         d->blogListBox->setEnabled( FALSE );
00066         
00067         AccountList accounts = AccountManager::self()->accounts();
00068         for( AccountList::ConstIterator it = accounts.begin();
00069              it != accounts.end();
00070              ++it )
00071         {
00072                 d->accountListBox->insertItem( (*it)->icon().pixmap(16,16), (*it)->id(), 0 );
00073                 d->accountItemMap.insert( *it, d->accountListBox->firstItem() );
00074         }
00075 
00076         d->accountListBox->sort();
00077 
00078         connect( d->accountListBox, SIGNAL( selectionChanged( void ) ),
00079                  this, SLOT( slotAccountSelectionChanged( void ) ) );
00080         connect( d->blogListBox, SIGNAL( selectionChanged( void ) ),
00081                  this, SLOT( slotBlogSelectionChanged( void ) ) );
00082 }
00083 
00084 Blokkal::Ui::BlogChooser::~BlogChooser( void )
00085 {
00086         delete d;
00087 }
00088 
00089 Blokkal::Account * Blokkal::Ui::BlogChooser::account( void ) const
00090 {
00091         return d->account;
00092 }
00093 
00094 void Blokkal::Ui::BlogChooser::setAccount( Blokkal::Account * account )
00095 {
00096         if( d->account == account ) {
00097                 return;
00098         }
00099 
00100         d->account = account;
00101 
00102         setBlog( 0 );
00103         d->blogItemMap.clear();
00104         d->blogListBox->clear();
00105         
00106         if( d->accountItemMap.contains( account ) ) {
00107                 QList<Blokkal::Blog * > blogs = account->blogs();
00108                 for( QList< Blokkal::Blog * >::ConstIterator it = blogs.begin();
00109                      it != blogs.end();
00110                      ++it )
00111                 {
00112                         d->blogListBox->insertItem( (*it)->icon().pixmap(16,16), (*it)->id(), 0 );
00113                         d->blogItemMap.insert( *it, d->blogListBox->firstItem() );
00114                 }
00115 
00116                 d->blogListBox->sort();
00117 
00118                 d->accountListBox->setSelected( d->accountItemMap[account], TRUE );
00119                 d->blogListBox->setEnabled( TRUE );
00120         }
00121         else {
00122                 d->accountListBox->clearSelection();
00123                 d->blogListBox->setEnabled( FALSE );
00124         }
00125 }
00126 
00127 Blokkal::Blog * Blokkal::Ui::BlogChooser::blog( void ) const
00128 {
00129         return d->blog;
00130 }
00131 
00132 void Blokkal::Ui::BlogChooser::setBlog( Blokkal::Blog * blog )
00133 {
00134         if( d->blog == blog ) {
00135                 return;
00136         }
00137 
00138         d->blog = blog;
00139         
00140         if( d->blogItemMap.contains( blog ) ) {
00141                 d->blogListBox->setSelected( d->blogItemMap[blog], TRUE );
00142                 emit blogSelected( blog );
00143         }
00144         else {
00145                 d->blogListBox->clearSelection();
00146                 emit blogSelected( 0 );
00147         }
00148 }
00149 
00150 void Blokkal::Ui::BlogChooser::slotAccountSelectionChanged( void )
00151 {
00152         //kdDebug() << k_funcinfo << endl;
00153         Q3ListBoxItem * item = d->accountListBox->selectedItem();
00154         if( !item ) {
00155                 setAccount( 0 );
00156                 return;
00157         }
00158         
00159         QList<Account*> accounts = d->accountItemMap.keys();
00160         for( QList<Account*>::ConstIterator it = accounts.begin();
00161              it != accounts.end();
00162              ++it )
00163         {
00164                 if( d->accountItemMap[*it] == item ) {
00165                         setAccount( *it );
00166                         return;
00167                 }
00168         }
00169 
00170         setAccount( 0 );
00171 }
00172 
00173 void Blokkal::Ui::BlogChooser::slotBlogSelectionChanged( void )
00174 {
00175         //kdDebug() << k_funcinfo << endl;
00176         Q3ListBoxItem * item = d->blogListBox->selectedItem();
00177         if( !item ) {
00178                 setBlog( 0 );
00179                 return;
00180         }
00181         
00182         QList<Blog*> blogs = d->blogItemMap.keys();
00183         for( QList<Blog*>::ConstIterator it = blogs.begin();
00184              it != blogs.end();
00185              ++it )
00186         {
00187                 if( d->blogItemMap[*it] == item ) {
00188                         setBlog( *it );
00189                         return;
00190                 }
00191         }
00192 
00193         setBlog( 0 );
00194 }
00195 
00196 class Blokkal::Ui::BlogChooserDialog::Private
00197 {
00198 public:
00199         Private( void ) {}
00200 
00201         BlogChooser * blogChooser;
00202 };
00203 
00204 Blokkal::Ui::BlogChooserDialog::BlogChooserDialog( const QString & message, QWidget * parent ):
00205 KDialog( parent ),
00206 d( new Private() )
00207 {
00208         setModal( TRUE );
00209         setButtons( Ok|Cancel );
00210         setDefaultButton( Ok );
00211         setCaption( i18n( "Select a Blog" ) );
00212         QWidget * mainWidget = new QWidget( this );
00213         QVBoxLayout * layout = new QVBoxLayout( mainWidget );
00214         layout->setSpacing( KDialog::spacingHint() );
00215         QLabel * messageLabel = new QLabel( message, mainWidget );
00216         messageLabel->setWordWrap( TRUE );
00217         layout->addWidget( messageLabel );
00218         d->blogChooser = new BlogChooser( mainWidget );
00219         layout->addWidget( d->blogChooser );
00220 
00221         setMainWidget( mainWidget );
00222         enableButton( Ok, FALSE );
00223 
00224         connect( d->blogChooser, SIGNAL( blogSelected( Blokkal::Blog * ) ),
00225                  this, SLOT( slotBlogSelected( Blokkal::Blog * ) ) );
00226 }
00227 
00228 Blokkal::Ui::BlogChooserDialog::~BlogChooserDialog( void )
00229 {
00230         delete d;
00231 }
00232 
00233 Blokkal::Ui::BlogChooser * Blokkal::Ui::BlogChooserDialog::blogChooser( void ) const
00234 {
00235         return d->blogChooser;
00236 }
00237 
00238 void Blokkal::Ui::BlogChooserDialog::slotBlogSelected( Blokkal::Blog * blog )
00239 {
00240         enableButton( Ok, blog );
00241 }