summaryrefslogtreecommitdiffstats
path: root/knewsticker/common/newsengine.h
blob: 554b95fb32cb23e94b7f62240a1afffecdb54d68 (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
/*
 * newsengine.h
 *
 * Copyright (c) 2000, 2001 Frerich Raabe <[email protected]>
 *
 * 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'.
 */
#ifndef NEWSENGINE_H
#define NEWSENGINE_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "configiface.h"
#include "xmlnewsaccess.h"

#include <tqptrlist.h>
#include <tqmap.h>
#include <tqpixmap.h>

#include <kio/job.h>
#include <klocale.h>
#include <ksharedptr.h>

class ConfigAccess;
class NewsIconMgr;
class NewsSourceBase;

class TQBuffer;
class TQDomDocument;

class KProcess;
class KURL;

class KDE_EXPORT Article : public XMLNewsArticle, public KShared
{
	public:
		typedef KSharedPtr<Article> Ptr;
		typedef TQValueList<Ptr> List;

		Article(NewsSourceBase *, const TQString & = TQString(),
				const KURL & = KURL());
		bool operator==(const Article &other) const;

		bool read() const { return m_read; }
		void setRead(bool read) { m_read = read; }

		NewsSourceBase *newsSource() const { return m_parent; }

		void open();

	private:
		NewsSourceBase *m_parent; // don't use KSharedPtr to avoid circular refs!
		bool m_read;
};

class KDE_EXPORT NewsSourceBase : public XMLNewsSource, public KShared
{
	Q_OBJECT
  TQ_OBJECT

	public:
		enum Subject {
			Arts = 0, Business, Computers, Games, Health, Home, Recreation,
			Reference, Science, Shopping, Society, Sports, Misc, Magazines
		};
		struct Data {
			Data(const TQString &_name = I18N_NOOP("Unknown"),
					const TQString &_sourceFile = TQString(),
					const TQString &_icon = TQString(),
					const Subject _subject = Computers,
					unsigned int _maxArticles = 10,
					bool _enabled = true, bool _isProgram = false,
					const TQString &_language = TQString::tqfromLatin1("C"))
			{
				name = _name;
				sourceFile = _sourceFile;
				icon = _icon;
				maxArticles = _maxArticles;
				subject = _subject;
				enabled = _enabled;
				isProgram = _isProgram;
				language = _language;
			}
				
			TQString name;
			TQString sourceFile;
			TQString icon;
			Subject subject;
			unsigned int maxArticles;
			bool enabled;
			bool isProgram;
			TQString language;
		};
		typedef KSharedPtr<NewsSourceBase> Ptr;
		typedef TQValueList<Ptr> List;

		NewsSourceBase(const Data &, ConfigIface *);

		virtual TQString newsSourceName() const;
		TQString sourceFile() const { return m_data.sourceFile; }
		unsigned int maxArticles() const { return m_data.maxArticles; }
		TQPixmap icon() const { return m_icon; }
		void getIcon();

		Data data() const { return m_data; }

		Article::List articles() const { return m_articles; }
		Article::Ptr article(const TQString &);

		static TQString subjectText(const Subject);

	signals:
		void newNewsAvailable(const NewsSourceBase::Ptr &, bool);
		void invalidInput(const NewsSourceBase::Ptr &);

	public slots:
		virtual void retrieveNews() = 0;

	protected slots:
		void slotProcessArticles(XMLNewsSource *, bool);
		void slotGotIcon(const KURL &, const TQPixmap &);

	protected:
		Data          m_data;
		TQPixmap       m_icon;
		ConfigAccess *m_cfg;
		NewsIconMgr  *m_newsIconMgr;
		Article::List m_articles;
};

class KDE_EXPORT SourceFileNewsSource : public NewsSourceBase
{
	Q_OBJECT
  TQ_OBJECT
	
	public:
		SourceFileNewsSource(const NewsSourceBase::Data &, ConfigIface *);
	
	public slots:
		virtual void retrieveNews();
};

// Make sure compilers don't translate
// ProgramNewsSource::ErrorCode into
// enum ErrorCode { 1 = 2, ... }
#undef NOERR
#undef EPERM
#undef ENOENT
#undef EIO
#undef E2BIG
#undef ENOEXEC
#undef EACCESS
#undef ENODEV
#undef ENOSPC
#undef EROFS
#undef ENOSYS
#undef ENODATA
#undef ENONET
#undef EPROTO
#undef EDESTADDRREQ
#undef ESOCKTNOSUPPORT
#undef ENETUNREACH
#undef ENETRESET
#undef ECONNRESET
#undef ETIMEDOUT
#undef ECONNREFUSED
#undef EHOSTDOWN
#undef EHOSTUNREACH

class KDE_EXPORT ProgramNewsSource : public NewsSourceBase
{
	Q_OBJECT
  TQ_OBJECT
	
	public:
		enum ErrorCode { NOERR = 0, EPERM, ENOENT, EIO = 5, E2BIG = 7,
			ENOEXEC, EACCESS = 13, ENODEV = 19, ENOSPC = 28, EROFS = 30,
			ENOSYS = 38, ENODATA = 61, ENONET = 64, EPROTO = 71, EDESTADDRREQ = 89,
			ESOCKTNOSUPPORT = 94, ENETUNREACH = 101, ENETRESET = 102,
			ECONNRESET = 104, ETIMEDOUT = 110, ECONNREFUSED, EHOSTDOWN, EHOSTUNREACH,
			ENOEXECBIT = 126, EBADREQ = 400, ENOAUTH, EMUSTPAY, EFORBIDDEN, ENOTFOUND,
			ETIMEOUT = 408, ESERVERE = 500, EHTTPNOSUP = 505 };
	
		ProgramNewsSource(const NewsSourceBase::Data &, ConfigIface *);
		virtual ~ProgramNewsSource();

	public slots:
		virtual void retrieveNews();

	protected slots:
		void slotGotProgramOutput(KProcess *, char *, int);
		void slotProgramExited(KProcess *);
	
	private:
		static TQString errorMessage(const ErrorCode);
		
		KProcess      *m_program;
		TQBuffer       *m_programOutput;
};

#endif // NEWSENGINE_H