From f7bdcff5bba2d43dccede84315ab115150e5a6f5 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sun, 4 Nov 2012 13:51:46 +0100 Subject: [PATCH 16/16] Implemented downloading in GUI. * The function download_of is almost the same code as himd_writemp3 from himdcli. * Improved QHiMDDownloaddialog a bit (displaying "Download complete" now if no track is left) * Modified QHiMDModel: added methods get_himd_handle and rescan. --- qhimdtransfer/qhimddownloaddialog.cpp | 5 +- qhimdtransfer/qhimdmainwindow.cpp | 91 ++++++++++++++++++++++++++++++++++- qhimdtransfer/qhimdmainwindow.h | 2 + qhimdtransfer/qhimdmodel.cpp | 6 +++ qhimdtransfer/qhimdmodel.h | 2 + 5 files changed, 104 insertions(+), 2 deletions(-) diff --git a/qhimdtransfer/qhimddownloaddialog.cpp b/qhimdtransfer/qhimddownloaddialog.cpp index ad0cde2..5d492f8 100644 --- a/qhimdtransfer/qhimddownloaddialog.cpp +++ b/qhimdtransfer/qhimddownloaddialog.cpp @@ -28,10 +28,13 @@ void QHiMDDownloadDialog::update_status() ui->success_text->setText(tr("%1 track(s) sucessfully downloaded").arg(tracks_success)); ui->failed_text->setText(tr("%1 track(s) could not be downloaded").arg(tracks_failed)); ui->header_text->setText(tr("please wait while downloading %1 track(s)").arg(tracks_total - (tracks_failed + tracks_success))); - ui->progress_text->setText(tr("Downloading track %1 / %2").arg(tracks_success + tracks_failed + 1, tracks_total)); ui->progressBar->setMinimum(0); ui->progressBar->setMaximum(tracks_total); ui->progressBar->setValue(tracks_failed + tracks_success); + if(tracks_failed + tracks_success == tracks_total) + ui->progress_text->setText(tr("Download completed")); + else + ui->progress_text->setText(tr("Downloading track %1 / %2").arg(tracks_success + tracks_failed + 1, tracks_total)); } void QHiMDDownloadDialog::on_show_details_toggled(bool checked) diff --git a/qhimdtransfer/qhimdmainwindow.cpp b/qhimdtransfer/qhimdmainwindow.cpp index 4e02338..9657d81 100644 --- a/qhimdtransfer/qhimdmainwindow.cpp +++ b/qhimdtransfer/qhimdmainwindow.cpp @@ -387,6 +387,7 @@ QHiMDMainWindow::QHiMDMainWindow(QWidget *parent) aboutDialog = new QHiMDAboutDialog; formatDialog = new QHiMDFormatDialog; uploadDialog = new QHiMDUploadDialog; + downloadDialog = new QHiMDDownloadDialog; detect = createDetection(this); ui->setupUi(this); ui->updir->setText(settings.value("lastUploadDirectory", @@ -564,6 +565,94 @@ void QHiMDMainWindow::himd_removed(QString HiMDPath) } } +void QHiMDMainWindow::download_of(const QString &path) +{ + emit himd_busy(ui->himdpath->text()); + downloadDialog->show(); + downloadDialog->startTrack(); + + QApplication::processEvents(); + + struct himderrinfo status; + unsigned char cid[20]; + unsigned char mp3codecinfo[3]; + GMappedFile * mp3file; + unsigned long mp3size; + gchar * mp3buffer; + gint idx_track; + gint nblocks=0, nframes=0; + mp3key key; + struct himd * h; + struct himd_writestream write_stream; + unsigned int first_blockno=0; + unsigned int last_blockno=0; + int seconds; + gchar * artist=NULL, * title=NULL, * album=NULL; + int lastpathsep; + QString filename; + + h = trackmodel.get_himd_handle(); + + himd_create_content_id(cid); + + // Load mp3 stream + mp3file = g_mapped_file_new(path.toLocal8Bit().data() , FALSE, NULL); + mp3size = g_mapped_file_get_length(mp3file); + mp3buffer = g_mapped_file_get_contents(mp3file); + + // Get track key + idx_track = himd_get_free_trackindex(h); + if(himd_obtain_mp3key(h, idx_track, &key, &status) < 0) + { + downloadDialog->trackFailed(tr("Could not obtain MP3 key")); + emit himd_idle(ui->himdpath->text()); + return; + } + + // Write to ATDATA + if(himd_writestream_open(h, &write_stream, &first_blockno, &last_blockno, &status) < 0) + { + downloadDialog->trackFailed(tr("Error opening wriet stream")); + emit himd_idle(ui->himdpath->text()); + return; + } + himd_writestream_writemp3(mp3buffer, mp3size, &write_stream, key, &seconds, &nblocks, &nframes, cid, mp3codecinfo, &status); + himd_writestream_close(&write_stream); + + // Write trackinfo + idx_track = himd_add_mp3_track_info(h, first_blockno, nblocks, nframes, seconds, mp3codecinfo, cid, &status); + + // Set labels + if(get_songinfo(path.toLocal8Bit().data(), &artist, &title, &album)) + { + if(title) + himd_set_track_label(h, idx_track, title, LABEL_TYPE_TITLE, &status); + if(artist) + himd_set_track_label(h, idx_track, artist, LABEL_TYPE_ARTIST, &status); + if(album) + himd_set_track_label(h, idx_track, album, LABEL_TYPE_ALBUM, &status); + free(artist); + free(title); + free(album); + } + else + { + // Use filename, if no tags available. + filename = QString(path); + lastpathsep = filename.lastIndexOf("/"); + if(lastpathsep >= 0) + himd_set_track_label(h, idx_track, filename.remove(0, lastpathsep).toStdString().c_str(), LABEL_TYPE_TITLE, &status); + else // No path separator found? Use the whole path... + himd_set_track_label(h, idx_track, filename.toStdString().c_str(), LABEL_TYPE_TITLE, &status); + } + + himd_write_tifdata(h, &status); + + downloadDialog->trackSucceeded(); + trackmodel.rescan(); + emit himd_idle(ui->himdpath->text()); +} + void QHiMDMainWindow::on_himd_devices_activated(QString device) { open_himd_at(device); @@ -571,5 +660,5 @@ void QHiMDMainWindow::on_himd_devices_activated(QString device) void QHiMDMainWindow::on_download_button_clicked() { - /*download_of(localmodel.filePath(ui->localScan->currentIndex()));*/ + download_of(localmodel.filePath(ui->localScan->currentIndex())); } diff --git a/qhimdtransfer/qhimdmainwindow.h b/qhimdtransfer/qhimdmainwindow.h index a7a0e5e..9b5eefb 100644 --- a/qhimdtransfer/qhimdmainwindow.h +++ b/qhimdtransfer/qhimdmainwindow.h @@ -7,6 +7,7 @@ #include "qhimdaboutdialog.h" #include "qhimdformatdialog.h" #include "qhimduploaddialog.h" +#include "qhimddownloaddialog.h" #include "qhimddetection.h" #include "qhimdmodel.h" #include "../libhimd/himd.h" @@ -37,6 +38,7 @@ private: QHiMDAboutDialog * aboutDialog; QHiMDFormatDialog * formatDialog; QHiMDUploadDialog * uploadDialog; + QHiMDDownloadDialog * downloadDialog; QHiMDDetection * detect; QHiMDTracksModel trackmodel; QHiMDFileSystemModel localmodel; diff --git a/qhimdtransfer/qhimdmodel.cpp b/qhimdtransfer/qhimdmodel.cpp index ac4bde5..4f22a65 100644 --- a/qhimdtransfer/qhimdmodel.cpp +++ b/qhimdtransfer/qhimdmodel.cpp @@ -268,6 +268,12 @@ QHiMDTrackList QHiMDTracksModel::tracks(const QModelIndexList & modelindices) co return tracks; } +void QHiMDTracksModel::rescan() +{ + if(himd) + reset(); +} + QStringList QHiMDTracksModel::downloadableFileExtensions() const { return (QStringList() << "mp3"); diff --git a/qhimdtransfer/qhimdmodel.h b/qhimdtransfer/qhimdmodel.h index 6ccc2f8..6f187d7 100644 --- a/qhimdtransfer/qhimdmodel.h +++ b/qhimdtransfer/qhimdmodel.h @@ -53,6 +53,8 @@ public: QHiMDTrack track(int trackidx) const; QHiMDTrackList tracks(const QModelIndexList & indices) const; QStringList downloadableFileExtensions() const; + struct himd * get_himd_handle() { return himd; } + void rescan(); }; class QHiMDFileSystemModel : public QFileSystemModel { -- 1.8.0