Subversion Repositories tpanel

Rev

Rev 396 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 396 Rev 400
Line 17... Line 17...
17
 */
17
 */
18
 
18
 
19
#include <fstream>
19
#include <fstream>
20
#include <vector>
20
#include <vector>
21
#include <iterator>
21
#include <iterator>
-
 
22
#include <map>
22
#include <unistd.h>
23
#include <unistd.h>
23
#include <sys/stat.h>
24
#include <sys/stat.h>
24
#include <sys/types.h>
25
#include <sys/types.h>
25
#ifdef __ANDROID__
26
#ifdef __ANDROID__
26
#include <QUuid>
27
#include <QUuid>
Line 40... Line 41...
40
#if TARGET_OS_SIMULATOR || TARGET_OS_IOS
41
#if TARGET_OS_SIMULATOR || TARGET_OS_IOS
41
#include <QString>
42
#include <QString>
42
#include "ios/QASettings.h"
43
#include "ios/QASettings.h"
43
#endif
44
#endif
44
#endif
45
#endif
-
 
46
 
-
 
47
#if __cplusplus < 201402L
-
 
48
#   error "This module requires at least C++14 standard!"
-
 
49
#else   // __cplusplus < 201402L
-
 
50
#   if __cplusplus < 201703L
-
 
51
#       include <experimental/filesystem>
-
 
52
namespace fs = std::experimental::filesystem;
-
 
53
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
-
 
54
#   else    // __cplusplus < 201703L
-
 
55
#       include <filesystem>
-
 
56
#       ifdef __ANDROID__
-
 
57
namespace fs = std::__fs::filesystem;
-
 
58
#       else    // __ANDROID__
-
 
59
namespace fs = std::filesystem;
-
 
60
#       endif   // __ANDROID__
-
 
61
#   endif   // __cplusplus < 201703L
-
 
62
#endif  // __cplusplus < 201402L
-
 
63
 
45
using std::string;
64
using std::string;
46
using std::ifstream;
65
using std::ifstream;
47
using std::ofstream;
66
using std::ofstream;
48
using std::fstream;
67
using std::fstream;
49
using std::vector;
68
using std::vector;
-
 
69
using std::map;
-
 
70
using std::pair;
50
using std::cout;
71
using std::cout;
51
using std::cerr;
72
using std::cerr;
52
using std::endl;
73
using std::endl;
53
 
74
 
54
bool TConfig::mInitialized{false};
75
bool TConfig::mInitialized{false};
Line 134... Line 155...
134
};
155
};
135
 
156
 
136
typedef struct SETTINGS settings_t;
157
typedef struct SETTINGS settings_t;
137
static settings_t localSettings;        //!< Global defines settings used in class TConfig.
158
static settings_t localSettings;        //!< Global defines settings used in class TConfig.
138
static settings_t localSettings_temp;   //!< Global defines settings temporary settings
159
static settings_t localSettings_temp;   //!< Global defines settings temporary settings
-
 
160
static map<string, string> userPasswords;
-
 
161
static string fileUserPasswords("user_passwords.txt");
139
 
162
 
140
/**
163
/**
141
 * @brief TConfig::TConfig constructor
164
 * @brief TConfig::TConfig constructor
142
 *
165
 *
143
 * @param path  A path and name of a configuration file.
166
 * @param path  A path and name of a configuration file.
Line 157... Line 180...
157
    mCfgPaths.push_back("/opt/local/usr/etc");
180
    mCfgPaths.push_back("/opt/local/usr/etc");
158
    mCfgPaths.push_back("/opt/local/usr/etc/tpanel");
181
    mCfgPaths.push_back("/opt/local/usr/etc/tpanel");
159
#endif
182
#endif
160
    if (findConfig())
183
    if (findConfig())
161
        readConfig();
184
        readConfig();
-
 
185
 
-
 
186
    readUserPasswords();
162
#else
187
#else
163
    readConfig();
188
    readConfig();
-
 
189
    readUserPasswords();
164
#endif
190
#endif
165
}
191
}
166
 
192
 
167
/**
193
/**
168
 * Simple method to read the configuration again. This is usefull if, for
194
 * Simple method to read the configuration again. This is usefull if, for
Line 725... Line 751...
725
        localSettings.password4 = pw;
751
        localSettings.password4 = pw;
726
 
752
 
727
    mTemporary = false;
753
    mTemporary = false;
728
}
754
}
729
 
755
 
-
 
756
void TConfig::setUserPassword(const string& user, const string& pw)
-
 
757
{
-
 
758
    DECL_TRACER("TConfig::setUserPassword(const string& user, const string& pw)");
-
 
759
 
-
 
760
    userPasswords.insert(pair<string, string>(user, pw));
-
 
761
    writeUserPasswords();
-
 
762
}
-
 
763
 
-
 
764
string TConfig::getUserPassword(const string& user)
-
 
765
{
-
 
766
    map<string, string>::iterator pw = userPasswords.find(user);
-
 
767
 
-
 
768
    if (pw != userPasswords.end())
-
 
769
        return pw->second;
-
 
770
 
-
 
771
    return string();
-
 
772
}
-
 
773
 
-
 
774
void TConfig::clearUserPassword(const string& user)
-
 
775
{
-
 
776
    DECL_TRACER("TConfig::clearUserPassword(const string& user)");
-
 
777
 
-
 
778
    map<string, string>::iterator pw = userPasswords.find(user);
-
 
779
 
-
 
780
    if (pw != userPasswords.end())
-
 
781
    {
-
 
782
        userPasswords.erase(pw);
-
 
783
        writeUserPasswords();
-
 
784
    }
-
 
785
}
-
 
786
 
-
 
787
void TConfig::clearUserPasswords()
-
 
788
{
-
 
789
    DECL_TRACER("TConfig::clearUserPasswords()");
-
 
790
 
-
 
791
    if (!userPasswords.empty())
-
 
792
    {
-
 
793
        userPasswords.clear();
-
 
794
        fs::remove(localSettings.path + "/" + fileUserPasswords);
-
 
795
    }
-
 
796
}
-
 
797
 
-
 
798
void TConfig::readUserPasswords()
-
 
799
{
-
 
800
    DECL_TRACER("TConfig::readUserPasswords()");
-
 
801
 
-
 
802
    string fname = localSettings.path + "/" + fileUserPasswords;
-
 
803
 
-
 
804
    if (!fs::exists(fname))
-
 
805
        return;
-
 
806
 
-
 
807
    ifstream f;
-
 
808
 
-
 
809
    try
-
 
810
    {
-
 
811
        f.open(fname);
-
 
812
 
-
 
813
        for (string line; getline(f, line);)
-
 
814
        {
-
 
815
            size_t pos = line.find_first_of("|");
-
 
816
 
-
 
817
            if (pos == string::npos)
-
 
818
                continue;
-
 
819
 
-
 
820
            string user = line.substr(0, pos);
-
 
821
            string pass = line.substr(pos + 1);
-
 
822
            userPasswords.insert(pair<string, string>(user, pass));
-
 
823
        }
-
 
824
 
-
 
825
        f.close();
-
 
826
    }
-
 
827
    catch (std::exception& e)
-
 
828
    {
-
 
829
        MSG_ERROR("Error opening file " << fname << ": " << e.what());
-
 
830
 
-
 
831
        if (f.is_open())
-
 
832
            f.close();
-
 
833
    }
-
 
834
}
-
 
835
 
-
 
836
void TConfig::writeUserPasswords()
-
 
837
{
-
 
838
    DECL_TRACER("TConfig::writeUserPasswords()");
-
 
839
 
-
 
840
    if (userPasswords.empty())
-
 
841
        return;
-
 
842
 
-
 
843
    string fname = localSettings.path + "/" + fileUserPasswords;
-
 
844
    ofstream f;
-
 
845
 
-
 
846
    try
-
 
847
    {
-
 
848
        map<string, string>::iterator iter;
-
 
849
 
-
 
850
        f.open(fname);
-
 
851
 
-
 
852
        for (iter = userPasswords.begin(); iter != userPasswords.end(); ++iter)
-
 
853
        {
-
 
854
            string line = iter->first + "|" + iter->second + "\n";
-
 
855
            f.write(line.c_str(), line.length());
-
 
856
        }
-
 
857
 
-
 
858
        f.close();
-
 
859
    }
-
 
860
    catch(std::exception& e)
-
 
861
    {
-
 
862
        MSG_ERROR("Error reading file " << fname << ": " << e.what());
-
 
863
 
-
 
864
        if (f.is_open())
-
 
865
            f.close();
-
 
866
    }
-
 
867
}
-
 
868
 
730
void TConfig::saveSystemSoundFile(const std::string& snd)
869
void TConfig::saveSystemSoundFile(const std::string& snd)
731
{
870
{
732
    DECL_TRACER("TConfig::saveSystemSoundFile(const std::string& snd)");
871
    DECL_TRACER("TConfig::saveSystemSoundFile(const std::string& snd)");
733
 
872
 
734
    if (mTemporary)
873
    if (mTemporary)
Line 1884... Line 2023...
1884
        uuid_unparse_lower(uuid, sUUID);
2023
        uuid_unparse_lower(uuid, sUUID);
1885
        localSettings.uuid.assign(sUUID);
2024
        localSettings.uuid.assign(sUUID);
1886
        localSettings_temp = localSettings;
2025
        localSettings_temp = localSettings;
1887
    }
2026
    }
1888
 
2027
 
-
 
2028
    localSettings.password1 = settings.getPassword1();
-
 
2029
    localSettings.password2 = settings.getPassword2();
-
 
2030
    localSettings.password3 = settings.getPassword3();
-
 
2031
    localSettings.password4 = settings.getPassword4();
1889
    mInitialized = true;
2032
    mInitialized = true;
1890
 
2033
 
1891
    if (IS_LOG_DEBUG())
2034
    if (IS_LOG_DEBUG())
1892
    {
2035
    {
1893
        cout << "Selected Parameters:" << endl;
2036
        cout << "Selected Parameters:" << endl;