Files
2026-03-28 11:39:04 +11:00

187 lines
4.4 KiB
C++

/***************************************************************************************
*
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
*
* By downloading, copying, installing or using the software you agree to this license.
* If you do not agree to this license, do not download, install,
* copy or use the software.
*
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
*
* Redistribution and use in binary forms, with or without modification, are permitted.
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************************/
#include "FileBrowse.h"
#include "ui_FileBrowse.h"
#include "utils.h"
#include "config.h"
#include <QUrl>
#include <QDesktopServices>
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QScreen>
#include <QApplication>
#if defined(ANDROID)
#include <QJniObject>
#elif defined(IOS)
#include <ios/ios_launcher.h>
#endif
FileBrowse::FileBrowse(int fileFormat, QWidget *parent)
: QDialog(parent)
, ui(new Ui::FileBrowse)
, m_bEditing(false)
, m_fileFormat(fileFormat)
{
ui->setupUi(this);
initDialog();
connSignalSlot();
}
FileBrowse::~FileBrowse()
{
delete ui;
}
void FileBrowse::initDialog()
{
QStringList filters;
if (m_fileFormat == FILE_FORMAT_PIC)
{
filters << "*.jpg";
m_basePath = getSnapshotPath();
ui->labTips->setText(tr("Album"));
}
else
{
filters << "*.mp4";
filters << "*.avi";
m_basePath = getRecordPath();
ui->labTips->setText(tr("Video"));
}
ui->labPath->setText(tr("Path") + QString(" : ") + m_basePath);
m_pModel = new FileListModel(m_fileFormat, this);
m_pModel->setNameFilters(filters);
m_pModel->setDirPath(m_basePath);
ui->chkAll->setVisible(false);
ui->fileList->setModel(m_pModel);
showMaximized();
}
void FileBrowse::connSignalSlot()
{
QObject::connect(ui->btnBack, SIGNAL(clicked()), this, SLOT(slotBack()));
QObject::connect(ui->btnEdit, SIGNAL(clicked()), this, SLOT(slotEdit()));
QObject::connect(ui->chkAll, SIGNAL(clicked(bool)), this, SLOT(slotCheckAll(bool)));
QObject::connect(ui->fileList, SIGNAL(clicked(QModelIndex)), this, SLOT(slotItemClicked(QModelIndex)));
}
void FileBrowse::slotBack()
{
close();
}
void FileBrowse::slotEdit()
{
if (!m_bEditing)
{
m_bEditing = true;
ui->chkAll->setVisible(true);
m_pModel->setCheckable(true);
}
else
{
deleteSelectedFiles();
m_bEditing = false;
ui->chkAll->setVisible(false);
m_pModel->setCheckable(false);
}
}
void FileBrowse::slotCheckAll(bool flag)
{
int row = m_pModel->rowCount();
QModelIndex index;
for (int i = 0; i < row; i++)
{
index = m_pModel->index(i);
m_pModel->setData(index, flag, Qt::CheckStateRole);
}
}
void FileBrowse::slotItemClicked(QModelIndex index)
{
if (m_bEditing)
{
int flag = m_pModel->data(index, Qt::CheckStateRole).toInt();
if (flag == Qt::Checked)
{
m_pModel->setData(index, false, Qt::CheckStateRole);
}
else
{
m_pModel->setData(index, true, Qt::CheckStateRole);
}
}
else
{
QString name = m_pModel->data(index, Qt::DisplayRole).toString();
QString path = m_basePath + "/" + name;
#if defined(ANDROID)
QJniObject str = QJniObject::fromString(path);
QJniObject provider = QJniObject::fromString(FILE_PROVIDER);
QJniObject::callStaticMethod<void>("org/happytimesoft/util/HtUtil",
"openFile",
"(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V",
QNativeInterface::QAndroidApplication::context(),
str.object<jstring>(),
provider.object<jstring>());
#elif defined(IOS)
iosLaunchFile(path);
#endif
}
}
void FileBrowse::deleteSelectedFiles()
{
int flag;
int row = m_pModel->rowCount();
QModelIndex index;
for (int i = row - 1; i >= 0; i--)
{
index = m_pModel->index(i);
flag = m_pModel->data(index, Qt::CheckStateRole).toInt();
if (flag == Qt::Checked)
{
QString name = m_pModel->data(index, Qt::DisplayRole).toString();
QString path = m_basePath + "/" + name;
QFile::remove(path);
m_pModel->removeRows(i, 1, index.parent());
}
}
}