-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcut.cpp
424 lines (337 loc) · 11.5 KB
/
shortcut.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
#include <QtGui>
#include <QLocalServer>
#include <QLocalSocket>
#include <QRegExp>
#include "shortcut.h"
#include "shortcutlist.h"
#include "interaction.h"
#include "registryeditor.h"
#include "constants.h"
const QString Shortcut::Connection = "shortcutapp";
Shortcut::Shortcut(int argc, char* argv[]): QMainWindow(0){
QDir::addSearchPath("icons",RegistryEditor::getShortcutAppPath() + "/graphic");
shortcutList = new ShortcutList();
QIcon icon = QIcon("icons:logo.png");
interaction = new Interaction(this, icon);
if(argc == 2){
QString path = argv[1];
// if the second argument is -removeAll then clean the data
if (path.compare("-removeAll") == 0){
shortcutList->removeAll();
}else
stealthInteraction(path);
return;
}
// the GUI works so run the server,
createWindow();
setUpServer();
}
Shortcut::~Shortcut(){
delete shortcutList;
delete interaction;
}
void Shortcut::createWindow(){
QIcon icon("icons:logo.png");
setWindowIcon(icon);
setWindowTitle(SOFTWARE);
window = new QWidget();
setCentralWidget(window);
createWidgets();
loadListContent();
setMinimumSize(500,300);
show();
}
void Shortcut::createWidgets(){
QVBoxLayout *mainLayout = new QVBoxLayout;
setUpCentralList();
mainLayout->addWidget(shortcutTable);
mainLayout->addLayout(setUpBottomPanel());
window->setLayout(mainLayout);
}
QHBoxLayout* Shortcut::setUpBottomPanel(){
QHBoxLayout *bottomPanel = new QHBoxLayout;
editBut = new QPushButton("Edit Shortcut");
delBut = new QPushButton("Delete");
exitBut = new QPushButton("Exit");
searchBox = new QLineEdit;
searchBox->setFocus();
searchBox->setMaximumWidth(120);
QLabel *searchLabel = new QLabel(tr("Search:"));
bottomPanel->addWidget(searchLabel);
bottomPanel->addWidget(searchBox);
bottomPanel->addStretch(1);
bottomPanel->addWidget(editBut);
bottomPanel->addWidget(delBut);
bottomPanel->addWidget(exitBut);
bottomPanel->setAlignment(Qt::AlignLeft);
connect(delBut,SIGNAL(clicked()),this, SLOT(slotDeleteShortcut()));
connect(exitBut,SIGNAL(clicked()),this, SLOT(slotExit()));
connect(editBut,SIGNAL(clicked()),this, SLOT(slotEditShortcutButton()));
connect(searchBox,SIGNAL(textEdited(QString)),this,SLOT(slotFind()));
connect(searchBox,SIGNAL(textChanged(QString)),this,SLOT(slotSearchBoxTextChanged()));
return bottomPanel;
}
void Shortcut::setUpCentralList(){
shortcutTable = new QTableWidget(0,3);
QStringList columneTitles;
columneTitles << "Type" << "Shortcut" <<"Path";
shortcutTable->setHorizontalHeaderLabels(columneTitles);
shortcutTable->setColumnWidth(0,40);
shortcutTable->setColumnWidth(1,100);
shortcutTable->setColumnWidth(2,300);
shortcutTable->setSelectionBehavior(QAbstractItemView::SelectRows);
shortcutTable->setSelectionMode(QAbstractItemView::SingleSelection);
shortcutTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(shortcutTable,SIGNAL(itemDoubleClicked(QTableWidgetItem*)),
this, SLOT(slotEditShortcutTable(QTableWidgetItem*)));
}
void Shortcut::loadListContent(){
shortcutTable->clearContents();
shortcutTable->setRowCount(shortcutList->getShortcutCount());
QTableWidgetItem *shortcut;
QTableWidgetItem *path;
QTableWidgetItem *type;
for (int i = 0 ; i<shortcutTable->rowCount() ; i++){
type = new QTableWidgetItem;
shortcut = new QTableWidgetItem;
path = new QTableWidgetItem;
type->setText("File");
shortcut->setText(shortcutList->getShortcut(i));
path->setText(shortcutList->getPath(i));
shortcutTable->setItem(i,0,type);
shortcutTable->setItem(i,1, shortcut);
shortcutTable->setItem(i,2, path);
}
}
void Shortcut::slotCreateMenu()
{
aboutAction = new QAction(tr("&About"),this);
aboutAction->setShortcut(tr("Ctrl + A"));
aboutAction->setStatusTip(tr("More about the application"));
QLabel label("my label");
label.setAlignment(Qt::AlignCenter);
label.setMinimumSize(label.sizeHint());
statusBar()->addPermanentWidget(&label);
statusBar()->show();
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
}
void Shortcut::slotShortcutListChanged(){
// the connection to the server has been made, so the
// file with shortcuts must be reloaded and displayed
// which means that the QTableWidget must be refreshed
shortcutList->readFileShortcuts();
loadListContent();
}
void Shortcut::keyPressEvent(QKeyEvent *event){
if (event->key() != Qt::Key_Return)
return;
// return if the search box has no focus
if(!searchBox->hasFocus())
return;
QModelIndexList index = shortcutTable->selectionModel()->selectedIndexes();
if (index.size() == 0)
return;
slotEditShortcutButton();
}
void Shortcut::slotSearchBoxTextChanged(){
if (searchBox->text() != ""){
return;
}
shortcutTable->setCurrentCell(-1,-1);
}
void Shortcut::stealthInteraction(QString path){
// using the socket we will inform the main application the shortcuts have changed
setUpSocket();
int pathIndex = shortcutList->indexOfItemWithPath(path);
// if there exist a shortcut to the file ask if the user wants to delete it
bool toDelete = false;
if (pathIndex != -1)
toDelete = interaction->msgDoYouWantToDelete
(path,shortcutList->getShortcut(pathIndex));
if (toDelete){
shortcutList->delShortcut(pathIndex);
signalServer();
return;
}
// do not allow creating shortcuts if one exists
else if(toDelete == false && pathIndex != -1)
return;
// if the user inputs a wrong shortcut, ask again
QString shortcut;
do{
shortcut = interaction->askForShortcut(this,path);
if (!containsLetterAndNumbersOnly(shortcut)){
interaction->shortcutIncorrect();
continue;
}
break;
}while(true);
if (shortcut.compare("abort") == 0)
return;
bool shortcutExist = shortcutList->getIndex(shortcut);
int answer = 0;
if (shortcutExist == true)
answer = interaction->msgShortcutExists();
// the yes answer means that the shortcut should be replaced
int addedSuccessfully;
if (answer == QMessageBox::Yes || answer == 0){
addedSuccessfully = shortcutList->addShortcut(shortcut, path);
}
else if(answer == QMessageBox::Cancel)
return;
if (addedSuccessfully == 0){
interaction->msgShortcutAdded(shortcut);
signalServer();
return;
}else if(addedSuccessfully == -1){
interaction->msgShortcutError(shortcut);
return;
}
// is this needed here?
close();
}
/*
This function starts a server that waits for signals from other proccesses
of this application.
If anyone connects to it, it means that the shortcut list was updated.
*/
bool Shortcut::setUpServer(){
server = new QLocalServer(this);
if (!server->listen(Connection)){
QMessageBox::critical(this,tr("Synchronization problem"),
tr("Unable to start server : %1.")
.arg(server->errorString()));
close();
return false;
}
connect(server,SIGNAL(newConnection()),this, SLOT(slotShortcutListChanged()));
return true;
}
/*
Create a socket but do not make a connection.
*/
void Shortcut::setUpSocket(){
socket = new QLocalSocket(this);
}
void Shortcut::signalServer(){
socket->abort();
socket->connectToServer(Connection);
}
/*
Check if the input contains number and letter only.
*/
bool Shortcut::containsLetterAndNumbersOnly(QString name){
QRegExp exp("[a-zA-Z0-9]*");
return exp.exactMatch(name);
}
void Shortcut::slotFind(){
if (shortcutTable->rowCount() == 0)
return;
for (int i = 0 ; i < shortcutTable->rowCount() ; i++ ){
QTableWidgetItem *item = shortcutTable->item(i,1);
// if the item in the table is shorter then the
// item that the user is looking for,
// then no element should be selected
if(searchBox->text().length() > item->text().length()){
shortcutTable->setCurrentCell(-1,-1);
return;
}
else if (item->text().startsWith(searchBox->text(),Qt::CaseInsensitive)){
shortcutTable->setCurrentItem(item);
return;
}
}
}
void Shortcut::slotEditShortcutButton(){
QModelIndexList index = shortcutTable->selectionModel()->selectedIndexes();
if (index.size() == 0){
interaction->noShortcutSelected();
return;
}
QTableWidgetItem* shortcutName = shortcutTable->item(index.at(0).row(),1);
QTableWidgetItem* shortcutPath= shortcutTable->item(index.at(0).row(),2);
QString path = shortcutPath->text();
QString oldShortcut = shortcutName->text();
// remove " from front and end of the string,
path.remove(0,1);
path.remove(path.length()-1,1);
// if the user inputs a wrong shortcut, ask again
QString shortcut;
do{
shortcut = interaction->askForShortcut(this,oldShortcut,true);
if (!containsLetterAndNumbersOnly(shortcut)){
interaction->shortcutIncorrect();
continue;
}
break;
}while(true);
if (shortcut.compare("abort") == 0)
return;
// check if this shortcut exists, inform the user that the shortcut exists,
if (shortcutList->indexOfItemWithShortcut(shortcut) != -1){
interaction->msgShortcutInUse();
return;
}
// the yes answer means that the shortcut should be replaced
int addedSuccessfully = shortcutList->replaceItemByPath(path,shortcut);
if (addedSuccessfully){
interaction->msgShortcutAdded(shortcut);
loadListContent();
return;
}else{
interaction->msgShortcutError(shortcut);
return;
}
}
void Shortcut::slotEditShortcutTable(QTableWidgetItem *item){
// if the user inputs a wrong shortcut, ask again
QString shortcut;
do{
shortcut = interaction->askForShortcut(this,
shortcutList->getShortcut(item->row()),true);
if (!containsLetterAndNumbersOnly(shortcut)){
interaction->shortcutIncorrect();
continue;
}
break;
}while(true);
if (shortcut.compare("abort") == 0)
return;
// check if this shortcut exists, inform the user that the shortcut exists,
if (shortcutList->indexOfItemWithShortcut(shortcut) != -1){
interaction->msgShortcutInUse();
return;
}
// the yes answer means that the shortcut should be replaced
QString path = shortcutList->getPath(item->row());
path.remove(0,1);
path.remove(path.length()-1,1);
int addedSuccessfully = shortcutList->replaceItemByPath(path,shortcut);
if (addedSuccessfully){
interaction->msgShortcutAdded(shortcut);
loadListContent();
return;
}else{
interaction->msgShortcutError(shortcut);
return;
}
}
/*
Delete a shortcut from the list and the registry
*/
void Shortcut::slotDeleteShortcut(){
QModelIndexList indexes = shortcutTable->selectionModel()->selectedIndexes();
if (indexes.size() == 0){
interaction->noShortcutSelected();
return;
}
shortcutList->delShortcut(indexes.at(0).row());
shortcutTable->removeRow(indexes.at(0).row());
}
/*
Exits the program
*/
void Shortcut::slotExit(){
close();
}