1 /*
2  * dvbchannel.h
3  *
4  * Copyright (C) 2007-2011 Christoph Pfister <christophpfister@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef DVBCHANNEL_H
22 #define DVBCHANNEL_H
23 
24 #include <QObject>
25 #include <QMultiHash>
26 #include "../shareddata.h"
27 #include "../sqlinterface.h"
28 #include "dvbtransponder.h"
29 
30 class DvbChannel : public SharedData, public SqlKey
31 {
32 public:
DvbChannel()33 	DvbChannel() : number(-1), networkId(-1), transportStreamId(-1), pmtPid(-1), serviceId(-1),
34 		audioPid(-1), hasVideo(false), isScrambled(false) { }
~DvbChannel()35 	~DvbChannel() { }
36 
37 	// checks that all variables are ok
38 	// updates 'pmtSectionData' if 'serviceId' is valid
39 	// (else updates 'serviceId' if 'pmtSectionData' is valid)
40 	// 'sqlKey' is ignored
41 	bool validate();
42 
43 	QString name;
44 	int number;
45 
46 	QString source;
47 	DvbTransponder transponder;
48 	int networkId; // may be -1 (not present), atsc meaning: source id
49 	int transportStreamId;
50 	int pmtPid;
51 
52 	QByteArray pmtSectionData;
53 	int serviceId;
54 	int audioPid; // may be -1 (not present), doesn't have to be present in the pmt section
55 	bool hasVideo;
56 	bool isScrambled;
57 };
58 
59 typedef ExplicitlySharedDataPointer<const DvbChannel> DvbSharedChannel;
60 Q_DECLARE_TYPEINFO(DvbSharedChannel, Q_MOVABLE_TYPE);
61 
62 class DvbChannelId
63 {
64 public:
DvbChannelId(const DvbChannel * channel_)65 	explicit DvbChannelId(const DvbChannel *channel_) : channel(channel_) { }
DvbChannelId(const DvbSharedChannel & channel_)66 	explicit DvbChannelId(const DvbSharedChannel &channel_) : channel(channel_.constData()) { }
~DvbChannelId()67 	~DvbChannelId() { }
68 
69 	// compares channels by transmission type and
70 	// ( dvb) 'source', 'networkId', 'transportStreamId', 'serviceId'
71 	// (atsc) 'source', 'transponder', 'networkId'
72 
73 	bool operator==(const DvbChannelId &other) const;
74 
75 	bool operator!=(const DvbChannelId &other) const
76 	{
77 		return !(*this == other);
78 	}
79 
80 	friend uint qHash(const DvbChannelId &channel);
81 
82 private:
83 	const DvbChannel *channel;
84 };
85 
86 class DvbChannelModel : public QObject, private SqlInterface
87 {
88 	Q_OBJECT
89 public:
90 	explicit DvbChannelModel(QObject *parent);
91 	~DvbChannelModel();
92 
93 	static DvbChannelModel *createSqlModel(QObject *parent);
94 
95 	// channel names and numbers are guaranteed to be unique within this model
96 
97 	QMap<int, DvbSharedChannel> getChannels() const;
98 	DvbSharedChannel findChannelByName(const QString &channelName) const;
99 	bool hasChannelByName(const QString &channelName);
100 	DvbSharedChannel findChannelByNumber(int channelNumber) const;
101 	DvbSharedChannel findChannelById(const DvbChannel &channel) const;
102 
103 	void cloneFrom(DvbChannelModel *other);
104 	void addChannel(DvbChannel &channel);
105 	void updateChannel(DvbSharedChannel channel, DvbChannel &modifiedChannel);
106 	void removeChannel(DvbSharedChannel channel);
107 	bool areInTheSameBunch(DvbSharedChannel channel1, DvbSharedChannel channel2);
108 	void dndMoveChannels(const QList<DvbSharedChannel> &selectedChannels,
109 		int insertBeforeNumber);
110 	void channelFlush();
111 
112 signals:
113 	void channelAdded(const DvbSharedChannel &channel);
114 	// if this is the main model, updating doesn't change the channel pointer
115 	// (modifies existing content); otherwise the channel pointer may be updated
116 	void channelAboutToBeUpdated(const DvbSharedChannel &channel);
117 	void channelUpdated(const DvbSharedChannel &channel);
118 	void channelRemoved(const DvbSharedChannel &channel);
119 
120 private:
121 	void bindToSqlQuery(SqlKey sqlKey, QSqlQuery &query, int index) const;
122 	bool insertFromSqlQuery(SqlKey sqlKey, const QSqlQuery &query, int index);
123 
124 	QString extractBaseName(const QString &name) const;
125 	QString findNextFreeChannelName(const QString &name) const;
126 	int findNextFreeChannelNumber(int number) const;
127 
128 	QMap<QString, DvbSharedChannel> channelNames;
129 	QMap<int, DvbSharedChannel> channelNumbers;
130 	QMultiHash<DvbChannelId, DvbSharedChannel> channelIds;
131 	QMap<SqlKey, DvbSharedChannel> channels; // only used for the sql model
132 	bool hasPendingOperation;
133 	bool isSqlModel;
134 };
135 
136 #endif /* DVBCHANNEL_H */
137