Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
71 andreas 1
/*
2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tsystemsound.h"
20
#include "tvalidatefile.h"
21
#include "tconfig.h"
22
#include "terror.h"
23
#include "tresources.h"
24
 
25
#include <algorithm>
26
 
125 andreas 27
#if __cplusplus < 201402L
28
#   error "This module requires at least C++14 standard!"
29
#else
71 andreas 30
#   if __cplusplus < 201703L
125 andreas 31
#       include <experimental/filesystem>
32
        namespace fs = std::experimental::filesystem;
33
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
71 andreas 34
#   else
125 andreas 35
#       include <filesystem>
36
#       ifdef __ANDROID__
37
            namespace fs = std::__fs::filesystem;
38
#       else
39
            namespace fs = std::filesystem;
40
#       endif
71 andreas 41
#   endif
42
#endif
43
 
44
using std::string;
45
using std::vector;
46
 
204 andreas 47
static std::vector<std::string> mAllSounds;    // Cache
48
 
71 andreas 49
TSystemSound::TSystemSound(const string& path)
50
        : mPath(path)
51
{
52
    DECL_TRACER("TSystemSound::TSystemSound(const string& path)");
53
 
54
    TValidateFile vf;
55
 
56
    if (vf.isValidDir(path))
57
        mValid = true;
58
    else
59
    {
60
        MSG_WARNING("The path " << path << " is invalid!");
61
    }
62
 
63
    mFile = TConfig::getSystemSound();
64
    string p = path + "/" + mFile;
65
 
66
    if (mFile.empty() || !vf.isValidFile(p))
67
    {
68
        MSG_WARNING("No or invalid file (" << p << ")");
69
        mValid = false;
70
    }
71
 
72
    if (mValid)
73
        readAllSystemSounds();
74
}
75
 
76
TSystemSound::~TSystemSound()
77
{
78
    DECL_TRACER("TSystemSound::~TSystemSound()");
79
}
80
 
81
string TSystemSound::getTouchFeedbackSound()
82
{
83
    DECL_TRACER("TSystemSound::getTouchFeedbackSound()");
84
 
85
    if (!mValid)
86
        return string();
87
 
88
    return mPath + "/" + mFile;
89
}
90
 
91
bool TSystemSound::getSystemSoundState()
92
{
93
    DECL_TRACER("TSystemSound::getSystemSoundState()");
94
 
95
    if (!mValid)
96
    {
97
        MSG_ERROR("No or invalid path!");
98
        return false;
99
    }
100
 
101
    return TConfig::getSystemSoundState();
102
}
103
 
104
void TSystemSound::setPath(const string& path)
105
{
106
    DECL_TRACER("TSystemSound::setPath(const string& path)");
107
 
108
    TValidateFile vf;
109
 
110
    if (vf.isValidDir(path))
111
    {
112
        mPath = path;
113
        mValid = true;
114
    }
115
    else
116
    {
117
        MSG_WARNING("Path " << path << " is invalid and was ignored!");
118
    }
119
}
120
 
121
void TSystemSound::setFile(const string& file)
122
{
123
    DECL_TRACER("TSystemSound::setFile(const string& file)");
124
 
125
    if (!mValid)
126
    {
127
        MSG_WARNING("Because of no or an invalid path the file " << file << " will be ignored!");
128
        return;
129
    }
130
 
131
    string p = mPath + "/" + file;
132
    TValidateFile vf;
133
 
134
    if (!vf.isValidFile(p))
135
    {
136
        MSG_WARNING("The file " << file << " doesn't exist!");
137
        return;
138
    }
139
 
140
    mFile = file;
141
}
142
 
143
bool TSystemSound::readAllSystemSounds()
144
{
145
    DECL_TRACER("TSystemSound::readAllSystemSounds()");
146
 
147
    if (!mValid)
148
        return false;
149
 
204 andreas 150
    if (mAllSounds.empty())
71 andreas 151
    {
204 andreas 152
        try
71 andreas 153
        {
204 andreas 154
            for(auto& p: fs::directory_iterator(mPath))
155
            {
156
                string f = fs::path(p.path()).filename();
71 andreas 157
 
204 andreas 158
                if (f.at(0) == '.' || fs::is_directory(p.path()))
159
                    continue;
71 andreas 160
 
204 andreas 161
                if (fs::is_regular_file(p.path()))
162
                {
163
                    MSG_DEBUG("Found sound file " << f);
164
                    mAllSounds.push_back(f);
71 andreas 165
 
204 andreas 166
                    if (startsWith(f, "singleBeep"))
167
                        mSinglePeeps.push_back(f);
168
                    else if (startsWith(f, "doubleBeep"))
169
                        mDoubleBeeps.push_back(f);
170
                    else if (startsWith(f, "audio"))
171
                        mTestSound = f;
172
                    else if (startsWith(f, "docked"))
173
                        mDocked = f;
174
                    else if (startsWith(f, "ringback"))
175
                        mRingBack = f;
176
                    else if (startsWith(f, "ringtone"))
177
                        mRingTone = f;
178
                }
71 andreas 179
            }
180
        }
204 andreas 181
        catch(std::exception& e)
182
        {
183
            MSG_ERROR("Error: " << e.what());
184
            return false;
185
        }
71 andreas 186
    }
204 andreas 187
    else
188
        filterSounds();
71 andreas 189
 
190
    if (mSinglePeeps.size() > 0)
191
        std::sort(mSinglePeeps.begin(), mSinglePeeps.end());
192
 
193
    if (mDoubleBeeps.size() > 0)
194
        std::sort(mDoubleBeeps.begin(), mDoubleBeeps.end());
195
 
196
    return true;
197
}
198
 
204 andreas 199
void TSystemSound::filterSounds()
200
{
201
    DECL_TRACER("TSystemSound::filterSounds()");
202
 
203
    if (mAllSounds.empty())
204
        return;
205
 
206
    vector<string>::iterator iter;
207
 
208
    for (iter = mAllSounds.begin(); iter != mAllSounds.end(); ++iter)
209
    {
210
        string f = *iter;
211
 
212
        if (startsWith(f, "singleBeep"))
213
            mSinglePeeps.push_back(f);
214
        else if (startsWith(f, "doubleBeep"))
215
            mDoubleBeeps.push_back(f);
216
        else if (startsWith(f, "audio"))
217
            mTestSound = f;
218
        else if (startsWith(f, "docked"))
219
            mDocked = f;
220
        else if (startsWith(f, "ringback"))
221
            mRingBack = f;
222
        else if (startsWith(f, "ringtone"))
223
            mRingTone = f;
224
    }
225
}
226
 
71 andreas 227
string TSystemSound::getFirstSingleBeep()
228
{
229
    DECL_TRACER("TSystemSound::getFirstSingleBeep()");
230
    mSinglePos = 0;
231
 
232
    if (mSinglePeeps.size() == 0)
233
        return string();
234
 
235
    mSinglePos++;
236
    return mSinglePeeps.at(0);
237
}
238
 
239
string TSystemSound::getNextSingleBeep()
240
{
241
    DECL_TRACER("TSystemSound::getNextSingleBeep()");
242
 
243
    if (mSinglePeeps.size() >= mSinglePos)
244
        return string();
245
 
246
    size_t old = mSinglePos;
247
    mSinglePos++;
248
    return mSinglePeeps.at(old);
249
}
250
 
251
string TSystemSound::getFirstDoubleBeep()
252
{
253
    DECL_TRACER("TSystemSound::getFirstDoubleBeep()");
254
    mDoublePos = 0;
255
 
256
    if (mDoubleBeeps.size() == 0)
257
        return string();
258
 
259
    mDoublePos++;
260
    return mDoubleBeeps.at(0);
261
}
262
 
263
string TSystemSound::getNextDoubleBeep()
264
{
265
    DECL_TRACER("TSystemSound::getNextDoubleBeep()");
266
 
267
    if (mDoubleBeeps.size() >= mDoublePos)
268
        return string();
269
 
270
    size_t old = mDoublePos;
271
    mDoublePos++;
272
    return mDoubleBeeps.at(old);
273
}