Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysMedvid committed Sep 13, 2018
1 parent e0a5fc5 commit 2671711
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 196 deletions.
45 changes: 45 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Denis Medved <[email protected]>
392 changes: 196 additions & 196 deletions LICENSE

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions README.md
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
76 changes: 76 additions & 0 deletions about.ui
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: &lt;a href=&quot;https://www.gnu.org/licenses/gpl-3.0.txt&quot;&gt;GPLv3 or later&lt;/a&gt; </string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="contact">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Contact: Denis Medved &amp;lt;&lt;a href=&quot;mailto:[email protected]&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#2980b9;&quot;&gt;[email protected]&lt;/span&gt;&lt;/a&gt;&amp;gt; &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="source">
<property name="text">
<string>Source: &lt;a href=&quot;https://github.com/DenisMedved/bom-fixer&quot;&gt;https://github.com/DenisMedved/bom-fixer&lt;/a&gt;</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>
25 changes: 25 additions & 0 deletions bom-fixer.pro
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
85 changes: 85 additions & 0 deletions bomfilesystemmodel.cpp
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);
}
31 changes: 31 additions & 0 deletions bomfilesystemmodel.h
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
11 changes: 11 additions & 0 deletions main.cpp
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();
}
56 changes: 56 additions & 0 deletions mainwindow.cpp
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();
}
35 changes: 35 additions & 0 deletions mainwindow.h
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
Loading

0 comments on commit 2671711

Please sign in to comment.