summaryrefslogtreecommitdiffstats
path: root/knewsticker/newssourcedlgimpl.cpp
blob: e5c50090689474a7482e0a24b4c20c720b2f2a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * newssourcedlgimpl.cpp
 *
 * Copyright (c) 2001 Frerich Raabe <raabe@kde.org>
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "newssourcedlgimpl.h"
#include "xmlnewsaccess.h"
#include "configaccess.h"
#include "newsiconmgr.h"

#include <kcombobox.h>
#include <klineedit.h>
#include <kmessagebox.h>
#include <knuminput.h>
#include <kurlrequester.h>

#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqprogressbar.h>
#include <tqtimer.h>
#include <tqvbox.h>

SuggestProgressDlg::SuggestProgressDlg(const KURL &url, TQWidget *parent, const char *name)
	: KDialogBase(parent, name, true, i18n("Downloading Data"), Cancel, Cancel),
	m_gotSourceFile(false),
	m_gotIcon(false)
{
	TQVBox *mainWidget = makeVBoxMainWidget();

	new TQLabel(i18n("<qt>Please wait while KNewsTicker is downloading some "
	                "data necessary to suggest reasonable values.<br/>"
	                "<br/>"
	                "This will not take longer than one minute.</qt>" ),
	                mainWidget);

	m_progressBar = new TQProgressBar(60, mainWidget);
	m_progressBar->setPercentageVisible(false);

	m_timeoutTimer = new TQTimer(this);
	connect(m_timeoutTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimeoutTick()));
	m_timeoutTimer->start(1000);

	m_xmlSrc = new XMLNewsSource;
	connect(m_xmlSrc, TQT_SIGNAL(loadComplete(XMLNewsSource *, bool)),
	        this, TQT_SLOT(slotLoadComplete(XMLNewsSource *, bool)));
	m_xmlSrc->loadFrom(url);

	connect(NewsIconMgr::self(), TQT_SIGNAL(gotIcon(const KURL &, const TQPixmap &)),
	        this, TQT_SLOT(slotGotIcon(const KURL &, const TQPixmap &)));
	KURL u = url;
	if (url.isLocalFile())
		u = TQString();
	else
		u.setEncodedPathAndQuery(TQString::fromLatin1("/favicon.ico"));
	NewsIconMgr::self()->getIcon(u);
}

SuggestProgressDlg::~SuggestProgressDlg()
{
	delete m_xmlSrc;
}

void SuggestProgressDlg::slotTimeoutTick()
{
	if (m_progressBar->progress() == m_progressBar->totalSteps()) {
		m_timeoutTimer->stop();
		KMessageBox::error(this, i18n("Could not retrieve the specified source file."));
		reject();
		return;
	}
	m_progressBar->setProgress(m_progressBar->progress() + 1);
}

void SuggestProgressDlg::slotLoadComplete(XMLNewsSource *, bool succeeded)
{
	m_gotSourceFile = true;
	m_succeeded = succeeded;

	if (m_gotIcon)
		done(succeeded ? TQDialog::Accepted : TQDialog::Rejected);
}

void SuggestProgressDlg::slotGotIcon(const KURL &url, const TQPixmap &pixmap)
{
	m_gotIcon = true;
	m_icon = pixmap;
	m_iconURL = url;

	if (m_gotIcon)
		done(m_succeeded ? TQDialog::Accepted : TQDialog::Rejected);
}

NewsSourceDlgImpl::NewsSourceDlgImpl(TQWidget *parent,  const char *name, bool modal, WFlags fl)
	: NewsSourceDlg(parent, name, modal, fl),
	m_modified(false)
{
	connect(NewsIconMgr::self(), TQT_SIGNAL(gotIcon(const KURL &, const TQPixmap &)),
	        this, TQT_SLOT(slotGotIcon(const KURL &, const TQPixmap &)));

	for (unsigned int i = 0; i < DEFAULT_SUBJECTS; i++)
		comboCategory->insertItem(
				NewsSourceBase::subjectText(static_cast<NewsSourceBase::Subject>(i)));

}

void NewsSourceDlgImpl::slotCancelClicked()
{
	close();
}

void NewsSourceDlgImpl::slotOkClicked()
{
	KURL url (polishedURL(KURL(urlSourceFile->url())));

	if (!validateURL(url))
		return;

	if (leName->text().isEmpty()) {
		KMessageBox::error(this, i18n("You have to specify a name for this news"
					" source to be able to use it."), i18n("No Name Specified"));
		return;
	}

	// This finds out which subject is selected in the 'Subject' combo box.
	NewsSourceBase::Subject subject = NewsSourceBase::Computers;
	for (unsigned int i = 0; i < DEFAULT_SUBJECTS; i++) {
		NewsSourceBase::Subject thisSubj = static_cast<NewsSourceBase::Subject>(i);
		if (comboCategory->currentText() == NewsSourceBase::subjectText(thisSubj)) {
			subject = thisSubj;
			break;
		}
	}

	KURL iconURL ( leIcon->text() );
	if (iconURL.protocol().isEmpty())
		if (iconURL.host().startsWith(TQString::fromLatin1("ftp.")))
			iconURL.setProtocol(TQString::fromLatin1("ftp"));
		else if (iconURL.host().startsWith(TQString::fromLatin1("www.")))
			iconURL.setProtocol(TQString::fromLatin1("http"));
		else
			iconURL.setProtocol(TQString::fromLatin1("file"));

	NewsSourceBase::Data nsd(leName->text(), url.url(), iconURL.url(), subject,
			sbMaxArticles->value(), true, cbProgram->isChecked());

	emit newsSource(nsd);

	close();
}

void NewsSourceDlgImpl::slotSourceFileChanged()
{
	bSuggest->setEnabled(!urlSourceFile->url().isEmpty());
}

void NewsSourceDlgImpl::slotSuggestClicked()
{
	KURL url ( polishedURL(KURL( urlSourceFile->url() )) );

	if (!validateURL(url))
		return;

	SuggestProgressDlg dlg(url, this);
	if (dlg.exec() == TQDialog::Accepted) {
		pixmapIcon->setPixmap(dlg.icon());
		if (NewsIconMgr::self()->isStdIcon(dlg.icon()))
			leIcon->clear();
		else
			leIcon->setText(dlg.iconURL().url());
		cbProgram->setChecked(false);
		leName->setText(dlg.xmlSrc()->newsSourceName());
		sbMaxArticles->setValue(dlg.xmlSrc()->articles().count());
	}
}

void NewsSourceDlgImpl::slotModified()
{
	m_modified = true;
}

void NewsSourceDlgImpl::setup(const NewsSourceBase::Data &nsd, bool modify)
{
	leName->setText(nsd.name);
	urlSourceFile->setURL(nsd.sourceFile);
	cbProgram->setChecked(nsd.isProgram);
	comboCategory->setCurrentItem(nsd.subject);
	sbMaxArticles->setValue(nsd.maxArticles);
	KURL iconURL ( nsd.icon );
	if (iconURL.protocol() == TQString::fromLatin1("file"))
		iconURL.setProtocol(TQString());
	leIcon->setText(iconURL.url());
	NewsIconMgr::self()->getIcon(iconURL);
	if (modify == true) {
		setCaption(i18n("Edit News Source"));
	}
}

KURL NewsSourceDlgImpl::polishedURL(const KURL &url) const
{
	KURL newURL = url;

	if (url.protocol().isEmpty())
		if (url.url().startsWith(TQString::fromLatin1("ftp")))
			newURL = TQString::fromLatin1("ftp://") + url.url();
		else
			newURL = TQString::fromLatin1("http://") + url.url();

	return newURL;
}

bool NewsSourceDlgImpl::validateURL(const KURL &url)
{
	if (url.isEmpty()) {
		KMessageBox::error(this, i18n("You have to specify the source file for this"
					" news source to be able to use it."), i18n("No Source File"
					" Specified"));
		return false;
	}

	if (!url.isValid() || !url.hasPath() || url.encodedPathAndQuery() == TQString::fromLatin1("/")) {
		KMessageBox::error(this, i18n("KNewsTicker needs a valid RDF or RSS file to"
					" suggest sensible values. The specified source file is invalid."),
					i18n("Invalid Source File"));
		return false;
	}

	return true;
}

void NewsSourceDlgImpl::slotGotIcon(const KURL &, const TQPixmap &pixmap)
{
	pixmapIcon->setPixmap(pixmap);
}

#include "newssourcedlgimpl.moc"