Remove some GTK+-only code.
[metze/wireshark/wip.git] / ui / qt / capture_file.cpp
1 /* capture_file.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later*/
8
9 #include "capture_file.h"
10
11 /*
12  * @file Capture file class
13  *
14  * Wraps the capture_file struct, cfile global, and callbacks.
15  */
16
17 #include "globals.h"
18 capture_file cfile;
19
20 #include "file.h"
21 #include "log.h"
22
23 #include "epan/epan_dissect.h"
24
25 #include "ui/capture.h"
26
27 #include <QFileInfo>
28 #include <QTimer>
29 #include <QDebug>
30
31 CaptureEvent::CaptureEvent(Context ctx, EventType evt) :
32     _ctx(ctx),
33     _evt(evt),
34     _session(Q_NULLPTR)
35 {
36     qDebug() << "CaptureEvent [" << ctx <<"]: " << evt;
37 }
38
39 CaptureEvent::CaptureEvent(Context ctx, EventType evt, QString file) :
40     _ctx(ctx),
41     _evt(evt),
42     _filePath(file),
43     _session(Q_NULLPTR)
44 {
45     qDebug() << "CaptureEvent [" << ctx <<"]: " << evt << " :: File: " << file;
46 }
47
48 CaptureEvent::CaptureEvent(Context ctx, EventType evt, capture_session * session) :
49     _ctx(ctx),
50     _evt(evt),
51     _session(session)
52 {
53     qDebug() << "CaptureEvent [" << ctx <<"]: " << evt << " with session";
54 }
55
56 CaptureEvent::Context CaptureEvent::captureContext() const
57 { return _ctx; }
58
59 CaptureEvent::EventType CaptureEvent::eventType() const
60 { return _evt; }
61
62 QString CaptureEvent::filePath() const
63 { return _filePath; }
64
65 capture_session * CaptureEvent::capSession() const
66 { return _session; }
67
68 // To do:
69 // - Add getters and (if needed) setters:
70 //   - Full filename
71 //   - Capture state (stopped, prepared, running).
72 // - Call common_create_progress_dlg. This would let us manage the stop
73 //   flag here as well as emit progress signals.
74
75 QString CaptureFile::no_capture_file_ = QObject::tr("[no capture file]");
76
77 CaptureFile::CaptureFile(QObject *parent, capture_file *cap_file) :
78     QObject(parent),
79     cap_file_(cap_file),
80     file_name_(no_capture_file_),
81     file_state_(QString())
82 {
83 #ifdef HAVE_LIBPCAP
84     capture_callback_add(captureCallback, (gpointer) this);
85 #endif
86     cf_callback_add(captureFileCallback, (gpointer) this);
87 }
88
89 CaptureFile::~CaptureFile()
90 {
91     cf_callback_remove(captureFileCallback, this);
92 }
93
94 bool CaptureFile::isValid() const
95 {
96     if (cap_file_ && cap_file_->state != FILE_CLOSED) { // XXX FILE_READ_IN_PROGRESS as well?
97         return true;
98     }
99     return false;
100 }
101
102 int CaptureFile::currentRow()
103 {
104     if (isValid())
105         return cap_file_->current_row;
106     return -1;
107 }
108
109 const QString CaptureFile::fileName()
110 {
111     if (isValid()) {
112         QFileInfo cfi(QString::fromUtf8(cap_file_->filename));
113         file_name_ = cfi.fileName();
114     }
115
116     return file_name_;
117 }
118
119 struct _packet_info *CaptureFile::packetInfo()
120 {
121     if (capFile() && capFile()->edt) {
122         return &(capFile()->edt->pi);
123     }
124     return NULL;
125 }
126
127 int CaptureFile::timestampPrecision()
128 {
129     if (capFile() && capFile()->provider.wth) {
130         return wtap_file_tsprec(capFile()->provider.wth);
131     }
132     return WTAP_TSPREC_UNKNOWN;
133 }
134
135 void CaptureFile::retapPackets()
136 {
137     if (cap_file_) {
138         cf_retap_packets(cap_file_);
139     }
140 }
141
142 void CaptureFile::delayedRetapPackets()
143 {
144     QTimer::singleShot(0, this, SLOT(retapPackets()));
145 }
146
147 void CaptureFile::reload()
148 {
149     if (cap_file_ && cap_file_->state == FILE_READ_DONE) {
150         cf_reload(cap_file_);
151     }
152 }
153
154 void CaptureFile::stopLoading()
155 {
156     setCaptureStopFlag(true);
157 }
158
159 capture_file *CaptureFile::globalCapFile()
160 {
161     return &cfile;
162 }
163
164 gpointer CaptureFile::window()
165 {
166     if (cap_file_) return cap_file_->window;
167     return NULL;
168 }
169
170 void CaptureFile::setCaptureStopFlag(bool stop_flag)
171 {
172     if (cap_file_) cap_file_->stop_flag = stop_flag;
173 }
174
175 void CaptureFile::captureFileCallback(gint event, gpointer data, gpointer user_data)
176 {
177     CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
178     if (!capture_file) return;
179
180     capture_file->captureFileEvent(event, data);
181 }
182
183 #ifdef HAVE_LIBPCAP
184 void CaptureFile::captureCallback(gint event, capture_session *cap_session, gpointer user_data)
185 {
186     CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
187     if (!capture_file) return;
188
189     capture_file->captureSessionEvent(event, cap_session);
190 }
191 #endif
192
193 void CaptureFile::captureFileEvent(int event, gpointer data)
194 {
195     switch(event) {
196     case(cf_cb_file_opened):
197         cap_file_ = (capture_file *) data;
198         emit captureEvent(new CaptureEvent(CaptureEvent::File, CaptureEvent::Opened));
199         break;
200     case(cf_cb_file_closing):
201         file_state_ = tr(" [closing]");
202         emit captureEvent(new CaptureEvent(CaptureEvent::File, CaptureEvent::Closing));
203         break;
204     case(cf_cb_file_closed):
205         file_state_ = tr(" [closed]");
206         emit captureEvent(new CaptureEvent(CaptureEvent::File, CaptureEvent::Closed));
207         cap_file_ = NULL;
208         file_name_ = no_capture_file_;
209         file_state_ = QString();
210         break;
211     case(cf_cb_file_read_started):
212         emit captureEvent(new CaptureEvent(CaptureEvent::File, CaptureEvent::Started));
213         break;
214     case(cf_cb_file_read_finished):
215         emit captureEvent(new CaptureEvent(CaptureEvent::File, CaptureEvent::Finished));
216         break;
217     case(cf_cb_file_reload_started):
218         emit captureEvent(new CaptureEvent(CaptureEvent::Reload, CaptureEvent::Started));
219         break;
220     case(cf_cb_file_reload_finished):
221         emit captureEvent(new CaptureEvent(CaptureEvent::Reload, CaptureEvent::Finished));
222         break;
223     case(cf_cb_file_rescan_started):
224         emit captureEvent(new CaptureEvent(CaptureEvent::Rescan, CaptureEvent::Started));
225         break;
226     case(cf_cb_file_rescan_finished):
227         emit captureEvent(new CaptureEvent(CaptureEvent::Rescan, CaptureEvent::Finished));
228         break;
229     case(cf_cb_file_retap_started):
230         emit captureEvent(new CaptureEvent(CaptureEvent::Retap, CaptureEvent::Started));
231         break;
232     case(cf_cb_file_retap_finished):
233         /* Flush any pending tapped packet before emitting captureFileRetapFinished() */
234         emit captureEvent(new CaptureEvent(CaptureEvent::Retap, CaptureEvent::Finished));
235         emit captureEvent(new CaptureEvent(CaptureEvent::Retap, CaptureEvent::Flushed));
236         break;
237     case(cf_cb_file_merge_started):
238         emit captureEvent(new CaptureEvent(CaptureEvent::Merge, CaptureEvent::Started));
239         break;
240     case(cf_cb_file_merge_finished):
241         emit captureEvent(new CaptureEvent(CaptureEvent::Merge, CaptureEvent::Finished));
242         break;
243
244     case(cf_cb_file_fast_save_finished):
245         // gtk/main.c calls main_cf_cb_file_rescan_finished. Should we do
246         // the equivalent?
247         break;
248
249     case(cf_cb_file_save_started):
250     {
251         emit captureEvent(new CaptureEvent(CaptureEvent::Save, CaptureEvent::Started, QString((const char *)data)));
252         break;
253     }
254     case(cf_cb_file_save_finished):
255         emit captureEvent(new CaptureEvent(CaptureEvent::Save, CaptureEvent::Finished));
256         break;
257     case(cf_cb_file_save_failed):
258         emit captureEvent(new CaptureEvent(CaptureEvent::Save, CaptureEvent::Failed));
259         break;
260     case(cf_cb_file_save_stopped):
261         emit captureEvent(new CaptureEvent(CaptureEvent::Save, CaptureEvent::Stopped));
262         break;
263
264     default:
265         qWarning() << "CaptureFile::captureFileCallback: event " << event << " unknown";
266         Q_ASSERT(false);
267         break;
268     }
269 }
270
271 void CaptureFile::captureSessionEvent(int event, capture_session *cap_session)
272 {
273 #ifndef HAVE_LIBPCAP
274     Q_UNUSED(event)
275     Q_UNUSED(cap_session)
276 #else
277     switch(event) {
278     case(capture_cb_capture_prepared):
279         emit captureEvent(new CaptureEvent(CaptureEvent::Capture, CaptureEvent::Prepared, cap_session));
280         cap_file_ = cap_session->cf;
281         break;
282     case(capture_cb_capture_update_started):
283         emit captureEvent(new CaptureEvent(CaptureEvent::Update, CaptureEvent::Started, cap_session));
284         break;
285     case(capture_cb_capture_update_continue):
286         emit captureEvent(new CaptureEvent(CaptureEvent::Update, CaptureEvent::Continued, cap_session));
287         break;
288     case(capture_cb_capture_update_finished):
289         emit captureEvent(new CaptureEvent(CaptureEvent::Update, CaptureEvent::Finished, cap_session));
290         break;
291     case(capture_cb_capture_fixed_started):
292         emit captureEvent(new CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Started, cap_session));
293         break;
294     case(capture_cb_capture_fixed_continue):
295         emit captureEvent(new CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Continued, cap_session));
296         break;
297     case(capture_cb_capture_fixed_finished):
298         emit captureEvent(new CaptureEvent(CaptureEvent::Fixed, CaptureEvent::Finished, cap_session));
299         break;
300     case(capture_cb_capture_stopping):
301         /* Beware: this state won't be called, if the capture child
302              * closes the capturing on it's own! */
303         emit captureEvent(new CaptureEvent(CaptureEvent::Capture, CaptureEvent::Stopping, cap_session));
304         break;
305     case(capture_cb_capture_failed):
306         emit captureEvent(new CaptureEvent(CaptureEvent::Capture, CaptureEvent::Failed, cap_session));
307         break;
308     default:
309         qWarning() << "main_capture_callback: event " << event << " unknown";
310     }
311 #endif // HAVE_LIBPCAP
312 }
313
314 /*
315  * Editor modelines
316  *
317  * Local Variables:
318  * c-basic-offset: 4
319  * tab-width: 8
320  * indent-tabs-mode: nil
321  * End:
322  *
323  * ex: set shiftwidth=4 tabstop=8 expandtab:
324  * :indentSize=4:tabSize=8:noTabs=true:
325  */