2 * newssourcedlgimpl.cpp
4 * Copyright (c) 2001 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 "newssourcedlgimpl.h"
12 #include "xmlnewsaccess.h"
13 #include "configaccess.h"
14 #include "newsiconmgr.h"
16 #include <kcombobox.h>
17 #include <klineedit.h>
18 #include <kmessagebox.h>
19 #include <knuminput.h>
20 #include <kurlrequester.h>
23 #include <qcheckbox.h>
25 #include <QProgressBar>
29 SuggestProgressDlg::SuggestProgressDlg(const KUrl
&url
, QWidget
*parent
, const char *name
)
31 m_gotSourceFile(false),
34 setCaption( i18n("Downloading Data") );
36 setDefaultButton( Cancel
);
38 KVBox
*mainWidget
= new KVBox( this );
39 setMainWidget( mainWidget
);
41 new QLabel(i18n("<qt>Please wait while KNewsTicker is downloading some "
42 "data necessary to suggest reasonable values.<br/>"
44 "This will not take longer than one minute.</qt>" ),
47 m_progressBar
= new QProgressBar(mainWidget
);
48 m_progressBar
->setMaximum(60);
49 m_progressBar
->setTextVisible(false);
51 m_timeoutTimer
= new QTimer(this);
52 connect(m_timeoutTimer
, SIGNAL(timeout()), this, SLOT(slotTimeoutTick()));
53 m_timeoutTimer
->start(1000);
55 m_xmlSrc
= new XMLNewsSource
;
56 connect(m_xmlSrc
, SIGNAL(loadComplete(XMLNewsSource
*, bool)),
57 this, SLOT(slotLoadComplete(XMLNewsSource
*, bool)));
58 m_xmlSrc
->loadFrom(url
);
60 connect(NewsIconMgr::self(), SIGNAL(gotIcon(const KUrl
&, const QPixmap
&)),
61 this, SLOT(slotGotIcon(const KUrl
&, const QPixmap
&)));
63 if (url
.isLocalFile())
66 u
.setEncodedPathAndQuery(QLatin1String("/favicon.ico"));
67 NewsIconMgr::self()->getIcon(u
);
70 SuggestProgressDlg::~SuggestProgressDlg()
75 void SuggestProgressDlg::slotTimeoutTick()
77 if (m_progressBar
->value() == m_progressBar
->maximum()) {
78 m_timeoutTimer
->stop();
79 KMessageBox::error(this, i18n("Could not retrieve the specified source file."));
83 m_progressBar
->setValue(m_progressBar
->value() + 1);
86 void SuggestProgressDlg::slotLoadComplete(XMLNewsSource
*, bool succeeded
)
88 m_gotSourceFile
= true;
89 m_succeeded
= succeeded
;
92 done(succeeded
? QDialog::Accepted
: QDialog::Rejected
);
95 void SuggestProgressDlg::slotGotIcon(const KUrl
&url
, const QPixmap
&pixmap
)
102 done(m_succeeded
? QDialog::Accepted
: QDialog::Rejected
);
105 NewsSourceDlgImpl::NewsSourceDlgImpl(QWidget
*parent
, const char *name
, bool modal
, Qt::WFlags fl
)
106 : NewsSourceDlg(parent
, name
, modal
, fl
),
109 connect(NewsIconMgr::self(), SIGNAL(gotIcon(const KUrl
&, const QPixmap
&)),
110 this, SLOT(slotGotIcon(const KUrl
&, const QPixmap
&)));
112 for (unsigned int i
= 0; i
< DEFAULT_SUBJECTS
; i
++)
113 comboCategory
->addItem(
114 NewsSourceBase::subjectText(static_cast<NewsSourceBase::Subject
>(i
)));
118 void NewsSourceDlgImpl::slotCancelClicked()
123 void NewsSourceDlgImpl::slotOkClicked()
125 KUrl
url (polishedURL(KUrl(urlSourceFile
->url())));
127 if (!validateURL(url
))
130 if (leName
->text().isEmpty()) {
131 KMessageBox::error(this, i18n("You have to specify a name for this news"
132 " source to be able to use it."), i18n("No Name Specified"));
136 // This finds out which subject is selected in the 'Subject' combo box.
137 NewsSourceBase::Subject subject
= NewsSourceBase::Computers
;
138 for (unsigned int i
= 0; i
< DEFAULT_SUBJECTS
; i
++) {
139 NewsSourceBase::Subject thisSubj
= static_cast<NewsSourceBase::Subject
>(i
);
140 if (comboCategory
->currentText() == NewsSourceBase::subjectText(thisSubj
)) {
146 KUrl
iconURL ( leIcon
->text() );
147 if (iconURL
.protocol().isEmpty())
148 if (iconURL
.host().startsWith(QLatin1String("ftp.")))
149 iconURL
.setProtocol(QLatin1String("ftp"));
150 else if (iconURL
.host().startsWith(QLatin1String("www.")))
151 iconURL
.setProtocol(QLatin1String("http"));
153 iconURL
.setProtocol(QLatin1String("file"));
155 NewsSourceBase::Data
nsd(leName
->text(), url
.url(), iconURL
.url(), subject
,
156 sbMaxArticles
->value(), true, cbProgram
->isChecked());
158 emit
newsSource(nsd
);
163 void NewsSourceDlgImpl::slotSourceFileChanged()
165 bSuggest
->setEnabled(!urlSourceFile
->url().isEmpty());
168 void NewsSourceDlgImpl::slotSuggestClicked()
170 KUrl
url ( polishedURL(KUrl( urlSourceFile
->url() )) );
172 if (!validateURL(url
))
175 SuggestProgressDlg
dlg(url
, this);
176 if (dlg
.exec() == QDialog::Accepted
) {
177 pixmapIcon
->setPixmap(dlg
.icon());
178 if (NewsIconMgr::self()->isStdIcon(dlg
.icon()))
181 leIcon
->setText(dlg
.iconURL().url());
182 cbProgram
->setChecked(false);
183 leName
->setText(dlg
.xmlSrc()->newsSourceName());
184 sbMaxArticles
->setValue(dlg
.xmlSrc()->articles().count());
188 void NewsSourceDlgImpl::slotModified()
193 void NewsSourceDlgImpl::setup(const NewsSourceBase::Data
&nsd
, bool modify
)
195 leName
->setText(nsd
.name
);
196 urlSourceFile
->setUrl(nsd
.sourceFile
);
197 cbProgram
->setChecked(nsd
.isProgram
);
198 comboCategory
->setCurrentIndex(nsd
.subject
);
199 sbMaxArticles
->setValue(nsd
.maxArticles
);
200 KUrl
iconURL ( nsd
.icon
);
201 if (iconURL
.protocol() == QLatin1String("file"))
202 iconURL
.setProtocol(QString::null
);
203 leIcon
->setText(iconURL
.url());
204 NewsIconMgr::self()->getIcon(iconURL
);
205 if (modify
== true) {
206 setWindowTitle(i18n("Edit News Source"));
210 KUrl
NewsSourceDlgImpl::polishedURL(const KUrl
&url
) const
214 if (url
.protocol().isEmpty())
215 if (url
.url().startsWith(QLatin1String("ftp")))
216 newURL
= QLatin1String("ftp://") + url
.url();
218 newURL
= QLatin1String("http://") + url
.url();
223 bool NewsSourceDlgImpl::validateURL(const KUrl
&url
)
226 KMessageBox::error(this, i18n("You have to specify the source file for this"
227 " news source to be able to use it."), i18n("No Source File"
232 if (!url
.isValid() || !url
.hasPath() || url
.encodedPathAndQuery() == QLatin1String("/")) {
233 KMessageBox::error(this, i18n("KNewsTicker needs a valid RDF or RSS file to"
234 " suggest sensible values. The specified source file is invalid."),
235 i18n("Invalid Source File"));
242 void NewsSourceDlgImpl::slotGotIcon(const KUrl
&, const QPixmap
&pixmap
)
244 pixmapIcon
->setPixmap(pixmap
);
247 #include "newssourcedlgimpl.moc"