summaryrefslogtreecommitdiffstats
path: root/kradio3/convert-presets/convert-presets.cpp
blob: 7ad2dcabf6bf471466b311ba6fc49dfcdc16dadc (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <kapplication.h>
#include <tqstring.h>
#include <tqtextstream.h>
#include <tqfile.h>
#include <klocale.h>
#include <kdebug.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <tqregexp.h>
#include <time.h>
#include <sys/fcntl.h>
#include <unistd.h>

#define dev_urandom  "/dev/urandom"

TQString createStationID()
{
    const int buffersize = 32;
    unsigned char buffer[buffersize];

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

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

//    kdDebug() << i18n("generated StationID: ") << stime << srandom << endl;

    return stime + srandom;
}




bool convertFile(const TQString &file)
{
    ////////////////////////////////////////////////////////////////////////
    // read input
    ////////////////////////////////////////////////////////////////////////

    TQFile presetFile (file);

    if (! presetFile.open(IO_ReadOnly)) {
        kdDebug() << "convertFile: "
                  << i18n("error opening preset file")
                  << " " << file << " "
                  << i18n("for reading") << endl;
        return false;
    }

    TQString xmlData;

    // make sure that qtextstream is gone when we close presetFile
    {
        TQTextStream ins(&presetFile);
        ins.setEncoding(TQTextStream::Locale);
        xmlData = ins.read();
    }

    if (xmlData.find("<format>", 0, false) >= 0) {
        kdDebug() << "file " << file << " already in new format" << endl;
        // but add <?xml line at beginning if missing

        {
            presetFile.reset();
            TQTextStream ins(&presetFile);
            ins.setEncoding(TQTextStream::UnicodeUTF8);
            xmlData = ins.read();
        }

        if (xmlData.find("<?xml", 0, false) < 0) {
            xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
        }

    } else {

        ////////////////////////////////////////////////////////////////////////
        // convert file
        ////////////////////////////////////////////////////////////////////////

        TQRegExp qselect("<quickselect>.*</quickselect>");
        TQRegExp docking("<dockingmenu>.*</dockingmenu>");
        TQRegExp station("<station>(.*)</station>");
        TQRegExp stationlist("<stationlist>");
        TQRegExp emptyLines("\\n\\s*\\n");

        #define stationIDElement  "stationID"

        qselect.setMinimal(true);
        docking.setMinimal(true);
        station.setMinimal(true);

        xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
        xmlData.replace(stationlist, "<stationlist>\n\t\t<format>kradio-1.0</format>");
        xmlData.replace(qselect, "");
        xmlData.replace(docking, "");
        xmlData.replace(station, "<FrequencyRadioStation>\n"
                             "\t\t\t<" stationIDElement "></" stationIDElement ">"
                             "\\1</FrequencyRadioStation>"
                    );

        int p = 0;
        int f = 0;
        while ( (f = xmlData.find("<" stationIDElement "></" stationIDElement ">", p) ) >= 0) {
            xmlData.insert(f + 2 + TQString(stationIDElement).length(), createStationID());
        }

        xmlData.replace(emptyLines, "\n");
    }

    presetFile.close();


    ////////////////////////////////////////////////////////////////////////
    // write output
    ////////////////////////////////////////////////////////////////////////

    if (! presetFile.open(IO_WriteOnly)) {
        kdDebug() << "convertFile: "
                  << i18n("error opening preset file")
                  << " " << file << " "
                  << i18n("for writing") << endl;
       return false;
    }

    TQTextStream outs(&presetFile);
    outs.setEncoding(TQTextStream::UnicodeUTF8);

    outs << xmlData;

    if (presetFile.status() != IO_Ok) {
        kdDebug() << "StationList::writeXML: "
                  << i18n("error writing preset file")
                  << " " << file
                  << " (" << presetFile.state() << ")"
                  << endl;
        return false;
    }

    return true;
}


static const char *description = "convert-presets";

static KCmdLineOptions options[] =
{
  { "q", I18N_NOOP("be quiet"), 0},
  { "+[preset files]", I18N_NOOP("preset file to convert"), 0 },
  KCmdLineLastOption
};

int main(int argc, char *argv[])
{
    TDEAboutData aboutData("convert-presets", I18N_NOOP("convert-presets"),
                         VERSION, description, TDEAboutData::License_GPL,
                         "(c) 2003-2005 Martin Witte",
                         0,
                         "http://sourceforge.net/projects/kradio",
                         0);
    aboutData.addAuthor("Martin Witte",  "", "witte@kawo1.rwth-aachen.de");

    TDECmdLineArgs::init( argc, argv, &aboutData );
    TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.

    TDEApplication a (false, false);

    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

    for (int i = 0; i < args->count(); ++i) {
        const char *x = args->arg(i);
        if (! convertFile(x)) {
            return -1;
        } else {
            if (! args->isSet("q"))
                kdDebug() << x << ": ok" << endl;
        }
    }
    if (args->count() == 0) {
        kdDebug() << "no input" << endl;
        return -1;
    }

    return 0;
}