2
* Copyright (C) 2015 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "filesearchdialog.h"
21
#include <QMessageBox>
22
#include "fm-search.h"
23
#include "ui_filesearch.h"
25
#include <QFileDialog>
29
FileSearchDialog::FileSearchDialog(QStringList paths, QWidget* parent, Qt::WindowFlags f):
31
ui(new Ui::SearchDialog()) {
33
ui->minSize->setMaximum(std::numeric_limits<int>().max());
34
ui->maxSize->setMaximum(std::numeric_limits<int>().max());
35
Q_FOREACH(const QString& path, paths) {
36
ui->listView->addItem(path);
39
ui->maxTime->setDate(QDate::currentDate());
40
ui->minTime->setDate(QDate::currentDate());
42
connect(ui->addPath, &QPushButton::clicked, this, &FileSearchDialog::onAddPath);
43
connect(ui->removePath, &QPushButton::clicked, this, &FileSearchDialog::onRemovePath);
46
FileSearchDialog::~FileSearchDialog() {
50
void FileSearchDialog::accept() {
51
// build the search:/// uri
52
int n = ui->listView->count();
54
FmSearch* search = fm_search_new();
56
for(i = 0; i < n; ++i) { // add directories
57
QListWidgetItem* item = ui->listView->item(i);
58
fm_search_add_dir(search, item->text().toLocal8Bit().constData());
61
fm_search_set_recursive(search, ui->recursiveSearch->isChecked());
62
fm_search_set_show_hidden(search, ui->searchHidden->isChecked());
63
fm_search_set_name_patterns(search, ui->namePatterns->text().toUtf8().constData());
64
fm_search_set_name_ci(search, ui->nameCaseInsensitive->isChecked());
65
fm_search_set_name_regex(search, ui->nameRegExp->isChecked());
67
fm_search_set_content_pattern(search, ui->contentPattern->text().toUtf8().constData());
68
fm_search_set_content_ci(search, ui->contentCaseInsensitive->isChecked());
69
fm_search_set_content_regex(search, ui->contentRegExp->isChecked());
71
// search for the files of specific mime-types
72
if(ui->searchTextFiles->isChecked())
73
fm_search_add_mime_type(search, "text/plain");
74
if(ui->searchImages->isChecked())
75
fm_search_add_mime_type(search, "image/*");
76
if(ui->searchAudio->isChecked())
77
fm_search_add_mime_type(search, "audio/*");
78
if(ui->searchVideo->isChecked())
79
fm_search_add_mime_type(search, "video/*");
80
if(ui->searchFolders->isChecked())
81
fm_search_add_mime_type(search, "inode/directory");
82
if(ui->searchDocuments->isChecked()) {
83
const char* doc_types[] = {
86
"application/vnd.oasis.opendocument.*",
87
"application/vnd.openxmlformats-officedocument.*",
88
"application/msword;application/vnd.ms-word",
89
"application/msexcel;application/vnd.ms-excel"
91
for(i = 0; i < sizeof(doc_types)/sizeof(char*); ++i)
92
fm_search_add_mime_type(search, doc_types[i]);
95
// search based on file size
96
const unsigned int unit_bytes[] = {1, (1024), (1024*1024), (1024*1024*1024)};
97
if(ui->largerThan->isChecked()) {
98
guint64 size = ui->minSize->value() * unit_bytes[ui->minSizeUnit->currentIndex()];
99
fm_search_set_min_size(search, size);
102
if(ui->smallerThan->isChecked()) {
103
guint64 size = ui->maxSize->value() * unit_bytes[ui->maxSizeUnit->currentIndex()];
104
fm_search_set_min_size(search, size);
107
// search based on file mtime (we only support date in YYYY-MM-DD format)
108
if(ui->earlierThan->isChecked()) {
109
fm_search_set_max_mtime(search, ui->maxTime->date().toString(QStringLiteral("yyyy-MM-dd")).toUtf8().constData());
111
if(ui->laterThan->isChecked()) {
112
fm_search_set_min_mtime(search, ui->minTime->date().toString(QStringLiteral("yyyy-MM-dd")).toUtf8().constData());
115
searchUri_.take(fm_search_dup_path(search));
117
fm_search_free(search);
120
QMessageBox::critical(this, tr("Error"), tr("You should add at least add one directory to search."));
126
void FileSearchDialog::onAddPath() {
127
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder"));
130
// avoid adding duplicated items
131
if(ui->listView->findItems(dir, Qt::MatchFixedString|Qt::MatchCaseSensitive).isEmpty()) {
132
ui->listView->addItem(dir);
136
void FileSearchDialog::onRemovePath() {
137
// remove selected items
138
Q_FOREACH(QListWidgetItem* item, ui->listView->selectedItems()) {