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