Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QFileSystemModel>
- #include <QDir>
- #include <QDirIterator>
- #include <QDebug>
- #include <QMessageBox>
- #include "musicsortermain.h"
- #include "ui_musicsortermain.h"
- MusicSorterMain::MusicSorterMain(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MusicSorterMain) {
- ui->setupUi(this);
- /* The idea here is simple - move doubled songs to another directory for later deletion
- * We must do a few things to setup the enviroment
- * - create 2 models, DirModel and FileModel
- * - set them up the filters and root paths
- * - add these models to their respecting GUI views
- */
- MusicSorterMain::AddText("<font color = green> Instructions:<br>1. Simply select a directory that contains music files that may be doubled<br>2. Press the 'Remove doubles' button to sort the files<br>3. Press the 'Delete files' button to delete these doubled files.<br>You may set some advanced settings below.<br>Made for personal use.");
- DoublesDirectory = "/DOUBLED SONGS";
- QString FilePath = "C:/"; // the default directory, assume C:/ because most Windows PCs use this as their OS directory
- DirModel = new QFileSystemModel(this); // create our DirModel
- DirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs); // setup our filters for the DirModel
- DirModel->setRootPath(FilePath); // set our root path
- ui->DirectoryTree->setModel(DirModel); // finally add the model to our directory tree
- FileModel = new QFileSystemModel(this); // create our FileModel
- MusicFilter << "*.aac" << "*.wmv" << "*.avi" << "*.mpeg" << "*.mov" << "*.3gp" << "*.flv" << "*.mp3"; // these are the files that will be displayed
- FileModel->setNameFilterDisables(false); // disable graying out files that don't match MusicFilter
- FileModel->setNameFilters(MusicFilter); // set our name filter
- FileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files); // setup our filters for the DirModel
- FileModel->setRootPath(FilePath); // set our root path
- ui->MusicList->setModel(FileModel); // add the model to our music list
- }
- MusicSorterMain::~MusicSorterMain() {
- delete ui;
- }
- void MusicSorterMain::AddText(QString Text) { // this function will add a line of text t othe output console
- ui->ConsoleOutput->insertHtml("<font color = blue>" + Text);
- ui->ConsoleOutput->insertPlainText("\n");
- }
- void MusicSorterMain::on_DirectoryTree_clicked(const QModelIndex &index) { // this is run when the user changes directory in the directory tree
- QString FilePath = DirModel->fileInfo(index).absoluteFilePath(); // here we extract the directory of which the user selected
- FilePathFinal = FilePath;
- ui->MusicList->setRootIndex(FileModel->setRootPath(FilePath)); // finally, display the music files found in this directory
- }
- void MusicSorterMain::on_DoublesButton_clicked() { // this function will control moving doubled songs to a new directory
- QDir dir(FilePathFinal + DoublesDirectory);
- if (!dir.exists()) { // if the DOUBLES directory does not exist
- MusicSorterMain::AddText("Creating the directory " + DoublesDirectory + " at: " + FilePathFinal);
- dir.mkpath(FilePathFinal + DoublesDirectory); // create the directory
- }
- QDirIterator File(FilePathFinal, QStringList() << MusicFilter, QDir::Files); // this setups the files we are going to iterate over
- while (File.hasNext()) { // this while loop goes over /every/ file matching MusicFilter in our directory (NO SUB DIRECTORIES)
- if(File.fileName().contains(" - Copy")) { // if the file simply is a copy and paste, move the file (has - Copy in the name)
- QFile::rename(File.filePath(), File.path() + DoublesDirectory + "/" + File.fileName()); // now we move the file to the DOUBLES directory
- QString Message = "Moved file: " + File.filePath().toLatin1() + " to " + FilePathFinal + DoublesDirectory; // setup our console message
- MusicSorterMain::AddText(Message.toLatin1()); // add an output message to the console
- }
- File.next();
- }
- }
- void MusicSorterMain::on_RemoveButton_clicked() { // this function is run when the remove button is pressed
- QMessageBox::StandardButton reply;
- reply = QMessageBox::question(this, "Warning!", "Are you sure you want to delete song doubles?", QMessageBox::Yes|QMessageBox::No);
- if (reply == QMessageBox::Yes) { // the user does want to delete the file
- QDir dir(FilePathFinal + DoublesDirectory); // here we set the directory that contains our doubled songs
- dir.setNameFilters(QStringList() << MusicFilter); // here we set our name filter
- dir.setFilter(QDir::Files); // here we set our filter
- foreach(QString DirFile, dir.entryList()) { // this will loop between all files found in the directory that match MusicFilter
- dir.remove(DirFile); // here we remove the file
- MusicSorterMain::AddText("</font> <font color = red>Deleted file: " + DirFile + "</font>");
- }
- }
- }
- void MusicSorterMain::on_CustomDirLine_textChanged(const QString &arg1) { // this function is run when the CustomDirLine has changed text
- DoublesDirectory = arg1;
- DoublesDirectory.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_โ+=|:;<>ยซยป,.?/{}\'\"\\\[\\\\\\]"))); // here we remove ALL special characters.. a directory can't contain them
- if(DoublesDirectory.left(1) == " ") { // remove beginning space
- DoublesDirectory = "/InvalidDirectory";
- ui->CustomDirLine->setText(DoublesDirectory);
- }
- if(DoublesDirectory.left(1) != "/") { // we need a "/" as the first character, if it is not there, add it
- DoublesDirectory = "/" + DoublesDirectory; // our new doubles directory
- }
- }
- void MusicSorterMain::on_AddExtensionsButton_clicked() { // this controls the add extension button
- QString ExtraExtensions = ui->ExtraExtensions->text();
- MusicFilter << ExtraExtensions; // add our extra extension to the list of extensions
- FileModel->setNameFilters(MusicFilter); // reset our file model
- MusicSorterMain::AddText("Added extension " + ExtraExtensions + " to the filter.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement