* use more K* classes
[kdenetwork.git] / knewsticker / feedsettingswidget.cpp
blob2d34a242409666f1cc88aa40feaca39c67728c7f
1 /*
2 * feedsettingswidget.cpp
4 * Copyright (c) 2007 Frerich Raabe <raabe@kde.org>
6 * This program is distributed in the hope that it will be useful, but WITHOUT
7 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
9 * accompanying file 'COPYING'.
11 #include "feedsettingswidget.h"
12 #include "newsfeedmanager.h"
13 #include "settings.h"
15 #include <KInputDialog>
16 #include <KProgressDialog>
18 FeedSettingsWidget::FeedSettingsWidget( QWidget *parent )
19 : QWidget( parent ),
20 m_downloadMessageBox( 0 )
22 ui.setupUi( this );
23 ui.feedListWidget->addItems( Settings::feedUrls() );
24 connect( ui.feedListWidget, SIGNAL( itemSelectionChanged() ),
25 this, SLOT( feedItemChanged() ) );
26 connect( ui.addButton, SIGNAL( clicked() ),
27 this, SLOT( addButtonClicked() ) );
28 connect( ui.removeButton, SIGNAL( clicked() ),
29 this, SLOT( removeButtonClicked() ) );
31 connect( NewsFeedManager::self(), SIGNAL( feedLoaded( const QUrl & ) ),
32 this, SLOT( feedLoaded( const QUrl & ) ) );
34 if ( ui.feedListWidget->count() > 0 ) {
35 ui.feedListWidget->setCurrentRow( 0 );
36 feedItemChanged();
39 ui.addButton->setIcon( KIcon( "list-add" ) );
40 ui.removeButton->setIcon( KIcon( "list-remove" ) );
43 QStringList FeedSettingsWidget::feedUrls() const
45 QStringList urls;
46 for ( int i = 0; i < ui.feedListWidget->count(); ++i ) {
47 urls.append( ui.feedListWidget->item( i )->text() );
49 return urls;
52 void FeedSettingsWidget::feedItemChanged()
54 QListWidgetItem *item = ui.feedListWidget->currentItem();
55 ui.removeButton->setEnabled( item != 0 );
56 if ( item == 0 ) {
57 return;
60 QMap<QUrl, Syndication::FeedPtr> availableFeeds = NewsFeedManager::self()->availableFeeds();
61 QMap<QUrl, Syndication::FeedPtr>::ConstIterator it = availableFeeds.find( item->text() );
62 if ( it == availableFeeds.end() ) {
63 kDebug( 500 ) << "Don't have this item " << item->text();
64 return;
67 Syndication::FeedPtr feed = *it;
68 ui.feedTitleLabel->setText( feed->title() );
69 ui.feedUrlLabel->setText( feed->link() );
70 ui.feedDescriptionLabel->setText( feed->description() );
74 void FeedSettingsWidget::addButtonClicked()
76 bool ok;
77 QString url = KInputDialog::getText( i18n( "New Newsfeed" ),
78 i18n( "Enter the Address (URL) of the Newsfeed to be added:" ),
79 QString(),
80 &ok,
81 this );
83 if ( ok && !url.isEmpty() ) {
84 NewsFeedManager::self()->updateFeed( url );
85 m_downloadMessageBox = new KProgressDialog( this,
86 i18n( "Please wait..." ),
87 i18n( "Please wait while the newsfeed is downloaded..." ) );
88 m_downloadMessageBox->progressBar()->setRange( 0, 0 );
89 m_downloadMessageBox->exec();
93 void FeedSettingsWidget::removeButtonClicked()
95 int row = ui.feedListWidget->currentRow();
96 delete ui.feedListWidget->takeItem( row );
98 const int remainingItems = ui.feedListWidget->count();
99 if ( remainingItems > 0 ) {
100 if ( row == remainingItems ) {
101 row = remainingItems - 1;
103 ui.feedListWidget->setCurrentRow( row );
108 void FeedSettingsWidget::feedLoaded( const QUrl &url )
110 delete m_downloadMessageBox;
111 m_downloadMessageBox = 0;
113 QListWidgetItem *item = new QListWidgetItem( url.toString() );
114 ui.feedListWidget->addItem( item );
115 ui.feedListWidget->setCurrentItem( item );
118 #include "feedsettingswidget.moc"