~lubuntu-dev/lxde/libfm-qt-debian-git

« back to all changes in this revision

Viewing changes to src/filesearchdialog.cpp

  • Committer: Alf Gaida
  • Date: 2015-12-17 15:45:00 UTC
  • Revision ID: git-v1:99d4cf5e0b3761023e2285ffb96a79d050f0bdf4
Tags: upstream/0.10.0+20151214
Adding upstream version 0.10.0+20151214.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015  Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 */
 
19
 
 
20
#include "filesearchdialog.h"
 
21
#include <QMessageBox>
 
22
#include "fm-search.h"
 
23
#include "ui_filesearch.h"
 
24
#include <limits>
 
25
#include <QFileDialog>
 
26
 
 
27
namespace Fm {
 
28
 
 
29
FileSearchDialog::FileSearchDialog(QStringList paths, QWidget* parent, Qt::WindowFlags f):
 
30
  QDialog(parent, f),
 
31
  ui(new Ui::SearchDialog()) {
 
32
  ui->setupUi(this);
 
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);
 
37
  }
 
38
 
 
39
  ui->maxTime->setDate(QDate::currentDate());
 
40
  ui->minTime->setDate(QDate::currentDate());
 
41
 
 
42
  connect(ui->addPath, &QPushButton::clicked, this, &FileSearchDialog::onAddPath);
 
43
  connect(ui->removePath, &QPushButton::clicked, this, &FileSearchDialog::onRemovePath);
 
44
}
 
45
 
 
46
FileSearchDialog::~FileSearchDialog() {
 
47
  delete ui;
 
48
}
 
49
 
 
50
void FileSearchDialog::accept() {
 
51
  // build the search:/// uri
 
52
  int n = ui->listView->count();
 
53
  if(n > 0) {
 
54
    FmSearch* search = fm_search_new();
 
55
    int i;
 
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());
 
59
    }
 
60
 
 
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());
 
66
 
 
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());
 
70
 
 
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[] = {
 
84
        "application/pdf",
 
85
        /* "text/html;" */
 
86
        "application/vnd.oasis.opendocument.*",
 
87
        "application/vnd.openxmlformats-officedocument.*",
 
88
        "application/msword;application/vnd.ms-word",
 
89
        "application/msexcel;application/vnd.ms-excel"
 
90
      };
 
91
      for(i = 0; i < sizeof(doc_types)/sizeof(char*); ++i)
 
92
        fm_search_add_mime_type(search, doc_types[i]);
 
93
    }
 
94
 
 
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);
 
100
    }
 
101
 
 
102
    if(ui->smallerThan->isChecked()) {
 
103
      guint64 size = ui->maxSize->value() * unit_bytes[ui->maxSizeUnit->currentIndex()];
 
104
      fm_search_set_min_size(search, size);
 
105
    }
 
106
 
 
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());
 
110
    }
 
111
    if(ui->laterThan->isChecked()) {
 
112
      fm_search_set_min_mtime(search, ui->minTime->date().toString(QStringLiteral("yyyy-MM-dd")).toUtf8().constData());
 
113
    }
 
114
 
 
115
    searchUri_.take(fm_search_dup_path(search));
 
116
 
 
117
    fm_search_free(search);
 
118
  }
 
119
  else {
 
120
    QMessageBox::critical(this, tr("Error"), tr("You should add at least add one directory to search."));
 
121
    return;
 
122
  }
 
123
  QDialog::accept();
 
124
}
 
125
 
 
126
void FileSearchDialog::onAddPath() {
 
127
  QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder"));
 
128
  if(dir.isEmpty())
 
129
    return;
 
130
  // avoid adding duplicated items
 
131
  if(ui->listView->findItems(dir, Qt::MatchFixedString|Qt::MatchCaseSensitive).isEmpty()) {
 
132
    ui->listView->addItem(dir);
 
133
  }
 
134
}
 
135
 
 
136
void FileSearchDialog::onRemovePath() {
 
137
  // remove selected items
 
138
  Q_FOREACH(QListWidgetItem* item, ui->listView->selectedItems()) {
 
139
    delete item;
 
140
  }
 
141
}
 
142
 
 
143
}