-
Notifications
You must be signed in to change notification settings - Fork 10
/
mainwindow.cpp
351 lines (316 loc) · 9.08 KB
/
mainwindow.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
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(QSize(800,500));
client=new Client();
connectDlg = new ConnectDlg(this);
contextHelp = new ContextHelp(statusBar(), this);
setupPanels();
buildMenu();
console->setFocus();
setTitle("[disconnected]");
setWindowIcon(QIcon(":/icons/terminal.png"));
isConsoleMode = false;
isConnected = false;
showAbout();
}
void MainWindow::openConnectionDialog()
{
if(connectDlg->exec() == QDialog::Accepted)
{
closeConnection();
if(openConnection(connectDlg->getHost(), connectDlg->getPort(), connectDlg->getDbNumber(), connectDlg->getPrefix()))
updateKeys();
}
}
bool MainWindow::openConnection(QString host, QString port, QString dbNumber, QString prefix)
{
if(!client->connect(host, port.toInt()))
{
QMessageBox::critical(this, "Error", client->getErrorString());
isConnected = false;
return false;
}
QStringList selectResult = client->query(QString("SELECT %1").arg(dbNumber));
if(selectResult.at(0) != "string" || selectResult.at(1) != "OK")
{
QMessageBox::critical(this, "Error", QString("Can't select database %1").arg(dbNumber));
closeConnection();
return false;
}
this->host = host;
this->port = port;
this->dbNumber = dbNumber;
this->prefix = prefix;
isConnected = true;
filter->setText("");
if(!prefix.isEmpty())
setTitle(QString("[%1:%2,db%3#%4]").arg(host).arg(port).arg(dbNumber).arg(prefix));
else
setTitle(QString("[%1:%2,db%3]").arg(host).arg(port).arg(dbNumber));
return true;
}
void MainWindow::updateKeys()
{
if(!checkConnection())
return;
QList<QListWidgetItem *> list = keys->selectedItems();
QString oldKey = "";
if(!list.isEmpty())
oldKey = list.at(0)->text();
QStringList response=client->query(QString("KEYS \"%1*\"").arg(prefix));
keys->clear();
QString key;
QListWidgetItem *item;
for(int i=1;i<response.length();i++)
{
key = response.at(i);
item = new QListWidgetItem(QIcon(":/icons/key.png"), key);
keys->addItem(item);
if(!oldKey.isEmpty() && oldKey == key)
item->setSelected(true);
if(filter->text().isEmpty() || item->text().startsWith(filter->text()))
item->setHidden(false);
else
item->setHidden(true);
}
keys->sortItems();
}
void MainWindow::setupPanels()
{
keys = new QListWidget;
values = new QPlainTextEdit;
console = new Console;
filter = new QLineEdit;
values->setTabStopWidth(8);
values->setReadOnly(true);
filter->setPlaceholderText("quick key filter");
QFont font;
font.setFamily("Courier New");
font.setPointSize(10);
font.setStyleStrategy(QFont::NoAntialias);
keys->setFont(font);
values->setFont(font);
console->setFont(font);
filter->setFont(font);
QWidget *keysPanel = new QWidget;
QVBoxLayout *keysPanelLayout = new QVBoxLayout;
keysPanelLayout->addWidget(filter);
keysPanelLayout->addWidget(keys);
keysPanelLayout->setMargin(0);
keysPanelLayout->setSpacing(0);
keysPanel->setLayout(keysPanelLayout);
QSplitter *horizontal = new QSplitter(Qt::Horizontal, this);
horizontal->addWidget(keysPanel);
horizontal->addWidget(values);
horizontal->setStretchFactor(0, 1);
horizontal->setStretchFactor(1, 3);
vertical = new QSplitter(Qt::Vertical, this);
vertical->addWidget(horizontal);
vertical->addWidget(console);
QList<int> list;
list << 200 << 100;
vertical->setSizes(list);
setCentralWidget(vertical);
connect(keys, SIGNAL(itemSelectionChanged()), this, SLOT(onKeySelect()));
connect(console, SIGNAL(onCommand(QString)), this, SLOT(onCommand(QString)));
connect(filter, SIGNAL(textChanged(QString)), this, SLOT(onFilter(QString)));
connect(console, SIGNAL(onChange(QString)), contextHelp, SLOT(onUserInput(QString)));
}
void MainWindow::buildMenu()
{
QMenu *fileMenu = menuBar()->addMenu("&File");
fileMenu->addAction("&Open Connection", this, SLOT(openConnectionDialog()), QKeySequence("Ctrl+O"));
fileMenu->addAction("&Close", this, SLOT(closeConnection()), QKeySequence("Ctrl+W"));
fileMenu->addSeparator();
fileMenu->addAction("&Quit", this, SLOT(close()), QKeySequence("Alt+F4"));
QMenu *viewMenu = menuBar()->addMenu("&View");
viewMenu->addAction("&Update", this, SLOT(updateKeys()), QKeySequence("F5"));
viewMenu->addSeparator();
viewMenu->addAction("Server &Info", this, SLOT(showServerInfo()));
viewMenu->addSeparator();
consoleModeAction = viewMenu->addAction("&Console Mode", this, SLOT(consoleMode()), QKeySequence("F4"));
consoleModeAction->setCheckable(true);
QMenu *helpMenu = menuBar()->addMenu("&Help");
helpMenu->addAction("Online Reference", this, SLOT(showReference()), QKeySequence("Shift+F1"));
helpMenu->addSeparator();
helpMenu->addAction("&About", this, SLOT(showAbout()), QKeySequence("F1"));
}
void MainWindow::closeConnection()
{
if(client->isConnected())
{
client->disconnect();
}
isConnected = false;
keys->clear();
setTitle("[disconnected]");
}
void MainWindow::setTitle(QString title)
{
setWindowTitle("RedisConsole " + title);
}
void MainWindow::onCommand(QString cmd)
{
if(!checkConnection())
{
console->output("(error) no connection");
return;
}
QStringList response;
response=client->query(cmd);
if(response.isEmpty())
{
console->output("(error) empty response");
return;
}
QString type=response.at(0);
if(type=="string")
console->output(response.at(1));
else if(type=="error")
console->output("(error) "+response.at(1));
else if(type=="integer")
console->output("(integer) "+response.at(1));
else if(type=="bulk")
console->output("\""+response.at(1)+"\"");
else if(type=="list"){
QStringList output;
if(response.length()>1)
{
for(int k=1;k<response.length();k++)
output << QString("%1").arg(k)+". \""+response.at(k)+"\"";
console->output(output.join("\r\n"));
}
else
console->output("empty");
}
}
void MainWindow::showServerInfo()
{
if(!checkConnection())
return;
QStringList response = client->query("INFO");
QString type = response.at(0);
QString output;
if(type == "bulk")
output = "SERVER INFO:\r\n" + response.at(1);
else
output = "(none)";
values->setPlainText(output);
}
void MainWindow::onKeySelect()
{
QList<QListWidgetItem *> list = keys->selectedItems();
if(list.isEmpty())
{
values->setPlainText("");
return;
}
if(!checkConnection())
return;
QString key = list.at(0)->text();
QStringList response;
response = client->query("type " + key);
QString type = response.at(1);
QString output;
if(type == "none")
output = "(none)";
else if(type == "string")
{
response = client->query("get " + key);
output = QString("string \"%1\"").arg(response.at(1));
}
else if(type == "list")
{
response = client->query("lrange " + key + " 0 -1");
output = QString("list[%1] {\r\n").arg(response.length() - 1);
for(int i = 1; i < response.length(); i++)
{
output.append(QString("\t \"%1\"\r\n").arg(response.at(i)));
}
output.append("}");
}
else if(type == "set")
{
response = client->query("smembers " + key);
output = QString("set[%1] {\r\n").arg(response.length() - 1);
for(int i = 1; i < response.length(); i++)
{
output.append(QString("\t \"%1\"\r\n").arg(response.at(i)));
}
output.append("}");
}
else if(type == "zset")
{
response = client->query("zrange " + key + " 0 -1");
output = QString("zset[%1] {\r\n").arg(response.length() - 1);
for(int i = 1; i < response.length(); i++)
{
output.append(QString("\t \"%1\"\r\n").arg(response.at(i)));
}
output.append("}");
}
else if(type == "hash")
{
response = client->query("hvals " + key);
output = QString("hash[%1] {\r\n").arg(response.length() - 1);
for(int i = 1; i < response.length(); i++)
{
output.append(QString("\t \"%1\"\r\n").arg(response.at(i)));
}
output.append("}");
}
values->setPlainText(output);
}
void MainWindow::consoleMode()
{
QList<int> list;
if(!isConsoleMode)
list << 0 << 300;
else
list << 200 << 100;
vertical->setSizes(list);
console->setFocus();
console->scrollDown();
isConsoleMode = ! isConsoleMode;
consoleModeAction->setChecked(isConsoleMode);
}
void MainWindow::showAbout()
{
QFile file(":/about.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
values->setPlainText(in.readAll());
}
void MainWindow::showReference()
{
QDesktopServices::openUrl(QUrl("http://redis.io/commands"));
}
bool MainWindow::checkConnection()
{
if(isConnected)
{
if(!client->isConnected())
if(!openConnection(host, port, dbNumber, prefix))
{
closeConnection();
return false;
}
return true;
}
return false;
}
void MainWindow::onFilter(QString filter)
{
QListWidgetItem *item;
for(int i = 0; i < keys->count(); i++)
{
item = keys->item(i);
if(filter.isEmpty() || item->text().startsWith(filter))
item->setHidden(false);
else
item->setHidden(true);
}
}