summaryrefslogtreecommitdiffstats
path: root/src/radiostation.cpp
blob: e61ebada3ab0df874f004c496a967d38455006d8 (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
/***************************************************************************
                          radiostation.cpp  -  description
                             -------------------
    begin                : Sat Feb 2 2002
    copyright            : (C) 2002 by Martin Witte / Frank Schwanz
    email                : witte@kawo1.rwth-aachen.de / schwanz@fh-brandenburg.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "include/radiostation.h"
#include "include/radiostation-config.h"

#include <unistd.h>
#include <time.h>
#include <fcntl.h>

extern const char *StationNameElement;
extern const char *StationShortNameElement;
extern const char *StationIconStringElement;
extern const char *StationVolumePresetElement;
extern const char *StationIDElement;

const char *StationNameElement          = "name";
const char *StationShortNameElement     = "shortname";
const char *StationIconStringElement    = "icon";
const char *StationVolumePresetElement  = "volumepreset";
const char *StationIDElement            = "stationID";

const char *dev_urandom_radiostations = "/dev/urandom";

/////////////////////////////////////////////////////////////////////////////

TQDict<RadioStation> *RadioStation::stationClassRegistry = 0;

/////////////////////////////////////////////////////////////////////////////

TDE_EXPORT RegisterStationClass registerStationClass;
extern "C" const TDE_EXPORT UndefinedRadioStation undefinedRadioStation (registerStationClass);

/////////////////////////////////////////////////////////////////////////////


RadioStation::RadioStation(RegisterStationClass, const TQString &classname)
    : m_stationID(TQString()),  // mark this station as a prototype station
                                   // so that we can create later a real stationID
      m_name(TQString()),
      m_shortName(TQString()),
      m_initialVolume(-1),
      m_iconName(TQString())
{
    if (! stationClassRegistry)
        stationClassRegistry = new TQDict<RadioStation>;
    stationClassRegistry->insert(classname, this);
}

RadioStation::RadioStation()
    : m_name(TQString()),
      m_shortName(TQString()),
      m_initialVolume(-1),
      m_iconName(TQString())
{
    generateNewStationID();
}

RadioStation::RadioStation(const TQString &name, const TQString &shortName)
    : m_name(name),
      m_shortName(shortName),
      m_initialVolume(-1),
      m_iconName(TQString())
{
    generateNewStationID();
}


RadioStation::RadioStation(const RadioStation &s)
    : m_stationID(s.m_stationID),
      m_name(s.m_name),
      m_shortName(s.m_shortName),
      m_initialVolume(s.m_initialVolume),
      m_iconName(s.m_iconName)
{
    // create a real stationID if "s" is a prototype
    if (m_stationID.isNull())
        generateNewStationID();
}


RadioStation::~RadioStation()
{
}


void  RadioStation::copyDescriptionFrom(const RadioStation &rs)
{
    m_name      = rs.m_name;
    m_shortName = rs.m_shortName;
    m_iconName  = rs.m_iconName;
    m_stationID = rs.m_stationID;
}


void RadioStation::generateNewStationID()
{
    const int buffersize = 32;
    unsigned char buffer[buffersize];

    TQString stime, srandom = TQString();
    stime.setNum(time(NULL));

    int fd = open (dev_urandom_radiostations, O_RDONLY);
    read(fd, buffer, buffersize);
    close(fd);
    for (int i = 0; i < buffersize; ++i)
        srandom += TQString().sprintf("%02X", (unsigned int)buffer[i]);

    m_stationID = stime + srandom;
}


RadioStation const *RadioStation::getStationClass(const TQString &classname)
{
    if (stationClassRegistry)
        return stationClassRegistry->find(classname);
    else
        return NULL;
}


bool RadioStation::setProperty(const TQString &pn, const TQString &val)
{
    bool retval = false;
    if (pn == StationIDElement) {
        m_stationID = val;
        retval = true;
    } else if (pn == StationNameElement) {
        m_name = val;
        retval = true;
    } else if (pn == StationShortNameElement) {
        m_shortName = val;
        retval = true;
    } else if (pn == StationIconStringElement) {
        m_iconName = val;
        retval = true;
    } else if (pn == StationVolumePresetElement) {
        float x = val.toFloat(&retval);
        if (retval)
            m_initialVolume = x;
    }
    return retval;
}


TQString RadioStation::getProperty(const TQString &pn) const
{
    if (pn == StationIDElement) {
        return m_stationID;
    } else if (pn == StationNameElement) {
        return m_name;
    } else if (pn == StationShortNameElement) {
        return m_shortName;
    } else if (pn == StationIconStringElement) {
        return m_iconName;
    } else if (pn == StationVolumePresetElement) {
        return TQString().setNum(m_initialVolume);
    } else {
        return TQString();
    }
}


TQStringList RadioStation::getPropertyNames() const
{
    TQStringList l;
    l.push_back(StationIDElement);
    l.push_back(StationNameElement);
    l.push_back(StationShortNameElement);
    l.push_back(StationIconStringElement);
    l.push_back(StationVolumePresetElement);
    return l;
}

bool RadioStation::operator == (const RadioStation &x) const
{
    return m_stationID     == x.m_stationID &&
           m_name          == x.m_name      &&
           m_shortName     == x.m_shortName &&
           m_initialVolume == x.m_initialVolume &&
           m_iconName      == x.m_iconName;
}

/////////////////////////////////////////////////////////////////////////

int UndefinedRadioStation::compare(const RadioStation &_s) const
{
    UndefinedRadioStation const *s = dynamic_cast<UndefinedRadioStation const*>(&_s);

    if (!s)
        return -1;

    return 0;

}


RadioStationConfig *UndefinedRadioStation::createEditor() const
{
    return new UndefinedRadioStationConfig(NULL);
}