This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0a5fc5
commit 2671711
Showing
13 changed files
with
678 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# C++ objects and libs | ||
*.slo | ||
*.lo | ||
*.o | ||
*.a | ||
*.la | ||
*.lai | ||
*.so | ||
*.dll | ||
*.dylib | ||
|
||
# Qt-es | ||
object_script.*.Release | ||
object_script.*.Debug | ||
*_plugin_import.cpp | ||
/.qmake.cache | ||
/.qmake.stash | ||
*.pro.user | ||
*.pro.user.* | ||
*.qbs.user | ||
*.qbs.user.* | ||
*.moc | ||
moc_*.cpp | ||
moc_*.h | ||
qrc_*.cpp | ||
ui_*.h | ||
*.qmlc | ||
*.jsc | ||
Makefile* | ||
*build-* | ||
|
||
# Qt unit tests | ||
target_wrapper.* | ||
|
||
# QtCreator | ||
*.autosave | ||
|
||
# QtCreator Qml | ||
*.qmlproject.user | ||
*.qmlproject.user.* | ||
|
||
# QtCreator CMake | ||
CMakeLists.txt.user* | ||
|
||
bom-fixer.pro.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Denis Medved <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
BOM Fixer | ||
======= | ||
This program drops BOM header from all files | ||
|
||
Build: | ||
|
||
$ qmake | ||
$ make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>About</class> | ||
<widget class="QWidget" name="About"> | ||
<property name="enabled"> | ||
<bool>true</bool> | ||
</property> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>388</width> | ||
<height>158</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Form</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout_2"> | ||
<item> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QLabel" name="license"> | ||
<property name="text"> | ||
<string>License: <a href="https://www.gnu.org/licenses/gpl-3.0.txt">GPLv3 or later</a> </string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLabel" name="contact"> | ||
<property name="text"> | ||
<string><html><head/><body><p>Contact: Denis Medved &lt;<a href="mailto:[email protected]"><span style=" text-decoration: underline; color:#2980b9;">[email protected]</span></a>&gt; </p></body></html></string> | ||
</property> | ||
<property name="textFormat"> | ||
<enum>Qt::RichText</enum> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QLabel" name="source"> | ||
<property name="text"> | ||
<string>Source: <a href="https://github.com/DenisMedved/bom-fixer">https://github.com/DenisMedved/bom-fixer</a></string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="close"> | ||
<property name="text"> | ||
<string>Close</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections> | ||
<connection> | ||
<sender>close</sender> | ||
<signal>clicked()</signal> | ||
<receiver>About</receiver> | ||
<slot>close()</slot> | ||
<hints> | ||
<hint type="sourcelabel"> | ||
<x>84</x> | ||
<y>92</y> | ||
</hint> | ||
<hint type="destinationlabel"> | ||
<x>84</x> | ||
<y>57</y> | ||
</hint> | ||
</hints> | ||
</connection> | ||
</connections> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
QT += core gui widgets | ||
|
||
TARGET = bom-fixer | ||
TEMPLATE = app | ||
|
||
SOURCES += \ | ||
main.cpp \ | ||
mainwindow.cpp \ | ||
bomfilesystemmodel.cpp | ||
|
||
HEADERS += \ | ||
mainwindow.h \ | ||
bomfilesystemmodel.h | ||
|
||
FORMS += \ | ||
mainwindow.ui \ | ||
about.ui | ||
|
||
DISTFILES += \ | ||
LICENSE \ | ||
README.md \ | ||
AUTHORS | ||
|
||
CONFIG += static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "bomfilesystemmodel.h" | ||
#include <iostream> | ||
|
||
BOMFileSystemModel::BOMFileSystemModel(QObject *parent/* = Q_NULLPTR */) : | ||
QFileSystemModel(parent) | ||
{ | ||
|
||
} | ||
|
||
bool BOMFileSystemModel::hasBOM(QFileInfo& fileInfo) const | ||
{ | ||
QFile file(fileInfo.filePath()); | ||
|
||
if (!file.open(QIODevice::ReadOnly)) { | ||
return false; | ||
} | ||
|
||
QByteArray head = file.read(3); | ||
|
||
if (head.length() < 3) { | ||
return false; | ||
} | ||
|
||
return head.at(0) == '\xEF' | ||
&& head.at(1) == '\xBB' | ||
&& head.at(2) == '\xBF'; | ||
} | ||
|
||
void BOMFileSystemModel::fixBOM(QString path) const | ||
{ | ||
QFile file(path); | ||
if (!file.open(QIODevice::ReadWrite)) | ||
return ; | ||
|
||
QByteArray content = file.readAll(); | ||
content.remove(0,3); //drop 3 bytes from the begin | ||
file.resize(0); | ||
file.write(content); | ||
file.close(); | ||
} | ||
|
||
void BOMFileSystemModel::slotFixBOM() | ||
{ | ||
QSet<QString>::iterator it = m_bomPaths.begin(); | ||
while (it != m_bomPaths.end()) { | ||
fixBOM(*it); | ||
++it; | ||
} | ||
|
||
populateDirectory(rootDirectory()); | ||
|
||
emit fixed(); | ||
} | ||
|
||
void BOMFileSystemModel::populateDirectory(const QDir& dir) | ||
{ | ||
setRootPath(dir.path()); | ||
|
||
m_bomPaths.clear(); | ||
QDirIterator it(rootDirectory(), QDirIterator::Subdirectories); | ||
while (it.hasNext()) { | ||
it.next(); | ||
QFileInfo info = it.fileInfo(); | ||
|
||
if (!info.path().contains("/.") //ignore hidden dirs and subdirs | ||
&& info.fileName().at(0) != '.' //ignore hidden files | ||
&& info.isFile() | ||
&& !m_bomPaths.contains(info.filePath()) //check if already handled | ||
&& hasBOM(info)) | ||
|
||
m_bomPaths.insert(info.filePath()); | ||
} | ||
|
||
emit dataChanged(QModelIndex(), QModelIndex()); | ||
} | ||
|
||
QVariant BOMFileSystemModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const | ||
{ | ||
if (role == Qt::TextColorRole){ | ||
if (m_bomPaths.contains(filePath(index))) | ||
return QColor(Qt::red); | ||
} | ||
|
||
return QFileSystemModel::data(index, role); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef BOMFILESYSTEMMODEL_H | ||
#define BOMFILESYSTEMMODEL_H | ||
|
||
#include <memory> | ||
#include <QFileSystemModel> | ||
#include <QDir> | ||
#include <QVariant> | ||
#include <QModelIndex> | ||
#include <QSet> | ||
|
||
class BOMFileSystemModel : public QFileSystemModel | ||
{ | ||
Q_OBJECT | ||
|
||
private: | ||
bool hasBOM(QFileInfo& fileInfo) const; | ||
void fixBOM(QString path) const; | ||
QSet<QString> m_bomPaths; | ||
|
||
public: | ||
explicit BOMFileSystemModel(QObject *parent = 0); | ||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | ||
void populateDirectory(const QDir& dir); | ||
|
||
public slots: | ||
void slotFixBOM(); | ||
signals: | ||
void fixed(); | ||
}; | ||
|
||
#endif // BOMFILESYSTEMMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "mainwindow.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
MainWindow w; | ||
w.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
#include <QFileDialog> | ||
#include <QMessageBox> | ||
|
||
#include "mainwindow.h" | ||
#include "ui_mainwindow.h" | ||
#include "ui_about.h" | ||
|
||
MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | ||
m_pUi(std::make_shared<Ui::MainWindow>()), | ||
m_pFsModel(std::make_shared<BOMFileSystemModel>()), | ||
m_pUiAbout(std::make_shared<Ui::About>()) | ||
{ | ||
m_pUi->setupUi(this); | ||
m_pUiAbout->setupUi(&m_about); | ||
m_about.hide(); | ||
m_pUi->files->setModel(m_pFsModel.get()); | ||
|
||
connect(m_pUi->chooseDir, SIGNAL(clicked()), | ||
this, SLOT(slotChooseDir()) | ||
); | ||
connect(m_pUi->fix, SIGNAL(clicked()), | ||
m_pFsModel.get(), SLOT(slotFixBOM()) | ||
); | ||
connect(m_pFsModel.get(), SIGNAL(fixed()), | ||
this, SLOT(slotFixed()) | ||
); | ||
connect(m_pUi->about, SIGNAL(clicked()), | ||
this, SLOT(slotAbout()) | ||
); | ||
} | ||
|
||
void MainWindow::slotFixed() | ||
{ | ||
QMessageBox msgBox; | ||
msgBox.setText("BOM has been fixed."); | ||
msgBox.exec(); | ||
|
||
m_pUi->fix->setEnabled(false); | ||
} | ||
|
||
void MainWindow::slotChooseDir() | ||
{ | ||
QString path = QFileDialog::getExistingDirectory(this, "Choose Directory"); | ||
m_pDir = std::make_shared<QDir>(QDir(path)); | ||
m_pFsModel->populateDirectory(QDir(path)); | ||
m_pUi->files->setRootIndex(m_pFsModel->index(path)); | ||
m_pUi->files->setColumnWidth(0, 300); | ||
m_pUi->fix->setEnabled(true); | ||
} | ||
|
||
void MainWindow::slotAbout() | ||
{ | ||
m_about.show(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <memory> | ||
#include <QDir> | ||
#include <QMainWindow> | ||
|
||
#include "bomfilesystemmodel.h" | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
class About; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
private: | ||
std::shared_ptr<QDir> m_pDir; | ||
std::shared_ptr<BOMFileSystemModel> m_pFsModel; | ||
std::shared_ptr<Ui::MainWindow> m_pUi; | ||
std::shared_ptr<Ui::About> m_pUiAbout; | ||
QWidget m_about; | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = 0); | ||
|
||
public slots: | ||
void slotChooseDir(); | ||
void slotFixed(); | ||
void slotAbout(); | ||
}; | ||
|
||
#endif // MAINWINDOW_H |
Oops, something went wrong.