summaryrefslogtreecommitdiffstats
path: root/src/gui/application/StartupTester.cpp
blob: 7bb40a6becd97e0ba4ec8974ca80cdf8f40fde49 (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
241
242
243
244
245
246
/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    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.  See the file
    COPYING included with this distribution for more information.
*/


#include "StartupTester.h"

#include "misc/Debug.h"
#include "gui/dialogs/LilyPondOptionsDialog.h"

#include <kprocess.h>
#include <tqmutex.h>
#include <tqthread.h>
#include <tqregexp.h>


namespace Rosegarden
{

StartupTester::StartupTester() :
    m_ready(false),
    m_haveProjectPackager(false),
    m_haveLilyPondView(false),
    m_haveAudioFileImporter(false)
{
    TQHttp *http = new TQHttp();
    connect(http, TQT_SIGNAL(responseHeaderReceived(const TQHttpResponseHeader &)),
            this, TQT_SLOT(slotHttpResponseHeaderReceived(const TQHttpResponseHeader &)));
    connect(http, TQT_SIGNAL(done(bool)),
            this, TQT_SLOT(slotHttpDone(bool)));
    m_versionHttpFailed = false;
    http->setHost("www.rosegardenmusic.com");
    http->get("/latest-version.txt");
}

StartupTester::~StartupTester()
{
}

void
StartupTester::run()
{
    m_projectPackagerMutex.lock();
    m_lilyPondViewMutex.lock();
    m_audioFileImporterMutex.lock();
    m_ready = true;

    TDEProcess *proc = new TDEProcess();
    m_stdoutBuffer = "";
    TQObject::connect(proc, TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)),
                     this, TQT_SLOT(stdoutReceived(TDEProcess *, char *, int)));
    *proc << "rosegarden-audiofile-importer";
    *proc << "--conftest";
    proc->start(TDEProcess::Block, TDEProcess::All);
    if (!proc->normalExit() || proc->exitStatus()) {
        RG_DEBUG << "StartupTester - No audio file importer available" << endl;
        m_haveAudioFileImporter = false;
        parseStdoutBuffer(m_audioFileImporterMissing);
    } else {
        RG_DEBUG << "StartupTester - Audio file importer OK" << endl;
        m_haveAudioFileImporter = true;
    }
    delete proc;
    m_audioFileImporterMutex.unlock();

    proc = new TDEProcess;
    m_stdoutBuffer = "";
    TQObject::connect(proc, TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)),
                     this, TQT_SLOT(stdoutReceived(TDEProcess *, char *, int)));
    *proc << "rosegarden-project-package";
    *proc << "--conftest";
    proc->start(TDEProcess::Block, TDEProcess::All);
    if (!proc->normalExit() || proc->exitStatus()) {
        m_haveProjectPackager = false;
        // rosegarden-project-package ran but exited with an error code
        RG_DEBUG << "StartupTester - No project packager available" << endl;
        m_haveProjectPackager = false;
        parseStdoutBuffer(m_projectPackagerMissing);
    } else {
        RG_DEBUG << "StartupTester - Project packager OK" << endl;
        m_haveProjectPackager = true;
    }
    delete proc;
    m_projectPackagerMutex.unlock();

    proc = new TDEProcess();
    m_stdoutBuffer = "";
    TQObject::connect(proc, TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)),
                     this, TQT_SLOT(stdoutReceived(TDEProcess *, char *, int)));
    *proc << "rosegarden-lilypondview";
    *proc << "--conftest";
    proc->start(TDEProcess::Block, TDEProcess::All);
    if (!proc->normalExit() || proc->exitStatus()) {
        RG_DEBUG << "StartupTester - No lilypondview available" << endl;
        m_haveLilyPondView = false;
        parseStdoutBuffer(m_lilyPondViewMissing);
    } else {
        RG_DEBUG << "StartupTester - lilypondview OK" << endl;
        m_haveLilyPondView = true;
        TQRegExp re("LilyPond version: ([^\n]*)");
        if (re.search(m_stdoutBuffer) != -1) {
            LilyPondOptionsDialog::setDefaultLilyPondVersion(re.cap(1));
        }
    }
    delete proc;
    m_lilyPondViewMutex.unlock();
}

bool
StartupTester::isReady()
{
    while (!m_ready)
        usleep(10000);
    if (m_projectPackagerMutex.tryLock()) {
        m_projectPackagerMutex.unlock();
    } else {
        return false;
    }
    if (m_lilyPondViewMutex.tryLock()) {
        m_lilyPondViewMutex.unlock();
    } else {
        return false;
    }
    return true;
}

void
StartupTester::stdoutReceived(TDEProcess *, char *buffer, int len)
{
    m_stdoutBuffer += TQString::fromLatin1(buffer, len);
}

void
StartupTester::parseStdoutBuffer(TQStringList &target)
{
    TQRegExp re("Required: ([^\n]*)");
    if (re.search(m_stdoutBuffer) != -1) {
        target = TQStringList::split(", ", re.cap(1));
    }
}

bool
StartupTester::haveProjectPackager(TQStringList *missing)
{
    while (!m_ready)
        usleep(10000);
    TQMutexLocker locker(&m_projectPackagerMutex);
    if (missing) *missing = m_projectPackagerMissing;
    return m_haveProjectPackager;
}

bool
StartupTester::haveLilyPondView(TQStringList *missing)
{
    while (!m_ready)
        usleep(10000);
    TQMutexLocker locker(&m_lilyPondViewMutex);
    if (missing) *missing = m_lilyPondViewMissing;
    return m_haveLilyPondView;
}

bool
StartupTester::haveAudioFileImporter(TQStringList *missing)
{
    while (!m_ready)
        usleep(10000);
    TQMutexLocker locker(&m_audioFileImporterMutex);
    if (missing) *missing = m_audioFileImporterMissing;
    return m_haveAudioFileImporter;
}

bool
StartupTester::isVersionNewerThan(TQString a, TQString b)
{
    TQRegExp re("[._-]");
    TQStringList alist = TQStringList::split(re, a);
    TQStringList blist = TQStringList::split(re, b);
    int ae = alist.size();
    int be = blist.size();
    int e = std::max(ae, be);
    for (int i = 0; i < e; ++i) {
	int an = 0, bn = 0;
	if (i < ae) {
	    an = alist[i].toInt();
	    if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
	}
	if (i < be) {
	    bn = blist[i].toInt();
	    if (bn == 0) bn = -1;
	}
	if (an < bn) return false;
	if (an > bn) return true;
    }
    return false;
}

void
StartupTester::slotHttpResponseHeaderReceived(const TQHttpResponseHeader &h)
{
    if (h.statusCode() / 100 != 2) m_versionHttpFailed = true;
}

void
StartupTester::slotHttpDone(bool error)
{
    TQHttp *http = const_cast<TQHttp *>(dynamic_cast<const TQHttp *>(sender()));
    if (!http) return;
    http->deleteLater();
    if (error) return;

    TQByteArray responseData = http->readAll();
    TQString str = TQString::fromUtf8(responseData.data());
    TQStringList lines = TQStringList::split('\n', str);
    if (lines.empty()) return;

    TQString latestVersion = lines[0];
    std::cerr << "Comparing current version \"" << VERSION
              << "\" with latest version \"" << latestVersion.ascii() << "\""
              << std::endl;
    if (isVersionNewerThan(latestVersion, VERSION)) {
        emit newerVersionAvailable(latestVersion);
    }
}

}

#include "StartupTester.moc"