* fix crash in PersonalInviteDialog
[kdenetwork.git] / knewsticker / newssourcedlgimpl.cpp
blobcc8ae7bb1011256820697c587c667b971578557c
1 /*
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>
21 #include <kvbox.h>
23 #include <qcheckbox.h>
24 #include <qlabel.h>
25 #include <QProgressBar>
26 #include <qtimer.h>
27 #include <QPixmap>
29 SuggestProgressDlg::SuggestProgressDlg(const KUrl &url, QWidget *parent, const char *name)
30 : KDialog(parent),
31 m_gotSourceFile(false),
32 m_gotIcon(false)
34 setCaption( i18n("Downloading Data") );
35 setButtons( Cancel );
36 setDefaultButton( Cancel );
37 setModal( true );
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/>"
43 "<br/>"
44 "This will not take longer than one minute.</qt>" ),
45 mainWidget);
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 &)));
62 KUrl u = url;
63 if (url.isLocalFile())
64 u = QString::null;
65 else
66 u.setEncodedPathAndQuery(QLatin1String("/favicon.ico"));
67 NewsIconMgr::self()->getIcon(u);
70 SuggestProgressDlg::~SuggestProgressDlg()
72 delete m_xmlSrc;
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."));
80 reject();
81 return;
83 m_progressBar->setValue(m_progressBar->value() + 1);
86 void SuggestProgressDlg::slotLoadComplete(XMLNewsSource *, bool succeeded)
88 m_gotSourceFile = true;
89 m_succeeded = succeeded;
91 if (m_gotIcon)
92 done(succeeded ? QDialog::Accepted : QDialog::Rejected);
95 void SuggestProgressDlg::slotGotIcon(const KUrl &url, const QPixmap &pixmap)
97 m_gotIcon = true;
98 m_icon = pixmap;
99 m_iconURL = url;
101 if (m_gotIcon)
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),
107 m_modified(false)
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()
120 close();
123 void NewsSourceDlgImpl::slotOkClicked()
125 KUrl url (polishedURL(KUrl(urlSourceFile->url())));
127 if (!validateURL(url))
128 return;
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"));
133 return;
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)) {
141 subject = thisSubj;
142 break;
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"));
152 else
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);
160 close();
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))
173 return;
175 SuggestProgressDlg dlg(url, this);
176 if (dlg.exec() == QDialog::Accepted) {
177 pixmapIcon->setPixmap(dlg.icon());
178 if (NewsIconMgr::self()->isStdIcon(dlg.icon()))
179 leIcon->clear();
180 else
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()
190 m_modified = true;
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
212 KUrl newURL = url;
214 if (url.protocol().isEmpty())
215 if (url.url().startsWith(QLatin1String("ftp")))
216 newURL = QLatin1String("ftp://") + url.url();
217 else
218 newURL = QLatin1String("http://") + url.url();
220 return newURL;
223 bool NewsSourceDlgImpl::validateURL(const KUrl &url)
225 if (url.isEmpty()) {
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"
228 " Specified"));
229 return false;
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"));
236 return false;
239 return true;
242 void NewsSourceDlgImpl::slotGotIcon(const KUrl &, const QPixmap &pixmap)
244 pixmapIcon->setPixmap(pixmap);
247 #include "newssourcedlgimpl.moc"