24 #include <tqgroupbox.h>
27 #include <tqlineedit.h>
28 #include <tqvaluevector.h>
29 #include <tqspinbox.h>
32 #include <dcopclient.h>
34 #include <tdeaboutdata.h>
35 #include <tdeapplication.h>
36 #include <tdeaccelmanager.h>
37 #include <tdeconfig.h>
39 #include <kdialogbase.h>
40 #include <tdelistview.h>
41 #include <tdelocale.h>
42 #include <kpushbutton.h>
44 #include "kcmkontactknt.h"
46 #include "newsfeeds.h"
48 #include <tdemacros.h>
52 TDE_EXPORT TDECModule *create_kontactknt( TQWidget *parent,
const char * )
54 return new KCMKontactKNT( parent,
"kcmkontactknt" );
58 NewsEditDialog::NewsEditDialog(
const TQString& title,
const TQString& url, TQWidget *parent )
59 : KDialogBase( Plain, i18n(
"New News Feed" ), Ok | Cancel,
60 Ok, parent, 0, true, true )
62 TQWidget *page = plainPage();
63 TQGridLayout *layout =
new TQGridLayout( page, 2, 3, marginHint(),
66 TQLabel *label =
new TQLabel( i18n(
"Name:" ), page );
67 layout->addWidget( label, 0, 0 );
69 mTitle =
new TQLineEdit( page );
70 label->setBuddy( mTitle );
71 layout->addMultiCellWidget( mTitle, 0, 0, 1, 2 );
73 label =
new TQLabel( i18n(
"URL:" ), page );
74 layout->addWidget( label, 1, 0 );
76 mURL =
new TQLineEdit( page );
77 label->setBuddy( mURL );
78 layout->addMultiCellWidget( mURL, 1, 1, 1, 2 );
80 mTitle->setText( title );
83 connect( mTitle, TQ_SIGNAL( textChanged(
const TQString& ) ),
84 this, TQ_SLOT( modified() ) );
85 connect( mURL, TQ_SIGNAL( textChanged(
const TQString& ) ),
86 this, TQ_SLOT( modified() ) );
91 void NewsEditDialog::modified()
93 enableButton( KDialogBase::Ok, !title().isEmpty() && !url().isEmpty() );
96 TQString NewsEditDialog::title()
const
98 return mTitle->text();
101 TQString NewsEditDialog::url()
const
106 class NewsItem :
public TQListViewItem
109 NewsItem( TQListView *parent,
const TQString& title,
const TQString& url,
bool custom )
110 : TQListViewItem( parent ), mTitle( title ), mUrl( url ), mCustom( custom )
112 setText( 0, mTitle );
115 NewsItem( TQListViewItem *parent,
const TQString& title,
const TQString& url,
bool custom )
116 : TQListViewItem( parent ), mTitle( title ), mUrl( url ), mCustom( custom )
118 setText( 0, mTitle );
121 TQString title()
const {
return mTitle; }
122 TQString url()
const {
return mUrl; }
123 bool custom()
const {
return mCustom; }
131 KCMKontactKNT::KCMKontactKNT( TQWidget *parent,
const char *name )
132 : TDECModule( parent, name )
136 connect( mAllNews, TQ_SIGNAL( currentChanged( TQListViewItem* ) ),
137 this, TQ_SLOT( allCurrentChanged( TQListViewItem* ) ) );
138 connect( mSelectedNews, TQ_SIGNAL( selectionChanged( TQListViewItem* ) ),
139 this, TQ_SLOT( selectedChanged( TQListViewItem* ) ) );
141 connect( mUpdateInterval, TQ_SIGNAL( valueChanged(
int ) ), TQ_SLOT( modified() ) );
142 connect( mArticleCount, TQ_SIGNAL( valueChanged(
int ) ), TQ_SLOT( modified() ) );
144 connect( mAddButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( addNews() ) );
145 connect( mRemoveButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( removeNews() ) );
146 connect( mNewButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( newFeed() ) );
147 connect( mDeleteButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( deleteFeed() ) );
149 TDEAcceleratorManager::manage(
this );
154 void KCMKontactKNT::loadNews()
156 TQValueVector<TQListViewItem*> parents;
157 TQValueVector<TQListViewItem*>::Iterator it;
159 parents.append(
new TQListViewItem( mAllNews, i18n(
"Arts" ) ) );
160 parents.append(
new TQListViewItem( mAllNews, i18n(
"Business" ) ) );
161 parents.append(
new TQListViewItem( mAllNews, i18n(
"Computers" ) ) );
162 parents.append(
new TQListViewItem( mAllNews, i18n(
"Misc" ) ) );
163 parents.append(
new TQListViewItem( mAllNews, i18n(
"Recreation" ) ) );
164 parents.append(
new TQListViewItem( mAllNews, i18n(
"Society" ) ) );
166 for ( it = parents.begin(); it != parents.end(); ++it )
167 (*it)->setSelectable(
false );
169 for (
int i = 0; i < DEFAULT_NEWSSOURCES; ++i ) {
170 NewsSourceData data = NewsSourceDefault[ i ];
171 new NewsItem( parents[ data.category() ], data.name(), data.url(),
false );
172 mFeedMap.insert( data.url(), data.name() );
176 void KCMKontactKNT::loadCustomNews()
178 TDEConfig config(
"kcmkontactkntrc" );
179 TQMap<TQString, TQString> customFeeds = config.entryMap(
"CustomFeeds" );
180 config.setGroup(
"CustomFeeds" );
182 mCustomItem =
new TQListViewItem( mAllNews, i18n(
"Custom" ) );
183 mCustomItem->setSelectable(
false );
185 if ( customFeeds.count() == 0 )
186 mCustomItem->setVisible(
false );
188 TQMap<TQString, TQString>::Iterator it;
189 for ( it = customFeeds.begin(); it != customFeeds.end(); ++it ) {
190 TQStringList value = config.readListEntry( it.key() );
191 mCustomFeeds.append(
new NewsItem( mCustomItem, value[ 0 ], value[ 1 ],
true ) );
192 mFeedMap.insert( value[ 1 ], value[ 0 ] );
193 mCustomItem->setVisible(
true );
197 void KCMKontactKNT::storeCustomNews()
199 TDEConfig config(
"kcmkontactkntrc" );
200 config.deleteGroup(
"CustomFeeds" );
201 config.setGroup(
"CustomFeeds" );
204 TQValueList<NewsItem*>::Iterator it;
205 for ( it = mCustomFeeds.begin(); it != mCustomFeeds.end(); ++it ) {
207 value << (*it)->title() << (*it)->url();
208 config.writeEntry( TQString::number( counter ), value );
216 void KCMKontactKNT::addNews()
221 NewsItem *item =
dynamic_cast<NewsItem*
>( mAllNews->selectedItem() );
225 DCOPRef service(
"rssservice",
"RSSService" );
226 service.send(
"add(TQString)", item->url() );
230 emit changed(
true );
233 void KCMKontactKNT::removeNews()
238 NewsItem *item =
dynamic_cast<NewsItem*
>( mSelectedNews->selectedItem() );
242 DCOPRef service(
"rssservice",
"RSSService" );
243 service.send(
"remove(TQString)", item->url() );
247 emit changed(
true );
250 void KCMKontactKNT::newFeed()
252 NewsEditDialog dlg(
"",
"",
this );
255 NewsItem *item =
new NewsItem( mCustomItem, dlg.title(), dlg.url(),
true );
256 mCustomFeeds.append( item );
257 mFeedMap.insert( dlg.url(), dlg.title() );
259 mCustomItem->setVisible(
true );
260 mCustomItem->setOpen(
true );
261 mAllNews->ensureItemVisible( item );
262 mAllNews->setSelected( item,
true );
264 emit changed(
true );
268 void KCMKontactKNT::deleteFeed()
270 NewsItem *item =
dynamic_cast<NewsItem*
>( mAllNews->selectedItem() );
274 if ( mCustomFeeds.find( item ) == mCustomFeeds.end() )
277 mCustomFeeds.remove( item );
278 mFeedMap.remove( item->url() );
281 if ( mCustomFeeds.count() == 0 )
282 mCustomItem->setVisible(
false );
284 emit changed(
true );
287 void KCMKontactKNT::scanNews()
292 mSelectedNews->clear();
294 DCOPRef service(
"rssservice",
"RSSService" );
295 TQStringList urls = service.call(
"list()" );
297 for ( uint i = 0; i < urls.count(); ++i )
299 TQString url = urls[ i ];
300 TQString feedName = mFeedMap[ url ];
301 if ( feedName.isEmpty() )
303 new NewsItem( mSelectedNews, feedName, url,
false );
307 void KCMKontactKNT::selectedChanged( TQListViewItem *item )
309 mRemoveButton->setEnabled( item && item->isSelected() );
312 void KCMKontactKNT::allCurrentChanged( TQListViewItem *item )
314 NewsItem *newsItem =
dynamic_cast<NewsItem*
>( item );
316 bool addState =
false;
317 bool delState =
false;
318 if ( newsItem && newsItem->isSelected() ) {
320 delState = (mCustomFeeds.find( newsItem ) != mCustomFeeds.end());
323 mAddButton->setEnabled( addState );
324 mDeleteButton->setEnabled( delState );
327 void KCMKontactKNT::modified()
329 emit changed(
true );
332 void KCMKontactKNT::initGUI()
334 TQGridLayout *layout =
new TQGridLayout(
this, 2, 3, KDialog::marginHint(),
335 KDialog::spacingHint() );
337 mAllNews =
new TDEListView(
this );
338 mAllNews->addColumn( i18n(
"All" ) );
339 mAllNews->setRootIsDecorated(
true );
340 mAllNews->setFullWidth(
true );
341 layout->addWidget( mAllNews, 0, 0 );
343 TQVBoxLayout *vbox =
new TQVBoxLayout( layout, KDialog::spacingHint() );
346 mAddButton =
new KPushButton( i18n(
"Add" ),
this );
347 mAddButton->setEnabled(
false );
348 vbox->addWidget( mAddButton );
349 mRemoveButton =
new KPushButton( i18n(
"Remove" ),
this );
350 mRemoveButton->setEnabled(
false );
351 vbox->addWidget( mRemoveButton );
354 mSelectedNews =
new TDEListView(
this );
355 mSelectedNews->addColumn( i18n(
"Selected" ) );
356 mSelectedNews->setFullWidth(
true );
357 layout->addWidget( mSelectedNews, 0, 2 );
359 TQGroupBox *box =
new TQGroupBox( 0, TQt::Vertical,
360 i18n(
"News Feed Settings" ),
this );
362 TQGridLayout *boxLayout =
new TQGridLayout( box->layout(), 2, 3,
363 KDialog::spacingHint() );
365 TQLabel *label =
new TQLabel( i18n(
"Refresh time:" ), box );
366 boxLayout->addWidget( label, 0, 0 );
368 mUpdateInterval =
new TQSpinBox( 1, 3600, 1, box );
369 mUpdateInterval->setSuffix(
" sec." );
370 label->setBuddy( mUpdateInterval );
371 boxLayout->addWidget( mUpdateInterval, 0, 1 );
373 label =
new TQLabel( i18n(
"Number of items shown:" ), box );
374 boxLayout->addWidget( label, 1, 0 );
376 mArticleCount =
new TQSpinBox( box );
377 label->setBuddy( mArticleCount );
378 boxLayout->addWidget( mArticleCount, 1, 1 );
380 mNewButton =
new KPushButton( i18n(
"New Feed..." ), box );
381 boxLayout->addWidget( mNewButton, 0, 2 );
383 mDeleteButton =
new KPushButton( i18n(
"Delete Feed" ), box );
384 mDeleteButton->setEnabled(
false );
385 boxLayout->addWidget( mDeleteButton, 1, 2 );
387 layout->addMultiCellWidget( box, 1, 1, 0, 2 );
390 bool KCMKontactKNT::dcopActive()
const
395 DCOPClient *client = kapp->dcopClient();
396 if ( !client->isApplicationRegistered(
"rssservice" ) ) {
397 if ( TDEApplication::startServiceByDesktopName(
"rssservice", TQStringList(), &error, &appID ) )
404 void KCMKontactKNT::load()
412 TDEConfig config(
"kcmkontactkntrc" );
413 config.setGroup(
"General" );
415 mUpdateInterval->setValue( config.readNumEntry(
"UpdateInterval", 600 ) );
416 mArticleCount->setValue( config.readNumEntry(
"ArticleCount", 4 ) );
418 emit changed(
false );
421 void KCMKontactKNT::save()
425 TDEConfig config(
"kcmkontactkntrc" );
426 config.setGroup(
"General" );
428 config.writeEntry(
"UpdateInterval", mUpdateInterval->value() );
429 config.writeEntry(
"ArticleCount", mArticleCount->value() );
433 emit changed(
false );
436 void KCMKontactKNT::defaults()
440 const TDEAboutData* KCMKontactKNT::aboutData()
const
442 TDEAboutData *about =
new TDEAboutData( I18N_NOOP(
"kcmkontactknt" ),
443 I18N_NOOP(
"Newsticker Configuration Dialog" ),
444 0, 0, TDEAboutData::License_GPL,
445 I18N_NOOP(
"(c) 2003 - 2004 Tobias Koenig" ) );
447 about->addAuthor(
"Tobias Koenig", 0,
"tokoe@kde.org" );
452 #include "kcmkontactknt.moc"