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
 
27
#if __GNUC__ < 9 && !defined(__ANDROID__)
28
#   if __cplusplus < 201703L
29
#       warning "Your C++ compiler seems to have no support for C++17 standard!"
30
#   endif
31
#   include <experimental/filesystem>
32
namespace fs = std::experimental::filesystem;
33
#else
34
#   include <filesystem>
35
#   ifdef __ANDROID__
36
namespace fs = std::__fs::filesystem;
37
#   else
38
namespace fs = std::filesystem;
39
#   endif
40
#endif
41
 
42
using std::string;
43
using std::vector;
44
 
45
TSystemSound::TSystemSound(const string& path)
46
        : mPath(path)
47
{
48
    DECL_TRACER("TSystemSound::TSystemSound(const string& path)");
49
 
50
    TValidateFile vf;
51
 
52
    if (vf.isValidDir(path))
53
        mValid = true;
54
    else
55
    {
56
        MSG_WARNING("The path " << path << " is invalid!");
57
    }
58
 
59
    mFile = TConfig::getSystemSound();
60
    string p = path + "/" + mFile;
61
 
62
    if (mFile.empty() || !vf.isValidFile(p))
63
    {
64
        MSG_WARNING("No or invalid file (" << p << ")");
65
        mValid = false;
66
    }
67
 
68
    if (mValid)
69
        readAllSystemSounds();
70
}
71
 
72
TSystemSound::~TSystemSound()
73
{
74
    DECL_TRACER("TSystemSound::~TSystemSound()");
75
}
76
 
77
string TSystemSound::getTouchFeedbackSound()
78
{
79
    DECL_TRACER("TSystemSound::getTouchFeedbackSound()");
80
 
81
    if (!mValid)
82
        return string();
83
 
84
    return mPath + "/" + mFile;
85
}
86
 
87
bool TSystemSound::getSystemSoundState()
88
{
89
    DECL_TRACER("TSystemSound::getSystemSoundState()");
90
 
91
    if (!mValid)
92
    {
93
        MSG_ERROR("No or invalid path!");
94
        return false;
95
    }
96
 
97
    return TConfig::getSystemSoundState();
98
}
99
 
100
void TSystemSound::setPath(const string& path)
101
{
102
    DECL_TRACER("TSystemSound::setPath(const string& path)");
103
 
104
    TValidateFile vf;
105
 
106
    if (vf.isValidDir(path))
107
    {
108
        mPath = path;
109
        mValid = true;
110
    }
111
    else
112
    {
113
        MSG_WARNING("Path " << path << " is invalid and was ignored!");
114
    }
115
}
116
 
117
void TSystemSound::setFile(const string& file)
118
{
119
    DECL_TRACER("TSystemSound::setFile(const string& file)");
120
 
121
    if (!mValid)
122
    {
123
        MSG_WARNING("Because of no or an invalid path the file " << file << " will be ignored!");
124
        return;
125
    }
126
 
127
    string p = mPath + "/" + file;
128
    TValidateFile vf;
129
 
130
    if (!vf.isValidFile(p))
131
    {
132
        MSG_WARNING("The file " << file << " doesn't exist!");
133
        return;
134
    }
135
 
136
    mFile = file;
137
}
138
 
139
bool TSystemSound::readAllSystemSounds()
140
{
141
    DECL_TRACER("TSystemSound::readAllSystemSounds()");
142
 
143
    if (!mValid)
144
        return false;
145
 
146
    try
147
    {
148
        for(auto& p: fs::directory_iterator(mPath))
149
        {
150
            string f = fs::path(p.path()).filename();
151
 
152
            if (f.at(0) == '.' || fs::is_directory(p.path()))
153
                continue;
154
 
155
            if (fs::is_regular_file(p.path()))
156
            {
157
                MSG_DEBUG("Found sound file " << f);
158
 
159
                if (startsWith(f, "singleBeep"))
160
                    mSinglePeeps.push_back(f);
161
                else if (startsWith(f, "doubleBeep"))
162
                    mDoubleBeeps.push_back(f);
163
                else if (startsWith(f, "audio"))
164
                    mTestSound = f;
165
                else if (startsWith(f, "docked"))
166
                    mDocked = f;
167
                else if (startsWith(f, "ringback"))
168
                    mRingBack = f;
169
                else if (startsWith(f, "ringtone"))
170
                    mRingTone = f;
171
            }
172
        }
173
    }
174
    catch(std::exception& e)
175
    {
176
        MSG_ERROR("Error: " << e.what());
177
        return false;
178
    }
179
 
180
    if (mSinglePeeps.size() > 0)
181
        std::sort(mSinglePeeps.begin(), mSinglePeeps.end());
182
 
183
    if (mDoubleBeeps.size() > 0)
184
        std::sort(mDoubleBeeps.begin(), mDoubleBeeps.end());
185
 
186
    return true;
187
}
188
 
189
string TSystemSound::getFirstSingleBeep()
190
{
191
    DECL_TRACER("TSystemSound::getFirstSingleBeep()");
192
    mSinglePos = 0;
193
 
194
    if (mSinglePeeps.size() == 0)
195
        return string();
196
 
197
    mSinglePos++;
198
    return mSinglePeeps.at(0);
199
}
200
 
201
string TSystemSound::getNextSingleBeep()
202
{
203
    DECL_TRACER("TSystemSound::getNextSingleBeep()");
204
 
205
    if (mSinglePeeps.size() >= mSinglePos)
206
        return string();
207
 
208
    size_t old = mSinglePos;
209
    mSinglePos++;
210
    return mSinglePeeps.at(old);
211
}
212
 
213
string TSystemSound::getFirstDoubleBeep()
214
{
215
    DECL_TRACER("TSystemSound::getFirstDoubleBeep()");
216
    mDoublePos = 0;
217
 
218
    if (mDoubleBeeps.size() == 0)
219
        return string();
220
 
221
    mDoublePos++;
222
    return mDoubleBeeps.at(0);
223
}
224
 
225
string TSystemSound::getNextDoubleBeep()
226
{
227
    DECL_TRACER("TSystemSound::getNextDoubleBeep()");
228
 
229
    if (mDoubleBeeps.size() >= mDoublePos)
230
        return string();
231
 
232
    size_t old = mDoublePos;
233
    mDoublePos++;
234
    return mDoubleBeeps.at(old);
235
}