xref: /OK3568_Linux_fs/app/qfm/qfmwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "qfmwindow.h"
52 #include <QApplication>
53 #include <QColor>
54 #include <QCheckBox>
55 #include <QDebug>
56 #include <QDesktopWidget>
57 #include <QDir>
58 #include <QDirIterator>
59 #include <QMimeDatabase>
60 #include <QStandardPaths>
61 #include <QToolBar>
62 
63 
64 #define FILEMANAGER "File Manager"
QfmWindow(QWidget * parent)65 QfmWindow::QfmWindow(QWidget *parent) : QMainWindow(parent)
66 {
67     m_curDir= "top";
68     m_multichecking = false;
69     m_topDirList<<"Root"<<"Home"<<"Oem"<<"User Data"<<"SD Card"<<"USB Disk";
70     m_topPathList<<"/"<<QStandardPaths::standardLocations(QStandardPaths::HomeLocation)<<"/oem"<<"/userdata"<<"/sdcard"<<"/udisk";
71     m_videoSuffixList<<"*.mp4"<<"*.m4v"<<"*.avi"<<"*.wmv"<<"*.mkv"<<"*.asf"<<"*.mov"<<"*.ts"<<"*.mpg"<<"*.mpeg"<<"*.vob"<<"*.m2ts"<<"*.webm";
72     m_musicSuffixList<<"*.mp1"<<"*.mp2"<<"*.mp3"<<"*.wav"<<"*.wave"<<"*.wma"<<"*.ogg"<<"*.m4a"<<"*.flac";
73     m_picSuffixList<<"*.jpg"<<"*.png"<<"*.bmp"<<"*.jpeg";
74     m_Filter = FileAll;
75     initLayout();
76     connect(m_btnopen, SIGNAL(clicked(bool)), this, SLOT(on_openClicked()));
77     connect(m_btnreturn, SIGNAL(clicked(bool)), this, SLOT(on_returnClicked()));
78     connect(m_listWid, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(on_itemClicked(QListWidgetItem *)));
79 }
80 
initLayout()81 void QfmWindow::initLayout()
82 {
83     const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
84     resize(availableGeometry.width(), availableGeometry.height());
85 
86     m_btnreturn = new QPushButton(this);
87     m_btnreturn->setStyleSheet(tr("border-image: url(:/image/return.png);"));
88     QPixmap pixmap(":/image/return.png");
89     m_btnreturn->setFixedSize(pixmap.width(), pixmap.height());
90 
91     m_titleLabel = new QLabel(tr(FILEMANAGER), this);
92     QFont font = m_titleLabel->font();
93     font.setBold(true);
94     m_titleLabel->setFont(font);
95     m_titleLabel->setAlignment(Qt::AlignCenter);
96     m_titleLabel->setStyleSheet("background-color:rgba(255, 255, 255, 0)");
97 
98     m_btnopen = new QPushButton(this);
99     m_btnopen->setText(tr("open"));
100     m_btnopen->setVisible(true);
101 
102     m_listWid = new QListWidget(this);
103     m_listWid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
104     m_listWid->setStyleSheet("background-color:rgb(204,228,247)");
105     getlist(m_listWid, nullptr);
106     m_listWid->setObjectName(tr("filelist"));
107 
108     m_toolbar = new QToolBar(this);
109     m_toolbar->addWidget(m_btnreturn);
110     m_toolbar->addWidget(m_btnopen);
111     m_toolbar->addWidget(m_titleLabel);
112 
113 
114     setCentralWidget(m_listWid);
115     addToolBar(m_toolbar);
116     setStyleSheet("background-color:rgb(204,228,247)");
117     setWindowState(Qt::WindowMaximized);
118     setWindowFlags(Qt::FramelessWindowHint);
119 }
120 
getFiles(const QString & path)121 QFileInfoList QfmWindow::getFiles(const QString &path)
122 {
123     QDir *dir = new QDir(path);
124     QFileInfoList files;
125 
126     switch (m_Filter) {
127     case FileAll:
128         files = dir->entryInfoList(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
129         break;
130     case FileVideo:
131         files = dir->entryInfoList(m_videoSuffixList, QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
132         break;
133     case FileMusic:
134         files = dir->entryInfoList(m_musicSuffixList, QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
135         break;
136     case FilePic:
137         files = dir->entryInfoList(m_picSuffixList, QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
138         break;
139     }
140 
141     for(int i=0;i<files.count();i++){
142         QMimeDatabase db;
143         QMimeType type = db.mimeTypeForFile(files[i].fileName());
144         qDebug() << files[i].fileName() << "mime type: " << type.name() << "dir: " << files[i].isDir();
145     }
146 
147     return files;
148 }
149 
updatelabel(void)150 void QfmWindow::updatelabel(void)
151 {
152     if(m_curDir.compare("top")){
153         m_titleLabel->setText(m_curDir);
154      }else {
155         m_titleLabel->setText(tr(FILEMANAGER));
156     }
157 }
158 
updatecurdir(QString path,bool back)159 void QfmWindow::updatecurdir(QString path, bool back)
160 {
161     if(istop(m_curDir)){
162         for(int i = 0; i < m_topDirList.count(); i++){
163             if(!m_topDirList.at(i).compare(path)){
164                 m_curDir = m_topPathList.at(i);
165             }
166         }
167     }else if(inTopList(m_curDir)){
168         if(back){
169              m_curDir = "top";
170              return;
171         }
172         if(m_curDir.compare("/")){
173             m_curDir = m_curDir + '/' + path;
174         }else {
175             m_curDir = '/' + path;
176         }
177     }else {
178         if(back){
179             int index = m_curDir.lastIndexOf('/');
180 
181             if(index == 0){
182                 m_curDir = "/";
183             }else {
184                 m_curDir = m_curDir.mid(0, index);
185             }
186         }else {
187             if(m_curDir.compare("/")){
188                 m_curDir = m_curDir + '/' + path;
189             }else {
190                 m_curDir = '/' + path;
191             }
192         }
193     }
194 }
195 
getitem(QString name)196 QListWidgetItem* QfmWindow::getitem(QString name)
197 {
198     QListWidgetItem *item = new QListWidgetItem();
199     item->setText(name);
200     item->setTextAlignment(Qt::AlignJustify);
201     item->setTextColor(QColor(Qt::black));
202     item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
203     item->setCheckState(Qt::Unchecked);
204     return item;
205 }
206 
getlist(QListWidget * listWid,QListWidgetItem * item)207 void QfmWindow::getlist(QListWidget *listWid, QListWidgetItem *item)
208 {
209     QFileInfoList files;
210     bool intop = istop(m_curDir);
211     int row = listWid->row(item);
212     QString path;// = item->text();
213 
214     if(item){
215         path = item->text();
216     }
217 
218     listWid->clear();
219     if(intop){
220         if(item){
221             path = m_topPathList.at(row);
222             files = getFiles(path);
223             if(files.empty()){
224                 return;
225             }
226 
227             for(int i = 0; i < files.count(); ++i){
228                 listWid->addItem(getitem(files[i].fileName()));
229             }
230         }else{
231             for(int i = 0; i < m_topDirList.size(); ++i){
232                 listWid->addItem(getitem(m_topDirList.at(i)));
233             }
234         }
235 
236     }else {
237         if(inTopList(m_curDir)){
238             if(m_curDir.compare("/")){
239                 files = getFiles(m_curDir + '/' + path);
240             }else{
241                 files = getFiles('/' + path);
242             }
243 
244             if(files.empty()){
245                 return;
246             }
247 
248             for(int i = 0; i < files.count(); ++i){
249                 listWid->addItem(getitem(files[i].fileName()));
250             }
251         }else {
252             if(m_curDir.compare("/")){
253                 files = getFiles(m_curDir + '/' + path);
254             }else{
255                 files = getFiles('/' + path);
256             }
257 
258             if(files.empty()){
259                 return;
260             }
261 
262             for(int i = 0; i < files.count(); ++i){
263                 listWid->addItem(getitem(files[i].fileName()));
264             }
265         }
266 
267     }
268 
269 }
270 
istop(QString path)271 bool QfmWindow::istop(QString path)
272 {
273     if(! path.compare("top")){
274         return true;
275     }
276 
277     return false;
278 }
279 
inTopList(QString path)280 bool QfmWindow::inTopList(QString path)
281 {
282     for(int i = 0; i < m_topPathList.count(); i++){
283         if(!m_topPathList.at(i).compare(path)){
284             return true;
285         }
286     }
287     return false;
288 }
289 
getCheckedItemCnt(void)290 int QfmWindow::getCheckedItemCnt(void)
291 {
292     int cnt = 0;
293     for(int i=0; i < m_listWid->count(); i++){
294         QListWidgetItem * ii = m_listWid->item(i);
295         if(ii->checkState() == Qt::Checked){
296             cnt++;
297         }
298     }
299     return cnt;
300 }
301 
on_openClicked()302 void QfmWindow::on_openClicked()
303 {
304     QStringList files;
305     bool gogogo = false;
306 
307     if(! istop(m_curDir)){
308         for(int i=0; i < m_listWid->count(); i++){
309             QListWidgetItem * item = m_listWid->item(i);
310             if(item->checkState() == Qt::Checked){
311                 QString path = m_curDir + "/" + item->text();
312                 QFileInfo file(path);
313                 if(file.exists()){
314                     gogogo = true;
315                     files.append(file.absoluteFilePath());
316                 }
317             }
318         }
319     }
320     if(gogogo){
321         m_mimeUtils.openFiles(files);
322     }
323 }
324 
on_returnClicked()325 void QfmWindow::on_returnClicked()
326 {
327     if(istop(m_curDir)){
328         qApp->exit(0);
329     }else{
330         updatecurdir(nullptr, true);
331         getlist(m_listWid, nullptr);
332         updatelabel();
333     }
334 }
335 
on_itemClicked(QListWidgetItem * item)336 void QfmWindow::on_itemClicked(QListWidgetItem *item)
337 {
338     if(item->checkState() == Qt::Checked){
339         m_btnopen->show();
340     }
341     int checkedcnt = getCheckedItemCnt();
342     if(checkedcnt >= 1){
343         return;
344     }
345     if(! istop(m_curDir)){
346         QString path = m_curDir + "/" + item->text();
347         QDir dir(path);
348         if(! dir.exists()){
349             m_mimeUtils.openInApp(path, "");
350             return;
351         }
352     }
353     QString path = item->text();
354     getlist(m_listWid, item);
355     updatecurdir(path, false);
356     updatelabel();
357 }
358 
359