Refactored all decoder classes to use awaitProcess() from Tool_Abstract base class.
[LameXP.git] / src / Model_FileExts.cpp
blobe9625d1a6855a7ab7bfa6be5c5985791f0915894
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_FileExts.h"
25 //Internal
26 #include "Global.h"
27 #include "Registry_Encoder.h"
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/Sound.h>
33 //Qt
34 #include <QFileInfo>
35 #include <QDir>
36 #include <QFile>
37 #include <QTextCodec>
38 #include <QTextStream>
39 #include <QInputDialog>
41 static inline int LOG10(int x)
43 int ret = 1;
44 while(x >= 10)
46 ret++; x /= 10;
48 return ret;
51 static inline QString EXTENSION(const QString &string)
53 QRegExp regExp("^\\*\\.([A-Za-z0-9]+)$");
54 if(regExp.indexIn(string) >= 0)
56 return regExp.cap(1).trimmed().toLower();
58 return QString();
61 static inline bool VALIDATE(const QString &string)
63 QRegExp regExp("^[A-Za-z0-9]+$");
64 return (regExp.indexIn(string) >= 0);
67 ////////////////////////////////////////////////////////////
68 // Constructor & Destructor
69 ////////////////////////////////////////////////////////////
71 FileExtsModel::FileExtsModel(QObject *const parent )
73 QAbstractItemModel(parent),
74 m_label_1(":/icons/tag_blue.png"),
75 m_label_2(":/icons/tag_red.png")
77 //m_fileExts.append("mp4");
78 //m_replace.insert(m_fileExts.first(), "m4a");
81 FileExtsModel::~FileExtsModel(void)
85 ////////////////////////////////////////////////////////////
86 // Public Functions
87 ////////////////////////////////////////////////////////////
89 int FileExtsModel::columnCount(const QModelIndex &parent) const
91 return 2;
94 int FileExtsModel::rowCount(const QModelIndex &parent) const
96 return m_fileExt.count();
99 QVariant FileExtsModel::data(const QModelIndex &index, int role) const
101 if(((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) && (index.row() < m_fileExt.count()) && (index.row() >= 0))
103 switch(index.column())
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();
113 else if((role == Qt::DecorationRole))
115 switch(index.column())
117 case 0:
118 return m_label_1;
119 case 1:
120 return m_label_2;
121 default:
122 return QVariant();
125 else
127 return QVariant();
131 QVariant FileExtsModel::headerData(int section, Qt::Orientation orientation, int role) const
133 if(role == Qt::DisplayRole)
135 if(orientation == Qt::Horizontal)
137 switch(section)
139 case 0:
140 return QVariant(tr("File Extension"));
141 case 1:
142 return QVariant(tr("Replace With"));
143 default:
144 return QVariant();
147 else
149 return int2str(section + 1);
152 else
154 return QVariant();
158 QModelIndex FileExtsModel::index(int row, int column, const QModelIndex & parent) const
160 return createIndex(row, column, qHash((qint64(row) << 32)| qint64(column)));
163 QModelIndex FileExtsModel::parent(const QModelIndex & index) const
165 return QModelIndex();
168 ////////////////////////////////////////////////////////////
169 // Edit Functions
170 ////////////////////////////////////////////////////////////
172 bool FileExtsModel::addOverwrite(QWidget *const parent)
174 const QStringList allExts = EncoderRegistry::getOutputFileExtensions();
175 QStringList extensions;
176 for(QStringList::ConstIterator iter = allExts.constBegin(); iter != allExts.constEnd(); iter++)
178 if(!m_fileExt.contains((*iter), Qt::CaseInsensitive))
180 extensions << QString("*.%0").arg(*iter);
183 if(extensions.isEmpty())
185 return false;
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);
195 if(dialog.exec() == 0)
197 return false;
200 const QString selectedExt = EXTENSION(dialog.textValue());
201 if(selectedExt.isEmpty())
203 return false;
206 dialog.setComboBoxEditable(true);
207 dialog.setComboBoxItems(QStringList());
208 dialog.setLabelText(tr("Enter the new file extension:"));
210 QString replacement;
211 while(replacement.isEmpty())
213 dialog.setTextValue(QString("*.%0").arg(selectedExt));
214 if(dialog.exec() == 0)
216 return false;
218 replacement = EXTENSION(dialog.textValue());
219 if(!replacement.compare(selectedExt, Qt::CaseInsensitive))
221 replacement.clear();
223 if(replacement.isEmpty())
225 MUtils::Sound::beep(MUtils::Sound::BEEP_ERR);
229 beginResetModel();
230 m_fileExt.append(selectedExt);
231 m_fileExt.sort();
232 m_replace.insert(selectedExt, replacement);
233 endResetModel();
234 return true;
237 bool FileExtsModel::removeOverwrite(const QModelIndex &index)
239 if((index.row() < m_fileExt.count()) && (index.row() >= 0))
241 beginResetModel();
242 m_replace.remove(m_fileExt.at(index.row()));
243 m_fileExt.removeAt(index.row());
244 endResetModel();
245 return true;
247 return false;
250 ////////////////////////////////////////////////////////////
251 // Export and Import
252 ////////////////////////////////////////////////////////////
254 QString FileExtsModel::exportItems(void) const
256 QString exported;
257 for(QStringList::ConstIterator iter = m_fileExt.constBegin(); iter != m_fileExt.constEnd(); iter++)
259 if(m_replace.contains(*iter))
261 if(!exported.isEmpty()) exported.append('|');
262 exported.append(QString("%0>%1").arg(iter->trimmed(), m_replace.value(*iter).trimmed()));
265 return exported;
268 void FileExtsModel::importItems(const QString &data)
270 beginResetModel();
271 m_fileExt.clear();
272 m_replace.clear();
274 const QStringList list = data.split('|', QString::SkipEmptyParts);
275 for(QStringList::ConstIterator iter = list.constBegin(); iter != list.constEnd(); iter++)
277 const QStringList item = iter->trimmed().split('>');
278 if(item.count() >= 2)
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)))
284 m_fileExt.append(fileExt);
285 m_replace.insert(fileExt, replace);
290 m_fileExt.sort();
291 endResetModel();
294 ////////////////////////////////////////////////////////////
295 // Apply Replacement
296 ////////////////////////////////////////////////////////////
298 QString FileExtsModel::apply(const QString &originalExtension) const
300 if((!m_replace.isEmpty()) && m_replace.contains(originalExtension.toLower()))
302 return m_replace.value(originalExtension);
304 return originalExtension;
308 ////////////////////////////////////////////////////////////
309 // Private Functions
310 ////////////////////////////////////////////////////////////
312 QString FileExtsModel::int2str(const int &value) const
314 if(m_fileExt.count() < 10)
316 return QString().sprintf("%d", value);
318 else
320 const QString format = QString().sprintf("%%0%dd", LOG10(m_fileExt.count()));
321 return QString().sprintf(format.toLatin1().constData(), value);