aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core-utils/lumina-search/MainUI.cpp
blob: 363d788e7286d3be885ad0a50a1738f03323283a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2014, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "MainUI.h"
#include "ui_MainUI.h"

#include "ConfigUI.h"

MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI){
  ui->setupUi(this); //load the designer file
  //setupIcons();
  ui->radio_apps->setChecked(true); //always default to starting here
  ui->tool_stop->setVisible(false); //no search running initially
  ui->tool_configure->setVisible(false); //app search initially set
	
  livetime = new QTimer(this);
    livetime->setInterval(300); //1/3 second for live searches
    livetime->setSingleShot(true);
    
  workthread = new QThread(this);
	workthread->setObjectName("Lumina Search Process");
	
  searcher = new Worker();
    searcher->moveToThread(workthread);
	
  closeShort = new QShortcut(QKeySequence(tr("Esc")), this);
	
  //Setup the connections
  connect(livetime, SIGNAL(timeout()), this, SLOT(startSearch()) );
  connect(this, SIGNAL(SearchTerm(QString, bool)), searcher, SLOT(StartSearch(QString, bool)) );
  connect(searcher, SIGNAL(FoundItem(QString)), this, SLOT(foundSearchItem(QString)) );
  connect(searcher, SIGNAL(SearchUpdate(QString)), this, SLOT(searchMessage(QString)) );
  connect(searcher, SIGNAL(SearchDone()), this, SLOT(searchFinished()) );
  connect(ui->tool_stop, SIGNAL(clicked()), this, SLOT(stopSearch()) );
  connect(ui->push_done, SIGNAL(clicked()), this, SLOT(closeApplication()) );
  connect(ui->push_launch, SIGNAL(clicked()), this, SLOT(LaunchItem()) );
  connect(ui->line_search, SIGNAL(textEdited(QString)), this, SLOT(searchChanged()) );
  connect(ui->line_search, SIGNAL(returnPressed()), this, SLOT(LaunchItem()) );
  connect(ui->radio_apps, SIGNAL(toggled(bool)), this, SLOT(searchTypeChanged()) );
  connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(LaunchItem(QListWidgetItem*)) );
  connect(ui->listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(LaunchItem(QListWidgetItem*)) );
  connect(ui->tool_configure, SIGNAL(clicked()), this, SLOT(configureSearch()) );
  connect(closeShort, SIGNAL(activated()), this, SLOT( close() ) );
  
  //Setup the settings file
  QSettings::setPath(QSettings::NativeFormat, QSettings::UserScope, QDir::homePath()+"/.lumina");
  settings = new QSettings("LuminaDE", "lumina-search",this);
  searcher->startDir = settings->value("StartSearchDir", QDir::homePath()).toString();
  searcher->skipDirs = settings->value("SkipSearchDirs", QStringList()).toStringList();
  updateDefaultStatusTip();
  this->show();
  workthread->start();
  QTimer::singleShot(0,this, SLOT(setupIcons()) );
}

MainUI::~MainUI(){
  workthread->quit();
  workthread->wait();	
}

void MainUI::setupIcons(){
  //Setup the icons
  this->setWindowIcon( LXDG::findIcon("edit-find","") );
  ui->push_launch->setIcon( LXDG::findIcon("quickopen","") );
  ui->push_done->setIcon( LXDG::findIcon("window-close","") );
  ui->tool_stop->setIcon( LXDG::findIcon("dialog-cancel","") );
  ui->tool_configure->setIcon( LXDG::findIcon("configure","") );
}

// ===============
//    PUBLIC FUNCTIONS (for input handling primarily)
// ===============
void MainUI::disableExcludes(){
  searcher->skipDirs.clear();
  updateDefaultStatusTip();
}

void MainUI::setSearchDirectory(QString path){
  ui->radio_files->setChecked(true);
  searcher->startDir = path;
  updateDefaultStatusTip();
}

void MainUI::setSearchTerm(QString text){
  ui->line_search->setText(text);
}

//==============
//  PRIVATE
//==============
void MainUI::updateDefaultStatusTip(){
  if(ui->radio_files->isChecked()){
    QString txt = tr("Search: %1 -- Smart: %2");
    QString dir = searcher->startDir;
      dir.replace(QDir::homePath(), "~");
    QString smart = searcher->skipDirs.isEmpty() ? tr("Off"): tr("On");
    txt = txt.arg(dir,smart);
    ui->statusbar->showMessage(txt);
  }
}

//==============
//  PRIVATE SLOTS
//==============
void MainUI::LaunchItem(){
int index = ui->listWidget->currentRow();
if(index<0 && ui->listWidget->count()>0){ index = 0; } //grab the first item instead
else if(index<0){ return; } //no items available/selected
QString item = ui->listWidget->item(index)->whatsThis();
QProcess::startDetached("lumina-open \""+item+"\"");
//Close the search utility if an application was launched (quick launch functionality)
if(ui->radio_apps->isChecked()){ this->close(); }
}

void MainUI::LaunchItem(QListWidgetItem *item){
    QProcess::startDetached("lumina-open \""+item->whatsThis()+"\"");
}

void MainUI::searchTypeChanged(){
  startSearch();	
}
	
void MainUI::configureSearch(){
  ConfigUI dlg(this);
    dlg.loadInitialValues( settings->value("StartSearchDir",QDir::homePath()).toString(), settings->value("SkipSearchDirs",QStringList()).toStringList());
    dlg.exec();
  if(dlg.newStartDir.isEmpty()){ return; }//cancelled
  QString startdir = dlg.newStartDir;
  QStringList skipdirs = dlg.newSkipDirs;
	
  //Save these values for later (if selected)
  if(dlg.newDefaults){
    //save these values as the new defaults
    settings->setValue("StartSearchDir", startdir);
    settings->setValue("SkipSearchDirs", skipdirs);
  }
  
  //Set these values in the searcher
  searcher->startDir = startdir;
  searcher->skipDirs = skipdirs;
  updateDefaultStatusTip();
}

void MainUI::searchChanged(){
  if(livetime->isActive()){ livetime->stop(); }
  livetime->start();
}
	
//Worker Interaction
void MainUI::startSearch(){
  ui->listWidget->clear();
  stopSearch(); //just in case a search is still running
  if(ui->line_search->text().isEmpty()){ updateDefaultStatusTip(); return; } //nothing to search for
  
  //emit the proper signal for the worker
  if(!workthread->isRunning()){ workthread->start(); } //make sure the thread is running
  emit SearchTerm(ui->line_search->text(), ui->radio_apps->isChecked());
  ui->tool_stop->setVisible(true);
  ui->tool_configure->setVisible(false);
}

void MainUI::foundSearchItem(QString path){
   //To get the worker's results
  QListWidgetItem *it = new QListWidgetItem;
    it->setWhatsThis(path);
    it->setToolTip(path);
  //Now setup the visuals
  if(path.simplified().endsWith(".desktop")){
    bool ok = false;
    XDGDesktop desk = LXDG::loadDesktopFile(path,ok);
    if( !ok  || !LXDG::checkValidity(desk) ){delete it; return; } //invalid file
    it->setText(desk.name);
    it->setIcon( LXDG::findIcon(desk.icon, "application-x-desktop") );
  }else{
    if(QFileInfo(path).isDir()){ 
      it->setIcon( LXDG::findIcon("inode-directory","") );
      it->setText( path.replace(QDir::homePath(), "~") );
    }else{ 
      if(QFileInfo(path).isExecutable()){
	it->setIcon( LXDG::findIcon("application-x-executable","") ); 
      }else{
        it->setIcon( LXDG::findMimeIcon(path.section("/",-1).section(".",-1)) ); 
      }
      it->setText( path.section("/",-1) );
    }
    
  }
  //Now add it to the widget
  ui->listWidget->addItem(it);
}

void MainUI::stopSearch(){
  searcher->StopSearch();
  ui->tool_stop->setVisible(false);
  ui->tool_configure->setVisible(ui->radio_files->isChecked());
  updateDefaultStatusTip();
}

void MainUI::searchMessage(QString msg){
  ui->statusbar->showMessage(msg,2000);
}

void MainUI::searchFinished(){
  ui->tool_stop->setVisible(false);
  ui->tool_configure->setVisible(ui->radio_files->isChecked());
  updateDefaultStatusTip();
}
bgstack15