41#include <QDomDocument>
44#include <QCryptographicHash>
46#ifndef H2CORE_HAVE_QT6
62 setWindowFlags( windowFlags() | Qt::CustomizeWindowHint |
63 Qt::WindowMinMaxButtonsHint );
65 setWindowTitle( tr(
"Sound Library import" ) );
68 headers << tr(
"Sound library" ) << tr(
"Status" );
69 QTreeWidgetItem* header =
new QTreeWidgetItem( headers );
70 m_pDrumkitTree->setHeaderItem( header );
71 m_pDrumkitTree->header()->resizeSection( 0, 200 );
73 connect( m_pDrumkitTree, SIGNAL( currentItemChanged ( QTreeWidgetItem*, QTreeWidgetItem* ) ),
this, SLOT(
soundLibraryItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );
76 SoundLibraryNameLbl->setText(
"" );
77 SoundLibraryInfoLbl->setText(
"" );
78 DownloadBtn->setEnabled(
false );
80 InstallBtn->setEnabled (
false );
85 tabWidget->setCurrentIndex( 0 );
87 tabWidget->setCurrentIndex( 1 );
97 pH2App->removeEventListener(
this );
111 pref->
sServerList.push_back(
"http://hydrogen-music.org/feeds/drumkit_list.php" );
114 repositoryCombo->clear();
116 std::list<QString>::const_iterator cur_Server;
118 repositoryCombo->insertItem( 0, *cur_Server );
127 if(!repositoryCombo->currentText().isEmpty())
153 INFOLOG(
"Deleting cached image files from " + cacheDir );
155 QDir dir( cacheDir );
156 dir.setNameFilters(QStringList() <<
"*.png");
157 dir.setFilter(QDir::Files);
158 foreach(QString dirFile, dir.entryList())
160 if ( !dir.remove(dirFile) )
162 WARNINGLOG(
"Error removing image file(s) from cache.");
170 QString serverMd5 = QString(QCryptographicHash::hash(( repositoryCombo->currentText().toLatin1() ),QCryptographicHash::Md5).toHex());
171 QString cacheFile = cacheDir +
"/" + serverMd5;
178 QString kitNameMd5 = QString(QCryptographicHash::hash(( SoundLibraryNameLbl->text().toLatin1() ),QCryptographicHash::Md5).toHex());
179 QString cacheFile = cacheDir +
"/" + kitNameMd5 +
".png";
191 QFile outFile( fileName );
192 if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
194 ERRORLOG( QString(
"Failed to open file for writing repository cache: %1").arg( fileName ) );
198 QTextStream stream( &outFile );
208 QFile outFile( cacheFile );
209 if( !outFile.open( QIODevice::WriteOnly ) )
211 ERRORLOG( QString(
"Failed to open file for writing repository image cache: %1").arg( imageFile ) );
215 pixmap.save(&outFile);
223 QFile inFile( fileName );
224 if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
226 ERRORLOG( QString(
"Failed to open file for reading: %1").arg( fileName ) );
230 QDomDocument document;
231 if( !document.setContent( &inFile ) )
238 content = document.toString();
247 QFile file( cacheFile );
269 dom.setContent( sDrumkitXML );
270 QDomNode drumkitNode = dom.documentElement().firstChild();
271 while ( !drumkitNode.isNull() ) {
272 if( !drumkitNode.toElement().isNull() ) {
274 if ( drumkitNode.toElement().tagName() ==
"drumkit" || drumkitNode.toElement().tagName() ==
"song" || drumkitNode.toElement().tagName() ==
"pattern" ) {
278 if ( drumkitNode.toElement().tagName() ==
"song" ) {
279 soundLibInfo.
setType(
"song" );
282 if ( drumkitNode.toElement().tagName() ==
"drumkit" ) {
283 soundLibInfo.
setType(
"drumkit" );
286 if ( drumkitNode.toElement().tagName() ==
"pattern" ) {
287 soundLibInfo.
setType(
"pattern" );
290 QDomElement nameNode = drumkitNode.firstChildElement(
"name" );
291 if ( !nameNode.isNull() ) {
292 soundLibInfo.
setName( nameNode.text() );
295 QDomElement urlNode = drumkitNode.firstChildElement(
"url" );
296 if ( !urlNode.isNull() ) {
297 soundLibInfo.
setUrl( urlNode.text() );
300 QDomElement infoNode = drumkitNode.firstChildElement(
"info" );
301 if ( !infoNode.isNull() ) {
302 soundLibInfo.
setInfo( infoNode.text() );
305 QDomElement authorNode = drumkitNode.firstChildElement(
"author" );
306 if ( !authorNode.isNull() ) {
307 soundLibInfo.
setAuthor( authorNode.text() );
310 QDomElement licenseNode = drumkitNode.firstChildElement(
"license" );
311 if ( !licenseNode.isNull() ) {
312 soundLibInfo.
setLicense( licenseNode.text() );
315 QDomElement imageNode = drumkitNode.firstChildElement(
"image" );
316 if ( !imageNode.isNull() ) {
317 soundLibInfo.
setImage( imageNode.text() );
320 QDomElement imageLicenseNode = drumkitNode.firstChildElement(
"imageLicense" );
321 if ( !imageLicenseNode.isNull() ) {
329 drumkitNode = drumkitNode.nextSibling();
341 QApplication::setOverrideCursor(Qt::WaitCursor);
342 QString downloadUrl = repositoryCombo->currentText();
346 DownloadWidget drumkitList(
this, tr(
"Updating SoundLibrary list..." ), downloadUrl);
351 }
else if (drumkitList.
get_error().isEmpty()) {
360 QMessageBox::warning(
this,
"Hydrogen", drumkitList.
get_error() );
381 QApplication::restoreOverrideCursor();
390 m_pDrumkitTree->clear();
397 m_pSongItem =
new QTreeWidgetItem( m_pDrumkitTree );
408 QTreeWidgetItem* pDrumkitItem =
nullptr;
420 pDrumkitItem->setText( 0, sLibraryName );
421 pDrumkitItem->setText( 1, tr(
"Installed" ) );
424 pDrumkitItem->setText( 0, sLibraryName );
425 pDrumkitItem->setText( 1, tr(
"New" ) );
448 QString sName = QFileInfo( sInfo.
getUrl() ).fileName();
449 sName = sName.left( sName.lastIndexOf(
"." ) );
451 if ( sInfo.
getType() ==
"drumkit" ) {
457 if ( sInfo.
getType() ==
"pattern" ) {
462 if ( sInfo.
getType() ==
"song" ) {
482 int x = (int) drumkitImageLabel->size().width();
483 int y = drumkitImageLabel->size().height();
484 float labelAspect = (float) x / y;
485 float imageAspect = (float) pixmap.width() / pixmap.height();
487 if ( ( x < pixmap.width() ) || ( y < pixmap.height() ) )
489 if ( labelAspect >= imageAspect )
492 pixmap = pixmap.scaledToHeight( y );
497 pixmap = pixmap.scaledToWidth( x );
500 drumkitImageLabel->setPixmap( pixmap );
510 QString selected = current->text(0);
517 SoundLibraryNameLbl->setText( info.
getName() );
519 if( info.
getType() ==
"pattern" ){
520 SoundLibraryInfoLbl->setText(
"");
522 SoundLibraryInfoLbl->setText( info.
getInfo() );
525 AuthorLbl->setText( tr(
"Author: %1" ).arg( info.
getAuthor() ) );
527 LicenseLbl->setText( tr(
"Drumkit License: %1" )
530 ImageLicenseLbl->setText( tr(
"Image License: %1" )
535 drumkitImageLabel->setPixmap( QPixmap() );
536 drumkitImageLabel->setText( info.
getImage() );
538 if ( info.
getImage().length() > 0 ) {
541 QString sName = QFileInfo( info.
getUrl() ).fileName();
542 sName = sName.left( sName.lastIndexOf(
"." ) );
546 if ( pDrumkit !=
nullptr ) {
548 QPixmap pixmap ( pDrumkit->get_path() +
"/" + pDrumkit->get_image() );
549 INFOLOG(
"Loaded image " + pDrumkit->get_image() +
" from local filesystem");
561 if ( cachedFile.length() > 0 ) {
562 QPixmap pixmap ( cachedFile );
564 INFOLOG(
"Loaded image " + info.
getImage() +
" from cache (" + cachedFile +
")" );
573 if ( info.
getImage().length() > 0 ) {
577 sImageUrl = repositoryCombo->currentText().left( repositoryCombo->currentText().lastIndexOf( QString(
"/" )) + 1 ) + info.
getImage() ;
578 sLocalFile = QDir::tempPath() +
"/" + QFileInfo( sImageUrl ).fileName();
585 QFile::remove( sLocalFile );
591 DownloadBtn->setEnabled(
true );
597 SoundLibraryNameLbl->setText(
"" );
598 SoundLibraryInfoLbl->setText(
"" );
599 AuthorLbl->setText(
"" );
600 DownloadBtn->setEnabled(
false );
607 QApplication::setOverrideCursor(Qt::WaitCursor);
608 QString selected = m_pDrumkitTree->currentItem()->text(0);
617 if( sType ==
"drumkit") {
618 sLocalFile = QDir::tempPath() +
"/" + QFileInfo( sURL ).fileName();
621 if( sType ==
"song") {
625 if( sType ==
"pattern") {
632 DownloadWidget dl(
this, tr(
"Downloading SoundLibrary..." ), sURL, sLocalFile );
636 if (redirect_url.isEmpty() ) {
642 sURL = redirect_url.toEncoded();
653 if ( sType ==
"drumkit" ) {
655 QApplication::restoreOverrideCursor();
659 if ( sType ==
"song" || sType ==
"pattern") {
660 QApplication::restoreOverrideCursor();
664 QApplication::restoreOverrideCursor();
665 QMessageBox::warning(
this,
"Hydrogen", tr(
"An error occurred importing the SoundLibrary." ) );
670 QApplication::restoreOverrideCursor();
673 QApplication::setOverrideCursor(Qt::WaitCursor);
675 if( sType ==
"drumkit" ) {
677 dir.remove( sLocalFile );
682 QApplication::restoreOverrideCursor();
695 sPath = QDir::homePath();
699 fd.setAcceptMode( QFileDialog::AcceptOpen );
700 fd.setFileMode(QFileDialog::ExistingFile);
701 fd.setNameFilter(
"Hydrogen drumkit (*.h2drumkit)" );
702 fd.setDirectory( sPath );
704 fd.setWindowTitle( tr(
"Import drumkit" ) );
706 QString filename =
"";
707 if (fd.exec() == QDialog::Accepted) {
708 filename = fd.selectedFiles().first();
711 if (filename !=
"") {
712 SoundLibraryPathTxt->setText( filename );
714 InstallBtn->setEnabled (
true );
723 QApplication::setOverrideCursor(Qt::WaitCursor);
725 QString sError = tr(
"An error occurred importing the SoundLibrary." );
728 QString sImportedPath;
729 bool bEncodingIssues;
731 SoundLibraryPathTxt->text(),
"", &sImportedPath,
732 &bEncodingIssues,
false ) ) {
733 QApplication::restoreOverrideCursor();
735#ifndef H2CORE_HAVE_QT6
737 auto pCodec = QTextCodec::codecForLocale();
738 if ( ! pCodec->canEncode( SoundLibraryPathTxt->text() ) ) {
739 QMessageBox::critical(
740 this,
"Hydrogen", QString(
"%1\n\n%2\n\n%3: [%4]" )
741 .arg( sError ).arg( SoundLibraryPathTxt->text() )
743 ->getEncodingError() )
744 .arg( QString( pCodec->name() ) ) );
747 QMessageBox::critical(
this,
"Hydrogen", sError );
750 QMessageBox::critical(
this,
"Hydrogen", sError );
758 QApplication::restoreOverrideCursor();
759 if ( ! bEncodingIssues ) {
760 QMessageBox::information(
this,
"Hydrogen",
761 QString( tr(
"SoundLibrary imported in %1" )
762 .arg( sImportedPath ) ) );
765 QMessageBox::warning(
767 QString( tr(
"SoundLibrary imported in %1" )
768 .arg( sImportedPath ) )
769 .append( tr(
"\nBut there were encoding issues.\n\nPlease set your system's locale to UTF-8!" ) ) );
773 QApplication::restoreOverrideCursor();
774 QMessageBox::warning(
this,
"Hydrogen", sError );
const QString & get_xml_content()
const QString & get_error()
Custom file dialog checking whether the user has write access to the selected folder before allowing ...
static bool install(const QString &sSourcePath, const QString &sTargetPath="", QString *pInstalledPath=nullptr, bool *pEncodingIssuesDetected=nullptr, bool bSilent=false)
Extract a .h2drumkit file.
static bool dir_readable(const QString &path, bool silent=false)
returns true if the given path is a readable regular directory
static bool drumkit_exists(const QString &dk_name)
returns true if the drumkit exists within usable system or user drumkits
static QString songs_dir()
returns user songs path
static QString repositories_cache_dir()
returns user repository cache path
static bool file_exists(const QString &path, bool silent=false)
returns true if the given path is an existing regular file
static QString usr_data_path()
returns user data path
static bool song_exists(const QString &sg_name)
returns true if the song file exists
static QString patterns_dir()
returns user patterns path
static Hydrogen * get_instance()
Returns the current Hydrogen instance __instance.
SoundLibraryDatabase * getSoundLibraryDatabase() const
QString getLicenseString() const
Manager for User Preferences File (singleton)
QString getLastImportDrumkitDirectory() const
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
std::list< QString > sServerList
void setLastImportDrumkitDirectory(QString sPath)
bool isPatternInstalled(const QString &sPatternName) const
std::shared_ptr< Drumkit > getDrumkit(const QString &sDrumkitPath, bool bLoad=true)
Retrieve a drumkit from the database.
This class holds information about a soundlibrary.
QString getAuthor() const
void setAuthor(const QString &author)
void setInfo(const QString &info)
void setUrl(const QString &url)
H2Core::License getLicense() const
void setImage(const QString &image)
void setLicense(const H2Core::License &license)
void setName(const QString &name)
void setImageLicense(const H2Core::License &imageLicense)
H2Core::License getImageLicense() const
void setType(const QString &type)
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
std::vector< H2Core::SoundLibraryInfo > m_soundLibraryList
void loadImage(QString img)
QTreeWidgetItem * m_pDrumkitsItem
bool isSoundLibraryItemAlreadyInstalled(H2Core::SoundLibraryInfo sInfo)
Is the SoundLibrary already installed?
void on_BrowseBtn_clicked()
void writeCachedData(const QString &fileName, const QString &data)
QString getCachedImageFilename()
QString getCachedFilename()
~SoundLibraryImportDialog()
QString readCachedData(const QString &fileName)
void updateSoundLibraryList()
QTreeWidgetItem * m_pSongItem
void showImage(QPixmap pixmap)
QString readCachedImage(const QString &imageFile)
virtual void soundLibraryChangedEvent() override
void soundLibraryItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)
void on_UpdateListBtn_clicked()
Download and update the drumkit list.
void onRepositoryComboBoxIndexChanged(int)
void writeCachedImage(const QString &imageFile, QPixmap &pixmap)
SoundLibraryImportDialog(QWidget *pParent, bool bOnlineImport)
void updateRepositoryCombo()
void on_InstallBtn_clicked()
QTreeWidgetItem * m_pPatternItem
void on_EditListBtn_clicked()
Edit the server list.
void reloadRepositoryData()
void on_DownloadBtn_clicked()
void on_close_btn_clicked()