demos/embedded/desktopservices/contenttab.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the demonstration applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 // EXTERNAL INCLUDES
       
    43 #include <QKeyEvent>
       
    44 #include <QMessageBox>
       
    45 #include <QListWidget>
       
    46 #include <QVBoxLayout>
       
    47 #include <QFileInfoList>
       
    48 #include <QListWidgetItem>
       
    49 
       
    50 // INTERNAL INCLUDES
       
    51 
       
    52 // CLASS HEADER
       
    53 #include "contenttab.h"
       
    54 
       
    55 
       
    56 // CONSTRUCTORS & DESTRUCTORS
       
    57 ContentTab::ContentTab(QWidget *parent) :
       
    58         QListWidget(parent)
       
    59 {
       
    60     setDragEnabled(false);
       
    61     setIconSize(QSize(45, 45));
       
    62 }
       
    63 
       
    64 ContentTab::~ContentTab()
       
    65 {
       
    66 }
       
    67 
       
    68 // NEW PUBLIC METHODS
       
    69 void ContentTab::init(const QDesktopServices::StandardLocation &location,
       
    70                       const QString &filter,
       
    71                       const QString &icon)
       
    72 {
       
    73     setContentDir(location);
       
    74     QStringList filterList;
       
    75     filterList = filter.split(";");
       
    76     m_ContentDir.setNameFilters(filterList);
       
    77     setIcon(icon);
       
    78 
       
    79     connect(this, SIGNAL(itemClicked(QListWidgetItem *)),
       
    80             this, SLOT(openItem(QListWidgetItem *)));
       
    81 
       
    82     populateListWidget();
       
    83 }
       
    84 
       
    85 // NEW PROTECTED METHODS
       
    86 void ContentTab::setContentDir(const QDesktopServices::StandardLocation &location)
       
    87 {
       
    88     m_ContentDir.setPath(QDesktopServices::storageLocation(location));
       
    89 }
       
    90 
       
    91 void ContentTab::setIcon(const QString &icon)
       
    92 {
       
    93     m_Icon = QIcon(icon);
       
    94 }
       
    95 
       
    96 void ContentTab::populateListWidget()
       
    97 {
       
    98     QFileInfoList fileList = m_ContentDir.entryInfoList(QDir::Files, QDir::Time);
       
    99     foreach(QFileInfo item, fileList) {
       
   100         new QListWidgetItem(m_Icon, itemName(item), this);
       
   101     }
       
   102 }
       
   103 
       
   104 QString ContentTab::itemName(const QFileInfo &item)
       
   105 {
       
   106     return QString(item.baseName() + "." + item.completeSuffix());
       
   107 }
       
   108 
       
   109 QUrl ContentTab::itemUrl(QListWidgetItem *item)
       
   110 {
       
   111     return QUrl("file:///" + m_ContentDir.absolutePath() + "/" + item->text());
       
   112 }
       
   113 
       
   114 void ContentTab::keyPressEvent(QKeyEvent *event)
       
   115 {
       
   116     switch (event->key()) {
       
   117     case Qt::Key_Up:
       
   118         if (currentRow() == 0) {
       
   119             setCurrentRow(count() - 1);
       
   120         } else {
       
   121             setCurrentRow(currentRow() - 1);
       
   122         }
       
   123         break;
       
   124     case Qt::Key_Down:
       
   125         if (currentRow() == (count() - 1)) {
       
   126             setCurrentRow(0);
       
   127         } else {
       
   128             setCurrentRow(currentRow() + 1);
       
   129         }
       
   130         break;
       
   131     case Qt::Key_Select:
       
   132         openItem(currentItem());
       
   133     default:
       
   134         QListWidget::keyPressEvent(event);
       
   135         break;
       
   136     }
       
   137 }
       
   138 
       
   139 void ContentTab::handleErrorInOpen(QListWidgetItem *item)
       
   140 {
       
   141     Q_UNUSED(item);
       
   142     QMessageBox::warning(this, tr("Operation Failed"), tr("Unkown error!"), QMessageBox::Close);
       
   143 }
       
   144 
       
   145 // NEW SLOTS
       
   146 void ContentTab::openItem(QListWidgetItem *item)
       
   147 {
       
   148 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
       
   149     // Opening music files doesn't work in Symbian emulator and in some SDKs freezes the
       
   150     // emulator entirely, so prevent it.
       
   151     QStringList nameFilters = m_ContentDir.nameFilters();
       
   152     if (nameFilters.contains("*.mp3")) {
       
   153         QMessageBox::warning(this, tr("Operation Failed"), tr("Action not supported in emulator."),
       
   154                              QMessageBox::Close);
       
   155     } else
       
   156 #endif
       
   157     {
       
   158         bool ret = QDesktopServices::openUrl(itemUrl(item));
       
   159         if (!ret)
       
   160             handleErrorInOpen(item);
       
   161     }
       
   162 }
       
   163 
       
   164 
       
   165 // End of File