1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
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, see <https://www.gnu.org/licenses/>.
16 */
17#include "systemconsts.h"
18#include "utils.h"
19#include <QObject>
20#include <QSet>
21#include <QString>
22#include <QStringList>
23#include <QTextCodec>
24
25SystemConsts* pSystemConsts;
26
27SystemConsts::SystemConsts(): mDefaultFileFilters()
28{
29 addDefaultFileFilter(QObject::tr("All files"),"*");
30 addDefaultFileFilter(QObject::tr("Dev C++ Project files"),"*.dev");
31 addDefaultFileFilter(QObject::tr("C files"),"*.c");
32 addDefaultFileFilter(QObject::tr("C++ files"),"*.cpp *.cc *.cxx");
33 addDefaultFileFilter(QObject::tr("Header files"),"*.h *.hh");
34
35 addFileFilter(mIconFileFilters, QObject::tr("Icon files"), "*.ico");
36
37 mCodecNames.append(ENCODING_AUTO_DETECT);
38 mCodecNames.append(ENCODING_SYSTEM_DEFAULT);
39 mCodecNames.append(ENCODING_UTF8);
40 mCodecNames.append(ENCODING_UTF8_BOM);
41 QStringList codecNames;
42 QSet<QByteArray> codecAlias;
43 codecAlias.insert("system");
44 codecAlias.insert("utf-8");
45
46 foreach (const QByteArray& name, QTextCodec::availableCodecs()){
47 QByteArray lname = name.toLower();
48 if (lname.startsWith("cp"))
49 continue;
50 if (codecAlias.contains(lname))
51 continue;
52 codecNames.append(lname);
53 codecAlias.insert(lname);
54 QTextCodec* codec = QTextCodec::codecForName(name);
55 if (codec) {
56 foreach (const QByteArray& alias, codec->aliases()) {
57 codecAlias.insert(alias.toLower());
58 }
59 }
60 }
61 std::sort(codecNames.begin(),codecNames.end());
62 mCodecNames.append(codecNames);
63
64 mDefaultFileNameFilters.append("*.c");
65 mDefaultFileNameFilters.append("*.cpp");
66 mDefaultFileNameFilters.append("*.cc");
67 mDefaultFileNameFilters.append("*.C");
68 mDefaultFileNameFilters.append("*.cxx");
69 mDefaultFileNameFilters.append("*.cxx");
70 mDefaultFileNameFilters.append("*.h");
71 mDefaultFileNameFilters.append("*.hpp");
72 mDefaultFileNameFilters.append("*.hxx");
73 mDefaultFileNameFilters.append(".gitignore");
74 mDefaultFileNameFilters.append("*.vs");
75 mDefaultFileNameFilters.append("*.fs");
76 mDefaultFileNameFilters.append("*.txt");
77 mDefaultFileNameFilters.append("*.in");
78 mDefaultFileNameFilters.append("*.out");
79 mDefaultFileNameFilters.append("*.dat");
80 mDefaultFileNameFilters.append("*.md");
81 mDefaultFileNameFilters.append("*.dev");
82}
83
84const QStringList &SystemConsts::defaultFileFilters() const noexcept
85{
86 return mDefaultFileFilters;
87}
88
89const QString &SystemConsts::defaultCFileFilter() const noexcept
90{
91 return mDefaultFileFilters[2];
92}
93
94const QString &SystemConsts::defaultCPPFileFilter() const noexcept
95{
96 return mDefaultFileFilters[3];
97}
98
99const QString &SystemConsts::defaultAllFileFilter() const noexcept
100{
101 return mDefaultFileFilters[0];
102}
103
104void SystemConsts::addDefaultFileFilter(const QString &name, const QString &fileExtensions)
105{
106 addFileFilter(mDefaultFileFilters,name,fileExtensions);
107}
108
109void SystemConsts::addFileFilter(QStringList& filters, const QString &name, const QString &fileExtensions)
110{
111 filters.append(name+ " (" + fileExtensions+")");
112}
113
114const QStringList &SystemConsts::defaultFileNameFilters() const
115{
116 return mDefaultFileNameFilters;
117}
118
119const QStringList &SystemConsts::codecNames() const
120{
121 return mCodecNames;
122}
123
124const QStringList &SystemConsts::iconFileFilters() const
125{
126 return mIconFileFilters;
127}
128
129const QString &SystemConsts::iconFileFilter() const
130{
131 return mIconFileFilters[0];
132}
133
134void SystemConsts::setIconFileFilters(const QStringList &newIconFileFilters)
135{
136 mIconFileFilters = newIconFileFilters;
137}
138