1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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_Select:
|
---|
118 | openItem(currentItem());
|
---|
119 | default:
|
---|
120 | QListWidget::keyPressEvent(event);
|
---|
121 | break;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | void ContentTab::handleErrorInOpen(QListWidgetItem *item)
|
---|
126 | {
|
---|
127 | Q_UNUSED(item);
|
---|
128 | QMessageBox::warning(this, tr("Operation Failed"), tr("Unkown error!"), QMessageBox::Close);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // NEW SLOTS
|
---|
132 | void ContentTab::openItem(QListWidgetItem *item)
|
---|
133 | {
|
---|
134 | #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
|
---|
135 | // Opening music files doesn't work in Symbian emulator and in some SDKs freezes the
|
---|
136 | // emulator entirely, so prevent it.
|
---|
137 | QStringList nameFilters = m_ContentDir.nameFilters();
|
---|
138 | if (nameFilters.contains("*.mp3")) {
|
---|
139 | QMessageBox::warning(this, tr("Operation Failed"), tr("Action not supported in emulator."),
|
---|
140 | QMessageBox::Close);
|
---|
141 | } else
|
---|
142 | #endif
|
---|
143 | {
|
---|
144 | bool ret = QDesktopServices::openUrl(itemUrl(item));
|
---|
145 | if (!ret)
|
---|
146 | handleErrorInOpen(item);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | // End of File
|
---|