OSDN Git Service

Updated CA certificates file for cURL.
[lamexp/LameXP.git] / src / Model_FileExts.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2022 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Model_FileExts.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Registry_Encoder.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/Sound.h>
32
33 //Qt
34 #include <QFileInfo>
35 #include <QDir>
36 #include <QFile>
37 #include <QTextCodec>
38 #include <QTextStream>
39 #include <QInputDialog>
40
41 static inline int LOG10(int x)
42 {
43         int ret = 1;
44         while(x >= 10)
45         {
46                 ret++; x /= 10;
47         }
48         return ret;
49 }
50
51 static inline QString EXTENSION(const QString &string)
52 {
53         QRegExp regExp("^\\*\\.([A-Za-z0-9]+)$");
54         if(regExp.indexIn(string) >= 0)
55         {
56                 return regExp.cap(1).trimmed().toLower();
57         }
58         return QString();
59 }
60
61 static inline bool VALIDATE(const QString &string)
62 {
63         QRegExp regExp("^[A-Za-z0-9]+$");
64         return (regExp.indexIn(string) >= 0);
65 }
66
67 ////////////////////////////////////////////////////////////
68 // Constructor & Destructor
69 ////////////////////////////////////////////////////////////
70
71 FileExtsModel::FileExtsModel(QObject *const parent )
72 :
73         QAbstractItemModel(parent),
74         m_label_1(":/icons/tag_blue.png"),
75         m_label_2(":/icons/tag_red.png")
76 {
77         //m_fileExts.append("mp4");
78         //m_replace.insert(m_fileExts.first(), "m4a");
79 }
80
81 FileExtsModel::~FileExtsModel(void)
82 {
83 }
84
85 ////////////////////////////////////////////////////////////
86 // Public Functions
87 ////////////////////////////////////////////////////////////
88
89 int FileExtsModel::columnCount(const QModelIndex& /*parent*/) const
90 {
91         return 2;
92 }
93
94 int FileExtsModel::rowCount(const QModelIndex& /*parent*/) const
95 {
96         return m_fileExt.count();
97 }
98
99 QVariant FileExtsModel::data(const QModelIndex &index, int role) const
100 {
101         if(((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) && (index.row() < m_fileExt.count()) && (index.row() >= 0))
102         {
103                 switch(index.column())
104                 {
105                 case 0:
106                         return QString("*.%0").arg(m_fileExt.at(index.row()));
107                 case 1:
108                         return QString("*.%0").arg(m_replace.value(m_fileExt.at(index.row())));
109                 default:
110                         return QVariant();
111                 }               
112         }
113         else if((role == Qt::DecorationRole))
114         {
115                 switch(index.column())
116                 {
117                 case 0:
118                         return m_label_1;
119                 case 1:
120                         return m_label_2;
121                 default:
122                         return QVariant();
123                 }
124         }
125         else
126         {
127                 return QVariant();
128         }
129 }
130
131 QVariant FileExtsModel::headerData(int section, Qt::Orientation orientation, int role) const
132 {
133         if(role == Qt::DisplayRole)
134         {
135                 if(orientation == Qt::Horizontal)
136                 {
137                         switch(section)
138                         {
139                         case 0:
140                                 return QVariant(tr("File Extension"));
141                         case 1:
142                                 return QVariant(tr("Replace With"));
143                         default:
144                                 return QVariant();
145                         }
146                 }
147                 else
148                 {
149                         return int2str(section + 1);
150                 }
151         }
152         else
153         {
154                 return QVariant();
155         }
156 }
157
158 QModelIndex FileExtsModel::index(int row, int column, const QModelIndex& /*parent*/) const
159 {
160         return createIndex(row, column, qHash((qint64(row) << 32)| qint64(column)));
161 }
162
163 QModelIndex  FileExtsModel::parent(const QModelIndex& /*index*/) const
164 {
165         return QModelIndex();
166 }
167
168 ////////////////////////////////////////////////////////////
169 // Edit Functions
170 ////////////////////////////////////////////////////////////
171
172 bool FileExtsModel::addOverwrite(QWidget *const parent)
173 {
174         const QStringList allExts = EncoderRegistry::getOutputFileExtensions();
175         QStringList extensions;
176         for(QStringList::ConstIterator iter = allExts.constBegin(); iter != allExts.constEnd(); iter++)
177         {
178                 if(!m_fileExt.contains((*iter), Qt::CaseInsensitive))
179                 {
180                         extensions << QString("*.%0").arg(*iter);
181                 }
182         }
183         if(extensions.isEmpty())
184         {
185                 return false;
186         }
187
188         QInputDialog dialog(parent);
189         dialog.setLabelText(tr("Select file extensions to overwrite:"));
190         dialog.setInputMode(QInputDialog::TextInput);
191         dialog.setTextEchoMode(QLineEdit::Normal);
192         dialog.setComboBoxEditable(false);
193         dialog.setComboBoxItems(extensions);
194         
195         if(dialog.exec() == 0)
196         {
197                 return false;
198         }
199
200         const QString selectedExt = EXTENSION(dialog.textValue());
201         if(selectedExt.isEmpty())
202         {
203                 return false;
204         }
205
206         dialog.setComboBoxEditable(true);
207         dialog.setComboBoxItems(QStringList());
208         dialog.setLabelText(tr("Enter the new file extension:"));
209
210         QString replacement;
211         while(replacement.isEmpty())
212         {
213                 dialog.setTextValue(QString("*.%0").arg(selectedExt));
214                 if(dialog.exec() == 0)
215                 {
216                         return false;
217                 }
218                 replacement = EXTENSION(dialog.textValue());
219                 if(!replacement.compare(selectedExt, Qt::CaseInsensitive))
220                 {
221                         replacement.clear();
222                 }
223                 if(replacement.isEmpty())
224                 {
225                         MUtils::Sound::beep(MUtils::Sound::BEEP_ERR);
226                 }
227         }
228
229         beginResetModel();
230         m_fileExt.append(selectedExt);
231         m_fileExt.sort();
232         m_replace.insert(selectedExt, replacement);
233         endResetModel();
234         return true;
235 }
236
237 bool FileExtsModel::removeOverwrite(const QModelIndex &index)
238 {
239         if((index.row() < m_fileExt.count()) && (index.row() >= 0))
240         {
241                 beginResetModel();
242                 m_replace.remove(m_fileExt.at(index.row()));
243                 m_fileExt.removeAt(index.row());
244                 endResetModel();
245                 return true;
246         }
247         return false;
248 }
249
250 ////////////////////////////////////////////////////////////
251 // Export and Import
252 ////////////////////////////////////////////////////////////
253
254 QString FileExtsModel::exportItems(void) const
255 {
256         QString exported;
257         for(QStringList::ConstIterator iter = m_fileExt.constBegin(); iter != m_fileExt.constEnd(); iter++)
258         {
259                 if(m_replace.contains(*iter))
260                 {
261                         if(!exported.isEmpty()) exported.append('|');
262                         exported.append(QString("%0>%1").arg(iter->trimmed(), m_replace.value(*iter).trimmed()));
263                 }
264         }
265         return exported;
266 }
267
268 void FileExtsModel::importItems(const QString &data)
269 {
270         beginResetModel();
271         m_fileExt.clear();
272         m_replace.clear();
273
274         const QStringList list = data.split('|', QString::SkipEmptyParts);
275         for(QStringList::ConstIterator iter = list.constBegin(); iter != list.constEnd(); iter++)
276         {
277                 const QStringList item = iter->trimmed().split('>');
278                 if(item.count() >= 2)
279                 {
280                         const QString fileExt = item.at(0).simplified().toLower();
281                         const QString replace = item.at(1).simplified().toLower();
282                         if(VALIDATE(fileExt) && VALIDATE(replace) && (!m_fileExt.contains(fileExt)))
283                         {
284                                 m_fileExt.append(fileExt);
285                                 m_replace.insert(fileExt, replace);
286                         }
287                 }
288         }
289
290         m_fileExt.sort();
291         endResetModel();
292 }
293
294 ////////////////////////////////////////////////////////////
295 // Apply Replacement
296 ////////////////////////////////////////////////////////////
297
298 QString FileExtsModel::apply(const QString &originalExtension) const
299 {
300         if((!m_replace.isEmpty()) && m_replace.contains(originalExtension.toLower()))
301         {
302                 return m_replace.value(originalExtension);
303         }
304         return originalExtension;
305 }
306
307
308 ////////////////////////////////////////////////////////////
309 // Private Functions
310 ////////////////////////////////////////////////////////////
311
312 QString FileExtsModel::int2str(const int &value) const
313 {
314         if(m_fileExt.count() < 10)
315         {
316                 return QString().sprintf("%d", value);
317         }
318         else
319         {
320                 const QString format = QString().sprintf("%%0%dd", LOG10(m_fileExt.count()));
321                 return QString().sprintf(format.toLatin1().constData(), value);
322         }
323 }