• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 13633248611

03 Mar 2025 02:31PM UTC coverage: 31.871% (+0.4%) from 31.5%
13633248611

push

github

web-flow
actions: add chrpath to profile

14689 of 46089 relevant lines covered (31.87%)

26426.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.01
/engine/src/qlcfile.cpp
1
/*
2
  Q Light Controller Plus
3
  qlcfile.cpp
4

5
  Copyright (C) Heikki Junnila
6
                Massimo Callegari
7

8
  Licensed under the Apache License, Version 2.0 (the "License");
9
  you may not use this file except in compliance with the License.
10
  You may obtain a copy of the License at
11

12
      http://www.apache.org/licenses/LICENSE-2.0.txt
13

14
  Unless required by applicable law or agreed to in writing, software
15
  distributed under the License is distributed on an "AS IS" BASIS,
16
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
  See the License for the specific language governing permissions and
18
  limitations under the License.
19
*/
20

21
#include <QXmlStreamReader>
22
#include <QXmlStreamWriter>
23
#include <QCoreApplication>
24
#include <QFile>
25

26
#ifdef QT_XML_LIB
27
#   include <QtXml>
28
#else
29
#   include <QDebug>
30
#endif
31

32
#if defined(WIN32) || defined(Q_OS_WIN)
33
#   include <windows.h>
34
#   include <lmcons.h>
35
#else
36
#   include <sys/types.h>
37
#   include <unistd.h>
38
#   include <pwd.h>
39
#endif
40

41
#include "qlcconfig.h"
42
#include "qlcfile.h"
43

44
bool QLCFile::m_hasWindowManager = true;
45

46
QXmlStreamReader *QLCFile::getXMLReader(const QString &path)
234✔
47
{
48
    QXmlStreamReader *reader = NULL;
49

50
    if (path.isEmpty() == true)
234✔
51
    {
52
        qWarning() << Q_FUNC_INFO
2✔
53
                   << "Empty path given. Not attempting to load file.";
1✔
54
        return reader;
1✔
55
    }
56

57
    QFile *file = new QFile(path);
233✔
58
    if (file->open(QIODevice::ReadOnly | QFile::Text) == true)
233✔
59
    {
60
        reader = new QXmlStreamReader(file);
219✔
61
    }
62
    else
63
    {
64
        qWarning() << Q_FUNC_INFO << "Unable to open file:" << path;
14✔
65
    }
66

67
    return reader;
68
}
69

70
void QLCFile::releaseXMLReader(QXmlStreamReader *reader)
173✔
71
{
72
    if (reader == NULL)
173✔
73
        return;
74

75
    if (reader->device() != NULL)
172✔
76
    {
77
        if (reader->device()->isOpen())
172✔
78
            reader->device()->close();
172✔
79
        delete reader->device();
172✔
80
    }
81
    delete reader;
172✔
82
}
83

84
bool QLCFile::writeXMLHeader(QXmlStreamWriter *xml, const QString &content, const QString &author)
3✔
85
{
86
    if (xml == NULL || xml->device() == NULL)
3✔
87
        return false;
×
88

89
    xml->writeStartDocument();
3✔
90
    xml->writeDTD(QString("<!DOCTYPE %1>").arg(content));
3✔
91

92
    xml->writeStartElement(content);
3✔
93
    xml->writeAttribute("xmlns", KXMLQLCplusNamespace + content);
3✔
94

95
    xml->writeStartElement(KXMLQLCCreator);
3✔
96
    xml->writeTextElement(KXMLQLCCreatorName, APPNAME);
3✔
97
    xml->writeTextElement(KXMLQLCCreatorVersion, APPVERSION);
3✔
98
    if (author.isEmpty())
3✔
99
        xml->writeTextElement(KXMLQLCCreatorAuthor, currentUserName());
2✔
100
    else
101
        xml->writeTextElement(KXMLQLCCreatorAuthor, author);
1✔
102
    xml->writeEndElement(); // close KXMLQLCCreator
3✔
103

104
    return true;
3✔
105
}
106

107
QString QLCFile::errorString(QFile::FileError error)
24✔
108
{
109
    switch (error)
24✔
110
    {
111
    case QFile::NoError:
1✔
112
        return QObject::tr("No error occurred.");
113
    case QFile::ReadError:
9✔
114
        return QObject::tr("An error occurred when reading from the file.");
115
    case QFile::WriteError:
1✔
116
        return QObject::tr("An error occurred when writing to the file.");
117
    case QFile::FatalError:
1✔
118
        return QObject::tr("A fatal error occurred.");
119
    case QFile::ResourceError:
1✔
120
        return QObject::tr("Resource error occurred.");
121
    case QFile::OpenError:
1✔
122
        return QObject::tr("The file could not be opened.");
123
    case QFile::AbortError:
1✔
124
        return QObject::tr("The operation was aborted.");
125
    case QFile::TimeOutError:
1✔
126
        return QObject::tr("A timeout occurred.");
127
    case QFile::UnspecifiedError:
1✔
128
        return QObject::tr("An unspecified error occurred.");
129
    case QFile::RemoveError:
2✔
130
        return QObject::tr("The file could not be removed.");
131
    case QFile::RenameError:
1✔
132
        return QObject::tr("The file could not be renamed.");
133
    case QFile::PositionError:
1✔
134
        return QObject::tr("The position in the file could not be changed.");
135
    case QFile::ResizeError:
1✔
136
        return QObject::tr("The file could not be resized.");
137
    case QFile::PermissionsError:
1✔
138
        return QObject::tr("The file could not be accessed.");
139
    case QFile::CopyError:
1✔
140
        return QObject::tr("The file could not be copied.");
141
    default:
1✔
142
        return QObject::tr("An unknown error occurred.");
143
    }
144
}
145

146
QString QLCFile::currentUserName()
2✔
147
{
148
#if defined(WIN32) || defined(Q_OS_WIN)
149
    DWORD length = UNLEN + 1;
150
    TCHAR name[length];
151
    if (GetUserName(name, &length))
152
        return QString::fromUtf16(reinterpret_cast<char16_t*>(name));
153
    else
154
        return QString("Unknown windows user");
155
#else
156
 #if defined(Q_OS_ANDROID)
157
    return QString(getenv("USER"));
158
 #else
159
    QString name;
160
    struct passwd* passwd = getpwuid(getuid());
2✔
161
    if (passwd == NULL)
2✔
162
        name.append(getenv("USER"));
×
163
    else
164
        name.append(passwd->pw_gecos);
2✔
165
    name.remove(",,,");
2✔
166
    return name;
2✔
167
 #endif
168
#endif
169
}
×
170

171
void QLCFile::setHasWindowManager(bool enable)
1✔
172
{
173
    m_hasWindowManager = enable;
1✔
174
}
1✔
175

176
bool QLCFile::hasWindowManager()
2✔
177
{
178
    return m_hasWindowManager;
2✔
179
}
180

181
QDir QLCFile::systemDirectory(QString path, QString extension)
94✔
182
{
183
    QDir dir;
94✔
184
#if defined(Q_OS_IOS)
185
    dir.setPath(QString("%1/%2").arg(QCoreApplication::applicationDirPath())
186
                                   .arg(path));
187
#elif defined(__APPLE__) || defined(Q_OS_MAC)
188
    dir.setPath(QString("%1/../%2").arg(QCoreApplication::applicationDirPath())
189
                                   .arg(path));
190
#elif defined(WIN32) || defined(Q_OS_WIN)
191
    dir.setPath(QString("%1%2%3").arg(QCoreApplication::applicationDirPath())
192
                                 .arg(QDir::separator())
193
                                 .arg(path));
194
#elif defined(Q_OS_ANDROID)
195
    dir.setPath(QString("assets:/%1").arg(path.remove(0, path.lastIndexOf("/") + 1)));
196
#else
197
    dir.setPath(path);
94✔
198
#endif
199

200
    dir.setFilter(QDir::Files);
94✔
201
    if (!extension.isEmpty())
94✔
202
        dir.setNameFilters(QStringList() << QString("*%1").arg(extension));
6✔
203

204
    return dir;
94✔
205
}
×
206

207
QDir QLCFile::userDirectory(QString path, QString fallBackPath, QStringList extensions)
6✔
208
{
209
    Q_UNUSED(fallBackPath)
210
    QDir dir;
6✔
211

212
#if defined(Q_WS_X11) || defined(Q_OS_LINUX)
213
    // If the current user is root, return the system fixture dir.
214
    // Otherwise return a path under user's home dir.
215
    if (geteuid() == 0 && QLCFile::hasWindowManager())
6✔
216
        dir = QDir(fallBackPath);
×
217
    else
218
        dir.setPath(QString("%1/%2").arg(getenv("HOME")).arg(path));
6✔
219
#elif defined(__APPLE__) || defined (Q_OS_MAC)
220
    /* User's input profile directory on OSX */
221
    dir.setPath(QString("%1/%2").arg(getenv("HOME")).arg(path));
222
#else
223
    /* User's input profile directory on Windows */
224
    LPTSTR home = (LPTSTR) malloc(256 * sizeof(TCHAR));
225
    GetEnvironmentVariable(TEXT("UserProfile"), home, 256);
226
    dir.setPath(QString("%1/%2")
227
                    .arg(QString::fromUtf16(reinterpret_cast<char16_t*>(home)))
228
                    .arg(path));
229
    free(home);
230
#endif
231

232
    // Ensure the directory exists
233
    if (dir.exists() == false)
6✔
234
        dir.mkpath(".");
3✔
235

236
    dir.setFilter(QDir::Files);
6✔
237
    dir.setNameFilters(extensions);
6✔
238

239
    return dir;
6✔
240
}
×
241

242
QString QLCFile::fileUrlPrefix()
×
243
{
244
#if defined(WIN32) || defined(Q_OS_WIN)
245
    return QString("file:///");
246
#else
247
    return QString("file://");
×
248
#endif
249
}
250

251
quint32 QLCFile::getQtRuntimeVersion()
1✔
252
{
253
    QString ver(qVersion());
1✔
254
    if (ver.isEmpty())
1✔
255
        return 0;
256

257
    QStringList digits = ver.split(".");
2✔
258

259
    return digits.at(0).toInt() * 10000 + digits.at(1).toInt() * 100 + digits.at(2).toInt();
1✔
260
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc