#include <QKeyEvent>
#include <QMessageBox>
#include <QListWidget>
#include <QVBoxLayout>
#include <QFileInfoList>
#include <QListWidgetItem>
#include "contenttab.h"
ContentTab::ContentTab(QWidget *parent) :
QListWidget(parent)
{
setDragEnabled(false);
setIconSize(QSize(45, 45));
}
ContentTab::~ContentTab()
{
}
void ContentTab::init(const QDesktopServices::StandardLocation &location,
const QString &filter,
const QString &icon)
{
setContentDir(location);
QStringList filterList;
filterList = filter.split(";");
m_ContentDir.setNameFilters(filterList);
setIcon(icon);
connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(openItem(QListWidgetItem*)));
populateListWidget();
}
void ContentTab::setContentDir(const QDesktopServices::StandardLocation &location)
{
m_ContentDir.setPath(QDesktopServices::storageLocation(location));
}
void ContentTab::setIcon(const QString &icon)
{
m_Icon = QIcon(icon);
}
void ContentTab::populateListWidget()
{
QFileInfoList fileList = m_ContentDir.entryInfoList(QDir::Files, QDir::Time);
foreach(QFileInfo item, fileList) {
new QListWidgetItem(m_Icon, itemName(item), this);
}
}
QString ContentTab::itemName(const QFileInfo &item)
{
return QString(item.baseName() + "." + item.completeSuffix());
}
QUrl ContentTab::itemUrl(QListWidgetItem *item)
{
return QUrl("file:///" + m_ContentDir.absolutePath() + "/" + item->text());
}
void ContentTab::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Select:
openItem(currentItem());
default:
QListWidget::keyPressEvent(event);
break;
}
}
void ContentTab::handleErrorInOpen(QListWidgetItem *item)
{
Q_UNUSED(item);
QMessageBox::warning(this, tr("Operation Failed"), tr("Unknown error!"), QMessageBox::Close);
}
void ContentTab::openItem(QListWidgetItem *item)
{
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
QStringList nameFilters = m_ContentDir.nameFilters();
if (nameFilters.contains("*.mp3")) {
QMessageBox::warning(this, tr("Operation Failed"), tr("Action not supported in emulator."),
QMessageBox::Close);
} else
#endif
{
bool ret = QDesktopServices::openUrl(itemUrl(item));
if (!ret)
handleErrorInOpen(item);
}
}