forked from bitspalter/DNS-Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_TreeView.cpp
315 lines (244 loc) · 11.3 KB
/
C_TreeView.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
//////////////////////////////////////////////////////////////////////////////////
// [ TreeView_Class_Source ]
//////////////////////////////////////////////////////////////////////////////////
#include "C_TreeView.hpp"
//////////////////////////////////////////////////////////////////////////////////
// [ Konstructor ]
//////////////////////////////////////////////////////////////////////////////////
C_TreeView::C_TreeView(){
//Create the Tree model:
m_refTreeModel = Gtk::ListStore::create(m_Columns);
set_model(m_refTreeModel);
//Add the TreeView's view columns:
//This number will be shown with the default numeric formatting.
append_column("Nr", m_Columns.m_col_nr);
append_column("Time", m_Columns.m_col_time);
append_column("User", m_Columns.m_col_user);
append_column("Request", m_Columns.m_col_request);
//append_column("Result", m_Columns.m_col_result);
////////////////////////////////////////
//TEST
//Gtk::TreeViewColumn* view_column = Gtk::manage(new Gtk::TreeViewColumn("Result"));
//Gtk::CellRendererText* cell = Gtk::manage(new Gtk::CellRendererText());
//Die Spalte Colour soll farbig dargestellt werden
//Zuerst legen wir einen entsprechenden CellRenderer an
//Gtk::CellRendererText *cell = new Gtk::CellRendererText;
//Wir fuegen die Spalte hinzu
int column_count = append_column("Result", m_cell);
//Dann holen wir uns die gerade hinzugefuegte Spalte
Gtk::TreeViewColumn *pColumn = get_column(column_count - 1);
//Gtk::TreeViewColumn* pColumn = get_column(4);
//pColumn->pack_start(m_cell, false);
pColumn->set_cell_data_func(m_cell, sigc::mem_fun(*this, &C_TreeView::on_cell_data_name));
//append_column("Result", *view_column);
////////////////////////////////////////
//Make all the columns reorderable:
//This is not necessary, but it's nice to show the feature.
//You can use TreeView::set_column_drag_function() to more
//finely control column drag and drop.
for(guint i = 0; i < 5; i++){
auto column = get_column(i);
column->set_reorderable();
column->set_resizable(true);
column->set_expand(true);
}
////////////////////////
//Fill popup menu:
auto item = Gtk::manage(new Gtk::MenuItem("_whitelist", true));
m_Popup.append(*item);
auto item2 = Gtk::manage(new Gtk::MenuItem("_add", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_WhiteAdd));
m_Popup_White.append(*item2);
item2 = Gtk::manage(new Gtk::MenuItem("_del", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_WhiteDel));
m_Popup_White.append(*item2);
item2 = Gtk::manage(new Gtk::MenuItem("_edi", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_WhiteEdi));
m_Popup_White.append(*item2);
item->set_submenu(m_Popup_White);
////////////////////////////
item = Gtk::manage(new Gtk::MenuItem("_blacklist", true));
m_Popup.append(*item);
item2 = Gtk::manage(new Gtk::MenuItem("_add", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_BlackAdd));
m_Popup_Black.append(*item2);
item2 = Gtk::manage(new Gtk::MenuItem("_del", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_BlackDel));
m_Popup_Black.append(*item2);
item2 = Gtk::manage(new Gtk::MenuItem("_edi", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_BlackEdi));
m_Popup_Black.append(*item2);
item->set_submenu(m_Popup_Black);
////////////////////////////
item = Gtk::manage(new Gtk::MenuItem("_result", true));
m_Popup.append(*item);
item2 = Gtk::manage(new Gtk::MenuItem("_clear", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_ResultClear));
m_Popup_Result.append(*item2);
item2 = Gtk::manage(new Gtk::MenuItem("_save", true));
item2->signal_activate().connect(sigc::mem_fun(*this, &C_TreeView::on_popup_ResultSave));
m_Popup_Result.append(*item2);
item->set_submenu(m_Popup_Result);
////////////////////////////
m_Popup.accelerate(*this);
m_Popup.show_all(); //Show all menu items when the menu pops up
}
//////////////////////////////////////////////////////////////////////////////////
// [ Destructor ]
//////////////////////////////////////////////////////////////////////////////////
C_TreeView::~C_TreeView(){
}
//////////////////////////////////////////////////////////////////////////////////
// [ setFilter ]
//////////////////////////////////////////////////////////////////////////////////
int C_TreeView::setFilter(C_Filterlist* pBlist, C_Filterlist* pWlist){
if(!pBlist || !pWlist) return(C_TREEVIEW_ERROR);
pBlacklist = pBlist;
pWhitelist = pWlist;
return(C_TREEVIEW_READY);
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_button_press_event ]
//////////////////////////////////////////////////////////////////////////////////
bool C_TreeView::on_button_press_event(GdkEventButton* button_event){
bool return_value = false;
//Call base class, to allow normal handling,
//such as allowing the row to be selected by the right-click:
return_value = TreeView::on_button_press_event(button_event);
//Then do our custom stuff:
if((button_event->type == GDK_BUTTON_PRESS) && (button_event->button == 3)){
//m_Popup.popup_at_pointer((GdkEvent*)button_event);
// Menu::popup_at_pointer() is new in gtkmm 3.22.
// If you have an older revision, try this:
m_Popup.popup(button_event->button, button_event->time);
}
return return_value;
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_WhiteAdd ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_WhiteAdd(){
auto refSelection = get_selection();
if(refSelection){
Gtk::TreeModel::iterator iter = refSelection->get_selected();
if(iter){
Glib::ustring request = (*iter)[m_Columns.m_col_request];
pWhitelist->add(request.c_str());
}
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_WhiteDel ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_WhiteDel(){
auto refSelection = get_selection();
if(refSelection){
Gtk::TreeModel::iterator iter = refSelection->get_selected();
if(iter){
Glib::ustring request = (*iter)[m_Columns.m_col_request];
pWhitelist->del(request.c_str());
}
}
on_popup_WhiteEdi();
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_WhiteEdi ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_WhiteEdi(){
m_refTreeModel->clear();
for(int nWhite = 0; nWhite < pWhitelist->CDA_FilterList.getnItems(); nWhite++){
C_Array<char>* pData = pWhitelist->CDA_FilterList.getpItempData(nWhite);
Gtk::TreeModel::iterator iter = m_refTreeModel->append();
Gtk::TreeModel::Row row = *iter;
row.set_value(0, nWhite + 1);
row.set_value(3, (Glib::ustring)pData->getpBuffer());
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_BlackAdd ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_BlackAdd(){
auto refSelection = get_selection();
if(refSelection){
Gtk::TreeModel::iterator iter = refSelection->get_selected();
if(iter){
Glib::ustring request = (*iter)[m_Columns.m_col_request];
pBlacklist->add(request.c_str());
}
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_BlackDel ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_BlackDel(){
auto refSelection = get_selection();
if(refSelection){
Gtk::TreeModel::iterator iter = refSelection->get_selected();
if(iter){
Glib::ustring request = (*iter)[m_Columns.m_col_request];
pBlacklist->del(request.c_str());
}
}
on_popup_BlackEdi();
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_BlackEdi ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_BlackEdi(){
m_refTreeModel->clear();
for(int nBlack = 0; nBlack < pBlacklist->CDA_FilterList.getnItems(); nBlack++){
C_Array<char>* pData = pBlacklist->CDA_FilterList.getpItempData(nBlack);
Gtk::TreeModel::iterator iter = m_refTreeModel->append();
Gtk::TreeModel::Row row = *iter;
row.set_value(0, nBlack + 1);
row.set_value(3, (Glib::ustring)pData->getpBuffer());
}
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_ResultClear ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_ResultClear(){
m_refTreeModel->clear();
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_popup_ResultSave ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_popup_ResultSave(){
fstream fOutput;
fOutput.open("23.txt", ios_base::out | ios_base::binary);
if(!fOutput.is_open()) return;
typedef Gtk::TreeModel::Children type_children; //minimise code length.
type_children children = m_refTreeModel->children();
int nRow = 0;
for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter){
fOutput << nRow++ << " "
<< (*iter)[m_Columns.m_col_time] << " "
<< (*iter)[m_Columns.m_col_user] << " "
<< (*iter)[m_Columns.m_col_request] << " "
<< (*iter)[m_Columns.m_col_result] << endl;
}
fOutput.close();
}
//////////////////////////////////////////////////////////////////////////////////
// [ on_cell_data_name ]
//////////////////////////////////////////////////////////////////////////////////
void C_TreeView::on_cell_data_name(Gtk::CellRenderer* renderer, const Gtk::TreeModel::iterator& iter){
//Get the value from the model and show it appropriately in the view:
Gtk::TreeModel::Row row = *iter;
Glib::ustring result = row[m_Columns.m_col_result];
//if(id == SOME_MAGIC_NUMBER)
//result = "<b>" + result + "</b>";
Glib::ustring sKey = "cell-background";
Glib::ustring sVal = "magenta";
//pNameRend->set_property(sKey, sVal);
Gdk::RGBA putzi = Gdk::RGBA("#2baf33");
Gdk::RGBA patzi = Gdk::RGBA("#af2b33");
Gtk::CellRendererText* text_renderer = dynamic_cast<Gtk::CellRendererText*>(renderer);
if(text_renderer){
//cout << "on_cell_data_name:" << (result == "BLOCK" ? patzi : putzi) << endl;
text_renderer->property_foreground_rgba() = (result == "BLOCK" ? patzi : putzi);
text_renderer->property_text() = result;
//text_renderer->set_property(sKey, sVal);
//text_renderer->property_markup() = result;
}
}