Advertisement
BeamNG_IRC

Untitled

Mar 10th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.09 KB | None | 0 0
  1. #include <QFileSystemModel>
  2. #include <QDir>
  3. #include <QDirIterator>
  4. #include <QDebug>
  5. #include <QMessageBox>
  6. #include "musicsortermain.h"
  7. #include "ui_musicsortermain.h"
  8.  
  9. MusicSorterMain::MusicSorterMain(QWidget *parent) :
  10.     QMainWindow(parent),
  11.     ui(new Ui::MusicSorterMain) {
  12.     ui->setupUi(this);
  13.     /* The idea here is simple - move doubled songs to another directory for later deletion
  14.      * We must do a few things to setup the enviroment
  15.      * - create 2 models, DirModel and FileModel
  16.      * - set them up the filters and root paths
  17.      * - add these models to their respecting GUI views
  18.      */
  19.     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.");
  20.     DoublesDirectory = "/DOUBLED SONGS";
  21.     QString FilePath = "C:/"; // the default directory, assume C:/ because most Windows PCs use this as their OS directory
  22.     DirModel = new QFileSystemModel(this); // create our DirModel
  23.     DirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs); // setup our filters for the DirModel
  24.     DirModel->setRootPath(FilePath); // set our root path
  25.     ui->DirectoryTree->setModel(DirModel); // finally add the model to our directory tree
  26.  
  27.     FileModel = new QFileSystemModel(this); // create our FileModel
  28.     MusicFilter << "*.aac" << "*.wmv" << "*.avi" << "*.mpeg" << "*.mov" << "*.3gp" << "*.flv" << "*.mp3"; // these are the files that will be displayed
  29.     FileModel->setNameFilterDisables(false); // disable graying out files that don't match MusicFilter
  30.     FileModel->setNameFilters(MusicFilter); // set our name filter
  31.     FileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files); // setup our filters for the DirModel
  32.     FileModel->setRootPath(FilePath); // set our root path
  33.     ui->MusicList->setModel(FileModel); // add the model to our music list
  34. }
  35.  
  36. MusicSorterMain::~MusicSorterMain() {
  37.     delete ui;
  38. }
  39.  
  40. void MusicSorterMain::AddText(QString Text) { // this function will add a line of text t othe output console
  41.     ui->ConsoleOutput->insertHtml("<font color = blue>" + Text);
  42.     ui->ConsoleOutput->insertPlainText("\n");
  43. }
  44.  
  45. void MusicSorterMain::on_DirectoryTree_clicked(const QModelIndex &index) { // this is run when the user changes directory in the directory tree
  46.     QString FilePath = DirModel->fileInfo(index).absoluteFilePath(); // here we extract the directory of which the user selected
  47.     FilePathFinal = FilePath;
  48.     ui->MusicList->setRootIndex(FileModel->setRootPath(FilePath)); // finally, display the music files found in this directory
  49. }
  50.  
  51. void MusicSorterMain::on_DoublesButton_clicked() { // this function will control moving doubled songs to a new directory
  52.     QDir dir(FilePathFinal + DoublesDirectory);
  53.     if (!dir.exists()) { // if the DOUBLES directory does not exist
  54.             MusicSorterMain::AddText("Creating the directory " + DoublesDirectory + " at: " + FilePathFinal);
  55.         dir.mkpath(FilePathFinal + DoublesDirectory); // create the directory
  56.     }
  57.     QDirIterator File(FilePathFinal, QStringList() << MusicFilter, QDir::Files); // this setups the files we are going to iterate over
  58.     while (File.hasNext()) { // this while loop goes over /every/ file matching MusicFilter in our directory (NO SUB DIRECTORIES)
  59.         if(File.fileName().contains(" - Copy")) { // if the file simply is a copy and paste, move the file (has - Copy in the name)
  60.             QFile::rename(File.filePath(), File.path() + DoublesDirectory + "/" + File.fileName()); // now we move the file to the DOUBLES directory
  61.             QString Message = "Moved file: " + File.filePath().toLatin1() + " to " + FilePathFinal + DoublesDirectory; // setup our console message
  62.             MusicSorterMain::AddText(Message.toLatin1()); // add an output message to the console
  63.         }
  64.         File.next();
  65.     }
  66. }
  67.  
  68. void MusicSorterMain::on_RemoveButton_clicked() { // this function is run when the remove button is pressed
  69.     QMessageBox::StandardButton reply;
  70.       reply = QMessageBox::question(this, "Warning!", "Are you sure you want to delete song doubles?", QMessageBox::Yes|QMessageBox::No);
  71.       if (reply == QMessageBox::Yes) { // the user does want to delete the file
  72.           QDir dir(FilePathFinal + DoublesDirectory); // here we set the directory that contains our doubled songs
  73.           dir.setNameFilters(QStringList() << MusicFilter); // here we set our name filter
  74.           dir.setFilter(QDir::Files); // here we set our filter
  75.           foreach(QString DirFile, dir.entryList()) { // this will loop between all files found in the directory that match MusicFilter
  76.               dir.remove(DirFile); // here we remove the file
  77.                MusicSorterMain::AddText("</font> <font color = red>Deleted file: " + DirFile + "</font>");
  78.           }
  79.       }
  80. }
  81.  
  82. void MusicSorterMain::on_CustomDirLine_textChanged(const QString &arg1) { // this function is run when the CustomDirLine has changed text
  83.     DoublesDirectory = arg1;
  84.     DoublesDirectory.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_โ€”+=|:;<>ยซยป,.?/{}\'\"\\\[\\\\\\]"))); // here we remove ALL special characters.. a directory can't contain them
  85.     if(DoublesDirectory.left(1) == " ") { // remove beginning space
  86.         DoublesDirectory = "/InvalidDirectory";
  87.         ui->CustomDirLine->setText(DoublesDirectory);
  88.     }
  89.     if(DoublesDirectory.left(1) != "/") { // we need a "/" as the first character, if it is not there, add it
  90.         DoublesDirectory = "/" + DoublesDirectory; // our new doubles directory
  91.     }
  92. }
  93.  
  94. void MusicSorterMain::on_AddExtensionsButton_clicked() { // this controls the add extension button
  95.     QString ExtraExtensions = ui->ExtraExtensions->text();
  96.     MusicFilter << ExtraExtensions; // add our extra extension to the list of extensions
  97.     FileModel->setNameFilters(MusicFilter); // reset our file model
  98.     MusicSorterMain::AddText("Added extension " + ExtraExtensions + " to the filter.");
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement