-
Notifications
You must be signed in to change notification settings - Fork 207
/
ryconnectiontableview.cpp
221 lines (189 loc) · 8.28 KB
/
ryconnectiontableview.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
#include "ryconnectiontableview.h"
#include <QTemporaryFile>
#include "quazip/quazip.h"
#include "quazip/quazipfile.h"
#include "rytablesortfilterproxymodel.h"
#include "rymimedata.h"
extern QString appPath;
extern QByteArray gzipDecompress(QByteArray data);
RyConnectionTableView::RyConnectionTableView(QWidget *parent) :
QTableView(parent){
_isMouseDown = false;
this->setDragEnabled(true);
this->setAlternatingRowColors(true);
createMenu();
}
void RyConnectionTableView::contextMenuEvent(QContextMenuEvent *event){
//QModelIndex index = this->indexAt(event->pos());
if(this->selectionModel()->selection().length() > 0){
_saveSelectedSessionAct->setEnabled(true);
_removeSelectedSessionAct->setEnabled(true);
_saveSessionRespnoseBodyAct->setEnabled(true);
}else{
_saveSelectedSessionAct->setEnabled(false);
_removeSelectedSessionAct->setEnabled(false);
_saveSessionRespnoseBodyAct->setEnabled(false);
}
_contextMenu->popup(event->globalPos());
event->accept();
}
void RyConnectionTableView::createMenu(){
_contextMenu = new QMenu(this);
_saveSelectedSessionAct = _contextMenu->addAction(tr("save selected sessions..."));
_saveSessionRespnoseBodyAct = _contextMenu->addAction(tr("&save selected response body..."));
// TODO: finish these actions
_autoScrollAct = _contextMenu->addAction(tr("auto scroll session list"));
_autoScrollAct->setCheckable(true);
_autoScrollAct->setEnabled(false);
_saveMenu = _contextMenu->addMenu(tr("&save"));
_saveMenu->setEnabled(false);
_saveAllSessionAct = _saveMenu->addAction(tr("&save all sessions"));
_saveAllSessionAct->setEnabled(false);
_saveSessionsRevertAct = _saveMenu->addAction(tr("save sessions unselected"));
_saveSessionsRevertAct->setEnabled(false);
_removeMenu = _contextMenu->addMenu(tr("&remove"));
_removeMenu->setEnabled(false);
_removeSelectedSessionAct = _removeMenu->addAction(tr("remove selected session"));
_removeSelectedSessionAct->setEnabled(false);
_removeAllSessionAct = _removeMenu->addAction(tr("remove all sessions"));
_removeAllSessionAct->setEnabled(false);
_removeSessionsRevertAct = _removeMenu->addAction(tr("remove sessions unselected"));
_removeSessionsRevertAct->setEnabled(false);
_contextMenu->connect(_contextMenu,SIGNAL(triggered(QAction*)),this,SLOT(onAction(QAction*)));
}
void RyConnectionTableView::mousePressEvent(QMouseEvent *event){
_isMouseDown = true;
QTableView::mousePressEvent(event);
}
void RyConnectionTableView::mouseReleaseEvent(QMouseEvent *event){
_isMouseDown = false;
QTableView::mouseReleaseEvent(event);
}
void RyConnectionTableView::mouseMoveEvent(QMouseEvent *event){
if(!_isMouseDown){
QTableView::mouseMoveEvent(event);
return;
}
QModelIndex index = this->indexAt(event->pos());
if (!index.isValid()){
return;
}
QPoint hotSpot = event->pos() - this->pos();
RyTableSortFilterProxyModel* model = qobject_cast<RyTableSortFilterProxyModel*>(this->model());
RyPipeData_ptr data = model->getItem(index);
if(data.isNull()){
return;
}
RyMimeData *mimeData = new RyMimeData(data);
mimeData->setText(data->getRequestHeader("Host"));
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
//drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);
drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
}
void RyConnectionTableView::onAction(QAction *action){
if(0 == action){
return;
}
if(action==_saveSelectedSessionAct){
QFileDialog dialog;
QString saveFileName = dialog.getSaveFileName(this);
QuaZip zip(saveFileName);
zip.open(QuaZip::mdCreate);
RyTableSortFilterProxyModel *model = qobject_cast<RyTableSortFilterProxyModel*>(this->model());
QItemSelection selection = this->selectionModel()->selection();
int index=0;
for(int j=0;j<selection.length();++j){
QItemSelectionRange r = selection.at(j);
for(int c=r.top();c<=r.bottom();++c){
index++;
RyPipeData_ptr item = model->getItem(c);
QByteArray baRequest = item->requestHeaderRawData();
baRequest.append("\r\n\r\n");
baRequest.append(item->requestBodyRawData());
QuaZipFile outFileReq(&zip);
if(!outFileReq.open(QIODevice::WriteOnly,
QuaZipNewInfo(
QString("./raw/%1_c.txt").arg(QString::number(index)))
)){
qDebug()<<"error on write zip:"<<QString::number(zip.getZipError());
return;
}
outFileReq.write(baRequest);
outFileReq.close();
QByteArray baResponse = item->responseHeaderRawData();
baResponse.append("\r\n\r\n");
baResponse.append(item->responseBodyRawData());
QuaZipFile outFileRes(&zip);
if(!outFileRes.open(QIODevice::WriteOnly,
QuaZipNewInfo(
QString("./raw/%1_s.txt").arg(QString::number(index)))
)){
qDebug()<<"error on write zip:"<<QString::number(zip.getZipError());
return;
}
outFileRes.write(baResponse);
outFileRes.close();
}
}
zip.close();
}else if(action == _saveSessionRespnoseBodyAct){
QFileDialog dialog;
QString saveDir = dialog.getExistingDirectory(this,tr("save session response body to..."));
if(saveDir.isEmpty()){
return;
}
QDir dir(saveDir);
RyTableSortFilterProxyModel *model = qobject_cast<RyTableSortFilterProxyModel*>(this->model());
QItemSelection selection = this->selectionModel()->selection();
int index=0;
for(int j=0;j<selection.length();++j){
QItemSelectionRange r = selection.at(j);
for(int c=r.top();c<=r.bottom();++c){
index++;
RyPipeData_ptr item = model->getItem(c);
QByteArray baResponse;
if(item->isResponseChunked()){
baResponse = item->responseBodyRawDataUnChunked();
}else{
baResponse = item->responseBodyRawData();
}
bool isEncrypted = !item->getResponseHeader("Content-Encoding").isEmpty();
if(isEncrypted){
baResponse = gzipDecompress(baResponse);
}
if(baResponse.isEmpty()){
continue;
}
QString pathWithoutQueryAndHash = item->path;
if(item->path.indexOf("?")!=-1){
//http://game.duopao.com/show/project/yangtuoxiadao/yangtuoxiadao.html?urltype=game&app_id=200201191&app_lang=2052&app_nonce=3078456541&app_openid=21AAFDDB7426843957238A9D56F3AD42&app_openkey=8F13C6A479C715CF3655E5B15DD50E15&app_ts=1334802669&pushparam=0&sig=1580B0E043129FFA3BFF396678568010C7B1B09A
pathWithoutQueryAndHash = item->path.left(item->path.indexOf("?"));
qDebug()<<"remove ? to"<<pathWithoutQueryAndHash;
}
if(pathWithoutQueryAndHash.indexOf("&")!=-1){
pathWithoutQueryAndHash = pathWithoutQueryAndHash.left(pathWithoutQueryAndHash.indexOf("&"));
}
QString fileName = "/"+item->host+pathWithoutQueryAndHash;
if(fileName.endsWith("/")){
fileName.append("index.html");
}
qDebug()<<fileName;
fileName.prepend(saveDir);
QFile saveFile(fileName);
QFileInfo fi(saveFile);
QString dirPath = fi.absolutePath();
dir.mkpath(dirPath);
if(saveFile.exists()){
//todo
qDebug()<<"exits"<<fileName;
}
//qDebug()<<fileName;
saveFile.open(QIODevice::WriteOnly);
saveFile.write(baResponse);
saveFile.close();
}
}
}
}