-
Notifications
You must be signed in to change notification settings - Fork 23
/
commanditemmodel.cpp
476 lines (371 loc) · 12.8 KB
/
commanditemmodel.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Alexander Sokoloff <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "commanditemmodel.h"
#include <LXQt/Globals>
#include <LXQt/Settings>
#include <XdgIcon>
#include <QFileInfo>
#include <QProcess>
#include <QDebug>
#include <limits.h>
/************************************************
************************************************/
CommandItemModel::CommandItemModel(bool useHistory, QObject *parent) :
QSortFilterProxyModel(parent),
mSourceModel(new CommandSourceItemModel(useHistory, this)),
mOnlyHistory(false),
mShowHistoryFirst(true)
{
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
setSourceModel(mSourceModel);
sort(0);
}
/************************************************
************************************************/
CommandItemModel::~CommandItemModel()
{
}
/************************************************
************************************************/
bool CommandItemModel::isOutDated() const
{
return mSourceModel->isOutDated();
}
/************************************************
************************************************/
const CommandProviderItem *CommandItemModel::command(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
QModelIndex ind = mapToSource(index);
return mSourceModel->command(ind);
}
/************************************************
************************************************/
void CommandItemModel::clearHistory()
{
mSourceModel->clearHistory();
}
/************************************************
************************************************/
bool CommandItemModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QRegularExpression re(filterRegularExpression());
if (re.pattern().isEmpty() && !mOnlyHistory)
return false;
const CommandProviderItem *item = mSourceModel->command(sourceRow);
if (!item)
return false;
bool accept = item->compare(re);
if (accept)
{
//check if CustomCommand can be filtered out (equivalent app link is shown)
const CustomCommandItem * cust_i = qobject_cast<const CustomCommandItem *>(item);
if (nullptr != cust_i)
{
for (int i = mSourceModel->rowCount(sourceParent); 0 <= i; --i)
{
const AppLinkItem * app_i = qobject_cast<const AppLinkItem *>(mSourceModel->command(i));
if (nullptr != app_i && cust_i->exec() == app_i->exec() && app_i->compare(re))
return false;
}
}
}
return accept;
}
/************************************************
************************************************/
bool CommandItemModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (left == mSourceModel->customCommandIndex())
return true;
if (right == mSourceModel->customCommandIndex())
return false;
if (mOnlyHistory)
return left.row() < right.row();
const auto leftItem = mSourceModel->command(left);
const auto righItem = mSourceModel->command(right);
HistoryItem const * i_left = dynamic_cast<HistoryItem const *>(leftItem);
HistoryItem const * i_right = dynamic_cast<HistoryItem const *>(righItem);
if (nullptr != i_left && nullptr == i_right)
return mShowHistoryFirst;
if (nullptr == i_left && nullptr != i_right)
return !mShowHistoryFirst;
if (nullptr != i_left && nullptr != i_right)
{
QRegularExpression re(filterRegularExpression());
//Note: -1 should not be returned if the item passed the filter previously
const int pos_left = i_left->command().indexOf(re);
const int pos_right = i_right->command().indexOf(re);
Q_ASSERT(-1 != pos_left && -1 != pos_right);
return pos_left < pos_right
|| (pos_left == pos_right && QSortFilterProxyModel::lessThan(left, right));
}
// compare visible names
if (leftItem != nullptr && righItem != nullptr)
{
int comp = leftItem->title().compare(righItem->title());
if (comp != 0)
return comp < 0;
}
return QSortFilterProxyModel::lessThan(left, right);
}
/************************************************
************************************************/
int CommandItemModel::itemType(const QModelIndex &index) const
{
if (index.row() == mSourceModel->customCommandIndex().row())
return 1;
else if (index.row() < mSourceModel->externalProviderStartIndex().row())
return 2;
else
return 3;
}
/************************************************
************************************************/
QModelIndex CommandItemModel::appropriateItem(const QString &pattern) const
{
QModelIndex res;
unsigned int rank = 0;
int cnt = rowCount();
for (int i=0; i<cnt; ++i)
{
QModelIndex ind = index(i,0);
QModelIndex srcIndex = mapToSource(ind);
if (srcIndex == mSourceModel->customCommandIndex())
return ind;
const CommandProviderItem *item = mSourceModel->command(srcIndex);
if (!item)
continue;
unsigned int r = item->rank(pattern);
if (r > rank)
{
res = ind;
rank = r;
}
if (rank >= MAX_RANK)
break;
}
if (!res.isValid())
res = index(0, 0);
return res;
}
/************************************************
************************************************/
void CommandItemModel::showHistoryFirst(bool first/* = true*/)
{
mShowHistoryFirst = first;
invalidate();
}
/************************************************
************************************************/
void CommandItemModel::rebuild()
{
mSourceModel->rebuild();
}
/************************************************
************************************************/
CommandSourceItemModel::CommandSourceItemModel(bool useHistory, QObject *parent) :
QAbstractListModel(parent),
mHistoryProvider(nullptr)
{
mCustomCommandProvider = new CustomCommandProvider;
mProviders.append(mCustomCommandProvider);
rebuild();
mCustomCommandIndex = index(0, 0);
if (useHistory)
{
mHistoryProvider = new HistoryProvider();
mProviders.append(mHistoryProvider);
mCustomCommandProvider->setHistoryProvider(mHistoryProvider);
}
mProviders.append(new AppLinkProvider());
#ifdef MATH_ENABLED
mProviders.append(new MathProvider());
#endif
#ifdef VBOX_ENABLED
mProviders.append(new VirtualBoxProvider());
#endif
rebuild();
mExternalProviderStartIndex = index(rowCount(), 0);
LXQt::Settings settings(QSL("lxqt-runner"));
int numExternalProviders = settings.beginReadArray(QSL("external providers"));
for (int i = 0; i < numExternalProviders; i++)
{
settings.setArrayIndex(i);
qDebug() << "Adding external provider:" << settings.value(QL1S("name")) << settings.value(QL1S("executable"));
ExternalProvider* externalProvider = new ExternalProvider(settings.value(QL1S("name")).toString(),
settings.value(QL1S("executable")).toString());
mProviders.append(externalProvider);
mExternalProviders.append(externalProvider);
}
settings.endArray();
for(const CommandProvider* provider : std::as_const(mProviders))
{
connect(provider, &CommandProvider::changed, this, [this] { emit layoutChanged(); } );
connect(provider, &CommandProvider::aboutToBeChanged, this, [this] { emit layoutAboutToBeChanged(); } );
}
rebuild();
}
/************************************************
************************************************/
CommandSourceItemModel::~CommandSourceItemModel()
{
qDeleteAll(mProviders);
mHistoryProvider = 0;
}
/************************************************
************************************************/
int CommandSourceItemModel::rowCount(const QModelIndex& /*parent*/) const
{
int ret=0;
for(const CommandProvider* provider : std::as_const(mProviders))
ret += provider->count();
return ret;
}
/************************************************
************************************************/
QVariant CommandSourceItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= rowCount())
return QVariant();
const CommandProviderItem *item = command(index);
if (!item)
return QVariant();
switch (role)
{
case Qt::DisplayRole:
return QString::fromLatin1("<b>%1</b><br>\n%2\n").arg(item->title(), item->comment());
case Qt::DecorationRole:
return item->icon();
case Qt::ToolTipRole:
return item->toolTip();
}
return QVariant();
}
/************************************************
************************************************/
bool CommandSourceItemModel::isOutDated() const
{
QListIterator<CommandProvider*> i(mProviders);
while (i.hasNext())
{
if (i.next()->isOutDated())
return true;
}
return false;
}
/************************************************
************************************************/
void CommandSourceItemModel::rebuild()
{
emit layoutAboutToBeChanged();
// FIXME: is this implementation correct?
QListIterator<CommandProvider*> i(mProviders);
while (i.hasNext())
{
CommandProvider *p = i.next();
if (p->isOutDated())
p->rebuild();
}
emit layoutChanged();
}
/************************************************
************************************************/
void CommandSourceItemModel::clearHistory()
{
if (mHistoryProvider)
{
beginResetModel();
mHistoryProvider->clearHistory();
endResetModel();
} else
{
QScopedPointer<HistoryProvider> history_p{new HistoryProvider};
history_p->clearHistory();
}
}
/************************************************
************************************************/
const CommandProviderItem *CommandSourceItemModel::command(int row) const
{
int n = row;
QListIterator<CommandProvider*> i(mProviders);
while (i.hasNext())
{
CommandProvider *p = i.next();
if (n < p->count())
return p->at(n);
else
n -=p->count();
}
return 0;
}
/************************************************
************************************************/
const CommandProviderItem *CommandSourceItemModel::command(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return command(index.row());
}
/***********************************************
***********************************************/
void CommandSourceItemModel::setCommand(const QString& command)
{
mCustomCommandProvider->setCommand(command);
for (ExternalProvider* const externalProvider : std::as_const(mExternalProviders))
{
externalProvider->setSearchTerm(command);
}
}
/***********************************************
***********************************************/
void CommandSourceItemModel::setUseHistory(bool useHistory)
{
const bool now_using_history = mHistoryProvider != nullptr;
if (now_using_history == useHistory)
return;
beginResetModel();
if (now_using_history)
{
mProviders.removeAll(mHistoryProvider);
mCustomCommandProvider->setHistoryProvider(nullptr);
delete mHistoryProvider;
mHistoryProvider = nullptr;
} else
{
mHistoryProvider = new HistoryProvider;
mProviders.append(mHistoryProvider);
mCustomCommandProvider->setHistoryProvider(mHistoryProvider);
connect(mHistoryProvider, &HistoryProvider::changed, this, [this] { emit layoutChanged(); } );
connect(mHistoryProvider, &HistoryProvider::aboutToBeChanged, this, [this] { emit layoutAboutToBeChanged(); } );
}
endResetModel();
}