Qt: Add a CaptureFile class.
[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  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "capture_file.h"
23
24 /*
25  * @file Capture file class
26  *
27  * Wraps the capture_file struct, cfile global, and callbacks.
28  */
29
30 #include "globals.h"
31 capture_file cfile;
32
33 #include "file.h"
34 #include "log.h"
35
36 #include "ui/capture.h"
37
38 // To do:
39 // - Add isValid or isOpen methods instead of checking capFile() for NULL.
40 // - Add getters and (if needed) setters:
41 //   - Base filename
42 //   - Full filename
43
44 #include <QDebug>
45
46 CaptureFile::CaptureFile(QObject *parent, capture_file *cap_file) :
47     QObject(parent),
48     cap_file_(cap_file)
49 {
50 #ifdef HAVE_LIBPCAP
51     capture_callback_add(captureCallback, (gpointer) this);
52 #endif
53     cf_callback_add(captureFileCallback, (gpointer) this);
54 }
55
56 CaptureFile::~CaptureFile()
57 {
58     cf_callback_remove(captureFileCallback, this);
59 }
60
61 capture_file *CaptureFile::globalCapFile()
62 {
63     return &cfile;
64 }
65
66 gpointer CaptureFile::window()
67 {
68     if (cap_file_) return cap_file_->window;
69     return NULL;
70 }
71
72 void CaptureFile::captureFileCallback(gint event, gpointer data, gpointer user_data)
73 {
74     CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
75     if (!capture_file) return;
76
77     capture_file->captureFileEvent(event, data);
78 }
79
80 #ifdef HAVE_LIBPCAP
81 void CaptureFile::captureCallback(gint event, capture_session *cap_session, gpointer user_data)
82 {
83     CaptureFile *capture_file = static_cast<CaptureFile *>(user_data);
84     if (!capture_file) return;
85
86     capture_file->captureEvent(event, cap_session);
87 }
88 #endif
89
90 void CaptureFile::captureFileEvent(int event, gpointer data)
91 {
92     qDebug() << "=cfe" << event << data;
93     switch(event) {
94     case(cf_cb_file_opened):
95         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Opened");
96         cap_file_ = (capture_file *) data;
97         qDebug() << "=cfe fo" << cap_file_;
98         emit captureFileOpened();
99         break;
100     case(cf_cb_file_closing):
101         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Closing");
102         emit captureFileClosing();
103         break;
104     case(cf_cb_file_closed):
105         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Closed");
106         qDebug() << "=cfe fc" << cap_file_;
107         emit captureFileClosed();
108         cap_file_ = NULL;
109         break;
110     case(cf_cb_file_read_started):
111         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Read started");
112         emit captureFileReadStarted();
113         break;
114     case(cf_cb_file_read_finished):
115         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Read finished");
116         emit captureFileReadFinished();
117         break;
118     case(cf_cb_file_reload_started):
119         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Reload started");
120         emit captureFileReadStarted();
121         break;
122     case(cf_cb_file_reload_finished):
123         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Reload finished");
124         emit captureFileReadFinished();
125         break;
126
127     case(cf_cb_packet_selected):
128     case(cf_cb_packet_unselected):
129     case(cf_cb_field_unselected):
130         // Signals and slots handled elsewhere.
131         break;
132
133 //    case(cf_cb_file_save_started): // data = string
134 //        g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Save started");
135 //        break;
136 //    case(cf_cb_file_save_finished):
137 //        g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Save finished");
138 //        break;
139 //    case(cf_cb_file_save_failed):
140 //        g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: Save failed");
141 //        break;
142     default:
143         g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: main_cf_callback %d %p", event, data);
144         g_warning("CaptureFile::captureFileCallback: event %u unknown", event);
145         break;
146     }
147 }
148
149 void CaptureFile::captureEvent(int event, capture_session *cap_session)
150 {
151 #ifndef HAVE_LIBPCAP
152     Q_UNUSED(event)
153     Q_UNUSED(cap_session)
154 #else
155     qDebug() << "=ce" << event << cap_session->cf;
156     switch(event) {
157     case(capture_cb_capture_prepared):
158         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture prepared");
159         emit captureCapturePrepared(cap_session);
160         cap_file_ = cap_session->cf;
161         break;
162     case(capture_cb_capture_update_started):
163         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture update started");
164         emit captureCaptureUpdateStarted(cap_session);
165         break;
166     case(capture_cb_capture_update_continue):
167         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture update continue");
168         emit captureCaptureUpdateContinue(cap_session);
169         break;
170     case(capture_cb_capture_update_finished):
171         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture update finished");
172         emit captureCaptureUpdateFinished(cap_session);
173         break;
174     case(capture_cb_capture_fixed_started):
175         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture fixed started");
176         emit captureCaptureFixedStarted(cap_session);
177         break;
178     case(capture_cb_capture_fixed_continue):
179         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture fixed continue");
180         break;
181     case(capture_cb_capture_fixed_finished):
182         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture fixed finished");
183         emit captureCaptureFixedFinished(cap_session);
184         break;
185     case(capture_cb_capture_stopping):
186         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture stopping");
187         /* Beware: this state won't be called, if the capture child
188              * closes the capturing on it's own! */
189         emit captureCaptureStopping(cap_session);
190         break;
191     case(capture_cb_capture_failed):
192         g_log(LOG_DOMAIN_MAIN, G_LOG_LEVEL_DEBUG, "Callback: capture failed");
193         emit captureCaptureFailed(cap_session);
194         break;
195     default:
196         g_warning("main_capture_callback: event %u unknown", event);
197     }
198 #endif // HAVE_LIBPCAP
199 }
200
201 /*
202  * Editor modelines
203  *
204  * Local Variables:
205  * c-basic-offset: 4
206  * tab-width: 8
207  * indent-tabs-mode: nil
208  * End:
209  *
210  * ex: set shiftwidth=4 tabstop=8 expandtab:
211  * :indentSize=4:tabSize=8:noTabs=true:
212  */